Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Unified Diff: base/android/java/src/org/chromium/base/ContextUtils.java

Issue 1874423002: 🍰 Migrate app shared preferences to ContextUtils. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ResourceExtractor.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/java/src/org/chromium/base/ContextUtils.java
diff --git a/base/android/java/src/org/chromium/base/ContextUtils.java b/base/android/java/src/org/chromium/base/ContextUtils.java
index 95cdc4015348dd0af75da09e14d0955f82f22edb..4b615cb3ea93160c5d55ad37905335d1a64c17db 100644
--- a/base/android/java/src/org/chromium/base/ContextUtils.java
+++ b/base/android/java/src/org/chromium/base/ContextUtils.java
@@ -5,11 +5,13 @@
package org.chromium.base;
import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
import org.chromium.base.annotations.JNINamespace;
/**
- * This class provides Android Context utility methods.
+ * This class provides Android application context related utility methods.
*/
@JNINamespace("base::android")
public class ContextUtils {
@@ -17,6 +19,14 @@ public class ContextUtils {
private static Context sApplicationContext;
/**
+ * Initialization-on-demand holder. This exists for thread-safe lazy initialization.
+ */
+ private static class Holder {
+ // Not final for tests.
+ private static SharedPreferences sSharedPreferences = fetchAppSharedPreferences();
+ }
+
+ /**
* Get the Android application context.
*
* Under normal circumstances there is only one application context in a process, so it's safe
@@ -60,6 +70,26 @@ public class ContextUtils {
}
/**
+ * Only called by the static holder class and tests.
+ *
+ * @return The application-wide shared preferences.
+ */
+ private static SharedPreferences fetchAppSharedPreferences() {
+ return PreferenceManager.getDefaultSharedPreferences(sApplicationContext);
+ }
+
+ /**
+ * This is used to ensure that we always use the application context to fetch the default shared
+ * preferences. This avoids needless I/O for android N and above. It also makes it clear that
+ * the app-wide shared preference is desired, rather than the potentially context-specific one.
+ *
+ * @return application-wide shared preferences.
+ */
+ public static SharedPreferences getAppSharedPreferences() {
+ return Holder.sSharedPreferences;
+ }
+
+ /**
* Occasionally tests cannot ensure the application context doesn't change between tests (junit)
* and sometimes specific tests has its own special needs, initApplicationContext should be used
* as much as possible, but this method can be used to override it.
@@ -69,6 +99,7 @@ public class ContextUtils {
@VisibleForTesting
public static void initApplicationContextForTests(Context appContext) {
initJavaSideApplicationContext(appContext);
+ Holder.sSharedPreferences = fetchAppSharedPreferences();
}
private static void initJavaSideApplicationContext(Context appContext) {
« no previous file with comments | « no previous file | base/android/java/src/org/chromium/base/ResourceExtractor.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698