Commit c4d14c85 authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

notifications: Add areNotificationsEnabled() to NotificationManagerProxy

This allows mocking of the NotificationManager in places where
areNotificationsEnabled() is called.

Added in preparation of the mobile tracing feature, which requires
mocking this method for tests.

TBR=mvanouwerkerk@chromium.org,nyquist@chromium.org

Bug: 898512
Change-Id: Ic739269858970a2365ae6772f269e0aff19d4f5c
Reviewed-on: https://chromium-review.googlesource.com/c/1321730Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606272}
parent 7d284aff
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
package org.chromium.chrome.browser.notifications; package org.chromium.chrome.browser.notifications;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
...@@ -30,8 +29,8 @@ public class NotificationBuilderFactory { ...@@ -30,8 +29,8 @@ public class NotificationBuilderFactory {
boolean preferCompat, String channelId) { boolean preferCompat, String channelId) {
Context context = ContextUtils.getApplicationContext(); Context context = ContextUtils.getApplicationContext();
NotificationManagerProxyImpl notificationManagerProxy = new NotificationManagerProxyImpl( NotificationManagerProxyImpl notificationManagerProxy =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)); new NotificationManagerProxyImpl(context);
ChannelsInitializer channelsInitializer = ChannelsInitializer channelsInitializer =
new ChannelsInitializer(notificationManagerProxy, context.getResources()); new ChannelsInitializer(notificationManagerProxy, context.getResources());
......
...@@ -20,6 +20,10 @@ import java.util.List; ...@@ -20,6 +20,10 @@ import java.util.List;
* https://developer.android.com/reference/android/app/NotificationManager.html</a> * https://developer.android.com/reference/android/app/NotificationManager.html</a>
*/ */
public interface NotificationManagerProxy { public interface NotificationManagerProxy {
// Implemented by NotificationManagerCompat and thus available on all API levels.
// Note that on < Kitkat, this always returns true.
boolean areNotificationsEnabled();
void cancel(int id); void cancel(int id);
void cancel(String tag, int id); void cancel(String tag, int id);
void cancelAll(); void cancelAll();
......
...@@ -9,7 +9,9 @@ import android.app.Notification; ...@@ -9,7 +9,9 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import java.util.List; import java.util.List;
...@@ -18,10 +20,18 @@ import java.util.List; ...@@ -18,10 +20,18 @@ import java.util.List;
* normal Android Notification Manager. * normal Android Notification Manager.
*/ */
public class NotificationManagerProxyImpl implements NotificationManagerProxy { public class NotificationManagerProxyImpl implements NotificationManagerProxy {
private final Context mContext;
private final NotificationManager mNotificationManager; private final NotificationManager mNotificationManager;
public NotificationManagerProxyImpl(NotificationManager notificationManager) { public NotificationManagerProxyImpl(Context context) {
mNotificationManager = notificationManager; mContext = context;
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public boolean areNotificationsEnabled() {
return NotificationManagerCompat.from(mContext).areNotificationsEnabled();
} }
@Override @Override
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.notifications; package org.chromium.chrome.browser.notifications;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.RemoteInput; import android.app.RemoteInput;
import android.content.Context; import android.content.Context;
...@@ -130,8 +129,7 @@ public class NotificationPlatformBridge { ...@@ -130,8 +129,7 @@ public class NotificationPlatformBridge {
if (sNotificationManagerOverride != null) { if (sNotificationManagerOverride != null) {
mNotificationManager = sNotificationManagerOverride; mNotificationManager = sNotificationManagerOverride;
} else { } else {
mNotificationManager = new NotificationManagerProxyImpl( mNotificationManager = new NotificationManagerProxyImpl(context);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
} }
mTwaClient = new TrustedWebActivityClient( mTwaClient = new TrustedWebActivityClient(
new TrustedWebActivityServiceConnectionManager(context)); new TrustedWebActivityServiceConnectionManager(context));
......
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
package org.chromium.chrome.browser.notifications.channels; package org.chromium.chrome.browser.notifications.channels;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
...@@ -35,10 +33,8 @@ public class ChannelsUpdater { ...@@ -35,10 +33,8 @@ public class ChannelsUpdater {
public static final ChannelsUpdater INSTANCE = Build.VERSION.SDK_INT < Build.VERSION_CODES.O public static final ChannelsUpdater INSTANCE = Build.VERSION.SDK_INT < Build.VERSION_CODES.O
? new ChannelsUpdater(false /* isAtLeastO */, null, null, -1) ? new ChannelsUpdater(false /* isAtLeastO */, null, null, -1)
: new ChannelsUpdater(true /* isAtLeastO */, ContextUtils.getAppSharedPreferences(), : new ChannelsUpdater(true /* isAtLeastO */, ContextUtils.getAppSharedPreferences(),
new ChannelsInitializer( new ChannelsInitializer(new NotificationManagerProxyImpl(
new NotificationManagerProxyImpl( ContextUtils.getApplicationContext()),
(NotificationManager) ContextUtils.getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE)),
ContextUtils.getApplicationContext().getResources()), ContextUtils.getApplicationContext().getResources()),
ChannelDefinitions.CHANNELS_VERSION); ChannelDefinitions.CHANNELS_VERSION);
} }
......
...@@ -8,7 +8,6 @@ import android.annotation.TargetApi; ...@@ -8,7 +8,6 @@ import android.annotation.TargetApi;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationChannelGroup; import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
...@@ -38,10 +37,8 @@ public class SiteChannelsManager { ...@@ -38,10 +37,8 @@ public class SiteChannelsManager {
} }
private static class LazyHolder { private static class LazyHolder {
public static final SiteChannelsManager INSTANCE = public static final SiteChannelsManager INSTANCE = new SiteChannelsManager(
new SiteChannelsManager(new NotificationManagerProxyImpl( new NotificationManagerProxyImpl(ContextUtils.getApplicationContext()));
(NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE)));
} }
@VisibleForTesting @VisibleForTesting
......
...@@ -457,9 +457,7 @@ public class ContentSuggestionsNotifier { ...@@ -457,9 +457,7 @@ public class ContentSuggestionsNotifier {
if (prefs.getBoolean(PREF_CHANNEL_CREATED, false)) return false; if (prefs.getBoolean(PREF_CHANNEL_CREATED, false)) return false;
ChannelsInitializer initializer = new ChannelsInitializer( ChannelsInitializer initializer = new ChannelsInitializer(
new NotificationManagerProxyImpl( new NotificationManagerProxyImpl(ContextUtils.getApplicationContext()),
(NotificationManager) ContextUtils.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE)),
ContextUtils.getApplicationContext().getResources()); ContextUtils.getApplicationContext().getResources());
if (enabled) { if (enabled) {
initializer.ensureInitialized(ChannelDefinitions.ChannelId.CONTENT_SUGGESTIONS); initializer.ensureInitialized(ChannelDefinitions.ChannelId.CONTENT_SUGGESTIONS);
...@@ -482,9 +480,8 @@ public class ContentSuggestionsNotifier { ...@@ -482,9 +480,8 @@ public class ContentSuggestionsNotifier {
SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
if (!prefs.getBoolean(PREF_CHANNEL_CREATED, false)) return; if (!prefs.getBoolean(PREF_CHANNEL_CREATED, false)) return;
NotificationManagerProxy manager = new NotificationManagerProxyImpl( NotificationManagerProxy manager =
(NotificationManager) ContextUtils.getApplicationContext().getSystemService( new NotificationManagerProxyImpl(ContextUtils.getApplicationContext());
Context.NOTIFICATION_SERVICE));
manager.deleteNotificationChannel(ChannelDefinitions.ChannelId.CONTENT_SUGGESTIONS); manager.deleteNotificationChannel(ChannelDefinitions.ChannelId.CONTENT_SUGGESTIONS);
prefs.edit().remove(PREF_CHANNEL_CREATED).apply(); prefs.edit().remove(PREF_CHANNEL_CREATED).apply();
} }
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.sync; package org.chromium.chrome.browser.sync;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
...@@ -40,9 +39,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC ...@@ -40,9 +39,8 @@ public class SyncNotificationController implements ProfileSyncService.SyncStateC
private final ProfileSyncService mProfileSyncService; private final ProfileSyncService mProfileSyncService;
public SyncNotificationController() { public SyncNotificationController() {
mNotificationManager = new NotificationManagerProxyImpl( mNotificationManager =
(NotificationManager) ContextUtils.getApplicationContext().getSystemService( new NotificationManagerProxyImpl(ContextUtils.getApplicationContext());
Context.NOTIFICATION_SERVICE));
mProfileSyncService = ProfileSyncService.get(); mProfileSyncService = ProfileSyncService.get();
assert mProfileSyncService != null; assert mProfileSyncService != null;
} }
......
...@@ -66,8 +66,7 @@ public class ChannelsInitializerTest { ...@@ -66,8 +66,7 @@ public class ChannelsInitializerTest {
mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess(); mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess();
mContext = InstrumentationRegistry.getTargetContext(); mContext = InstrumentationRegistry.getTargetContext();
mNotificationManagerProxy = new NotificationManagerProxyImpl( mNotificationManagerProxy = new NotificationManagerProxyImpl(mContext);
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE));
mChannelsInitializer = mChannelsInitializer =
new ChannelsInitializer(mNotificationManagerProxy, mContext.getResources()); new ChannelsInitializer(mNotificationManagerProxy, mContext.getResources());
......
...@@ -61,8 +61,7 @@ public class ChannelsUpdaterTest { ...@@ -61,8 +61,7 @@ public class ChannelsUpdaterTest {
mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess(); mNativeLibraryTestRule.loadNativeLibraryNoBrowserProcess();
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
mNotificationManagerProxy = new NotificationManagerProxyImpl( mNotificationManagerProxy = new NotificationManagerProxyImpl(context);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
mMockResources = context.getResources(); mMockResources = context.getResources();
......
...@@ -10,7 +10,6 @@ import static org.hamcrest.Matchers.is; ...@@ -10,7 +10,6 @@ import static org.hamcrest.Matchers.is;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
...@@ -62,8 +61,8 @@ public class SiteChannelsManagerTest { ...@@ -62,8 +61,8 @@ public class SiteChannelsManagerTest {
mChromeBrowserTestRule.loadNativeLibraryAndInitBrowserProcess(); mChromeBrowserTestRule.loadNativeLibraryAndInitBrowserProcess();
Context mContext = InstrumentationRegistry.getTargetContext(); Context mContext = InstrumentationRegistry.getTargetContext();
NotificationManagerProxy notificationManagerProxy = new NotificationManagerProxyImpl( NotificationManagerProxy notificationManagerProxy =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)); new NotificationManagerProxyImpl(mContext);
clearExistingSiteChannels(notificationManagerProxy); clearExistingSiteChannels(notificationManagerProxy);
mSiteChannelsManager = new SiteChannelsManager(notificationManagerProxy); mSiteChannelsManager = new SiteChannelsManager(notificationManagerProxy);
} }
......
...@@ -46,6 +46,8 @@ public class MockNotificationManagerProxy implements NotificationManagerProxy { ...@@ -46,6 +46,8 @@ public class MockNotificationManagerProxy implements NotificationManagerProxy {
private int mMutationCount; private int mMutationCount;
private boolean mNotificationsEnabled = true;
public MockNotificationManagerProxy() { public MockNotificationManagerProxy() {
mNotifications = new LinkedHashMap<>(); mNotifications = new LinkedHashMap<>();
mMutationCount = 0; mMutationCount = 0;
...@@ -76,6 +78,15 @@ public class MockNotificationManagerProxy implements NotificationManagerProxy { ...@@ -76,6 +78,15 @@ public class MockNotificationManagerProxy implements NotificationManagerProxy {
return mutationCount; return mutationCount;
} }
public void setNotificationsEnabled(boolean enabled) {
mNotificationsEnabled = enabled;
}
@Override
public boolean areNotificationsEnabled() {
return mNotificationsEnabled;
}
@Override @Override
public void cancel(int id) { public void cancel(int id) {
cancel(null /* tag */, id); cancel(null /* tag */, id);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment