diff options
author | zachh <zachh@google.com> | 2017-09-29 11:11:01 -0700 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-10-03 10:38:00 -0700 |
commit | f29d22e97bbbd6e3201c0347e48503e40309253e (patch) | |
tree | d89058813205107a15c59c7673413c3c0b457009 /java/com/android/dialer/strictmode | |
parent | 0c396de3c969d42cdd2a2d93280f6567b5295aae (diff) |
Warmup shared prefs in googledialer when strict mode is enabled.
This avoid some spurious strict mode violations.
Test: observed no strict mode notification launching google dialer
PiperOrigin-RevId: 170505918
Change-Id: Ie48671dcd306ea18b041a0bd7989b419bab903f5
Diffstat (limited to 'java/com/android/dialer/strictmode')
-rw-r--r-- | java/com/android/dialer/strictmode/StrictModeUtils.java | 30 | ||||
-rw-r--r-- | java/com/android/dialer/strictmode/impl/SystemDialerStrictMode.java | 39 |
2 files changed, 33 insertions, 36 deletions
diff --git a/java/com/android/dialer/strictmode/StrictModeUtils.java b/java/com/android/dialer/strictmode/StrictModeUtils.java index c83beb0b6..5ccd25d93 100644 --- a/java/com/android/dialer/strictmode/StrictModeUtils.java +++ b/java/com/android/dialer/strictmode/StrictModeUtils.java @@ -16,12 +16,17 @@ package com.android.dialer.strictmode; +import android.app.Application; +import android.content.Context; import android.os.Looper; import android.os.StrictMode; import android.os.StrictMode.ThreadPolicy; +import android.preference.PreferenceManager; import android.support.annotation.AnyThread; +import android.support.v4.os.UserManagerCompat; import com.android.dialer.buildtype.BuildType; import com.android.dialer.function.Supplier; +import com.android.dialer.util.DialerUtils; /** Utilities for enforcing strict-mode in an app. */ public final class StrictModeUtils { @@ -88,5 +93,30 @@ public final class StrictModeUtils { return Looper.getMainLooper().equals(Looper.myLooper()); } + /** + * We frequently access shared preferences on the main thread, which causes strict mode + * violations. When strict mode is allowed, warm up the shared preferences so that later uses of + * shared preferences access the in-memory versions and we don't have to bypass strict mode at + * every point in the application where shared preferences are accessed. + */ + public static void warmupSharedPrefs(Application application) { + // From credential-encrypted (CE) storage, i.e.: + // /data/data/com.android.dialer/shared_prefs + + if (UserManagerCompat.isUserUnlocked(application)) { + // <package_name>_preferences.xml + PreferenceManager.getDefaultSharedPreferences(application); + + // <package_name>.xml + application.getSharedPreferences(application.getPackageName(), Context.MODE_PRIVATE); + } + + // From device-encrypted (DE) storage, i.e.: + // /data/user_de/0/com.android.dialer/shared_prefs/ + + // <package_name>_preferences.xml + DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(application); + } + private StrictModeUtils() {} } diff --git a/java/com/android/dialer/strictmode/impl/SystemDialerStrictMode.java b/java/com/android/dialer/strictmode/impl/SystemDialerStrictMode.java index 4d6524123..b974ab18a 100644 --- a/java/com/android/dialer/strictmode/impl/SystemDialerStrictMode.java +++ b/java/com/android/dialer/strictmode/impl/SystemDialerStrictMode.java @@ -17,21 +17,17 @@ package com.android.dialer.strictmode.impl; import android.app.Application; -import android.content.Context; import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.StrictMode; import android.os.StrictMode.ThreadPolicy; import android.os.StrictMode.VmPolicy; -import android.preference.PreferenceManager; import android.support.annotation.MainThread; import android.support.annotation.Nullable; -import android.support.v4.os.UserManagerCompat; -import com.android.dialer.buildtype.BuildType; import com.android.dialer.common.Assert; import com.android.dialer.strictmode.DialerStrictMode; -import com.android.dialer.util.DialerUtils; +import com.android.dialer.strictmode.StrictModeUtils; import com.google.auto.value.AutoValue; import java.util.Map; import javax.inject.Inject; @@ -49,8 +45,8 @@ final class SystemDialerStrictMode implements DialerStrictMode { @MainThread @Override public void onApplicationCreate(Application application) { - if (isStrictModeAllowed()) { - warmupSharedPrefs(application); + if (StrictModeUtils.isStrictModeAllowed()) { + StrictModeUtils.warmupSharedPrefs(application); setRecommendedMainThreadPolicy(THREAD_DEATH_PENALTY); setRecommendedVMPolicy(VM_DEATH_PENALTY); @@ -64,35 +60,6 @@ final class SystemDialerStrictMode implements DialerStrictMode { } /** - * We frequently access shared preferences on the main thread, which causes strict mode - * violations. When strict mode is allowed, warm up the shared preferences so that later uses of - * shared preferences access the in-memory versions and we don't have to bypass strict mode at - * every point in the application where shared preferences are accessed. - */ - private static void warmupSharedPrefs(Application application) { - // From credential-encrypted (CE) storage, i.e.: - // /data/data/com.android.dialer/shared_prefs - - if (UserManagerCompat.isUserUnlocked(application)) { - // <package_name>_preferences.xml - PreferenceManager.getDefaultSharedPreferences(application); - - // <package_name>.xml - application.getSharedPreferences(application.getPackageName(), Context.MODE_PRIVATE); - } - - // From device-encrypted (DE) storage, i.e.: - // /data/user_de/0/com.android.dialer/shared_prefs/ - - // <package_name>_preferences.xml - DialerUtils.getDefaultSharedPreferenceForDeviceProtectedStorageContext(application); - } - - private static boolean isStrictModeAllowed() { - return BuildType.get() == BuildType.BUGFOOD; - } - - /** * Set the recommended policy for the app. * * @param threadPenalties policy with preferred penalties. Detection bits will be ignored. |