Remove unsupported per-app locales after the LocaleConfig override

Remove the per-app locales when they do not exist in the LocaleConfig after setting/removing the LocaleConfig override

Bug: 270104902
Test: atest LocaleManagerTests
      atest AppLocalesBackupTest
      atest LocaleConfigAppUpdateTest
      atest LocaleManagerOverrideLocaleConfigTest
Change-Id: Iabefd7e2a463d88c94eb63b4d12efe91b6b5e5c2
This commit is contained in:
Josh Hou
2023-03-07 10:14:54 +00:00
parent aeba685393
commit 42a9793367
7 changed files with 100 additions and 409 deletions

View File

@@ -1,180 +0,0 @@
/*
* Copyright (C) 2022 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.server.locales;
import android.app.LocaleConfig;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.LocaleList;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.FeatureFlagUtils;
import android.util.Log;
import android.util.Slog;
import com.android.internal.annotations.VisibleForTesting;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
/**
* Track when a app is being updated.
*/
public class AppUpdateTracker {
private static final String TAG = "AppUpdateTracker";
private final Context mContext;
private final LocaleManagerService mLocaleManagerService;
private final LocaleManagerBackupHelper mBackupHelper;
AppUpdateTracker(Context context, LocaleManagerService localeManagerService,
LocaleManagerBackupHelper backupHelper) {
mContext = context;
mLocaleManagerService = localeManagerService;
mBackupHelper = backupHelper;
}
/**
* <p><b>Note:</b> This is invoked by service's common monitor
* {@link LocaleManagerServicePackageMonitor#onPackageUpdateFinished} when a package is upgraded
* on device.
*/
public void onPackageUpdateFinished(String packageName, int uid) {
Log.d(TAG, "onPackageUpdateFinished " + packageName);
int userId = UserHandle.getUserId(uid);
cleanApplicationLocalesIfNeeded(packageName, userId);
}
/**
* When the user has set per-app locales for a specific application from a delegate selector,
* and then the LocaleConfig of that application is removed in the upgraded version, the per-app
* locales needs to be reset to system default locales to avoid the user being unable to change
* system locales setting.
*/
private void cleanApplicationLocalesIfNeeded(String packageName, int userId) {
Set<String> packageNames = new ArraySet<>();
SharedPreferences delegateAppLocalePackages = mBackupHelper.getPersistedInfo();
if (delegateAppLocalePackages != null) {
packageNames = delegateAppLocalePackages.getStringSet(Integer.toString(userId),
new ArraySet<>());
}
try {
LocaleList appLocales = mLocaleManagerService.getApplicationLocales(packageName,
userId);
if (appLocales.isEmpty() || isLocalesExistedInLocaleConfig(appLocales, packageName,
userId) || !packageNames.contains(packageName)) {
return;
}
} catch (RemoteException | IllegalArgumentException e) {
Slog.e(TAG, "Exception when getting locales for " + packageName, e);
return;
}
Slog.d(TAG, "Clear app locales for " + packageName);
try {
mLocaleManagerService.setApplicationLocales(packageName, userId,
LocaleList.forLanguageTags(""), false);
} catch (RemoteException | IllegalArgumentException e) {
Slog.e(TAG, "Could not clear locales for " + packageName, e);
}
}
/**
* Check whether the LocaleConfig is existed and the per-app locales is presented in the
* LocaleConfig file after the application is upgraded.
*/
private boolean isLocalesExistedInLocaleConfig(LocaleList appLocales, String packageName,
int userId) {
LocaleList packageLocalesList = getPackageLocales(packageName, userId);
HashSet<Locale> packageLocales = new HashSet<>();
if (isSettingsAppLocalesOptIn()) {
if (packageLocalesList == null || packageLocalesList.isEmpty()) {
// The app locale feature is not enabled by the app
Slog.d(TAG, "opt-in: the app locale feature is not enabled");
return false;
}
} else {
if (packageLocalesList != null && packageLocalesList.isEmpty()) {
// The app locale feature is not enabled by the app
Slog.d(TAG, "opt-out: the app locale feature is not enabled");
return false;
}
}
if (packageLocalesList != null && !packageLocalesList.isEmpty()) {
// The app has added the supported locales into the LocaleConfig
for (int i = 0; i < packageLocalesList.size(); i++) {
packageLocales.add(packageLocalesList.get(i));
}
if (!matchesLocale(packageLocales, appLocales)) {
// The set app locales do not match with the list of app supported locales
Slog.d(TAG, "App locales: " + appLocales.toLanguageTags()
+ " are not existed in the supported locale list");
return false;
}
}
return true;
}
/**
* Get locales from LocaleConfig.
*/
@VisibleForTesting
public LocaleList getPackageLocales(String packageName, int userId) {
try {
LocaleConfig localeConfig = new LocaleConfig(
mContext.createPackageContextAsUser(packageName, 0, UserHandle.of(userId)));
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
return localeConfig.getSupportedLocales();
}
} catch (PackageManager.NameNotFoundException e) {
Slog.e(TAG, "Can not found the package name : " + packageName + " / " + e);
}
return null;
}
/**
* Check whether the feature to show per-app locales list in Settings is enabled.
*/
@VisibleForTesting
public boolean isSettingsAppLocalesOptIn() {
return FeatureFlagUtils.isEnabled(mContext,
FeatureFlagUtils.SETTINGS_APP_LOCALE_OPT_IN_ENABLED);
}
private boolean matchesLocale(HashSet<Locale> supported, LocaleList appLocales) {
if (supported.size() <= 0 || appLocales.size() <= 0) {
return true;
}
for (int i = 0; i < appLocales.size(); i++) {
final Locale appLocale = appLocales.get(i);
if (supported.stream().anyMatch(
locale -> LocaleList.matchesLanguageAndScript(locale, appLocale))) {
return true;
}
}
return false;
}
}

