Left: | ||
Right: |
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 |
(...skipping 12 matching lines...) Expand all Loading... | |
23 import java.io.FileOutputStream; | 23 import java.io.FileOutputStream; |
24 import java.io.IOException; | 24 import java.io.IOException; |
25 import java.io.InputStream; | 25 import java.io.InputStream; |
26 import java.io.InputStreamReader; | 26 import java.io.InputStreamReader; |
27 import java.io.OutputStreamWriter; | 27 import java.io.OutputStreamWriter; |
28 import java.io.Writer; | 28 import java.io.Writer; |
29 import java.net.URI; | 29 import java.net.URI; |
30 import java.net.URISyntaxException; | 30 import java.net.URISyntaxException; |
31 import java.net.URL; | 31 import java.net.URL; |
32 import java.net.URLEncoder; | 32 import java.net.URLEncoder; |
33 import java.nio.charset.StandardCharsets; | |
33 import java.util.ArrayList; | 34 import java.util.ArrayList; |
34 import java.util.Collections; | 35 import java.util.Collections; |
35 import java.util.HashMap; | 36 import java.util.HashMap; |
36 import java.util.List; | 37 import java.util.List; |
37 import java.util.Map; | 38 import java.util.Map; |
38 import java.util.Set; | 39 import java.util.Set; |
39 import java.util.TreeSet; | 40 import java.util.TreeSet; |
40 import java.util.concurrent.LinkedBlockingQueue; | 41 import java.util.concurrent.LinkedBlockingQueue; |
41 import java.util.concurrent.TimeUnit; | 42 import java.util.concurrent.TimeUnit; |
42 import java.util.concurrent.locks.ReentrantLock; | 43 import java.util.concurrent.locks.ReentrantLock; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 77 |
77 public static final String USER_FILTERS_TITLE = "__filters"; | 78 public static final String USER_FILTERS_TITLE = "__filters"; |
78 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; | 79 public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; |
79 | 80 |
80 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING"; | 81 public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowse r.contentBlocker.ACTION_SETTING"; |
81 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE"; | 82 public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.conte ntBlocker.ACTION_UPDATE"; |
82 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt"; | 83 public static final String EASYLIST_URL = "https://easylist-downloads.adblockp lus.org/easylist.txt"; |
83 | 84 |
84 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl"; | 85 public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_except ionsurl"; |
85 | 86 |
86 public static final String CHARSET_UTF_8 = "UTF-8"; | |
87 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version "; | 87 private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version "; |
88 | 88 |
89 // The value below specifies an interval of [x, 2*x[, where x = | 89 // The value below specifies an interval of [x, 2*x[, where x = |
90 // INITIAL_UPDATE_CHECK_DELAY_SECONDS | 90 // INITIAL_UPDATE_CHECK_DELAY_SECONDS |
91 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; | 91 private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; |
92 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; | 92 private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; |
93 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; | 93 private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; |
94 | 94 |
95 public static final long MILLIS_PER_SECOND = 1000; | 95 public static final long MILLIS_PER_SECOND = 1000; |
96 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; | 96 public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND; |
97 public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; | 97 public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; |
98 public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; | 98 public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; |
99 | 99 |
100 private final ReentrantLock accessLock = new ReentrantLock(); | 100 private final ReentrantLock accessLock = new ReentrantLock(); |
101 private DefaultSubscriptions defaultSubscriptions; | 101 private DefaultSubscriptions defaultSubscriptions; |
102 private Subscriptions subscriptions; | 102 private Subscriptions subscriptions; |
103 private JSONPrefs jsonPrefs; | 103 private JSONPrefs jsonPrefs; |
104 private AppInfo appInfo; | 104 private AppInfo appInfo; |
105 private LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueu e<EngineEvent>(); | 105 private LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueu e<>(); |
106 private Thread handlerThread; | 106 private Thread handlerThread; |
107 private Downloader downloader; | 107 private Downloader downloader; |
108 private final Context serviceContext; | 108 private final Context serviceContext; |
109 private boolean wasFirstRun = false; | 109 private boolean wasFirstRun = false; |
110 private long nextUpdateBroadcast = Long.MAX_VALUE; | 110 private long nextUpdateBroadcast = Long.MAX_VALUE; |
111 | 111 |
112 private Engine(final Context context) | 112 private Engine(final Context context) |
113 { | 113 { |
114 this.serviceContext = context; | 114 this.serviceContext = context; |
115 } | 115 } |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 final Engine engine = new Engine(context); | 344 final Engine engine = new Engine(context); |
345 | 345 |
346 // Migration data from previous version (if needed) | 346 // Migration data from previous version (if needed) |
347 engine.migrateFromPreviousVersion(context); | 347 engine.migrateFromPreviousVersion(context); |
348 Log.d(TAG, "Migration done"); | 348 Log.d(TAG, "Migration done"); |
349 | 349 |
350 engine.appInfo = AppInfo.create(context); | 350 engine.appInfo = AppInfo.create(context); |
351 | 351 |
352 Log.d(TAG, "Creating engine, appInfo=" + engine.appInfo.toString()); | 352 Log.d(TAG, "Creating engine, appInfo=" + engine.appInfo.toString()); |
353 | 353 |
354 final InputStream subscriptionsXml = context.getResources() | 354 try (final InputStream subscriptionsXml = context.getResources() |
355 .openRawResource(R.raw.subscriptions); | 355 .openRawResource(R.raw.subscriptions)) |
356 try | |
357 { | 356 { |
358 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription sXml); | 357 engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscription sXml); |
359 } | 358 } |
360 finally | |
361 { | |
362 subscriptionsXml.close(); | |
363 } | |
364 | 359 |
365 Log.d(TAG, "Finished reading 'subscriptions.xml'"); | 360 Log.d(TAG, "Finished reading 'subscriptions.xml'"); |
366 engine.subscriptions = Subscriptions.initialize(engine, getSubscriptionsDir( context), | 361 engine.subscriptions = Subscriptions.initialize(engine, getSubscriptionsDir( context), |
367 getFilterCacheDir(context)); | 362 getFilterCacheDir(context)); |
368 | 363 |
369 final InputStream prefsJson = context.getResources().openRawResource(R.raw.p refs); | 364 try (final InputStream prefsJson = context.getResources().openRawResource(R. raw.prefs)) |
370 try | |
371 { | 365 { |
372 engine.jsonPrefs = JSONPrefs.create(prefsJson); | 366 engine.jsonPrefs = JSONPrefs.create(prefsJson); |
373 } | 367 } |
374 finally | |
375 { | |
376 prefsJson.close(); | |
377 } | |
378 | 368 |
379 Log.d(TAG, "Finished reading JSON preferences"); | 369 Log.d(TAG, "Finished reading JSON preferences"); |
380 | 370 |
381 // Check if this is a fresh start, if so: initialize bundled easylist. | 371 // Check if this is a fresh start, if so: initialize bundled easylist. |
382 engine.wasFirstRun = engine.subscriptions.wasUnitialized(); | 372 engine.wasFirstRun = engine.subscriptions.wasUnitialized(); |
383 if (engine.subscriptions.wasUnitialized()) | 373 if (engine.subscriptions.wasUnitialized()) |
384 { | 374 { |
385 Log.d(TAG, "Subscription storage was uninitialized, initializing..."); | 375 Log.d(TAG, "Subscription storage was uninitialized, initializing..."); |
386 | 376 |
387 final InputStream easylistTxt = context.getResources().openRawResource(R.r aw.easylist); | 377 try (final InputStream easylistTxt = context.getResources().openRawResourc e(R.raw.easylist)) |
388 try | |
389 { | 378 { |
390 final Subscription easylist = engine.subscriptions.add(Subscription | 379 final Subscription easylist = engine.subscriptions.add(Subscription |
391 .create(EASYLIST_URL) | 380 .create(EASYLIST_URL) |
392 .parseLines(readLines(easylistTxt))); | 381 .parseLines(readLines(easylistTxt))); |
393 easylist.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); | 382 easylist.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); |
394 easylist.setEnabled(true); | 383 easylist.setEnabled(true); |
395 } | 384 } |
396 finally | |
397 { | |
398 easylistTxt.close(); | |
399 } | |
400 Log.d(TAG, "Added and enabled bundled easylist"); | 385 Log.d(TAG, "Added and enabled bundled easylist"); |
401 | 386 |
402 final InputStream exceptionsTxt = context.getResources() | 387 try (final InputStream exceptionsTxt = context.getResources() |
403 .openRawResource(R.raw.exceptionrules); | 388 .openRawResource(R.raw.exceptionrules)) |
404 try | |
405 { | 389 { |
406 final Subscription exceptions = engine.subscriptions.add(Subscription | 390 final Subscription exceptions = engine.subscriptions.add(Subscription |
407 .create(engine.getPrefsDefault(SUBSCRIPTIONS_EXCEPTIONSURL)) | 391 .create(engine.getPrefsDefault(SUBSCRIPTIONS_EXCEPTIONSURL)) |
408 .parseLines(readLines(exceptionsTxt))); | 392 .parseLines(readLines(exceptionsTxt))); |
409 exceptions.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); | 393 exceptions.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); |
410 exceptions.setEnabled(true); | 394 exceptions.setEnabled(true); |
411 } | 395 } |
412 finally | |
413 { | |
414 exceptionsTxt.close(); | |
415 } | |
416 Log.d(TAG, "Added and enabled bundled exceptionslist"); | 396 Log.d(TAG, "Added and enabled bundled exceptionslist"); |
417 | 397 |
418 int additional = 0; | 398 int additional = 0; |
419 for (final Subscription sub : engine.defaultSubscriptions.createSubscripti ons()) | 399 for (final Subscription sub : engine.defaultSubscriptions.createSubscripti ons()) |
420 { | 400 { |
421 if (!engine.subscriptions.hasSubscription(sub.getId())) | 401 if (!engine.subscriptions.hasSubscription(sub.getId())) |
422 { | 402 { |
423 additional++; | 403 additional++; |
424 engine.subscriptions.add(sub); | 404 engine.subscriptions.add(sub); |
425 } | 405 } |
(...skipping 14 matching lines...) Expand all Loading... | |
440 { | 420 { |
441 engine.writeFileAndSendUpdateBroadcast(); | 421 engine.writeFileAndSendUpdateBroadcast(); |
442 } | 422 } |
443 | 423 |
444 return engine; | 424 return engine; |
445 } | 425 } |
446 | 426 |
447 public static String readFileAsString(InputStream instream) throws IOException | 427 public static String readFileAsString(InputStream instream) throws IOException |
448 { | 428 { |
449 final StringBuilder sb = new StringBuilder(); | 429 final StringBuilder sb = new StringBuilder(); |
450 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8)); | 430 try (final BufferedReader r = new BufferedReader(new InputStreamReader( |
anton
2017/05/12 05:49:28
Now `r` is closed automatically (as it's `Closeabl
| |
451 for (int ch = r.read(); ch != -1; ch = r.read()) | 431 instream, StandardCharsets.UTF_8))) |
452 { | 432 { |
453 sb.append((char) ch); | 433 for (int ch = r.read(); ch != -1; ch = r.read()) |
434 { | |
435 sb.append((char) ch); | |
436 } | |
454 } | 437 } |
455 return sb.toString(); | 438 return sb.toString(); |
456 } | 439 } |
457 | 440 |
458 public static List<String> readLines(InputStream instream) throws IOException | 441 public static List<String> readLines(InputStream instream) throws IOException |
459 { | 442 { |
460 final ArrayList<String> list = new ArrayList<String>(); | 443 final ArrayList<String> list = new ArrayList<>(); |
461 final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8)); | 444 try (final BufferedReader r = new BufferedReader(new InputStreamReader( |
anton
2017/05/12 05:49:28
Now `r` is closed automatically (as it's `Closeabl
| |
462 for (String line = r.readLine(); line != null; line = r.readLine()) | 445 instream, StandardCharsets.UTF_8))) |
463 { | 446 { |
464 list.add(line); | 447 for (String line = r.readLine(); line != null; line = r.readLine()) |
448 { | |
449 list.add(line); | |
450 } | |
465 } | 451 } |
466 return list; | 452 return list; |
467 } | 453 } |
468 | 454 |
469 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion | 455 public static File getOrCreateCachedFilterFile(Context context) throws IOExcep tion |
470 { | 456 { |
471 final File cachedFilterFile = getCachedFilterFile(context); | 457 final File cachedFilterFile = getCachedFilterFile(context); |
472 if (cachedFilterFile != null && cachedFilterFile.exists()) | 458 if (cachedFilterFile != null && cachedFilterFile.exists()) |
473 { | 459 { |
474 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); | 460 Log.d(TAG, "Cached filter file found: " + cachedFilterFile); |
475 return cachedFilterFile; | 461 return cachedFilterFile; |
476 } | 462 } |
477 | 463 |
478 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); | 464 Log.d(TAG, "Cached filter file not found. Using dummy filter file"); |
479 final File dummyFilterFile = getDummyFilterFile(context); | 465 final File dummyFilterFile = getDummyFilterFile(context); |
480 if (!dummyFilterFile.exists()) | 466 if (!dummyFilterFile.exists()) |
481 { | 467 { |
482 Log.d(TAG, "Creating dummy filter file..."); | 468 Log.d(TAG, "Creating dummy filter file..."); |
483 dummyFilterFile.getParentFile().mkdirs(); | 469 dummyFilterFile.getParentFile().mkdirs(); |
484 final BufferedWriter writer = new BufferedWriter( | 470 try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWrit er( |
485 new OutputStreamWriter(new FileOutputStream(dummyFilterFile), CHARSET_ UTF_8)); | 471 new FileOutputStream(dummyFilterFile), StandardCharsets.UTF_8))) |
486 try | |
487 { | 472 { |
488 writeFilterHeaders(writer); | 473 writeFilterHeaders(writer); |
489 } | 474 } |
490 finally | |
491 { | |
492 writer.close(); | |
493 } | |
494 } | 475 } |
495 return dummyFilterFile; | 476 return dummyFilterFile; |
496 } | 477 } |
497 | 478 |
498 public static void writeFilterHeaders(Writer writer) throws IOException | 479 public static void writeFilterHeaders(Writer writer) throws IOException |
499 { | 480 { |
500 writer.write("[Adblock Plus 2.0]\n"); | 481 writer.write("[Adblock Plus 2.0]\n"); |
501 writer.write("! This file was automatically created.\n"); | 482 writer.write("! This file was automatically created.\n"); |
502 } | 483 } |
503 | 484 |
504 private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException | 485 private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException |
505 { | 486 { |
506 Log.d(TAG, "Writing whitelisted websites..."); | 487 Log.d(TAG, "Writing whitelisted websites..."); |
507 final SharedPreferences prefs = | 488 final SharedPreferences prefs = |
508 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont ext()); | 489 PreferenceManager.getDefaultSharedPreferences(context.getApplicationCont ext()); |
509 final String key = context.getString(R.string.key_whitelisted_websites); | 490 final String key = context.getString(R.string.key_whitelisted_websites); |
510 | 491 |
511 final Set<String> whitelistedWebsites = new TreeSet<String>(); | 492 final Set<String> whitelistedWebsites = new TreeSet<>(); |
512 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty Set())); | 493 whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>empty Set())); |
513 | 494 |
514 final BufferedWriter w = new BufferedWriter( | 495 try (final BufferedWriter w = new BufferedWriter( new OutputStreamWriter( |
515 new OutputStreamWriter(new FileOutputStream(filterFile, true), CHARSET_U TF_8)); | 496 new FileOutputStream(filterFile, true), StandardCharsets.UTF_8))) |
516 try | |
517 { | 497 { |
518 for (final String url : whitelistedWebsites) | 498 for (final String url : whitelistedWebsites) |
519 { | 499 { |
520 try | 500 try |
521 { | 501 { |
522 final URI uri = new URI(url); | 502 final URI uri = new URI(url); |
523 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h(); | 503 final String host = uri.getHost() != null ? uri.getHost() : uri.getPat h(); |
524 w.write("@@||" + host + "^$document"); | 504 w.write("@@||" + host + "^$document"); |
525 w.write('\n'); | 505 w.write('\n'); |
526 } | 506 } |
527 catch (URISyntaxException e) | 507 catch (URISyntaxException e) |
528 { | 508 { |
529 Log.w(TAG, "Failed to parse whitelisted website: " + url); | 509 Log.w(TAG, "Failed to parse whitelisted website: " + url); |
530 continue; | 510 continue; |
531 } | 511 } |
532 } | 512 } |
533 } | 513 } |
534 finally | |
535 { | |
536 w.close(); | |
537 } | |
538 } | 514 } |
539 | 515 |
540 private static File getCachedFilterFile(Context context) | 516 private static File getCachedFilterFile(Context context) |
541 { | 517 { |
542 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); | 518 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(context); |
543 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); | 519 final String cachedFilterPath = prefs.getString(context.getString(R.string.k ey_cached_filter_path), null); |
544 if (cachedFilterPath != null) | 520 if (cachedFilterPath != null) |
545 { | 521 { |
546 return new File(cachedFilterPath); | 522 return new File(cachedFilterPath); |
547 } | 523 } |
(...skipping 24 matching lines...) Expand all Loading... | |
572 if (sub.getURL().getQuery() != null) | 548 if (sub.getURL().getQuery() != null) |
573 { | 549 { |
574 sb.append('&'); | 550 sb.append('&'); |
575 } | 551 } |
576 else | 552 else |
577 { | 553 { |
578 sb.append('?'); | 554 sb.append('?'); |
579 } | 555 } |
580 | 556 |
581 sb.append("addonName="); | 557 sb.append("addonName="); |
582 sb.append(URLEncoder.encode(this.appInfo.addonName, CHARSET_UTF_8)); | 558 sb.append(URLEncoder.encode(this.appInfo.addonName, StandardCharsets.UTF_8.n ame())); |
583 sb.append("&addonVersion="); | 559 sb.append("&addonVersion="); |
584 sb.append(URLEncoder.encode(this.appInfo.addonVersion, CHARSET_UTF_8)); | 560 sb.append(URLEncoder.encode(this.appInfo.addonVersion, StandardCharsets.UTF_ 8.name())); |
585 sb.append("&application="); | 561 sb.append("&application="); |
586 sb.append(URLEncoder.encode(this.appInfo.application, CHARSET_UTF_8)); | 562 sb.append(URLEncoder.encode(this.appInfo.application, StandardCharsets.UTF_8 .name())); |
587 sb.append("&applicationVersion="); | 563 sb.append("&applicationVersion="); |
588 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, CHARSET_UTF_8)) ; | 564 sb.append(URLEncoder.encode(this.appInfo.applicationVersion, StandardCharset s.UTF_8.name())); |
589 sb.append("&platform="); | 565 sb.append("&platform="); |
590 sb.append(URLEncoder.encode(this.appInfo.platform, CHARSET_UTF_8)); | 566 sb.append(URLEncoder.encode(this.appInfo.platform, StandardCharsets.UTF_8.na me())); |
591 sb.append("&platformVersion="); | 567 sb.append("&platformVersion="); |
592 sb.append(URLEncoder.encode(this.appInfo.platformVersion, CHARSET_UTF_8)); | 568 sb.append(URLEncoder.encode(this.appInfo.platformVersion, StandardCharsets.U TF_8.name())); |
593 sb.append("&lastVersion="); | 569 sb.append("&lastVersion="); |
594 sb.append(sub.getVersion()); | 570 sb.append(sub.getVersion()); |
595 sb.append("&downloadCount="); | 571 sb.append("&downloadCount="); |
596 final long downloadCount = sub.getDownloadCount(); | 572 final long downloadCount = sub.getDownloadCount(); |
597 if (downloadCount < 5) | 573 if (downloadCount < 5) |
598 { | 574 { |
599 sb.append(downloadCount); | 575 sb.append(downloadCount); |
600 } | 576 } |
601 else | 577 else |
602 { | 578 { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 this.id = id; | 700 this.id = id; |
725 this.enabled = enabled; | 701 this.enabled = enabled; |
726 } | 702 } |
727 } | 703 } |
728 | 704 |
729 private static class DownloadFinishedEvent extends EngineEvent | 705 private static class DownloadFinishedEvent extends EngineEvent |
730 { | 706 { |
731 private final String id; | 707 private final String id; |
732 private final int responseCode; | 708 private final int responseCode; |
733 private final String response; | 709 private final String response; |
734 private final HashMap<String, String> headers = new HashMap<String, String>( ); | 710 private final HashMap<String, String> headers = new HashMap<>(); |
735 | 711 |
736 public DownloadFinishedEvent(final String id, | 712 public DownloadFinishedEvent(final String id, |
737 final int responseCode, | 713 final int responseCode, |
738 final String response, | 714 final String response, |
739 final Map<String, String> headers) | 715 final Map<String, String> headers) |
740 { | 716 { |
741 super(EngineEvent.EngineEventType.DOWNLOAD_FINISHED); | 717 super(EngineEvent.EngineEventType.DOWNLOAD_FINISHED); |
742 this.id = id; | 718 this.id = id; |
743 this.responseCode = responseCode; | 719 this.responseCode = responseCode; |
744 this.response = response; | 720 this.response = response; |
745 if (headers != null) | 721 if (headers != null) |
746 { | 722 { |
747 this.headers.putAll(headers); | 723 this.headers.putAll(headers); |
748 } | 724 } |
749 } | 725 } |
750 } | 726 } |
751 | 727 |
752 public void enqueueDownload(final Subscription sub, final boolean forced) thro ws IOException | 728 public void enqueueDownload(final Subscription sub, final boolean forced) thro ws IOException |
753 { | 729 { |
754 if (sub.getURL() != null && sub.shouldUpdate(forced)) | 730 if (sub.getURL() != null && sub.shouldUpdate(forced)) |
755 { | 731 { |
756 final HashMap<String, String> headers = new HashMap<String, String>(); | 732 final HashMap<String, String> headers = new HashMap<>(); |
757 if (sub.isMetaDataValid() && sub.isFiltersValid()) | 733 if (sub.isMetaDataValid() && sub.isFiltersValid()) |
758 { | 734 { |
759 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIF IED); | 735 final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIF IED); |
760 if (!TextUtils.isEmpty(lastModified)) | 736 if (!TextUtils.isEmpty(lastModified)) |
761 { | 737 { |
762 headers.put("If-Modified-Since", lastModified); | 738 headers.put("If-Modified-Since", lastModified); |
763 } | 739 } |
764 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); | 740 final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); |
765 if (!TextUtils.isEmpty(etag)) | 741 if (!TextUtils.isEmpty(etag)) |
766 { | 742 { |
767 headers.put("If-None-Match", etag); | 743 headers.put("If-None-Match", etag); |
768 } | 744 } |
769 } | 745 } |
770 Log.d(TAG, headers.toString()); | 746 Log.d(TAG, headers.toString()); |
771 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); | 747 this.downloader.enqueueDownload(this.createDownloadURL(sub), sub.getId(), headers); |
772 } | 748 } |
773 } | 749 } |
774 | 750 |
775 public void connectivityChanged() | 751 public void connectivityChanged() |
776 { | 752 { |
777 this.downloader.connectivityChanged(); | 753 this.downloader.connectivityChanged(); |
778 } | 754 } |
779 } | 755 } |
OLD | NEW |