]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveUrl.java
6a4671bc7e20b478c9ed4d014efaf0cd3f86dcff
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / SaveUrl.java
1 /*
2  * Copyright © 2020-2021 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.database.Cursor;
25 import android.net.Uri;
26 import android.os.AsyncTask;
27 import android.os.Build;
28 import android.provider.OpenableColumns;
29 import android.util.Base64;
30 import android.webkit.CookieManager;
31
32 import com.google.android.material.snackbar.Snackbar;
33 import com.stoutner.privacybrowser.R;
34 import com.stoutner.privacybrowser.helpers.ProxyHelper;
35 import com.stoutner.privacybrowser.views.NoSwipeViewPager;
36
37 import java.io.BufferedInputStream;
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     // Declare the weak references.
48     private final WeakReference<Context> contextWeakReference;
49     private final WeakReference<Activity> activityWeakReference;
50
51     // Define a success string constant.
52     private final String SUCCESS = "Success";
53
54     // Declare the class variables.
55     private final Uri fileUri;
56     private final String userAgent;
57     private final boolean cookiesEnabled;
58     private Snackbar savingFileSnackbar;
59     private long fileSize;
60     private String formattedFileSize;
61     private final String fileNameString;
62
63     // The public constructor.
64     public SaveUrl(Context context, Activity activity, Uri fileUri, String userAgent, boolean cookiesEnabled) {
65         // Populate weak references to the calling context and activity.
66         contextWeakReference = new WeakReference<>(context);
67         activityWeakReference = new WeakReference<>(activity);
68
69         // Store the class variables.
70         this.fileUri = fileUri;
71         this.userAgent = userAgent;
72         this.cookiesEnabled = cookiesEnabled;
73
74         // Query the exact file name if the API >= 26.
75         if (Build.VERSION.SDK_INT >= 26) {
76             // Get a cursor from the content resolver.
77             Cursor contentResolverCursor = activity.getContentResolver().query(fileUri, null, null, null);
78
79             // Move to the first row.
80             contentResolverCursor.moveToFirst();
81
82             // Get the file name from the cursor.
83             fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
84
85             // Close the cursor.
86             contentResolverCursor.close();
87         } else {
88             // Use the file URI last path segment as the file name string.
89             fileNameString = fileUri.getLastPathSegment();
90         }
91     }
92
93     // `onPreExecute()` operates on the UI thread.
94     @Override
95     protected void onPreExecute() {
96         // Get a handle for the activity.
97         Activity activity = activityWeakReference.get();
98
99         // Abort if the activity is gone.
100         if ((activity==null) || activity.isFinishing()) {
101             return;
102         }
103
104         // Get a handle for the no swipe view pager.
105         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
106
107         // Create a saving file snackbar.
108         savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + "  0%  -  " + fileNameString, Snackbar.LENGTH_INDEFINITE);
109
110         // Display the saving file snackbar.
111         savingFileSnackbar.show();
112     }
113
114     @Override
115     protected String doInBackground(String... urlToSave) {
116         // Get handles for the context and activity.
117         Context context = contextWeakReference.get();
118         Activity activity = activityWeakReference.get();
119
120         // Abort if the activity is gone.
121         if ((activity == null) || activity.isFinishing()) {
122             return null;
123         }
124
125         // Define a save disposition string.
126         String saveDisposition = SUCCESS;
127
128         // Get the URL string.
129         String urlString = urlToSave[0];
130
131         try {
132             // Open an output stream.
133             OutputStream outputStream = activity.getContentResolver().openOutputStream(fileUri);
134
135             // Save the URL.
136             if (urlString.startsWith("data:")) {  // The URL contains the entire data of an image.
137                 // Get the Base64 data, which begins after a `,`.
138                 String base64DataString = urlString.substring(urlString.indexOf(",") + 1);
139
140                 // Decode the Base64 string to a byte array.
141                 byte[] base64DecodedDataByteArray = Base64.decode(base64DataString, Base64.DEFAULT);
142
143                 // Write the Base64 byte array to the output stream.
144                 outputStream.write(base64DecodedDataByteArray);
145             } else {  // The URL points to the data location on the internet.
146                 // Get the URL from the calling activity.
147                 URL url = new URL(urlString);
148
149                 // Instantiate the proxy helper.
150                 ProxyHelper proxyHelper = new ProxyHelper();
151
152                 // Get the current proxy.
153                 Proxy proxy = proxyHelper.getCurrentProxy(context);
154
155                 // Open a connection to the URL.  No data is actually sent at this point.
156                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
157
158                 // Add the user agent to the header property.
159                 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
160
161                 // Add the cookies if they are enabled.
162                 if (cookiesEnabled) {
163                     // Get the cookies for the current domain.
164                     String cookiesString = CookieManager.getInstance().getCookie(url.toString());
165
166                     // Only add the cookies if they are not null.
167                     if (cookiesString != null) {
168                         // Add the cookies to the header property.
169                         httpUrlConnection.setRequestProperty("Cookie", cookiesString);
170                     }
171                 }
172
173                 // 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.
174                 try {
175                     // Get the content length header, which causes the connection to the server to be made.
176                     String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
177
178                     // Make sure the content length isn't null.
179                     if (contentLengthString != null) {  // The content length isn't null.
180                         // Convert the content length to an long.
181                         fileSize = Long.parseLong(contentLengthString);
182
183                         // Format the file size for display.
184                         formattedFileSize = NumberFormat.getInstance().format(fileSize);
185                     } else {  // The content length is null.
186                         // Set the file size to be `-1`.
187                         fileSize = -1;
188                     }
189
190                     // Get the response body stream.
191                     InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
192
193                     // Initialize the conversion buffer byte array.
194                     // This is set to a megabyte so that frequent updating of the snackbar doesn't freeze the interface on download.  <https://redmine.stoutner.com/issues/709>
195                     byte[] conversionBufferByteArray = new byte[1048576];
196
197                     // Initialize the downloaded kilobytes counter.
198                     long downloadedKilobytesCounter = 0;
199
200                     // Define the buffer length variable.
201                     int bufferLength;
202
203                     // 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.
204                     while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) {  // Proceed while the amount of data stored in the buffer in > 0.
205                         // Write the contents of the conversion buffer to the file output stream.
206                         outputStream.write(conversionBufferByteArray, 0, bufferLength);
207
208                         // Update the downloaded kilobytes counter.
209                         downloadedKilobytesCounter = downloadedKilobytesCounter + bufferLength;
210
211                         // Update the file download progress snackbar.
212                         publishProgress(downloadedKilobytesCounter);
213                     }
214
215                     // Close the input stream.
216                     inputStream.close();
217                 } finally {
218                     // Disconnect the HTTP URL connection.
219                     httpUrlConnection.disconnect();
220                 }
221             }
222
223             // Close the output stream.
224             outputStream.close();
225         } catch (Exception exception) {
226             // Store the error in the save disposition string.
227             saveDisposition = exception.toString();
228         }
229
230         // Return the save disposition string.
231         return saveDisposition;
232     }
233
234     // `onProgressUpdate()` operates on the UI thread.
235     @Override
236     protected void onProgressUpdate(Long... numberOfBytesDownloaded) {
237         // Get a handle for the activity.
238         Activity activity = activityWeakReference.get();
239
240         // Abort if the activity is gone.
241         if ((activity == null) || activity.isFinishing()) {
242             return;
243         }
244
245         // Format the number of bytes downloaded.
246         String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded[0]);
247
248         // Check to see if the file size is known.
249         if (fileSize == -1) {  // The size of the download file is not known.
250             // Update the snackbar.
251             savingFileSnackbar.setText(activity.getString(R.string.saving_file) + "  " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + "  -  " + fileNameString);
252         } else {  // The size of the download file is known.
253             // Calculate the download percentage.
254             long downloadPercentage = (numberOfBytesDownloaded[0] * 100) / fileSize;
255
256             // Update the snackbar.
257             savingFileSnackbar.setText(activity.getString(R.string.saving_file) + "  " + downloadPercentage + "%  -  " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " / " +
258                     formattedFileSize + " " + activity.getString(R.string.bytes) + "  -  " + fileNameString);
259         }
260     }
261
262     // `onPostExecute()` operates on the UI thread.
263     @Override
264     protected void onPostExecute(String saveDisposition) {
265         // Get handles for the context and activity.
266         Activity activity = activityWeakReference.get();
267
268         // Abort if the activity is gone.
269         if ((activity == null) || activity.isFinishing()) {
270             return;
271         }
272
273         // Get a handle for the no swipe view pager.
274         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
275
276         // Dismiss the saving file snackbar.
277         savingFileSnackbar.dismiss();
278
279         // Display a save disposition snackbar.
280         if (saveDisposition.equals(SUCCESS)) {
281             // Display the file saved snackbar.
282             Snackbar.make(noSwipeViewPager, activity.getString(R.string.file_saved) + "  " + fileNameString, Snackbar.LENGTH_LONG).show();
283         } else {
284             // Display the file saving error.
285             Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + "  " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();
286         }
287     }
288 }