Chromium Code Reviews| 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.text.TextUtils; | 7 import android.text.TextUtils; |
| 8 | 8 |
| 9 import org.chromium.base.ObserverList; | 9 import org.chromium.base.ObserverList; |
| 10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.base.annotations.CalledByNative; | 11 import org.chromium.base.annotations.CalledByNative; |
| 12 | 12 |
| 13 import java.util.ArrayList; | 13 import java.util.ArrayList; |
| 14 import java.util.List; | 14 import java.util.List; |
| 15 import java.util.regex.Matcher; | |
| 16 import java.util.regex.Pattern; | |
| 15 | 17 |
| 16 /** | 18 /** |
| 17 * Android wrapper of the TemplateUrlService which provides access from the Java | 19 * Android wrapper of the TemplateUrlService which provides access from the Java |
| 18 * layer. | 20 * layer. |
| 19 * | 21 * |
| 20 * Only usable from the UI thread as it's primary purpose is for supporting the Android | 22 * Only usable from the UI thread as it's primary purpose is for supporting the Android |
| 21 * preferences UI. | 23 * preferences UI. |
| 22 * | 24 * |
| 23 * See components/search_engines/template_url_service.h for more details. | 25 * See components/search_engines/template_url_service.h for more details. |
| 24 */ | 26 */ |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 35 * Observer to be notified whenever the set of TemplateURLs are modified. | 37 * Observer to be notified whenever the set of TemplateURLs are modified. |
| 36 */ | 38 */ |
| 37 public interface TemplateUrlServiceObserver { | 39 public interface TemplateUrlServiceObserver { |
| 38 /** | 40 /** |
| 39 * Notification that the template url model has changed in some way. | 41 * Notification that the template url model has changed in some way. |
| 40 */ | 42 */ |
| 41 void onTemplateURLServiceChanged(); | 43 void onTemplateURLServiceChanged(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 /** | 46 /** |
| 47 * Type to sort & display different layouts for different type of search eng ines. | |
| 48 */ | |
| 49 public enum TemplateUrlType { | |
|
Ian Wen
2016/09/27 04:42:00
Let's use ints instead of enums. https://developer
ltian
2016/09/28 19:19:47
Done.
| |
| 50 PREPOPULATED, | |
| 51 /** | |
| 52 * Type for default search engine which is not prepopulated. This is nee ded because | |
| 53 * if a custom search engine is set as default, it will move to default list. | |
| 54 */ | |
| 55 DEFAULT, | |
| 56 /** | |
| 57 * Type to identify divdier view. | |
| 58 */ | |
| 59 DIVIDER, | |
|
Ian Wen
2016/09/27 04:42:00
Remove DIVIDER
ltian
2016/09/28 19:19:47
Done.
| |
| 60 CUSTOM | |
|
Ian Wen
2016/09/27 04:42:00
I wouldn't use the term "custom" here, as the list
ltian
2016/09/28 19:19:46
Done.
| |
| 61 } | |
| 62 | |
| 63 /** | |
| 45 * Represents search engine with its index. | 64 * Represents search engine with its index. |
| 46 */ | 65 */ |
| 47 public static class TemplateUrl { | 66 public static class TemplateUrl implements Comparable<TemplateUrl> { |
| 48 private final int mIndex; | 67 private final int mIndex; |
| 49 private final String mShortName; | 68 private final String mShortName; |
| 50 private boolean mIsPrepopulated; | 69 private final String mUrl; |
| 70 private final boolean mIsPrepopulated; | |
| 71 private TemplateUrlType mTemplateUrlType; | |
| 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) { |
| 76 return new TemplateUrl(id, shortName, url, isPrepopulated); | |
| 55 } | 77 } |
| 56 | 78 |
| 57 public TemplateUrl(int index, String shortName, boolean isPrepopulated) { | 79 public TemplateUrl(int index, String shortName, String url, boolean isPr epopulated) { |
| 58 mIndex = index; | 80 mIndex = index; |
| 59 mShortName = shortName; | 81 mShortName = shortName; |
| 82 Pattern pattern = Pattern.compile("[^/]+.com"); | |
| 83 Matcher m = pattern.matcher(url); | |
| 84 if (!m.find()) { | |
| 85 mUrl = ""; | |
| 86 } else { | |
| 87 mUrl = m.group(0); | |
| 88 } | |
| 60 mIsPrepopulated = isPrepopulated; | 89 mIsPrepopulated = isPrepopulated; |
| 61 } | 90 } |
| 62 | 91 |
| 63 public int getIndex() { | 92 public int getIndex() { |
| 64 return mIndex; | 93 return mIndex; |
| 65 } | 94 } |
| 66 | 95 |
| 67 public String getShortName() { | 96 public String getShortName() { |
| 68 return mShortName; | 97 return mShortName; |
| 69 } | 98 } |
| 70 | 99 |
| 100 public String getUrl() { | |
| 101 return mUrl; | |
| 102 } | |
| 103 | |
| 104 public boolean getIsPrepopulated() { | |
|
Ian Wen
2016/09/27 04:42:00
Instead of creating this method, you could create
ltian
2016/09/28 19:19:47
Done.
| |
| 105 return mIsPrepopulated; | |
| 106 } | |
| 107 | |
| 108 public void setTemplateUrlType(TemplateUrlType templateUrlType) { | |
| 109 mTemplateUrlType = templateUrlType; | |
| 110 } | |
| 111 | |
| 112 public TemplateUrlType getTemplateUrlType() { | |
| 113 return mTemplateUrlType; | |
| 114 } | |
| 115 | |
| 71 @Override | 116 @Override |
| 72 public int hashCode() { | 117 public int hashCode() { |
| 73 final int prime = 31; | 118 final int prime = 31; |
| 74 int result = 1; | 119 int result = 1; |
| 75 result = prime * result + mIndex; | 120 result = prime * result + mIndex; |
| 76 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode()); | 121 result = prime * result + ((mShortName == null) ? 0 : mShortName.has hCode()); |
| 77 return result; | 122 return result; |
| 78 } | 123 } |
| 79 | 124 |
| 80 @Override | 125 @Override |
| 81 public boolean equals(Object other) { | 126 public boolean equals(Object other) { |
| 82 if (!(other instanceof TemplateUrl)) return false; | 127 if (!(other instanceof TemplateUrl)) return false; |
| 83 TemplateUrl otherTemplateUrl = (TemplateUrl) other; | 128 TemplateUrl otherTemplateUrl = (TemplateUrl) other; |
| 84 return mIndex == otherTemplateUrl.mIndex | 129 return mIndex == otherTemplateUrl.mIndex |
| 85 && TextUtils.equals(mShortName, otherTemplateUrl.mShortName) ; | 130 && TextUtils.equals(mShortName, otherTemplateUrl.mShortName) ; |
| 86 } | 131 } |
| 132 | |
| 133 @Override | |
| 134 public int compareTo(TemplateUrl templateUrl) { | |
|
Ian Wen
2016/09/27 04:42:00
Q: why adding this?
ltian
2016/09/28 19:19:47
Done.
| |
| 135 return mTemplateUrlType.compareTo(templateUrl.getTemplateUrlType()); | |
| 136 } | |
| 87 } | 137 } |
| 88 | 138 |
| 89 private static TemplateUrlService sService; | 139 private static TemplateUrlService sService; |
| 90 | 140 |
| 91 public static TemplateUrlService getInstance() { | 141 public static TemplateUrlService getInstance() { |
| 92 ThreadUtils.assertOnUiThread(); | 142 ThreadUtils.assertOnUiThread(); |
| 93 if (sService == null) { | 143 if (sService == null) { |
| 94 sService = new TemplateUrlService(); | 144 sService = new TemplateUrlService(); |
| 95 } | 145 } |
| 96 return sService; | 146 return sService; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 123 * Warning: TemplateUrl.getIndex() is *not* an index into this list, since t his list contains | 173 * 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 | 174 * only prepopulated search engines. E.g. getLocalizedSearchEngines().get(0) .getIndex() could |
| 125 * return 3. | 175 * return 3. |
| 126 */ | 176 */ |
| 127 public List<TemplateUrl> getLocalizedSearchEngines() { | 177 public List<TemplateUrl> getLocalizedSearchEngines() { |
| 128 ThreadUtils.assertOnUiThread(); | 178 ThreadUtils.assertOnUiThread(); |
| 129 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid); | 179 int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServi ceAndroid); |
| 130 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount); | 180 List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlC ount); |
| 131 for (int i = 0; i < templateUrlCount; i++) { | 181 for (int i = 0; i < templateUrlCount; i++) { |
| 132 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i); | 182 TemplateUrl templateUrl = nativeGetTemplateUrlAt(mNativeTemplateUrlS erviceAndroid, i); |
| 133 if (templateUrl != null && templateUrl.mIsPrepopulated) { | 183 if (templateUrl != null) { |
| 134 templateUrls.add(templateUrl); | 184 templateUrls.add(templateUrl); |
| 135 } | 185 } |
| 136 } | 186 } |
| 137 return templateUrls; | 187 return templateUrls; |
| 138 } | 188 } |
| 139 | 189 |
| 140 /** | 190 /** |
| 141 * Called from native when template URL service is done loading. | 191 * Called from native when template URL service is done loading. |
| 142 */ | 192 */ |
| 143 @CalledByNative | 193 @CalledByNative |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 String query); | 366 String query); |
| 317 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid, | 367 private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrl ServiceAndroid, |
| 318 String query); | 368 String query); |
| 319 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid, | 369 private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlSe rviceAndroid, |
| 320 String query, String currentUrl); | 370 String query, String currentUrl); |
| 321 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid, | 371 private native String nativeGetUrlForContextualSearchQuery(long nativeTempla teUrlServiceAndroid, |
| 322 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion); | 372 String query, String alternateTerm, boolean shouldPrefetch, String p rotocolVersion); |
| 323 private native String nativeGetSearchEngineUrlFromTemplateUrl( | 373 private native String nativeGetSearchEngineUrlFromTemplateUrl( |
| 324 long nativeTemplateUrlServiceAndroid, int index); | 374 long nativeTemplateUrlServiceAndroid, int index); |
| 325 } | 375 } |
| OLD | NEW |