Commit 9d73e393 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move SharedPrefs from PrefetchPrefs to ChromePreferenceKeys.

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

Add testing coverage for the return value of
SharedPreferencesManager#incrementInt() that was missing.

Bug: 1022108
Change-Id: I49a3b72b30dbfc0dfb7dd3c3747cb85523f2e4ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2001222
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarCarlos Knippschild <carlosk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732993}
parent 3b24d14e
......@@ -139,9 +139,7 @@ public class OfflineNotificationBackgroundTask extends NativeBackgroundTask {
return StartBeforeNativeResult.DONE;
}
int offlineCounter = PrefetchPrefs.getOfflineCounter();
offlineCounter++;
PrefetchPrefs.setOfflineCounter(offlineCounter);
int offlineCounter = PrefetchPrefs.incrementOfflineCounter();
if (offlineCounter < OFFLINE_POLLING_ATTEMPTS) {
scheduleTask(DETECTION_MODE_OFFLINE);
return StartBeforeNativeResult.DONE;
......
......@@ -4,7 +4,8 @@
package org.chromium.chrome.browser.offlinepages.prefetch;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
/**
* Preferences used to provide prefetch related notifications.
......@@ -15,98 +16,92 @@ import org.chromium.base.ContextUtils;
* reacting to it
*/
public class PrefetchPrefs {
static final String PREF_PREFETCH_NOTIFICATION_ENABLED = "prefetch_notification_enabled";
static final String PREF_PREFETCH_HAS_NEW_PAGES = "prefetch_notification_has_new_pages";
static final String PREF_PREFETCH_NOTIFICATION_TIME = "prefetch_notification_shown_time";
static final String PREF_PREFETCH_OFFLINE_COUNTER = "prefetch_notification_offline_counter";
static final String PREF_PREFETCH_IGNORED_NOTIFICATION_COUNTER =
"prefetch_notification_ignored_counter";
/**
* Sets the flag to tell whether prefetch notifications are enabled in user settings.
*/
public static void setNotificationEnabled(boolean enabled) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(PREF_PREFETCH_NOTIFICATION_ENABLED, enabled)
.apply();
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.PREFETCH_NOTIFICATION_ENABLED, enabled);
}
/**
* Returns the flag to tell whether prefetch notifications are enabled in user settings.
*/
public static boolean getNotificationEnabled() {
return ContextUtils.getAppSharedPreferences().getBoolean(
PREF_PREFETCH_NOTIFICATION_ENABLED, true);
return SharedPreferencesManager.getInstance().readBoolean(
ChromePreferenceKeys.PREFETCH_NOTIFICATION_ENABLED, true);
}
/**
* Sets the flag to tell whether new pages have been saved.
*/
public static void setHasNewPages(boolean hasNewPages) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(PREF_PREFETCH_HAS_NEW_PAGES, hasNewPages)
.apply();
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.PREFETCH_HAS_NEW_PAGES, hasNewPages);
}
/**
* Returns the flag to tell whether new pages have been saved.
*/
public static boolean getHasNewPages() {
return ContextUtils.getAppSharedPreferences().getBoolean(
PREF_PREFETCH_HAS_NEW_PAGES, false);
return SharedPreferencesManager.getInstance().readBoolean(
ChromePreferenceKeys.PREFETCH_HAS_NEW_PAGES, false);
}
/**
* Sets the last time a notification is shown, in milliseconds since the epoch.
*/
public static void setNotificationLastShownTime(long timeInMillis) {
ContextUtils.getAppSharedPreferences()
.edit()
.putLong(PREF_PREFETCH_NOTIFICATION_TIME, timeInMillis)
.apply();
SharedPreferencesManager.getInstance().writeLong(
ChromePreferenceKeys.PREFETCH_NOTIFICATION_TIME, timeInMillis);
}
/**
* Returns the last time a notification is shown, in milliseconds since the epoch.
*/
public static long getNotificationLastShownTime() {
return ContextUtils.getAppSharedPreferences().getLong(PREF_PREFETCH_NOTIFICATION_TIME, 0);
return SharedPreferencesManager.getInstance().readLong(
ChromePreferenceKeys.PREFETCH_NOTIFICATION_TIME);
}
/**
* Sets the offline counter.
*/
public static void setOfflineCounter(int counter) {
ContextUtils.getAppSharedPreferences()
.edit()
.putInt(PREF_PREFETCH_OFFLINE_COUNTER, counter)
.apply();
SharedPreferencesManager.getInstance().writeInt(
ChromePreferenceKeys.PREFETCH_OFFLINE_COUNTER, counter);
}
/**
* Returns the offline counter.
* Increments and returns the offline counter.
*/
public static int getOfflineCounter() {
return ContextUtils.getAppSharedPreferences().getInt(PREF_PREFETCH_OFFLINE_COUNTER, 0);
public static int incrementOfflineCounter() {
return SharedPreferencesManager.getInstance().incrementInt(
ChromePreferenceKeys.PREFETCH_OFFLINE_COUNTER);
}
/**
* Sets the ignored notification counter.
*/
public static void setIgnoredNotificationCounter(int counter) {
ContextUtils.getAppSharedPreferences()
.edit()
.putInt(PREF_PREFETCH_IGNORED_NOTIFICATION_COUNTER, counter)
.apply();
SharedPreferencesManager.getInstance().writeInt(
ChromePreferenceKeys.PREFETCH_IGNORED_NOTIFICATION_COUNTER, counter);
}
/**
* Returns the ignored notification counter.
*/
public static int getIgnoredNotificationCounter() {
return ContextUtils.getAppSharedPreferences().getInt(
PREF_PREFETCH_IGNORED_NOTIFICATION_COUNTER, 0);
return SharedPreferencesManager.getInstance().readInt(
ChromePreferenceKeys.PREFETCH_IGNORED_NOTIFICATION_COUNTER);
}
/**
* Increments and returns the ignored notification counter.
*/
public static int incrementIgnoredNotificationCounter() {
return SharedPreferencesManager.getInstance().incrementInt(
ChromePreferenceKeys.PREFETCH_IGNORED_NOTIFICATION_COUNTER);
}
}
......@@ -146,8 +146,7 @@ public class PrefetchedPagesNotifier {
manager.notify(notification);
// Increment ignored notification counter. This will be reset on click.
PrefetchPrefs.setIgnoredNotificationCounter(
PrefetchPrefs.getIgnoredNotificationCounter() + 1);
PrefetchPrefs.incrementIgnoredNotificationCounter();
// Metrics tracking
recordNotificationAction(NOTIFICATION_ACTION_SHOWN);
......
......@@ -446,6 +446,13 @@ public final class ChromePreferenceKeys {
/** Preference to indicate whether payment request has been completed successfully once.*/
public static final String PAYMENTS_PAYMENT_COMPLETE_ONCE = "payment_complete_once";
public static final String PREFETCH_HAS_NEW_PAGES = "prefetch_notification_has_new_pages";
public static final String PREFETCH_IGNORED_NOTIFICATION_COUNTER =
"prefetch_notification_ignored_counter";
public static final String PREFETCH_NOTIFICATION_ENABLED = "prefetch_notification_enabled";
public static final String PREFETCH_NOTIFICATION_TIME = "prefetch_notification_shown_time";
public static final String PREFETCH_OFFLINE_COUNTER = "prefetch_notification_offline_counter";
public static final String PRIVACY_METRICS_REPORTING = "metrics_reporting";
public static final String PRIVACY_METRICS_IN_SAMPLE = "in_metrics_sample";
public static final String PRIVACY_NETWORK_PREDICTIONS = "network_predictions";
......@@ -766,6 +773,11 @@ public final class ChromePreferenceKeys {
NTP_SNIPPETS_IS_SCHEDULED,
OFFLINE_INDICATOR_V2_ENABLED,
PAYMENTS_PAYMENT_COMPLETE_ONCE,
PREFETCH_HAS_NEW_PAGES,
PREFETCH_IGNORED_NOTIFICATION_COUNTER,
PREFETCH_NOTIFICATION_ENABLED,
PREFETCH_NOTIFICATION_TIME,
PREFETCH_OFFLINE_COUNTER,
PRIVACY_ALLOW_PRERENDER_OLD,
PRIVACY_BANDWIDTH_NO_CELLULAR_OLD,
PRIVACY_BANDWIDTH_OLD,
......
......@@ -70,16 +70,18 @@ public class SharedPreferencesManagerTest {
@SmallTest
public void testIncrementInt() {
mSubject.writeInt("int_key", 100);
mSubject.incrementInt("int_key");
int result = mSubject.incrementInt("int_key");
assertEquals(101, result);
assertEquals(101, mSubject.readInt("int_key"));
}
@Test
@SmallTest
public void testIncrementIntDefault() {
mSubject.incrementInt("int_key");
int result = mSubject.incrementInt("int_key");
assertEquals(1, result);
assertEquals(1, mSubject.readInt("int_key"));
}
......
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