From 4945fc7b89b2bac3a651c5f3f34ab6ea86dfc151 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Mon, 29 Jun 2020 21:01:57 +0200 Subject: [PATCH] Added setting to disable autocomplete (#3551) Do not get search suggestions in private mode! Completion fixes --- .../vrbrowser/browser/SettingsStore.java | 11 +++++++++ .../vrbrowser/search/SearchEngineWrapper.java | 22 ++++++++++++++---- ...nsCLient.kt => SearchSuggestionsClient.kt} | 11 ++++++++- .../suggestions/SuggestionsProvider.java | 2 +- .../ui/widgets/NavigationBarWidget.java | 2 +- .../widgets/settings/PrivacyOptionsView.java | 23 +++++++++++++++++-- app/src/main/res/layout/options_privacy.xml | 6 +++++ app/src/main/res/values/non_L10n.xml | 1 + app/src/main/res/values/strings.xml | 4 ++++ 9 files changed, 73 insertions(+), 9 deletions(-) rename app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/{SearchSuggestionsCLient.kt => SearchSuggestionsClient.kt} (51%) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java b/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java index a38b327b8..65eab0cc4 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/browser/SettingsStore.java @@ -106,6 +106,7 @@ SettingsStore getInstance(final @NonNull Context aContext) { public final static boolean MULTI_E10S = false; public final static int DOWNLOADS_STORAGE_DEFAULT = INTERNAL; public final static int DOWNLOADS_SORTING_ORDER_DEFAULT = SortingContextMenuWidget.SORT_DATE_ASC; + public final static boolean AUTOCOMPLETE_ENABLED = true; // Enable telemetry by default (opt-out). public final static boolean CRASH_REPORTING_DEFAULT = false; @@ -795,4 +796,14 @@ public String getRemotePropsVersionName() { return mPrefs.getString(mContext.getString(R.string.settings_key_remote_props_version_name), "0"); } + public void setAutocompleteEnabled(boolean isEnabled) { + SharedPreferences.Editor editor = mPrefs.edit(); + editor.putBoolean(mContext.getString(R.string.settings_key_autocomplete), isEnabled); + editor.commit(); + } + + public boolean isAutocompleteEnabled() { + return mPrefs.getBoolean(mContext.getString(R.string.settings_key_autocomplete), AUTOCOMPLETE_ENABLED); + } + } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/search/SearchEngineWrapper.java b/app/src/common/shared/org/mozilla/vrbrowser/search/SearchEngineWrapper.java index 7e54447a5..92aa01cb5 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/search/SearchEngineWrapper.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/search/SearchEngineWrapper.java @@ -12,9 +12,12 @@ import androidx.annotation.NonNull; import org.mozilla.vrbrowser.R; +import org.mozilla.vrbrowser.VRBrowserActivity; import org.mozilla.vrbrowser.browser.SettingsStore; +import org.mozilla.vrbrowser.browser.engine.Session; +import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.geolocation.GeolocationData; -import org.mozilla.vrbrowser.search.suggestions.SearchSuggestionsCLientKt; +import org.mozilla.vrbrowser.search.suggestions.SearchSuggestionsClientKt; import org.mozilla.vrbrowser.utils.SystemUtils; import java.util.ArrayList; @@ -64,10 +67,12 @@ SearchEngineWrapper get(final @NonNull Context aContext) { private SearchEngine mSearchEngine; private SearchSuggestionClient mSuggestionsClient; private SharedPreferences mPrefs; + private boolean mAutocompleteEnabled; private SearchEngineWrapper(@NonNull Context aContext) { mContext = aContext; mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); + mAutocompleteEnabled = SettingsStore.getInstance(mContext).isAutocompleteEnabled(); setupSearchEngine(aContext, EMPTY); } @@ -100,8 +105,7 @@ public String getSearchURL(String aQuery) { } public CompletableFuture> getSuggestions(String aQuery) { - String query = mSearchEngine.buildSuggestionsURL(aQuery); - return SearchSuggestionsCLientKt.getSuggestionsAsync(mSuggestionsClient, query != null ? query : ""); + return SearchSuggestionsClientKt.getSuggestionsAsync(mSuggestionsClient, aQuery != null ? aQuery : ""); } public String getResourceURL() { @@ -167,7 +171,14 @@ private void setupSearchEngine(@NonNull Context aContext, String userPref) { // A name can be used if the user get's to choose among the available engines mSearchEngine = mSearchEngineManager.getDefaultSearchEngine(aContext, userPref); - mSuggestionsClient = new SearchSuggestionClient(mSearchEngine, (s, continuation) -> null); + mSuggestionsClient = new SearchSuggestionClient( + mSearchEngine, + (searchUrl, continuation) -> { + return (mAutocompleteEnabled && !((VRBrowserActivity)mContext).getWindows().isInPrivateMode()) ? + SearchSuggestionsClientKt.fetchSearchSuggestions(mContext, searchUrl) : + null; + } + ); } private String getEngine(String aCountryCode) { @@ -181,6 +192,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin if (mContext != null) { if (key.equals(mContext.getString(R.string.settings_key_geolocation_data))) { setupSearchEngine(mContext, EMPTY); + + } else if (key.equals(mContext.getString(R.string.settings_key_autocomplete))) { + mAutocompleteEnabled = SettingsStore.getInstance(mContext).isAutocompleteEnabled(); } } } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsCLient.kt b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsClient.kt similarity index 51% rename from app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsCLient.kt rename to app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsClient.kt index aba557de6..dc723a71e 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsCLient.kt +++ b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SearchSuggestionsClient.kt @@ -1,11 +1,20 @@ package org.mozilla.vrbrowser.search.suggestions +import android.content.Context import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.future.future import mozilla.components.browser.search.suggestions.SearchSuggestionClient +import mozilla.components.concept.fetch.Request +import org.mozilla.vrbrowser.browser.engine.EngineProvider +import java.nio.charset.StandardCharsets import java.util.concurrent.CompletableFuture fun getSuggestionsAsync(client: SearchSuggestionClient, query: String): CompletableFuture?> = GlobalScope.future { client.getSuggestions(query) - } \ No newline at end of file + } + +fun fetchSearchSuggestions(context: Context, searchUrl: String): String? { + val request = Request(searchUrl); + return EngineProvider.getDefaultClient(context).fetch(request).body.string(StandardCharsets.UTF_8) +} \ No newline at end of file diff --git a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java index ed938d0b2..24a3ad415 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/search/suggestions/SuggestionsProvider.java @@ -134,7 +134,7 @@ private CompletableFuture> getSearchEngineSuggestions(@NonN CompletableFuture> future = new CompletableFuture<>(); // Completion from browser-domains - if (!mText.equals(mFilterText)) { + if (!mText.equals(mFilterText) && UrlUtils.isDomain(mText)) { items.add(SuggestionItem.create( mText, getSearchURLOrDomain(mText), diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java index f3f9aac3e..bad5ac67c 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java @@ -989,7 +989,7 @@ public void onShowAwesomeBar() { .whenCompleteAsync((items, ex) -> { if (mBinding.navigationBarNavigation.urlBar.hasFocus()) { mAwesomeBar.updateItems(items); - mAwesomeBar.setHighlightedText(originalText); + mAwesomeBar.setHighlightedText(mBinding.navigationBarNavigation.urlBar.getOriginalText().trim()); if (!mAwesomeBar.isVisible()) { mAwesomeBar.updatePlacement((int) WidgetPlacement.convertPixelsToDp(getContext(), mBinding.navigationBarNavigation.urlBar.getWidth())); diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/PrivacyOptionsView.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/PrivacyOptionsView.java index e28fe22e9..a6f4ad684 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/PrivacyOptionsView.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/settings/PrivacyOptionsView.java @@ -22,10 +22,8 @@ import org.mozilla.geckoview.StorageController; import org.mozilla.vrbrowser.R; import org.mozilla.vrbrowser.browser.SettingsStore; -import org.mozilla.vrbrowser.browser.engine.SessionState; import org.mozilla.vrbrowser.browser.engine.SessionStore; import org.mozilla.vrbrowser.databinding.OptionsPrivacyBinding; -import org.mozilla.vrbrowser.db.SitePermission; import org.mozilla.vrbrowser.ui.views.settings.RadioGroupSetting; import org.mozilla.vrbrowser.ui.views.settings.SwitchSetting; import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate; @@ -132,6 +130,9 @@ protected void updateUI() { mBinding.restoreTabsSwitch.setOnCheckedChangeListener(mRestoreTabsListener); setRestoreTabs(SettingsStore.getInstance(getContext()).isRestoreTabsEnabled(), false); + mBinding.autocompleteSwitch.setOnCheckedChangeListener(mAutocompleteListener); + setAutocomplete(SettingsStore.getInstance(getContext()).isAutocompleteEnabled(), false); + mBinding.webxrSwitch.setOnCheckedChangeListener(mWebXRListener); setWebXR(SettingsStore.getInstance(getContext()).isWebXREnabled(), false); mBinding.webxrExceptionsButton.setOnClickListener(v -> mDelegate.showView(SettingViewType.WEBXR_EXCEPTIONS)); @@ -202,6 +203,10 @@ public void reject() { setRestoreTabs(value, doApply); }; + private SwitchSetting.OnCheckedChangeListener mAutocompleteListener = (compoundButton, value, doApply) -> { + setAutocomplete(value, doApply); + }; + private SwitchSetting.OnCheckedChangeListener mWebXRListener = (compoundButton, value, doApply) -> { setWebXR(value, doApply); }; @@ -243,6 +248,10 @@ private void resetOptions() { setRestoreTabs(SettingsStore.RESTORE_TABS_ENABLED, true); } + if (mBinding.autocompleteSwitch.isChecked() != SettingsStore.AUTOCOMPLETE_ENABLED) { + setAutocomplete(SettingsStore.AUTOCOMPLETE_ENABLED, true); + } + if (mBinding.webxrSwitch.isChecked() != SettingsStore.WEBXR_ENABLED_DEFAULT) { setWebXR(SettingsStore.WEBXR_ENABLED_DEFAULT, true); } @@ -332,6 +341,16 @@ private void setRestoreTabs(boolean value, boolean doApply) { } } + private void setAutocomplete(boolean value, boolean doApply) { + mBinding.autocompleteSwitch.setOnCheckedChangeListener(null); + mBinding.autocompleteSwitch.setValue(value, false); + mBinding.autocompleteSwitch.setOnCheckedChangeListener(mAutocompleteListener); + + if (doApply) { + SettingsStore.getInstance(getContext()).setAutocompleteEnabled(value); + } + } + private void setWebXR(boolean value, boolean doApply) { mBinding.webxrSwitch.setOnCheckedChangeListener(null); mBinding.webxrSwitch.setValue(value, false); diff --git a/app/src/main/res/layout/options_privacy.xml b/app/src/main/res/layout/options_privacy.xml index d213df8dd..60ef4ec5a 100644 --- a/app/src/main/res/layout/options_privacy.xml +++ b/app/src/main/res/layout/options_privacy.xml @@ -49,6 +49,12 @@ app:buttonText="@string/developer_options_show" app:description="@string/settings_privacy_policy" /> + + settings_key_downloads_sorting_order settings_key_remote_props_version_name settings_key_remote_props + settings_key_autocomplete https://github.com/MozillaReality/FirefoxReality/wiki/Environments https://www.mozilla.org/privacy/firefox/ https://mixedreality.mozilla.org/fxr/report?src=browser-fxr&label=browser-firefox-reality&url=%1$s diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5da33ecd9..817e0c32c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -642,6 +642,10 @@ and is used to enable or disable the tabs restoration after a fresh app start. --> Restore tabs and windows after restart + + Address Bar Auto-complete + Tracking Protection