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

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

Issue 29453722: Noissue - Lint adjustments and optimizations (Closed)
Patch Set: Adjustments based on review comments Created June 13, 2017, 9:23 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
(...skipping 26 matching lines...) Expand all
37 import java.util.HashMap; 37 import java.util.HashMap;
38 import java.util.HashSet; 38 import java.util.HashSet;
39 import java.util.List; 39 import java.util.List;
40 import java.util.Locale; 40 import java.util.Locale;
41 import java.util.Map; 41 import java.util.Map;
42 import java.util.Map.Entry; 42 import java.util.Map.Entry;
43 import java.util.zip.GZIPInputStream; 43 import java.util.zip.GZIPInputStream;
44 import java.util.zip.GZIPOutputStream; 44 import java.util.zip.GZIPOutputStream;
45 45
46 import android.text.TextUtils; 46 import android.text.TextUtils;
47 import android.text.format.DateUtils;
47 import android.util.Log; 48 import android.util.Log;
48 49
49 /** 50 /**
50 * Simple subscription representation. 51 * Simple subscription representation.
51 */ 52 */
52 final class Subscription 53 final class Subscription
53 { 54 {
54 private static final String TAG = Subscription.class.getSimpleName(); 55 private static final String TAG = Subscription.class.getSimpleName();
55 public static final String KEY_TITLE = "title"; 56 public static final String KEY_TITLE = "title";
56 public static final String KEY_VERSION = "version"; 57 public static final String KEY_VERSION = "version";
57 public static final String KEY_FORCED_TITLE = "_title"; 58 public static final String KEY_FORCED_TITLE = "_title";
58 public static final String KEY_HTTP_ETAG = "_etag"; 59 public static final String KEY_HTTP_ETAG = "_etag";
59 public static final String KEY_HTTP_LAST_MODIFIED = "_last_modified"; 60 public static final String KEY_HTTP_LAST_MODIFIED = "_last_modified";
60 public static final String KEY_UPDATE_TIMESTAMP = "_update_timestamp"; 61 public static final String KEY_UPDATE_TIMESTAMP = "_update_timestamp";
61 public static final String KEY_TRIED_UPDATE_TIMESTAMP = "_tried_update_timesta mp"; 62 public static final String KEY_TRIED_UPDATE_TIMESTAMP = "_tried_update_timesta mp";
62 public static final String KEY_DOWNLOAD_COUNT = "_download_count"; 63 public static final String KEY_DOWNLOAD_COUNT = "_download_count";
63 public static final String KEY_ENABLED = "_enabled"; 64 public static final String KEY_ENABLED = "_enabled";
64 public static final String KEY_HAS_FILTERS = "_has_filters"; 65 public static final String KEY_HAS_FILTERS = "_has_filters";
65 public static final String KEY_META_HASH = "_meta_hash"; 66 public static final String KEY_META_HASH = "_meta_hash";
66 67
67 public static final long MINIMAL_DOWNLOAD_INTERVAL = Engine.MILLIS_PER_HOUR / 4; 68 private static final long MINIMAL_DOWNLOAD_INTERVAL = DateUtils.HOUR_IN_MILLIS / 4;
68 public static final long DOWNLOAD_RETRY_INTERVAL = Engine.MILLIS_PER_HOUR; 69 private static final long DOWNLOAD_RETRY_INTERVAL = DateUtils.HOUR_IN_MILLIS;
69 70
70 private static final HashSet<String> ALLOWED_META_KEYS = new HashSet<>(); 71 private static final HashSet<String> ALLOWED_META_KEYS = new HashSet<>();
71 private static final Locale LOCALE_EN = Locale.ENGLISH; 72 private static final Locale LOCALE_EN = Locale.ENGLISH;
72 73
73 private final long updateInterval = Engine.MILLIS_PER_DAY 74 private final long updateInterval = DateUtils.DAY_IN_MILLIS
74 + (long) (Engine.MILLIS_PER_HOUR * 8. * Math.random()); 75 + (long) (DateUtils.HOUR_IN_MILLIS * 8. * Math.random());
75 76
76 /** 77 /**
77 * List of meta keys that are allowed to import from a downloaded 78 * List of meta keys that are allowed to import from a downloaded
78 * subscription. 79 * subscription.
79 */ 80 */
80 private static final String[] ALLOWED_META_KEYS_ARRAY = 81 private static final String[] ALLOWED_META_KEYS_ARRAY =
81 { 82 {
82 "checksum", KEY_VERSION, KEY_TITLE, "last modified", "expires", "homepage" , "licence" 83 "checksum", KEY_VERSION, KEY_TITLE, "last modified", "expires", "homepage" , "licence"
83 }; 84 };
84 85
85 private final URL url; 86 private final URL url;
86 private final Type type; 87 private final Type type;
87 private final HashMap<String, String> meta = new HashMap<>(); 88 private final HashMap<String, String> meta = new HashMap<>();
88 private final HashSet<String> filters = new HashSet<>(); 89 private final HashSet<String> filters = new HashSet<>();
89 90
90 private boolean metaDataValid = true; 91 private boolean metaDataValid = true;
91 private boolean filtersValid = true; 92 private boolean filtersValid = true;
92 93
93 static 94 static
94 { 95 {
95 for (final String s : ALLOWED_META_KEYS_ARRAY) 96 Collections.addAll(ALLOWED_META_KEYS, ALLOWED_META_KEYS_ARRAY);
anton 2017/06/16 13:38:44 Can we replace: ``` private static final HashSet<S
diegocarloslima 2017/07/19 16:39:24 Yeah its possible to initialize the HashSet withou
96 {
97 ALLOWED_META_KEYS.add(s);
98 }
99 } 97 }
100 98
101 /** 99 /**
102 * Subscription type. 100 * Subscription type.
103 * 101 *
104 * @author René Jeschke &lt;rene@adblockplus.org&gt; 102 * @author René Jeschke &lt;rene@adblockplus.org&gt;
105 */ 103 */
106 public enum Type 104 public enum Type
107 { 105 {
108 /** 106 /**
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 return "url:" + subscription.url.toString(); 349 return "url:" + subscription.url.toString();
352 case USER: 350 case USER:
353 return "user:" + subscription.getMeta(KEY_TITLE); 351 return "user:" + subscription.getMeta(KEY_TITLE);
354 } 352 }
355 return ""; 353 return "";
356 } 354 }
357 355
358 private static String byteArrayToHexString(final byte[] array) 356 private static String byteArrayToHexString(final byte[] array)
359 { 357 {
360 final StringBuilder sb = new StringBuilder(array.length * 2); 358 final StringBuilder sb = new StringBuilder(array.length * 2);
361 for (int i = 0; i < array.length; i++) 359 for (final byte b : array)
362 { 360 {
363 final int value = array[i] & 255; 361 final int value = b & 255;
364 if (value < 16) 362 if (value < 16)
365 { 363 {
366 sb.append('0'); 364 sb.append('0');
367 } 365 }
368 sb.append(Integer.toHexString(value)); 366 sb.append(Integer.toHexString(value));
369 } 367 }
370 return sb.toString(); 368 return sb.toString();
371 } 369 }
372 370
373 private static String createMetaDataHash(final HashMap<String, String> meta) t hrows IOException 371 private static String createMetaDataHash(final HashMap<String, String> meta) t hrows IOException
(...skipping 23 matching lines...) Expand all
397 } 395 }
398 catch (final NoSuchAlgorithmException e) 396 catch (final NoSuchAlgorithmException e)
399 { 397 {
400 throw new IOException("MD5 is unavailable: " + e.getMessage(), e); 398 throw new IOException("MD5 is unavailable: " + e.getMessage(), e);
401 } 399 }
402 } 400 }
403 401
404 public void serializeMetaData(final File metaFile) throws IOException 402 public void serializeMetaData(final File metaFile) throws IOException
405 { 403 {
406 this.putMeta(KEY_META_HASH, createMetaDataHash(this.meta)); 404 this.putMeta(KEY_META_HASH, createMetaDataHash(this.meta));
407 try (final DataOutputStream metaOut = new DataOutputStream(new GZIPOutputStr eam( 405 try (final DataOutputStream metaOut = new DataOutputStream(new BufferedOutpu tStream(
408 new BufferedOutputStream(new FileOutputStream(metaFile))))) 406 new GZIPOutputStream(new FileOutputStream(metaFile)))))
409 { 407 {
410 metaOut.writeUTF(this.url != null ? this.url.toString() : ""); 408 metaOut.writeUTF(this.url != null ? this.url.toString() : "");
411 metaOut.writeInt(this.meta.size()); 409 metaOut.writeInt(this.meta.size());
412 for (final Entry<String, String> e : this.meta.entrySet()) 410 for (final Entry<String, String> e : this.meta.entrySet())
413 { 411 {
414 metaOut.writeUTF(e.getKey()); 412 metaOut.writeUTF(e.getKey());
415 metaOut.writeUTF(e.getValue()); 413 metaOut.writeUTF(e.getValue());
416 } 414 }
417 } 415 }
418 } 416 }
419 417
420 public void serializeFilters(final File filtersFile) throws IOException 418 public void serializeFilters(final File filtersFile) throws IOException
421 { 419 {
422 try (final DataOutputStream filtersOut = new DataOutputStream(new GZIPOutput Stream( 420 try (final DataOutputStream filtersOut = new DataOutputStream(new BufferedOu tputStream(
423 new BufferedOutputStream(new FileOutputStream(filtersFile))))) 421 new GZIPOutputStream(new FileOutputStream(filtersFile)))))
424 { 422 {
425 filtersOut.writeInt(this.filters.size()); 423 filtersOut.writeInt(this.filters.size());
426 filtersOut.writeUTF(createFilterHash(new ArrayList<>(this.filters))); 424 filtersOut.writeUTF(createFilterHash(new ArrayList<>(this.filters)));
427 for (final String s : this.filters) 425 for (final String s : this.filters)
428 { 426 {
429 final byte[] b = s.getBytes(StandardCharsets.UTF_8); 427 final byte[] b = s.getBytes(StandardCharsets.UTF_8);
430 filtersOut.writeInt(b.length); 428 filtersOut.writeInt(b.length);
431 filtersOut.write(b); 429 filtersOut.write(b);
432 } 430 }
433 } 431 }
434 } 432 }
435 433
436 public void serializeSubscription(final File metaFile, final File filtersFile) throws IOException 434 public void serializeSubscription(final File metaFile, final File filtersFile) throws IOException
437 { 435 {
438 this.serializeMetaData(metaFile); 436 this.serializeMetaData(metaFile);
439 this.serializeFilters(filtersFile); 437 this.serializeFilters(filtersFile);
440 } 438 }
441 439
442 public static Subscription deserializeSubscription(final File metaFile) 440 public static Subscription deserializeSubscription(final File metaFile)
443 { 441 {
444 Subscription sub = null; 442 Subscription sub = null;
445 try (final DataInputStream in = new DataInputStream(new GZIPInputStream(new BufferedInputStream( 443 try (final DataInputStream in = new DataInputStream(new BufferedInputStream( new GZIPInputStream(
446 new FileInputStream(metaFile))))) 444 new FileInputStream(metaFile)))))
447 { 445 {
448 final String urlString = in.readUTF(); 446 final String urlString = in.readUTF();
449 sub = new Subscription(!TextUtils.isEmpty(urlString) ? new URL(urlString) : null); 447 sub = new Subscription(!TextUtils.isEmpty(urlString) ? new URL(urlString) : null);
450 sub.metaDataValid = false; 448 sub.metaDataValid = false;
451 final int numMetaEntries = in.readInt(); 449 final int numMetaEntries = in.readInt();
452 for (int i = 0; i < numMetaEntries; i++) 450 for (int i = 0; i < numMetaEntries; i++)
453 { 451 {
454 final String key = in.readUTF(); 452 final String key = in.readUTF();
455 final String value = in.readUTF(); 453 final String value = in.readUTF();
456 sub.meta.put(key, value); 454 sub.meta.put(key, value);
457 } 455 }
458 sub.metaDataValid = createMetaDataHash(sub.meta).equals(sub.getMeta(KEY_ME TA_HASH)); 456 sub.metaDataValid = createMetaDataHash(sub.meta).equals(sub.getMeta(KEY_ME TA_HASH));
459 } 457 }
460 catch (Throwable t) 458 catch (Throwable t)
461 { 459 {
462 // We catch Throwable here in order to return whatever we could retrieve f rom the meta file 460 // We catch Throwable here in order to return whatever we could retrieve f rom the meta file
463 } 461 }
464 return sub; 462 return sub;
465 } 463 }
466 464
467 public void deserializeFilters(final File filtersFile) 465 public void deserializeFilters(final File filtersFile)
468 { 466 {
469 this.clearFilters(); 467 this.clearFilters();
470 this.filtersValid = false; 468 this.filtersValid = false;
471 try (final DataInputStream in = new DataInputStream(new GZIPInputStream(new BufferedInputStream( 469 try (final DataInputStream in = new DataInputStream(new BufferedInputStream( new GZIPInputStream(
472 new FileInputStream(filtersFile))))) 470 new FileInputStream(filtersFile)))))
473 { 471 {
474 final int numFilters = in.readInt(); 472 final int numFilters = in.readInt();
475 final String filtersHash = in.readUTF(); 473 final String filtersHash = in.readUTF();
476 for (int i = 0; i < numFilters; i++) 474 for (int i = 0; i < numFilters; i++)
477 { 475 {
478 final int length = in.readInt(); 476 final int length = in.readInt();
479 final byte[] b = new byte[length]; 477 final byte[] b = new byte[length];
480 in.readFully(b); 478 in.readFully(b);
481 this.filters.add(new String(b, StandardCharsets.UTF_8)); 479 this.filters.add(new String(b, StandardCharsets.UTF_8));
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 this.serializeMetaData(metaFile); 605 this.serializeMetaData(metaFile);
608 if (filtersChanged) 606 if (filtersChanged)
609 { 607 {
610 this.serializeFilters(filtersFile); 608 this.serializeFilters(filtersFile);
611 this.clearFilters(); 609 this.clearFilters();
612 } 610 }
613 611
614 return filtersChanged; 612 return filtersChanged;
615 } 613 }
616 } 614 }
OLDNEW

Powered by Google App Engine
This is Rietveld