Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.locale; | 5 package org.chromium.chrome.browser.locale; |
| 6 | 6 |
| 7 import android.content.Context; | |
| 8 import android.content.SharedPreferences; | |
| 9 | |
| 10 import org.chromium.base.ContextUtils; | |
| 11 import org.chromium.chrome.browser.ChromeApplication; | |
| 7 import org.chromium.chrome.browser.ChromeFeatureList; | 12 import org.chromium.chrome.browser.ChromeFeatureList; |
| 8 | 13 |
| 9 /** | 14 /** |
| 10 * Manager for some locale specific logics. | 15 * Manager for some locale specific logics. |
| 11 */ | 16 */ |
| 12 public class LocaleManager { | 17 public class LocaleManager { |
| 18 public static final String PREF_PROMO_SHOWN = "LocaleManager_PREF_PROMO_SHOW N"; | |
| 13 public static final String SPECIAL_LOCALE_ID = "US"; | 19 public static final String SPECIAL_LOCALE_ID = "US"; |
| 14 | 20 |
| 21 private static LocaleManager sInstance; | |
| 22 | |
| 15 private SpecialLocaleHandler mLocaleHandler; | 23 private SpecialLocaleHandler mLocaleHandler; |
| 16 | 24 |
| 17 /** | 25 /** |
| 26 * @return An instance of the {@link LocaleManager}. | |
| 27 */ | |
| 28 public static LocaleManager getInstance() { | |
| 29 if (sInstance == null) { | |
|
Maria
2016/10/04 03:42:22
this should either be synchronized or assert that
Ian Wen
2016/10/04 18:28:45
Done.
| |
| 30 sInstance = ((ChromeApplication) ContextUtils.getApplicationContext( )) | |
| 31 .createLocaleManager(); | |
| 32 } | |
| 33 return sInstance; | |
| 34 } | |
| 35 | |
| 36 /** | |
| 18 * Starts recording metrics in deferred startup. | 37 * Starts recording metrics in deferred startup. |
| 19 */ | 38 */ |
| 20 public void recordStartupMetrics() {} | 39 public void recordStartupMetrics() {} |
| 21 | 40 |
| 22 /** | 41 /** |
| 23 * @return Whether the Chrome instance is running in a special locale. | 42 * @return Whether the Chrome instance is running in a special locale. |
| 24 */ | 43 */ |
| 25 public boolean isSpecialLocaleEnabled() { | 44 public boolean isSpecialLocaleEnabled() { |
| 26 // If there is a kill switch sent from the server, disable the feature. | 45 // If there is a kill switch sent from the server, disable the feature. |
| 27 if (!ChromeFeatureList.isEnabled("SpecialLocaleWrapper")) { | 46 if (!ChromeFeatureList.isEnabled("SpecialLocaleWrapper")) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 45 // TODO(ianwen): Let this method be called in ChromeActivity#finishNativ eInitialization(). | 64 // TODO(ianwen): Let this method be called in ChromeActivity#finishNativ eInitialization(). |
| 46 if (!isSpecialLocaleEnabled()) return; | 65 if (!isSpecialLocaleEnabled()) return; |
| 47 getSpecialLocaleHandler().loadTemplateUrls(); | 66 getSpecialLocaleHandler().loadTemplateUrls(); |
| 48 } | 67 } |
| 49 | 68 |
| 50 /** | 69 /** |
| 51 * Overrides the default search engine to a different search engine we desig nate. This is a | 70 * Overrides the default search engine to a different search engine we desig nate. This is a |
| 52 * no-op if the user has changed DSP settings before. | 71 * no-op if the user has changed DSP settings before. |
| 53 */ | 72 */ |
| 54 public void overrideDefaultSearchEngine() { | 73 public void overrideDefaultSearchEngine() { |
| 55 // TODO(ianwen): Let this method be called in promotion. | |
| 56 // TODO(ianwen): Implement search engine auto switching. | 74 // TODO(ianwen): Implement search engine auto switching. |
| 57 if (!isSpecialLocaleEnabled()) return; | 75 if (!isSpecialLocaleEnabled()) return; |
| 58 getSpecialLocaleHandler().overrideDefaultSearchProvider(); | 76 getSpecialLocaleHandler().overrideDefaultSearchProvider(); |
| 59 } | 77 } |
| 60 | 78 |
| 61 /** | 79 /** |
| 62 * Removes local search engines for special locale. | 80 * Removes local search engines for special locale. |
| 63 */ | 81 */ |
| 64 public void removeSpecialSearchEngines() { | 82 public void removeSpecialSearchEngines() { |
| 65 // TODO(ianwen): Let this method be called when device configuration cha nges. | 83 // TODO(ianwen): Let this method be called when device configuration cha nges. |
| 66 if (isSpecialLocaleEnabled()) return; | 84 if (isSpecialLocaleEnabled()) return; |
| 67 getSpecialLocaleHandler().removeTemplateUrls(); | 85 getSpecialLocaleHandler().removeTemplateUrls(); |
| 68 } | 86 } |
| 69 | 87 |
| 70 /** | 88 /** |
| 89 * Shows a promotion dialog saying the default search engine will be set to Sogou. No-op if | |
| 90 * device is not in special locale. | |
| 91 * | |
| 92 * @return Whether such dialog is needed. | |
| 93 */ | |
| 94 public boolean showSearchEnginePromoIfNeeded(Context context) { | |
| 95 if (!isSpecialLocaleEnabled()) return false; | |
| 96 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | |
| 97 if (preferences.getBoolean(PREF_PROMO_SHOWN, false)) { | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 new SearchEnginePromoDialog(context, this).show(); | |
| 102 | |
| 103 preferences.edit().putBoolean(PREF_PROMO_SHOWN, true).apply(); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 /** | |
| 71 * Does some extra checking about whether the user is in special locale. | 108 * Does some extra checking about whether the user is in special locale. |
| 72 * @param inSpecialLocale Whether the variation service thinks the client is in special locale. | 109 * @param inSpecialLocale Whether the variation service thinks the client is in special locale. |
| 73 * @return The result after extra confirmation. | 110 * @return The result after extra confirmation. |
| 74 */ | 111 */ |
| 75 protected boolean isReallyInSpecialLocale(boolean inSpecialLocale) { | 112 protected boolean isReallyInSpecialLocale(boolean inSpecialLocale) { |
| 76 return inSpecialLocale; | 113 return inSpecialLocale; |
| 77 } | 114 } |
| 78 | 115 |
| 79 private SpecialLocaleHandler getSpecialLocaleHandler() { | 116 private SpecialLocaleHandler getSpecialLocaleHandler() { |
| 80 if (mLocaleHandler == null) mLocaleHandler = new SpecialLocaleHandler(ge tSpecialLocaleId()); | 117 if (mLocaleHandler == null) mLocaleHandler = new SpecialLocaleHandler(ge tSpecialLocaleId()); |
| 81 return mLocaleHandler; | 118 return mLocaleHandler; |
| 82 } | 119 } |
| 83 } | 120 } |
| OLD | NEW |