View File

@@ -22,6 +22,7 @@ import static com.android.server.locales.LocaleManagerService.DEBUG;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.app.LocaleConfig;
import android.app.backup.BackupManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -296,6 +297,16 @@ class LocaleManagerBackupHelper {
}
}
/**
* <p><b>Note:</b> This is invoked by service's common monitor
* {@link LocaleManagerServicePackageMonitor#onPackageUpdateFinished} when a package is upgraded
* on device.
*/
void onPackageUpdateFinished(String packageName, int uid) {
int userId = UserHandle.getUserId(uid);
cleanApplicationLocalesIfNeeded(packageName, userId);
}
/**
* <p><b>Note:</b> This is invoked by service's common monitor
* {@link LocaleManagerServicePackageMonitor#onPackageDataCleared} when a package's data
@@ -608,4 +619,52 @@ class LocaleManagerBackupHelper {
Slog.e(TAG, "failed to commit locale setter info");
}
}
boolean areLocalesSetFromDelegate(@UserIdInt int userId, String packageName) {
if (mDelegateAppLocalePackages == null) {
Slog.w(TAG, "Failed to persist data into the shared preference!");
return false;
}
String user = Integer.toString(userId);
Set<String> packageNames = new ArraySet<>(
mDelegateAppLocalePackages.getStringSet(user, new ArraySet<>()));
return packageNames.contains(packageName);
}
/**
* When the user has set per-app locales for a specific application from a delegate selector,
* and then the LocaleConfig of that application is removed in the upgraded version, the per-app
* locales need to be removed or reset to system default locales to avoid the user being unable
* to change system locales setting.
*/
private void cleanApplicationLocalesIfNeeded(String packageName, int userId) {
if (mDelegateAppLocalePackages == null) {
Slog.w(TAG, "Failed to persist data into the shared preference!");
return;
}
String user = Integer.toString(userId);
Set<String> packageNames = new ArraySet<>(
mDelegateAppLocalePackages.getStringSet(user, new ArraySet<>()));
try {
LocaleList appLocales = mLocaleManagerService.getApplicationLocales(packageName,
userId);
if (appLocales.isEmpty() || !packageNames.contains(packageName)) {
return;
}
} catch (RemoteException | IllegalArgumentException e) {
Slog.e(TAG, "Exception when getting locales for " + packageName, e);
return;
}
try {
LocaleConfig localeConfig = new LocaleConfig(
mContext.createPackageContextAsUser(packageName, 0, UserHandle.of(userId)));
mLocaleManagerService.removeUnsupportedAppLocales(packageName, userId, localeConfig);
} catch (PackageManager.NameNotFoundException e) {
Slog.e(TAG, "Can not found the package name : " + packageName + " / " + e);
}
}
}

