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.content.Intent; | 7 import android.content.Intent; |
8 import android.net.Uri; | 8 import android.net.Uri; |
9 import android.support.v4.app.ActivityOptionsCompat; | 9 import android.support.v4.app.ActivityOptionsCompat; |
10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
11 import android.view.LayoutInflater; | 11 import android.view.LayoutInflater; |
12 import android.view.View; | 12 import android.view.View; |
13 import android.view.ViewGroup; | 13 import android.view.ViewGroup; |
14 | 14 |
15 import org.chromium.base.Callback; | |
16 import org.chromium.base.Log; | |
15 import org.chromium.base.VisibleForTesting; | 17 import org.chromium.base.VisibleForTesting; |
16 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
17 import org.chromium.chrome.browser.IntentHandler; | 19 import org.chromium.chrome.browser.IntentHandler; |
18 import org.chromium.chrome.browser.WebContentsFactory; | 20 import org.chromium.chrome.browser.WebContentsFactory; |
19 import org.chromium.chrome.browser.WindowDelegate; | 21 import org.chromium.chrome.browser.WindowDelegate; |
20 import org.chromium.chrome.browser.customtabs.CustomTabsConnection; | 22 import org.chromium.chrome.browser.customtabs.CustomTabsConnection; |
21 import org.chromium.chrome.browser.init.AsyncInitializationActivity; | 23 import org.chromium.chrome.browser.init.AsyncInitializationActivity; |
24 import org.chromium.chrome.browser.locale.LocaleManager; | |
22 import org.chromium.chrome.browser.omnibox.AutocompleteController; | 25 import org.chromium.chrome.browser.omnibox.AutocompleteController; |
23 import org.chromium.chrome.browser.snackbar.SnackbarManager; | 26 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
24 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarManageable; | 27 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarManageable; |
25 import org.chromium.chrome.browser.tab.Tab; | 28 import org.chromium.chrome.browser.tab.Tab; |
26 import org.chromium.chrome.browser.tab.TabDelegateFactory; | 29 import org.chromium.chrome.browser.tab.TabDelegateFactory; |
27 import org.chromium.chrome.browser.tab.TabIdManager; | 30 import org.chromium.chrome.browser.tab.TabIdManager; |
28 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 31 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
29 import org.chromium.chrome.browser.util.IntentUtils; | 32 import org.chromium.chrome.browser.util.IntentUtils; |
30 import org.chromium.components.url_formatter.UrlFormatter; | 33 import org.chromium.components.url_formatter.UrlFormatter; |
31 import org.chromium.content_public.browser.LoadUrlParams; | 34 import org.chromium.content_public.browser.LoadUrlParams; |
32 import org.chromium.ui.base.ActivityWindowAndroid; | 35 import org.chromium.ui.base.ActivityWindowAndroid; |
33 | 36 |
34 /** Queries the user's default search engine and shows autocomplete suggestions. */ | 37 /** Queries the user's default search engine and shows autocomplete suggestions. */ |
35 public class SearchActivity extends AsyncInitializationActivity | 38 public class SearchActivity extends AsyncInitializationActivity |
36 implements SnackbarManageable, SearchActivityLocationBarLayout.Delegate { | 39 implements SnackbarManageable, SearchActivityLocationBarLayout.Delegate { |
40 private static final String TAG = "searchwidget"; | |
41 | |
37 /** Setting this field causes the Activity to finish itself immediately for tests. */ | 42 /** Setting this field causes the Activity to finish itself immediately for tests. */ |
38 private static boolean sIsDisabledForTest; | 43 private static boolean sIsDisabledForTest; |
39 | 44 |
40 /** Main content view. */ | 45 /** Main content view. */ |
41 private ViewGroup mContentView; | 46 private ViewGroup mContentView; |
42 | 47 |
43 /** Whether the native library has been loaded. */ | 48 /** Whether the user is now allowed to perform searches. */ |
44 private boolean mIsNativeReady; | 49 private boolean mIsActivityUsable; |
45 | 50 |
46 /** Input submitted before before the native library was loaded. */ | 51 /** Input submitted before before the native library was loaded. */ |
47 private String mQueuedUrl; | 52 private String mQueuedUrl; |
48 | 53 |
49 /** The View that represents the search box. */ | 54 /** The View that represents the search box. */ |
50 private SearchActivityLocationBarLayout mSearchBox; | 55 private SearchActivityLocationBarLayout mSearchBox; |
51 | 56 |
52 private SnackbarManager mSnackbarManager; | 57 private SnackbarManager mSnackbarManager; |
53 private SearchBoxDataProvider mSearchBoxDataProvider; | 58 private SearchBoxDataProvider mSearchBoxDataProvider; |
54 private Tab mTab; | 59 private Tab mTab; |
(...skipping 27 matching lines...) Expand all Loading... | |
82 // Build the search box. | 87 // Build the search box. |
83 mContentView = createContentView(); | 88 mContentView = createContentView(); |
84 mSearchBox = (SearchActivityLocationBarLayout) mContentView.findViewById ( | 89 mSearchBox = (SearchActivityLocationBarLayout) mContentView.findViewById ( |
85 R.id.search_location_bar); | 90 R.id.search_location_bar); |
86 mSearchBox.setDelegate(this); | 91 mSearchBox.setDelegate(this); |
87 mSearchBox.setToolbarDataProvider(mSearchBoxDataProvider); | 92 mSearchBox.setToolbarDataProvider(mSearchBoxDataProvider); |
88 mSearchBox.initializeControls(new WindowDelegate(getWindow()), getWindow Android()); | 93 mSearchBox.initializeControls(new WindowDelegate(getWindow()), getWindow Android()); |
89 setContentView(mContentView); | 94 setContentView(mContentView); |
90 | 95 |
91 // Kick off everything needed for the user to type into the box. | 96 // Kick off everything needed for the user to type into the box. |
97 // TODO(dfalcantara): We should prevent the user from doing anything whi le we're running the | |
98 // logic to determine if they need to see a search en gine promo. Given | |
99 // that the logic requires native to be loaded, we'll have to make some | |
100 // easy Java-only first-pass checks. | |
92 beginQuery(); | 101 beginQuery(); |
93 mSearchBox.showCachedZeroSuggestResultsIfAvailable(); | 102 mSearchBox.showCachedZeroSuggestResultsIfAvailable(); |
94 | 103 |
95 // Kick off loading of the native library. | 104 // Kick off loading of the native library. |
96 mHandler.post(new Runnable() { | 105 mHandler.post(new Runnable() { |
97 @Override | 106 @Override |
98 public void run() { | 107 public void run() { |
99 beginLoadingLibrary(); | 108 beginLoadingLibrary(); |
100 } | 109 } |
101 }); | 110 }); |
102 } | 111 } |
103 | 112 |
104 @Override | 113 @Override |
105 public void finishNativeInitialization() { | 114 public void finishNativeInitialization() { |
106 super.finishNativeInitialization(); | 115 super.finishNativeInitialization(); |
107 mIsNativeReady = true; | |
108 | 116 |
109 mTab = new Tab(TabIdManager.getInstance().generateValidId(Tab.INVALID_TA B_ID), | 117 mTab = new Tab(TabIdManager.getInstance().generateValidId(Tab.INVALID_TA B_ID), |
110 Tab.INVALID_TAB_ID, false, this, getWindowAndroid(), | 118 Tab.INVALID_TAB_ID, false, this, getWindowAndroid(), |
111 TabLaunchType.FROM_EXTERNAL_APP, null, null); | 119 TabLaunchType.FROM_EXTERNAL_APP, null, null); |
112 mTab.initialize(WebContentsFactory.createWebContents(false, false), null , | 120 mTab.initialize(WebContentsFactory.createWebContents(false, false), null , |
113 new TabDelegateFactory(), false, false); | 121 new TabDelegateFactory(), false, false); |
114 mTab.loadUrl(new LoadUrlParams("about:blank")); | 122 mTab.loadUrl(new LoadUrlParams("about:blank")); |
115 | 123 |
116 mSearchBoxDataProvider.onNativeLibraryReady(mTab); | 124 mSearchBoxDataProvider.onNativeLibraryReady(mTab); |
117 mSearchBox.onNativeLibraryReady(); | 125 mSearchBox.onNativeLibraryReady(); |
118 | 126 |
119 if (mQueuedUrl != null) loadUrl(mQueuedUrl); | 127 // Force the user to choose a search engine if they have to. |
120 | 128 final Callback<Boolean> deferredCallback = new Callback<Boolean>() { |
121 mHandler.post(new Runnable() { | |
122 @Override | 129 @Override |
123 public void run() { | 130 public void onResult(Boolean result) { |
124 onDeferredStartup(); | 131 finishDeferredInitialization(result); |
125 } | 132 } |
126 }); | 133 }; |
134 if (!LocaleManager.getInstance().showSearchEnginePromoIfNeeded(this, def erredCallback)) { | |
135 mHandler.post(new Runnable() { | |
136 @Override | |
137 public void run() { | |
138 deferredCallback.onResult(true); | |
139 } | |
140 }); | |
141 } | |
127 } | 142 } |
128 | 143 |
129 @Override | 144 private void finishDeferredInitialization(Boolean result) { |
130 public void onDeferredStartup() { | 145 if (result == null || !result.booleanValue()) { |
131 super.onDeferredStartup(); | 146 Log.e(TAG, "User failed to select a default search engine."); |
147 finish(); | |
Ted C
2017/04/27 16:40:46
and remove task?
pre-L, will this still be in rec
gone
2017/04/27 17:48:52
It doesn't seem to be in recents; the AndroidManif
Ted C
2017/04/27 18:10:32
Acknowledged.
| |
148 return; | |
149 } | |
150 | |
151 mIsActivityUsable = true; | |
152 if (mQueuedUrl != null) loadUrl(mQueuedUrl); | |
132 | 153 |
133 AutocompleteController.nativePrefetchZeroSuggestResults(); | 154 AutocompleteController.nativePrefetchZeroSuggestResults(); |
134 CustomTabsConnection.getInstance(getApplication()).warmup(0); | 155 CustomTabsConnection.getInstance(getApplication()).warmup(0); |
135 mSearchBox.onDeferredStartup(isVoiceSearchIntent()); | 156 mSearchBox.onDeferredStartup(isVoiceSearchIntent()); |
136 } | 157 } |
137 | 158 |
138 @Override | 159 @Override |
139 protected View getViewToBeDrawnBeforeInitializingNative() { | 160 protected View getViewToBeDrawnBeforeInitializingNative() { |
140 return mSearchBox; | 161 return mSearchBox; |
141 } | 162 } |
(...skipping 26 matching lines...) Expand all Loading... | |
168 } | 189 } |
169 | 190 |
170 @Override | 191 @Override |
171 public boolean shouldStartGpuProcess() { | 192 public boolean shouldStartGpuProcess() { |
172 return true; | 193 return true; |
173 } | 194 } |
174 | 195 |
175 @Override | 196 @Override |
176 public void loadUrl(String url) { | 197 public void loadUrl(String url) { |
177 // Wait until native has loaded. | 198 // Wait until native has loaded. |
178 if (!mIsNativeReady) { | 199 if (!mIsActivityUsable) { |
179 mQueuedUrl = url; | 200 mQueuedUrl = url; |
180 return; | 201 return; |
181 } | 202 } |
182 | 203 |
183 // Don't do anything if the input was empty. This is done after the nati ve check to prevent | 204 // Don't do anything if the input was empty. This is done after the nati ve check to prevent |
184 // resending a queued query after the user deleted it. | 205 // resending a queued query after the user deleted it. |
185 if (TextUtils.isEmpty(url)) return; | 206 if (TextUtils.isEmpty(url)) return; |
186 | 207 |
187 // Fix up the URL and send it to the full browser. | 208 // Fix up the URL and send it to the full browser. |
188 String fixedUrl = UrlFormatter.fixupUrl(url); | 209 String fixedUrl = UrlFormatter.fixupUrl(url); |
(...skipping 26 matching lines...) Expand all Loading... | |
215 finish(); | 236 finish(); |
216 overridePendingTransition(0, R.anim.activity_close_exit); | 237 overridePendingTransition(0, R.anim.activity_close_exit); |
217 } | 238 } |
218 | 239 |
219 /** See {@link #sIsDisabledForTest}. */ | 240 /** See {@link #sIsDisabledForTest}. */ |
220 @VisibleForTesting | 241 @VisibleForTesting |
221 static void disableForTests() { | 242 static void disableForTests() { |
222 sIsDisabledForTest = true; | 243 sIsDisabledForTest = true; |
223 } | 244 } |
224 } | 245 } |
OLD | NEW |