diff options
| -rw-r--r-- | services/core/java/com/android/server/notification/PreferencesHelper.java | 76 | ||||
| -rw-r--r-- | services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java | 281 |
2 files changed, 169 insertions, 188 deletions
diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index 59af58fb0dfb..4bafbc73a96b 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -135,7 +135,6 @@ public class PreferencesHelper implements RankingConfig { private static final String ATT_SHOW_BADGE = "show_badge"; private static final String ATT_APP_USER_LOCKED_FIELDS = "app_user_locked_fields"; private static final String ATT_ENABLED = "enabled"; - private static final String ATT_USER_ALLOWED = "allowed"; private static final String ATT_HIDE_SILENT = "hide_gentle"; private static final String ATT_SENT_INVALID_MESSAGE = "sent_invalid_msg"; private static final String ATT_SENT_VALID_MESSAGE = "sent_valid_msg"; @@ -363,15 +362,12 @@ public class PreferencesHelper implements RankingConfig { String delegateName = XmlUtils.readStringAttribute(parser, ATT_NAME); boolean delegateEnabled = parser.getAttributeBoolean( null, ATT_ENABLED, Delegate.DEFAULT_ENABLED); - boolean userAllowed = parser.getAttributeBoolean( - null, ATT_USER_ALLOWED, Delegate.DEFAULT_USER_ALLOWED); Delegate d = null; if (delegateId != UNKNOWN_UID && !TextUtils.isEmpty(delegateName)) { - d = new Delegate(delegateName, delegateId, delegateEnabled, userAllowed); + d = new Delegate(delegateName, delegateId, delegateEnabled); } r.delegate = d; } - } try { @@ -640,9 +636,6 @@ public class PreferencesHelper implements RankingConfig { if (r.delegate.mEnabled != Delegate.DEFAULT_ENABLED) { out.attributeBoolean(null, ATT_ENABLED, r.delegate.mEnabled); } - if (r.delegate.mUserAllowed != Delegate.DEFAULT_USER_ALLOWED) { - out.attributeBoolean(null, ATT_USER_ALLOWED, r.delegate.mUserAllowed); - } out.endTag(null, TAG_DELEGATE); } @@ -725,10 +718,17 @@ public class PreferencesHelper implements RankingConfig { @Override public void setShowBadge(String packageName, int uid, boolean showBadge) { + boolean changed = false; synchronized (mPackagePreferences) { - getOrCreatePackagePreferencesLocked(packageName, uid).showBadge = showBadge; + PackagePreferences pkgPrefs = getOrCreatePackagePreferencesLocked(packageName, uid); + if (pkgPrefs.showBadge != showBadge) { + pkgPrefs.showBadge = showBadge; + changed = true; + } + } + if (changed) { + updateConfig(); } - updateConfig(); } public boolean isInInvalidMsgState(String packageName, int uid) { @@ -985,7 +985,9 @@ public class PreferencesHelper implements RankingConfig { needsPolicyFileChange = true; } - updateConfig(); + if (needsPolicyFileChange) { + updateConfig(); + } if (needsPolicyFileChange && !wasUndeleted) { mNotificationChannelLogger.logNotificationChannelModified(existing, uid, pkg, previousLoggingImportance, false); @@ -1072,6 +1074,7 @@ public class PreferencesHelper implements RankingConfig { boolean fromUser) { Objects.requireNonNull(updatedChannel); Objects.requireNonNull(updatedChannel.getId()); + boolean changed = false; boolean needsDndChange = false; synchronized (mPackagePreferences) { PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); @@ -1105,6 +1108,7 @@ public class PreferencesHelper implements RankingConfig { ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT; r.visibility = updatedChannel.getLockscreenVisibility(); r.showBadge = updatedChannel.canShowBadge(); + changed = true; } if (!channel.equals(updatedChannel)) { @@ -1113,17 +1117,21 @@ public class PreferencesHelper implements RankingConfig { .setSubtype(fromUser ? 1 : 0)); mNotificationChannelLogger.logNotificationChannelModified(updatedChannel, uid, pkg, NotificationChannelLogger.getLoggingImportance(channel), fromUser); + changed = true; } if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd || channel.getImportance() != updatedChannel.getImportance()) { needsDndChange = true; + changed = true; } } if (needsDndChange) { updateChannelsBypassingDnd(); } - updateConfig(); + if (changed) { + updateConfig(); + } } @Override @@ -1788,7 +1796,7 @@ public class PreferencesHelper implements RankingConfig { if (prefs == null || prefs.delegate == null) { return null; } - if (!prefs.delegate.mUserAllowed || !prefs.delegate.mEnabled) { + if (!prefs.delegate.mEnabled) { return null; } return prefs.delegate.mPkg; @@ -1802,46 +1810,20 @@ public class PreferencesHelper implements RankingConfig { String delegatePkg, int delegateUid) { synchronized (mPackagePreferences) { PackagePreferences prefs = getOrCreatePackagePreferencesLocked(sourcePkg, sourceUid); - - boolean userAllowed = prefs.delegate == null || prefs.delegate.mUserAllowed; - Delegate delegate = new Delegate(delegatePkg, delegateUid, true, userAllowed); - prefs.delegate = delegate; + prefs.delegate = new Delegate(delegatePkg, delegateUid, true); } - updateConfig(); } /** * Used by an app to turn off its notification delegate. */ public void revokeNotificationDelegate(String sourcePkg, int sourceUid) { - boolean changed = false; synchronized (mPackagePreferences) { PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); if (prefs != null && prefs.delegate != null) { prefs.delegate.mEnabled = false; - changed = true; - } - } - if (changed) { - updateConfig(); - } - } - - /** - * Toggles whether an app can have a notification delegate on behalf of a user. - */ - public void toggleNotificationDelegate(String sourcePkg, int sourceUid, boolean userAllowed) { - boolean changed = false; - synchronized (mPackagePreferences) { - PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); - if (prefs != null && prefs.delegate != null) { - prefs.delegate.mUserAllowed = userAllowed; - changed = true; } } - if (changed) { - updateConfig(); - } } /** @@ -2726,17 +2708,15 @@ public class PreferencesHelper implements RankingConfig { private static class Delegate { static final boolean DEFAULT_ENABLED = true; - static final boolean DEFAULT_USER_ALLOWED = true; - String mPkg; - int mUid = UNKNOWN_UID; - boolean mEnabled = DEFAULT_ENABLED; - boolean mUserAllowed = DEFAULT_USER_ALLOWED; - Delegate(String pkg, int uid, boolean enabled, boolean userAllowed) { + final String mPkg; + final int mUid; + boolean mEnabled; + + Delegate(String pkg, int uid, boolean enabled) { mPkg = pkg; mUid = uid; mEnabled = enabled; - mUserAllowed = userAllowed; } public boolean isAllowed(String pkg, int uid) { @@ -2745,7 +2725,7 @@ public class PreferencesHelper implements RankingConfig { } return pkg.equals(mPkg) && uid == mUid - && (mUserAllowed && mEnabled); + && mEnabled; } } } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index 79f69ee94a62..06bcb9134c07 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -63,6 +63,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -70,6 +71,7 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; import android.app.AppOpsManager; @@ -161,7 +163,6 @@ public class PreferencesHelperTest extends UiServiceTestCase { private static final String SYSTEM_PKG = "android"; private static final int SYSTEM_UID = 1000; private static final UserHandle USER2 = UserHandle.of(10); - private static final String TEST_CHANNEL_ID = "test_channel_id"; private static final String TEST_AUTHORITY = "test"; private static final Uri SOUND_URI = Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10"); @@ -284,9 +285,9 @@ public class PreferencesHelperTest extends UiServiceTestCase { anyString(), eq(null), anyString())).thenReturn(MODE_DEFAULT); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(true, false)); - appPermissions.put(new Pair(UID_N_MR1, PKG_N_MR1), new Pair(true, false)); + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_N_MR1, PKG_N_MR1), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -422,8 +423,8 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertTrue(mHelper.createNotificationChannel(package10, uid10, channel10, true, false)); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(uid0, package0), new Pair(false, false)); - appPermissions.put(new Pair(uid10, package10), new Pair(true, false)); + appPermissions.put(new Pair<>(uid0, package0), new Pair<>(false, false)); + appPermissions.put(new Pair<>(uid10, package10), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(10)) .thenReturn(appPermissions); @@ -454,7 +455,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertTrue(mHelper.createNotificationChannel(package0, uid0, channel0, true, false)); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(uid0, package0), new Pair(true, false)); + appPermissions.put(new Pair<>(uid0, package0), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -1009,11 +1010,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory, false); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); - appPermissions.put(new Pair(UID_N_MR1, PKG_N_MR1), new Pair(true, false)); + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_N_MR1, PKG_N_MR1), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -1089,11 +1090,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory, false); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); - appPermissions.put(new Pair(UID_N_MR1, PKG_N_MR1), new Pair(true, false)); + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_N_MR1, PKG_N_MR1), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -1175,8 +1176,8 @@ public class PreferencesHelperTest extends UiServiceTestCase { mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory, false); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -1254,11 +1255,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory, false); ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); - appPermissions.put(new Pair(UID_N_MR1, PKG_N_MR1), new Pair(true, false)); + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); + appPermissions.put(new Pair<>(UID_N_MR1, PKG_N_MR1), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -1372,7 +1373,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testBackupRestoreXml_withNullSoundUri() throws Exception { ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(UID_N_MR1, PKG_N_MR1), new Pair(true, false)); + appPermissions.put(new Pair<>(UID_N_MR1, PKG_N_MR1), new Pair<>(true, false)); when(mPermissionHelper.getNotificationPermissionValues(USER_SYSTEM)) .thenReturn(appPermissions); @@ -2605,10 +2606,8 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testClearData() { - ArraySet<String> pkg = new ArraySet<>(); - pkg.add(PKG_O); ArraySet<Pair<String, Integer>> pkgPair = new ArraySet<>(); - pkgPair.add(new Pair(PKG_O, UID_O)); + pkgPair.add(new Pair<>(PKG_O, UID_O)); mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false); mHelper.createNotificationChannelGroup( PKG_O, UID_O, new NotificationChannelGroup("1", "bye"), true); @@ -2879,10 +2878,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { // package permissions map to be passed in ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); // not in local prefs - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); // not in local prefs - appPermissions.put(new Pair(UID_P, PKG_P), new Pair(true, false)); // in local prefs - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); // in local prefs + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); // not in local prefs + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); // not in local prefs + appPermissions.put(new Pair<>(UID_P, PKG_P), new Pair<>(true, false)); // in local prefs + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); // in local prefs NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); @@ -2901,15 +2900,15 @@ public class PreferencesHelperTest extends UiServiceTestCase { ArrayMap<Pair<Integer, String>, String> expected = new ArrayMap<>(); // packages that only exist via the app permissions; should be present - expected.put(new Pair(UserHandle.getUserId(1), "first"), "DEFAULT"); - expected.put(new Pair(UserHandle.getUserId(3), "third"), "NONE"); + expected.put(new Pair<>(UserHandle.getUserId(1), "first"), "DEFAULT"); + expected.put(new Pair<>(UserHandle.getUserId(3), "third"), "NONE"); // packages that exist in both app permissions & local preferences - expected.put(new Pair(UserHandle.getUserId(UID_P), PKG_P), "DEFAULT"); - expected.put(new Pair(UserHandle.getUserId(UID_O), PKG_O), "NONE"); + expected.put(new Pair<>(UserHandle.getUserId(UID_P), PKG_P), "DEFAULT"); + expected.put(new Pair<>(UserHandle.getUserId(UID_O), PKG_O), "NONE"); // package that only exists in local preferences; expect no importance output - expected.put(new Pair(UserHandle.getUserId(UID_N_MR1), PKG_N_MR1), null); + expected.put(new Pair<>(UserHandle.getUserId(UID_N_MR1), PKG_N_MR1), null); JSONArray actual = (JSONArray) mHelper.dumpJson( new NotificationManagerService.DumpFilter(), appPermissions) @@ -2918,7 +2917,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { for (int i = 0; i < actual.length(); i++) { JSONObject pkgInfo = actual.getJSONObject(i); Pair<Integer, String> pkgKey = - new Pair(pkgInfo.getInt("userId"), pkgInfo.getString("packageName")); + new Pair<>(pkgInfo.getInt("userId"), pkgInfo.getString("packageName")); assertTrue(expected.containsKey(pkgKey)); if (pkgInfo.has("importance")) { assertThat(pkgInfo.getString("importance")).isEqualTo(expected.get(pkgKey)); @@ -2957,16 +2956,16 @@ public class PreferencesHelperTest extends UiServiceTestCase { // have their permission set to false, and not based on PackagePreferences importance ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); // not in local prefs - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); // not in local prefs - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); // in local prefs + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); // not in local prefs + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); // not in local prefs + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); // in local prefs mHelper.canShowBadge(PKG_O, UID_O); // expected output ArraySet<Pair<Integer, String>> expected = new ArraySet<>(); - expected.add(new Pair(UserHandle.getUserId(3), "third")); - expected.add(new Pair(UserHandle.getUserId(UID_O), PKG_O)); + expected.add(new Pair<>(UserHandle.getUserId(3), "third")); + expected.add(new Pair<>(UserHandle.getUserId(UID_O), PKG_O)); // make sure that's the only thing in the package ban output JSONArray actual = mHelper.dumpBansJson( @@ -2976,7 +2975,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { for (int i = 0; i < actual.length(); i++) { JSONObject ban = actual.getJSONObject(i); assertTrue(expected.contains( - new Pair(ban.getInt("userId"), ban.getString("packageName")))); + new Pair<>(ban.getInt("userId"), ban.getString("packageName")))); } } @@ -2994,9 +2993,9 @@ public class PreferencesHelperTest extends UiServiceTestCase { // confirm that the string resulting from dumpImpl contains only importances from permission ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); // not in local prefs - appPermissions.put(new Pair(3, "third"), new Pair(false, true)); // not in local prefs - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); // in local prefs + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); // not in local prefs + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, true)); // not in local prefs + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); // in local prefs // local package preferences mHelper.canShowBadge(PKG_O, UID_O); @@ -3055,9 +3054,9 @@ public class PreferencesHelperTest extends UiServiceTestCase { // permissions -- these should take precedence ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); // not in local prefs - appPermissions.put(new Pair(3, "third"), new Pair(false, false)); // not in local prefs - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, false)); // in local prefs + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); // not in local prefs + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, false)); // not in local prefs + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, false)); // in local prefs // local package preferences mHelper.canShowBadge(PKG_O, UID_O); @@ -3067,14 +3066,14 @@ public class PreferencesHelperTest extends UiServiceTestCase { // should have importance set (aka not PKG_P) // map format: (uid, package name) -> importance (int) ArrayMap<Pair<Integer, String>, Integer> expected = new ArrayMap<>(); - expected.put(new Pair(1, "first"), IMPORTANCE_DEFAULT); - expected.put(new Pair(3, "third"), IMPORTANCE_NONE); - expected.put(new Pair(UID_O, PKG_O), IMPORTANCE_NONE); + expected.put(new Pair<>(1, "first"), IMPORTANCE_DEFAULT); + expected.put(new Pair<>(3, "third"), IMPORTANCE_NONE); + expected.put(new Pair<>(UID_O, PKG_O), IMPORTANCE_NONE); // unfortunately, due to how nano protos work, there's no distinction between unset // fields and default-value fields, so we have no choice here but to check for a value of 0. // at least we can make sure the local importance for PKG_P in this test is not 0 (NONE). - expected.put(new Pair(UID_P, PKG_P), 0); + expected.put(new Pair<>(UID_P, PKG_P), 0); // get the proto output and inspect its contents ProtoOutputStream proto = new ProtoOutputStream(); @@ -3084,7 +3083,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertThat(actual.records.length).isEqualTo(expected.size()); for (int i = 0; i < actual.records.length; i++) { RankingHelperProto.RecordProto record = actual.records[i]; - Pair<Integer, String> pkgKey = new Pair(record.uid, record.package_); + Pair<Integer, String> pkgKey = new Pair<>(record.uid, record.package_); assertTrue(expected.containsKey(pkgKey)); assertThat(record.importance).isEqualTo(expected.get(pkgKey)); } @@ -3403,26 +3402,6 @@ public class PreferencesHelperTest extends UiServiceTestCase { } @Test - public void testToggleNotificationDelegate() { - mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); - mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); - - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - - mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); - assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); - } - - @Test - public void testToggleNotificationDelegate_noDelegateExistsNoCrash() { - mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - - mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - } - - @Test public void testIsDelegateAllowed_noSource() { assertFalse(mHelper.isDelegateAllowed("does not exist", -1, "whatever", 0)); } @@ -3451,14 +3430,6 @@ public class PreferencesHelperTest extends UiServiceTestCase { } @Test - public void testIsDelegateAllowed_delegateDisabledByUser() { - mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); - mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); - - assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53)); - } - - @Test public void testIsDelegateAllowed() { mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); @@ -3503,46 +3474,8 @@ public class PreferencesHelperTest extends UiServiceTestCase { loadStreamXml(baos, false, UserHandle.USER_ALL); assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - } - @Test - public void testDelegateXml_userDisabledDelegate() throws Exception { mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); - mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); - - ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); - mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, - mPermissionHelper, mLogger, - mAppOpsManager, mStatsEventBuilderFactory, false); - loadStreamXml(baos, false, UserHandle.USER_ALL); - - // appears disabled - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - - // but was loaded and can be toggled back on - mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); - assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); - } - - @Test - public void testDelegateXml_entirelyDisabledDelegate() throws Exception { - mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); - mHelper.toggleNotificationDelegate(PKG_O, UID_O, false); - mHelper.revokeNotificationDelegate(PKG_O, UID_O); - - ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL); - mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, - mPermissionHelper, mLogger, - mAppOpsManager, mStatsEventBuilderFactory, false); - loadStreamXml(baos, false, UserHandle.USER_ALL); - - // appears disabled - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - - mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53); - assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O)); - - mHelper.toggleNotificationDelegate(PKG_O, UID_O, true); assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O)); } @@ -3686,7 +3619,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testUpdateNotificationChannel_defaultApp() { ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(0, null, toAdd); NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); @@ -3820,7 +3753,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG_O, UserHandle.PER_USER_RANGE + 1, c, true, true); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) @@ -3840,7 +3773,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, false, false); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) @@ -3858,7 +3791,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG_O, UID_O, b, false, false); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) @@ -3884,7 +3817,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, false, false); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); @@ -3897,7 +3830,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { ArraySet<String> toRemove = new ArraySet<>(); toRemove.add(PKG_O); toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_N_MR1, UID_N_MR1)); + toAdd.add(new Pair<>(PKG_N_MR1, UID_N_MR1)); mHelper.updateDefaultApps(USER.getIdentifier(), toRemove, toAdd); assertFalse(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) @@ -3909,7 +3842,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testUpdateDefaultApps_appDoesNotExist_noCrash() { ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); ArraySet<String> toRemove = new ArraySet<>(); toRemove.add(PKG_N_MR1); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), toRemove, toAdd); @@ -3922,7 +3855,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false) @@ -3938,7 +3871,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); NotificationChannel update = new NotificationChannel("a", "a", IMPORTANCE_NONE); @@ -3964,7 +3897,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testDefaultApp_appHasNoSettingsYet() { ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH); @@ -4004,7 +3937,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { @Test public void testUpdateDefaultApps_thenNotFixedPermission() { ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(0, null, toAdd); UserInfo user = new UserInfo(); @@ -4043,7 +3976,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { UID_O}); ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); - toAdd.add(new Pair(PKG_O, UID_O)); + toAdd.add(new Pair<>(PKG_O, UID_O)); mHelper.updateDefaultApps(UserHandle.getUserId(UID_O), null, toAdd); TypedXmlPullParser parser = Xml.newFastPullParser(); @@ -5041,9 +4974,9 @@ public class PreferencesHelperTest extends UiServiceTestCase { // build a collection of app permissions that should be passed in but ignored ArrayMap<Pair<Integer, String>, Pair<Boolean, Boolean>> appPermissions = new ArrayMap<>(); - appPermissions.put(new Pair(1, "first"), new Pair(true, false)); // not in local prefs - appPermissions.put(new Pair(3, "third"), new Pair(false, true)); // not in local prefs - appPermissions.put(new Pair(UID_O, PKG_O), new Pair(false, true)); // in local prefs + appPermissions.put(new Pair<>(1, "first"), new Pair<>(true, false)); // not in local prefs + appPermissions.put(new Pair<>(3, "third"), new Pair<>(false, true)); // not in local prefs + appPermissions.put(new Pair<>(UID_O, PKG_O), new Pair<>(false, true)); // in local prefs // local preferences mHelper.canShowBadge(PKG_O, UID_O); @@ -5052,10 +4985,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { // expected output. format: uid -> importance, as only uid (and not package name) // is in PackageNotificationPreferences ArrayMap<Integer, Pair<Integer, Boolean>> expected = new ArrayMap<>(); - expected.put(1, new Pair(IMPORTANCE_DEFAULT, false)); - expected.put(3, new Pair(IMPORTANCE_NONE, true)); - expected.put(UID_O, new Pair(IMPORTANCE_NONE, true)); // banned by permissions - expected.put(UID_P, new Pair(IMPORTANCE_NONE, false)); // defaults to none, false + expected.put(1, new Pair<>(IMPORTANCE_DEFAULT, false)); + expected.put(3, new Pair<>(IMPORTANCE_NONE, true)); + expected.put(UID_O, new Pair<>(IMPORTANCE_NONE, true)); // banned by permissions + expected.put(UID_P, new Pair<>(IMPORTANCE_NONE, false)); // defaults to none, false ArrayList<StatsEvent> events = new ArrayList<>(); mHelper.pullPackagePreferencesStats(events, appPermissions); @@ -5109,4 +5042,72 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertTrue((channelB.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0); assertTrue((channelC.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0); } + + @Test + public void createNotificationChannel_updateDifferent_requestsSort() { + NotificationChannel original = new NotificationChannel("id", "Bah", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, original, true, false); + clearInvocations(mHandler); + + NotificationChannel updated = new NotificationChannel("id", "Wow", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, updated, true, false); + + verify(mHandler).requestSort(); + } + + @Test + public void createNotificationChannel_updateSame_doesNotRequestSort() { + NotificationChannel original = new NotificationChannel("id", "Bah", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, original, true, false); + clearInvocations(mHandler); + + NotificationChannel same = new NotificationChannel("id", "Bah", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, same, true, false); + + verifyZeroInteractions(mHandler); + } + + @Test + public void updateNotificationChannel_different_requestsSort() { + NotificationChannel original = new NotificationChannel("id", "Bah", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, original, true, false); + clearInvocations(mHandler); + + NotificationChannel updated = new NotificationChannel("id", "Wow", IMPORTANCE_DEFAULT); + mHelper.updateNotificationChannel(PKG_P, 0, updated, false); + + verify(mHandler).requestSort(); + } + + @Test + public void updateNotificationChannel_same_doesNotRequestSort() { + NotificationChannel original = new NotificationChannel("id", "Bah", IMPORTANCE_DEFAULT); + mHelper.createNotificationChannel(PKG_P, 0, original, true, false); + clearInvocations(mHandler); + // Note: Creating a NotificationChannel identical to the original is not equals(), because + // of mOriginalImportance. So we create a "true copy" instead. + Parcel parcel = Parcel.obtain(); + original.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + NotificationChannel same = NotificationChannel.CREATOR.createFromParcel(parcel); + parcel.recycle(); + + mHelper.updateNotificationChannel(PKG_P, 0, same, false); + + verifyZeroInteractions(mHandler); + } + + @Test + public void setShowBadge_update_requestsSort() { + mHelper.setShowBadge(PKG_P, 0, false); + + verify(mHandler).requestSort(); + } + + @Test + public void setShowBadge_same_doesNotRequestSort() { + mHelper.setShowBadge(PKG_P, 0, true); // true == DEFAULT_SHOW_BADGE + + verifyZeroInteractions(mHandler); + } } |