Commit 280bbbe1 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move SharedPrefs from MainIntentBehaviorMetrics to ChromePreferenceKeys

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

Bug: 1022108
Change-Id: I792f8fd240f72ca0a6cf643fe9174962f90bd569
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2018250
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737845}
parent f03bbbd5
...@@ -5,23 +5,22 @@ ...@@ -5,23 +5,22 @@
package org.chromium.chrome.browser.metrics; package org.chromium.chrome.browser.metrics;
import android.app.Activity; import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ActivityState; import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationState; import org.chromium.base.ApplicationState;
import org.chromium.base.ApplicationStatus; import org.chromium.base.ApplicationStatus;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction; import org.chromium.base.metrics.RecordUserAction;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.ntp.NewTabPage; import org.chromium.chrome.browser.ntp.NewTabPage;
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.tab.TabLaunchType; import org.chromium.chrome.browser.tab.TabLaunchType;
import org.chromium.chrome.browser.tab.TabSelectionType; import org.chromium.chrome.browser.tab.TabSelectionType;
...@@ -65,11 +64,6 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat ...@@ -65,11 +64,6 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat
}); });
} }
@VisibleForTesting
static final String LAUNCH_TIMESTAMP_PREF = "MainIntent.LaunchTimestamp";
@VisibleForTesting
static final String LAUNCH_COUNT_PREF = "MainIntent.LaunchCount";
// Constants used to log UMA "enum" histogram about launch type. // Constants used to log UMA "enum" histogram about launch type.
private static final int LAUNCH_FROM_ICON = 0; private static final int LAUNCH_FROM_ICON = 0;
private static final int LAUNCH_NOT_FROM_ICON = 1; private static final int LAUNCH_NOT_FROM_ICON = 1;
...@@ -269,11 +263,11 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat ...@@ -269,11 +263,11 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat
if (sLoggedLaunchBehavior) return; if (sLoggedLaunchBehavior) return;
sLoggedLaunchBehavior = true; sLoggedLaunchBehavior = true;
SharedPreferences pref = ContextUtils.getAppSharedPreferences(); SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
SharedPreferences.Editor editor = pref.edit();
long current = System.currentTimeMillis(); long current = System.currentTimeMillis();
long timestamp = pref.getLong(LAUNCH_TIMESTAMP_PREF, 0); long timestamp =
int count = pref.getInt(LAUNCH_COUNT_PREF, 0); prefs.readLong(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, 0);
int count = prefs.readInt(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 0);
if (current - timestamp > DateUtils.DAY_IN_MILLIS) { if (current - timestamp > DateUtils.DAY_IN_MILLIS) {
// Log count if it's not first launch of Chrome. // Log count if it's not first launch of Chrome.
...@@ -281,11 +275,11 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat ...@@ -281,11 +275,11 @@ public class MainIntentBehaviorMetrics implements ApplicationStatus.ActivityStat
RecordHistogram.recordCountHistogram("MobileStartup.DailyLaunchCount", count); RecordHistogram.recordCountHistogram("MobileStartup.DailyLaunchCount", count);
} }
count = 0; count = 0;
editor.putLong(LAUNCH_TIMESTAMP_PREF, current); prefs.writeLong(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, current);
} }
count++; count++;
editor.putInt(LAUNCH_COUNT_PREF, count).apply(); prefs.writeInt(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, count);
RecordHistogram.recordEnumeratedHistogram("MobileStartup.LaunchType", RecordHistogram.recordEnumeratedHistogram("MobileStartup.LaunchType",
isLaunchFromIcon ? LAUNCH_FROM_ICON : LAUNCH_NOT_FROM_ICON, LAUNCH_BOUNDARY); isLaunchFromIcon ? LAUNCH_FROM_ICON : LAUNCH_NOT_FROM_ICON, LAUNCH_BOUNDARY);
......
...@@ -23,7 +23,6 @@ import org.junit.Rule; ...@@ -23,7 +23,6 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.metrics.RecordHistogram; import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
...@@ -35,6 +34,8 @@ import org.chromium.chrome.browser.bookmarks.BookmarkActivity; ...@@ -35,6 +34,8 @@ import org.chromium.chrome.browser.bookmarks.BookmarkActivity;
import org.chromium.chrome.browser.download.DownloadActivity; import org.chromium.chrome.browser.download.DownloadActivity;
import org.chromium.chrome.browser.history.HistoryActivity; import org.chromium.chrome.browser.history.HistoryActivity;
import org.chromium.chrome.browser.omnibox.UrlBar; import org.chromium.chrome.browser.omnibox.UrlBar;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.settings.SettingsActivity; import org.chromium.chrome.browser.settings.SettingsActivity;
import org.chromium.chrome.browser.tab.TabLaunchType; import org.chromium.chrome.browser.tab.TabLaunchType;
import org.chromium.chrome.browser.tabmodel.TabModelUtils; import org.chromium.chrome.browser.tabmodel.TabModelUtils;
...@@ -200,26 +201,19 @@ public class MainIntentBehaviorMetricsIntegrationTest { ...@@ -200,26 +201,19 @@ public class MainIntentBehaviorMetricsIntegrationTest {
@Test @Test
public void testLaunch_Duration_MoreThan_1Day() { public void testLaunch_Duration_MoreThan_1Day() {
long timestamp = System.currentTimeMillis() - 25 * HOURS_IN_MS; long timestamp = System.currentTimeMillis() - 25 * HOURS_IN_MS;
ContextUtils.getAppSharedPreferences() SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
.edit() prefs.writeLongSync(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, timestamp);
.putLong(MainIntentBehaviorMetrics.LAUNCH_TIMESTAMP_PREF, timestamp) prefs.writeIntSync(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 10);
.commit();
ContextUtils.getAppSharedPreferences()
.edit()
.putInt(MainIntentBehaviorMetrics.LAUNCH_COUNT_PREF, 10)
.commit();
mActivityTestRule.startMainActivityFromLauncher(); mActivityTestRule.startMainActivityFromLauncher();
assertEquals(1, assertEquals(1,
RecordHistogram.getHistogramValueCountForTesting( RecordHistogram.getHistogramValueCountForTesting(
"MobileStartup.DailyLaunchCount", 10)); "MobileStartup.DailyLaunchCount", 10));
assertEquals(1, assertEquals(1, prefs.readInt(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 0));
ContextUtils.getAppSharedPreferences().getInt(
MainIntentBehaviorMetrics.LAUNCH_COUNT_PREF, 0));
long newTimestamp = ContextUtils.getAppSharedPreferences().getLong( long newTimestamp =
MainIntentBehaviorMetrics.LAUNCH_TIMESTAMP_PREF, 0); prefs.readLong(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, 0);
assertNotEquals(timestamp, newTimestamp); assertNotEquals(timestamp, newTimestamp);
assertNotEquals(0, newTimestamp); assertNotEquals(0, newTimestamp);
} }
...@@ -228,27 +222,19 @@ public class MainIntentBehaviorMetricsIntegrationTest { ...@@ -228,27 +222,19 @@ public class MainIntentBehaviorMetricsIntegrationTest {
@Test @Test
public void testLaunch_Duration_LessThan_1Day() { public void testLaunch_Duration_LessThan_1Day() {
long timestamp = System.currentTimeMillis() - 12 * HOURS_IN_MS; long timestamp = System.currentTimeMillis() - 12 * HOURS_IN_MS;
ContextUtils.getAppSharedPreferences() SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
.edit() prefs.writeLongSync(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, timestamp);
.putLong(MainIntentBehaviorMetrics.LAUNCH_TIMESTAMP_PREF, timestamp) prefs.writeIntSync(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 1);
.commit();
ContextUtils.getAppSharedPreferences()
.edit()
.putInt(MainIntentBehaviorMetrics.LAUNCH_COUNT_PREF, 1)
.commit();
mActivityTestRule.startMainActivityFromLauncher(); mActivityTestRule.startMainActivityFromLauncher();
assertEquals(0, assertEquals(0,
RecordHistogram.getHistogramValueCountForTesting( RecordHistogram.getHistogramValueCountForTesting(
"MobileStartup.DailyLaunchCount", 1)); "MobileStartup.DailyLaunchCount", 1));
assertEquals(2, assertEquals(2, prefs.readInt(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 0));
ContextUtils.getAppSharedPreferences().getInt(
MainIntentBehaviorMetrics.LAUNCH_COUNT_PREF, 0));
assertEquals(timestamp, assertEquals(timestamp,
ContextUtils.getAppSharedPreferences().getLong( prefs.readLong(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, 0));
MainIntentBehaviorMetrics.LAUNCH_TIMESTAMP_PREF, 0));
} }
@MediumTest @MediumTest
...@@ -258,10 +244,9 @@ public class MainIntentBehaviorMetricsIntegrationTest { ...@@ -258,10 +244,9 @@ public class MainIntentBehaviorMetricsIntegrationTest {
try { try {
MainIntentBehaviorMetrics.setTimeoutDurationMsForTesting(0); MainIntentBehaviorMetrics.setTimeoutDurationMsForTesting(0);
long timestamp = System.currentTimeMillis() - 12 * HOURS_IN_MS; long timestamp = System.currentTimeMillis() - 12 * HOURS_IN_MS;
ContextUtils.getAppSharedPreferences() SharedPreferencesManager prefs = SharedPreferencesManager.getInstance();
.edit() prefs.writeLongSync(
.putLong(MainIntentBehaviorMetrics.LAUNCH_TIMESTAMP_PREF, timestamp) ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP, timestamp);
.commit();
mActivityTestRule.startMainActivityFromLauncher(); mActivityTestRule.startMainActivityFromLauncher();
...@@ -292,9 +277,8 @@ public class MainIntentBehaviorMetricsIntegrationTest { ...@@ -292,9 +277,8 @@ public class MainIntentBehaviorMetricsIntegrationTest {
mActivityTestRule.getActivity(), R.id.open_history_menu_id)); mActivityTestRule.getActivity(), R.id.open_history_menu_id));
historyActivity.finish(); historyActivity.finish();
assertEquals(1, assertEquals(
ContextUtils.getAppSharedPreferences().getInt( 1, prefs.readInt(ChromePreferenceKeys.METRICS_MAIN_INTENT_LAUNCH_COUNT, 0));
MainIntentBehaviorMetrics.LAUNCH_COUNT_PREF, 0));
} finally { } finally {
MainIntentBehaviorMetrics.setTimeoutDurationMsForTesting( MainIntentBehaviorMetrics.setTimeoutDurationMsForTesting(
MainIntentBehaviorMetrics.TIMEOUT_DURATION_MS); MainIntentBehaviorMetrics.TIMEOUT_DURATION_MS);
......
...@@ -431,6 +431,9 @@ public final class ChromePreferenceKeys { ...@@ -431,6 +431,9 @@ public final class ChromePreferenceKeys {
public static final String MEDIA_WEBRTC_NOTIFICATION_IDS = "WebRTCNotificationIds"; public static final String MEDIA_WEBRTC_NOTIFICATION_IDS = "WebRTCNotificationIds";
public static final String METRICS_MAIN_INTENT_LAUNCH_COUNT = "MainIntent.LaunchCount";
public static final String METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP = "MainIntent.LaunchTimestamp";
public static final String NOTIFICATIONS_CHANNELS_VERSION = "channels_version_key"; public static final String NOTIFICATIONS_CHANNELS_VERSION = "channels_version_key";
public static final String NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE = public static final String NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE =
"NotificationUmaTracker.LastShownNotificationType"; "NotificationUmaTracker.LastShownNotificationType";
...@@ -837,6 +840,8 @@ public final class ChromePreferenceKeys { ...@@ -837,6 +840,8 @@ public final class ChromePreferenceKeys {
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, MEDIA_WEBRTC_NOTIFICATION_IDS,
METRICS_MAIN_INTENT_LAUNCH_COUNT,
METRICS_MAIN_INTENT_LAUNCH_TIMESTAMP,
NOTIFICATIONS_CHANNELS_VERSION, NOTIFICATIONS_CHANNELS_VERSION,
NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE, NOTIFICATIONS_LAST_SHOWN_NOTIFICATION_TYPE,
NOTIFICATIONS_NEXT_TRIGGER, NOTIFICATIONS_NEXT_TRIGGER,
......
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