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 10 matching lines...) Expand all Loading... | |
21 import android.widget.RemoteViews; | 21 import android.widget.RemoteViews; |
22 | 22 |
23 import org.chromium.base.ContextUtils; | 23 import org.chromium.base.ContextUtils; |
24 import org.chromium.base.Log; | 24 import org.chromium.base.Log; |
25 import org.chromium.base.ThreadUtils; | 25 import org.chromium.base.ThreadUtils; |
26 import org.chromium.base.VisibleForTesting; | 26 import org.chromium.base.VisibleForTesting; |
27 import org.chromium.base.library_loader.LibraryLoader; | 27 import org.chromium.base.library_loader.LibraryLoader; |
28 import org.chromium.chrome.R; | 28 import org.chromium.chrome.R; |
29 import org.chromium.chrome.browser.IntentHandler; | 29 import org.chromium.chrome.browser.IntentHandler; |
30 import org.chromium.chrome.browser.firstrun.FirstRunFlowSequencer; | 30 import org.chromium.chrome.browser.firstrun.FirstRunFlowSequencer; |
31 import org.chromium.chrome.browser.omnibox.LocationBarLayout; | |
31 import org.chromium.chrome.browser.search_engines.TemplateUrlService; | 32 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
32 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; | 33 import org.chromium.chrome.browser.search_engines.TemplateUrlService.LoadListene r; |
33 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ServiceObserver; | 34 import org.chromium.chrome.browser.search_engines.TemplateUrlService.TemplateUrl ServiceObserver; |
34 import org.chromium.chrome.browser.util.IntentUtils; | 35 import org.chromium.chrome.browser.util.IntentUtils; |
35 | 36 |
36 /** | 37 /** |
37 * Widget that lets the user search using their default search engine. | 38 * Widget that lets the user search using their default search engine. |
38 * | 39 * |
39 * Because this is a BroadcastReceiver, it dies immediately after it runs. A ne w one is created | 40 * Because this is a BroadcastReceiver, it dies immediately after it runs. A ne w one is created |
40 * for each new broadcast. | 41 * for each new broadcast. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 static final String PREF_SEARCH_ENGINE_SHORTNAME = | 117 static final String PREF_SEARCH_ENGINE_SHORTNAME = |
117 "org.chromium.chrome.browser.searchwidget.SEARCH_ENGINE_SHORTNAME"; | 118 "org.chromium.chrome.browser.searchwidget.SEARCH_ENGINE_SHORTNAME"; |
118 | 119 |
119 /** Number of consecutive crashes this widget will absorb before giving up. */ | 120 /** Number of consecutive crashes this widget will absorb before giving up. */ |
120 private static final int CRASH_LIMIT = 3; | 121 private static final int CRASH_LIMIT = 3; |
121 | 122 |
122 private static final String TAG = "searchwidget"; | 123 private static final String TAG = "searchwidget"; |
123 private static final Object DELEGATE_LOCK = new Object(); | 124 private static final Object DELEGATE_LOCK = new Object(); |
124 private static final Object OBSERVER_LOCK = new Object(); | 125 private static final Object OBSERVER_LOCK = new Object(); |
125 | 126 |
127 /** The default search engine's root URL. */ | |
128 private static String sDefaultSearchEngineUrl; | |
129 | |
126 private static SearchWidgetTemplateUrlServiceObserver sObserver; | 130 private static SearchWidgetTemplateUrlServiceObserver sObserver; |
127 private static SearchWidgetProviderDelegate sDelegate; | 131 private static SearchWidgetProviderDelegate sDelegate; |
128 | 132 |
129 /** | 133 /** |
130 * Creates the singleton instance of the observer that will monitor for sear ch engine changes. | 134 * Creates the singleton instance of the observer that will monitor for sear ch engine changes. |
131 * The native library and the browser process must have been fully loaded be fore calling this. | 135 * The native library and the browser process must have been fully loaded be fore calling this. |
132 */ | 136 */ |
133 public static void initialize() { | 137 public static void initialize() { |
134 ThreadUtils.assertOnUiThread(); | 138 ThreadUtils.assertOnUiThread(); |
135 assert LibraryLoader.isInitialized(); | 139 assert LibraryLoader.isInitialized(); |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 /** Attempts to update the cached search engine name. */ | 288 /** Attempts to update the cached search engine name. */ |
285 public static void updateCachedEngineName() { | 289 public static void updateCachedEngineName() { |
286 ThreadUtils.assertOnUiThread(); | 290 ThreadUtils.assertOnUiThread(); |
287 if (!LibraryLoader.isInitialized()) return; | 291 if (!LibraryLoader.isInitialized()) return; |
288 | 292 |
289 // Getting an instance of the TemplateUrlService requires that the nativ e library be | 293 // 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. | 294 // loaded, but the TemplateUrlService also itself needs to be initialize d. |
291 TemplateUrlService service = TemplateUrlService.getInstance(); | 295 TemplateUrlService service = TemplateUrlService.getInstance(); |
292 if (!service.isLoaded()) return; | 296 if (!service.isLoaded()) return; |
293 | 297 |
298 // Update the URL that we show for zero-suggest. | |
299 String searchEngineUrl = service.getSearchEngineUrlFromTemplateUrl( | |
300 service.getDefaultSearchEngineTemplateUrl().getKeyword()); | |
301 sDefaultSearchEngineUrl = | |
302 LocationBarLayout.splitPathFromUrlDisplayText(searchEngineUrl).f irst; | |
303 | |
294 updateCachedEngineName(service.getDefaultSearchEngineTemplateUrl().getSh ortName()); | 304 updateCachedEngineName(service.getDefaultSearchEngineTemplateUrl().getSh ortName()); |
295 } | 305 } |
296 | 306 |
297 /** | 307 /** |
298 * Updates the name of the user's default search engine that is cached in Sh aredPreferences. | 308 * 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 | 309 * Caching it in SharedPreferences prevents us from having to load the nativ e library and the |
300 * TemplateUrlService whenever the widget is updated. | 310 * TemplateUrlService whenever the widget is updated. |
301 */ | 311 */ |
302 static void updateCachedEngineName(String engineName) { | 312 static void updateCachedEngineName(String engineName) { |
303 SharedPreferences prefs = getDelegate().getSharedPreferences(); | 313 SharedPreferences prefs = getDelegate().getSharedPreferences(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 getDelegate().getContext(), null, false); | 386 getDelegate().getContext(), null, false); |
377 return freIntent == null; | 387 return freIntent == null; |
378 } | 388 } |
379 | 389 |
380 /** Sets an {@link SearchWidgetProviderDelegate} to interact with. */ | 390 /** Sets an {@link SearchWidgetProviderDelegate} to interact with. */ |
381 @VisibleForTesting | 391 @VisibleForTesting |
382 static void setDelegateForTest(SearchWidgetProviderDelegate delegate) { | 392 static void setDelegateForTest(SearchWidgetProviderDelegate delegate) { |
383 assert sDelegate == null; | 393 assert sDelegate == null; |
384 sDelegate = delegate; | 394 sDelegate = delegate; |
385 } | 395 } |
396 | |
397 /** See {@link #sDefaultSearchEngineUrl}. */ | |
398 static String getDefaultSearchEngineUrl() { | |
399 return sDefaultSearchEngineUrl; | |
Ted C
2017/04/27 16:40:46
can/should this ever be null? Wondering if we sho
gone
2017/04/27 17:48:52
This was added by Yusuf for showing the engine's U
| |
400 } | |
386 } | 401 } |
OLD | NEW |