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