| Index: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java |
| =================================================================== |
| --- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java |
| +++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java |
| @@ -25,16 +25,17 @@ import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.io.OutputStreamWriter; |
| import java.io.Writer; |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| import java.net.URL; |
| import java.net.URLEncoder; |
| +import java.nio.charset.StandardCharsets; |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Set; |
| import java.util.TreeSet; |
| import java.util.concurrent.LinkedBlockingQueue; |
| @@ -78,17 +79,16 @@ public final class Engine |
| public static final String USER_EXCEPTIONS_TITLE = "__exceptions"; |
| public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowser.contentBlocker.ACTION_SETTING"; |
| public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.contentBlocker.ACTION_UPDATE"; |
| public static final String EASYLIST_URL = "https://easylist-downloads.adblockplus.org/easylist.txt"; |
| public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_exceptionsurl"; |
| - public static final String CHARSET_UTF_8 = "UTF-8"; |
| private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version"; |
| // The value below specifies an interval of [x, 2*x[, where x = |
| // INITIAL_UPDATE_CHECK_DELAY_SECONDS |
| private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5; |
| private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30; |
| private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500; |
| @@ -97,17 +97,17 @@ public final class Engine |
| public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; |
| public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; |
| private final ReentrantLock accessLock = new ReentrantLock(); |
| private DefaultSubscriptions defaultSubscriptions; |
| private Subscriptions subscriptions; |
| private JSONPrefs jsonPrefs; |
| private AppInfo appInfo; |
| - private LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueue<EngineEvent>(); |
| + private LinkedBlockingQueue<EngineEvent> engineEvents = new LinkedBlockingQueue<>(); |
| private Thread handlerThread; |
| private Downloader downloader; |
| private final Context serviceContext; |
| private boolean wasFirstRun = false; |
| private long nextUpdateBroadcast = Long.MAX_VALUE; |
| private Engine(final Context context) |
| { |
| @@ -346,78 +346,58 @@ public final class Engine |
| // Migration data from previous version (if needed) |
| engine.migrateFromPreviousVersion(context); |
| Log.d(TAG, "Migration done"); |
| engine.appInfo = AppInfo.create(context); |
| Log.d(TAG, "Creating engine, appInfo=" + engine.appInfo.toString()); |
| - final InputStream subscriptionsXml = context.getResources() |
| - .openRawResource(R.raw.subscriptions); |
| - try |
| + try (final InputStream subscriptionsXml = context.getResources() |
| + .openRawResource(R.raw.subscriptions)) |
| { |
| engine.defaultSubscriptions = DefaultSubscriptions.fromStream(subscriptionsXml); |
| } |
| - finally |
| - { |
| - subscriptionsXml.close(); |
| - } |
| Log.d(TAG, "Finished reading 'subscriptions.xml'"); |
| engine.subscriptions = Subscriptions.initialize(engine, getSubscriptionsDir(context), |
| getFilterCacheDir(context)); |
| - final InputStream prefsJson = context.getResources().openRawResource(R.raw.prefs); |
| - try |
| + try (final InputStream prefsJson = context.getResources().openRawResource(R.raw.prefs)) |
| { |
| engine.jsonPrefs = JSONPrefs.create(prefsJson); |
| } |
| - finally |
| - { |
| - prefsJson.close(); |
| - } |
| Log.d(TAG, "Finished reading JSON preferences"); |
| // Check if this is a fresh start, if so: initialize bundled easylist. |
| engine.wasFirstRun = engine.subscriptions.wasUnitialized(); |
| if (engine.subscriptions.wasUnitialized()) |
| { |
| Log.d(TAG, "Subscription storage was uninitialized, initializing..."); |
| - final InputStream easylistTxt = context.getResources().openRawResource(R.raw.easylist); |
| - try |
| + try (final InputStream easylistTxt = context.getResources().openRawResource(R.raw.easylist)) |
| { |
| final Subscription easylist = engine.subscriptions.add(Subscription |
| .create(EASYLIST_URL) |
| .parseLines(readLines(easylistTxt))); |
| easylist.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); |
| easylist.setEnabled(true); |
| } |
| - finally |
| - { |
| - easylistTxt.close(); |
| - } |
| Log.d(TAG, "Added and enabled bundled easylist"); |
| - final InputStream exceptionsTxt = context.getResources() |
| - .openRawResource(R.raw.exceptionrules); |
| - try |
| + try (final InputStream exceptionsTxt = context.getResources() |
| + .openRawResource(R.raw.exceptionrules)) |
| { |
| final Subscription exceptions = engine.subscriptions.add(Subscription |
| .create(engine.getPrefsDefault(SUBSCRIPTIONS_EXCEPTIONSURL)) |
| .parseLines(readLines(exceptionsTxt))); |
| exceptions.putMeta(Subscription.KEY_UPDATE_TIMESTAMP, "0"); |
| exceptions.setEnabled(true); |
| } |
| - finally |
| - { |
| - exceptionsTxt.close(); |
| - } |
| Log.d(TAG, "Added and enabled bundled exceptionslist"); |
| int additional = 0; |
| for (final Subscription sub : engine.defaultSubscriptions.createSubscriptions()) |
| { |
| if (!engine.subscriptions.hasSubscription(sub.getId())) |
| { |
| additional++; |
| @@ -442,31 +422,37 @@ public final class Engine |
| } |
| return engine; |
| } |
| public static String readFileAsString(InputStream instream) throws IOException |
| { |
| final StringBuilder sb = new StringBuilder(); |
| - final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8)); |
| - for (int ch = r.read(); ch != -1; ch = r.read()) |
| + try (final BufferedReader r = new BufferedReader(new InputStreamReader( |
|
anton
2017/05/12 05:49:28
Now `r` is closed automatically (as it's `Closeabl
|
| + instream, StandardCharsets.UTF_8))) |
| { |
| - sb.append((char) ch); |
| + for (int ch = r.read(); ch != -1; ch = r.read()) |
| + { |
| + sb.append((char) ch); |
| + } |
| } |
| return sb.toString(); |
| } |
| public static List<String> readLines(InputStream instream) throws IOException |
| { |
| - final ArrayList<String> list = new ArrayList<String>(); |
| - final BufferedReader r = new BufferedReader(new InputStreamReader(instream, CHARSET_UTF_8)); |
| - for (String line = r.readLine(); line != null; line = r.readLine()) |
| + final ArrayList<String> list = new ArrayList<>(); |
| + try (final BufferedReader r = new BufferedReader(new InputStreamReader( |
|
anton
2017/05/12 05:49:28
Now `r` is closed automatically (as it's `Closeabl
|
| + instream, StandardCharsets.UTF_8))) |
| { |
| - list.add(line); |
| + for (String line = r.readLine(); line != null; line = r.readLine()) |
| + { |
| + list.add(line); |
| + } |
| } |
| return list; |
| } |
| public static File getOrCreateCachedFilterFile(Context context) throws IOException |
| { |
| final File cachedFilterFile = getCachedFilterFile(context); |
| if (cachedFilterFile != null && cachedFilterFile.exists()) |
| @@ -476,26 +462,21 @@ public final class Engine |
| } |
| Log.d(TAG, "Cached filter file not found. Using dummy filter file"); |
| final File dummyFilterFile = getDummyFilterFile(context); |
| if (!dummyFilterFile.exists()) |
| { |
| Log.d(TAG, "Creating dummy filter file..."); |
| dummyFilterFile.getParentFile().mkdirs(); |
| - final BufferedWriter writer = new BufferedWriter( |
| - new OutputStreamWriter(new FileOutputStream(dummyFilterFile), CHARSET_UTF_8)); |
| - try |
| + try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( |
| + new FileOutputStream(dummyFilterFile), StandardCharsets.UTF_8))) |
| { |
| writeFilterHeaders(writer); |
| } |
| - finally |
| - { |
| - writer.close(); |
| - } |
| } |
| return dummyFilterFile; |
| } |
| public static void writeFilterHeaders(Writer writer) throws IOException |
| { |
| writer.write("[Adblock Plus 2.0]\n"); |
| writer.write("! This file was automatically created.\n"); |
| @@ -503,22 +484,21 @@ public final class Engine |
| private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException |
| { |
| Log.d(TAG, "Writing whitelisted websites..."); |
| final SharedPreferences prefs = |
| PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); |
| final String key = context.getString(R.string.key_whitelisted_websites); |
| - final Set<String> whitelistedWebsites = new TreeSet<String>(); |
| + final Set<String> whitelistedWebsites = new TreeSet<>(); |
| whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>emptySet())); |
| - final BufferedWriter w = new BufferedWriter( |
| - new OutputStreamWriter(new FileOutputStream(filterFile, true), CHARSET_UTF_8)); |
| - try |
| + try (final BufferedWriter w = new BufferedWriter( new OutputStreamWriter( |
| + new FileOutputStream(filterFile, true), StandardCharsets.UTF_8))) |
| { |
| for (final String url : whitelistedWebsites) |
| { |
| try |
| { |
| final URI uri = new URI(url); |
| final String host = uri.getHost() != null ? uri.getHost() : uri.getPath(); |
| w.write("@@||" + host + "^$document"); |
| @@ -526,20 +506,16 @@ public final class Engine |
| } |
| catch (URISyntaxException e) |
| { |
| Log.w(TAG, "Failed to parse whitelisted website: " + url); |
| continue; |
| } |
| } |
| } |
| - finally |
| - { |
| - w.close(); |
| - } |
| } |
| private static File getCachedFilterFile(Context context) |
| { |
| final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
| final String cachedFilterPath = prefs.getString(context.getString(R.string.key_cached_filter_path), null); |
| if (cachedFilterPath != null) |
| { |
| @@ -574,27 +550,27 @@ public final class Engine |
| sb.append('&'); |
| } |
| else |
| { |
| sb.append('?'); |
| } |
| sb.append("addonName="); |
| - sb.append(URLEncoder.encode(this.appInfo.addonName, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.addonName, StandardCharsets.UTF_8.name())); |
| sb.append("&addonVersion="); |
| - sb.append(URLEncoder.encode(this.appInfo.addonVersion, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.addonVersion, StandardCharsets.UTF_8.name())); |
| sb.append("&application="); |
| - sb.append(URLEncoder.encode(this.appInfo.application, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.application, StandardCharsets.UTF_8.name())); |
| sb.append("&applicationVersion="); |
| - sb.append(URLEncoder.encode(this.appInfo.applicationVersion, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.applicationVersion, StandardCharsets.UTF_8.name())); |
| sb.append("&platform="); |
| - sb.append(URLEncoder.encode(this.appInfo.platform, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.platform, StandardCharsets.UTF_8.name())); |
| sb.append("&platformVersion="); |
| - sb.append(URLEncoder.encode(this.appInfo.platformVersion, CHARSET_UTF_8)); |
| + sb.append(URLEncoder.encode(this.appInfo.platformVersion, StandardCharsets.UTF_8.name())); |
| sb.append("&lastVersion="); |
| sb.append(sub.getVersion()); |
| sb.append("&downloadCount="); |
| final long downloadCount = sub.getDownloadCount(); |
| if (downloadCount < 5) |
| { |
| sb.append(downloadCount); |
| } |
| @@ -726,17 +702,17 @@ public final class Engine |
| } |
| } |
| private static class DownloadFinishedEvent extends EngineEvent |
| { |
| private final String id; |
| private final int responseCode; |
| private final String response; |
| - private final HashMap<String, String> headers = new HashMap<String, String>(); |
| + private final HashMap<String, String> headers = new HashMap<>(); |
| public DownloadFinishedEvent(final String id, |
| final int responseCode, |
| final String response, |
| final Map<String, String> headers) |
| { |
| super(EngineEvent.EngineEventType.DOWNLOAD_FINISHED); |
| this.id = id; |
| @@ -748,17 +724,17 @@ public final class Engine |
| } |
| } |
| } |
| public void enqueueDownload(final Subscription sub, final boolean forced) throws IOException |
| { |
| if (sub.getURL() != null && sub.shouldUpdate(forced)) |
| { |
| - final HashMap<String, String> headers = new HashMap<String, String>(); |
| + final HashMap<String, String> headers = new HashMap<>(); |
| if (sub.isMetaDataValid() && sub.isFiltersValid()) |
| { |
| final String lastModified = sub.getMeta(Subscription.KEY_HTTP_LAST_MODIFIED); |
| if (!TextUtils.isEmpty(lastModified)) |
| { |
| headers.put("If-Modified-Since", lastModified); |
| } |
| final String etag = sub.getMeta(Subscription.KEY_HTTP_ETAG); |