This source file includes following definitions.
- onTemplateUrlServiceLoaded
- CalledByNative
- create
- getIndex
- getShortName
- getKeyword
- getInstance
- isLoaded
- load
- getLocalizedSearchEngines
- templateUrlServiceLoaded
- getDefaultSearchEngineIndex
- getDefaultSearchEngineTemplateUrl
- setSearchEngine
- isSearchProviderManaged
- isSearchByImageAvailable
- isDefaultSearchEngineGoogle
- registerLoadListener
- unregisterLoadListener
- getUrlForSearchQuery
- getUrlForVoiceSearchQuery
- replaceSearchTermsInUrl
- nativeInit
- nativeLoad
- nativeIsLoaded
- nativeGetTemplateUrlCount
- nativeGetPrepopulatedTemplateUrlAt
- nativeSetDefaultSearchProvider
- nativeGetDefaultSearchProvider
- nativeIsSearchProviderManaged
- nativeIsSearchByImageAvailable
- nativeIsDefaultSearchEngineGoogle
- nativeGetUrlForSearchQuery
- nativeGetUrlForVoiceSearchQuery
- nativeReplaceSearchTermsInUrl
package org.chromium.chrome.browser.search_engines;
import org.chromium.base.CalledByNative;
import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import java.util.ArrayList;
import java.util.List;
public class TemplateUrlService {
public interface LoadListener {
public abstract void onTemplateUrlServiceLoaded();
}
public static class TemplateUrl {
private final int mIndex;
private final String mShortName;
private final String mKeyword;
@CalledByNative("TemplateUrl")
public static TemplateUrl create(int id, String shortName, String keyword) {
return new TemplateUrl(id, shortName, keyword);
}
public TemplateUrl(int index, String shortName, String keyword) {
mIndex = index;
mShortName = shortName;
mKeyword = keyword;
}
public int getIndex() {
return mIndex;
}
public String getShortName() {
return mShortName;
}
public String getKeyword() {
return mKeyword;
}
}
private static TemplateUrlService sService;
public static TemplateUrlService getInstance() {
ThreadUtils.assertOnUiThread();
if (sService == null) {
sService = new TemplateUrlService();
}
return sService;
}
private final long mNativeTemplateUrlServiceAndroid;
private final ObserverList<LoadListener> mLoadListeners = new ObserverList<LoadListener>();
private TemplateUrlService() {
mNativeTemplateUrlServiceAndroid = nativeInit();
}
public boolean isLoaded() {
ThreadUtils.assertOnUiThread();
return nativeIsLoaded(mNativeTemplateUrlServiceAndroid);
}
public void load() {
ThreadUtils.assertOnUiThread();
nativeLoad(mNativeTemplateUrlServiceAndroid);
}
public List<TemplateUrl> getLocalizedSearchEngines() {
ThreadUtils.assertOnUiThread();
int templateUrlCount = nativeGetTemplateUrlCount(mNativeTemplateUrlServiceAndroid);
List<TemplateUrl> templateUrls = new ArrayList<TemplateUrl>(templateUrlCount);
for (int i = 0; i < templateUrlCount; i++) {
TemplateUrl templateUrl = nativeGetPrepopulatedTemplateUrlAt(
mNativeTemplateUrlServiceAndroid, i);
if (templateUrl != null) {
templateUrls.add(templateUrl);
}
}
return templateUrls;
}
@CalledByNative
private void templateUrlServiceLoaded() {
ThreadUtils.assertOnUiThread();
for (LoadListener listener : mLoadListeners) {
listener.onTemplateUrlServiceLoaded();
}
}
public int getDefaultSearchEngineIndex() {
ThreadUtils.assertOnUiThread();
return nativeGetDefaultSearchProvider(mNativeTemplateUrlServiceAndroid);
}
public TemplateUrl getDefaultSearchEngineTemplateUrl() {
if (!isLoaded()) return null;
int defaultSearchEngineIndex = getDefaultSearchEngineIndex();
if (defaultSearchEngineIndex == -1) return null;
assert defaultSearchEngineIndex >= 0;
assert defaultSearchEngineIndex < nativeGetTemplateUrlCount(
mNativeTemplateUrlServiceAndroid);
return nativeGetPrepopulatedTemplateUrlAt(
mNativeTemplateUrlServiceAndroid, defaultSearchEngineIndex);
}
public void setSearchEngine(int selectedIndex) {
ThreadUtils.assertOnUiThread();
nativeSetDefaultSearchProvider(mNativeTemplateUrlServiceAndroid, selectedIndex);
}
public boolean isSearchProviderManaged() {
return nativeIsSearchProviderManaged(mNativeTemplateUrlServiceAndroid);
}
public boolean isSearchByImageAvailable() {
ThreadUtils.assertOnUiThread();
return nativeIsSearchByImageAvailable(mNativeTemplateUrlServiceAndroid);
}
public boolean isDefaultSearchEngineGoogle() {
return nativeIsDefaultSearchEngineGoogle(mNativeTemplateUrlServiceAndroid);
}
public void registerLoadListener(LoadListener listener) {
ThreadUtils.assertOnUiThread();
boolean added = mLoadListeners.addObserver(listener);
assert added;
}
public void unregisterLoadListener(LoadListener listener) {
ThreadUtils.assertOnUiThread();
boolean removed = mLoadListeners.removeObserver(listener);
assert removed;
}
public String getUrlForSearchQuery(String query) {
return nativeGetUrlForSearchQuery(mNativeTemplateUrlServiceAndroid, query);
}
public String getUrlForVoiceSearchQuery(String query) {
return nativeGetUrlForVoiceSearchQuery(mNativeTemplateUrlServiceAndroid, query);
}
public String replaceSearchTermsInUrl(String query, String url) {
return nativeReplaceSearchTermsInUrl(mNativeTemplateUrlServiceAndroid, query, url);
}
private native long nativeInit();
private native void nativeLoad(long nativeTemplateUrlServiceAndroid);
private native boolean nativeIsLoaded(long nativeTemplateUrlServiceAndroid);
private native int nativeGetTemplateUrlCount(long nativeTemplateUrlServiceAndroid);
private native TemplateUrl nativeGetPrepopulatedTemplateUrlAt(
long nativeTemplateUrlServiceAndroid, int i);
private native void nativeSetDefaultSearchProvider(long nativeTemplateUrlServiceAndroid,
int selectedIndex);
private native int nativeGetDefaultSearchProvider(long nativeTemplateUrlServiceAndroid);
private native boolean nativeIsSearchProviderManaged(long nativeTemplateUrlServiceAndroid);
private native boolean nativeIsSearchByImageAvailable(long nativeTemplateUrlServiceAndroid);
private native boolean nativeIsDefaultSearchEngineGoogle(long nativeTemplateUrlServiceAndroid);
private native String nativeGetUrlForSearchQuery(long nativeTemplateUrlServiceAndroid,
String query);
private native String nativeGetUrlForVoiceSearchQuery(long nativeTemplateUrlServiceAndroid,
String query);
private native String nativeReplaceSearchTermsInUrl(long nativeTemplateUrlServiceAndroid,
String query, String currentUrl);
}