OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.sbrowser.contentblocker; |
| 19 |
| 20 import android.app.AlertDialog; |
| 21 import android.content.Context; |
| 22 import android.content.DialogInterface; |
| 23 import android.content.SharedPreferences; |
| 24 import android.preference.DialogPreference; |
| 25 import android.preference.PreferenceCategory; |
| 26 import android.preference.PreferenceManager; |
| 27 import android.text.Html; |
| 28 import android.util.AttributeSet; |
| 29 import android.view.View; |
| 30 |
| 31 import org.adblockplus.adblockplussbrowser.R; |
| 32 import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
| 33 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; |
| 34 |
| 35 import java.util.Collections; |
| 36 import java.util.Set; |
| 37 import java.util.TreeSet; |
| 38 |
| 39 public class WhitelistedWebsitesPreferenceCategory extends PreferenceCategory |
| 40 { |
| 41 private final Set<String> whitelistedWebsites = new TreeSet<String>(); |
| 42 private Engine engine; |
| 43 |
| 44 public WhitelistedWebsitesPreferenceCategory(Context context, AttributeSet att
rs) |
| 45 { |
| 46 super(context, attrs); |
| 47 // This is required to remove the title TextView of the PreferenceCategory |
| 48 this.setLayoutResource(R.layout.empty_view); |
| 49 } |
| 50 |
| 51 @Override |
| 52 protected void onAttachedToActivity() |
| 53 { |
| 54 super.onAttachedToActivity(); |
| 55 EngineService.startService(this.getContext().getApplicationContext(), |
| 56 new EngineService.OnEngineCreatedCallback() |
| 57 { |
| 58 @Override |
| 59 public void onEngineCreated(Engine engine, boolean success) |
| 60 { |
| 61 if (!success) |
| 62 { |
| 63 return; |
| 64 } |
| 65 WhitelistedWebsitesPreferenceCategory.this.engine = engine; |
| 66 WhitelistedWebsitesPreferenceCategory.this.initEntries(); |
| 67 } |
| 68 }); |
| 69 } |
| 70 |
| 71 private void initEntries() |
| 72 { |
| 73 final SharedPreferences prefs = |
| 74 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli
cationContext()); |
| 75 final String key = this.getContext().getString(R.string.key_whitelisted_webs
ites); |
| 76 this.whitelistedWebsites.clear(); |
| 77 this.whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>
emptySet())); |
| 78 this.refreshEntries(); |
| 79 } |
| 80 |
| 81 private void refreshEntries() |
| 82 { |
| 83 this.removeAll(); |
| 84 for (final String url : this.whitelistedWebsites) |
| 85 { |
| 86 final WhitelistedWebsitePreference whitelistedWebsitePreference = |
| 87 new WhitelistedWebsitePreference(this.getContext(), url, new DialogInt
erface.OnClickListener() |
| 88 { |
| 89 @Override |
| 90 public void onClick(DialogInterface dialog, int which) |
| 91 { |
| 92 removeWhitelistedWebsite(url); |
| 93 } |
| 94 }); |
| 95 this.addPreference(whitelistedWebsitePreference); |
| 96 } |
| 97 |
| 98 final UrlInputOpenerPreference urlPreference = new UrlInputOpenerPreference(
this.getContext()); |
| 99 urlPreference.setTitle(R.string.whitelisted_websites_add_button); |
| 100 urlPreference.setDialogTitle(R.string.whitelist_website_dialog_title); |
| 101 urlPreference.setDialogMessage(R.string.whitelist_website_dialog_message); |
| 102 urlPreference.getEditText().setHint(R.string.whitelist_website_dialog_hint); |
| 103 urlPreference.setOnUrlReadyListener(new UrlInputOpenerPreference.OnUrlReadyL
istener() |
| 104 { |
| 105 @Override |
| 106 public void onUrlReady(String url) |
| 107 { |
| 108 WhitelistedWebsitesPreferenceCategory.this.whitelistWebsite(url); |
| 109 } |
| 110 }); |
| 111 this.addPreference(urlPreference); |
| 112 } |
| 113 |
| 114 private void whitelistWebsite(String url) |
| 115 { |
| 116 this.whitelistedWebsites.add(url); |
| 117 final SharedPreferences prefs = |
| 118 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli
cationContext()); |
| 119 final String key = this.getContext().getString(R.string.key_whitelisted_webs
ites); |
| 120 prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); |
| 121 this.refreshEntries(); |
| 122 this.engine.requestUpdateBroadcast(); |
| 123 } |
| 124 |
| 125 private void removeWhitelistedWebsite(String url) |
| 126 { |
| 127 this.whitelistedWebsites.remove(url); |
| 128 final SharedPreferences prefs = |
| 129 PreferenceManager.getDefaultSharedPreferences(this.getContext().getAppli
cationContext()); |
| 130 final String key = this.getContext().getString(R.string.key_whitelisted_webs
ites); |
| 131 prefs.edit().putStringSet(key, this.whitelistedWebsites).apply(); |
| 132 this.refreshEntries(); |
| 133 this.engine.requestUpdateBroadcast(); |
| 134 } |
| 135 |
| 136 private static class WhitelistedWebsitePreference extends DialogPreference |
| 137 { |
| 138 private final DialogInterface.OnClickListener onDeleteClickListener; |
| 139 |
| 140 WhitelistedWebsitePreference(Context context, String url, |
| 141 DialogInterface.OnClickListener onDeleteClickListener) |
| 142 { |
| 143 super(context); |
| 144 this.onDeleteClickListener = onDeleteClickListener; |
| 145 final String message = context.getString(R.string.whitelist_remove_dialog_
message, url); |
| 146 setWidgetLayoutResource(R.layout.whitelisted_website_delete_widget); |
| 147 setTitle(url); |
| 148 setDialogTitle(R.string.whitelist_remove_dialog_title); |
| 149 setDialogMessage(Html.fromHtml(message)); |
| 150 setNegativeButtonText(android.R.string.cancel); |
| 151 } |
| 152 |
| 153 @Override |
| 154 protected void onBindView(View view) |
| 155 { |
| 156 super.onBindView(view); |
| 157 final View deleteButton = view.findViewById(R.id.whitelisted_website_delet
e_button); |
| 158 deleteButton.setOnClickListener(new View.OnClickListener() |
| 159 { |
| 160 @Override |
| 161 public void onClick(View v) |
| 162 { |
| 163 if (getDialog() == null || !getDialog().isShowing()) |
| 164 { |
| 165 showDialog(null); |
| 166 } |
| 167 } |
| 168 }); |
| 169 } |
| 170 |
| 171 @Override |
| 172 protected void onClick() |
| 173 { |
| 174 // Overriding the default behaviour of showing a dialog here |
| 175 // We just want to show a dialog when the delete button is clicked |
| 176 } |
| 177 |
| 178 @Override |
| 179 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) |
| 180 { |
| 181 builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClic
kListener() |
| 182 { |
| 183 @Override |
| 184 public void onClick(DialogInterface dialog, int which) |
| 185 { |
| 186 if (WhitelistedWebsitePreference.this.onDeleteClickListener != null) |
| 187 { |
| 188 WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dial
og, which); |
| 189 } |
| 190 } |
| 191 }); |
| 192 } |
| 193 } |
| 194 } |
OLD | NEW |