| Index: src/org/adblockplus/sbrowser/contentblocker/WhitelistedWebsitesPreferenceCategory.java |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/src/org/adblockplus/sbrowser/contentblocker/WhitelistedWebsitesPreferenceCategory.java |
| @@ -0,0 +1,194 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-2016 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.sbrowser.contentblocker; |
| + |
| +import android.app.AlertDialog; |
| +import android.content.Context; |
| +import android.content.DialogInterface; |
| +import android.content.SharedPreferences; |
| +import android.preference.DialogPreference; |
| +import android.preference.PreferenceCategory; |
| +import android.preference.PreferenceManager; |
| +import android.text.Html; |
| +import android.util.AttributeSet; |
| +import android.view.View; |
| + |
| +import org.adblockplus.adblockplussbrowser.R; |
| +import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
| +import org.adblockplus.sbrowser.contentblocker.engine.EngineService; |
| + |
| +import java.util.Collections; |
| +import java.util.Set; |
| +import java.util.TreeSet; |
| + |
| +public class WhitelistedWebsitesPreferenceCategory extends PreferenceCategory |
| +{ |
| + |
|
anton
2017/03/06 09:36:32
is empty line required?
diegocarloslima
2017/03/09 20:15:04
Not actually. Will fix that
|
| + private final Set<String> whitelistedWebsites = new TreeSet<>(); |
| + private Engine engine; |
| + |
| + public WhitelistedWebsitesPreferenceCategory(Context context, AttributeSet attrs) |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
Felix Dahlke
2017/03/07 15:43:38
For Java code it's 100, see: https://developer.moz
anton
2017/03/09 09:57:40
no problem
|
| + { |
| + super(context, attrs); |
| + // This is required to remove the title TextView of the PreferenceCategory |
| + this.setLayoutResource(R.layout.empty_view); |
| + } |
| + |
| + @Override |
| + protected void onAttachedToActivity() |
| + { |
| + super.onAttachedToActivity(); |
| + EngineService.startService(this.getContext().getApplicationContext(), |
| + new EngineService.OnEngineCreatedCallback() |
| + { |
| + @Override |
| + public void onEngineCreated(Engine engine, boolean success) |
| + { |
| + if (!success) |
| + { |
| + return; |
| + } |
| + WhitelistedWebsitesPreferenceCategory.this.engine = engine; |
| + WhitelistedWebsitesPreferenceCategory.this.initEntries(); |
| + } |
| + }); |
| + } |
| + |
| + private void initEntries() |
| + { |
| + final SharedPreferences prefs = |
| + PreferenceManager.getDefaultSharedPreferences(this.getContext().getApplicationContext()); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + final String key = this.getContext().getString(R.string.key_whitelisted_websites); |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + this.whitelistedWebsites.clear(); |
| + this.whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>emptySet())); |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + this.refreshEntries(); |
| + } |
| + |
| + private void refreshEntries() |
| + { |
| + this.removeAll(); |
| + for (final String url : this.whitelistedWebsites) |
| + { |
| + final WhitelistedWebsitePreference whitelistedWebsitePreference = |
| + new WhitelistedWebsitePreference(this.getContext(), url, new DialogInterface.OnClickListener() |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + { |
| + @Override |
| + public void onClick(DialogInterface dialog, int which) |
| + { |
| + removeWhitelistedWebsite(url); |
| + } |
| + }); |
| + this.addPreference(whitelistedWebsitePreference); |
| + } |
| + |
| + final UrlInputOpenerPreference urlPreference = new UrlInputOpenerPreference(this.getContext()); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + urlPreference.setTitle(R.string.whitelisted_websites_add_button); |
| + urlPreference.setDialogTitle(R.string.whitelist_website_dialog_title); |
| + urlPreference.setDialogMessage(R.string.whitelist_website_dialog_message); |
| + urlPreference.getEditText().setHint(R.string.whitelist_website_dialog_hint); |
| + urlPreference.setOnUrlReadyListener(new UrlInputOpenerPreference.OnUrlReadyListener() |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + { |
| + @Override |
| + public void onUrlReady(String url) |
| + { |
| + WhitelistedWebsitesPreferenceCategory.this.whitelistWebsite(url); |
| + } |
| + }); |
| + this.addPreference(urlPreference); |
| + } |
| + |
| + private void whitelistWebsite(String url) |
| + { |
| + this.whitelistedWebsites.add(url); |
| + final SharedPreferences prefs = |
| + PreferenceManager.getDefaultSharedPreferences(this.getContext().getApplicationContext()); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + final String key = this.getContext().getString(R.string.key_whitelisted_websites); |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); |
| + this.refreshEntries(); |
| + this.engine.requestUpdateBroadcast(); |
| + } |
| + |
| + private void removeWhitelistedWebsite(String url) |
| + { |
| + this.whitelistedWebsites.remove(url); |
| + final SharedPreferences prefs = |
| + PreferenceManager.getDefaultSharedPreferences(this.getContext().getApplicationContext()); |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + final String key = this.getContext().getString(R.string.key_whitelisted_websites); |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); |
| + this.refreshEntries(); |
| + this.engine.requestUpdateBroadcast(); |
| + } |
| + |
| + private static class WhitelistedWebsitePreference extends DialogPreference |
| + { |
| + private final DialogInterface.OnClickListener onDeleteClickListener; |
| + |
| + WhitelistedWebsitePreference(Context context, String url, DialogInterface.OnClickListener onDeleteClickListener) |
|
anton
2017/03/09 09:57:40
exceeds 100 characters line limit (not too signifi
|
| + { |
| + super(context); |
| + this.onDeleteClickListener = onDeleteClickListener; |
| + final String message = context.getString(R.string.whitelist_remove_dialog_message, url); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + setWidgetLayoutResource(R.layout.whitelisted_website_delete_widget); |
| + setTitle(url); |
| + setDialogTitle(R.string.whitelist_remove_dialog_title); |
| + setDialogMessage(Html.fromHtml(message)); |
| + setNegativeButtonText(android.R.string.cancel); |
| + } |
| + |
| + @Override |
| + protected void onBindView(View view) |
| + { |
| + super.onBindView(view); |
| + final View deleteButton = view.findViewById(R.id.whitelisted_website_delete_button); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + deleteButton.setOnClickListener(new View.OnClickListener() |
| + { |
| + @Override |
| + public void onClick(View v) |
| + { |
| + if (getDialog() == null || !getDialog().isShowing()) |
| + { |
| + 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() |
|
anton
2017/03/06 09:36:31
please make sure it suits into line length (see ou
|
| + { |
| + @Override |
| + public void onClick(DialogInterface dialog, int which) |
| + { |
| + if (WhitelistedWebsitePreference.this.onDeleteClickListener != null) |
| + { |
| + WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dialog, which); |
|
anton
2017/03/06 09:36:32
please make sure it suits into line length (see ou
|
| + } |
| + } |
| + }); |
| + } |
| + } |
| +} |