| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2015 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 java.util.HashMap; | |
| 21 import java.util.HashSet; | |
| 22 import java.util.Map.Entry; | |
| 23 import java.util.concurrent.Semaphore; | |
| 24 | |
| 25 import org.mozilla.gecko.R; | |
| 26 import org.mozilla.gecko.util.NativeJSObject; | |
| 27 import org.mozilla.gecko.util.ThreadUtils; | |
| 28 | |
| 29 import android.app.ProgressDialog; | |
| 30 import android.content.Context; | |
| 31 import android.os.Build; | |
| 32 import android.preference.CheckBoxPreference; | |
| 33 import android.preference.Preference; | |
| 34 import android.preference.PreferenceGroup; | |
| 35 import android.util.AttributeSet; | |
| 36 import android.util.Log; | |
| 37 import android.view.View; | |
| 38 import android.view.ViewGroup; | |
| 39 | |
| 40 public class MoreSubscriptionsPreferenceGroup extends PreferenceGroup implements | |
|
Felix Dahlke
2015/07/29 20:32:07
Naming nit again: Seems you're still mostly callin
René Jeschke
2015/07/30 09:26:44
Done.
| |
| 41 UrlInputDialog.UrlReadyCallback | |
| 42 { | |
| 43 private static final String TAG = "AdblockBrowser.OtherPreferenceGroup"; | |
| 44 private static final HashMap<String, Integer> BUILTIN_URL_TO_INDEX = new HashM ap<String, Integer>(); | |
| 45 private static final HashSet<String> IGNORED_URLS = new HashSet<String>(); | |
| 46 private static SubscriptionContainer recommendedSubscriptions = null; | |
| 47 | |
| 48 private final CheckBoxChangeListener checkBoxChangeListener = new CheckBoxChan geListener(); | |
| 49 private final ActiveSubscriptionContainer activeSubscriptions = new ActiveSubs criptionContainer(); | |
| 50 private ProgressDialog progressDialog; | |
| 51 | |
| 52 private static final int[] BUILTIN_TITLES = | |
| 53 { | |
| 54 R.string.abb_list_disable_tracking, | |
| 55 R.string.abb_list_disable_malware, | |
| 56 R.string.abb_list_disable_anti_adblock, | |
| 57 R.string.abb_list_disable_social_media | |
| 58 }; | |
| 59 | |
| 60 private static final String[] BUILTIN_LISTS = | |
| 61 { | |
| 62 "EasyPrivacy", | |
| 63 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", | |
| 64 "Malware Domains", | |
| 65 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", | |
| 66 "Adblock Warning Removal List", | |
| 67 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", | |
| 68 "Fanboy's Social Blocking List", | |
| 69 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" | |
| 70 }; | |
| 71 | |
| 72 static | |
| 73 { | |
| 74 for (int i = 0; i < BUILTIN_TITLES.length; i++) | |
| 75 { | |
| 76 BUILTIN_URL_TO_INDEX.put(BUILTIN_LISTS[i * 2 + 1], Integer.valueOf(i)); | |
| 77 } | |
| 78 | |
| 79 IGNORED_URLS.add("https://easylist-downloads.adblockplus.org/exceptionrules. txt"); | |
| 80 } | |
| 81 | |
| 82 private synchronized static void initRecommendedSubscriptions() | |
| 83 { | |
| 84 if (recommendedSubscriptions == null) | |
| 85 { | |
| 86 recommendedSubscriptions = SubscriptionContainer.create(false); | |
| 87 | |
| 88 for (SubscriptionContainer.Subscription s : recommendedSubscriptions.getSu bscriptions(false)) | |
| 89 { | |
| 90 IGNORED_URLS.add(s.url); | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 public MoreSubscriptionsPreferenceGroup(final Context context, final Attribute Set attrs) | |
| 96 { | |
| 97 super(context, attrs); | |
| 98 } | |
| 99 | |
| 100 public MoreSubscriptionsPreferenceGroup(final Context context, final Attribute Set attrs, | |
| 101 final int defStyleAttr) | |
| 102 { | |
| 103 super(context, attrs, defStyleAttr); | |
| 104 } | |
| 105 | |
| 106 @Override | |
| 107 protected View onCreateView(final ViewGroup parent) | |
| 108 { | |
| 109 this.setLayoutResource(R.layout.abb_minimal_widget); | |
| 110 return super.onCreateView(parent); | |
| 111 } | |
| 112 | |
| 113 public static Preference createCheckBoxOrSwitch(final Context context) | |
| 114 { | |
| 115 if (Build.VERSION.SDK_INT < 14) | |
| 116 { | |
| 117 return new CheckBoxPreference(context); | |
| 118 } | |
| 119 try | |
| 120 { | |
| 121 return (Preference) Class.forName("android.preference.SwitchPreference") | |
| 122 .getConstructor(Context.class) | |
| 123 .newInstance(context); | |
| 124 } | |
| 125 catch (Exception e) | |
| 126 { | |
| 127 Log.e(TAG, "Failed to create SwitchPreference, falling back to CheckBoxPre ference", e); | |
| 128 return new CheckBoxPreference(context); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 @Override | |
| 133 protected void onAttachedToActivity() | |
| 134 { | |
| 135 this.setEnabled(false); | |
| 136 this.setShouldDisableView(true); | |
| 137 | |
| 138 super.onAttachedToActivity(); | |
| 139 | |
| 140 this.progressDialog = new ProgressDialog(this.getContext()); | |
| 141 this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); | |
| 142 this.progressDialog.setMessage(this.getContext().getString(R.string.abb_adbl ocking_waiting)); | |
| 143 this.progressDialog.show(); | |
| 144 | |
| 145 UrlInputOpenerPreference.setRedirectUrlReadyCallback(this); | |
| 146 | |
| 147 AddOnBridge.handlerPost(new Runnable() | |
| 148 { | |
| 149 @Override | |
| 150 public void run() | |
| 151 { | |
| 152 initRecommendedSubscriptions(); | |
| 153 MoreSubscriptionsPreferenceGroup.this.activeSubscriptions.refresh(); | |
| 154 | |
| 155 ThreadUtils.postToUiThread(new Runnable() | |
| 156 { | |
| 157 @Override | |
| 158 public void run() | |
| 159 { | |
| 160 MoreSubscriptionsPreferenceGroup.this.initEntries(); | |
| 161 } | |
| 162 }); | |
| 163 } | |
| 164 }); | |
| 165 } | |
| 166 | |
| 167 private void initEntries() | |
| 168 { | |
| 169 this.removeAll(); | |
| 170 int i = 0; | |
| 171 for (; i < BUILTIN_TITLES.length; i++) | |
| 172 { | |
| 173 final CheckBoxPreference cbp = new CheckBoxPreference(this.getContext()); | |
| 174 final String url = BUILTIN_LISTS[i * 2 + 1]; | |
| 175 cbp.setOrder(i); | |
| 176 cbp.setTitle(BUILTIN_TITLES[i]); | |
| 177 cbp.setKey(url); | |
| 178 cbp.setChecked(this.activeSubscriptions.enabledSubscriptions.containsKey(u rl)); | |
| 179 cbp.setOnPreferenceChangeListener(this.checkBoxChangeListener); | |
| 180 cbp.setPersistent(false); | |
| 181 this.addPreference(cbp); | |
| 182 } | |
| 183 | |
| 184 for (Entry<String, String> e : this.activeSubscriptions.enabledSubscriptions .entrySet()) | |
| 185 { | |
| 186 if (!BUILTIN_URL_TO_INDEX.containsKey(e.getKey())) | |
| 187 { | |
| 188 final CheckBoxPreference cbp = new CheckBoxPreference(this.getContext()) ; | |
| 189 cbp.setOrder(i++); | |
| 190 cbp.setTitle(e.getValue()); | |
| 191 cbp.setKey(e.getKey()); | |
| 192 cbp.setChecked(true); | |
| 193 cbp.setOnPreferenceChangeListener(this.checkBoxChangeListener); | |
| 194 cbp.setPersistent(false); | |
| 195 this.addPreference(cbp); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 this.setEnabled(true); | |
| 200 this.setShouldDisableView(false); | |
| 201 if (this.progressDialog != null) | |
| 202 { | |
| 203 this.progressDialog.dismiss(); | |
| 204 this.progressDialog = null; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 private void addNewSubscription(final String url) | |
| 209 { | |
| 210 this.progressDialog = new ProgressDialog(this.getContext()); | |
| 211 this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); | |
| 212 this.progressDialog | |
| 213 .setMessage(this.getContext().getString(R.string.abb_add_filter_list_add ing)); | |
| 214 this.progressDialog.show(); | |
| 215 | |
| 216 AddOnBridge.handlerPost(new Runnable() | |
| 217 { | |
| 218 @Override | |
| 219 public void run() | |
| 220 { | |
| 221 try | |
| 222 { | |
| 223 final Semaphore finished = new Semaphore(0); | |
| 224 | |
| 225 AddOnBridge.addSubscription(new AdblockPlusApiCallback() | |
| 226 { | |
| 227 @Override | |
| 228 public void onApiRequestSucceeded(NativeJSObject jsObject) | |
| 229 { | |
| 230 finished.release(); | |
| 231 } | |
| 232 | |
| 233 @Override | |
| 234 public void onApiRequestFailed(String errorMessage) | |
| 235 { | |
| 236 finished.release(); | |
| 237 } | |
| 238 }, url, null); | |
| 239 | |
| 240 finished.acquireUninterruptibly(); | |
| 241 | |
| 242 MoreSubscriptionsPreferenceGroup.this.activeSubscriptions.refresh(); | |
| 243 | |
| 244 ThreadUtils.postToUiThread(new Runnable() | |
| 245 { | |
| 246 @Override | |
| 247 public void run() | |
| 248 { | |
| 249 MoreSubscriptionsPreferenceGroup.this.initEntries(); | |
| 250 } | |
| 251 }); | |
| 252 } | |
| 253 catch (Throwable t) | |
| 254 { | |
| 255 if (MoreSubscriptionsPreferenceGroup.this.progressDialog != null) | |
| 256 { | |
| 257 MoreSubscriptionsPreferenceGroup.this.progressDialog.dismiss(); | |
| 258 MoreSubscriptionsPreferenceGroup.this.progressDialog = null; | |
| 259 } | |
| 260 } | |
| 261 } | |
| 262 }); | |
| 263 } | |
| 264 | |
| 265 @Override | |
| 266 public void callback(final String url) | |
| 267 { | |
| 268 if (url == null) | |
| 269 { | |
| 270 return; | |
| 271 } | |
| 272 | |
| 273 Log.d(TAG, "Adding: " + url); | |
| 274 this.addNewSubscription(url); | |
| 275 } | |
| 276 | |
| 277 private static class ActiveSubscriptionContainer implements AdblockPlusApiCall back | |
| 278 { | |
| 279 public final HashMap<String, String> enabledSubscriptions = new HashMap<Stri ng, String>(); | |
| 280 private final Semaphore entriesReady = new Semaphore(0); | |
| 281 | |
| 282 public void refresh() | |
| 283 { | |
| 284 AddOnBridge.queryActiveSubscriptions(this); | |
| 285 this.entriesReady.acquireUninterruptibly(); | |
| 286 } | |
| 287 | |
| 288 @Override | |
| 289 public void onApiRequestSucceeded(NativeJSObject jsObject) | |
| 290 { | |
| 291 try | |
| 292 { | |
| 293 this.enabledSubscriptions.clear(); | |
| 294 if (jsObject.getBoolean("success")) | |
| 295 { | |
| 296 NativeJSObject[] subs = jsObject.getObjectArray("value"); | |
| 297 for (int i = 0; i < subs.length; i++) | |
| 298 { | |
| 299 final String url = subs[i].getString("url"); | |
| 300 final String title = subs[i].has("title") ? subs[i].getString("title ") : url; | |
| 301 if (!IGNORED_URLS.contains(url)) | |
| 302 { | |
| 303 Log.d(TAG, "Adding: " + url + ", " + title); | |
| 304 this.enabledSubscriptions.put(url, title); | |
| 305 } | |
| 306 } | |
| 307 } | |
| 308 } | |
| 309 finally | |
| 310 { | |
| 311 this.entriesReady.release(); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 @Override | |
| 316 public void onApiRequestFailed(String errorMessage) | |
| 317 { | |
| 318 this.entriesReady.release(); | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 private static class CheckBoxChangeListener implements OnPreferenceChangeListe ner, | |
| 323 AdblockPlusApiCallback | |
| 324 { | |
| 325 @Override | |
| 326 public boolean onPreferenceChange(Preference preference, Object newValue) | |
| 327 { | |
| 328 if (preference instanceof CheckBoxPreference && newValue instanceof Boolea n) | |
| 329 { | |
| 330 final CheckBoxPreference cbp = (CheckBoxPreference) preference; | |
| 331 final boolean enable = ((Boolean) newValue).booleanValue(); | |
| 332 | |
| 333 if (enable) | |
| 334 { | |
| 335 AddOnBridge.addSubscription(this, cbp.getKey(), null); | |
| 336 } | |
| 337 else | |
| 338 { | |
| 339 AddOnBridge.removeSubscription(this, cbp.getKey()); | |
| 340 } | |
| 341 } | |
| 342 return true; | |
| 343 } | |
| 344 | |
| 345 @Override | |
| 346 public void onApiRequestSucceeded(NativeJSObject jsObject) | |
| 347 { | |
| 348 // ignored | |
| 349 } | |
| 350 | |
| 351 @Override | |
| 352 public void onApiRequestFailed(String errorMessage) | |
| 353 { | |
| 354 // ignored | |
| 355 } | |
| 356 } | |
| 357 } | |
| OLD | NEW |