Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Update search codes (#3283)
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo authored Apr 30, 2020
1 parent c9f36b7 commit db2cadb
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public class GeolocationLocalizationProvider implements SearchLocalizationProvid
mRegion = data.getCountryCode();
}

GeolocationLocalizationProvider(@NonNull String countryCode, @NonNull String region) {
mCountry = countryCode;
mLanguage = Locale.getDefault().getLanguage();
mRegion = region;
}

@Nullable
@Override
public SearchLocalization determineRegion(@NonNull Continuation<? super SearchLocalization> continuation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@
import androidx.annotation.NonNull;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.VRBrowserApplication;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.EngineProvider;
import org.mozilla.vrbrowser.geolocation.GeolocationData;
import org.mozilla.vrbrowser.search.suggestions.SuggestionsClient;
import org.mozilla.vrbrowser.search.suggestions.SearchSuggestionsCLientKt;
import org.mozilla.vrbrowser.utils.SystemUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import kotlinx.coroutines.Dispatchers;
import mozilla.components.browser.search.SearchEngine;
Expand Down Expand Up @@ -64,22 +60,14 @@ SearchEngineWrapper get(final @NonNull Context aContext) {
return mSearchEngineWrapperInstance;
}

public interface SuggestionsDelegate {
void OnSuggestions(List<String> aSuggestionsList);
}

private Context mContext;
private SearchEngine mSearchEngine;
private SearchLocalizationProvider mLocalizationProvider;
private SearchEngineManager mSearchEngineManager;
private SearchSuggestionClient mSuggestionsClient;
private SharedPreferences mPrefs;
private Executor mUIThreadExecutor;

private SearchEngineWrapper(@NonNull Context aContext) {
mContext = aContext;
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
mUIThreadExecutor = ((VRBrowserApplication)aContext.getApplicationContext()).getExecutors().mainThread();

setupSearchEngine(aContext, EMPTY);
}
Expand Down Expand Up @@ -111,21 +99,9 @@ public String getSearchURL(String aQuery) {
return mSearchEngine.buildSearchUrl(aQuery);
}

private String getSuggestionURL(String aQuery) {
return mSearchEngine.buildSuggestionsURL(aQuery);
}

public CompletableFuture<List<String>> getSuggestions(String aQuery) {
CompletableFuture<List<String>> future = new CompletableFuture<>();
// TODO: Use mSuggestionsClient.getSuggestions when fixed in browser-search.
String query = getSuggestionURL(aQuery);
mUIThreadExecutor.execute(() ->
SuggestionsClient.getSuggestions(
EngineProvider.INSTANCE.getDefaultGeckoWebExecutor(mContext),
mSearchEngine,
query).thenAcceptAsync(future::complete));

return future;
String query = mSearchEngine.buildSuggestionsURL(aQuery);
return SearchSuggestionsCLientKt.getSuggestionsAsync(mSuggestionsClient, query != null ? query : "");
}

public String getResourceURL() {
Expand Down Expand Up @@ -157,6 +133,7 @@ private void setupSearchEngine(@NonNull Context aContext, String userPref) {
List<SearchEngineFilter> engineFilterList = new ArrayList<>();

GeolocationData data = GeolocationData.parse(SettingsStore.getInstance(aContext).getGeolocationData());
SearchLocalizationProvider mLocalizationProvider;
if (data == null) {
Log.d(LOGTAG, "Using Locale based search localization provider");
// If we don't have geolocation data we default to the Locale search localization provider
Expand All @@ -181,7 +158,7 @@ private void setupSearchEngine(@NonNull Context aContext, String userPref) {
engineFilterList,
Collections.emptyList());

mSearchEngineManager = new SearchEngineManager(Arrays.asList(engineProvider), Dispatchers.getDefault());
SearchEngineManager mSearchEngineManager = new SearchEngineManager(Collections.singletonList(engineProvider), Dispatchers.getDefault());

// If we don't get any result we use the default configuration.
if (mSearchEngineManager.getSearchEngines(aContext).size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mozilla.vrbrowser.search.suggestions

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.future
import mozilla.components.browser.search.suggestions.SearchSuggestionClient
import java.util.concurrent.CompletableFuture

fun getSuggestionsAsync(client: SearchSuggestionClient, query: String): CompletableFuture<List<String>?> =
GlobalScope.future {
client.getSuggestions(query)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,37 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;

public class SuggestionsProvider {

private static final String LOGTAG = SuggestionsProvider.class.getSimpleName();

public class DefaultSuggestionsComparator implements Comparator {
public static class DefaultSuggestionsComparator implements Comparator<SuggestionItem> {

public int compare(Object obj1, Object obj2) {
SuggestionItem suggestion1 = (SuggestionItem)obj1;
SuggestionItem suggestion2 = (SuggestionItem)obj2;
if (suggestion1.type == Type.SUGGESTION && suggestion2.type == Type.SUGGESTION) {
public int compare(SuggestionItem obj1, SuggestionItem obj2) {
if (obj1.type == Type.SUGGESTION && obj2.type == Type.SUGGESTION) {
return 0;

} else if (suggestion1.type == suggestion2.type) {
if (suggestion1.type == Type.HISTORY) {
if (suggestion1.score != suggestion2.score) {
return suggestion1.score - suggestion2.score;
} else if (obj1.type == obj2.type) {
if (obj1.type == Type.HISTORY) {
if (obj1.score != obj2.score) {
return obj1.score - obj2.score;
}
}

return suggestion1.url.compareTo(suggestion2.url);
return obj1.url.compareTo(obj2.url);

} else {
return suggestion1.type.ordinal() - suggestion2.type.ordinal();
return obj1.type.ordinal() - obj2.type.ordinal();
}
}
}

private SearchEngineWrapper mSearchEngineWrapper;
private String mText;
private String mFilterText;
private Comparator mComparator;
private Comparator<SuggestionItem> mComparator;
private Executor mUIThreadExecutor;

public SuggestionsProvider(Context context) {
Expand All @@ -74,15 +73,15 @@ public void setFilterText(String text) {

public void setText(String text) { mText = text; }

public void setComparator(Comparator comparator) {
public void setComparator(Comparator<SuggestionItem> comparator) {
mComparator = comparator;
}

private CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();
CompletableFuture<List<SuggestionItem>> future = new CompletableFuture<>();
SessionStore.get().getBookmarkStore().searchBookmarks(mFilterText, 100).thenAcceptAsync((bookmarks) -> {
bookmarks.stream()
.filter((b) -> !b.getUrl().startsWith("place:") &&
.filter((b) -> b.getUrl() != null && !b.getUrl().startsWith("place:") &&
!b.getUrl().startsWith("about:reader"))
.forEach(b -> items.add(SuggestionItem.create(
b.getTitle(),
Expand All @@ -107,7 +106,7 @@ private CompletableFuture<List<SuggestionItem>> getBookmarkSuggestions(@NonNull
}

private CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull final List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();
CompletableFuture<List<SuggestionItem>> future = new CompletableFuture<>();
SessionStore.get().getHistoryStore().getSuggestions(mFilterText, 100).thenAcceptAsync((history) -> {
history.forEach(h -> items.add(SuggestionItem.create(
h.getTitle(),
Expand All @@ -132,7 +131,7 @@ private CompletableFuture<List<SuggestionItem>> getHistorySuggestions(@NonNull f
}

private CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonNull final List<SuggestionItem> items) {
CompletableFuture future = new CompletableFuture();
CompletableFuture<List<SuggestionItem>> future = new CompletableFuture<>();

// Completion from browser-domains
if (!mText.equals(mFilterText)) {
Expand Down Expand Up @@ -182,7 +181,7 @@ private CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonN
}

public CompletableFuture<List<SuggestionItem>> getSuggestions() {
return CompletableFuture.supplyAsync(() -> new ArrayList<SuggestionItem>())
return CompletableFuture.supplyAsync((Supplier<ArrayList<SuggestionItem>>) ArrayList::new)
.thenComposeAsync(this::getSearchEngineSuggestions)
.thenComposeAsync(this::getBookmarkSuggestions)
.thenComposeAsync(this::getHistorySuggestions);
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/assets/searchplugins/google-b-1-m.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
<ShortName>Google</ShortName>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAMAAADVRocKAAAB71BMVEUAAAD29vb29vb29vb39/f////39/f19fXqQzU0qFNChfT7vAX09PRjmvSVufXqSDr19PTywr41qVRVtnDrT0Lv8/D06unviYHufnQ7q1nrW07rV0rqRTZ4p/Xz9PPj7ub36sTvlo93w4xywYdnvH5euXZIsGQ/rVz6xSr7wRj6vQrk6/XR3/WnxfVUkfXg6fSQtvR+q/RHifPq8ez04d/N5dPE4svyx8Op17bxsq2X0KaAx5Nrv4FNsWbsa2BDrl9Brl/rSz77wBPo7fW90/W4zvWwyvWHsfVclvRMjPRFh/T18O/17+718+z06ejf7OLz4+Lb6+D1793V6Nr27tbz1NHzzMhipsi938a33cLyu7al1bLxt7L25K/25K6g067wrqjxrKaOzJ/woJqFyJj435X43Iv42oT42Xxju3rugnlPs2rtcmjtcGacwWXtZ1v50FnsYVXsXFD5zU/2sEvrU0b5vDz5xzb6xzPt8PXX4vXH2PVrn/RpnfT06+tMjurf7eNTleH03dzz2tjz2NZbnday2rzxwLxnrbid0qtosqer1KXwp6HwpJ6IyZnvn5jxspdltZRdtIPueW9yu26Hvm1etmuzwVjsY1f0oE/xj0/we0vGwkjua0bsW0DqTzrXwDX5wCnkvyX6wB3wvhYYaN+hAAAAB3RSTlMA8si8ZBhlc+JuAAAAA9xJREFUaN7dmmdT2zAchw2EysYuhCSQEDYkhNFC2Xu3UEYZpWxaZhezUKB77733Hh+0oclR21L0lzA+7vq8yyX6PbEkS7IlwY8lPAzBKNgHgLBwi7BBZARHOKckItIviGAP55dECMIuI+XhH1mEcKCoQUO4EIZM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0gV0zco/t89HGT+Npf5G3WGtxp3vA38GN/u3fkNa8DAVcD0QwPmH3uw8BTl/5HF9KMn8OEZfjSHZ0w//vMHVqViODGkXAcAAAAASUVORK5CYII=</Image>
<Url type="application/x-suggestions+json" method="GET" template="https://www.google.com/complete/search?client=firefox&amp;q={searchTerms}"/>
<Url type="text/html" method="GET" template="https://www.google.com/search">
<Param name="q" value="{searchTerms}"/>
<Param name="ie" value="utf-8"/>
<Param name="oe" value="utf-8"/>
<Param name="client" value="firefox-b-1-o"/>
</Url>
<SearchForm>https://www.google.com</SearchForm>
</SearchPlugin>
Loading

0 comments on commit db2cadb

Please sign in to comment.