]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/PrepareSaveDialog.java
Convert the views and data classes to Kotlin. https://redmine.stoutner.com/issues/744
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / PrepareSaveDialog.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.webkit.CookieManager;
27 import android.webkit.MimeTypeMap;
28
29 import androidx.fragment.app.DialogFragment;
30 import androidx.fragment.app.FragmentManager;
31
32 import com.stoutner.privacybrowser.R;
33 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
34 import com.stoutner.privacybrowser.dataclasses.PendingDialog;
35 import com.stoutner.privacybrowser.dialogs.SaveWebpageDialog;
36 import com.stoutner.privacybrowser.helpers.ProxyHelper;
37
38 import java.lang.ref.WeakReference;
39 import java.net.HttpURLConnection;
40 import java.net.Proxy;
41 import java.net.URL;
42 import java.text.NumberFormat;
43
44 public class PrepareSaveDialog extends AsyncTask<String, Void, String[]> {
45     // Define weak references.
46     private final WeakReference<Activity> activityWeakReference;
47     private final WeakReference<Context> contextWeakReference;
48     private final WeakReference<FragmentManager> fragmentManagerWeakReference;
49
50     // Define the class variables.
51     private final int saveType;
52     private final String userAgent;
53     private final boolean cookiesEnabled;
54     private String urlString;
55
56     // The public constructor.
57     public PrepareSaveDialog(Activity activity, Context context, FragmentManager fragmentManager, int saveType, String userAgent, boolean cookiesEnabled) {
58         // Populate the weak references.
59         activityWeakReference = new WeakReference<>(activity);
60         contextWeakReference = new WeakReference<>(context);
61         fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
62
63         // Store the class variables.
64         this.saveType = saveType;
65         this.userAgent = userAgent;
66         this.cookiesEnabled = cookiesEnabled;
67     }
68
69     @Override
70     protected String[] doInBackground(String... urlToSave) {
71         // Get a handle for the activity and context.
72         Activity activity = activityWeakReference.get();
73         Context context = contextWeakReference.get();
74
75         // Abort if the activity is gone.
76         if (activity == null || activity.isFinishing()) {
77             // Return a null string array.
78             return null;
79         }
80
81         // Get the URL string.
82         urlString = urlToSave[0];
83
84         // Define the strings.
85         String formattedFileSize;
86         String fileNameString;
87
88         // Populate the file size and name strings.
89         if (urlString.startsWith("data:")) {  // The URL contains the entire data of an image.
90             // Remove `data:` from the beginning of the URL.
91             String urlWithoutData = urlString.substring(5);
92
93             // Get the URL MIME type, which end with a `;`.
94             String urlMimeType = urlWithoutData.substring(0, urlWithoutData.indexOf(";"));
95
96             // Get the Base64 data, which begins after a `,`.
97             String base64DataString = urlWithoutData.substring(urlWithoutData.indexOf(",") + 1);
98
99             // Calculate the file size of the data URL.  Each Base64 character represents 6 bits.
100             formattedFileSize = NumberFormat.getInstance().format(base64DataString.length() * 3L / 4) + " " + context.getString(R.string.bytes);
101
102             // Set the file name according to the MIME type.
103             fileNameString = context.getString(R.string.file) + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(urlMimeType);
104         } else {  // The URL refers to the location of the data.
105             // Initialize the formatted file size string.
106             formattedFileSize = context.getString(R.string.unknown_size);
107
108             // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions.
109             try {
110                 // Convert the URL string to a URL.
111                 URL url = new URL(urlString);
112
113                 // Instantiate the proxy helper.
114                 ProxyHelper proxyHelper = new ProxyHelper();
115
116                 // Get the current proxy.
117                 Proxy proxy = proxyHelper.getCurrentProxy(context);
118
119                 // Open a connection to the URL.  No data is actually sent at this point.
120                 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
121
122                 // Add the user agent to the header property.
123                 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
124
125                 // Add the cookies if they are enabled.
126                 if (cookiesEnabled) {
127                     // Get the cookies for the current domain.
128                     String cookiesString = CookieManager.getInstance().getCookie(url.toString());
129
130                     // only add the cookies if they are not null.
131                     if (cookiesString != null) {
132                         // Add the cookies to the header property.
133                         httpUrlConnection.setRequestProperty("Cookie", cookiesString);
134                     }
135                 }
136
137                 // 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.
138                 try {
139                     // Get the status code.  This initiates a network connection.
140                     int responseCode = httpUrlConnection.getResponseCode();
141
142                     // Check the response code.
143                     if (responseCode >= 400) {  // The response code is an error message.
144                         // Set the formatted file size to indicate a bad URL.
145                         formattedFileSize = context.getString(R.string.invalid_url);
146
147                         // Set the file name according to the URL.
148                         fileNameString = getFileNameFromUrl(context, urlString, null);
149                     } else {  // The response code is not an error message.
150                         // Get the headers.
151                         String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
152                         String contentDispositionString = httpUrlConnection.getHeaderField("Content-Disposition");
153                         String contentTypeString = httpUrlConnection.getContentType();
154
155                         // Remove anything after the MIME type in the content type string.
156                         if (contentTypeString.contains(";")) {
157                             // Remove everything beginning with the `;`.
158                             contentTypeString = contentTypeString.substring(0, contentTypeString.indexOf(";"));
159                         }
160
161                         // Only process the content length string if it isn't null.
162                         if (contentLengthString != null) {
163                             // Convert the content length string to a long.
164                             long fileSize = Long.parseLong(contentLengthString);
165
166                             // Format the file size.
167                             formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes);
168                         }
169
170                         // Get the file name string from the content disposition.
171                         fileNameString = getFileNameFromHeaders(context, contentDispositionString, contentTypeString, urlString);
172                     }
173                 } finally {
174                     // Disconnect the HTTP URL connection.
175                     httpUrlConnection.disconnect();
176                 }
177             } catch (Exception exception) {
178                 // Set the formatted file size to indicate a bad URL.
179                 formattedFileSize = context.getString(R.string.invalid_url);
180
181                 // Set the file name according to the URL.
182                 fileNameString = getFileNameFromUrl(context, urlString, null);
183             }
184         }
185
186         // Return the formatted file size and name as a string array.
187         return new String[] {formattedFileSize, fileNameString};
188     }
189
190     // `onPostExecute()` operates on the UI thread.
191     @Override
192     protected void onPostExecute(String[] fileStringArray) {
193         // Get a handle for the activity and the fragment manager.
194         Activity activity = activityWeakReference.get();
195         FragmentManager fragmentManager = fragmentManagerWeakReference.get();
196
197         // Abort if the activity is gone.
198         if (activity == null || activity.isFinishing()) {
199             // Exit.
200             return;
201         }
202
203         // Instantiate the save dialog.
204         DialogFragment saveDialogFragment = SaveWebpageDialog.saveWebpage(saveType, urlString, fileStringArray[0], fileStringArray[1], userAgent, cookiesEnabled);
205
206         // Try to show the dialog.  Sometimes the window is not active.
207         try {
208             // Show the save dialog.  It must be named `save_dialog` so that the file picker can update the file name.
209             saveDialogFragment.show(fragmentManager, activity.getString(R.string.save_dialog));
210         } catch (Exception exception) {
211             // Add the dialog to the pending dialog array list.  It will be displayed in `onStart()`.
212             MainWebViewActivity.pendingDialogsArrayList.add(new PendingDialog(saveDialogFragment, activity.getString(R.string.save_dialog)));
213         }
214     }
215
216     // Content dispositions can contain other text besides the file name, and they can be in any order.
217     // Elements are separated by semicolons.  Sometimes the file names are contained in quotes.
218     public static String getFileNameFromHeaders(Context context, String contentDispositionString, String contentTypeString, String urlString) {
219         // Define a file name string.
220         String fileNameString;
221
222         // Only process the content disposition string if it isn't null.
223         if (contentDispositionString != null) {  // The content disposition is not null.
224             // Check to see if the content disposition contains a file name.
225             if (contentDispositionString.contains("filename=")) {  // The content disposition contains a filename.
226                 // Get the part of the content disposition after `filename=`.
227                 fileNameString = contentDispositionString.substring(contentDispositionString.indexOf("filename=") + 9);
228
229                 // Remove any `;` and anything after it.  This removes any entries after the filename.
230                 if (fileNameString.contains(";")) {
231                     // Remove the first `;` and everything after it.
232                     fileNameString = fileNameString.substring(0, fileNameString.indexOf(";") - 1);
233                 }
234
235                 // Remove any `"` at the beginning of the string.
236                 if (fileNameString.startsWith("\"")) {
237                     // Remove the first character.
238                     fileNameString = fileNameString.substring(1);
239                 }
240
241                 // Remove any `"` at the end of the string.
242                 if (fileNameString.endsWith("\"")) {
243                     // Remove the last character.
244                     fileNameString = fileNameString.substring(0, fileNameString.length() - 1);
245                 }
246             } else {  // The headers contain no useful information.
247                 // Get the file name string from the URL.
248                 fileNameString = getFileNameFromUrl(context, urlString, contentTypeString);
249             }
250         } else {  // The content disposition is null.
251             // Get the file name string from the URL.
252             fileNameString = getFileNameFromUrl(context, urlString, contentTypeString);
253         }
254
255         // Return the file name string.
256         return fileNameString;
257     }
258
259     private static String getFileNameFromUrl(Context context, String urlString, String contentTypeString) {
260         // Convert the URL string to a URI.
261         Uri uri = Uri.parse(urlString);
262
263         // Get the last path segment.
264         String lastPathSegment = uri.getLastPathSegment();
265
266         // Use a default file name if the last path segment is null.
267         if (lastPathSegment == null) {
268             lastPathSegment = context.getString(R.string.file);
269
270             if (MimeTypeMap.getSingleton().hasMimeType(contentTypeString)) {  // The content type contains a MIME type.
271                 // Add the file extension that matches the MIME type.
272                 lastPathSegment = lastPathSegment + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(contentTypeString);
273             }
274         }
275
276         // Return the last path segment as the file name.
277         return lastPathSegment;
278     }
279 }