| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.preferences; | 5 package org.chromium.chrome.browser.preferences; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.os.StrictMode; | |
| 10 import android.preference.PreferenceManager; | 9 import android.preference.PreferenceManager; |
| 11 import android.text.TextUtils; | |
| 12 | 10 |
| 13 import org.chromium.base.ApplicationStatus; | |
| 14 import org.chromium.base.CommandLine; | |
| 15 import org.chromium.base.Log; | |
| 16 import org.chromium.base.annotations.SuppressFBWarnings; | 11 import org.chromium.base.annotations.SuppressFBWarnings; |
| 17 import org.chromium.chrome.browser.ChromeSwitches; | 12 import org.chromium.chrome.browser.ChromeSwitches; |
| 18 import org.chromium.chrome.browser.ChromeVersionInfo; | |
| 19 import org.chromium.chrome.browser.crash.MinidumpUploadService.ProcessType; | 13 import org.chromium.chrome.browser.crash.MinidumpUploadService.ProcessType; |
| 20 import org.chromium.chrome.browser.signin.SigninPromoUma; | 14 import org.chromium.chrome.browser.signin.SigninPromoUma; |
| 21 import org.chromium.ui.base.DeviceFormFactor; | 15 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 22 | 16 |
| 23 import java.util.Locale; | 17 import java.util.Locale; |
| 24 | 18 |
| 25 | 19 |
| 26 /** | 20 /** |
| 27 * ChromePreferenceManager stores and retrieves various values in Android shared
preferences. | 21 * ChromePreferenceManager stores and retrieves various values in Android shared
preferences. |
| 28 */ | 22 */ |
| 29 public class ChromePreferenceManager { | 23 public class ChromePreferenceManager { |
| 30 /** | 24 /** |
| 31 * Preference that denotes that Chrome has attempted to migrate from tabbed
mode to document | 25 * Preference that denotes that Chrome has attempted to migrate from tabbed
mode to document |
| (...skipping 20 matching lines...) Expand all Loading... |
| 52 private static final String ENABLE_CUSTOM_TABS = "enable_custom_tabs"; | 46 private static final String ENABLE_CUSTOM_TABS = "enable_custom_tabs"; |
| 53 private static final String HERB_FLAVOR_KEY = "herb_flavor"; | 47 private static final String HERB_FLAVOR_KEY = "herb_flavor"; |
| 54 | 48 |
| 55 private static final String SUCCESS_UPLOAD_SUFFIX = "_crash_success_upload"; | 49 private static final String SUCCESS_UPLOAD_SUFFIX = "_crash_success_upload"; |
| 56 private static final String FAILURE_UPLOAD_SUFFIX = "_crash_failure_upload"; | 50 private static final String FAILURE_UPLOAD_SUFFIX = "_crash_failure_upload"; |
| 57 | 51 |
| 58 private static final int SIGNIN_PROMO_CYCLE_IN_DAYS = 120; | 52 private static final int SIGNIN_PROMO_CYCLE_IN_DAYS = 120; |
| 59 private static final long MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24; | 53 private static final long MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24; |
| 60 | 54 |
| 61 private static ChromePreferenceManager sPrefs; | 55 private static ChromePreferenceManager sPrefs; |
| 62 private static String sCachedHerbFlavor; | |
| 63 private static boolean sIsHerbFlavorCached; | |
| 64 | 56 |
| 65 private final SharedPreferences mSharedPreferences; | 57 private final SharedPreferences mSharedPreferences; |
| 66 private final Context mContext; | 58 private final Context mContext; |
| 67 | 59 |
| 68 private ChromePreferenceManager(Context context) { | 60 private ChromePreferenceManager(Context context) { |
| 69 mContext = context.getApplicationContext(); | 61 mContext = context.getApplicationContext(); |
| 70 mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mCont
ext); | 62 mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mCont
ext); |
| 71 } | 63 } |
| 72 | 64 |
| 73 /** | 65 /** |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 318 |
| 327 /** | 319 /** |
| 328 * Sets the number of tap gestures that have been received when not waiting
for the promo. | 320 * Sets the number of tap gestures that have been received when not waiting
for the promo. |
| 329 * @param count Number of taps that have been received when not waiting for
the promo. | 321 * @param count Number of taps that have been received when not waiting for
the promo. |
| 330 */ | 322 */ |
| 331 public void setContextualSearchTapCount(int count) { | 323 public void setContextualSearchTapCount(int count) { |
| 332 writeInt(CONTEXTUAL_SEARCH_TAP_COUNT, count); | 324 writeInt(CONTEXTUAL_SEARCH_TAP_COUNT, count); |
| 333 } | 325 } |
| 334 | 326 |
| 335 /** | 327 /** |
| 336 * @return Which flavor of Herb is active, or null if a prototype isn't bein
g tested. | 328 * @return Which UI prototype the user is testing. This is cached from nativ
e via |
| 329 * {@link FeatureUtilities#cacheHerbFlavor}. |
| 337 */ | 330 */ |
| 338 public static String getHerbFlavor() { | 331 public String getCachedHerbFlavor() { |
| 339 if (!sIsHerbFlavorCached) { | 332 return mSharedPreferences.getString(HERB_FLAVOR_KEY, ChromeSwitches.HERB
_FLAVOR_DISABLED); |
| 340 Context context = ApplicationStatus.getApplicationContext(); | |
| 341 if (ChromeVersionInfo.isStableBuild() || ChromeVersionInfo.isBetaBui
ld()) return null; | |
| 342 if (DeviceFormFactor.isTablet(context)) return null; | |
| 343 | |
| 344 // Allowing disk access for preferences while prototyping. | |
| 345 sCachedHerbFlavor = null; | |
| 346 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(
); | |
| 347 try { | |
| 348 sCachedHerbFlavor = | |
| 349 ChromePreferenceManager.getInstance(context).getHerbFlav
orInternal(); | |
| 350 } finally { | |
| 351 StrictMode.setThreadPolicy(oldPolicy); | |
| 352 } | |
| 353 | |
| 354 sIsHerbFlavorCached = true; | |
| 355 Log.d(TAG, "Retrieved Herb flavor: " + sCachedHerbFlavor); | |
| 356 } | |
| 357 | |
| 358 return sCachedHerbFlavor; | |
| 359 } | |
| 360 | |
| 361 private String getHerbFlavorInternal() { | |
| 362 return mSharedPreferences.getString(HERB_FLAVOR_KEY, null); | |
| 363 } | 333 } |
| 364 | 334 |
| 365 /** | 335 /** |
| 366 * Caches which flavor of Herb the user prefers from native. | 336 * Caches which UI prototype the user is testing. |
| 367 */ | 337 */ |
| 368 public static boolean cacheHerbFlavor() { | 338 public void setCachedHerbFlavor(String flavor) { |
| 369 String oldFlavor = getHerbFlavor(); | 339 writeString(HERB_FLAVOR_KEY, flavor); |
| 370 String newFlavor = | |
| 371 CommandLine.getInstance().getSwitchValue(ChromeSwitches.HERB_FLA
VOR, null); | |
| 372 sCachedHerbFlavor = newFlavor; | |
| 373 Log.d(TAG, "Caching Herb flavor: " + sCachedHerbFlavor); | |
| 374 | |
| 375 if (!TextUtils.equals(oldFlavor, newFlavor)) { | |
| 376 Context context = ApplicationStatus.getApplicationContext(); | |
| 377 ChromePreferenceManager.getInstance(context).writeString(HERB_FLAVOR
_KEY, newFlavor); | |
| 378 return true; | |
| 379 } | |
| 380 return false; | |
| 381 } | 340 } |
| 382 | 341 |
| 383 /** | 342 /** |
| 384 * Writes the given int value to the named shared preference. | 343 * Writes the given int value to the named shared preference. |
| 385 * | 344 * |
| 386 * @param key The name of the preference to modify. | 345 * @param key The name of the preference to modify. |
| 387 * @param value The new value for the preference. | 346 * @param value The new value for the preference. |
| 388 */ | 347 */ |
| 389 private void writeInt(String key, int value) { | 348 private void writeInt(String key, int value) { |
| 390 SharedPreferences.Editor ed = mSharedPreferences.edit(); | 349 SharedPreferences.Editor ed = mSharedPreferences.edit(); |
| 391 ed.putInt(key, value); | 350 ed.putInt(key, value); |
| 392 ed.apply(); | 351 ed.apply(); |
| 393 } | 352 } |
| 394 | 353 |
| 395 /** | 354 /** |
| 396 * Writes the given String to the named shared preference. | 355 * Writes the given String to the named shared preference. |
| 397 * | 356 * |
| 398 * @param key The name of the preference to modify. | 357 * @param key The name of the preference to modify. |
| 399 * @param value The new value for the preference. | 358 * @param value The new value for the preference. |
| 400 */ | 359 */ |
| 401 private void writeString(String key, String value) { | 360 private void writeString(String key, String value) { |
| 402 SharedPreferences.Editor ed = mSharedPreferences.edit(); | 361 SharedPreferences.Editor ed = mSharedPreferences.edit(); |
| 403 ed.putString(key, value); | 362 ed.putString(key, value); |
| 404 ed.apply(); | 363 ed.apply(); |
| 405 } | 364 } |
| 406 } | 365 } |
| OLD | NEW |