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

Commit

Permalink
Added setting to disable autocomplete (#3551)
Browse files Browse the repository at this point in the history
Do not get search suggestions in private mode!
Completion fixes
  • Loading branch information
keianhzo committed Jun 29, 2020
1 parent af3cbb3 commit 4945fc7
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -100,8 +105,7 @@ public String getSearchURL(String aQuery) {
}

public CompletableFuture<List<String>> 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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<String>?> =
GlobalScope.future {
client.getSuggestions(query)
}
}

fun fetchSearchSuggestions(context: Context, searchUrl: String): String? {
val request = Request(searchUrl);
return EngineProvider.getDefaultClient(context).fetch(request).body.string(StandardCharsets.UTF_8)
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private CompletableFuture<List<SuggestionItem>> getSearchEngineSuggestions(@NonN
CompletableFuture<List<SuggestionItem>> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/options_privacy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
app:buttonText="@string/developer_options_show"
app:description="@string/settings_privacy_policy" />

<org.mozilla.vrbrowser.ui.views.settings.SwitchSetting
android:id="@+id/autocompleteSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="@string/security_options_autocomplete" />

<org.mozilla.vrbrowser.ui.views.settings.SwitchSetting
android:id="@+id/drmContentPlaybackSwitch"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/non_L10n.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<string name="settings_key_downloads_sorting_order" translatable="false">settings_key_downloads_sorting_order</string>
<string name="settings_key_remote_props_version_name" translatable="false">settings_key_remote_props_version_name</string>
<string name="settings_key_remote_props" translatable="false">settings_key_remote_props</string>
<string name="settings_key_autocomplete" translatable="false">settings_key_autocomplete</string>
<string name="environment_override_help_url" translatable="false">https://github.com/MozillaReality/FirefoxReality/wiki/Environments</string>
<string name="private_policy_url" translatable="false">https://www.mozilla.org/privacy/firefox/</string>
<string name="private_report_url" translatable="false">https://mixedreality.mozilla.org/fxr/report?src=browser-fxr&amp;label=browser-firefox-reality&amp;url=%1$s</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@
and is used to enable or disable the tabs restoration after a fresh app start. -->
<string name="security_options_restore_tabs">Restore tabs and windows after restart</string>

<!-- This string labels an Allow/Don't Allow switch in the Privacy and Security settings dialog
and is used to enable or disable the address bar autocomplete. -->
<string name="security_options_autocomplete">Address Bar Auto-complete</string>

<!-- This string labels an On/Off switch in the Privacy and Security settings dialog
and is used to enable or disable tracking protection. -->
<string name="security_options_tracking_protection">Tracking Protection</string>
Expand Down

0 comments on commit 4945fc7

Please sign in to comment.