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.io.IOException; |
| 21 import java.io.StringReader; |
| 22 import java.util.ArrayList; |
| 23 import java.util.HashMap; |
| 24 import java.util.List; |
| 25 import java.util.concurrent.ConcurrentHashMap; |
| 26 import java.util.concurrent.Semaphore; |
| 27 |
| 28 import org.mozilla.gecko.util.NativeJSObject; |
| 29 import org.xmlpull.v1.XmlPullParser; |
| 30 import org.xmlpull.v1.XmlPullParserException; |
| 31 |
| 32 import android.util.Log; |
| 33 import android.util.Xml; |
| 34 |
| 35 final class SubscriptionContainer implements AdblockPlusApiCallback |
| 36 { |
| 37 private static final String TAG = SubscriptionContainer.class.getName(); |
| 38 private final ConcurrentHashMap<String, Boolean> enableState = new ConcurrentH
ashMap<String, Boolean>(); |
| 39 private final Semaphore entriesReady = new Semaphore(0); |
| 40 private final List<SubscriptionContainer.Subscription> entries = new ArrayList
<SubscriptionContainer.Subscription>(); |
| 41 private final HashMap<String, SubscriptionContainer.Subscription> urlMap = new
HashMap<String, SubscriptionContainer.Subscription>(); |
| 42 |
| 43 private SubscriptionContainer() |
| 44 { |
| 45 // prevent external instantiation |
| 46 } |
| 47 |
| 48 public final static SubscriptionContainer create() |
| 49 { |
| 50 return create(true); |
| 51 } |
| 52 |
| 53 public final static SubscriptionContainer create(final boolean refresh) |
| 54 { |
| 55 final SubscriptionContainer sc = new SubscriptionContainer(); |
| 56 AddOnBridge.queryValue(sc, "subscriptionsXml"); |
| 57 sc.entriesReady.acquireUninterruptibly(); |
| 58 |
| 59 for (final SubscriptionContainer.Subscription e : sc.entries) |
| 60 { |
| 61 sc.urlMap.put(e.url, e); |
| 62 sc.enableState.put(e.url, Boolean.FALSE); |
| 63 } |
| 64 |
| 65 if (refresh) |
| 66 { |
| 67 sc.refresh(); |
| 68 } |
| 69 |
| 70 return sc; |
| 71 } |
| 72 |
| 73 public void refresh() |
| 74 { |
| 75 if (!this.entries.isEmpty()) |
| 76 { |
| 77 for (int i = 0; i < this.entries.size(); i++) |
| 78 { |
| 79 SubscriptionChangeAction.post(this.entries.get(i), |
| 80 this, |
| 81 SubscriptionChangeAction.Mode.QUERY_SUBSCRIPTION_ENABLED); |
| 82 } |
| 83 |
| 84 this.entriesReady.acquireUninterruptibly(this.entries.size()); |
| 85 } |
| 86 } |
| 87 |
| 88 public List<SubscriptionContainer.Subscription> getSubscriptions(boolean enabl
ed) |
| 89 { |
| 90 final List<SubscriptionContainer.Subscription> ret = new ArrayList<Subscript
ionContainer.Subscription>(); |
| 91 for (final SubscriptionContainer.Subscription e : this.entries) |
| 92 { |
| 93 if (this.isSubscriptionListed(e.url) == enabled) |
| 94 { |
| 95 ret.add(e); |
| 96 } |
| 97 } |
| 98 return ret; |
| 99 } |
| 100 |
| 101 public boolean isSubscriptionListed(final String url) |
| 102 { |
| 103 return this.enableState.containsKey(url) && this.enableState.get(url).boolea
nValue(); |
| 104 } |
| 105 |
| 106 public void changeSubscriptionState(final String url, final boolean enable) |
| 107 { |
| 108 final SubscriptionContainer.Subscription e = this.urlMap.get(url); |
| 109 if (e != null) |
| 110 { |
| 111 if (enable) |
| 112 { |
| 113 SubscriptionChangeAction.post(e, SubscriptionPreferenceCategory.subscrip
tionContainer, |
| 114 SubscriptionChangeAction.Mode.ENABLE_SUBSCRIPTION); |
| 115 } |
| 116 else |
| 117 { |
| 118 SubscriptionChangeAction.post(e, SubscriptionPreferenceCategory.subscrip
tionContainer, |
| 119 SubscriptionChangeAction.Mode.DISABLE_SUBSCRIPTION); |
| 120 } |
| 121 } |
| 122 } |
| 123 |
| 124 @Override |
| 125 public void onApiRequestSucceeded(NativeJSObject jsObject) |
| 126 { |
| 127 final XmlPullParser parser = Xml.newPullParser(); |
| 128 try |
| 129 { |
| 130 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); |
| 131 parser.setInput(new StringReader(AddOnBridge.getStringFromJsObject(jsObjec
t, "value", ""))); |
| 132 parser.nextTag(); |
| 133 parser.require(XmlPullParser.START_TAG, null, "subscriptions"); |
| 134 while (parser.next() != XmlPullParser.END_TAG) |
| 135 { |
| 136 if (parser.getEventType() != XmlPullParser.START_TAG) |
| 137 { |
| 138 continue; |
| 139 } |
| 140 if ("subscription".equals(parser.getName())) |
| 141 { |
| 142 final String title = parser.getAttributeValue(null, "title"); |
| 143 final String specialization = parser.getAttributeValue(null, "speciali
zation"); |
| 144 final String url = parser.getAttributeValue(null, "url"); |
| 145 this.entries.add(new Subscription(title, specialization, url)); |
| 146 } |
| 147 parser.next(); |
| 148 } |
| 149 } |
| 150 catch (XmlPullParserException e) |
| 151 { |
| 152 Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e); |
| 153 } |
| 154 catch (IOException e) |
| 155 { |
| 156 Log.e(TAG, "Failed to parse subscriptions.xml: " + e.getMessage(), e); |
| 157 } |
| 158 finally |
| 159 { |
| 160 this.entriesReady.release(); |
| 161 } |
| 162 } |
| 163 |
| 164 @Override |
| 165 public void onApiRequestFailed(String errorMessage) |
| 166 { |
| 167 Log.e(TAG, "Error: " + errorMessage); |
| 168 this.entriesReady.release(); |
| 169 } |
| 170 |
| 171 private static class SubscriptionChangeAction implements AdblockPlusApiCallbac
k |
| 172 { |
| 173 private static final String TAG = SubscriptionContainer.SubscriptionChangeAc
tion.class.getName(); |
| 174 |
| 175 private final SubscriptionContainer.Subscription subscription; |
| 176 private final SubscriptionContainer parent; |
| 177 private final SubscriptionChangeAction.Mode mode; |
| 178 |
| 179 public enum Mode |
| 180 { |
| 181 QUERY_SUBSCRIPTION_ENABLED, |
| 182 ENABLE_SUBSCRIPTION, |
| 183 DISABLE_SUBSCRIPTION, |
| 184 } |
| 185 |
| 186 public SubscriptionChangeAction(final SubscriptionContainer.Subscription sub
scription, |
| 187 final SubscriptionContainer parent, |
| 188 final SubscriptionChangeAction.Mode mode) |
| 189 { |
| 190 this.subscription = subscription; |
| 191 this.parent = parent; |
| 192 this.mode = mode; |
| 193 } |
| 194 |
| 195 public static SubscriptionContainer.SubscriptionChangeAction post(final Subs
criptionContainer.Subscription subscription, |
| 196 final SubscriptionContainer parent, |
| 197 final SubscriptionChangeAction.Mode mode) |
| 198 { |
| 199 return new SubscriptionChangeAction(subscription, parent, mode).post(); |
| 200 } |
| 201 |
| 202 public SubscriptionContainer.SubscriptionChangeAction post() |
| 203 { |
| 204 switch (this.mode) |
| 205 { |
| 206 case QUERY_SUBSCRIPTION_ENABLED: |
| 207 AddOnBridge.querySubscriptionListStatus(this, this.subscription.url); |
| 208 break; |
| 209 case ENABLE_SUBSCRIPTION: |
| 210 AddOnBridge.addSubscription(this, this.subscription.url, this.subscrip
tion.title); |
| 211 break; |
| 212 case DISABLE_SUBSCRIPTION: |
| 213 AddOnBridge.removeSubscription(this, this.subscription.url); |
| 214 break; |
| 215 default: |
| 216 break; |
| 217 } |
| 218 return this; |
| 219 } |
| 220 |
| 221 @Override |
| 222 public void onApiRequestSucceeded(NativeJSObject jsObject) |
| 223 { |
| 224 switch (this.mode) |
| 225 { |
| 226 case QUERY_SUBSCRIPTION_ENABLED: |
| 227 try |
| 228 { |
| 229 this.parent.enableState.put(this.subscription.url, |
| 230 Boolean.valueOf(AddOnBridge.getBooleanFromJsObject(jsObject, "va
lue", false))); |
| 231 } |
| 232 finally |
| 233 { |
| 234 this.parent.entriesReady.release(); |
| 235 } |
| 236 break; |
| 237 default: |
| 238 break; |
| 239 } |
| 240 } |
| 241 |
| 242 @Override |
| 243 public void onApiRequestFailed(String errorMessage) |
| 244 { |
| 245 switch (this.mode) |
| 246 { |
| 247 case QUERY_SUBSCRIPTION_ENABLED: |
| 248 this.parent.enableState.put(this.subscription.url, Boolean.FALSE); |
| 249 this.parent.entriesReady.release(); |
| 250 break; |
| 251 default: |
| 252 break; |
| 253 } |
| 254 |
| 255 Log.e(TAG, "Error for '" + this.subscription.url |
| 256 + "', mode: " + this.mode + ": " |
| 257 + errorMessage); |
| 258 } |
| 259 } |
| 260 |
| 261 public static class Subscription |
| 262 { |
| 263 public final String title; |
| 264 public final String specialization; |
| 265 public final String url; |
| 266 |
| 267 public Subscription(final String title, final String specialization, final S
tring url) |
| 268 { |
| 269 this.title = title; |
| 270 this.specialization = specialization; |
| 271 this.url = url; |
| 272 } |
| 273 |
| 274 @Override |
| 275 public String toString() |
| 276 { |
| 277 return this.specialization + " (" + this.title + ") @ " + this.url; |
| 278 } |
| 279 } |
| 280 } |
OLD | NEW |