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

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 Peter and Dan's comments. Created 4 years 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 @IntDef({TYPE_DEFAULT, TYPE_PREPOPULATED, TYPE_RECENT})
54 @Retention(RetentionPolicy.SOURCE)
55 public @interface TemplateUrlType {}
56 public static final int TYPE_DEFAULT = 0;
57 public static final int TYPE_PREPOPULATED = 1;
58 public static final int TYPE_RECENT = 2;
59
60 /**
45 * Represents search engine with its index. 61 * Represents search engine with its index.
46 */ 62 */
47 public static class TemplateUrl { 63 public static class TemplateUrl {
48 private final int mIndex; 64 private final int mIndex;
49 private final String mShortName; 65 private final String mShortName;
50 private boolean mIsPrepopulated; 66 private final String mUrl;
67 private final boolean mIsPrepopulated;
68 private final String mKeyword;
69 @TemplateUrlType private int mTemplateUrlType;
51 70
52 @CalledByNative("TemplateUrl") 71 @CalledByNative("TemplateUrl")
53 public static TemplateUrl create(int id, String shortName, boolean isPre populated) { 72 public static TemplateUrl create(
54 return new TemplateUrl(id, shortName, isPrepopulated); 73 int id, String shortName, String url, boolean isPrepopulated, St ring keyword) {
74 return new TemplateUrl(id, shortName, url, isPrepopulated, keyword);
55 } 75 }
56 76
57 public TemplateUrl(int index, String shortName, boolean isPrepopulated) { 77 public TemplateUrl(
78 int index, String shortName, String url, boolean isPrepopulated, String keyword) {
58 mIndex = index; 79 mIndex = index;
59 mShortName = shortName; 80 mShortName = shortName;
81 Pattern pattern = Pattern.compile("[^/]+.com");
82 Matcher m = pattern.matcher(url);
83 mUrl = m.find() ? m.group(0) : "";
60 mIsPrepopulated = isPrepopulated; 84 mIsPrepopulated = isPrepopulated;
85 mKeyword = keyword;
61 } 86 }
62 87
63 public int getIndex() { 88 public int getIndex() {
64 return mIndex; 89 return mIndex;
65 } 90 }
66 91
67 public String getShortName() { 92 public String getShortName() {
68 return mShortName; 93 return mShortName;
69 } 94 }
70 95
96 public String getUrl() {
97 return mUrl;
98 }
99
100 public boolean getIsPrepopulated() {
101 return mIsPrepopulated;
102 }
103
104 public String getKeyword() {
105 return mKeyword;
106 }
107
108 public void setType(@TemplateUrlType int templateUrlType) {
109 mTemplateUrlType = templateUrlType;
110 }
111
112 @TemplateUrlType
113 public int getType() {
114 return mTemplateUrlType;
115 }
116
71 @Override 117 @Override
72 public int hashCode() { 118 public int hashCode() {
73 final int prime = 31; 119 final int prime = 31;
74 int result = 1; 120 int result = 1;
75 result = prime * result + mIndex; 121 result = prime * result + mIndex;
76 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode()); 122 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode());
77 return result; 123 return result;
78 } 124 }
79 125
80 @Override 126 @Override
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 nativeLoad(mNativeTemplateUrlServiceAndroid); 163 nativeLoad(mNativeTemplateUrlServiceAndroid);
118 } 164 }
119 165
120 /** 166 /**
121 * Returns a list of the prepopulated search engines. 167 * Returns a list of the prepopulated search engines.
122 * 168 *
123 * Warning: TemplateUrl.getIndex() is *not* an index into this list, since t his list contains 169 * 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 170 * only prepopulated search engines. E.g. getLocalizedSearchEngines().get(0) .getIndex() could
125 * return 3. 171 * return 3.
126 */ 172 */
127 public List<TemplateUrl> getLocalizedSearchEngines() { 173 public List<TemplateUrl> getSearchEngines() {
128 ThreadUtils.assertOnUiThread(); 174 ThreadUtils.assertOnUiThread();
175 int defaultSearchEngineIndex = getDefaultSearchEngineIndex();
129 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid); 176 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid);
130 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount); 177 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount);
131 for (int i = 0; i < templateUrlCount; i++) { 178 for (int i = 0; i < templateUrlCount; i++) {
132 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i); 179 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i);
133 if (templateUrl != null && templateUrl.mIsPrepopulated) { 180 if (templateUrl != null) {
181 setSearchEngineType(templateUrl, defaultSearchEngineIndex);
134 templateUrls.add(templateUrl); 182 templateUrls.add(templateUrl);
135 } 183 }
136 } 184 }
137 return templateUrls; 185 return templateUrls;
138 } 186 }
139 187
188 private void setSearchEngineType(TemplateUrl templateUrl, int defaultSearchE ngineIndex) {
189 if (templateUrl.getIsPrepopulated()) {
190 templateUrl.setType(TYPE_PREPOPULATED);
191 } else if (templateUrl.getIndex() == defaultSearchEngineIndex) {
192 templateUrl.setType(TYPE_DEFAULT);
193 } else {
194 templateUrl.setType(TYPE_RECENT);
195 }
196 }
197
140 /** 198 /**
141 * Called from native when template URL service is done loading. 199 * Called from native when template URL service is done loading.
142 */ 200 */
143 @CalledByNative 201 @CalledByNative
144 private void templateUrlServiceLoaded() { 202 private void templateUrlServiceLoaded() {
145 ThreadUtils.assertOnUiThread(); 203 ThreadUtils.assertOnUiThread();
146 for (LoadListener listener : mLoadListeners) { 204 for (LoadListener listener : mLoadListeners) {
147 listener.onTemplateUrlServiceLoaded(); 205 listener.onTemplateUrlServiceLoaded();
148 } 206 }
149 } 207 }
150 208
151 @CalledByNative 209 @CalledByNative
152 private void onTemplateURLServiceChanged() { 210 private void onTemplateURLServiceChanged() {
153 for (TemplateUrlServiceObserver observer : mObservers) { 211 for (TemplateUrlServiceObserver observer : mObservers) {
154 observer.onTemplateURLServiceChanged(); 212 observer.onTemplateURLServiceChanged();
155 } 213 }
156 } 214 }
157 215
158 /** 216 /**
159 * @return The default search engine index (e.g., 0, 1, 2,...). 217 * @return The default search engine index (e.g., 0, 1, 2,...).
160 */ 218 */
161 public int getDefaultSearchEngineIndex() { 219 public int getDefaultSearchEngineIndex() {
162 ThreadUtils.assertOnUiThread(); 220 ThreadUtils.assertOnUiThread();
163 return nativeGetDefaultSearchProvider(mNativeTemplateUrlServiceAndroid); 221 return nativeGetDefaultSearchProviderIndex(mNativeTemplateUrlServiceAndr oid);
164 } 222 }
165 223
166 /** 224 /**
167 * @return {@link TemplateUrlService.TemplateUrl} for the default search eng ine. 225 * @return {@link TemplateUrlService.TemplateUrl} for the default search eng ine.
168 */ 226 */
169 public TemplateUrl getDefaultSearchEngineTemplateUrl() { 227 public TemplateUrl getDefaultSearchEngineTemplateUrl() {
170 if (!isLoaded()) return null; 228 if (!isLoaded()) return null;
171 229
172 int defaultSearchEngineIndex = getDefaultSearchEngineIndex(); 230 int defaultSearchEngineIndex = getDefaultSearchEngineIndex();
173 if (defaultSearchEngineIndex == -1) return null; 231 if (defaultSearchEngineIndex == -1) return null;
174 232
175 assert defaultSearchEngineIndex >= 0; 233 assert defaultSearchEngineIndex >= 0;
176 assert defaultSearchEngineIndex < nativeGetTemplateUrlCount( 234 assert defaultSearchEngineIndex < nativeGetTemplateUrlCount(
177 mNativeTemplateUrlServiceAndroid); 235 mNativeTemplateUrlServiceAndroid);
178 236
179 return nativeGetTemplateUrlAt(mNativeTemplateUrlServiceAndroid, defaultS earchEngineIndex); 237 return nativeGetTemplateUrlAt(mNativeTemplateUrlServiceAndroid, defaultS earchEngineIndex);
180 } 238 }
181 239
182 public void setSearchEngine(int selectedIndex) { 240 public void setSearchEngine(String selectedKeyword) {
183 ThreadUtils.assertOnUiThread(); 241 ThreadUtils.assertOnUiThread();
184 nativeSetUserSelectedDefaultSearchProvider(mNativeTemplateUrlServiceAndr oid, selectedIndex); 242 nativeSetUserSelectedDefaultSearchProvider(
243 mNativeTemplateUrlServiceAndroid, selectedKeyword);
185 } 244 }
186 245
187 public boolean isSearchProviderManaged() { 246 public boolean isSearchProviderManaged() {
188 return nativeIsSearchProviderManaged(mNativeTemplateUrlServiceAndroid); 247 return nativeIsSearchProviderManaged(mNativeTemplateUrlServiceAndroid);
189 } 248 }
190 249
191 /** 250 /**
192 * @return Whether or not the default search engine has search by image supp ort. 251 * @return Whether or not the default search engine has search by image supp ort.
193 */ 252 */
194 public boolean isSearchByImageAvailable() { 253 public boolean isSearchByImageAvailable() {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion) { 362 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion) {
304 return nativeGetUrlForContextualSearchQuery(mNativeTemplateUrlServiceAnd roid, query, 363 return nativeGetUrlForContextualSearchQuery(mNativeTemplateUrlServiceAnd roid, query,
305 alternateTerm, shouldPrefetch, protocolVersion); 364 alternateTerm, shouldPrefetch, protocolVersion);
306 } 365 }
307 366
308 /** 367 /**
309 * Finds the URL for the search engine at the given index. 368 * Finds the URL for the search engine at the given index.
310 * @param index The templateUrl index to look up. 369 * @param index The templateUrl index to look up.
311 * @return A {@link String} that contains the url of the specified sear ch engine. 370 * @return A {@link String} that contains the url of the specified sear ch engine.
312 */ 371 */
313 public String getSearchEngineUrlFromTemplateUrl(int index) { 372 public String getSearchEngineUrlFromTemplateUrl(String keyword) {
314 return nativeGetSearchEngineUrlFromTemplateUrl(mNativeTemplateUrlService Android, index); 373 return nativeGetSearchEngineUrlFromTemplateUrl(mNativeTemplateUrlService Android, keyword);
315 } 374 }
316 375
317 private native long nativeInit(); 376 private native long nativeInit();
318 private native void nativeLoad(long nativeTemplateUrlServiceAndroid); 377 private native void nativeLoad(long nativeTemplateUrlServiceAndroid);
319 private native boolean nativeIsLoaded(long nativeTemplateUrlServiceAndroid); 378 private native boolean nativeIsLoaded(long nativeTemplateUrlServiceAndroid);
320 private native int nativeGetTemplateUrlCount(long nativeTemplateUrlServiceAn droid); 379 private native int nativeGetTemplateUrlCount(long nativeTemplateUrlServiceAn droid);
321 private native TemplateUrl nativeGetTemplateUrlAt(long nativeTemplateUrlServ iceAndroid, int i); 380 private native TemplateUrl nativeGetTemplateUrlAt(long nativeTemplateUrlServ iceAndroid, int i);
322 private native void nativeSetUserSelectedDefaultSearchProvider( 381 private native void nativeSetUserSelectedDefaultSearchProvider(
323 long nativeTemplateUrlServiceAndroid, int selectedIndex); 382 long nativeTemplateUrlServiceAndroid, String selectedKeyword);
324 private native int nativeGetDefaultSearchProvider(long nativeTemplateUrlServ iceAndroid); 383 private native int nativeGetDefaultSearchProviderIndex(long nativeTemplateUr lServiceAndroid);
325 private native boolean nativeIsSearchProviderManaged(long nativeTemplateUrlS erviceAndroid); 384 private native boolean nativeIsSearchProviderManaged(long nativeTemplateUrlS erviceAndroid);
326 private native boolean nativeIsSearchByImageAvailable(long nativeTemplateUrl ServiceAndroid); 385 private native boolean nativeIsSearchByImageAvailable(long nativeTemplateUrl ServiceAndroid);
327 private native boolean nativeIsDefaultSearchEngineGoogle(long nativeTemplate UrlServiceAndroid); 386 private native boolean nativeIsDefaultSearchEngineGoogle(long nativeTemplate UrlServiceAndroid);
328 private native String nativeGetUrlForSearchQuery(long nativeTemplateUrlServi ceAndroid, 387 private native String nativeGetUrlForSearchQuery(long nativeTemplateUrlServi ceAndroid,
329 String query); 388 String query);
330 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid, 389 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid,
331 String query); 390 String query);
332 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid, 391 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid,
333 String query, String currentUrl); 392 String query, String currentUrl);
334 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid, 393 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid,
335 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion); 394 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion);
336 private native String nativeGetSearchEngineUrlFromTemplateUrl( 395 private native String nativeGetSearchEngineUrlFromTemplateUrl(
337 long nativeTemplateUrlServiceAndroid, int index); 396 long nativeTemplateUrlServiceAndroid, String keyword);
338 } 397 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698