| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.searchwidget; | 5 package org.chromium.chrome.browser.searchwidget; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
| 9 import android.appwidget.AppWidgetManager; | 9 import android.appwidget.AppWidgetManager; |
| 10 import android.appwidget.AppWidgetProvider; | 10 import android.appwidget.AppWidgetProvider; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 TemplateUrlService.getInstance().unregisterLoadListener(this); | 88 TemplateUrlService.getInstance().unregisterLoadListener(this); |
| 89 updateCachedEngineName(); | 89 updateCachedEngineName(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 @Override | 92 @Override |
| 93 public void onTemplateURLServiceChanged() { | 93 public void onTemplateURLServiceChanged() { |
| 94 updateCachedEngineName(); | 94 updateCachedEngineName(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 private void updateCachedEngineName() { | 97 private void updateCachedEngineName() { |
| 98 SearchWidgetProvider.updateCachedEngineName(); | 98 assert LibraryLoader.isInitialized(); |
| 99 |
| 100 // Getting an instance of the TemplateUrlService requires that the n
ative library be |
| 101 // loaded, but the TemplateUrlService also itself needs to be initia
lized. |
| 102 TemplateUrlService service = TemplateUrlService.getInstance(); |
| 103 assert service.isLoaded(); |
| 104 SearchWidgetProvider.updateCachedEngineName( |
| 105 service.getDefaultSearchEngineTemplateUrl().getShortName()); |
| 99 } | 106 } |
| 100 } | 107 } |
| 101 | 108 |
| 102 static final String ACTION_START_TEXT_QUERY = | 109 static final String ACTION_START_TEXT_QUERY = |
| 103 "org.chromium.chrome.browser.searchwidget.START_TEXT_QUERY"; | 110 "org.chromium.chrome.browser.searchwidget.START_TEXT_QUERY"; |
| 104 static final String ACTION_START_VOICE_QUERY = | 111 static final String ACTION_START_VOICE_QUERY = |
| 105 "org.chromium.chrome.browser.searchwidget.START_VOICE_QUERY"; | 112 "org.chromium.chrome.browser.searchwidget.START_VOICE_QUERY"; |
| 106 static final String ACTION_UPDATE_ALL_WIDGETS = | 113 static final String ACTION_UPDATE_ALL_WIDGETS = |
| 107 "org.chromium.chrome.browser.searchwidget.UPDATE_ALL_WIDGETS"; | 114 "org.chromium.chrome.browser.searchwidget.UPDATE_ALL_WIDGETS"; |
| 108 | 115 |
| 109 static final String EXTRA_START_VOICE_SEARCH = | 116 static final String EXTRA_START_VOICE_SEARCH = |
| 110 "org.chromium.chrome.browser.searchwidget.START_VOICE_SEARCH"; | 117 "org.chromium.chrome.browser.searchwidget.START_VOICE_SEARCH"; |
| 111 | 118 |
| 112 private static final String PREF_IS_VOICE_SEARCH_AVAILABLE = | 119 private static final String PREF_IS_VOICE_SEARCH_AVAILABLE = |
| 113 "org.chromium.chrome.browser.searchwidget.IS_VOICE_SEARCH_AVAILABLE"
; | 120 "org.chromium.chrome.browser.searchwidget.IS_VOICE_SEARCH_AVAILABLE"
; |
| 114 private static final String PREF_NUM_CONSECUTIVE_CRASHES = | 121 private static final String PREF_NUM_CONSECUTIVE_CRASHES = |
| 115 "org.chromium.chrome.browser.searchwidget.NUM_CONSECUTIVE_CRASHES"; | 122 "org.chromium.chrome.browser.searchwidget.NUM_CONSECUTIVE_CRASHES"; |
| 116 static final String PREF_SEARCH_ENGINE_SHORTNAME = | 123 private static final String PREF_SEARCH_ENGINE_SHORTNAME = |
| 117 "org.chromium.chrome.browser.searchwidget.SEARCH_ENGINE_SHORTNAME"; | 124 "org.chromium.chrome.browser.searchwidget.SEARCH_ENGINE_SHORTNAME"; |
| 118 | 125 |
| 119 /** Number of consecutive crashes this widget will absorb before giving up.
*/ | 126 /** Number of consecutive crashes this widget will absorb before giving up.
*/ |
| 120 private static final int CRASH_LIMIT = 3; | 127 private static final int CRASH_LIMIT = 3; |
| 121 | 128 |
| 122 private static final String TAG = "searchwidget"; | 129 private static final String TAG = "searchwidget"; |
| 123 private static final Object DELEGATE_LOCK = new Object(); | 130 private static final Object DELEGATE_LOCK = new Object(); |
| 124 private static final Object OBSERVER_LOCK = new Object(); | 131 private static final Object OBSERVER_LOCK = new Object(); |
| 125 | 132 |
| 126 private static SearchWidgetTemplateUrlServiceObserver sObserver; | 133 private static SearchWidgetTemplateUrlServiceObserver sObserver; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 Intent voiceIntent = createStartQueryIntent(context, ACTION_START_VO
ICE_QUERY, id); | 256 Intent voiceIntent = createStartQueryIntent(context, ACTION_START_VO
ICE_QUERY, id); |
| 250 views.setOnClickPendingIntent(R.id.microphone_icon, | 257 views.setOnClickPendingIntent(R.id.microphone_icon, |
| 251 PendingIntent.getBroadcast( | 258 PendingIntent.getBroadcast( |
| 252 context, 0, voiceIntent, PendingIntent.FLAG_UPDATE_C
URRENT)); | 259 context, 0, voiceIntent, PendingIntent.FLAG_UPDATE_C
URRENT)); |
| 253 views.setViewVisibility(R.id.microphone_icon, View.VISIBLE); | 260 views.setViewVisibility(R.id.microphone_icon, View.VISIBLE); |
| 254 } else { | 261 } else { |
| 255 views.setViewVisibility(R.id.microphone_icon, View.GONE); | 262 views.setViewVisibility(R.id.microphone_icon, View.GONE); |
| 256 } | 263 } |
| 257 | 264 |
| 258 // Update what string is displayed by the widget. | 265 // Update what string is displayed by the widget. |
| 259 String text = TextUtils.isEmpty(engineName) || !shouldShowFullString() | 266 String text = TextUtils.isEmpty(engineName) |
| 260 ? context.getString(R.string.search_widget_default) | 267 ? context.getString(R.string.search_widget_default) |
| 261 : context.getString(R.string.search_with_product, engineName); | 268 : context.getString(R.string.search_with_product, engineName); |
| 262 views.setTextViewText(R.id.title, text); | 269 views.setTextViewText(R.id.title, text); |
| 263 | 270 |
| 264 return views; | 271 return views; |
| 265 } | 272 } |
| 266 | 273 |
| 267 /** Creates a trusted Intent that lets the user begin performing queries. */ | 274 /** Creates a trusted Intent that lets the user begin performing queries. */ |
| 268 private static Intent createStartQueryIntent(Context context, String action,
int widgetId) { | 275 private static Intent createStartQueryIntent(Context context, String action,
int widgetId) { |
| 269 Intent intent = new Intent(action, Uri.parse(String.valueOf(widgetId))); | 276 Intent intent = new Intent(action, Uri.parse(String.valueOf(widgetId))); |
| 270 intent.setClass(context, SearchWidgetProvider.class); | 277 intent.setClass(context, SearchWidgetProvider.class); |
| 271 IntentHandler.addTrustedIntentExtras(intent); | 278 IntentHandler.addTrustedIntentExtras(intent); |
| 272 return intent; | 279 return intent; |
| 273 } | 280 } |
| 274 | 281 |
| 275 /** Caches whether or not a voice search is possible. */ | 282 /** Caches whether or not a voice search is possible. */ |
| 276 static void updateCachedVoiceSearchAvailability(boolean isVoiceSearchAvailab
le) { | 283 static void updateCachedVoiceSearchAvailability(boolean isVoiceSearchAvailab
le) { |
| 277 SharedPreferences prefs = getDelegate().getSharedPreferences(); | 284 SharedPreferences prefs = getDelegate().getSharedPreferences(); |
| 278 if (getCachedVoiceSearchAvailability(prefs) != isVoiceSearchAvailable) { | 285 if (getCachedVoiceSearchAvailability(prefs) != isVoiceSearchAvailable) { |
| 279 prefs.edit().putBoolean(PREF_IS_VOICE_SEARCH_AVAILABLE, isVoiceSearc
hAvailable).apply(); | 286 prefs.edit().putBoolean(PREF_IS_VOICE_SEARCH_AVAILABLE, isVoiceSearc
hAvailable).apply(); |
| 280 performUpdate(null); | 287 performUpdate(null); |
| 281 } | 288 } |
| 282 } | 289 } |
| 283 | 290 |
| 284 /** Attempts to update the cached search engine name. */ | |
| 285 public static void updateCachedEngineName() { | |
| 286 ThreadUtils.assertOnUiThread(); | |
| 287 if (!LibraryLoader.isInitialized()) return; | |
| 288 | |
| 289 // Getting an instance of the TemplateUrlService requires that the nativ
e library be | |
| 290 // loaded, but the TemplateUrlService also itself needs to be initialize
d. | |
| 291 TemplateUrlService service = TemplateUrlService.getInstance(); | |
| 292 if (!service.isLoaded()) return; | |
| 293 | |
| 294 updateCachedEngineName(service.getDefaultSearchEngineTemplateUrl().getSh
ortName()); | |
| 295 } | |
| 296 | |
| 297 /** | 291 /** |
| 298 * Updates the name of the user's default search engine that is cached in Sh
aredPreferences. | 292 * Updates the name of the user's default search engine that is cached in Sh
aredPreferences. |
| 299 * Caching it in SharedPreferences prevents us from having to load the nativ
e library and the | 293 * Caching it in SharedPreferences prevents us from having to load the nativ
e library and the |
| 300 * TemplateUrlService whenever the widget is updated. | 294 * TemplateUrlService whenever the widget is updated. |
| 301 */ | 295 */ |
| 302 static void updateCachedEngineName(String engineName) { | 296 static void updateCachedEngineName(String engineName) { |
| 303 SharedPreferences prefs = getDelegate().getSharedPreferences(); | 297 SharedPreferences prefs = getDelegate().getSharedPreferences(); |
| 304 | |
| 305 if (!shouldShowFullString()) engineName = null; | |
| 306 | |
| 307 if (!TextUtils.equals(getCachedEngineName(prefs), engineName)) { | 298 if (!TextUtils.equals(getCachedEngineName(prefs), engineName)) { |
| 308 prefs.edit().putString(PREF_SEARCH_ENGINE_SHORTNAME, engineName).app
ly(); | 299 prefs.edit().putString(PREF_SEARCH_ENGINE_SHORTNAME, engineName).app
ly(); |
| 309 performUpdate(null); | 300 performUpdate(null); |
| 310 } | 301 } |
| 311 } | 302 } |
| 312 | 303 |
| 313 /** Updates the number of consecutive crashes this widget has absorbed. */ | 304 /** Updates the number of consecutive crashes this widget has absorbed. */ |
| 314 @SuppressLint("CommitPrefEdits") | 305 @SuppressLint("CommitPrefEdits") |
| 315 static void updateNumConsecutiveCrashes(int newValue) { | 306 static void updateNumConsecutiveCrashes(int newValue) { |
| 316 SharedPreferences prefs = getDelegate().getSharedPreferences(); | 307 SharedPreferences prefs = getDelegate().getSharedPreferences(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 if (numCrashes < CRASH_LIMIT) { | 355 if (numCrashes < CRASH_LIMIT) { |
| 365 // Absorb the crash. | 356 // Absorb the crash. |
| 366 Log.e(TAG, "Absorbing exception caught when attempting to launch
widget.", e); | 357 Log.e(TAG, "Absorbing exception caught when attempting to launch
widget.", e); |
| 367 } else { | 358 } else { |
| 368 // Too many crashes have happened consecutively. Let Android ha
ndle it. | 359 // Too many crashes have happened consecutively. Let Android ha
ndle it. |
| 369 throw e; | 360 throw e; |
| 370 } | 361 } |
| 371 } | 362 } |
| 372 } | 363 } |
| 373 | 364 |
| 374 static boolean shouldShowFullString() { | |
| 375 Intent freIntent = FirstRunFlowSequencer.checkIfFirstRunIsNecessary( | |
| 376 getDelegate().getContext(), null, false); | |
| 377 return freIntent == null; | |
| 378 } | |
| 379 | |
| 380 /** Sets an {@link SearchWidgetProviderDelegate} to interact with. */ | 365 /** Sets an {@link SearchWidgetProviderDelegate} to interact with. */ |
| 381 @VisibleForTesting | 366 @VisibleForTesting |
| 382 static void setDelegateForTest(SearchWidgetProviderDelegate delegate) { | 367 static void setDelegateForTest(SearchWidgetProviderDelegate delegate) { |
| 383 assert sDelegate == null; | 368 assert sDelegate == null; |
| 384 sDelegate = delegate; | 369 sDelegate = delegate; |
| 385 } | 370 } |
| 386 } | 371 } |
| OLD | NEW |