OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.sbrowser.contentblocker.engine; | 18 package org.adblockplus.sbrowser.contentblocker.engine; |
19 | 19 |
20 import java.io.BufferedWriter; | 20 import java.io.BufferedWriter; |
21 import java.io.File; | 21 import java.io.File; |
22 import java.io.FileOutputStream; | 22 import java.io.FileOutputStream; |
23 import java.io.IOException; | 23 import java.io.IOException; |
24 import java.io.OutputStreamWriter; | 24 import java.io.OutputStreamWriter; |
25 import java.nio.charset.StandardCharsets; | 25 import java.nio.charset.StandardCharsets; |
26 import java.util.ArrayList; | 26 import java.util.ArrayList; |
27 import java.util.HashMap; | 27 import java.util.HashMap; |
28 import java.util.HashSet; | 28 import java.util.HashSet; |
29 import java.util.List; | 29 import java.util.List; |
| 30 import java.util.Locale; |
30 import java.util.Map; | 31 import java.util.Map; |
31 | 32 |
32 import org.adblockplus.sbrowser.contentblocker.engine.Subscription.Type; | 33 import org.adblockplus.sbrowser.contentblocker.engine.Subscription.Type; |
33 | 34 |
34 import android.util.Log; | 35 import android.util.Log; |
35 | 36 |
36 /** | 37 /** |
37 * This class holds all listed subscriptions and manages the subscription | 38 * This class holds all listed subscriptions and manages the subscription |
38 * aggregation cache folder. | 39 * aggregation cache folder. |
39 */ | 40 */ |
40 final class Subscriptions | 41 final class Subscriptions |
41 { | 42 { |
42 private static final String TAG = Subscriptions.class.getSimpleName(); | 43 private static final String TAG = Subscriptions.class.getSimpleName(); |
43 private static final String[] USER_SUBSCRIPTIONS = | 44 private static final String[] USER_SUBSCRIPTIONS = |
44 { Engine.USER_FILTERS_TITLE, Engine.USER_EXCEPTIONS_TITLE }; | 45 { Engine.USER_FILTERS_TITLE, Engine.USER_EXCEPTIONS_TITLE }; |
45 // Filters that begin with '|$' , '||$' , '@@|$' or '@@||$' | 46 // Filters that begin with '|$' , '||$' , '@@|$' or '@@||$' |
46 // See https://issues.adblockplus.org/ticket/4772 | 47 // See https://issues.adblockplus.org/ticket/4772 |
47 private static final String UNSUPPORTED_FILTERS_REGEX = "^(\\|\\$|\\|\\|\\$|@
@\\|\\$|@@\\|\\|\\$).*"; | 48 private static final String UNSUPPORTED_FILTERS_REGEX = "^(\\|\\$|\\|\\|\\$|@
@\\|\\$|@@\\|\\|\\$).*"; |
48 private final HashMap<String, Subscription> subscriptions = new HashMap<>(); | 49 private final HashMap<String, Subscription> subscriptions = new HashMap<>(); |
49 | 50 |
50 private final Engine engine; | 51 private final Engine engine; |
51 private final File subscriptionFolder; | 52 private final File subscriptionFolder; |
52 private final File cacheFolder; | 53 private final File cacheFolder; |
53 private final boolean wasUnitialized; | 54 private final boolean wasUninitialized; |
54 | 55 |
55 private Subscriptions(final Engine engine, final File appFolder, final File ca
cheFolder) | 56 private Subscriptions(final Engine engine, final File appFolder, final File ca
cheFolder) |
56 { | 57 { |
57 this.engine = engine; | 58 this.engine = engine; |
58 this.subscriptionFolder = appFolder; | 59 this.subscriptionFolder = appFolder; |
59 this.wasUnitialized = !this.subscriptionFolder.exists(); | 60 this.wasUninitialized = !this.subscriptionFolder.exists(); |
60 this.cacheFolder = cacheFolder; | 61 this.cacheFolder = cacheFolder; |
61 } | 62 } |
62 | 63 |
63 public boolean wasUnitialized() | 64 public boolean wasUnitialized() |
64 { | 65 { |
65 return this.wasUnitialized; | 66 return this.wasUninitialized; |
66 } | 67 } |
67 | 68 |
68 public File createAndWriteFile() throws IOException | 69 public File createAndWriteFile() throws IOException |
69 { | 70 { |
70 for (;;) | 71 for (;;) |
71 { | 72 { |
72 final File file = new File(this.cacheFolder, String.format("tmp-%d.txt", | 73 final File file = new File(this.cacheFolder, String.format(Locale.ENGLISH,
"tmp-%d.txt", |
73 (int) (Math.random() * 1e8))); | 74 (int) (Math.random() * 1e8))); |
74 if (!file.exists()) | 75 if (!file.exists()) |
75 { | 76 { |
76 Log.d(TAG, "Writing filters to " + file); | 77 Log.d(TAG, "Writing filters to " + file); |
77 this.writeFile(file); | 78 this.writeFile(file); |
78 return file; | 79 return file; |
79 } | 80 } |
80 } | 81 } |
81 } | 82 } |
82 | 83 |
83 List<SubscriptionInfo> getSubscriptions(final Engine engine) | 84 List<SubscriptionInfo> getSubscriptions(final Engine engine) |
84 { | 85 { |
85 final ArrayList<SubscriptionInfo> subs = new ArrayList<>(); | 86 final ArrayList<SubscriptionInfo> subs = new ArrayList<>(); |
86 for (final Subscription sub : this.subscriptions.values()) | 87 for (final Subscription sub : this.subscriptions.values()) |
87 { | 88 { |
88 subs.add(SubscriptionInfo.create(engine, sub)); | 89 subs.add(SubscriptionInfo.create(engine, sub)); |
89 } | 90 } |
90 return subs; | 91 return subs; |
91 } | 92 } |
92 | 93 |
93 void getSubscriptions(final List<Subscription> list) | 94 void loadSubscriptions(final List<Subscription> list) |
94 { | 95 { |
95 list.addAll(this.subscriptions.values()); | 96 list.addAll(this.subscriptions.values()); |
96 } | 97 } |
97 | 98 |
98 public boolean hasSubscription(final String id) | 99 public boolean hasSubscription(final String id) |
99 { | 100 { |
100 return this.subscriptions.containsKey(id); | 101 return this.subscriptions.containsKey(id); |
101 } | 102 } |
102 | 103 |
103 public boolean isSubscriptionEnabled(final String id) | 104 public boolean isSubscriptionEnabled(final String id) |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if (sub != null) | 284 if (sub != null) |
284 { | 285 { |
285 if (sub.updateSubscription(responseCode, text, httpHeaders, this.getMetaFi
le(sub), | 286 if (sub.updateSubscription(responseCode, text, httpHeaders, this.getMetaFi
le(sub), |
286 this.getFiltersFile(sub))) | 287 this.getFiltersFile(sub))) |
287 { | 288 { |
288 this.engine.requestUpdateBroadcast(); | 289 this.engine.requestUpdateBroadcast(); |
289 } | 290 } |
290 } | 291 } |
291 } | 292 } |
292 } | 293 } |
OLD | NEW |