| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 connection.getInputStream(), StandardCharsets.UTF_8))) | 102 connection.getInputStream(), StandardCharsets.UTF_8))) |
| 103 { | 103 { |
| 104 for (int ch = r.read(); ch != -1; ch = r.read()) | 104 for (int ch = r.read(); ch != -1; ch = r.read()) |
| 105 { | 105 { |
| 106 sb.append((char) ch); | 106 sb.append((char) ch); |
| 107 } | 107 } |
| 108 job.responseText = sb.toString(); | 108 job.responseText = sb.toString(); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 public void enqueueDownload(final URL url, final String id, final Map<String,
String> headers) | 112 public void enqueueDownload(final URL url, final String id, final Map<String,
String> headers, |
| 113 final boolean allowMetered) |
| 113 { | 114 { |
| 114 this.lock(); | 115 this.lock(); |
| 115 try | 116 try |
| 116 { | 117 { |
| 117 if (!this.enqueuedIds.contains(id)) | 118 if (!this.enqueuedIds.contains(id)) |
| 118 { | 119 { |
| 119 this.enqueuedIds.add(id); | 120 this.enqueuedIds.add(id); |
| 120 this.downloadJobs.add(new DownloadJob(url, id, headers)); | 121 this.downloadJobs.add(new DownloadJob(url, id, headers, allowMetered)); |
| 121 } | 122 } |
| 122 } | 123 } |
| 123 finally | 124 finally |
| 124 { | 125 { |
| 125 this.unlock(); | 126 this.unlock(); |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 public static Downloader create(final Engine engine) | 130 public static Downloader create(final Engine engine) |
| 130 { | 131 { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 160 try | 161 try |
| 161 { | 162 { |
| 162 if (!this.downloader.downloaderEnabled) | 163 if (!this.downloader.downloaderEnabled) |
| 163 { | 164 { |
| 164 Thread.sleep(30000); | 165 Thread.sleep(30000); |
| 165 continue; | 166 continue; |
| 166 } | 167 } |
| 167 job = this.downloader.downloadJobs.poll(5 * 60, TimeUnit.SECONDS); | 168 job = this.downloader.downloadJobs.poll(5 * 60, TimeUnit.SECONDS); |
| 168 if (job != null) | 169 if (job != null) |
| 169 { | 170 { |
| 170 if (this.downloader.engine.canUseInternet()) | 171 if (this.downloader.engine.canUseInternet(job.allowMetered)) |
| 171 { | 172 { |
| 172 Log.d(TAG, "Downloading '" + job.id + "' using " + job.url); | 173 Log.d(TAG, "Downloading '" + job.id + "' using " + job.url); |
| 173 download(job); | 174 download(job); |
| 174 Log.d(TAG, "Downloading '" + job.id + "' finished with response co
de " | 175 Log.d(TAG, "Downloading '" + job.id + "' finished with response co
de " |
| 175 + job.responseCode); | 176 + job.responseCode); |
| 176 this.downloader.lock(); | 177 this.downloader.lock(); |
| 177 try | 178 try |
| 178 { | 179 { |
| 179 this.downloader.enqueuedIds.remove(job.id); | 180 this.downloader.enqueuedIds.remove(job.id); |
| 180 } | 181 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 } | 235 } |
| 235 } | 236 } |
| 236 Log.d(TAG, "Handler thread finished"); | 237 Log.d(TAG, "Handler thread finished"); |
| 237 } | 238 } |
| 238 } | 239 } |
| 239 | 240 |
| 240 private static class DownloadJob | 241 private static class DownloadJob |
| 241 { | 242 { |
| 242 private final URL url; | 243 private final URL url; |
| 243 private final String id; | 244 private final String id; |
| 245 private final boolean allowMetered; |
| 244 private final HashMap<String, String> headers = new HashMap<>(); | 246 private final HashMap<String, String> headers = new HashMap<>(); |
| 245 private int retryCount = 0; | 247 private int retryCount = 0; |
| 246 | 248 |
| 247 private int responseCode = 0; | 249 private int responseCode = 0; |
| 248 private final HashMap<String, String> responseHeaders = new HashMap<>(); | 250 private final HashMap<String, String> responseHeaders = new HashMap<>(); |
| 249 private String responseText = null; | 251 private String responseText = null; |
| 250 | 252 |
| 251 public DownloadJob(final URL url, final String id, final Map<String, String>
headers) | 253 public DownloadJob(final URL url, final String id, final Map<String, String>
headers, boolean allowMetered) |
| 252 { | 254 { |
| 253 this.url = url; | 255 this.url = url; |
| 254 this.id = id; | 256 this.id = id; |
| 257 this.allowMetered = allowMetered; |
| 255 if (headers != null) | 258 if (headers != null) |
| 256 { | 259 { |
| 257 this.headers.putAll(headers); | 260 this.headers.putAll(headers); |
| 258 } | 261 } |
| 259 } | 262 } |
| 260 } | 263 } |
| 261 } | 264 } |
| OLD | NEW |