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.BufferedReader; | 20 import java.io.BufferedReader; |
| 21 import java.io.BufferedWriter; |
21 import java.io.File; | 22 import java.io.File; |
| 23 import java.io.FileOutputStream; |
22 import java.io.IOException; | 24 import java.io.IOException; |
23 import java.io.InputStream; | 25 import java.io.InputStream; |
24 import java.io.InputStreamReader; | 26 import java.io.InputStreamReader; |
| 27 import java.io.OutputStreamWriter; |
| 28 import java.io.Writer; |
25 import java.net.URL; | 29 import java.net.URL; |
26 import java.net.URLEncoder; | 30 import java.net.URLEncoder; |
27 import java.util.ArrayList; | 31 import java.util.ArrayList; |
28 import java.util.HashMap; | 32 import java.util.HashMap; |
29 import java.util.List; | 33 import java.util.List; |
30 import java.util.Map; | 34 import java.util.Map; |
31 import java.util.concurrent.LinkedBlockingQueue; | 35 import java.util.concurrent.LinkedBlockingQueue; |
32 import java.util.concurrent.TimeUnit; | 36 import java.util.concurrent.TimeUnit; |
33 import java.util.concurrent.locks.ReentrantLock; | 37 import java.util.concurrent.locks.ReentrantLock; |
34 import java.util.regex.Pattern; | 38 import java.util.regex.Pattern; |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT
ION_DELAY_MILLIS; | 161 this.nextUpdateBroadcast = System.currentTimeMillis() + BROADCAST_COMBINAT
ION_DELAY_MILLIS; |
158 } | 162 } |
159 finally | 163 finally |
160 { | 164 { |
161 this.unlock(); | 165 this.unlock(); |
162 } | 166 } |
163 } | 167 } |
164 | 168 |
165 private void sendUpdateBroadcast() | 169 private void sendUpdateBroadcast() |
166 { | 170 { |
| 171 createAndWriteFile(); |
| 172 |
167 runOnUiThread(new Runnable() | 173 runOnUiThread(new Runnable() |
168 { | 174 { |
169 @Override | 175 @Override |
170 public void run() | 176 public void run() |
171 { | 177 { |
172 final Intent intent = new Intent(); | 178 final Intent intent = new Intent(); |
173 intent.setAction(ACTION_UPDATE); | 179 intent.setAction(ACTION_UPDATE); |
174 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack
ageName())); | 180 intent.setData(Uri.parse("package:" + Engine.this.serviceContext.getPack
ageName())); |
175 Engine.this.serviceContext.sendBroadcast(intent); | 181 Engine.this.serviceContext.sendBroadcast(intent); |
176 } | 182 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 { | 235 { |
230 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled)); | 236 this.engineEvents.add(new ChangeEnabledStateEvent(id, enabled)); |
231 } | 237 } |
232 | 238 |
233 void downloadFinished(final String id, final int responseCode, final String re
sponse, | 239 void downloadFinished(final String id, final int responseCode, final String re
sponse, |
234 final Map<String, String> headers) | 240 final Map<String, String> headers) |
235 { | 241 { |
236 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response,
headers)); | 242 this.engineEvents.add(new DownloadFinishedEvent(id, responseCode, response,
headers)); |
237 } | 243 } |
238 | 244 |
239 public File createAndWriteFile() throws IOException | 245 public void createAndWriteFile() |
240 { | 246 { |
241 this.lock(); | 247 this.lock(); |
242 try | 248 try |
243 { | 249 { |
244 Log.d(TAG, "Writing filters..."); | 250 Log.d(TAG, "Writing filters..."); |
245 return this.subscriptions.createAndWriteFile(); | 251 final File filterFile = this.subscriptions.createAndWriteFile(); |
| 252 |
| 253 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferen
ces(this.serviceContext); |
| 254 final String key = this.serviceContext.getString(R.string.key_cached_filte
r_path); |
| 255 prefs.edit().putString(key, filterFile.getAbsolutePath()).commit(); |
| 256 |
| 257 } |
| 258 catch (IOException e) |
| 259 { |
| 260 Log.e(TAG, "Failed to write filters", e); |
246 } | 261 } |
247 finally | 262 finally |
248 { | 263 { |
249 this.unlock(); | 264 this.unlock(); |
250 } | 265 } |
251 } | 266 } |
252 | 267 |
253 public static void runOnUiThread(final Runnable runnable) | 268 public static void runOnUiThread(final Runnable runnable) |
254 { | 269 { |
255 new Handler(Looper.getMainLooper()).post(runnable); | 270 new Handler(Looper.getMainLooper()).post(runnable); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 try | 335 try |
321 { | 336 { |
322 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription
sXml); | 337 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription
sXml); |
323 } | 338 } |
324 finally | 339 finally |
325 { | 340 { |
326 subscriptionsXml.close(); | 341 subscriptionsXml.close(); |
327 } | 342 } |
328 | 343 |
329 Log.d(TAG, "Finished reading 'subscriptions.xml'"); | 344 Log.d(TAG, "Finished reading 'subscriptions.xml'"); |
330 engine.subscriptions = Subscriptions.initialize(engine, context.getFilesDir(
), | 345 engine.subscriptions = Subscriptions.initialize(engine, getSubscriptionsDir(
context), |
331 context.getCacheDir()); | 346 getFilterCacheDir(context)); |
332 | 347 |
333 final InputStream prefsJson = context.getResources().openRawResource(R.raw.p
refs); | 348 final InputStream prefsJson = context.getResources().openRawResource(R.raw.p
refs); |
334 try | 349 try |
335 { | 350 { |
336 engine.jsonPrefs = JSONPrefs.create(prefsJson); | 351 engine.jsonPrefs = JSONPrefs.create(prefsJson); |
337 } | 352 } |
338 finally | 353 finally |
339 { | 354 { |
340 prefsJson.close(); | 355 prefsJson.close(); |
341 } | 356 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 { | 401 { |
387 additional++; | 402 additional++; |
388 engine.subscriptions.add(sub); | 403 engine.subscriptions.add(sub); |
389 } | 404 } |
390 } | 405 } |
391 | 406 |
392 Log.d(TAG, "Added " + additional + " additional default/built-in subscript
ions"); | 407 Log.d(TAG, "Added " + additional + " additional default/built-in subscript
ions"); |
393 engine.subscriptions.persistSubscriptions(); | 408 engine.subscriptions.persistSubscriptions(); |
394 } | 409 } |
395 | 410 |
| 411 final File cachedFilterFile = getCachedFilterFile(context); |
| 412 if (cachedFilterFile == null || !cachedFilterFile.exists()) |
| 413 { |
| 414 engine.sendUpdateBroadcast(); |
| 415 } |
| 416 |
396 engine.handlerThread = new Thread(new EventHandler(engine)); | 417 engine.handlerThread = new Thread(new EventHandler(engine)); |
397 engine.handlerThread.setDaemon(true); | 418 engine.handlerThread.setDaemon(true); |
398 engine.handlerThread.start(); | 419 engine.handlerThread.start(); |
399 | 420 |
400 engine.downloader = Downloader.create(context, engine); | 421 engine.downloader = Downloader.create(context, engine); |
401 | 422 |
402 return engine; | 423 return engine; |
403 } | 424 } |
404 | 425 |
405 public static String readFileAsString(InputStream instream) throws IOException | 426 public static String readFileAsString(InputStream instream) throws IOException |
(...skipping 11 matching lines...) Expand all Loading... |
417 { | 438 { |
418 final ArrayList<String> list = new ArrayList<String>(); | 439 final ArrayList<String> list = new ArrayList<String>(); |
419 final BufferedReader r = new BufferedReader(new InputStreamReader(instream,
"UTF-8")); | 440 final BufferedReader r = new BufferedReader(new InputStreamReader(instream,
"UTF-8")); |
420 for (String line = r.readLine(); line != null; line = r.readLine()) | 441 for (String line = r.readLine(); line != null; line = r.readLine()) |
421 { | 442 { |
422 list.add(line); | 443 list.add(line); |
423 } | 444 } |
424 return list; | 445 return list; |
425 } | 446 } |
426 | 447 |
| 448 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep
tion |
| 449 { |
| 450 final File cachedFilterFile = getCachedFilterFile(context); |
| 451 if (cachedFilterFile != null && cachedFilterFile.exists()) |
| 452 { |
| 453 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); |
| 454 return cachedFilterFile; |
| 455 } |
| 456 |
| 457 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); |
| 458 final File dummyFilterFile = getDummyFilterFile(context); |
| 459 if (!dummyFilterFile.exists()) |
| 460 { |
| 461 Log.d(TAG, "Creating dummy filter file..."); |
| 462 dummyFilterFile.getParentFile().mkdirs(); |
| 463 final BufferedWriter writer = new BufferedWriter( |
| 464 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), "UTF-8")
); |
| 465 try |
| 466 { |
| 467 writeFilterHeaders(writer); |
| 468 } |
| 469 finally |
| 470 { |
| 471 writer.close(); |
| 472 } |
| 473 } |
| 474 return dummyFilterFile; |
| 475 } |
| 476 |
| 477 public static void writeFilterHeaders(Writer writer) throws IOException |
| 478 { |
| 479 writer.write("[Adblock Plus 2.0]\n"); |
| 480 writer.write("! This file was automatically created.\n"); |
| 481 } |
| 482 |
| 483 private static File getCachedFilterFile(Context context) |
| 484 { |
| 485 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference
s(context); |
| 486 final String cachedFilterPath = prefs.getString(context.getString(R.string.k
ey_cached_filter_path), null); |
| 487 if (cachedFilterPath != null) |
| 488 { |
| 489 return new File(cachedFilterPath); |
| 490 } |
| 491 |
| 492 return null; |
| 493 } |
| 494 |
| 495 private static File getDummyFilterFile(Context context) |
| 496 { |
| 497 return new File(getFilterCacheDir(context), "dummy.txt"); |
| 498 } |
| 499 |
| 500 private static File getFilterCacheDir(Context context) |
| 501 { |
| 502 return new File(context.getCacheDir(), "subscriptions"); |
| 503 } |
| 504 |
| 505 private static File getSubscriptionsDir(Context context) |
| 506 { |
| 507 return new File(context.getFilesDir(), "subscriptions"); |
| 508 } |
| 509 |
427 URL createDownloadURL(final Subscription sub) throws IOException | 510 URL createDownloadURL(final Subscription sub) throws IOException |
428 { | 511 { |
429 final StringBuilder sb = new StringBuilder(); | 512 final StringBuilder sb = new StringBuilder(); |
430 | 513 |
431 sb.append(sub.getURL()); | 514 sb.append(sub.getURL()); |
432 if (sub.getURL().getQuery() != null) | 515 if (sub.getURL().getQuery() != null) |
433 { | 516 { |
434 sb.append('&'); | 517 sb.append('&'); |
435 } | 518 } |
436 else | 519 else |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 Log.d(TAG, headers.toString()); | 710 Log.d(TAG, headers.toString()); |
628 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(),
headers); | 711 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(),
headers); |
629 } | 712 } |
630 } | 713 } |
631 | 714 |
632 public void connectivityChanged() | 715 public void connectivityChanged() |
633 { | 716 { |
634 this.downloader.connectivityChanged(); | 717 this.downloader.connectivityChanged(); |
635 } | 718 } |
636 } | 719 } |
OLD | NEW |