View File

@@ -129,11 +129,8 @@ public class LocaleManagerService extends SystemService {
mBackupHelper = new LocaleManagerBackupHelper(this,
mPackageManager, broadcastHandlerThread);
AppUpdateTracker appUpdateTracker =
new AppUpdateTracker(mContext, this, mBackupHelper);
mPackageMonitor = new LocaleManagerServicePackageMonitor(mBackupHelper,
systemAppUpdateTracker, appUpdateTracker, this);
systemAppUpdateTracker, this);
mPackageMonitor.register(context, broadcastHandlerThread.getLooper(),
UserHandle.ALL,
true);
@@ -598,7 +595,7 @@ public class LocaleManagerService extends SystemService {
}
private void setOverrideLocaleConfigUnchecked(@NonNull String appPackageName,
@UserIdInt int userId, @Nullable LocaleConfig overridelocaleConfig,
@UserIdInt int userId, @Nullable LocaleConfig overrideLocaleConfig,
@NonNull AppSupportedLocalesChangedAtomRecord atomRecord) {
synchronized (mWriteLock) {
if (DEBUG) {
@@ -606,26 +603,35 @@ public class LocaleManagerService extends SystemService {
"set the override LocaleConfig for package " + appPackageName + " and user "
+ userId);
}
LocaleConfig resLocaleConfig = null;
try {
resLocaleConfig = LocaleConfig.fromContextIgnoringOverride(
mContext.createPackageContext(appPackageName, 0));
} catch (PackageManager.NameNotFoundException e) {
Slog.e(TAG, "Unknown package name " + appPackageName);
return;
}
final File file = getXmlFileNameForUser(appPackageName, userId);
if (overridelocaleConfig == null) {
if (overrideLocaleConfig == null) {
if (file.exists()) {
Slog.d(TAG, "remove the override LocaleConfig");
file.delete();
}
removeUnsupportedAppLocales(appPackageName, userId, resLocaleConfig);
atomRecord.setOverrideRemoved(true);
atomRecord.setStatus(FrameworkStatsLog
.APP_SUPPORTED_LOCALES_CHANGED__STATUS__SUCCESS);
return;
} else {
if (overridelocaleConfig.isSameLocaleConfig(
if (overrideLocaleConfig.isSameLocaleConfig(
getOverrideLocaleConfig(appPackageName, userId))) {
Slog.d(TAG, "the same override, ignore it");
atomRecord.setSameAsPrevConfig(true);
return;
}
LocaleList localeList = overridelocaleConfig.getSupportedLocales();
LocaleList localeList = overrideLocaleConfig.getSupportedLocales();
// Normally the LocaleList object should not be null. However we reassign it as the
// empty list in case it happens.
if (localeList == null) {
@@ -654,16 +660,10 @@ public class LocaleManagerService extends SystemService {
}
atomicFile.finishWrite(stream);
// Clear per-app locales if they are not in the override LocaleConfig.
removeUnsupportedAppLocales(appPackageName, userId, overridelocaleConfig);
try {
Context appContext = mContext.createPackageContext(appPackageName, 0);
if (overridelocaleConfig.isSameLocaleConfig(
LocaleConfig.fromContextIgnoringOverride(appContext))) {
Slog.d(TAG, "setOverrideLocaleConfig, same as the app's LocaleConfig");
atomRecord.setSameAsResConfig(true);
}
} catch (PackageManager.NameNotFoundException e) {
Slog.e(TAG, "Unknown package name " + appPackageName);
removeUnsupportedAppLocales(appPackageName, userId, overrideLocaleConfig);
if (overrideLocaleConfig.isSameLocaleConfig(resLocaleConfig)) {
Slog.d(TAG, "setOverrideLocaleConfig, same as the app's LocaleConfig");
atomRecord.setSameAsResConfig(true);
}
atomRecord.setStatus(FrameworkStatsLog
.APP_SUPPORTED_LOCALES_CHANGED__STATUS__SUCCESS);
@@ -675,23 +675,29 @@ public class LocaleManagerService extends SystemService {
}
/**
* Checks if the per-app locales are in the new override LocaleConfig. Per-app locales
* missing from the new LocaleConfig will be removed.
* Checks if the per-app locales are in the LocaleConfig. Per-app locales missing from the
* LocaleConfig will be removed.
*/
private void removeUnsupportedAppLocales(String appPackageName, int userId,
void removeUnsupportedAppLocales(String appPackageName, int userId,
LocaleConfig localeConfig) {
LocaleList appLocales = getApplicationLocalesUnchecked(appPackageName, userId);
// Remove the app locale from the locale list if it doesn't exist in the override
// LocaleConfig.
// Remove the per-app locales from the locale list if they don't exist in the LocaleConfig.
boolean resetAppLocales = false;
List<Locale> newAppLocales = new ArrayList<Locale>();
for (int i = 0; i < appLocales.size(); i++) {
if (!localeConfig.containsLocale(appLocales.get(i))) {
Slog.i(TAG, "reset the app locales");
resetAppLocales = true;
continue;
if (localeConfig == null) {
//Reset the app locales to the system default
Slog.i(TAG, "There is no LocaleConfig, reset app locales");
resetAppLocales = true;
} else {
for (int i = 0; i < appLocales.size(); i++) {
if (!localeConfig.containsLocale(appLocales.get(i))) {
Slog.i(TAG, "Missing from the LocaleConfig, reset app locales");
resetAppLocales = true;
continue;
}
newAppLocales.add(appLocales.get(i));
}
newAppLocales.add(appLocales.get(i));
}
if (resetAppLocales) {
@@ -699,7 +705,8 @@ public class LocaleManagerService extends SystemService {
Locale[] locales = new Locale[newAppLocales.size()];
try {
setApplicationLocales(appPackageName, userId,
new LocaleList(newAppLocales.toArray(locales)), false);
new LocaleList(newAppLocales.toArray(locales)),
mBackupHelper.areLocalesSetFromDelegate(userId, appPackageName));
} catch (RemoteException | IllegalArgumentException e) {
Slog.e(TAG, "Could not set locales for " + appPackageName, e);
}
@@ -829,7 +836,7 @@ public class LocaleManagerService extends SystemService {
@NonNull
private File getXmlFileNameForUser(@NonNull String appPackageName, @UserIdInt int userId) {
// TODO(b/262752965): use per-package data directory
final File dir = new File(Environment.getDataSystemDeDirectory(userId), LOCALE_CONFIGS);
final File dir = new File(Environment.getDataSystemCeDirectory(userId), LOCALE_CONFIGS);
return new File(dir, appPackageName + SUFFIX_FILE_NAME);
}

View File

@@ -37,16 +37,13 @@ import com.android.internal.content.PackageMonitor;
final class LocaleManagerServicePackageMonitor extends PackageMonitor {
private LocaleManagerBackupHelper mBackupHelper;
private SystemAppUpdateTracker mSystemAppUpdateTracker;
private AppUpdateTracker mAppUpdateTracker;
private LocaleManagerService mLocaleManagerService;
LocaleManagerServicePackageMonitor(@NonNull LocaleManagerBackupHelper localeManagerBackupHelper,
@NonNull SystemAppUpdateTracker systemAppUpdateTracker,
@NonNull AppUpdateTracker appUpdateTracker,
@NonNull LocaleManagerService localeManagerService) {
mBackupHelper = localeManagerBackupHelper;
mSystemAppUpdateTracker = systemAppUpdateTracker;
mAppUpdateTracker = appUpdateTracker;
mLocaleManagerService = localeManagerService;
}
@@ -68,7 +65,7 @@ final class LocaleManagerServicePackageMonitor extends PackageMonitor {
@Override
public void onPackageUpdateFinished(String packageName, int uid) {
mAppUpdateTracker.onPackageUpdateFinished(packageName, uid);
mBackupHelper.onPackageUpdateFinished(packageName, uid);
mSystemAppUpdateTracker.onPackageUpdateFinished(packageName, uid);
}
}

View File

@@ -1,190 +0,0 @@
/*
* Copyright (C) 2022 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.server.locales;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.LocaleList;
import android.util.ArraySet;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import java.util.Arrays;
import java.util.Set;
/**
* Unit tests for {@link AppUpdateTracker}.
*/
@RunWith(AndroidJUnit4.class)
public class AppUpdateTrackerTest {
private static final String DEFAULT_PACKAGE_NAME = "com.android.myapp";
private static final int DEFAULT_UID = Binder.getCallingUid() + 100;
private static final int DEFAULT_USER_ID = 0;
private static final String DEFAULT_LOCALE_TAGS = "en-XC,ar-XB";
private static final LocaleList DEFAULT_LOCALES = LocaleList.forLanguageTags(
DEFAULT_LOCALE_TAGS);
private AppUpdateTracker mAppUpdateTracker;
@Mock
private Context mMockContext;
@Mock
private LocaleManagerService mMockLocaleManagerService;
@Mock
private ShadowLocaleManagerBackupHelper mMockBackupHelper;
@Before
public void setUp() throws Exception {
mMockContext = mock(Context.class);
mMockLocaleManagerService = mock(LocaleManagerService.class);
mMockBackupHelper = mock(ShadowLocaleManagerBackupHelper.class);
mAppUpdateTracker = spy(
new AppUpdateTracker(mMockContext, mMockLocaleManagerService, mMockBackupHelper));
}
@Test
public void testPackageUpgraded_localeEmpty_doNothing() throws Exception {
setUpLocalesForPackage(DEFAULT_PACKAGE_NAME, LocaleList.getEmptyLocaleList());
setUpPackageNamesForSp(new ArraySet<>(Arrays.asList(DEFAULT_PACKAGE_NAME)));
setUpPackageLocaleConfig(null, DEFAULT_PACKAGE_NAME);
setUpAppLocalesOptIn(true);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verifyNoLocalesCleared();
}
@Test
public void testPackageUpgraded_pkgNotInSp_doNothing() throws Exception {
setUpLocalesForPackage(DEFAULT_PACKAGE_NAME, DEFAULT_LOCALES);
String pkgNameA = "com.android.myAppA";
String pkgNameB = "com.android.myAppB";
setUpPackageNamesForSp(new ArraySet<>(Arrays.asList(pkgNameA, pkgNameB)));
setUpPackageLocaleConfig(null, DEFAULT_PACKAGE_NAME);
setUpAppLocalesOptIn(true);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verifyNoLocalesCleared();
}
@Test
public void testPackageUpgraded_appLocalesSupported_doNothing() throws Exception {
setUpLocalesForPackage(DEFAULT_PACKAGE_NAME, DEFAULT_LOCALES);
setUpPackageNamesForSp(new ArraySet<>(Arrays.asList(DEFAULT_PACKAGE_NAME)));
setUpPackageLocaleConfig(DEFAULT_LOCALES, DEFAULT_PACKAGE_NAME);
setUpAppLocalesOptIn(true);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verifyNoLocalesCleared();
setUpAppLocalesOptIn(false);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verifyNoLocalesCleared();
setUpAppLocalesOptIn(false);
setUpPackageLocaleConfig(null, DEFAULT_PACKAGE_NAME);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verifyNoLocalesCleared();
}
@Test
public void testPackageUpgraded_appLocalesNotSupported_clearAppLocale() throws Exception {
setUpLocalesForPackage(DEFAULT_PACKAGE_NAME, DEFAULT_LOCALES);
setUpPackageNamesForSp(new ArraySet<>(Arrays.asList(DEFAULT_PACKAGE_NAME)));
setUpPackageLocaleConfig(null, DEFAULT_PACKAGE_NAME);
setUpAppLocalesOptIn(true);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verify(mMockLocaleManagerService, times(1)).setApplicationLocales(DEFAULT_PACKAGE_NAME,
DEFAULT_USER_ID, LocaleList.forLanguageTags(""), false);
setUpPackageLocaleConfig(LocaleList.getEmptyLocaleList(), DEFAULT_PACKAGE_NAME);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verify(mMockLocaleManagerService, times(2)).setApplicationLocales(DEFAULT_PACKAGE_NAME,
DEFAULT_USER_ID, LocaleList.forLanguageTags(""), false);
setUpAppLocalesOptIn(false);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verify(mMockLocaleManagerService, times(3)).setApplicationLocales(DEFAULT_PACKAGE_NAME,
DEFAULT_USER_ID, LocaleList.forLanguageTags(""), false);
}
@Test
public void testPackageUpgraded_appLocalesNotInLocaleConfig_clearAppLocale() throws Exception {
setUpLocalesForPackage(DEFAULT_PACKAGE_NAME, DEFAULT_LOCALES);
setUpPackageNamesForSp(new ArraySet<>(Arrays.asList(DEFAULT_PACKAGE_NAME)));
setUpPackageLocaleConfig(LocaleList.forLanguageTags("hi,fr"), DEFAULT_PACKAGE_NAME);
setUpAppLocalesOptIn(true);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verify(mMockLocaleManagerService, times(1)).setApplicationLocales(DEFAULT_PACKAGE_NAME,
DEFAULT_USER_ID, LocaleList.forLanguageTags(""), false);
setUpAppLocalesOptIn(false);
mAppUpdateTracker.onPackageUpdateFinished(DEFAULT_PACKAGE_NAME, DEFAULT_UID);
verify(mMockLocaleManagerService, times(2)).setApplicationLocales(DEFAULT_PACKAGE_NAME,
DEFAULT_USER_ID, LocaleList.forLanguageTags(""), false);
}
private void setUpLocalesForPackage(String packageName, LocaleList locales) throws Exception {
doReturn(locales).when(mMockLocaleManagerService).getApplicationLocales(eq(packageName),
anyInt());
}
private void setUpPackageNamesForSp(Set<String> packageNames) {
SharedPreferences mockSharedPreference = mock(SharedPreferences.class);
doReturn(mockSharedPreference).when(mMockBackupHelper).getPersistedInfo();
doReturn(packageNames).when(mockSharedPreference).getStringSet(anyString(), any());
}
private void setUpPackageLocaleConfig(LocaleList locales, String packageName) {
doReturn(locales).when(mAppUpdateTracker).getPackageLocales(eq(packageName), anyInt());
}
private void setUpAppLocalesOptIn(boolean optIn) {
doReturn(optIn).when(mAppUpdateTracker).isSettingsAppLocalesOptIn();
}
/**
* Verifies that no app locales needs to be cleared for any package.
*
* <p>If {@link LocaleManagerService#setApplicationLocales} is not invoked when receiving the
* callback of package upgraded, we can conclude that no app locales needs to be cleared.
*/
private void verifyNoLocalesCleared() throws Exception {
verify(mMockLocaleManagerService, times(0)).setApplicationLocales(anyString(), anyInt(),
any(), anyBoolean());
}
}

View File

@@ -142,7 +142,6 @@ public class LocaleManagerBackupRestoreTest {
mMockDelegateAppLocalePackages = mock(SharedPreferences.class);
mMockSpEditor = mock(SharedPreferences.Editor.class);
SystemAppUpdateTracker systemAppUpdateTracker = mock(SystemAppUpdateTracker.class);
AppUpdateTracker appUpdateTracker = mock(AppUpdateTracker.class);
doReturn(mMockPackageManager).when(mMockContext).getPackageManager();
doReturn(mMockSpEditor).when(mMockDelegateAppLocalePackages).edit();
@@ -158,7 +157,7 @@ public class LocaleManagerBackupRestoreTest {
mUserMonitor = mBackupHelper.getUserMonitor();
mPackageMonitor = new LocaleManagerServicePackageMonitor(mBackupHelper,
systemAppUpdateTracker, appUpdateTracker, mMockLocaleManagerService);
systemAppUpdateTracker, mMockLocaleManagerService);
setCurrentTimeMillis(DEFAULT_CREATION_TIME_MILLIS);
}

View File

@@ -138,9 +138,8 @@ public class SystemAppUpdateTrackerTest {
mSystemAppUpdateTracker = new SystemAppUpdateTracker(mMockContext,
mLocaleManagerService, mStoragefile);
AppUpdateTracker appUpdateTracker = mock(AppUpdateTracker.class);
mPackageMonitor = new LocaleManagerServicePackageMonitor(mockLocaleManagerBackupHelper,
mSystemAppUpdateTracker, appUpdateTracker, mLocaleManagerService);
mSystemAppUpdateTracker, mLocaleManagerService);
}
@After