Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsUpdater.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsUpdater.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsUpdater.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bd45fe82d327cda73bdd7ed8c78abd92325aefcd |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/ChannelsUpdater.java |
@@ -0,0 +1,69 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.notifications; |
+ |
+import android.app.NotificationManager; |
+import android.content.Context; |
+import android.content.SharedPreferences; |
+ |
+import org.chromium.base.BuildInfo; |
+import org.chromium.base.ContextUtils; |
+import org.chromium.base.VisibleForTesting; |
+ |
+/** |
+ * Contains helper methods for checking if we should update channels and updating them if so. |
+ */ |
+public class ChannelsUpdater { |
+ @VisibleForTesting |
+ static final String CHANNELS_VERSION_KEY = "channels_version_key"; |
+ /** |
+ * Version number identifying the current set of channels. This must be incremented whenever |
+ * any channels are added or removed from the set of channels created in |
+ * {@link ChannelsInitializer#initializeStartupChannels()} |
+ */ |
+ private static final int CHANNELS_VERSION = 0; |
+ private final ChannelsInitializer mChannelsInitializer; |
nyquist
2017/04/11 19:03:49
Optional: Extra newline before the non-static memb
awdf
2017/04/12 13:58:15
Done.
|
+ private final SharedPreferences mSharedPreferences; |
+ private boolean mIsAtLeastO; |
nyquist
2017/04/11 19:03:49
Nit: final here and mChannelsVersion
awdf
2017/04/12 13:58:15
Done.
|
+ private int mChannelsVersion; |
+ |
+ public static ChannelsUpdater getInstance() { |
+ return LazyHolder.INSTANCE; |
+ } |
+ |
+ private static class LazyHolder { |
+ public static final ChannelsUpdater INSTANCE = new ChannelsUpdater(BuildInfo.isAtLeastO(), |
+ ContextUtils.getAppSharedPreferences(), |
+ new NotificationManagerProxyImpl( |
+ (NotificationManager) ContextUtils.getApplicationContext().getSystemService( |
+ Context.NOTIFICATION_SERVICE)), |
+ CHANNELS_VERSION); |
+ } |
+ |
+ @VisibleForTesting |
+ ChannelsUpdater(boolean isAtLeastO, SharedPreferences sharedPreferences, |
+ NotificationManagerProxy notificationManagerProxy, int channelsVersion) { |
+ mIsAtLeastO = isAtLeastO; |
+ mSharedPreferences = sharedPreferences; |
+ mChannelsInitializer = new ChannelsInitializer(notificationManagerProxy); |
+ mChannelsVersion = channelsVersion; |
+ } |
+ |
+ public boolean shouldUpdateChannels() { |
+ return mIsAtLeastO |
+ && mSharedPreferences.getInt(CHANNELS_VERSION_KEY, -1) != mChannelsVersion; |
+ } |
+ |
+ public void updateChannels() { |
+ if (!mIsAtLeastO) return; |
+ mChannelsInitializer.deleteAllChannels(); |
+ mChannelsInitializer.initializeStartupChannels(); |
Peter Beverloo
2017/04/11 19:08:29
This assumes that removing and recreating channels
awdf
2017/04/12 13:58:15
Added a TODO. There's also the risk of a race cond
|
+ storeChannelVersionInPrefs(); |
+ } |
+ |
+ private void storeChannelVersionInPrefs() { |
+ mSharedPreferences.edit().putInt(CHANNELS_VERSION_KEY, mChannelsVersion).apply(); |
+ } |
+} |