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