Commit 4458357f authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Migrate SharedPrefs from notifications to ChromePreferenceKeys

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

Bug: 1022108
Change-Id: Ie5e834f38eae482407592226cea890818125926c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2011290Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733484}
parent 82d5ea4d
......@@ -8,9 +8,10 @@ import android.text.format.DateUtils;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
/**
* The {@link NotificationTriggerScheduler} singleton is responsible for scheduling notification
......@@ -18,7 +19,6 @@ import org.chromium.base.annotations.NativeMethods;
* Thread model: This class is to be run on the UI thread only.
*/
public class NotificationTriggerScheduler {
private static final String KEY_NEXT_TRIGGER = "notification_trigger_scheduler.next_trigger";
/** Clock to use so we can mock time in tests. */
public static interface Clock { public long currentTimeMillis(); }
......@@ -110,15 +110,18 @@ public class NotificationTriggerScheduler {
}
private long getNextTrigger() {
return ContextUtils.getAppSharedPreferences().getLong(KEY_NEXT_TRIGGER, Long.MAX_VALUE);
return SharedPreferencesManager.getInstance().readLong(
ChromePreferenceKeys.NOTIFICATIONS_NEXT_TRIGGER, Long.MAX_VALUE);
}
private void removeNextTrigger() {
ContextUtils.getAppSharedPreferences().edit().remove(KEY_NEXT_TRIGGER).apply();
SharedPreferencesManager.getInstance().removeKey(
ChromePreferenceKeys.NOTIFICATIONS_NEXT_TRIGGER);
}
private void setNextTrigger(long timestamp) {
ContextUtils.getAppSharedPreferences().edit().putLong(KEY_NEXT_TRIGGER, timestamp).apply();
SharedPreferencesManager.getInstance().writeLong(
ChromePreferenceKeys.NOTIFICATIONS_NEXT_TRIGGER, timestamp);
}
@NativeMethods
......
......@@ -8,7 +8,6 @@ import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.text.format.DateUtils;
......@@ -22,6 +21,8 @@ import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.metrics.CachedMetrics;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.chrome.browser.notifications.channels.ChannelDefinitions;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
......@@ -127,15 +128,12 @@ public class NotificationUmaTracker {
int NUM_ENTRIES = 13;
}
private static final String LAST_SHOWN_NOTIFICATION_TYPE_KEY =
"NotificationUmaTracker.LastShownNotificationType";
private static class LazyHolder {
private static final NotificationUmaTracker INSTANCE = new NotificationUmaTracker();
}
/** Cached objects. */
private final SharedPreferences mSharedPreferences;
private final SharedPreferencesManager mSharedPreferences;
private final NotificationManagerCompat mNotificationManager;
public static NotificationUmaTracker getInstance() {
......@@ -143,7 +141,7 @@ public class NotificationUmaTracker {
}
private NotificationUmaTracker() {
mSharedPreferences = ContextUtils.getAppSharedPreferences();
mSharedPreferences = SharedPreferencesManager.getInstance();
mNotificationManager = NotificationManagerCompat.from(ContextUtils.getApplicationContext());
}
......@@ -297,14 +295,17 @@ public class NotificationUmaTracker {
}
private void saveLastShownNotification(@SystemNotificationType int type) {
mSharedPreferences.edit().putInt(LAST_SHOWN_NOTIFICATION_TYPE_KEY, type).apply();
mSharedPreferences.writeInt(
ChromePreferenceKeys.NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE, type);
}
private void logPotentialBlockedCause() {
int lastType = mSharedPreferences.getInt(
LAST_SHOWN_NOTIFICATION_TYPE_KEY, SystemNotificationType.UNKNOWN);
int lastType = mSharedPreferences.readInt(
ChromePreferenceKeys.NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE,
SystemNotificationType.UNKNOWN);
if (lastType == -1) return;
mSharedPreferences.edit().remove(LAST_SHOWN_NOTIFICATION_TYPE_KEY).apply();
mSharedPreferences.removeKey(
ChromePreferenceKeys.NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE);
recordHistogram("Mobile.SystemNotification.BlockedAfterShown", lastType);
}
......
......@@ -430,6 +430,11 @@ public final class ChromePreferenceKeys {
public static final String LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE =
"LocaleManager_WAS_IN_SPECIAL_LOCALE";
public static final String NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE =
"NotificationUmaTracker.LastShownNotificationType";
public static final String NOTIFICATIONS_NEXT_TRIGGER =
"notification_trigger_scheduler.next_trigger";
public static final String NTP_SNIPPETS_IS_SCHEDULED = "ntp_snippets.is_scheduled";
/**
......@@ -794,6 +799,8 @@ public final class ChromePreferenceKeys {
LOCALE_MANAGER_PROMO_SHOWN,
LOCALE_MANAGER_SEARCH_ENGINE_PROMO_SHOW_STATE,
LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE,
NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE,
NOTIFICATIONS_NEXT_TRIGGER,
NTP_SNIPPETS_IS_SCHEDULED,
OFFLINE_INDICATOR_V2_ENABLED,
PAYMENTS_PAYMENT_COMPLETE_ONCE,
......
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