Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/search_engines/TemplateUrlService.java

Issue 2367373003: [Android] Allow setting recently visited search engines as default search engine (Closed)
Patch Set: Update based on Dan's comments. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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.search_engines; 5 package org.chromium.chrome.browser.search_engines;
6 6
7 import android.support.annotation.IntDef;
7 import android.text.TextUtils; 8 import android.text.TextUtils;
8 9
9 import org.chromium.base.ObserverList; 10 import org.chromium.base.ObserverList;
10 import org.chromium.base.ThreadUtils; 11 import org.chromium.base.ThreadUtils;
11 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.CalledByNative;
12 13
14 import java.lang.annotation.Retention;
15 import java.lang.annotation.RetentionPolicy;
13 import java.util.ArrayList; 16 import java.util.ArrayList;
14 import java.util.List; 17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
15 20
16 /** 21 /**
17 * Android wrapper of the TemplateUrlService which provides access from the Java 22 * Android wrapper of the TemplateUrlService which provides access from the Java
18 * layer. 23 * layer.
19 * 24 *
20 * Only usable from the UI thread as it's primary purpose is for supporting the Android 25 * Only usable from the UI thread as it's primary purpose is for supporting the Android
21 * preferences UI. 26 * preferences UI.
22 * 27 *
23 * See components/search_engines/template_url_service.h for more details. 28 * See components/search_engines/template_url_service.h for more details.
24 */ 29 */
(...skipping 10 matching lines...) Expand all
35 * Observer to be notified whenever the set of TemplateURLs are modified. 40 * Observer to be notified whenever the set of TemplateURLs are modified.
36 */ 41 */
37 public interface TemplateUrlServiceObserver { 42 public interface TemplateUrlServiceObserver {
38 /** 43 /**
39 * Notification that the template url model has changed in some way. 44 * Notification that the template url model has changed in some way.
40 */ 45 */
41 void onTemplateURLServiceChanged(); 46 void onTemplateURLServiceChanged();
42 } 47 }
43 48
44 /** 49 /**
50 * Type for default search engine which is not prepopulated. This is needed because
51 * if a custom search engine is set as default, it will be moved to the prep opulated list.
52 */
53 public static final int TYPE_DEFAULT = 0;
54 public static final int TYPE_PREPOPULATED = 1;
55 public static final int TYPE_RECENT = 2;
56
57 @IntDef({TYPE_PREPOPULATED, TYPE_DEFAULT, TYPE_RECENT})
gone 2016/11/01 21:30:29 Reorder these so that they match the value orderin
ltian 2016/11/01 22:59:36 Done.
58 @Retention(RetentionPolicy.SOURCE)
59 public @interface TemplateUrlType {}
60
61 /**
45 * Represents search engine with its index. 62 * Represents search engine with its index.
46 */ 63 */
47 public static class TemplateUrl { 64 public static class TemplateUrl {
48 private final int mIndex; 65 private final int mIndex;
49 private final String mShortName; 66 private final String mShortName;
50 private boolean mIsPrepopulated; 67 private final String mUrl;
68 private final boolean mIsPrepopulated;
69 private final String mKeyword;
70 @TemplateUrlType
71 private int mTemplateUrlType;
gone 2016/11/01 21:30:29 maybe @TemplateUrlType private int mTemplateUrlTy
ltian 2016/11/01 22:59:36 Done.
51 72
52 @CalledByNative("TemplateUrl") 73 @CalledByNative("TemplateUrl")
53 public static TemplateUrl create(int id, String shortName, boolean isPre populated) { 74 public static TemplateUrl create(
54 return new TemplateUrl(id, shortName, isPrepopulated); 75 int id, String shortName, String url, boolean isPrepopulated, St ring keyword) {
76 return new TemplateUrl(id, shortName, url, isPrepopulated, keyword);
55 } 77 }
56 78
57 public TemplateUrl(int index, String shortName, boolean isPrepopulated) { 79 public TemplateUrl(
80 int index, String shortName, String url, boolean isPrepopulated, String keyword) {
58 mIndex = index; 81 mIndex = index;
59 mShortName = shortName; 82 mShortName = shortName;
83 Pattern pattern = Pattern.compile("[^/]+.com");
84 Matcher m = pattern.matcher(url);
gone 2016/11/01 21:30:29 mUrl = m.find() ? m.group(0) : "";
ltian 2016/11/01 22:59:36 Done.
85 if (!m.find()) {
86 mUrl = "";
87 } else {
88 mUrl = m.group(0);
89 }
60 mIsPrepopulated = isPrepopulated; 90 mIsPrepopulated = isPrepopulated;
91 mKeyword = keyword;
61 } 92 }
62 93
63 public int getIndex() { 94 public int getIndex() {
64 return mIndex; 95 return mIndex;
65 } 96 }
66 97
67 public String getShortName() { 98 public String getShortName() {
68 return mShortName; 99 return mShortName;
69 } 100 }
70 101
102 public String getUrl() {
103 return mUrl;
104 }
105
106 public boolean getIsPrepopulated() {
107 return mIsPrepopulated;
108 }
109
110 public String getKeyword() {
111 return mKeyword;
112 }
113
114 public void setType(@TemplateUrlType int templateUrlType) {
115 mTemplateUrlType = templateUrlType;
116 }
117
118 @TemplateUrlType
119 public int getType() {
120 return mTemplateUrlType;
121 }
122
71 @Override 123 @Override
72 public int hashCode() { 124 public int hashCode() {
73 final int prime = 31; 125 final int prime = 31;
74 int result = 1; 126 int result = 1;
75 result = prime * result + mIndex; 127 result = prime * result + mIndex;
76 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode()); 128 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode());
77 return result; 129 return result;
78 } 130 }
79 131
80 @Override 132 @Override
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 nativeLoad(mNativeTemplateUrlServiceAndroid); 169 nativeLoad(mNativeTemplateUrlServiceAndroid);
118 } 170 }
119 171
120 /** 172 /**
121 * Returns a list of the prepopulated search engines. 173 * Returns a list of the prepopulated search engines.
122 * 174 *
123 * Warning: TemplateUrl.getIndex() is *not* an index into this list, since t his list contains 175 * Warning: TemplateUrl.getIndex() is *not* an index into this list, since t his list contains
124 * only prepopulated search engines. E.g. getLocalizedSearchEngines().get(0) .getIndex() could 176 * only prepopulated search engines. E.g. getLocalizedSearchEngines().get(0) .getIndex() could
125 * return 3. 177 * return 3.
126 */ 178 */
127 public List<TemplateUrl> getLocalizedSearchEngines() { 179 public List<TemplateUrl> getSearchEngines() {
128 ThreadUtils.assertOnUiThread(); 180 ThreadUtils.assertOnUiThread();
181 int defaultSearchEngineIndex = getDefaultSearchEngineIndex();
129 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid); 182 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid);
130 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount); 183 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount);
131 for (int i = 0; i < templateUrlCount; i++) { 184 for (int i = 0; i < templateUrlCount; i++) {
132 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i); 185 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i);
133 if (templateUrl != null && templateUrl.mIsPrepopulated) { 186 if (templateUrl != null) {
187 setSearchEngineType(templateUrl, defaultSearchEngineIndex);
134 templateUrls.add(templateUrl); 188 templateUrls.add(templateUrl);
135 } 189 }
136 } 190 }
137 return templateUrls; 191 return templateUrls;
138 } 192 }
139 193
194 private void setSearchEngineType(TemplateUrl templateUrl, int defaultSearchE ngineIndex) {
195 if (templateUrl.getIsPrepopulated()) {
196 templateUrl.setType(TYPE_PREPOPULATED);
197 } else if (templateUrl.getIndex() == defaultSearchEngineIndex) {
198 templateUrl.setType(TYPE_DEFAULT);
199 } else {
200 templateUrl.setType(TYPE_RECENT);
201 }
202 }
203
140 /** 204 /**
141 * Called from native when template URL service is done loading. 205 * Called from native when template URL service is done loading.
142 */ 206 */
143 @CalledByNative 207 @CalledByNative
144 private void templateUrlServiceLoaded() { 208 private void templateUrlServiceLoaded() {
145 ThreadUtils.assertOnUiThread(); 209 ThreadUtils.assertOnUiThread();
146 for (LoadListener listener : mLoadListeners) { 210 for (LoadListener listener : mLoadListeners) {
147 listener.onTemplateUrlServiceLoaded(); 211 listener.onTemplateUrlServiceLoaded();
148 } 212 }
149 } 213 }
(...skipping 22 matching lines...) Expand all
172 int defaultSearchEngineIndex = getDefaultSearchEngineIndex(); 236 int defaultSearchEngineIndex = getDefaultSearchEngineIndex();
173 if (defaultSearchEngineIndex == -1) return null; 237 if (defaultSearchEngineIndex == -1) return null;
174 238
175 assert defaultSearchEngineIndex >= 0; 239 assert defaultSearchEngineIndex >= 0;
176 assert defaultSearchEngineIndex < nativeGetTemplateUrlCount( 240 assert defaultSearchEngineIndex < nativeGetTemplateUrlCount(
177 mNativeTemplateUrlServiceAndroid); 241 mNativeTemplateUrlServiceAndroid);
178 242
179 return nativeGetTemplateUrlAt(mNativeTemplateUrlServiceAndroid, defaultS earchEngineIndex); 243 return nativeGetTemplateUrlAt(mNativeTemplateUrlServiceAndroid, defaultS earchEngineIndex);
180 } 244 }
181 245
182 public void setSearchEngine(int selectedIndex) { 246 public void setSearchEngine(String selectedKeyword) {
183 ThreadUtils.assertOnUiThread(); 247 ThreadUtils.assertOnUiThread();
184 nativeSetUserSelectedDefaultSearchProvider(mNativeTemplateUrlServiceAndr oid, selectedIndex); 248 nativeSetUserSelectedDefaultSearchProvider(
249 mNativeTemplateUrlServiceAndroid, selectedKeyword);
185 } 250 }
186 251
187 public boolean isSearchProviderManaged() { 252 public boolean isSearchProviderManaged() {
188 return nativeIsSearchProviderManaged(mNativeTemplateUrlServiceAndroid); 253 return nativeIsSearchProviderManaged(mNativeTemplateUrlServiceAndroid);
189 } 254 }
190 255
191 /** 256 /**
192 * @return Whether or not the default search engine has search by image supp ort. 257 * @return Whether or not the default search engine has search by image supp ort.
193 */ 258 */
194 public boolean isSearchByImageAvailable() { 259 public boolean isSearchByImageAvailable() {
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion) { 355 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion) {
291 return nativeGetUrlForContextualSearchQuery(mNativeTemplateUrlServiceAnd roid, query, 356 return nativeGetUrlForContextualSearchQuery(mNativeTemplateUrlServiceAnd roid, query,
292 alternateTerm, shouldPrefetch, protocolVersion); 357 alternateTerm, shouldPrefetch, protocolVersion);
293 } 358 }
294 359
295 /** 360 /**
296 * Finds the URL for the search engine at the given index. 361 * Finds the URL for the search engine at the given index.
297 * @param index The templateUrl index to look up. 362 * @param index The templateUrl index to look up.
298 * @return A {@link String} that contains the url of the specified sear ch engine. 363 * @return A {@link String} that contains the url of the specified sear ch engine.
299 */ 364 */
300 public String getSearchEngineUrlFromTemplateUrl(int index) { 365 public String getSearchEngineUrlFromTemplateUrl(String keyword) {
301 return nativeGetSearchEngineUrlFromTemplateUrl(mNativeTemplateUrlService Android, index); 366 return nativeGetSearchEngineUrlFromTemplateUrl(mNativeTemplateUrlService Android, keyword);
302 } 367 }
303 368
304 private native long nativeInit(); 369 private native long nativeInit();
305 private native void nativeLoad(long nativeTemplateUrlServiceAndroid); 370 private native void nativeLoad(long nativeTemplateUrlServiceAndroid);
306 private native boolean nativeIsLoaded(long nativeTemplateUrlServiceAndroid); 371 private native boolean nativeIsLoaded(long nativeTemplateUrlServiceAndroid);
307 private native int nativeGetTemplateUrlCount(long nativeTemplateUrlServiceAn droid); 372 private native int nativeGetTemplateUrlCount(long nativeTemplateUrlServiceAn droid);
308 private native TemplateUrl nativeGetTemplateUrlAt(long nativeTemplateUrlServ iceAndroid, int i); 373 private native TemplateUrl nativeGetTemplateUrlAt(long nativeTemplateUrlServ iceAndroid, int i);
309 private native void nativeSetUserSelectedDefaultSearchProvider( 374 private native void nativeSetUserSelectedDefaultSearchProvider(
310 long nativeTemplateUrlServiceAndroid, int selectedIndex); 375 long nativeTemplateUrlServiceAndroid, String selectedKeyword);
311 private native int nativeGetDefaultSearchProvider(long nativeTemplateUrlServ iceAndroid); 376 private native int nativeGetDefaultSearchProvider(long nativeTemplateUrlServ iceAndroid);
312 private native boolean nativeIsSearchProviderManaged(long nativeTemplateUrlS erviceAndroid); 377 private native boolean nativeIsSearchProviderManaged(long nativeTemplateUrlS erviceAndroid);
313 private native boolean nativeIsSearchByImageAvailable(long nativeTemplateUrl ServiceAndroid); 378 private native boolean nativeIsSearchByImageAvailable(long nativeTemplateUrl ServiceAndroid);
314 private native boolean nativeIsDefaultSearchEngineGoogle(long nativeTemplate UrlServiceAndroid); 379 private native boolean nativeIsDefaultSearchEngineGoogle(long nativeTemplate UrlServiceAndroid);
315 private native String nativeGetUrlForSearchQuery(long nativeTemplateUrlServi ceAndroid, 380 private native String nativeGetUrlForSearchQuery(long nativeTemplateUrlServi ceAndroid,
316 String query); 381 String query);
317 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid, 382 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid,
318 String query); 383 String query);
319 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid, 384 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid,
320 String query, String currentUrl); 385 String query, String currentUrl);
321 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid, 386 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid,
322 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion); 387 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion);
323 private native String nativeGetSearchEngineUrlFromTemplateUrl( 388 private native String nativeGetSearchEngineUrlFromTemplateUrl(
324 long nativeTemplateUrlServiceAndroid, int index); 389 long nativeTemplateUrlServiceAndroid, String keyword);
325 } 390 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698