Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Subscriptions.java

Issue 29436555: Issue 5231 - Use Java 7 features (Closed)
Patch Set: Created May 11, 2017, 6:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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.util.ArrayList; 26 import java.util.ArrayList;
26 import java.util.HashMap; 27 import java.util.HashMap;
27 import java.util.HashSet; 28 import java.util.HashSet;
28 import java.util.List; 29 import java.util.List;
29 import java.util.Map; 30 import java.util.Map;
30 31
31 import org.adblockplus.sbrowser.contentblocker.engine.Subscription.Type; 32 import org.adblockplus.sbrowser.contentblocker.engine.Subscription.Type;
32 33
33 import android.util.Log; 34 import android.util.Log;
34 35
35 /** 36 /**
36 * This class holds all listed subscriptions and manages the subscription 37 * This class holds all listed subscriptions and manages the subscription
37 * aggregation cache folder. 38 * aggregation cache folder.
38 */ 39 */
39 final class Subscriptions 40 final class Subscriptions
40 { 41 {
41 private static final String TAG = Subscriptions.class.getSimpleName(); 42 private static final String TAG = Subscriptions.class.getSimpleName();
42 private static final String[] USER_SUBSCRIPTIONS = 43 private static final String[] USER_SUBSCRIPTIONS =
43 { Engine.USER_FILTERS_TITLE, Engine.USER_EXCEPTIONS_TITLE }; 44 { Engine.USER_FILTERS_TITLE, Engine.USER_EXCEPTIONS_TITLE };
44 // Filters that begin with '|$' , '||$' , '@@|$' or '@@||$' 45 // Filters that begin with '|$' , '||$' , '@@|$' or '@@||$'
45 // See https://issues.adblockplus.org/ticket/4772 46 // See https://issues.adblockplus.org/ticket/4772
46 private static final String UNSUPPORTED_FILTERS_REGEX = "^(\\|\\$|\\|\\|\\$|@ @\\|\\$|@@\\|\\|\\$).*"; 47 private static final String UNSUPPORTED_FILTERS_REGEX = "^(\\|\\$|\\|\\|\\$|@ @\\|\\$|@@\\|\\|\\$).*";
47 private final HashMap<String, Subscription> subscriptions = new HashMap<String , Subscription>(); 48 private final HashMap<String, Subscription> subscriptions = new HashMap<>();
48 49
49 private final Engine engine; 50 private final Engine engine;
50 private final File subscriptionFolder; 51 private final File subscriptionFolder;
51 private final File cacheFolder; 52 private final File cacheFolder;
52 private final boolean wasUnitialized; 53 private final boolean wasUnitialized;
53 54
54 private Subscriptions(final Engine engine, final File appFolder, final File ca cheFolder) 55 private Subscriptions(final Engine engine, final File appFolder, final File ca cheFolder)
55 { 56 {
56 this.engine = engine; 57 this.engine = engine;
57 this.subscriptionFolder = appFolder; 58 this.subscriptionFolder = appFolder;
(...skipping 16 matching lines...) Expand all
74 { 75 {
75 Log.d(TAG, "Writing filters to " + file); 76 Log.d(TAG, "Writing filters to " + file);
76 this.writeFile(file); 77 this.writeFile(file);
77 return file; 78 return file;
78 } 79 }
79 } 80 }
80 } 81 }
81 82
82 List<SubscriptionInfo> getSubscriptions(final Engine engine) 83 List<SubscriptionInfo> getSubscriptions(final Engine engine)
83 { 84 {
84 final ArrayList<SubscriptionInfo> subs = new ArrayList<SubscriptionInfo>(); 85 final ArrayList<SubscriptionInfo> subs = new ArrayList<>();
85 for (final Subscription sub : this.subscriptions.values()) 86 for (final Subscription sub : this.subscriptions.values())
86 { 87 {
87 subs.add(SubscriptionInfo.create(engine, sub)); 88 subs.add(SubscriptionInfo.create(engine, sub));
88 } 89 }
89 return subs; 90 return subs;
90 } 91 }
91 92
92 void getSubscriptions(final List<Subscription> list) 93 void getSubscriptions(final List<Subscription> list)
93 { 94 {
94 list.addAll(this.subscriptions.values()); 95 list.addAll(this.subscriptions.values());
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 162
162 /** 163 /**
163 * This method combines all currently listed and enabled subscriptions into 164 * This method combines all currently listed and enabled subscriptions into
164 * one text file. 165 * one text file.
165 * 166 *
166 * @param output 167 * @param output
167 * @throws IOException 168 * @throws IOException
168 */ 169 */
169 private void writeFile(final File output) throws IOException 170 private void writeFile(final File output) throws IOException
170 { 171 {
171 final HashSet<String> filters = new HashSet<String>(); 172 final HashSet<String> filters = new HashSet<>();
172 for (final Subscription s : this.subscriptions.values()) 173 for (final Subscription s : this.subscriptions.values())
173 { 174 {
174 if (s.isEnabled()) 175 if (s.isEnabled())
175 { 176 {
176 Log.d(TAG, "Adding filters for '" + s.getId() + "'"); 177 Log.d(TAG, "Adding filters for '" + s.getId() + "'");
177 s.deserializeFilters(this.getFiltersFile(s)); 178 s.deserializeFilters(this.getFiltersFile(s));
178 s.copyFilters(filters); 179 s.copyFilters(filters);
179 s.clearFilters(); 180 s.clearFilters();
180 } 181 }
181 if ((!s.isMetaDataValid() || !s.isFiltersValid()) && s.getURL() != null) 182 if ((!s.isMetaDataValid() || !s.isFiltersValid()) && s.getURL() != null)
182 { 183 {
183 this.engine.enqueueDownload(s, true); 184 this.engine.enqueueDownload(s, true);
184 } 185 }
185 } 186 }
186 187
187 final BufferedWriter w = new BufferedWriter( 188 try (final BufferedWriter w = new BufferedWriter(
188 new OutputStreamWriter(new FileOutputStream(output), Engine.CHARSET_UTF_ 8)); 189 new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UT F_8)))
189 try
190 { 190 {
191 Log.d(TAG, "Writing " + filters.size() + " filters"); 191 Log.d(TAG, "Writing " + filters.size() + " filters");
192 Engine.writeFilterHeaders(w); 192 Engine.writeFilterHeaders(w);
193 for (final String filter : filters) 193 for (final String filter : filters)
194 { 194 {
195 // This is a temporary fix to not write filters that might crash Samsung Internet 195 // This is a temporary fix to not write filters that might crash Samsung Internet
196 // See https://issues.adblockplus.org/ticket/4772 196 // See https://issues.adblockplus.org/ticket/4772
197 if (!filter.matches(UNSUPPORTED_FILTERS_REGEX)) 197 if (!filter.matches(UNSUPPORTED_FILTERS_REGEX))
198 { 198 {
199 w.write(filter); 199 w.write(filter);
200 w.write('\n'); 200 w.write('\n');
201 } 201 }
202 else 202 else
203 { 203 {
204 Log.d(TAG, "Ignoring unsupported filter: " + filter); 204 Log.d(TAG, "Ignoring unsupported filter: " + filter);
205 } 205 }
206 } 206 }
207 } 207 }
208 finally
209 {
210 w.close();
211 }
212 } 208 }
213 209
214 public Subscription add(final Subscription sub) 210 public Subscription add(final Subscription sub)
215 { 211 {
216 final String id = sub.getId(); 212 final String id = sub.getId();
217 if (!this.subscriptions.containsKey(id)) 213 if (!this.subscriptions.containsKey(id))
218 { 214 {
219 this.subscriptions.put(id, sub); 215 this.subscriptions.put(id, sub);
220 return sub; 216 return sub;
221 } 217 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 if (sub != null) 282 if (sub != null)
287 { 283 {
288 if (sub.updateSubscription(responseCode, text, httpHeaders, this.getMetaFi le(sub), 284 if (sub.updateSubscription(responseCode, text, httpHeaders, this.getMetaFi le(sub),
289 this.getFiltersFile(sub))) 285 this.getFiltersFile(sub)))
290 { 286 {
291 this.engine.requestUpdateBroadcast(); 287 this.engine.requestUpdateBroadcast();
292 } 288 }
293 } 289 }
294 } 290 }
295 } 291 }
OLDNEW

Powered by Google App Engine
This is Rietveld