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

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

Issue 29760569: Issue 6238 - Download/store notifications.json (Closed)
Patch Set: Created May 15, 2018, 9:57 a.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
(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 private static final String TAG = DownloadJobService.class.getSimpleName();
44 private DownloadJobAsyncTask downloadJobAsyncTask = null;
45 private Engine engine = null;
46
47 @Override
48 public void onCreate()
49 {
50 super.onCreate();
51 Log.i(TAG, "DownloadJobService created.");
52 EngineManager.getInstance().retrieveEngine(this, this);
53 }
54
55 @Override
56 public int onStartCommand(final Intent intent, final int flags, final int star tId)
57 {
58 return START_NOT_STICKY;
59 }
60
61 @Override
62 public boolean onStartJob(final JobParameters params)
63 {
64 Log.i(TAG, "Job with id " + params.getJobId() + " started.");
65 this.downloadJobAsyncTask = new DownloadJobAsyncTask()
66 {
67 @Override
68 protected void onPostExecute(final DownloadJob job)
69 {
70 Log.i(DownloadJobService.TAG, "Job with id " + params.getJobId() + " fin ished.");
71 jobFinished(params, false);
72 if (engine != null && job != null)
73 {
74 engine.downloadFinished(job.id, job.responseCode, job.responseText, jo b.headers);
75 }
76 }
77 };
78 try
79 {
80 this.downloadJobAsyncTask.execute(createDownloadJobFromExtras(params.getEx tras()));
81 }
82 catch (final MalformedURLException e)
83 {
84 Log.e(TAG, "Malformed URL, cannot create download.", e);
85 return false;
86 }
87 return true;
88 }
89
90 @Override
91 public boolean onStopJob(JobParameters params)
92 {
93 if (downloadJobAsyncTask != null)
94 {
95 downloadJobAsyncTask.cancel(true);
96 }
97 return true;
98 }
99
100 @Override
101 public void onDestroy()
102 {
103 EngineManager.getInstance().removeOnEngineCreatedCallback(this);
104 Log.i(TAG, "DownloadJobService destroyed.");
105 super.onDestroy();
106 }
107
108 private DownloadJob createDownloadJobFromExtras(final PersistableBundle extras ) throws MalformedURLException
109 {
110 return new DownloadJob(
111 new URL(extras.getString(Notification.KEY_EXTRA_URL)),
112 extras.getString(Notification.KEY_EXTRA_ID),
113 null);
114 }
115
116 @Override
117 public void onEngineCreated(final Engine engine)
118 {
119 this.engine = engine;
120 }
121
122 private static class DownloadJobAsyncTask extends AsyncTask<DownloadJob, Void, DownloadJob>
123 {
124 public static final String TAG = DownloadJobAsyncTask.class.getSimpleName();
125
126 @Override
127 protected DownloadJob doInBackground(final DownloadJob... downloadJob)
128 {
129 final DownloadJob job = downloadJob[0];
130 try
131 {
132 return download(job);
133 }
134 catch (Exception e)
135 {
136 Log.e(TAG, "Error at download: ", e);
137 return null;
138 }
139 }
140
141 private DownloadJob download(final DownloadJob job) throws IOException
142 {
143 final HttpsURLConnection connection = (HttpsURLConnection) job.url.openCon nection();
144 connection.setRequestMethod("GET");
145 for (final Map.Entry<String, String> e : job.headers.entrySet())
146 {
147 connection.addRequestProperty(e.getKey(), e.getValue());
148 }
149 connection.connect();
150 job.responseCode = connection.getResponseCode();
151 job.responseHeaders.clear();
152 job.responseText = null;
153
154 for (int i = 1; ; i++)
155 {
156 final String key = connection.getHeaderFieldKey(i);
157 if (key == null)
158 {
159 break;
160 }
161 final String value = connection.getHeaderField(i);
162 job.responseHeaders.put(key.toLowerCase(), value);
163 }
164
165 final StringBuilder sb = new StringBuilder();
166 try (final BufferedReader r = new BufferedReader(new InputStreamReader(
167 connection.getInputStream(), StandardCharsets.UTF_8)))
168 {
169 for (int ch = r.read(); ch != -1; ch = r.read())
170 {
171 sb.append((char) ch);
172 }
173 job.responseText = sb.toString();
174 }
175 return job;
176 }
177 }
178
179 private static class DownloadJob
180 {
181 private final URL url;
182 private final String id;
183 private final HashMap<String, String> headers = new HashMap<>();
184
185 private int responseCode;
186 private final HashMap<String, String> responseHeaders = new HashMap<>();
187 private String responseText;
188
189 DownloadJob(final URL url, final String id, final Map<String, String> header s)
190 {
191 this.url = url;
192 this.id = id;
193 if (headers != null)
194 {
195 this.headers.putAll(headers);
196 }
197 }
198 }
199 }
OLDNEW

Powered by Google App Engine
This is Rietveld