OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present 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.preference.DialogPreference; |
| 24 import android.preference.PreferenceCategory; |
| 25 import android.text.Html; |
| 26 import android.util.AttributeSet; |
| 27 import android.view.View; |
| 28 |
| 29 import org.adblockplus.adblockplussbrowser.R; |
| 30 import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
| 31 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; |
| 32 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils; |
| 33 |
| 34 import java.util.Collections; |
| 35 import java.util.Set; |
| 36 import java.util.TreeSet; |
| 37 |
| 38 public class WhitelistedWebsitesPreferenceCategory extends PreferenceCategory |
| 39 { |
| 40 private final Set<String> whitelistedWebsites = new TreeSet<>(); |
| 41 private Engine engine; |
| 42 |
| 43 public WhitelistedWebsitesPreferenceCategory(Context context, AttributeSet att
rs) |
| 44 { |
| 45 super(context, attrs); |
| 46 // This is required to remove the title TextView of the PreferenceCategory |
| 47 this.setLayoutResource(R.layout.empty_view); |
| 48 } |
| 49 |
| 50 @Override |
| 51 protected void onAttachedToActivity() |
| 52 { |
| 53 super.onAttachedToActivity(); |
| 54 EngineService.startService(this.getContext().getApplicationContext(), |
| 55 new EngineService.OnEngineCreatedCallback() |
| 56 { |
| 57 @Override |
| 58 public void onEngineCreated(Engine engine, boolean success) |
| 59 { |
| 60 if (!success) |
| 61 { |
| 62 return; |
| 63 } |
| 64 WhitelistedWebsitesPreferenceCategory.this.engine = engine; |
| 65 WhitelistedWebsitesPreferenceCategory.this.initEntries(); |
| 66 } |
| 67 }); |
| 68 } |
| 69 |
| 70 private void initEntries() |
| 71 { |
| 72 final Set<String> whitelistedWebsites = SharedPrefsUtils.getStringSet( |
| 73 this.getContext(), R.string.key_whitelisted_websites, Collections.<Strin
g>emptySet()); |
| 74 |
| 75 this.whitelistedWebsites.clear(); |
| 76 this.whitelistedWebsites.addAll(whitelistedWebsites); |
| 77 this.refreshEntries(); |
| 78 } |
| 79 |
| 80 private void refreshEntries() |
| 81 { |
| 82 this.removeAll(); |
| 83 for (final String url : this.whitelistedWebsites) |
| 84 { |
| 85 final WhitelistedWebsitePreference whitelistedWebsitePreference = |
| 86 new WhitelistedWebsitePreference(this.getContext(), url, new DialogInt
erface.OnClickListener() |
| 87 { |
| 88 @Override |
| 89 public void onClick(DialogInterface dialog, int which) |
| 90 { |
| 91 removeWhitelistedWebsite(url); |
| 92 } |
| 93 }); |
| 94 this.addPreference(whitelistedWebsitePreference); |
| 95 } |
| 96 |
| 97 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo
gPreference(this.getContext()); |
| 98 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp
e.DOMAIN); |
| 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.setOnInputReadyListener(new InputValidatorDialogPreference.OnI
nputReadyListener() |
| 104 { |
| 105 @Override |
| 106 public void onInputReady(String input) |
| 107 { |
| 108 WhitelistedWebsitesPreferenceCategory.this.whitelistWebsite(input); |
| 109 } |
| 110 }); |
| 111 this.addPreference(urlPreference); |
| 112 } |
| 113 |
| 114 private void whitelistWebsite(String url) |
| 115 { |
| 116 this.whitelistedWebsites.add(url); |
| 117 this.storeWhitelistedWebsites(); |
| 118 } |
| 119 |
| 120 private void removeWhitelistedWebsite(String url) |
| 121 { |
| 122 this.whitelistedWebsites.remove(url); |
| 123 this.storeWhitelistedWebsites(); |
| 124 } |
| 125 |
| 126 private void storeWhitelistedWebsites() { |
| 127 SharedPrefsUtils.putStringSet( |
| 128 this.getContext(), R.string.key_whitelisted_websites, this.whitelistedWe
bsites); |
| 129 this.refreshEntries(); |
| 130 this.engine.requestUpdateBroadcast(); |
| 131 } |
| 132 |
| 133 private static class WhitelistedWebsitePreference extends DialogPreference |
| 134 { |
| 135 private final DialogInterface.OnClickListener onDeleteClickListener; |
| 136 |
| 137 WhitelistedWebsitePreference(Context context, String url, |
| 138 DialogInterface.OnClickListener onDeleteClickListener) |
| 139 { |
| 140 super(context); |
| 141 this.onDeleteClickListener = onDeleteClickListener; |
| 142 final String message = context.getString(R.string.whitelist_remove_dialog_
message, url); |
| 143 setWidgetLayoutResource(R.layout.whitelisted_website_delete_widget); |
| 144 setTitle(url); |
| 145 setDialogTitle(R.string.whitelist_remove_dialog_title); |
| 146 setDialogMessage(Html.fromHtml(message)); |
| 147 setNegativeButtonText(android.R.string.cancel); |
| 148 } |
| 149 |
| 150 @Override |
| 151 protected void onBindView(View view) |
| 152 { |
| 153 super.onBindView(view); |
| 154 final View deleteButton = view.findViewById(R.id.whitelisted_website_delet
e_button); |
| 155 deleteButton.setOnClickListener(new View.OnClickListener() |
| 156 { |
| 157 @Override |
| 158 public void onClick(View v) |
| 159 { |
| 160 if (getDialog() == null || !getDialog().isShowing()) |
| 161 { |
| 162 showDialog(null); |
| 163 } |
| 164 } |
| 165 }); |
| 166 } |
| 167 |
| 168 @Override |
| 169 protected void onClick() |
| 170 { |
| 171 // Overriding the default behaviour of showing a dialog here |
| 172 // We just want to show a dialog when the delete button is clicked |
| 173 } |
| 174 |
| 175 @Override |
| 176 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) |
| 177 { |
| 178 builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClic
kListener() |
| 179 { |
| 180 @Override |
| 181 public void onClick(DialogInterface dialog, int which) |
| 182 { |
| 183 if (WhitelistedWebsitePreference.this.onDeleteClickListener != null) |
| 184 { |
| 185 WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dial
og, which); |
| 186 } |
| 187 } |
| 188 }); |
| 189 } |
| 190 } |
| 191 } |
OLD | NEW |