]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveUrl.java
Redesign file access to work with the scoped storage. https://redmine.stoutner.com...
[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.net.Uri;
25 import android.os.AsyncTask;
26 import android.util.Base64;
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.InputStream;
36 import java.io.OutputStream;
37 import java.lang.ref.WeakReference;
38 import java.net.HttpURLConnection;
39 import java.net.Proxy;
40 import java.net.URL;
41 import java.text.NumberFormat;
42
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;
47
48     // Define a success string constant.
49     private final String SUCCESS = "Success";
50
51     // Define the class variables.
52     private final String filePathString;
53     private final String userAgent;
54     private final boolean cookiesEnabled;
55     private Snackbar savingFileSnackbar;
56
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);
62
63         // Store the class variables.
64         this.filePathString = filePathString;
65         this.userAgent = userAgent;
66         this.cookiesEnabled = cookiesEnabled;
67     }
68
69     // `onPreExecute()` operates on the UI thread.
70     @Override
71     protected void onPreExecute() {
72         // Get a handle for the activity.
73         Activity activity = activityWeakReference.get();
74
75         // Abort if the activity is gone.
76         if ((activity==null) || activity.isFinishing()) {
77             return;
78         }
79
80         // Get a handle for the no swipe view pager.
81         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
82
83         // Create a saving file snackbar.
84         savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + "  0% - " + filePathString, Snackbar.LENGTH_INDEFINITE);
85
86         // Display the saving file snackbar.
87         savingFileSnackbar.show();
88     }
89
90     @Override
91     protected String doInBackground(String... urlToSave) {
92         // Get handles for the context and activity.
93         Context context = contextWeakReference.get();
94         Activity activity = activityWeakReference.get();
95
96         // Abort if the activity is gone.
97         if ((activity == null) || activity.isFinishing()) {
98             return null;
99         }
100
101         // Define a save disposition string.
102         String saveDisposition = SUCCESS;
103
104         try {
105             // Open an output stream.
106             OutputStream outputStream = activity.getContentResolver().openOutputStream(Uri.parse(filePathString));
107
108             // Save the URL.
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);
112
113                 // Decode the Base64 string to a byte array.
114                 byte[] base64DecodedDataByteArray = Base64.decode(base64DataString, Base64.DEFAULT);
115
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]);
121
122                 // Instantiate the proxy helper.
123                 ProxyHelper proxyHelper = new ProxyHelper();
124
125                 // Get the current proxy.
126                 Proxy proxy = proxyHelper.getCurrentProxy(context);
127
128                 // Open a connection to the URL.  No data is actually sent at this point.
129                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
130
131                 // Add the user agent to the header property.
132                 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
133
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());
138
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);
143                     }
144                 }
145
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.
147                 try {
148                     // Get the content length header, which causes the connection to the server to be made.
149                     String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
150
151                     // Define the file size long.
152                     long fileSize;
153
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`.
160                         fileSize = -1;
161                     }
162
163                     // Get the response body stream.
164                     InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
165
166                     // Initialize the conversion buffer byte array.
167                     byte[] conversionBufferByteArray = new byte[1024];
168
169                     // Initialize the downloaded kilobytes counter.
170                     long downloadedKilobytesCounter = 0;
171
172                     // Define the buffer length variable.
173                     int bufferLength;
174
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);
179
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;
184
185                             publishProgress(downloadedKilobytesCounter);
186                         } else {  // The file size is known.
187                             // Update the downloaded kilobytes counter.
188                             downloadedKilobytesCounter = downloadedKilobytesCounter + bufferLength;
189
190                             // Calculate the download percentage.
191                             long downloadPercentage = (downloadedKilobytesCounter * 100) / fileSize;
192
193                             // Update the download percentage.
194                             publishProgress(downloadPercentage);
195                         }
196                     }
197
198                     // Close the input stream.
199                     inputStream.close();
200                 } finally {
201                     // Disconnect the HTTP URL connection.
202                     httpUrlConnection.disconnect();
203                 }
204             }
205
206             // Flush the output stream.
207             outputStream.flush();
208
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();
214         }
215
216         // Return the save disposition string.
217         return saveDisposition;
218     }
219
220     // `onProgressUpdate()` operates on the UI thread.
221     @Override
222     protected void onProgressUpdate(Long... downloadPercentage) {
223         // Get a handle for the activity.
224         Activity activity = activityWeakReference.get();
225
226         // Abort if the activity is gone.
227         if ((activity == null) || activity.isFinishing()) {
228             return;
229         }
230
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];
235
236             // Format the number of bytes downloaded.
237             String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded);
238
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);
244         }
245     }
246
247     // `onPostExecute()` operates on the UI thread.
248     @Override
249     protected void onPostExecute(String saveDisposition) {
250         // Get handles for the context and activity.
251         Activity activity = activityWeakReference.get();
252
253         // Abort if the activity is gone.
254         if ((activity == null) || activity.isFinishing()) {
255             return;
256         }
257
258         // Get a handle for the no swipe view pager.
259         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
260
261         // Dismiss the saving file snackbar.
262         savingFileSnackbar.dismiss();
263
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();
268         } else {
269             // Display the file saving error.
270             Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + "  " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();
271         }
272     }
273 }