2 * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.asynctasks;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.os.AsyncTask;
27 import android.webkit.CookieManager;
29 import com.google.android.material.snackbar.Snackbar;
30 import com.stoutner.privacybrowser.R;
31 import com.stoutner.privacybrowser.helpers.ProxyHelper;
32 import com.stoutner.privacybrowser.views.NoSwipeViewPager;
34 import java.io.BufferedInputStream;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40 import java.lang.ref.WeakReference;
41 import java.net.HttpURLConnection;
42 import java.net.Proxy;
44 import java.text.NumberFormat;
46 public class SaveUrl extends AsyncTask<String, Long, String> {
47 // Define a weak references for the calling context and activity.
48 private WeakReference<Context> contextWeakReference;
49 private WeakReference<Activity> activityWeakReference;
51 // Define a success string constant.
52 private final String SUCCESS = "Success";
54 // Define the class variables.
55 private String filePathString;
56 private String userAgent;
57 private boolean cookiesEnabled;
58 private Snackbar savingFileSnackbar;
60 // The public constructor.
61 public SaveUrl(Context context, Activity activity, String filePathString, String userAgent, boolean cookiesEnabled) {
62 // Populate weak references to the calling context and activity.
63 contextWeakReference = new WeakReference<>(context);
64 activityWeakReference = new WeakReference<>(activity);
66 // Store the class variables.
67 this.filePathString = filePathString;
68 this.userAgent = userAgent;
69 this.cookiesEnabled = cookiesEnabled;
72 // `onPreExecute()` operates on the UI thread.
74 protected void onPreExecute() {
75 // Get a handle for the activity.
76 Activity activity = activityWeakReference.get();
78 // Abort if the activity is gone.
79 if ((activity==null) || activity.isFinishing()) {
83 // Get a handle for the no swipe view pager.
84 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
86 // Create a saving file snackbar.
87 savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + ": " + filePathString, Snackbar.LENGTH_INDEFINITE);
89 // Display the saving file snackbar.
90 savingFileSnackbar.show();
94 protected String doInBackground(String... urlToSave) {
95 // Get a handle for the context and activity.
96 Context context = contextWeakReference.get();
97 Activity activity = activityWeakReference.get();
99 // Abort if the activity is gone.
100 if ((activity == null) || activity.isFinishing()) {
104 // Define a save disposition string.
105 String saveDisposition = SUCCESS;
107 // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
109 // Get the URL from the calling activity.
110 URL url = new URL(urlToSave[0]);
112 // Instantiate the proxy helper.
113 ProxyHelper proxyHelper = new ProxyHelper();
115 // Get the current proxy.
116 Proxy proxy = proxyHelper.getCurrentProxy(context);
118 // Open a connection to the URL. No data is actually sent at this point.
119 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
121 // Add the user agent to the header property.
122 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
124 // Add the cookies if they are enabled.
125 if (cookiesEnabled) {
126 // Get the cookies for the current domain.
127 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
129 // Only add the cookies if they are not null.
130 if (cookiesString != null) {
131 // Add the cookies to the header property.
132 httpUrlConnection.setRequestProperty("Cookie", cookiesString);
136 // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block.
138 // Get the content length header, which causes the connection to the server to be made.
139 String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
141 // Define the file size long.
144 // Make sure the content length isn't null.
145 if (contentLengthString != null) { // The content length isn't null.
146 // Convert the content length to an long.
147 fileSize = Long.parseLong(contentLengthString);
148 } else { // The content length is null.
149 // Set the file size to be `-1`.
153 // Get the response body stream.
154 InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
157 File file = new File(filePathString);
159 // Delete the file if it exists.
161 //noinspection ResultOfMethodCallIgnored
165 // Create a new file.
166 //noinspection ResultOfMethodCallIgnored
167 file.createNewFile();
169 // Create an output file stream.
170 OutputStream outputStream = new FileOutputStream(file);
172 // Initialize the conversion buffer byte array.
173 byte[] conversionBufferByteArray = new byte[1024];
175 // Initialize the downloaded kilobytes counter.
176 long downloadedKilobytesCounter = 0;
178 // Define the buffer length variable.
181 // Attempt to read data from the input stream and store it in the output stream. Also store the amount of data read in the buffer length variable.
182 while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) { // Proceed while the amount of data stored in the buffer in > 0.
183 // Write the contents of the conversion buffer to the output stream.
184 outputStream.write(conversionBufferByteArray, 0, bufferLength);
186 // Increment the downloaded kilobytes counter.
187 downloadedKilobytesCounter++;
189 // Update the file download progress snackbar.
190 if (fileSize == -1) { // The file size is unknown.
191 // Convert the downloaded kilobytes counter to a negative number
192 long downloadedKilobytes = 0 - downloadedKilobytesCounter;
194 publishProgress(downloadedKilobytes);
195 } else { // The file size is known.
196 // Calculate the download percentage.
197 long downloadPercentage = (downloadedKilobytesCounter * 1024 * 100) / fileSize;
199 // Update the download percentage.
200 publishProgress(downloadPercentage);
204 // Close the input stream.
207 // Close the output stream.
208 outputStream.close();
210 // Define a media scanner intent, which adds items like pictures to Android's recent file list.
211 Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
213 // Add the URI to the media scanner intent.
214 mediaScannerIntent.setData(Uri.fromFile(file));
217 activity.sendBroadcast(mediaScannerIntent);
219 // Disconnect the HTTP URL connection.
220 httpUrlConnection.disconnect();
222 } catch (IOException exception) {
223 // Store the error in the save disposition string.
224 saveDisposition = exception.toString();
227 // Return the save disposition string.
228 return saveDisposition;
231 // `onProgressUpdate()` operates on the UI thread.
233 protected void onProgressUpdate(Long... downloadPercentage) {
234 // Get a handle for the activity.
235 Activity activity = activityWeakReference.get();
237 // Abort if the activity is gone.
238 if ((activity == null) || activity.isFinishing()) {
242 // Check to see if a download percentage has been calculated.
243 if (downloadPercentage[0] < 0) { // There is no download percentage. The negative number represents the raw downloaded kilobytes.
244 // Calculate the number of bytes downloaded.
245 long numberOfBytesDownloaded = (0 - downloadPercentage[0]) * 1024;
247 // Format the number of bytes downloaded.
248 String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded);
250 // Update the snackbar.
251 savingFileSnackbar.setText(activity.getString(R.string.saving_file) + ": " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " - " + filePathString);
252 } else { // There is a download percentage.
253 // Update the snackbar.
254 savingFileSnackbar.setText(activity.getString(R.string.saving_file) + ": " + downloadPercentage[0] + "% - " + filePathString);
258 // `onPostExecute()` operates on the UI thread.
260 protected void onPostExecute(String saveDisposition) {
261 // Get a handle for the activity.
262 Activity activity = activityWeakReference.get();
264 // Abort if the activity is gone.
265 if ((activity == null) || activity.isFinishing()) {
269 // Get a handle for the no swipe view pager.
270 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
272 // Dismiss the saving file snackbar.
273 savingFileSnackbar.dismiss();
275 // Display a save disposition snackbar.
276 if (saveDisposition.equals(SUCCESS)) {
277 Snackbar.make(noSwipeViewPager, R.string.file_saved, Snackbar.LENGTH_SHORT).show();
279 Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + " " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();