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.browser; |
| 19 |
| 20 import android.app.AlertDialog; |
| 21 import android.app.ProgressDialog; |
| 22 import android.content.Context; |
| 23 import android.content.DialogInterface; |
| 24 import android.preference.DialogPreference; |
| 25 import android.preference.PreferenceGroup; |
| 26 import android.text.Html; |
| 27 import android.util.AttributeSet; |
| 28 import android.util.Log; |
| 29 import android.view.View; |
| 30 |
| 31 import org.mozilla.gecko.R; |
| 32 import org.mozilla.gecko.Tab; |
| 33 import org.mozilla.gecko.Tabs; |
| 34 import org.mozilla.gecko.util.NativeJSObject; |
| 35 import org.mozilla.gecko.util.ThreadUtils; |
| 36 |
| 37 import java.util.Set; |
| 38 import java.util.TreeSet; |
| 39 |
| 40 public class WhitelistedWebsitesPreferenceGroup extends PreferenceGroup implemen
ts |
| 41 AdblockPlusApiCallback, InputValidatorDialogPreference.OnInputReadyListener |
| 42 { |
| 43 |
| 44 private static final String TAG = WhitelistedWebsitesPreferenceGroup.class.get
SimpleName(); |
| 45 |
| 46 private final Set<String> whitelistedWebsites = new TreeSet<>(); |
| 47 private ProgressDialog progressDialog; |
| 48 private final WhitelistCallback whitelistCallback = new WhitelistCallback(); |
| 49 |
| 50 public WhitelistedWebsitesPreferenceGroup(Context context, AttributeSet attrs) |
| 51 { |
| 52 super(context, attrs); |
| 53 this.init(); |
| 54 } |
| 55 |
| 56 public WhitelistedWebsitesPreferenceGroup(Context context, AttributeSet attrs,
int defStyleAttr) |
| 57 { |
| 58 super(context, attrs, defStyleAttr); |
| 59 this.init(); |
| 60 } |
| 61 |
| 62 private void init() |
| 63 { |
| 64 this.setLayoutResource(R.layout.abb_minimal_widget); |
| 65 } |
| 66 |
| 67 @Override |
| 68 protected void onAttachedToActivity() |
| 69 { |
| 70 super.onAttachedToActivity(); |
| 71 this.showDialog(); |
| 72 AddOnBridge.queryWhitelistedWebsites(this); |
| 73 } |
| 74 |
| 75 @Override |
| 76 public void onApiRequestSucceeded(NativeJSObject jsObject) |
| 77 { |
| 78 this.dismissDialog(); |
| 79 this.initEntries(jsObject); |
| 80 } |
| 81 |
| 82 @Override |
| 83 public void onApiRequestFailed(String errorMessage) |
| 84 { |
| 85 Log.w(TAG, "queryWhitelistedWebsites failed: " + errorMessage); |
| 86 this.dismissDialog(); |
| 87 } |
| 88 |
| 89 @Override |
| 90 public void onInputReady(String input) |
| 91 { |
| 92 this.whitelistedWebsites.add(UrlUtils.getHostFromUrl(input)); |
| 93 AddOnBridge.whitelistWebsite(this.whitelistCallback, input, true); |
| 94 this.refreshEntries(); |
| 95 } |
| 96 |
| 97 private void initEntries(NativeJSObject jsObject) |
| 98 { |
| 99 this.whitelistedWebsites.clear(); |
| 100 |
| 101 final NativeJSObject[] whitelistedWebsites = jsObject.getObjectArray("value"
); |
| 102 for (int i = 0; i < whitelistedWebsites.length; i++) |
| 103 { |
| 104 final String url = whitelistedWebsites[i].getString("url"); |
| 105 this.whitelistedWebsites.add(url); |
| 106 } |
| 107 |
| 108 this.refreshEntries(); |
| 109 } |
| 110 |
| 111 private void refreshEntries() |
| 112 { |
| 113 this.removeAll(); |
| 114 for (final String url : this.whitelistedWebsites) |
| 115 { |
| 116 final WhitelistedWebsitePreference whitelistedWebsitePreference = |
| 117 new WhitelistedWebsitePreference(this.getContext(), url, new DialogInt
erface.OnClickListener() |
| 118 { |
| 119 @Override |
| 120 public void onClick(DialogInterface dialog, int which) |
| 121 { |
| 122 AddOnBridge.whitelistWebsite(WhitelistedWebsitesPreferenceGroup.th
is.whitelistCallback, url, false); |
| 123 WhitelistedWebsitesPreferenceGroup.this.whitelistedWebsites.remove
(url); |
| 124 WhitelistedWebsitesPreferenceGroup.this.refreshEntries(); |
| 125 } |
| 126 }); |
| 127 this.addPreference(whitelistedWebsitePreference); |
| 128 } |
| 129 |
| 130 final InputValidatorDialogPreference inputPreference = new InputValidatorDia
logPreference( |
| 131 this.getContext()); |
| 132 inputPreference.setValidationType(InputValidatorDialogPreference.ValidationT
ype.DOMAIN); |
| 133 inputPreference.setTitle(R.string.abb_whitelisted_websites_add_button); |
| 134 inputPreference.setDialogTitle(R.string.abb_whitelist_website_dialog_title); |
| 135 inputPreference.setDialogMessage(R.string.abb_whitelist_website_dialog_messa
ge); |
| 136 inputPreference.getEditText().setHint(R.string.abb_whitelist_website_dialog_
hint); |
| 137 inputPreference.setOnInputReadyListener(this); |
| 138 this.addPreference(inputPreference); |
| 139 } |
| 140 |
| 141 private void showDialog() |
| 142 { |
| 143 this.dismissDialog(); |
| 144 this.progressDialog = new ProgressDialog(getContext()); |
| 145 this.progressDialog.setMessage(this.getContext().getString(R.string.abb_whit
elisted_websites_loading)); |
| 146 this.progressDialog.show(); |
| 147 } |
| 148 |
| 149 private void dismissDialog() |
| 150 { |
| 151 if (this.progressDialog != null && this.progressDialog.isShowing()) |
| 152 { |
| 153 this.progressDialog.dismiss(); |
| 154 this.progressDialog = null; |
| 155 } |
| 156 } |
| 157 |
| 158 private static class WhitelistedWebsitePreference extends DialogPreference |
| 159 { |
| 160 private final DialogInterface.OnClickListener onDeleteClickListener; |
| 161 |
| 162 WhitelistedWebsitePreference(Context context, String url, |
| 163 DialogInterface.OnClickListener onDeleteClickListener) |
| 164 { |
| 165 super(context, null); |
| 166 this.onDeleteClickListener = onDeleteClickListener; |
| 167 final String message = context.getString(R.string.abb_whitelist_remove_dia
log_message, url); |
| 168 this.setWidgetLayoutResource(R.layout.abb_whitelisted_website_delete_widge
t); |
| 169 this.setTitle(url); |
| 170 this.setDialogTitle(R.string.abb_whitelist_remove_dialog_title); |
| 171 this.setDialogMessage(Html.fromHtml(message)); |
| 172 this.setNegativeButtonText(android.R.string.cancel); |
| 173 } |
| 174 |
| 175 @Override |
| 176 protected void onBindView(View view) |
| 177 { |
| 178 super.onBindView(view); |
| 179 final View deleteButton = view.findViewById(R.id.abb_whitelisted_website_d
elete_button); |
| 180 deleteButton.setOnClickListener(new View.OnClickListener() |
| 181 { |
| 182 @Override |
| 183 public void onClick(View v) |
| 184 { |
| 185 if (WhitelistedWebsitePreference.this.getDialog() == null |
| 186 || !WhitelistedWebsitePreference.this.getDialog().isShowing()) |
| 187 { |
| 188 WhitelistedWebsitePreference.this.showDialog(null); |
| 189 } |
| 190 } |
| 191 }); |
| 192 } |
| 193 |
| 194 @Override |
| 195 protected void onClick() |
| 196 { |
| 197 // Overriding the default behaviour of showing a dialog here |
| 198 // We just want to show a dialog when the delete button is clicked |
| 199 } |
| 200 |
| 201 @Override |
| 202 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) |
| 203 { |
| 204 builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClic
kListener() |
| 205 { |
| 206 @Override |
| 207 public void onClick(DialogInterface dialog, int which) |
| 208 { |
| 209 if (WhitelistedWebsitePreference.this.onDeleteClickListener != null) |
| 210 { |
| 211 WhitelistedWebsitePreference.this.onDeleteClickListener.onClick(dial
og, which); |
| 212 } |
| 213 } |
| 214 }); |
| 215 } |
| 216 } |
| 217 |
| 218 private static class WhitelistCallback implements AdblockPlusApiCallback |
| 219 { |
| 220 |
| 221 private final Runnable reloadTabsRunnable = new Runnable() |
| 222 { |
| 223 @Override |
| 224 public void run() |
| 225 { |
| 226 final Tab selectedTab = Tabs.getInstance().getSelectedTab(); |
| 227 if (selectedTab != null) |
| 228 { |
| 229 selectedTab.doReload(false); |
| 230 } |
| 231 } |
| 232 }; |
| 233 |
| 234 @Override |
| 235 public void onApiRequestSucceeded(NativeJSObject jsObject) |
| 236 { |
| 237 ThreadUtils.postToUiThread(this.reloadTabsRunnable); |
| 238 } |
| 239 |
| 240 @Override |
| 241 public void onApiRequestFailed(String errorMessage) |
| 242 { |
| 243 Log.w(TAG, "whitelistWebsite failed: " + errorMessage); |
| 244 } |
| 245 } |
| 246 } |
OLD | NEW |