| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 package org.adblockplus.sbrowser.contentblocker.engine; | |
| 19 | |
| 20 import java.io.BufferedReader; | |
| 21 import java.io.IOException; | |
| 22 import java.io.InputStreamReader; | |
| 23 import java.net.MalformedURLException; | |
| 24 import java.net.URL; | |
| 25 import java.nio.charset.StandardCharsets; | |
| 26 import java.util.HashMap; | |
| 27 import java.util.Map; | |
| 28 | |
| 29 import android.app.job.JobParameters; | |
| 30 import android.app.job.JobService; | |
| 31 import android.content.Intent; | |
| 32 import android.os.AsyncTask; | |
| 33 import android.os.PersistableBundle; | |
| 34 import android.util.Log; | |
| 35 | |
| 36 import javax.net.ssl.HttpsURLConnection; | |
| 37 | |
| 38 /** | |
| 39 * JobService that handles download jobs | |
| 40 */ | |
| 41 public class DownloadJobService extends JobService implements EngineManager.OnEn gineCreatedCallback | |
| 42 { | |
| 43 | |
|
anton
2018/05/03 07:23:20
is empty line required here?
jens
2018/05/03 07:44:37
Acknowledged.
| |
| 44 static final String TAG = DownloadJobService.class.getSimpleName(); | |
|
anton
2018/05/03 07:23:20
shouldn't it be `private`?
jens
2018/05/03 07:44:37
Acknowledged.
| |
| 45 private DownloadJobAsyncTask downloadJobAsyncTask = null; | |
| 46 private Engine engine; | |
|
anton
2018/05/03 07:23:20
i think `engine` should be initialized as `null` t
jens
2018/05/03 07:44:37
Acknowledged.
| |
| 47 | |
| 48 @Override | |
| 49 public void onCreate() | |
| 50 { | |
| 51 super.onCreate(); | |
| 52 Log.i(TAG, "DownloadJobService created."); | |
| 53 EngineManager.getInstance().retrieveEngine(this, this); | |
| 54 } | |
| 55 | |
| 56 @Override | |
| 57 public int onStartCommand(final Intent intent, final int flags, final int star tId) | |
| 58 { | |
| 59 return START_NOT_STICKY; | |
| 60 } | |
| 61 | |
| 62 @Override | |
| 63 public boolean onStartJob(final JobParameters params) | |
| 64 { | |
| 65 Log.i(TAG, "Job with id " + params.getJobId() + " started."); | |
| 66 this.downloadJobAsyncTask = new DownloadJobAsyncTask() | |
| 67 { | |
| 68 @Override | |
| 69 protected void onPostExecute(final DownloadJob job) | |
| 70 { | |
| 71 Log.i(DownloadJobService.TAG, "Job with id " + params.getJobId() + " fin ished."); | |
| 72 jobFinished(params, false); | |
| 73 if (engine != null && job != null) | |
| 74 { | |
| 75 engine.downloadFinished(job.id, job.responseCode, job.responseText, jo b.headers); | |
| 76 } | |
| 77 } | |
| 78 }; | |
| 79 try | |
| 80 { | |
| 81 this.downloadJobAsyncTask.execute(createDownloadJobFromExtras(params.getEx tras())); | |
| 82 } | |
| 83 catch (final MalformedURLException e) | |
| 84 { | |
| 85 Log.e(TAG, "Malformed URL, cannot create download.", e); | |
| 86 return false; | |
| 87 } | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 @Override | |
| 92 public boolean onStopJob(JobParameters params) | |
| 93 { | |
| 94 if (downloadJobAsyncTask != null) | |
| 95 { | |
| 96 downloadJobAsyncTask.cancel(true); | |
| 97 } | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 @Override | |
| 102 public void onDestroy() | |
| 103 { | |
| 104 EngineManager.getInstance().removeOnEngineCreatedCallback(this); | |
| 105 Log.i(TAG, "DownloadJobService destroyed."); | |
| 106 super.onDestroy(); | |
| 107 } | |
| 108 | |
| 109 private DownloadJob createDownloadJobFromExtras(final PersistableBundle extras ) throws MalformedURLException | |
| 110 { | |
| 111 return new DownloadJob( | |
| 112 new URL(extras.getString(Notification.KEY_EXTRA_URL)), | |
| 113 extras.getString(Notification.KEY_EXTRA_ID), | |
| 114 null); | |
| 115 } | |
| 116 | |
| 117 @Override | |
| 118 public void onEngineCreated(final Engine engine) | |
| 119 { | |
| 120 this.engine = engine; | |
| 121 } | |
| 122 | |
| 123 private static class DownloadJobAsyncTask extends AsyncTask<DownloadJob, Void, DownloadJob> | |
| 124 { | |
| 125 public static final String TAG = DownloadJobAsyncTask.class.getSimpleName(); | |
| 126 | |
| 127 @Override | |
| 128 protected DownloadJob doInBackground(final DownloadJob... downloadJob) | |
| 129 { | |
| 130 final DownloadJob job = downloadJob[0]; | |
| 131 try | |
| 132 { | |
| 133 return download(job); | |
| 134 } | |
| 135 catch (Exception e) | |
| 136 { | |
| 137 Log.e(TAG, "Error at download: ", e); | |
| 138 return null; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 private DownloadJob download(final DownloadJob job) throws IOException | |
| 143 { | |
| 144 final HttpsURLConnection connection = (HttpsURLConnection) job.url.openCon nection(); | |
| 145 connection.setRequestMethod("GET"); | |
| 146 for (final Map.Entry<String, String> e : job.headers.entrySet()) | |
| 147 { | |
| 148 connection.addRequestProperty(e.getKey(), e.getValue()); | |
| 149 } | |
| 150 connection.connect(); | |
| 151 job.responseCode = connection.getResponseCode(); | |
| 152 job.responseHeaders.clear(); | |
| 153 job.responseText = null; | |
| 154 | |
| 155 for (int i = 1; ; i++) | |
| 156 { | |
| 157 final String key = connection.getHeaderFieldKey(i); | |
| 158 if (key == null) | |
| 159 { | |
| 160 break; | |
| 161 } | |
| 162 final String value = connection.getHeaderField(i); | |
| 163 job.responseHeaders.put(key.toLowerCase(), value); | |
| 164 } | |
| 165 | |
| 166 final StringBuilder sb = new StringBuilder(); | |
| 167 try (final BufferedReader r = new BufferedReader(new InputStreamReader( | |
| 168 connection.getInputStream(), StandardCharsets.UTF_8))) | |
| 169 { | |
| 170 for (int ch = r.read(); ch != -1; ch = r.read()) | |
| 171 { | |
| 172 sb.append((char) ch); | |
| 173 } | |
| 174 job.responseText = sb.toString(); | |
| 175 } | |
| 176 return job; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 private static class DownloadJob | |
| 181 { | |
| 182 private final URL url; | |
| 183 private final String id; | |
| 184 private final HashMap<String, String> headers = new HashMap<>(); | |
| 185 | |
| 186 private int responseCode = 0; | |
| 187 private final HashMap<String, String> responseHeaders = new HashMap<>(); | |
| 188 private String responseText = null; | |
| 189 | |
| 190 DownloadJob(final URL url, final String id, final Map<String, String> header s) | |
| 191 { | |
| 192 this.url = url; | |
| 193 this.id = id; | |
| 194 if (headers != null) | |
| 195 { | |
| 196 this.headers.putAll(headers); | |
| 197 } | |
| 198 } | |
| 199 } | |
| 200 } | |
| OLD | NEW |