Commit b90a43b0 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move a pref from MediaCaptureNotificationService to ChromePreferenceKeys

Register it in ChromePreferenceKeys and use SharedPreferencesManager
consistently instead of SharedPreferences directly.

Bug: 1022108
Change-Id: I9212d59d33d1eb65a84222db85a103ad2cf4d425
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2025934Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737348}
parent e3e7e403
...@@ -8,7 +8,6 @@ import android.app.PendingIntent; ...@@ -8,7 +8,6 @@ import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
...@@ -29,6 +28,8 @@ import org.chromium.chrome.browser.notifications.NotificationMetadata; ...@@ -29,6 +28,8 @@ import org.chromium.chrome.browser.notifications.NotificationMetadata;
import org.chromium.chrome.browser.notifications.NotificationUmaTracker; import org.chromium.chrome.browser.notifications.NotificationUmaTracker;
import org.chromium.chrome.browser.notifications.PendingIntentProvider; import org.chromium.chrome.browser.notifications.PendingIntentProvider;
import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions; import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabWindowManager; import org.chromium.chrome.browser.tabmodel.TabWindowManager;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
...@@ -55,7 +56,6 @@ public class MediaCaptureNotificationService extends Service { ...@@ -55,7 +56,6 @@ public class MediaCaptureNotificationService extends Service {
private static final String NOTIFICATION_MEDIA_TYPE_EXTRA = "NotificationMediaType"; private static final String NOTIFICATION_MEDIA_TYPE_EXTRA = "NotificationMediaType";
private static final String NOTIFICATION_MEDIA_URL_EXTRA = "NotificationMediaUrl"; private static final String NOTIFICATION_MEDIA_URL_EXTRA = "NotificationMediaUrl";
private static final String WEBRTC_NOTIFICATION_IDS = "WebRTCNotificationIds";
private static final String TAG = "MediaCapture"; private static final String TAG = "MediaCapture";
@IntDef({MediaType.NO_MEDIA, MediaType.AUDIO_AND_VIDEO, MediaType.VIDEO_ONLY, @IntDef({MediaType.NO_MEDIA, MediaType.AUDIO_AND_VIDEO, MediaType.VIDEO_ONLY,
...@@ -69,14 +69,14 @@ public class MediaCaptureNotificationService extends Service { ...@@ -69,14 +69,14 @@ public class MediaCaptureNotificationService extends Service {
} }
private NotificationManagerProxy mNotificationManager; private NotificationManagerProxy mNotificationManager;
private SharedPreferences mSharedPreferences; private SharedPreferencesManager mSharedPreferences;
private final SparseIntArray mNotifications = new SparseIntArray(); private final SparseIntArray mNotifications = new SparseIntArray();
@Override @Override
public void onCreate() { public void onCreate() {
mNotificationManager = mNotificationManager =
new NotificationManagerProxyImpl(ContextUtils.getApplicationContext()); new NotificationManagerProxyImpl(ContextUtils.getApplicationContext());
mSharedPreferences = ContextUtils.getAppSharedPreferences(); mSharedPreferences = SharedPreferencesManager.getInstance();
super.onCreate(); super.onCreate();
} }
...@@ -129,16 +129,14 @@ public class MediaCaptureNotificationService extends Service { ...@@ -129,16 +129,14 @@ public class MediaCaptureNotificationService extends Service {
* after a browser crash which caused old notifications to exist). * after a browser crash which caused old notifications to exist).
*/ */
private void cancelPreviousWebRtcNotifications() { private void cancelPreviousWebRtcNotifications() {
Set<String> notificationIds = Set<String> notificationIds = mSharedPreferences.readStringSet(
mSharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, null); ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS, null);
if (notificationIds == null) return; if (notificationIds == null) return;
Iterator<String> iterator = notificationIds.iterator(); Iterator<String> iterator = notificationIds.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
mNotificationManager.cancel(NOTIFICATION_NAMESPACE, Integer.parseInt(iterator.next())); mNotificationManager.cancel(NOTIFICATION_NAMESPACE, Integer.parseInt(iterator.next()));
} }
SharedPreferences.Editor sharedPreferenceEditor = mSharedPreferences.edit(); mSharedPreferences.removeKey(ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS);
sharedPreferenceEditor.remove(MediaCaptureNotificationService.WEBRTC_NOTIFICATION_IDS);
sharedPreferenceEditor.apply();
} }
/** /**
...@@ -320,18 +318,16 @@ public class MediaCaptureNotificationService extends Service { ...@@ -320,18 +318,16 @@ public class MediaCaptureNotificationService extends Service {
* @param remove Boolean describing if the notification was added or removed. * @param remove Boolean describing if the notification was added or removed.
*/ */
private void updateSharedPreferencesEntry(int notificationId, boolean remove) { private void updateSharedPreferencesEntry(int notificationId, boolean remove) {
Set<String> notificationIds = Set<String> notificationIds = new HashSet<>(mSharedPreferences.readStringSet(
new HashSet<String>(mSharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS, new HashSet<>()));
new HashSet<String>()));
if (remove && !notificationIds.isEmpty() if (remove && !notificationIds.isEmpty()
&& notificationIds.contains(String.valueOf(notificationId))) { && notificationIds.contains(String.valueOf(notificationId))) {
notificationIds.remove(String.valueOf(notificationId)); notificationIds.remove(String.valueOf(notificationId));
} else if (!remove) { } else if (!remove) {
notificationIds.add(String.valueOf(notificationId)); notificationIds.add(String.valueOf(notificationId));
} }
SharedPreferences.Editor sharedPreferenceEditor = mSharedPreferences.edit(); mSharedPreferences.writeStringSet(
sharedPreferenceEditor.putStringSet(WEBRTC_NOTIFICATION_IDS, notificationIds); ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS, notificationIds);
sharedPreferenceEditor.apply();
} }
@Override @Override
...@@ -380,10 +376,9 @@ public class MediaCaptureNotificationService extends Service { ...@@ -380,10 +376,9 @@ public class MediaCaptureNotificationService extends Service {
private static boolean shouldStartService( private static boolean shouldStartService(
Context context, @MediaType int mediaType, int tabId) { Context context, @MediaType int mediaType, int tabId) {
if (mediaType != MediaType.NO_MEDIA) return true; if (mediaType != MediaType.NO_MEDIA) return true;
SharedPreferences sharedPreferences = SharedPreferencesManager sharedPreferences = SharedPreferencesManager.getInstance();
ContextUtils.getAppSharedPreferences(); Set<String> notificationIds = sharedPreferences.readStringSet(
Set<String> notificationIds = ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS, null);
sharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, null);
if (notificationIds != null if (notificationIds != null
&& !notificationIds.isEmpty() && !notificationIds.isEmpty()
&& notificationIds.contains(String.valueOf(tabId))) { && notificationIds.contains(String.valueOf(tabId))) {
...@@ -427,10 +422,9 @@ public class MediaCaptureNotificationService extends Service { ...@@ -427,10 +422,9 @@ public class MediaCaptureNotificationService extends Service {
* Clear any previous media notifications. * Clear any previous media notifications.
*/ */
public static void clearMediaNotifications() { public static void clearMediaNotifications() {
SharedPreferences sharedPreferences = SharedPreferencesManager sharedPreferences = SharedPreferencesManager.getInstance();
ContextUtils.getAppSharedPreferences(); Set<String> notificationIds = sharedPreferences.readStringSet(
Set<String> notificationIds = ChromePreferenceKeys.MEDIA_WEBRTC_NOTIFICATION_IDS, null);
sharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, null);
if (notificationIds == null || notificationIds.isEmpty()) return; if (notificationIds == null || notificationIds.isEmpty()) return;
Context context = ContextUtils.getApplicationContext(); Context context = ContextUtils.getApplicationContext();
......
...@@ -429,6 +429,8 @@ public final class ChromePreferenceKeys { ...@@ -429,6 +429,8 @@ public final class ChromePreferenceKeys {
public static final String LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE = public static final String LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE =
"LocaleManager_WAS_IN_SPECIAL_LOCALE"; "LocaleManager_WAS_IN_SPECIAL_LOCALE";
public static final String MEDIA_WEBRTC_NOTIFICATION_IDS = "WebRTCNotificationIds";
public static final String NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE = public static final String NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE =
"NotificationUmaTracker.LastShownNotificationType"; "NotificationUmaTracker.LastShownNotificationType";
public static final String NOTIFICATIONS_NEXT_TRIGGER = public static final String NOTIFICATIONS_NEXT_TRIGGER =
...@@ -833,6 +835,7 @@ public final class ChromePreferenceKeys { ...@@ -833,6 +835,7 @@ public final class ChromePreferenceKeys {
LOCALE_MANAGER_PROMO_SHOWN, LOCALE_MANAGER_PROMO_SHOWN,
LOCALE_MANAGER_SEARCH_ENGINE_PROMO_SHOW_STATE, LOCALE_MANAGER_SEARCH_ENGINE_PROMO_SHOW_STATE,
LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE, LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE,
MEDIA_WEBRTC_NOTIFICATION_IDS,
NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE, NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE,
NOTIFICATIONS_NEXT_TRIGGER, NOTIFICATIONS_NEXT_TRIGGER,
NTP_SNIPPETS_IS_SCHEDULED, NTP_SNIPPETS_IS_SCHEDULED,
......
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