]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveUrl.java
def7babd913b4032787ff5e1c9777d46bce7c329
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / SaveUrl.java
1 /*
2  * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.asynctasks;
21
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;
28
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;
33
34 import java.io.BufferedInputStream;
35 import java.io.File;
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;
43 import java.net.URL;
44 import java.text.NumberFormat;
45
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;
50
51     // Define a success string constant.
52     private final String SUCCESS = "Success";
53
54     // Define the class variables.
55     private String filePathString;
56     private String userAgent;
57     private boolean cookiesEnabled;
58     private Snackbar savingFileSnackbar;
59
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);
65
66         // Store the class variables.
67         this.filePathString = filePathString;
68         this.userAgent = userAgent;
69         this.cookiesEnabled = cookiesEnabled;
70     }
71
72     // `onPreExecute()` operates on the UI thread.
73     @Override
74     protected void onPreExecute() {
75         // Get a handle for the activity.
76         Activity activity = activityWeakReference.get();
77
78         // Abort if the activity is gone.
79         if ((activity==null) || activity.isFinishing()) {
80             return;
81         }
82
83         // Get a handle for the no swipe view pager.
84         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
85
86         // Create a saving file snackbar.
87         savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + ":  " + filePathString, Snackbar.LENGTH_INDEFINITE);
88
89         // Display the saving file snackbar.
90         savingFileSnackbar.show();
91     }
92
93     @Override
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();
98
99         // Abort if the activity is gone.
100         if ((activity == null) || activity.isFinishing()) {
101             return null;
102         }
103
104         // Define a save disposition string.
105         String saveDisposition = SUCCESS;
106
107         // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
108         try {
109             // Get the URL from the calling activity.
110             URL url = new URL(urlToSave[0]);
111
112             // Instantiate the proxy helper.
113             ProxyHelper proxyHelper = new ProxyHelper();
114
115             // Get the current proxy.
116             Proxy proxy = proxyHelper.getCurrentProxy(context);
117
118             // Open a connection to the URL.  No data is actually sent at this point.
119             HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
120
121             // Add the user agent to the header property.
122             httpUrlConnection.setRequestProperty("User-Agent", userAgent);
123
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());
128
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);
133                 }
134             }
135
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.
137             try {
138                 // Get the content length header, which causes the connection to the server to be made.
139                 String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
140
141                 // Define the file size long.
142                 long fileSize;
143
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`.
150                     fileSize = -1;
151                 }
152
153                 // Get the response body stream.
154                 InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
155
156                 // Get the file.
157                 File file = new File(filePathString);
158
159                 // Delete the file if it exists.
160                 if (file.exists()) {
161                     //noinspection ResultOfMethodCallIgnored
162                     file.delete();
163                 }
164
165                 // Create a new file.
166                 //noinspection ResultOfMethodCallIgnored
167                 file.createNewFile();
168
169                 // Create an output file stream.
170                 OutputStream outputStream = new FileOutputStream(file);
171
172                 // Initialize the conversion buffer byte array.
173                 byte[] conversionBufferByteArray = new byte[1024];
174
175                 // Initialize the downloaded kilobytes counter.
176                 long downloadedKilobytesCounter = 0;
177
178                 // Define the buffer length variable.
179                 int bufferLength;
180
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);
185
186                     // Increment the downloaded kilobytes counter.
187                     downloadedKilobytesCounter++;
188
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;
193
194                         publishProgress(downloadedKilobytes);
195                     } else {  // The file size is known.
196                         // Calculate the download percentage.
197                         long downloadPercentage = (downloadedKilobytesCounter * 1024 * 100) / fileSize;
198
199                         // Update the download percentage.
200                         publishProgress(downloadPercentage);
201                     }
202                 }
203
204                 // Close the input stream.
205                 inputStream.close();
206
207                 // Close the output stream.
208                 outputStream.close();
209
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);
212
213                 // Add the URI to the media scanner intent.
214                 mediaScannerIntent.setData(Uri.fromFile(file));
215
216                 // Make it so.
217                 activity.sendBroadcast(mediaScannerIntent);
218             } finally {
219                 // Disconnect the HTTP URL connection.
220                 httpUrlConnection.disconnect();
221             }
222         } catch (IOException exception) {
223             // Store the error in the save disposition string.
224             saveDisposition = exception.toString();
225         }
226
227         // Return the save disposition string.
228         return saveDisposition;
229     }
230
231     // `onProgressUpdate()` operates on the UI thread.
232     @Override
233     protected void onProgressUpdate(Long... downloadPercentage) {
234         // Get a handle for the activity.
235         Activity activity = activityWeakReference.get();
236
237         // Abort if the activity is gone.
238         if ((activity == null) || activity.isFinishing()) {
239             return;
240         }
241
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;
246
247             // Format the number of bytes downloaded.
248             String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded);
249
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);
255         }
256     }
257
258     // `onPostExecute()` operates on the UI thread.
259     @Override
260     protected void onPostExecute(String saveDisposition) {
261         // Get a handle for the activity.
262         Activity activity = activityWeakReference.get();
263
264         // Abort if the activity is gone.
265         if ((activity == null) || activity.isFinishing()) {
266             return;
267         }
268
269         // Get a handle for the no swipe view pager.
270         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
271
272         // Dismiss the saving file snackbar.
273         savingFileSnackbar.dismiss();
274
275         // Display a save disposition snackbar.
276         if (saveDisposition.equals(SUCCESS)) {
277             Snackbar.make(noSwipeViewPager, R.string.file_saved, Snackbar.LENGTH_SHORT).show();
278         } else {
279             Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + "  " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();
280         }
281     }
282 }