diff options
23 files changed, 207 insertions, 100 deletions
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index cf44c7d58fbd..ba419337f044 100755 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -6946,6 +6946,13 @@ public final class Settings { public static final String CMAS_ADDITIONAL_BROADCAST_PKG = "cmas_additional_broadcast_pkg"; /** + * Whether the launcher should show any notification badges. + * The value is boolean (1 or 0). + * @hide + */ + public static final String NOTIFICATION_BADGING = "notification_badging"; + + /** * This are the settings to be backed up. * * NOTE: Settings are backed up and restored in the order they appear @@ -7040,7 +7047,8 @@ public final class Settings { AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN, ASSIST_GESTURE_ENABLED, ASSIST_GESTURE_SENSITIVITY, - VR_DISPLAY_MODE + VR_DISPLAY_MODE, + NOTIFICATION_BADGING }; /** diff --git a/core/proto/android/providers/settings.proto b/core/proto/android/providers/settings.proto index ea40fd5bb472..8b73daf47f0f 100644 --- a/core/proto/android/providers/settings.proto +++ b/core/proto/android/providers/settings.proto @@ -503,6 +503,7 @@ message SecureSettingsProto { SettingProto demo_user_setup_complete = 165; SettingProto instant_apps_enabled = 166; SettingProto device_paired = 167; + SettingProto notification_badging = 168; } message SystemSettingsProto { diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index b7e8467ef385..6640102b88d9 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1013,6 +1013,9 @@ <!-- Is the notification LED intrusive? Used to decide if there should be a disable option --> <bool name="config_intrusiveNotificationLed">false</bool> + <!-- De we do icon badges? Used to decide if there should be a disable option--> + <bool name="config_notificationBadging">true</bool> + <!-- Default value for LED off time when the battery is low on charge in miliseconds --> <integer name="config_notificationsBatteryLedOff">2875</integer> diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 31d13c941a9f..4121ce571de5 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -2087,6 +2087,7 @@ <java-symbol type="string" name="config_wifi_tether_enable" /> <java-symbol type="integer" name="config_wifi_wakeup_available" /> <java-symbol type="bool" name="config_intrusiveNotificationLed" /> + <java-symbol type="bool" name="config_notificationBadging" /> <java-symbol type="dimen" name="preference_fragment_padding_bottom" /> <java-symbol type="dimen" name="preference_fragment_padding_side" /> <java-symbol type="drawable" name="expander_ic_maximized" /> diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java index f475361b9b22..930935914967 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java @@ -1434,6 +1434,9 @@ class SettingsProtoDumpUtil { dumpSetting(s, p, Settings.Secure.DEVICE_PAIRED, SecureSettingsProto.DEVICE_PAIRED); + dumpSetting(s, p, + Settings.Secure.NOTIFICATION_BADGING, + SecureSettingsProto.NOTIFICATION_BADGING); } private static void dumpProtoSystemSettingsLocked( diff --git a/services/core/java/com/android/server/notification/BadgeExtractor.java b/services/core/java/com/android/server/notification/BadgeExtractor.java index e6edaf1d3079..1bd2085952f1 100644 --- a/services/core/java/com/android/server/notification/BadgeExtractor.java +++ b/services/core/java/com/android/server/notification/BadgeExtractor.java @@ -41,9 +41,10 @@ public class BadgeExtractor implements NotificationSignalExtractor { if (DBG) Slog.d(TAG, "missing config"); return null; } + boolean userWantsBadges = mConfig.badgingEnabled(record.sbn.getUser()); boolean appCanShowBadge = mConfig.canShowBadge(record.sbn.getPackageName(), record.sbn.getUid()); - if (!appCanShowBadge) { + if (!userWantsBadges || !appCanShowBadge) { record.setShowBadge(false); } else { record.setShowBadge(mConfig.getNotificationChannel(record.sbn.getPackageName(), diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index da919ec7a9f2..6cf54c89651c 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -927,6 +927,8 @@ public class NotificationManagerService extends SystemService { }; private final class SettingsObserver extends ContentObserver { + private final Uri NOTIFICATION_BADGING_URI + = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); private final Uri NOTIFICATION_LIGHT_PULSE_URI = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); private final Uri NOTIFICATION_RATE_LIMIT_URI @@ -938,6 +940,8 @@ public class NotificationManagerService extends SystemService { void observe() { ContentResolver resolver = getContext().getContentResolver(); + resolver.registerContentObserver(NOTIFICATION_BADGING_URI, + false, this, UserHandle.USER_ALL); resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this, UserHandle.USER_ALL); resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, @@ -963,6 +967,9 @@ public class NotificationManagerService extends SystemService { mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); } + if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { + mRankingHelper.updateBadgingEnabled(); + } } } @@ -4022,6 +4029,7 @@ public class NotificationManagerService extends SystemService { private void handleRankingSort(Message msg) { if (!(msg.obj instanceof Boolean)) return; + if (mRankingHelper == null) return; boolean forceUpdate = ((Boolean) msg.obj == null) ? false : (boolean) msg.obj; synchronized (mNotificationLock) { final int N = mNotificationList.size(); diff --git a/services/core/java/com/android/server/notification/RankingConfig.java b/services/core/java/com/android/server/notification/RankingConfig.java index 4d19b52c327a..36da04dfc3c6 100644 --- a/services/core/java/com/android/server/notification/RankingConfig.java +++ b/services/core/java/com/android/server/notification/RankingConfig.java @@ -18,6 +18,7 @@ package com.android.server.notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.content.pm.ParceledListSlice; +import android.os.UserHandle; import java.util.Collection; @@ -27,6 +28,7 @@ public interface RankingConfig { int getImportance(String packageName, int uid); void setShowBadge(String packageName, int uid, boolean showBadge); boolean canShowBadge(String packageName, int uid); + boolean badgingEnabled(UserHandle userHandle); Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg, int uid); diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java index 788f21ddf092..2c51510de689 100644 --- a/services/core/java/com/android/server/notification/RankingHelper.java +++ b/services/core/java/com/android/server/notification/RankingHelper.java @@ -21,6 +21,8 @@ import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.nano.MetricsProto; import com.android.internal.util.Preconditions; +import android.annotation.UserIdInt; +import android.app.ActivityManager; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; @@ -33,10 +35,14 @@ import android.content.pm.ParceledListSlice; import android.metrics.LogMaker; import android.os.Build; import android.os.UserHandle; +import android.os.UserManager; +import android.provider.Settings; +import android.provider.Settings.Secure; import android.service.notification.NotificationListenerService.Ranking; import android.text.TextUtils; import android.util.ArrayMap; import android.util.Slog; +import android.util.SparseBooleanArray; import org.json.JSONArray; import org.json.JSONException; @@ -91,6 +97,7 @@ public class RankingHelper implements RankingConfig { private final Context mContext; private final RankingHandler mRankingHandler; private final PackageManager mPm; + private SparseBooleanArray mBadgingEnabled; public RankingHelper(Context context, PackageManager pm, RankingHandler rankingHandler, NotificationUsageStats usageStats, String[] extractorNames) { @@ -100,6 +107,8 @@ public class RankingHelper implements RankingConfig { mPreliminaryComparator = new NotificationComparator(mContext); + updateBadgingEnabled(); + final int N = extractorNames.length; mSignalExtractors = new NotificationSignalExtractor[N]; for (int i = 0; i < N; i++) { @@ -1111,6 +1120,38 @@ public class RankingHelper implements RankingConfig { channel.getImportance()); } + public void updateBadgingEnabled() { + if (mBadgingEnabled == null) { + mBadgingEnabled = new SparseBooleanArray(); + } + boolean changed = false; + // update the cached values + for (int index = 0; index < mBadgingEnabled.size(); index++) { + int userId = mBadgingEnabled.keyAt(index); + final boolean oldValue = mBadgingEnabled.get(userId); + final boolean newValue = Secure.getIntForUser(mContext.getContentResolver(), + Secure.NOTIFICATION_BADGING, + DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0; + mBadgingEnabled.put(userId, newValue); + changed |= oldValue != newValue; + } + if (changed) { + mRankingHandler.requestSort(false); + } + } + + public boolean badgingEnabled(UserHandle userHandle) { + int userId = userHandle.getIdentifier(); + if (mBadgingEnabled.indexOfKey(userId) < 0) { + mBadgingEnabled.put(userId, + Secure.getIntForUser(mContext.getContentResolver(), + Secure.NOTIFICATION_BADGING, + DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0); + } + return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE); + } + + private static class Record { static int UNKNOWN_UID = UserHandle.USER_NULL; diff --git a/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java b/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java index 0cf4994ebbce..262516dda7ad 100644 --- a/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java +++ b/services/tests/notification/src/com/android/server/notification/BadgeExtractorTest.java @@ -18,20 +18,19 @@ package com.android.server.notification; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; +import static android.app.NotificationManager.IMPORTANCE_HIGH; +import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; + import android.app.ActivityManager; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationChannel; import android.app.NotificationManager; -import android.content.Context; import android.os.UserHandle; +import android.provider.Settings.Secure; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -43,7 +42,7 @@ import org.mockito.MockitoAnnotations; @SmallTest @RunWith(AndroidJUnit4.class) -public class BadgeExtractorTest { +public class BadgeExtractorTest extends NotificationTestCase { @Mock RankingConfig mConfig; @@ -59,7 +58,11 @@ public class BadgeExtractorTest { MockitoAnnotations.initMocks(this); } - private NotificationRecord getNotificationRecord(NotificationChannel channel) { + private NotificationRecord getNotificationRecord(boolean showBadge, int importanceHigh) { + NotificationChannel channel = new NotificationChannel("a", "a", importanceHigh); + channel.setShowBadge(showBadge); + when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); + final Builder builder = new Builder(getContext()) .setContentTitle("foo") .setSmallIcon(android.R.drawable.sym_def_app_icon) @@ -73,10 +76,6 @@ public class BadgeExtractorTest { return r; } - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - // // Tests // @@ -86,13 +85,9 @@ public class BadgeExtractorTest { BadgeExtractor extractor = new BadgeExtractor(); extractor.setConfig(mConfig); + when(mConfig.badgingEnabled(mUser)).thenReturn(true); when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); - NotificationChannel channel = - new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED); - when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); - channel.setShowBadge(false); - - NotificationRecord r = getNotificationRecord(channel); + NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); extractor.process(r); @@ -104,13 +99,9 @@ public class BadgeExtractorTest { BadgeExtractor extractor = new BadgeExtractor(); extractor.setConfig(mConfig); + when(mConfig.badgingEnabled(mUser)).thenReturn(true); when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); - NotificationChannel channel = - new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_HIGH); - channel.setShowBadge(true); - when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); - - NotificationRecord r = getNotificationRecord(channel); + NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH); extractor.process(r); @@ -122,13 +113,9 @@ public class BadgeExtractorTest { BadgeExtractor extractor = new BadgeExtractor(); extractor.setConfig(mConfig); + when(mConfig.badgingEnabled(mUser)).thenReturn(true); when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); - NotificationChannel channel = - new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED); - channel.setShowBadge(true); - when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); - - NotificationRecord r = getNotificationRecord(channel); + NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED); extractor.process(r); @@ -140,13 +127,23 @@ public class BadgeExtractorTest { BadgeExtractor extractor = new BadgeExtractor(); extractor.setConfig(mConfig); + when(mConfig.badgingEnabled(mUser)).thenReturn(true); when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(false); - NotificationChannel channel = - new NotificationChannel("a", "a", NotificationManager.IMPORTANCE_UNSPECIFIED); - channel.setShowBadge(false); - when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel); + NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED); + + extractor.process(r); + + assertFalse(r.canShowBadge()); + } - NotificationRecord r = getNotificationRecord(channel); + @Test + public void testAppYesChannelYesUserNo() throws Exception { + BadgeExtractor extractor = new BadgeExtractor(); + extractor.setConfig(mConfig); + + when(mConfig.badgingEnabled(mUser)).thenReturn(false); + when(mConfig.canShowBadge(mPkg, mUid)).thenReturn(true); + NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH); extractor.process(r); diff --git a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java index d4904f5aecf7..39caa3ce147c 100644 --- a/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java +++ b/services/tests/notification/src/com/android/server/notification/BuzzBeepBlinkTest.java @@ -34,7 +34,6 @@ import android.app.ActivityManager; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; -import android.content.Context; import android.app.NotificationChannel; import android.graphics.Color; import android.media.AudioAttributes; @@ -47,7 +46,6 @@ import android.os.Vibrator; import android.os.VibrationEffect; import android.provider.Settings; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -69,7 +67,7 @@ import static org.mockito.Mockito.when; @SmallTest @RunWith(AndroidJUnit4.class) -public class BuzzBeepBlinkTest { +public class BuzzBeepBlinkTest extends NotificationTestCase { @Mock AudioManager mAudioManager; @Mock Vibrator mVibrator; @@ -328,10 +326,6 @@ public class BuzzBeepBlinkTest { eq(CUSTOM_LIGHT_COLOR), anyInt(), eq(CUSTOM_LIGHT_ON), eq(CUSTOM_LIGHT_OFF)); } - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - // // Tests // diff --git a/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java b/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java index 24cb72e8b0fb..f92bd842e815 100644 --- a/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java +++ b/services/tests/notification/src/com/android/server/notification/GlobalSortKeyComparatorTest.java @@ -25,7 +25,6 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.UserHandle; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -38,7 +37,7 @@ import java.util.List; @SmallTest @RunWith(AndroidJUnit4.class) -public class GlobalSortKeyComparatorTest { +public class GlobalSortKeyComparatorTest extends NotificationTestCase { private final String PKG = "PKG"; private final int UID = 1111111; @@ -46,24 +45,23 @@ public class GlobalSortKeyComparatorTest { @Test public void testComparator() throws Exception { - Notification n = new Notification.Builder( - InstrumentationRegistry.getContext(), TEST_CHANNEL_ID) + Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .build(); - NotificationRecord left = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord left = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), "", 1499), getDefaultChannel()); left.setGlobalSortKey("first"); - NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord right = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), "", 1499), getDefaultChannel()); right.setGlobalSortKey("second"); - NotificationRecord last = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord last = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), @@ -86,16 +84,15 @@ public class GlobalSortKeyComparatorTest { @Test public void testNoCrash_leftNull() throws Exception { - Notification n = new Notification.Builder( - InstrumentationRegistry.getContext(), TEST_CHANNEL_ID) + Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .build(); - NotificationRecord left = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord left = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), "", 1499), getDefaultChannel()); - NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord right = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), @@ -117,17 +114,16 @@ public class GlobalSortKeyComparatorTest { @Test public void testNoCrash_rightNull() throws Exception { - Notification n = new Notification.Builder( - InstrumentationRegistry.getContext(), TEST_CHANNEL_ID) + Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) .build(); - NotificationRecord left = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord left = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), "", 1499), getDefaultChannel()); left.setGlobalSortKey("not null"); - NotificationRecord right = new NotificationRecord(InstrumentationRegistry.getContext(), + NotificationRecord right = new NotificationRecord(getContext(), new StatusBarNotification(PKG, PKG, 1, "media", UID, UID, n, new UserHandle(UserHandle.myUserId()), diff --git a/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java b/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java index 05c33a4e1cea..8dd177921022 100644 --- a/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/GroupHelperTest.java @@ -34,10 +34,8 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.os.UserHandle; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -46,15 +44,11 @@ import java.util.List; @SmallTest @RunWith(AndroidJUnit4.class) -public class GroupHelperTest { +public class GroupHelperTest extends NotificationTestCase { private @Mock GroupHelper.Callback mCallback; private GroupHelper mGroupHelper; - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - @Before public void setUp() { MockitoAnnotations.initMocks(this); diff --git a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java index 3dbd803e0a76..d325e10b5897 100644 --- a/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java +++ b/services/tests/notification/src/com/android/server/notification/ImportanceExtractorTest.java @@ -24,10 +24,8 @@ import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.NotificationChannel; -import android.content.Context; import android.os.UserHandle; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -43,7 +41,7 @@ import static org.junit.Assert.assertEquals; @SmallTest @RunWith(AndroidJUnit4.class) -public class ImportanceExtractorTest { +public class ImportanceExtractorTest extends NotificationTestCase { @Mock RankingConfig mConfig; @@ -75,10 +73,6 @@ public class ImportanceExtractorTest { return r; } - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - // // Tests // diff --git a/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java b/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java index dde08fc12a50..1e5f96f7be3a 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationComparatorTest.java @@ -35,7 +35,6 @@ import android.os.UserHandle; import android.provider.Settings; import android.service.notification.StatusBarNotification; import android.telecom.TelecomManager; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -51,7 +50,7 @@ import java.util.List; @SmallTest @RunWith(AndroidJUnit4.class) -public class NotificationComparatorTest { +public class NotificationComparatorTest extends NotificationTestCase { @Mock Context mContext; @Mock TelecomManager mTm; @Mock RankingHandler handler; @@ -83,10 +82,8 @@ public class NotificationComparatorTest { MockitoAnnotations.initMocks(this); int userId = UserHandle.myUserId(); - when(mContext.getResources()).thenReturn( - InstrumentationRegistry.getTargetContext().getResources()); - when(mContext.getContentResolver()).thenReturn( - InstrumentationRegistry.getTargetContext().getContentResolver()); + when(mContext.getResources()).thenReturn(getContext().getResources()); + when(mContext.getContentResolver()).thenReturn(getContext().getContentResolver()); when(mContext.getPackageManager()).thenReturn(mPm); when(mContext.getSystemService(eq(Context.TELECOM_SERVICE))).thenReturn(mTm); when(mTm.getDefaultDialerPackage()).thenReturn(callPkg); diff --git a/services/tests/notification/src/com/android/server/notification/NotificationListenerServiceTest.java b/services/tests/notification/src/com/android/server/notification/NotificationListenerServiceTest.java index f0f4c4d66936..725e8f2937b0 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationListenerServiceTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationListenerServiceTest.java @@ -38,7 +38,7 @@ import java.util.List; @SmallTest @RunWith(AndroidJUnit4.class) -public class NotificationListenerServiceTest { +public class NotificationListenerServiceTest extends NotificationTestCase { private String[] mKeys = new String[] { "key", "key1", "key2", "key3"}; diff --git a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java index 177c02d63280..9afb2d2b210c 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -52,9 +52,9 @@ import android.graphics.Color; import android.os.Binder; import android.os.Process; import android.os.UserHandle; +import android.provider.Settings.Secure; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; import android.testing.TestableLooper.RunWithLooper; @@ -75,7 +75,7 @@ import com.android.server.lights.LightsManager; @RunWith(AndroidTestingRunner.class) @RunWithLooper -public class NotificationManagerServiceTest { +public class NotificationManagerServiceTest extends NotificationTestCase { private static final long WAIT_FOR_IDLE_TIMEOUT = 2; private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; private final int uid = Binder.getCallingUid(); @@ -86,7 +86,7 @@ public class NotificationManagerServiceTest { private IPackageManager mPackageManager; @Mock private PackageManager mPackageManagerClient; - private Context mContext = InstrumentationRegistry.getTargetContext(); + private Context mContext = getContext(); private final String PKG = mContext.getPackageName(); private TestableLooper mTestableLooper; @Mock @@ -122,6 +122,12 @@ public class NotificationManagerServiceTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + + // most tests assume badging is enabled + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 1, + UserHandle.getUserHandleForUid(uid).getIdentifier()); + mNotificationManagerService = new TestableNotificationManagerService(mContext); // MockPackageManager - default returns ApplicationInfo with matching calling UID diff --git a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java index 1c8ca84815d3..267d2a6d5c4f 100644 --- a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java +++ b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java @@ -40,7 +40,6 @@ import android.os.Build; import android.os.UserHandle; import android.provider.Settings; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; @@ -56,7 +55,7 @@ import java.util.Objects; @SmallTest @RunWith(AndroidJUnit4.class) -public class NotificationRecordTest { +public class NotificationRecordTest extends NotificationTestCase { private final Context mMockContext = Mockito.mock(Context.class); @Mock PackageManager mPm; @@ -96,8 +95,7 @@ public class NotificationRecordTest { public void setUp() { MockitoAnnotations.initMocks(this); - when(mMockContext.getResources()).thenReturn( - InstrumentationRegistry.getContext().getResources()); + when(mMockContext.getResources()).thenReturn(getContext().getResources()); when(mMockContext.getPackageManager()).thenReturn(mPm); legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1; diff --git a/services/tests/notification/src/com/android/server/notification/NotificationTestCase.java b/services/tests/notification/src/com/android/server/notification/NotificationTestCase.java new file mode 100644 index 000000000000..cc30aab68aa2 --- /dev/null +++ b/services/tests/notification/src/com/android/server/notification/NotificationTestCase.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2017 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.notification; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.testing.TestableContext; + +import org.junit.Rule; + + +public class NotificationTestCase { + @Rule + public final TestableContext mContext = + new TestableContext(InstrumentationRegistry.getContext(), null); + + protected Context getContext() { + return mContext; + } +} diff --git a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java index 30d68129c836..b7f51864331c 100644 --- a/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/RankingHelperTest.java @@ -49,11 +49,14 @@ import android.media.AudioAttributes; import android.net.Uri; import android.os.Build; import android.os.UserHandle; +import android.provider.Settings.Secure; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; +import android.testing.TestableContext; +import android.testing.TestableSettingsProvider; import android.util.ArrayMap; import android.util.Slog; import android.util.Xml; @@ -81,11 +84,13 @@ import static org.mockito.Mockito.when; @SmallTest @RunWith(AndroidJUnit4.class) -public class RankingHelperTest { +public class RankingHelperTest extends NotificationTestCase { private static final String PKG = "com.android.server.notification"; private static final int UID = 0; + private static final UserHandle USER = UserHandle.getUserHandleForUid(UID); private static final String UPDATED_PKG = "updatedPkg"; private static final int UID2 = 1111111; + private static final UserHandle USER2 = UserHandle.getUserHandleForUid(UID2); private static final String TEST_CHANNEL_ID = "test_channel_id"; @Mock NotificationUsageStats mUsageStats; @@ -106,10 +111,6 @@ public class RankingHelperTest { private RankingHelper mHelper; private AudioAttributes mAudioAttributes; - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); @@ -126,6 +127,9 @@ public class RankingHelperTest { InstrumentationRegistry.getContext().getResources()); when(mContext.getPackageManager()).thenReturn(mPm); when(mContext.getApplicationInfo()).thenReturn(legacy); + // most tests assume badging is enabled + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID)); mHelper = new RankingHelper(getContext(), mPm, mHandler, mUsageStats, new String[] {ImportanceExtractor.class.getName()}); @@ -1199,4 +1203,36 @@ public class RankingHelperTest { object.getInt("channelCount")); } } + + @Test + public void testBadgingOverrideTrue() throws Exception { + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 1, + USER.getIdentifier()); + mHelper.updateBadgingEnabled(); // would be called by settings observer + assertTrue(mHelper.badgingEnabled(USER)); + } + + @Test + public void testBadgingOverrideFalse() throws Exception { + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 0, + USER.getIdentifier()); + mHelper.updateBadgingEnabled(); // would be called by settings observer + assertFalse(mHelper.badgingEnabled(USER)); + } + + @Test + public void testBadgingOverrideUserIsolation() throws Exception { + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 0, + USER.getIdentifier()); + Secure.putIntForUser(getContext().getContentResolver(), + Secure.NOTIFICATION_BADGING, 1, + USER2.getIdentifier()); + mHelper.updateBadgingEnabled(); // would be called by settings observer + assertFalse(mHelper.badgingEnabled(USER)); + assertTrue(mHelper.badgingEnabled(USER2)); + } + } diff --git a/services/tests/notification/src/com/android/server/notification/RateEstimatorTest.java b/services/tests/notification/src/com/android/server/notification/RateEstimatorTest.java index 07f3162a7100..e354267dce21 100644 --- a/services/tests/notification/src/com/android/server/notification/RateEstimatorTest.java +++ b/services/tests/notification/src/com/android/server/notification/RateEstimatorTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertFalse; @SmallTest @RunWith(AndroidJUnit4.class) -public class RateEstimatorTest { +public class RateEstimatorTest extends NotificationTestCase { private long mTestStartTime; private RateEstimator mEstimator; diff --git a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java index bc25860e8e92..07b21fbc8f5f 100644 --- a/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java +++ b/services/tests/notification/src/com/android/server/notification/SnoozeHelperTest.java @@ -27,11 +27,9 @@ import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.Context; import android.os.SystemClock; import android.os.UserHandle; import android.service.notification.StatusBarNotification; -import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; import android.util.Slog; @@ -51,7 +49,7 @@ import static org.mockito.Mockito.when; @SmallTest @RunWith(AndroidJUnit4.class) -public class SnoozeHelperTest { +public class SnoozeHelperTest extends NotificationTestCase { private static final String TEST_CHANNEL_ID = "test_channel_id"; @Mock SnoozeHelper.Callback mCallback; @@ -60,10 +58,6 @@ public class SnoozeHelperTest { private SnoozeHelper mSnoozeHelper; - private Context getContext() { - return InstrumentationRegistry.getTargetContext(); - } - @Before public void setUp() { MockitoAnnotations.initMocks(this); diff --git a/services/tests/notification/src/com/android/server/notification/ValidateNotificationPeopleTest.java b/services/tests/notification/src/com/android/server/notification/ValidateNotificationPeopleTest.java index d09b858733e7..4ac0c65791fd 100644 --- a/services/tests/notification/src/com/android/server/notification/ValidateNotificationPeopleTest.java +++ b/services/tests/notification/src/com/android/server/notification/ValidateNotificationPeopleTest.java @@ -32,7 +32,7 @@ import static org.junit.Assert.assertEquals; @SmallTest @RunWith(AndroidJUnit4.class) -public class ValidateNotificationPeopleTest { +public class ValidateNotificationPeopleTest extends NotificationTestCase { @Test public void testNoExtra() throws Exception { |