Remove the xml entry when the system language is removed

Bug: 301395074
Test: atest SettingsRoboTests:com.android.settings.localepicker
Change-Id: Ic771cd5146ab954c3da65748bf87cd14d30770b4
This commit is contained in:
Allen Su
2023-12-26 08:58:11 +00:00
parent 3b300cbf9f
commit d8695cbf9b
7 changed files with 66 additions and 18 deletions

View File

@@ -158,13 +158,14 @@ public class AppLocalePickerActivity extends SettingsBaseActivity
private void broadcastAppLocaleChange(LocaleStore.LocaleInfo localeInfo) { private void broadcastAppLocaleChange(LocaleStore.LocaleInfo localeInfo) {
if (!localeNotificationEnabled()) { if (!localeNotificationEnabled()) {
Log.w(TAG, "Locale notification is not enabled");
return; return;
} }
String localeTag = localeInfo.getLocale().toLanguageTag(); if (localeInfo.isAppCurrentLocale()) {
if (LocaleUtils.isInSystemLocale(localeTag) || localeInfo.isAppCurrentLocale()) {
return; return;
} }
try { try {
String localeTag = localeInfo.getLocale().toLanguageTag();
int uid = getPackageManager().getApplicationInfo(mPackageName, int uid = getPackageManager().getApplicationInfo(mPackageName,
PackageManager.GET_META_DATA).uid; PackageManager.GET_META_DATA).uid;
boolean launchNotification = mNotificationController.shouldTriggerNotification( boolean launchNotification = mNotificationController.shouldTriggerNotification(

View File

@@ -270,12 +270,14 @@ class LocaleDragAndDropAdapter
void removeChecked() { void removeChecked() {
int itemCount = mFeedItemList.size(); int itemCount = mFeedItemList.size();
LocaleStore.LocaleInfo localeInfo; LocaleStore.LocaleInfo localeInfo;
NotificationController controller = NotificationController.getInstance(mContext);
for (int i = itemCount - 1; i >= 0; i--) { for (int i = itemCount - 1; i >= 0; i--) {
localeInfo = mFeedItemList.get(i); localeInfo = mFeedItemList.get(i);
if (localeInfo.getChecked()) { if (localeInfo.getChecked()) {
FeatureFactory.getFeatureFactory().getMetricsFeatureProvider() FeatureFactory.getFeatureFactory().getMetricsFeatureProvider()
.action(mContext, SettingsEnums.ACTION_REMOVE_LANGUAGE); .action(mContext, SettingsEnums.ACTION_REMOVE_LANGUAGE);
mFeedItemList.remove(i); mFeedItemList.remove(i);
controller.removeNotificationInfo(localeInfo.getLocale().toLanguageTag());
} }
} }
notifyDataSetChanged(); notifyDataSetChanged();

View File

@@ -62,6 +62,17 @@ public class LocaleNotificationDataManager {
editor.apply(); editor.apply();
} }
/**
* Removes one entry with the corresponding locale from the {@link SharedPreferences}.
*
* @param locale A locale which the application sets to
*/
public void removeNotificationInfo(String locale) {
SharedPreferences.Editor editor = getSharedPreferences(mContext).edit();
editor.remove(locale);
editor.apply();
}
/** /**
* Gets the {@link NotificationInfo} with the associated locale from the * Gets the {@link NotificationInfo} with the associated locale from the
* {@link SharedPreferences}. * {@link SharedPreferences}.

View File

@@ -110,6 +110,15 @@ public class NotificationController {
return (info != null) ? info.getNotificationId() : -1; return (info != null) ? info.getNotificationId() : -1;
} }
/**
* Remove the {@link NotificationInfo} with the corresponding locale
*
* @param locale The locale which the application sets to
*/
public void removeNotificationInfo(@NonNull String locale) {
mDataManager.removeNotificationInfo(locale);
}
private boolean updateLocaleNotificationInfo(int uid, String locale) { private boolean updateLocaleNotificationInfo(int uid, String locale) {
NotificationInfo info = mDataManager.getNotificationInfo(locale); NotificationInfo info = mDataManager.getNotificationInfo(locale);
if (info == null) { if (info == null) {
@@ -135,20 +144,20 @@ public class NotificationController {
int notificationCount = info.getNotificationCount(); int notificationCount = info.getNotificationCount();
long lastNotificationTime = info.getLastNotificationTimeMs(); long lastNotificationTime = info.getLastNotificationTimeMs();
int notificationId = info.getNotificationId(); int notificationId = info.getNotificationId();
// Add the uid into the locale's uid list
uidSet.add(uid);
if (dismissCount < DISMISS_COUNT_THRESHOLD if (dismissCount < DISMISS_COUNT_THRESHOLD
&& notificationCount < NOTIFICATION_COUNT_THRESHOLD && notificationCount < NOTIFICATION_COUNT_THRESHOLD) {
// Notification should fire on multiples of 2 apps using the locale. // Add the uid into the locale's uid list
&& uidSet.size() % MULTIPLE_BASE == 0 uidSet.add(uid);
&& !isNotificationFrequent(lastNotificationTime)) { // Notification should fire on multiples of 2 apps using the locale.
// Increment the count because the notification can be triggered. if (uidSet.size() % MULTIPLE_BASE == 0
notificationCount = info.getNotificationCount() + 1; && !isNotificationFrequent(lastNotificationTime)) {
lastNotificationTime = Calendar.getInstance().getTimeInMillis(); // Increment the count because the notification can be triggered.
Log.i(TAG, "notificationCount:" + notificationCount); notificationCount = info.getNotificationCount() + 1;
if (notificationCount == 1) { lastNotificationTime = Calendar.getInstance().getTimeInMillis();
notificationId = (int) SystemClock.uptimeMillis(); Log.i(TAG, "notificationCount:" + notificationCount);
if (notificationCount == 1) {
notificationId = (int) SystemClock.uptimeMillis();
}
} }
} }
return new NotificationInfo(uidSet, notificationCount, dismissCount, lastNotificationTime, return new NotificationInfo(uidSet, notificationCount, dismissCount, lastNotificationTime,

View File

@@ -369,7 +369,7 @@ public class AppLocalePickerActivityTest {
// In the proto file, en-US's uid list contains 103, the notificationCount equals 1, and // In the proto file, en-US's uid list contains 103, the notificationCount equals 1, and
// LastNotificationTime > 0. // LastNotificationTime > 0.
NotificationInfo info = mDataManager.getNotificationInfo(EN_US); NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
assertThat(info.getUidCollection().contains(sUid)).isTrue(); assertThat(info.getUidCollection()).contains(sUid);
assertThat(info.getNotificationCount()).isEqualTo(1); assertThat(info.getNotificationCount()).isEqualTo(1);
assertThat(info.getDismissCount()).isEqualTo(0); assertThat(info.getDismissCount()).isEqualTo(0);
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0); assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
@@ -440,7 +440,7 @@ public class AppLocalePickerActivityTest {
@Test @Test
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED) @RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
public void testEvaluateLocaleNotification_localeUpdateReachThreshold_uidAddedNoNotification() public void testEvaluateLocaleNotification_localeUpdateReachThreshold_noUidNorNotification()
throws Exception { throws Exception {
// App with uid 106 changed its locale from System to en-US. // App with uid 106 changed its locale from System to en-US.
sUid = 106; sUid = 106;
@@ -460,7 +460,7 @@ public class AppLocalePickerActivityTest {
// In the proto file, en-US's uid list contains 106, the notificationCount equals 2, and // In the proto file, en-US's uid list contains 106, the notificationCount equals 2, and
// LastNotificationTime > 0. // LastNotificationTime > 0.
NotificationInfo info = mDataManager.getNotificationInfo(EN_US); NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
assertThat(info.getUidCollection()).contains(sUid); assertThat(info.getUidCollection().contains(sUid)).isFalse();
assertThat(info.getNotificationCount()).isEqualTo(2); assertThat(info.getNotificationCount()).isEqualTo(2);
assertThat(info.getDismissCount()).isEqualTo(0); assertThat(info.getDismissCount()).isEqualTo(0);
assertThat(info.getLastNotificationTimeMs()).isEqualTo(lastNotificationTime); assertThat(info.getLastNotificationTimeMs()).isEqualTo(lastNotificationTime);

View File

@@ -68,6 +68,18 @@ public class LocaleNotificationDataManagerTest {
info.getLastNotificationTimeMs()); info.getLastNotificationTimeMs());
} }
@Test
public void testRemoveNotificationInfo() {
String locale = "en-US";
Set<Integer> uidSet = Set.of(101);
NotificationInfo info = new NotificationInfo(uidSet, 1, 1, 100L, 1000);
mDataManager.putNotificationInfo(locale, info);
assertThat(mDataManager.getNotificationInfo(locale)).isEqualTo(info);
mDataManager.removeNotificationInfo(locale);
assertThat(mDataManager.getNotificationInfo(locale)).isNull();
}
@Test @Test
public void testGetNotificationMap() { public void testGetNotificationMap() {
String enUS = "en-US"; String enUS = "en-US";

View File

@@ -71,6 +71,19 @@ public class NotificationControllerTest {
assertThat(result.getNotificationId()).isEqualTo(id); assertThat(result.getNotificationId()).isEqualTo(id);
} }
@Test
public void testRemoveNotificationInfo_removed() throws Exception {
String enUS = "en-US";
Set<Integer> uidSet = Set.of(100, 101);
long lastNotificationTime = Calendar.getInstance().getTimeInMillis();
int id = (int) SystemClock.uptimeMillis();
initSharedPreference(enUS, uidSet, 0, 1, lastNotificationTime, id);
mNotificationController.removeNotificationInfo(enUS);
assertThat(mDataManager.getNotificationInfo(enUS)).isNull();
}
@Test @Test
public void testShouldTriggerNotification_inSystemLocale_returnFalse() throws Exception { public void testShouldTriggerNotification_inSystemLocale_returnFalse() throws Exception {
int uid = 102; int uid = 102;