2 * Copyright © 2020-2021 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.net.Uri;
25 import android.os.AsyncTask;
26 import android.util.Base64;
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;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.lang.ref.WeakReference;
38 import java.net.HttpURLConnection;
39 import java.net.Proxy;
41 import java.text.NumberFormat;
43 public class SaveUrl extends AsyncTask<String, Long, String> {
44 // Define a weak references.
45 private final WeakReference<Context> contextWeakReference;
46 private final WeakReference<Activity> activityWeakReference;
48 // Define a success string constant.
49 private final String SUCCESS = "Success";
51 // Define the class variables.
52 private final String filePathString;
53 private final String userAgent;
54 private final boolean cookiesEnabled;
55 private Snackbar savingFileSnackbar;
57 // The public constructor.
58 public SaveUrl(Context context, Activity activity, String filePathString, String userAgent, boolean cookiesEnabled) {
59 // Populate weak references to the calling context and activity.
60 contextWeakReference = new WeakReference<>(context);
61 activityWeakReference = new WeakReference<>(activity);
63 // Store the class variables.
64 this.filePathString = filePathString;
65 this.userAgent = userAgent;
66 this.cookiesEnabled = cookiesEnabled;
69 // `onPreExecute()` operates on the UI thread.
71 protected void onPreExecute() {
72 // Get a handle for the activity.
73 Activity activity = activityWeakReference.get();
75 // Abort if the activity is gone.
76 if ((activity==null) || activity.isFinishing()) {
80 // Get a handle for the no swipe view pager.
81 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
83 // Create a saving file snackbar.
84 savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + " 0% - " + filePathString, Snackbar.LENGTH_INDEFINITE);
86 // Display the saving file snackbar.
87 savingFileSnackbar.show();
91 protected String doInBackground(String... urlToSave) {
92 // Get handles for the context and activity.
93 Context context = contextWeakReference.get();
94 Activity activity = activityWeakReference.get();
96 // Abort if the activity is gone.
97 if ((activity == null) || activity.isFinishing()) {
101 // Define a save disposition string.
102 String saveDisposition = SUCCESS;
105 // Open an output stream.
106 OutputStream outputStream = activity.getContentResolver().openOutputStream(Uri.parse(filePathString));
109 if (urlToSave[0].startsWith("data:")) { // The URL contains the entire data of an image.
110 // Get the Base64 data, which begins after a `,`.
111 String base64DataString = urlToSave[0].substring(urlToSave[0].indexOf(",") + 1);
113 // Decode the Base64 string to a byte array.
114 byte[] base64DecodedDataByteArray = Base64.decode(base64DataString, Base64.DEFAULT);
116 // Write the Base64 byte array to the output stream.
117 outputStream.write(base64DecodedDataByteArray);
118 } else { // The URL points to the data location on the internet.
119 // Get the URL from the calling activity.
120 URL url = new URL(urlToSave[0]);
122 // Instantiate the proxy helper.
123 ProxyHelper proxyHelper = new ProxyHelper();
125 // Get the current proxy.
126 Proxy proxy = proxyHelper.getCurrentProxy(context);
128 // Open a connection to the URL. No data is actually sent at this point.
129 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
131 // Add the user agent to the header property.
132 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
134 // Add the cookies if they are enabled.
135 if (cookiesEnabled) {
136 // Get the cookies for the current domain.
137 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
139 // Only add the cookies if they are not null.
140 if (cookiesString != null) {
141 // Add the cookies to the header property.
142 httpUrlConnection.setRequestProperty("Cookie", cookiesString);
146 // 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.
148 // Get the content length header, which causes the connection to the server to be made.
149 String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
151 // Define the file size long.
154 // Make sure the content length isn't null.
155 if (contentLengthString != null) { // The content length isn't null.
156 // Convert the content length to an long.
157 fileSize = Long.parseLong(contentLengthString);
158 } else { // The content length is null.
159 // Set the file size to be `-1`.
163 // Get the response body stream.
164 InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
166 // Initialize the conversion buffer byte array.
167 byte[] conversionBufferByteArray = new byte[1024];
169 // Initialize the downloaded kilobytes counter.
170 long downloadedKilobytesCounter = 0;
172 // Define the buffer length variable.
175 // 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.
176 while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) { // Proceed while the amount of data stored in the buffer in > 0.
177 // Write the contents of the conversion buffer to the file output stream.
178 outputStream.write(conversionBufferByteArray, 0, bufferLength);
180 // Update the file download progress snackbar.
181 if (fileSize == -1) { // The file size is unknown.
182 // Negatively update the downloaded kilobytes counter.
183 downloadedKilobytesCounter = downloadedKilobytesCounter - bufferLength;
185 publishProgress(downloadedKilobytesCounter);
186 } else { // The file size is known.
187 // Update the downloaded kilobytes counter.
188 downloadedKilobytesCounter = downloadedKilobytesCounter + bufferLength;
190 // Calculate the download percentage.
191 long downloadPercentage = (downloadedKilobytesCounter * 100) / fileSize;
193 // Update the download percentage.
194 publishProgress(downloadPercentage);
198 // Close the input stream.
201 // Disconnect the HTTP URL connection.
202 httpUrlConnection.disconnect();
206 // Flush the output stream.
207 outputStream.flush();
209 // Close the output stream.
210 outputStream.close();
211 } catch (Exception exception) {
212 // Store the error in the save disposition string.
213 saveDisposition = exception.toString();
216 // Return the save disposition string.
217 return saveDisposition;
220 // `onProgressUpdate()` operates on the UI thread.
222 protected void onProgressUpdate(Long... downloadPercentage) {
223 // Get a handle for the activity.
224 Activity activity = activityWeakReference.get();
226 // Abort if the activity is gone.
227 if ((activity == null) || activity.isFinishing()) {
231 // Check to see if a download percentage has been calculated.
232 if (downloadPercentage[0] < 0) { // There is no download percentage. The negative number represents the raw downloaded kilobytes.
233 // Calculate the number of bytes downloaded. When the `downloadPercentage` is negative, it is actually the raw number of kilobytes downloaded.
234 long numberOfBytesDownloaded = - downloadPercentage[0];
236 // Format the number of bytes downloaded.
237 String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded);
239 // Update the snackbar.
240 savingFileSnackbar.setText(activity.getString(R.string.saving_file) + " " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " - " + filePathString);
241 } else { // There is a download percentage.
242 // Update the snackbar.
243 savingFileSnackbar.setText(activity.getString(R.string.saving_file) + " " + downloadPercentage[0] + "% - " + filePathString);
247 // `onPostExecute()` operates on the UI thread.
249 protected void onPostExecute(String saveDisposition) {
250 // Get handles for the context and activity.
251 Activity activity = activityWeakReference.get();
253 // Abort if the activity is gone.
254 if ((activity == null) || activity.isFinishing()) {
258 // Get a handle for the no swipe view pager.
259 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
261 // Dismiss the saving file snackbar.
262 savingFileSnackbar.dismiss();
264 // Display a save disposition snackbar.
265 if (saveDisposition.equals(SUCCESS)) {
266 // Display the file saved snackbar.
267 Snackbar.make(noSwipeViewPager, activity.getString(R.string.file_saved) + " " + filePathString, Snackbar.LENGTH_LONG).show();
269 // Display the file saving error.
270 Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + " " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();