From 4f8a9d98c582d8cf71ffa334ee81196a94a121a3 Mon Sep 17 00:00:00 2001 From: Ruslan Tkhakokhov Date: Wed, 1 Apr 2020 15:09:42 +0100 Subject: Try to reapply live wallpaper component once it's installed See go/br-fr-live-wallpaper for context Bug: 30205325 Test: 1. Set live wallpaper (from pre-installed / from a 3p app Muzei) 2. Run Backup Now 3. Wipe the device 4. Restore (regular/deferred) 5. Verify the same live wallpaper is set Change-Id: Iacdb4e4b4bd895740f0a7f72f075a24fae1ae368 --- .../wallpaperbackup/WallpaperBackupAgent.java | 93 +++++-- .../wallpaperbackup/WallpaperBackupAgentTest.java | 282 +++++++++++++++++++++ .../tests/WallpaperBackupAgentTest.java | 185 -------------- 3 files changed, 358 insertions(+), 202 deletions(-) create mode 100644 packages/WallpaperBackup/test/src/com/android/wallpaperbackup/WallpaperBackupAgentTest.java delete mode 100644 packages/WallpaperBackup/test/src/com/android/wallpaperbackup/tests/WallpaperBackupAgentTest.java diff --git a/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java b/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java index 93bffe9f54e4..52a82dd2a156 100644 --- a/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java +++ b/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java @@ -36,10 +36,12 @@ import android.os.FileUtils; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.os.UserHandle; +import android.provider.Settings; import android.util.Slog; import android.util.Xml; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.content.PackageMonitor; import libcore.io.IoUtils; @@ -261,22 +263,7 @@ public class WallpaperBackupAgent extends BackupAgent { // And reset to the wallpaper service we should be using ComponentName wpService = parseWallpaperComponent(infoStage, "wp"); - if (servicePackageExists(wpService)) { - Slog.i(TAG, "Using wallpaper service " + wpService); - mWm.setWallpaperComponent(wpService, UserHandle.USER_SYSTEM); - if (!lockImageStage.exists()) { - // We have a live wallpaper and no static lock image, - // allow live wallpaper to show "through" on lock screen. - mWm.clear(FLAG_LOCK); - } - } else { - // If we've restored a live wallpaper, but the component doesn't exist, - // we should log it as an error so we can easily identify the problem - // in reports from users - if (wpService != null) { - Slog.e(TAG, "Wallpaper service " + wpService + " isn't available."); - } - } + updateWallpaperComponent(wpService, !lockImageStage.exists()); } catch (Exception e) { Slog.e(TAG, "Unable to restore wallpaper: " + e.getMessage()); } finally { @@ -293,6 +280,28 @@ public class WallpaperBackupAgent extends BackupAgent { } } + @VisibleForTesting + void updateWallpaperComponent(ComponentName wpService, boolean applyToLock) throws IOException { + if (servicePackageExists(wpService)) { + Slog.i(TAG, "Using wallpaper service " + wpService); + mWm.setWallpaperComponent(wpService, UserHandle.USER_SYSTEM); + if (applyToLock) { + // We have a live wallpaper and no static lock image, + // allow live wallpaper to show "through" on lock screen. + mWm.clear(FLAG_LOCK); + } + } else { + // If we've restored a live wallpaper, but the component doesn't exist, + // we should log it as an error so we can easily identify the problem + // in reports from users + if (wpService != null) { + applyComponentAtInstall(wpService, applyToLock); + Slog.w(TAG, "Wallpaper service " + wpService + " isn't available. " + + " Will try to apply later"); + } + } + } + private void restoreFromStage(File stage, File info, String hintTag, int which) throws IOException { if (stage.exists()) { @@ -372,7 +381,8 @@ public class WallpaperBackupAgent extends BackupAgent { return (value == null) ? defValue : Integer.parseInt(value); } - private boolean servicePackageExists(ComponentName comp) { + @VisibleForTesting + boolean servicePackageExists(ComponentName comp) { try { if (comp != null) { final IPackageManager pm = AppGlobals.getPackageManager(); @@ -401,4 +411,53 @@ public class WallpaperBackupAgent extends BackupAgent { throws IOException { // Intentionally blank } + + private void applyComponentAtInstall(ComponentName componentName, boolean applyToLock) { + PackageMonitor packageMonitor = getWallpaperPackageMonitor(componentName, applyToLock); + packageMonitor.register(getBaseContext(), null, UserHandle.ALL, true); + } + + @VisibleForTesting + PackageMonitor getWallpaperPackageMonitor(ComponentName componentName, boolean applyToLock) { + return new PackageMonitor() { + @Override + public void onPackageAdded(String packageName, int uid) { + if (!isDeviceInRestore()) { + // We don't want to reapply the wallpaper outside a restore. + unregister(); + return; + } + + if (componentName.getPackageName().equals(packageName)) { + Slog.d(TAG, "Applying component " + componentName); + mWm.setWallpaperComponent(componentName); + if (applyToLock) { + try { + mWm.clear(FLAG_LOCK); + } catch (IOException e) { + Slog.w(TAG, "Failed to apply live wallpaper to lock screen: " + e); + } + } + // We're only expecting to restore the wallpaper component once. + unregister(); + } + } + }; + } + + @VisibleForTesting + boolean isDeviceInRestore() { + try { + boolean isInSetup = Settings.Secure.getInt(getBaseContext().getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE) == 0; + boolean isInDeferredSetup = Settings.Secure.getInt(getBaseContext() + .getContentResolver(), + Settings.Secure.USER_SETUP_PERSONALIZATION_STATE) == + Settings.Secure.USER_SETUP_PERSONALIZATION_STARTED; + return isInSetup || isInDeferredSetup; + } catch (Settings.SettingNotFoundException e) { + Slog.w(TAG, "Failed to check if the user is in restore: " + e); + return false; + } + } } \ No newline at end of file diff --git a/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/WallpaperBackupAgentTest.java b/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/WallpaperBackupAgentTest.java new file mode 100644 index 000000000000..4367075abc74 --- /dev/null +++ b/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/WallpaperBackupAgentTest.java @@ -0,0 +1,282 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.wallpaperbackup; + +import static android.app.WallpaperManager.FLAG_LOCK; +import static android.app.WallpaperManager.FLAG_SYSTEM; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.WallpaperManager; +import android.app.backup.FullBackupDataOutput; +import android.content.ComponentName; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.UserHandle; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; + +import com.android.internal.content.PackageMonitor; +import com.android.wallpaperbackup.WallpaperBackupAgent; +import com.android.wallpaperbackup.utils.ContextWithServiceOverrides; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@RunWith(AndroidJUnit4.class) +public class WallpaperBackupAgentTest { + private static final String SYSTEM_GENERATION = "system_gen"; + private static final String LOCK_GENERATION = "lock_gen"; + private static final String TEST_WALLPAPER_PACKAGE = "wallpaper_package"; + + private static final int TEST_SYSTEM_WALLPAPER_ID = 1; + private static final int TEST_LOCK_WALLPAPER_ID = 2; + + @Mock private FullBackupDataOutput mOutput; + @Mock private WallpaperManager mWallpaperManager; + @Mock private SharedPreferences mSharedPreferences; + @Mock private SharedPreferences.Editor mSharedPreferenceEditor; + @Mock private Context mMockContext; + + @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); + + private ContextWithServiceOverrides mContext; + private IsolatedWallpaperBackupAgent mWallpaperBackupAgent; + private ComponentName mWallpaperComponent; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + when(mSharedPreferences.edit()).thenReturn(mSharedPreferenceEditor); + when(mSharedPreferenceEditor.putInt(anyString(), anyInt())) + .thenReturn(mSharedPreferenceEditor); + doNothing().when(mSharedPreferenceEditor).apply(); + + mContext = new ContextWithServiceOverrides(ApplicationProvider.getApplicationContext()); + mContext.injectSystemService(WallpaperManager.class, mWallpaperManager); + mContext.setSharedPreferencesOverride(mSharedPreferences); + + mWallpaperBackupAgent = new IsolatedWallpaperBackupAgent(mTemporaryFolder.getRoot()); + mWallpaperBackupAgent.attach(mContext); + mWallpaperBackupAgent.onCreate(); + + mWallpaperComponent = new ComponentName(TEST_WALLPAPER_PACKAGE, ""); + } + + @Test + public void testOnFullBackup_withNoChanges_onlyBacksUpEmptyFile() throws IOException { + mockBackedUpState(); + mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); + + mWallpaperBackupAgent.onFullBackup(mOutput); + + assertThat(mWallpaperBackupAgent.mBackedUpFiles.size()).isEqualTo(1); + assertThat(mWallpaperBackupAgent.mBackedUpFiles.get(0).getName()).isEqualTo("empty"); + } + + @Test + public void testOnFullBackup_withOnlyChangedSystem_updatesTheSharedPreferences() + throws IOException { + mockSystemWallpaperReadyToBackUp(); + mockUnbackedUpState(); + mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); + + mWallpaperBackupAgent.onFullBackup(mOutput); + + verify(mSharedPreferenceEditor).putInt(eq(SYSTEM_GENERATION), eq(TEST_SYSTEM_WALLPAPER_ID)); + } + + @Test + public void testOnFullBackup_withLockChangedToMatchSystem_updatesTheSharedPreferences() + throws IOException { + mockBackedUpState(); + mockSystemWallpaperReadyToBackUp(); + mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, -1); + + mWallpaperBackupAgent.onFullBackup(mOutput); + + InOrder inOrder = inOrder(mSharedPreferenceEditor); + inOrder.verify(mSharedPreferenceEditor) + .putInt(eq(SYSTEM_GENERATION), eq(TEST_SYSTEM_WALLPAPER_ID)); + inOrder.verify(mSharedPreferenceEditor).apply(); + inOrder.verify(mSharedPreferenceEditor).putInt(eq(LOCK_GENERATION), eq(-1)); + inOrder.verify(mSharedPreferenceEditor).apply(); + } + + @Test + public void updateWallpaperComponent_doesApplyLater() throws IOException { + mWallpaperBackupAgent.mIsDeviceInRestore = true; + + mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent, + /* applyToLock */ true); + + // Imitate wallpaper component installation. + mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE, + /* uid */0); + + verify(mWallpaperManager, times(1)).setWallpaperComponent(mWallpaperComponent); + verify(mWallpaperManager, times(1)).clear(eq(FLAG_LOCK)); + } + + @Test + public void updateWallpaperComponent_applyToLockFalse_doesApplyLaterOnlyToMainScreen() + throws IOException { + mWallpaperBackupAgent.mIsDeviceInRestore = true; + + mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent, + /* applyToLock */ false); + + // Imitate wallpaper component installation. + mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE, + /* uid */0); + + verify(mWallpaperManager, times(1)).setWallpaperComponent(mWallpaperComponent); + verify(mWallpaperManager, never()).clear(eq(FLAG_LOCK)); + } + + @Test + public void updateWallpaperComponent_deviceNotInRestore_doesNotApply() + throws IOException { + mWallpaperBackupAgent.mIsDeviceInRestore = false; + + mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent, + /* applyToLock */ true); + + // Imitate wallpaper component installation. + mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(TEST_WALLPAPER_PACKAGE, + /* uid */0); + + verify(mWallpaperManager, never()).setWallpaperComponent(mWallpaperComponent); + verify(mWallpaperManager, never()).clear(eq(FLAG_LOCK)); + } + + @Test + public void updateWallpaperComponent_differentPackageInstalled_doesNotApply() + throws IOException { + mWallpaperBackupAgent.mIsDeviceInRestore = false; + + mWallpaperBackupAgent.updateWallpaperComponent(mWallpaperComponent, + /* applyToLock */ true); + + // Imitate "wrong" wallpaper component installation. + mWallpaperBackupAgent.mWallpaperPackageMonitor.onPackageAdded(/* packageName */"", + /* uid */0); + + verify(mWallpaperManager, never()).setWallpaperComponent(mWallpaperComponent); + verify(mWallpaperManager, never()).clear(eq(FLAG_LOCK)); + } + + private void mockUnbackedUpState() { + mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); + when(mSharedPreferences.getInt(eq(SYSTEM_GENERATION), eq(-1))).thenReturn(-1); + when(mSharedPreferences.getInt(eq(LOCK_GENERATION), eq(-1))).thenReturn(-1); + } + + private void mockBackedUpState() { + when(mSharedPreferences.getInt(eq(SYSTEM_GENERATION), eq(-1))) + .thenReturn(TEST_SYSTEM_WALLPAPER_ID); + when(mSharedPreferences.getInt(eq(LOCK_GENERATION), eq(-1))) + .thenReturn(TEST_LOCK_WALLPAPER_ID); + } + + private void mockCurrentWallpapers(int systemWallpaperId, int lockWallpaperId) { + when(mWallpaperManager.getWallpaperIdForUser(eq(FLAG_SYSTEM), eq(UserHandle.USER_SYSTEM))) + .thenReturn(systemWallpaperId); + when(mWallpaperManager.getWallpaperIdForUser(eq(FLAG_LOCK), eq(UserHandle.USER_SYSTEM))) + .thenReturn(lockWallpaperId); + when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_SYSTEM))).thenReturn(true); + when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_LOCK))).thenReturn(true); + } + + private void mockSystemWallpaperReadyToBackUp() throws IOException { + // Create a system wallpaper file + mTemporaryFolder.newFile("wallpaper_orig"); + // Create staging file to simulate he wallpaper being ready to back up + new File(mContext.getFilesDir(), "wallpaper-stage").createNewFile(); + } + + private class IsolatedWallpaperBackupAgent extends WallpaperBackupAgent { + File mWallpaperBaseDirectory; + List mBackedUpFiles = new ArrayList<>(); + PackageMonitor mWallpaperPackageMonitor; + boolean mIsDeviceInRestore = false; + + IsolatedWallpaperBackupAgent(File wallpaperBaseDirectory) { + mWallpaperBaseDirectory = wallpaperBaseDirectory; + } + + @Override + protected File getWallpaperDir() { + return mWallpaperBaseDirectory; + } + + @Override + protected void backupFile(File file, FullBackupDataOutput data) { + mBackedUpFiles.add(file); + } + + @Override + public SharedPreferences getSharedPreferences(File file, int mode) { + return mSharedPreferences; + } + + @Override + boolean servicePackageExists(ComponentName comp) { + return false; + } + + @Override + boolean isDeviceInRestore() { + return mIsDeviceInRestore; + } + + @Override + PackageMonitor getWallpaperPackageMonitor(ComponentName componentName, + boolean applyToLock) { + mWallpaperPackageMonitor = super.getWallpaperPackageMonitor(componentName, applyToLock); + return mWallpaperPackageMonitor; + } + + @Override + public Context getBaseContext() { + return mMockContext; + } + } +} diff --git a/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/tests/WallpaperBackupAgentTest.java b/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/tests/WallpaperBackupAgentTest.java deleted file mode 100644 index 255fdef36cd7..000000000000 --- a/packages/WallpaperBackup/test/src/com/android/wallpaperbackup/tests/WallpaperBackupAgentTest.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.wallpaperbackup.tests; - -import static android.app.WallpaperManager.FLAG_LOCK; -import static android.app.WallpaperManager.FLAG_SYSTEM; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.inOrder; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.app.WallpaperManager; -import android.app.backup.FullBackupDataOutput; -import android.content.SharedPreferences; -import android.os.UserHandle; - -import androidx.test.core.app.ApplicationProvider; -import androidx.test.runner.AndroidJUnit4; - -import com.android.wallpaperbackup.WallpaperBackupAgent; -import com.android.wallpaperbackup.utils.ContextWithServiceOverrides; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -@RunWith(AndroidJUnit4.class) -public class WallpaperBackupAgentTest { - private static final String SYSTEM_GENERATION = "system_gen"; - private static final String LOCK_GENERATION = "lock_gen"; - - private static final int TEST_SYSTEM_WALLPAPER_ID = 1; - private static final int TEST_LOCK_WALLPAPER_ID = 2; - - @Mock private FullBackupDataOutput mOutput; - @Mock private WallpaperManager mWallpaperManager; - @Mock private SharedPreferences mSharedPreferences; - @Mock private SharedPreferences.Editor mSharedPreferenceEditor; - - @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); - - private ContextWithServiceOverrides mContext; - private IsolatedWallpaperBackupAgent mWallpaperBackupAgent; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - - when(mSharedPreferences.edit()).thenReturn(mSharedPreferenceEditor); - when(mSharedPreferenceEditor.putInt(anyString(), anyInt())) - .thenReturn(mSharedPreferenceEditor); - doNothing().when(mSharedPreferenceEditor).apply(); - - mContext = new ContextWithServiceOverrides(ApplicationProvider.getApplicationContext()); - mContext.injectSystemService(WallpaperManager.class, mWallpaperManager); - mContext.setSharedPreferencesOverride(mSharedPreferences); - - mWallpaperBackupAgent = new IsolatedWallpaperBackupAgent(mTemporaryFolder.getRoot()); - mWallpaperBackupAgent.attach(mContext); - mWallpaperBackupAgent.onCreate(); - } - - @Test - public void testOnFullBackup_withNoChanges_onlyBacksUpEmptyFile() throws IOException { - mockBackedUpState(); - mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); - - mWallpaperBackupAgent.onFullBackup(mOutput); - - assertThat(mWallpaperBackupAgent.mBackedUpFiles.size()).isEqualTo(1); - assertThat(mWallpaperBackupAgent.mBackedUpFiles.get(0).getName()).isEqualTo("empty"); - } - - @Test - public void testOnFullBackup_withOnlyChangedSystem_updatesTheSharedPreferences() - throws IOException { - mockSystemWallpaperReadyToBackUp(); - mockUnbackedUpState(); - mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); - - mWallpaperBackupAgent.onFullBackup(mOutput); - - verify(mSharedPreferenceEditor).putInt(eq(SYSTEM_GENERATION), eq(TEST_SYSTEM_WALLPAPER_ID)); - } - - @Test - public void testOnFullBackup_withLockChangedToMatchSystem_updatesTheSharedPreferences() - throws IOException { - mockBackedUpState(); - mockSystemWallpaperReadyToBackUp(); - mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, -1); - - mWallpaperBackupAgent.onFullBackup(mOutput); - - InOrder inOrder = inOrder(mSharedPreferenceEditor); - inOrder.verify(mSharedPreferenceEditor) - .putInt(eq(SYSTEM_GENERATION), eq(TEST_SYSTEM_WALLPAPER_ID)); - inOrder.verify(mSharedPreferenceEditor).apply(); - inOrder.verify(mSharedPreferenceEditor).putInt(eq(LOCK_GENERATION), eq(-1)); - inOrder.verify(mSharedPreferenceEditor).apply(); - } - - private void mockUnbackedUpState() { - mockCurrentWallpapers(TEST_SYSTEM_WALLPAPER_ID, TEST_LOCK_WALLPAPER_ID); - when(mSharedPreferences.getInt(eq(SYSTEM_GENERATION), eq(-1))).thenReturn(-1); - when(mSharedPreferences.getInt(eq(LOCK_GENERATION), eq(-1))).thenReturn(-1); - } - - private void mockBackedUpState() { - when(mSharedPreferences.getInt(eq(SYSTEM_GENERATION), eq(-1))) - .thenReturn(TEST_SYSTEM_WALLPAPER_ID); - when(mSharedPreferences.getInt(eq(LOCK_GENERATION), eq(-1))) - .thenReturn(TEST_LOCK_WALLPAPER_ID); - } - - private void mockCurrentWallpapers(int systemWallpaperId, int lockWallpaperId) { - when(mWallpaperManager.getWallpaperIdForUser(eq(FLAG_SYSTEM), eq(UserHandle.USER_SYSTEM))) - .thenReturn(systemWallpaperId); - when(mWallpaperManager.getWallpaperIdForUser(eq(FLAG_LOCK), eq(UserHandle.USER_SYSTEM))) - .thenReturn(lockWallpaperId); - when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_SYSTEM))).thenReturn(true); - when(mWallpaperManager.isWallpaperBackupEligible(eq(FLAG_LOCK))).thenReturn(true); - } - - private void mockSystemWallpaperReadyToBackUp() throws IOException { - // Create a system wallpaper file - mTemporaryFolder.newFile("wallpaper_orig"); - // Create staging file to simulate he wallpaper being ready to back up - new File(mContext.getFilesDir(), "wallpaper-stage").createNewFile(); - } - - private class IsolatedWallpaperBackupAgent extends WallpaperBackupAgent { - File mWallpaperBaseDirectory; - List mBackedUpFiles = new ArrayList<>(); - - IsolatedWallpaperBackupAgent(File wallpaperBaseDirectory) { - mWallpaperBaseDirectory = wallpaperBaseDirectory; - } - - @Override - protected File getWallpaperDir() { - return mWallpaperBaseDirectory; - } - - @Override - protected void backupFile(File file, FullBackupDataOutput data) { - mBackedUpFiles.add(file); - } - - @Override - public SharedPreferences getSharedPreferences(File file, int mode) { - return mSharedPreferences; - } - } -} -- cgit v1.2.3-59-g8ed1b