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