Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: mobile/android/thirdparty/org/adblockplus/browser/WhitelistedWebsitesPreferenceGroup.java

Issue 29543774: Issue 2801 - Create 'Whitelisted websites' screen and add link to 'Ad blocking' screen (Closed)
Patch Set: Created Sept. 13, 2017, 5:01 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mobile/android/thirdparty/org/adblockplus/browser/WhitelistedWebsitesPreferenceGroup.java
===================================================================
new file mode 100644
--- /dev/null
+++ b/mobile/android/thirdparty/org/adblockplus/browser/WhitelistedWebsitesPreferenceGroup.java
@@ -0,0 +1,246 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.browser;
+
+import android.app.AlertDialog;
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.preference.DialogPreference;
+import android.preference.PreferenceGroup;
+import android.text.Html;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.View;
+
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.Tab;
+import org.mozilla.gecko.Tabs;
+import org.mozilla.gecko.util.NativeJSObject;
+import org.mozilla.gecko.util.ThreadUtils;
+
+import java.util.Set;
+import java.util.TreeSet;
+
+public class WhitelistedWebsitesPreferenceGroup extends PreferenceGroup implements
+ AdblockPlusApiCallback, InputValidatorDialogPreference.OnInputReadyListener
+{
+
+ private static final String TAG = WhitelistedWebsitesPreferenceGroup.class.getSimpleName();
+
+ private final Set<String> whitelistedWebsites = new TreeSet<>();
+ private ProgressDialog progressDialog;
+ private final WhitelistCallback whitelistCallback = new WhitelistCallback();
+
+ public WhitelistedWebsitesPreferenceGroup(Context context, AttributeSet attrs)
+ {
+ super(context, attrs);
+ this.init();
+ }
+
+ public WhitelistedWebsitesPreferenceGroup(Context context, AttributeSet attrs, int defStyleAttr)
+ {
+ super(context, attrs, defStyleAttr);
+ this.init();
+ }
+
+ private void init()
+ {
+ this.setLayoutResource(R.layout.abb_minimal_widget);
+ }
+
+ @Override
+ protected void onAttachedToActivity()
+ {
+ super.onAttachedToActivity();
+ this.showDialog();
+ AddOnBridge.queryWhitelistedWebsites(this);
+ }
+
+ @Override
+ public void onApiRequestSucceeded(NativeJSObject jsObject)
+ {
+ this.dismissDialog();
+ this.initEntries(jsObject);
+ }
+
+ @Override
+ public void onApiRequestFailed(String errorMessage)
+ {
+ Log.w(TAG, "queryWhitelistedWebsites failed: " + errorMessage);
+ this.dismissDialog();
+ }
+
+ @Override
+ public void onInputReady(String input)
+ {
+ this.whitelistedWebsites.add(UrlUtils.getHostFromUrl(input));
+ AddOnBridge.whitelistWebsite(this.whitelistCallback, input, true);
+ this.refreshEntries();
+ }
+
+ private void initEntries(NativeJSObject jsObject)
+ {
+ this.whitelistedWebsites.clear();
+
+ final NativeJSObject[] whitelistedWebsites = jsObject.getObjectArray("value");
+ for (int i = 0; i < whitelistedWebsites.length; i++)
+ {
+ final String url = whitelistedWebsites[i].getString("url");
+ this.whitelistedWebsites.add(url);
+ }
+
+ this.refreshEntries();
+ }
+
+ private void refreshEntries()
+ {
+ this.removeAll();
+ for (final String url : this.whitelistedWebsites)
+ {
+ final WhitelistedWebsitePreference whitelistedWebsitePreference =
+ new WhitelistedWebsitePreference(this.getContext(), url, new DialogInterface.OnClickListener()
+ {
+ @Override
+ public void onClick(DialogInterface dialog, int which)
+ {
+ AddOnBridge.whitelistWebsite(WhitelistedWebsitesPreferenceGroup.this.whitelistCallback, url, false);
+ WhitelistedWebsitesPreferenceGroup.this.whitelistedWebsites.remove(url);
+ WhitelistedWebsitesPreferenceGroup.this.refreshEntries();
+ }
+ });
+ this.addPreference(whitelistedWebsitePreference);
+ }
+
+ final InputValidatorDialogPreference inputPreference = new InputValidatorDialogPreference(
+ this.getContext());
+ inputPreference.setValidationType(InputValidatorDialogPreference.ValidationType.DOMAIN);
+ inputPreference.setTitle(R.string.abb_whitelisted_websites_add_button);
+ inputPreference.setDialogTitle(R.string.abb_whitelist_website_dialog_title);
+ inputPreference.setDialogMessage(R.string.abb_whitelist_website_dialog_message);
+ inputPreference.getEditText().setHint(R.string.abb_whitelist_website_dialog_hint);
+ inputPreference.setOnInputReadyListener(this);
+ this.addPreference(inputPreference);
+ }
+
+ private void showDialog()
+ {
+ this.dismissDialog();
+ this.progressDialog = new ProgressDialog(getContext());
+ this.progressDialog.setMessage(this.getContext().getString(R.string.abb_whitelisted_websites_loading));
+ this.progressDialog.show();
+ }
+
+ private void dismissDialog()
+ {
+ if (this.progressDialog != null && this.progressDialog.isShowing())
+ {
+ this.progressDialog.dismiss();
+ this.progressDialog = null;
+ }
+ }
+
+ private static class WhitelistedWebsitePreference extends DialogPreference
+ {
+ private final DialogInterface.OnClickListener onDeleteClickListener;
+
+ WhitelistedWebsitePreference(Context context, String url,
+ DialogInterface.OnClickListener onDeleteClickListener)
+ {
+ super(context, null);
+ this.onDeleteClickListener = onDeleteClickListener;
+ final String message = context.getString(R.string.abb_whitelist_remove_dialog_message, url);
+ this.setWidgetLayoutResource(R.layout.abb_whitelisted_website_delete_widget);
+ this.setTitle(url);
+ this.setDialogTitle(R.string.abb_whitelist_remove_dialog_title);
+ this.setDialogMessage(Html.fromHtml(message));
+ this.setNegativeButtonText(android.R.string.cancel);
+ }
+
+ @Override
+ protected void onBindView(View view)
+ {
+ super.onBindView(view);
+ final View deleteButton = view.findViewById(R.id.abb_whitelisted_website_delete_button);
+ deleteButton.setOnClickListener(new View.OnClickListener()
+ {
+ @Override
+ public void onClick(View v)
+ {
+ if (WhitelistedWebsitePreference.this.getDialog() == null
+ || !WhitelistedWebsitePreference.this.getDialog().isShowing())
+ {
+ WhitelistedWebsitePreference.this.showDialog(null);
+ }
+ }
+ });
+ }
+
+ @Override
+ protected void onClick()
+ {
+ // Overriding the default behaviour of showing a dialog here
+ // We just want to show a dialog when the delete button is clicked
+ }
+
+ @Override
+ protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
+ {
+ builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
+ {
+ @Override
+ public void onClick(DialogInterface dialog, int which)
+ {
+ if (WhitelistedWebsitePreference.this.onDeleteClickListener != null)
+ {
+ WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dialog, which);
+ }
+ }
+ });
+ }
+ }
+
+ private static class WhitelistCallback implements AdblockPlusApiCallback
+ {
+
+ private final Runnable reloadTabsRunnable = new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ final Tab selectedTab = Tabs.getInstance().getSelectedTab();
+ if (selectedTab != null)
+ {
+ selectedTab.doReload(false);
+ }
+ }
+ };
+
+ @Override
+ public void onApiRequestSucceeded(NativeJSObject jsObject)
+ {
+ ThreadUtils.postToUiThread(this.reloadTabsRunnable);
+ }
+
+ @Override
+ public void onApiRequestFailed(String errorMessage)
+ {
+ Log.w(TAG, "whitelistWebsite failed: " + errorMessage);
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld