]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveUrl.java
Add share, copy, and save options to About > Version. https://redmine.stoutner.com...
[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.ContentResolver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.AsyncTask;
28 import android.os.Build;
29 import android.view.View;
30 import android.webkit.CookieManager;
31 import android.webkit.MimeTypeMap;
32
33 import androidx.core.content.FileProvider;
34
35 import com.google.android.material.snackbar.Snackbar;
36 import com.stoutner.privacybrowser.R;
37 import com.stoutner.privacybrowser.helpers.ProxyHelper;
38 import com.stoutner.privacybrowser.views.NoSwipeViewPager;
39
40 import java.io.BufferedInputStream;
41 import java.io.File;
42 import java.io.FileOutputStream;
43 import java.io.InputStream;
44 import java.io.OutputStream;
45 import java.lang.ref.WeakReference;
46 import java.net.HttpURLConnection;
47 import java.net.Proxy;
48 import java.net.URL;
49 import java.text.NumberFormat;
50
51 public class SaveUrl extends AsyncTask<String, Long, String> {
52     // Define a weak references.
53     private WeakReference<Context> contextWeakReference;
54     private WeakReference<Activity> activityWeakReference;
55
56     // Define a success string constant.
57     private final String SUCCESS = "Success";
58
59     // Define the class variables.
60     private String filePathString;
61     private String userAgent;
62     private boolean cookiesEnabled;
63     private Snackbar savingFileSnackbar;
64
65     // The public constructor.
66     public SaveUrl(Context context, Activity activity, String filePathString, String userAgent, boolean cookiesEnabled) {
67         // Populate weak references to the calling context and activity.
68         contextWeakReference = new WeakReference<>(context);
69         activityWeakReference = new WeakReference<>(activity);
70
71         // Store the class variables.
72         this.filePathString = filePathString;
73         this.userAgent = userAgent;
74         this.cookiesEnabled = cookiesEnabled;
75     }
76
77     // `onPreExecute()` operates on the UI thread.
78     @Override
79     protected void onPreExecute() {
80         // Get a handle for the activity.
81         Activity activity = activityWeakReference.get();
82
83         // Abort if the activity is gone.
84         if ((activity==null) || activity.isFinishing()) {
85             return;
86         }
87
88         // Get a handle for the no swipe view pager.
89         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
90
91         // Create a saving file snackbar.
92         savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file) + "  0% - " + filePathString, Snackbar.LENGTH_INDEFINITE);
93
94         // Display the saving file snackbar.
95         savingFileSnackbar.show();
96     }
97
98     @Override
99     protected String doInBackground(String... urlToSave) {
100         // Get handles for the context and activity.
101         Context context = contextWeakReference.get();
102         Activity activity = activityWeakReference.get();
103
104         // Abort if the activity is gone.
105         if ((activity == null) || activity.isFinishing()) {
106             return null;
107         }
108
109         // Define a save disposition string.
110         String saveDisposition = SUCCESS;
111
112         // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
113         try {
114             // Get the URL from the calling activity.
115             URL url = new URL(urlToSave[0]);
116
117             // Instantiate the proxy helper.
118             ProxyHelper proxyHelper = new ProxyHelper();
119
120             // Get the current proxy.
121             Proxy proxy = proxyHelper.getCurrentProxy(context);
122
123             // Open a connection to the URL.  No data is actually sent at this point.
124             HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
125
126             // Add the user agent to the header property.
127             httpUrlConnection.setRequestProperty("User-Agent", userAgent);
128
129             // Add the cookies if they are enabled.
130             if (cookiesEnabled) {
131                 // Get the cookies for the current domain.
132                 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
133
134                 // Only add the cookies if they are not null.
135                 if (cookiesString != null) {
136                     // Add the cookies to the header property.
137                     httpUrlConnection.setRequestProperty("Cookie", cookiesString);
138                 }
139             }
140
141             // 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.
142             try {
143                 // Get the content length header, which causes the connection to the server to be made.
144                 String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
145
146                 // Define the file size long.
147                 long fileSize;
148
149                 // Make sure the content length isn't null.
150                 if (contentLengthString != null) {  // The content length isn't null.
151                     // Convert the content length to an long.
152                     fileSize = Long.parseLong(contentLengthString);
153                 } else {  // The content length is null.
154                     // Set the file size to be `-1`.
155                     fileSize = -1;
156                 }
157
158                 // Get the response body stream.
159                 InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
160
161                 // Get the file.
162                 File file = new File(filePathString);
163
164                 // Delete the file if it exists.
165                 if (file.exists()) {
166                     //noinspection ResultOfMethodCallIgnored
167                     file.delete();
168                 }
169
170                 // Create a new file.
171                 //noinspection ResultOfMethodCallIgnored
172                 file.createNewFile();
173
174                 // Create an output file stream.
175                 OutputStream outputStream = new FileOutputStream(file);
176
177                 // Initialize the conversion buffer byte array.
178                 byte[] conversionBufferByteArray = new byte[1024];
179
180                 // Initialize the downloaded kilobytes counter.
181                 long downloadedKilobytesCounter = 0;
182
183                 // Define the buffer length variable.
184                 int bufferLength;
185
186                 // 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.
187                 while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) {  // Proceed while the amount of data stored in the buffer in > 0.
188                     // Write the contents of the conversion buffer to the output stream.
189                     outputStream.write(conversionBufferByteArray, 0, bufferLength);
190
191                     // Update the file download progress snackbar.
192                     if (fileSize == -1) {  // The file size is unknown.
193                         // Negatively update the downloaded kilobytes counter.
194                         downloadedKilobytesCounter = downloadedKilobytesCounter - bufferLength;
195
196                         publishProgress(downloadedKilobytesCounter);
197                     } else {  // The file size is known.
198                         // Update the downloaded kilobytes counter.
199                         downloadedKilobytesCounter = downloadedKilobytesCounter + bufferLength;
200
201                         // Calculate the download percentage.
202                         long downloadPercentage = (downloadedKilobytesCounter * 100) / fileSize;
203
204                         // Update the download percentage.
205                         publishProgress(downloadPercentage);
206                     }
207                 }
208
209                 // Close the input stream.
210                 inputStream.close();
211
212                 // Close the output stream.
213                 outputStream.close();
214
215                 // Create a media scanner intent, which adds items like pictures to Android's recent file list.
216                 Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
217
218                 // Add the URI to the media scanner intent.
219                 mediaScannerIntent.setData(Uri.fromFile(file));
220
221                 // Make it so.
222                 activity.sendBroadcast(mediaScannerIntent);
223             } finally {
224                 // Disconnect the HTTP URL connection.
225                 httpUrlConnection.disconnect();
226             }
227         } catch (Exception exception) {
228             // Store the error in the save disposition string.
229             saveDisposition = exception.toString();
230         }
231
232         // Return the save disposition string.
233         return saveDisposition;
234     }
235
236     // `onProgressUpdate()` operates on the UI thread.
237     @Override
238     protected void onProgressUpdate(Long... downloadPercentage) {
239         // Get a handle for the activity.
240         Activity activity = activityWeakReference.get();
241
242         // Abort if the activity is gone.
243         if ((activity == null) || activity.isFinishing()) {
244             return;
245         }
246
247         // Check to see if a download percentage has been calculated.
248         if (downloadPercentage[0] < 0) {  // There is no download percentage.  The negative number represents the raw downloaded kilobytes.
249             // Calculate the number of bytes downloaded.  When the `downloadPercentage` is negative, it is actually the raw number of kilobytes downloaded.
250             long numberOfBytesDownloaded = - downloadPercentage[0];
251
252             // Format the number of bytes downloaded.
253             String formattedNumberOfBytesDownloaded = NumberFormat.getInstance().format(numberOfBytesDownloaded);
254
255             // Update the snackbar.
256             savingFileSnackbar.setText(activity.getString(R.string.saving_file) + "  " + formattedNumberOfBytesDownloaded + " " + activity.getString(R.string.bytes) + " - " + filePathString);
257         } else {  // There is a download percentage.
258             // Update the snackbar.
259             savingFileSnackbar.setText(activity.getString(R.string.saving_file) + "  " + downloadPercentage[0] + "% - " + filePathString);
260         }
261     }
262
263     // `onPostExecute()` operates on the UI thread.
264     @Override
265     protected void onPostExecute(String saveDisposition) {
266         // Get handles for the context and activity.
267         Context context = contextWeakReference.get();
268         Activity activity = activityWeakReference.get();
269
270         // Abort if the activity is gone.
271         if ((activity == null) || activity.isFinishing()) {
272             return;
273         }
274
275         // Get a handle for the no swipe view pager.
276         NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
277
278         // Dismiss the saving file snackbar.
279         savingFileSnackbar.dismiss();
280
281         // Display a save disposition snackbar.
282         if (saveDisposition.equals(SUCCESS)) {
283             // Create a file saved snackbar.
284             Snackbar fileSavedSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.file_saved) + "  " + filePathString, Snackbar.LENGTH_LONG);
285
286             // Add an open action if the file is not an APK on API >= 26 (that scenario requires the REQUEST_INSTALL_PACKAGES permission).
287             if (!(Build.VERSION.SDK_INT >= 26 && filePathString.endsWith(".apk"))) {
288                 fileSavedSnackbar.setAction(R.string.open, (View view) -> {
289                     // Get a file for the file path string.
290                     File file = new File(filePathString);
291
292                     // Declare a file URI variable.
293                     Uri fileUri;
294
295                     // Get the URI for the file according to the Android version.
296                     if (Build.VERSION.SDK_INT >= 24) {  // Use a file provider.
297                         fileUri = FileProvider.getUriForFile(context, activity.getString(R.string.file_provider), file);
298                     } else {  // Get the raw file path URI.
299                         fileUri = Uri.fromFile(file);
300                     }
301
302                     // Get a handle for the content resolver.
303                     ContentResolver contentResolver = context.getContentResolver();
304
305                     // Create an open intent with `ACTION_VIEW`.
306                     Intent openIntent = new Intent(Intent.ACTION_VIEW);
307
308                     // Set the URI and the MIME type.
309                     if (filePathString.endsWith("apk") || filePathString.endsWith("APK")) {  // Force detection of APKs.
310                         openIntent.setDataAndType(fileUri, MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk"));
311                     } else {  // Autodetect the MIME type.
312                         openIntent.setDataAndType(fileUri, contentResolver.getType(fileUri));
313                     }
314
315                     // Allow the app to read the file URI.
316                     openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
317
318                     // Show the chooser.
319                     activity.startActivity(Intent.createChooser(openIntent, context.getString(R.string.open)));
320                 });
321             }
322
323             // Show the file saved snackbar.
324             fileSavedSnackbar.show();
325         } else {
326             // Display the file saving error.
327             Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + "  " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();
328         }
329     }
330 }