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 java.io.IOException; |
| 21 import java.util.ArrayList; |
| 22 import java.util.Collections; |
| 23 import java.util.Comparator; |
| 24 import java.util.HashMap; |
| 25 import java.util.List; |
| 26 import java.util.Map; |
| 27 |
| 28 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo; |
| 29 import org.adblockplus.sbrowser.contentblocker.engine.Engine; |
| 30 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; |
| 31 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo; |
| 32 import org.adblockplus.adblockplussbrowser.R; |
| 33 import org.adblockplus.sbrowser.contentblocker.preferences.MultilineCheckBoxPref
erence; |
| 34 |
| 35 import android.content.Context; |
| 36 import android.preference.Preference; |
| 37 import android.preference.PreferenceCategory; |
| 38 import android.preference.Preference.OnPreferenceChangeListener; |
| 39 import android.text.format.DateUtils; |
| 40 import android.util.AttributeSet; |
| 41 import android.util.Log; |
| 42 |
| 43 public class MoreBlockingPreferenceCategory extends PreferenceCategory implement
s |
| 44 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener, Engine.Su
bscriptionAddedCallback |
| 45 { |
| 46 private Engine engine = null; |
| 47 private static final int[] WHITELISTED_LIST_TITLES = |
| 48 { |
| 49 R.string.subscription_disable_tracking, |
| 50 R.string.subscription_disable_malware, |
| 51 R.string.subscription_disable_anti_adblock, |
| 52 R.string.subscription_disable_social_media |
| 53 }; |
| 54 |
| 55 private static final String[] WHITELISTED_LIST_URLS = |
| 56 { |
| 57 "https://easylist-downloads.adblockplus.org/easyprivacy.txt", |
| 58 "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt", |
| 59 "https://easylist-downloads.adblockplus.org/antiadblockfilters.txt", |
| 60 "https://easylist-downloads.adblockplus.org/fanboy-social.txt" |
| 61 }; |
| 62 |
| 63 private static final Map<String, Integer> URL_TO_RES_ID_MAP = new HashMap<>(); |
| 64 |
| 65 static |
| 66 { |
| 67 for (int i = 0; i < WHITELISTED_LIST_TITLES.length; i++) |
| 68 { |
| 69 URL_TO_RES_ID_MAP.put(WHITELISTED_LIST_URLS[i], WHITELISTED_LIST_TITLES[i]
); |
| 70 } |
| 71 } |
| 72 |
| 73 public MoreBlockingPreferenceCategory(final Context context) |
| 74 { |
| 75 super(context); |
| 76 } |
| 77 |
| 78 public MoreBlockingPreferenceCategory(final Context context, final AttributeSe
t attrs) |
| 79 { |
| 80 super(context, attrs); |
| 81 } |
| 82 |
| 83 @Override |
| 84 protected void onAttachedToActivity() |
| 85 { |
| 86 EngineService.startService(this.getContext().getApplicationContext(), this); |
| 87 super.onAttachedToActivity(); |
| 88 } |
| 89 |
| 90 @Override |
| 91 public void onEngineCreated(final Engine engine, final boolean success) |
| 92 { |
| 93 this.engine = engine; |
| 94 |
| 95 if (success) |
| 96 { |
| 97 refreshEntries(); |
| 98 } |
| 99 } |
| 100 |
| 101 private void refreshEntries() |
| 102 { |
| 103 final List<SubscriptionInfo> subs = getMoreBlockingPreferenceSubscriptions()
; |
| 104 sortSubscriptionsByRelevance(subs); |
| 105 this.removeAll(); |
| 106 |
| 107 for (final SubscriptionInfo sub : subs) |
| 108 { |
| 109 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); |
| 110 final MultilineCheckBoxPreference cbp = new MultilineCheckBoxPreference(th
is.getContext()); |
| 111 |
| 112 if (sub.isEnabled()) |
| 113 { |
| 114 final StringBuilder sb = new StringBuilder(); |
| 115 sb.append(this.getContext().getString(R.string.last_update)); |
| 116 sb.append(' '); |
| 117 final long timestamp = sub.getLastUpdateTime(); |
| 118 if (timestamp > 0) |
| 119 { |
| 120 sb.append(DateUtils.formatDateTime(this.getContext(), timestamp, |
| 121 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME)); |
| 122 } |
| 123 else |
| 124 { |
| 125 sb.append(this.getContext().getString(R.string.last_update_never)); |
| 126 } |
| 127 cbp.setSummary(sb.toString()); |
| 128 } |
| 129 else |
| 130 { |
| 131 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) |
| 132 { |
| 133 engine.removeSubscriptionById(sub.getId()); |
| 134 continue; |
| 135 } |
| 136 } |
| 137 |
| 138 cbp.setTitle(resInt == null ? sub.getTitle() : getContext().getString(resI
nt)); |
| 139 cbp.setChecked(sub.isEnabled()); |
| 140 cbp.setPersistent(false); |
| 141 cbp.setKey(sub.getId()); |
| 142 cbp.setOnPreferenceChangeListener(this); |
| 143 this.addPreference(cbp); |
| 144 } |
| 145 |
| 146 final InputValidatorDialogPreference urlPreference = new InputValidatorDialo
gPreference(this.getContext()); |
| 147 urlPreference.setValidationType(InputValidatorDialogPreference.ValidationTyp
e.URL); |
| 148 urlPreference.setTitle(R.string.add_another_list); |
| 149 urlPreference.setDialogTitle(R.string.add_another_list); |
| 150 urlPreference.getEditText().setHint(R.string.add_another_list_url_hint); |
| 151 urlPreference.setOnInputReadyListener(new InputValidatorDialogPreference.OnI
nputReadyListener() |
| 152 { |
| 153 @Override |
| 154 public void onInputReady(String input) |
| 155 { |
| 156 if (!input.toLowerCase().startsWith("http://") && !input.toLowerCase().s
tartsWith("https://")) |
| 157 { |
| 158 input = "http://" + input; |
| 159 } |
| 160 |
| 161 try |
| 162 { |
| 163 engine.createAndAddSubscriptionFromUrl(input, MoreBlockingPreferenceCa
tegory.this); |
| 164 } |
| 165 catch (IOException e) |
| 166 { |
| 167 Log.e(getClass().getSimpleName(), "Unable to add subscription from url
", e); |
| 168 } |
| 169 } |
| 170 }); |
| 171 this.addPreference(urlPreference); |
| 172 } |
| 173 |
| 174 private void sortSubscriptionsByRelevance(final List<SubscriptionInfo> moreBlo
ckingPreferenceSubscriptions) |
| 175 { |
| 176 Collections.sort(moreBlockingPreferenceSubscriptions, new Comparator<Subscri
ptionInfo>() |
| 177 { |
| 178 @Override |
| 179 public int compare(SubscriptionInfo o1, SubscriptionInfo o2) |
| 180 { |
| 181 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.cont
ainsKey(o2.getUrl())) |
| 182 { |
| 183 return o1.getTitle().compareTo(o2.getTitle()); |
| 184 } |
| 185 |
| 186 if (URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && !URL_TO_RES_ID_MAP.con
tainsKey(o2.getUrl())) |
| 187 { |
| 188 return -1; |
| 189 } |
| 190 |
| 191 if (!URL_TO_RES_ID_MAP.containsKey(o1.getUrl()) && URL_TO_RES_ID_MAP.con
tainsKey(o2.getUrl())) |
| 192 { |
| 193 return 1; |
| 194 } |
| 195 |
| 196 return 0; |
| 197 } |
| 198 }); |
| 199 } |
| 200 |
| 201 private List<SubscriptionInfo> getMoreBlockingPreferenceSubscriptions() |
| 202 { |
| 203 List<SubscriptionInfo> moreBlockingPreferenceSubscriptions = new ArrayList<>
(5); |
| 204 for (SubscriptionInfo sub : engine.getListedSubscriptions()) |
| 205 { |
| 206 final DefaultSubscriptionInfo info = engine.getDefaultSubscriptionInfoForU
rl(sub.getUrl()); |
| 207 Integer resInt = URL_TO_RES_ID_MAP.get(sub.getUrl()); |
| 208 |
| 209 if (sub.getType() == SubscriptionInfo.Type.CUSTOM) |
| 210 { |
| 211 moreBlockingPreferenceSubscriptions.add(sub); |
| 212 continue; |
| 213 } |
| 214 |
| 215 if (info != null && !info.isComplete() && sub.isEnabled()) |
| 216 { |
| 217 moreBlockingPreferenceSubscriptions.add(sub); |
| 218 continue; |
| 219 } |
| 220 |
| 221 if ((!(engine.isAcceptableAdsUrl(sub)) || sub.getTitle().startsWith("__")) |
| 222 && resInt != null |
| 223 && (info == null || info.getPrefixes().isEmpty() || sub.getType() != S
ubscriptionInfo.Type.ADS)) |
| 224 { |
| 225 moreBlockingPreferenceSubscriptions.add(sub); |
| 226 continue; |
| 227 } |
| 228 } |
| 229 |
| 230 return moreBlockingPreferenceSubscriptions; |
| 231 } |
| 232 |
| 233 @Override |
| 234 public boolean onPreferenceChange(final Preference preference, final Object ne
wValue) |
| 235 { |
| 236 final String id = preference.getKey(); |
| 237 final boolean enabled = (Boolean) newValue; |
| 238 |
| 239 this.engine.changeSubscriptionState(id, enabled); |
| 240 |
| 241 return true; |
| 242 } |
| 243 |
| 244 @Override |
| 245 public void subscriptionAdded() |
| 246 { |
| 247 refreshEntries(); |
| 248 } |
| 249 } |
OLD | NEW |