2 * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.asynctasks;
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;
28 import androidx.fragment.app.DialogFragment;
29 import androidx.fragment.app.FragmentManager;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.dialogs.SaveDialog;
33 import com.stoutner.privacybrowser.helpers.ProxyHelper;
35 import java.lang.ref.WeakReference;
36 import java.net.HttpURLConnection;
37 import java.net.Proxy;
39 import java.text.NumberFormat;
41 public class PrepareSaveDialog extends AsyncTask<String, Void, String[]> {
42 // Define weak references.
43 private WeakReference<Activity> activityWeakReference;
44 private WeakReference<Context> contextWeakReference;
45 private WeakReference<FragmentManager> fragmentManagerWeakReference;
47 // Define the class variables.
49 private String userAgent;
50 private boolean cookiesEnabled;
51 private String urlString;
53 // The public constructor.
54 public PrepareSaveDialog(Activity activity, Context context, FragmentManager fragmentManager, int saveType, String userAgent, boolean cookiesEnabled) {
55 // Populate the weak references.
56 activityWeakReference = new WeakReference<>(activity);
57 contextWeakReference = new WeakReference<>(context);
58 fragmentManagerWeakReference = new WeakReference<>(fragmentManager);
60 // Store the class variables.
61 this.saveType = saveType;
62 this.userAgent = userAgent;
63 this.cookiesEnabled = cookiesEnabled;
67 protected String[] doInBackground(String... urlToSave) {
68 // Get a handle for the activity and context.
69 Activity activity = activityWeakReference.get();
70 Context context = contextWeakReference.get();
72 // Abort if the activity is gone.
73 if (activity == null || activity.isFinishing()) {
74 // Return a null string array.
78 // Get the URL string.
79 urlString = urlToSave[0];
81 // Define the file name string.
82 String fileNameString;
84 // Initialize the formatted file size string.
85 String formattedFileSize = context.getString(R.string.unknown_size);
87 // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions.
89 // Convert the URL string to a URL.
90 URL url = new URL(urlString);
92 // Instantiate the proxy helper.
93 ProxyHelper proxyHelper = new ProxyHelper();
95 // Get the current proxy.
96 Proxy proxy = proxyHelper.getCurrentProxy(context);
98 // Open a connection to the URL. No data is actually sent at this point.
99 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
101 // Add the user agent to the header property.
102 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
104 // Add the cookies if they are enabled.
105 if (cookiesEnabled) {
106 // Get the cookies for the current domain.
107 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
109 // only add the cookies if they are not null.
110 if (cookiesString != null) {
111 // Add the cookies to the header property.
112 httpUrlConnection.setRequestProperty("Cookie", cookiesString);
116 // 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.
118 // Get the status code. This initiates a network connection.
119 int responseCode = httpUrlConnection.getResponseCode();
121 // Check the response code.
122 if (responseCode >= 400) { // The response code is an error message.
123 // Set the formatted file size to indicate a bad URL.
124 formattedFileSize = context.getString(R.string.invalid_url);
126 // Set the file name according to the URL.
127 fileNameString = getFileNameFromUrl(context, urlString);
128 } else { // The response code is not an error message.
129 // Get the content length and disposition headers.
130 String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
131 String contentDispositionString = httpUrlConnection.getHeaderField("Content-Disposition");
133 // Only process the content length string if it isn't null.
134 if (contentLengthString != null) {
135 // Convert the content length string to a long.
136 long fileSize = Long.parseLong(contentLengthString);
138 // Format the file size.
139 formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes);
142 // Get the file name string from the content disposition.
143 fileNameString = getFileNameFromContentDisposition(context, contentDispositionString, urlString);
146 // Disconnect the HTTP URL connection.
147 httpUrlConnection.disconnect();
149 } catch (Exception exception) {
150 // Set the formatted file size to indicate a bad URL.
151 formattedFileSize = context.getString(R.string.invalid_url);
153 // Set the file name according to the URL.
154 fileNameString = getFileNameFromUrl(context, urlString);
157 // Return the formatted file size and name as a string array.
158 return new String[] {formattedFileSize, fileNameString};
161 // `onPostExecute()` operates on the UI thread.
163 protected void onPostExecute(String[] fileStringArray) {
164 // Get a handle for the activity and the fragment manager.
165 Activity activity = activityWeakReference.get();
166 FragmentManager fragmentManager = fragmentManagerWeakReference.get();
168 // Abort if the activity is gone.
169 if (activity == null || activity.isFinishing()) {
174 // Instantiate the save dialog.
175 DialogFragment saveDialogFragment = SaveDialog.saveUrl(saveType, urlString, fileStringArray[0], fileStringArray[1], userAgent, cookiesEnabled);
177 // Show the save dialog. It must be named `save_dialog` so that the file picker can update the file name.
178 saveDialogFragment.show(fragmentManager, activity.getString(R.string.save_dialog));
181 // Content dispositions can contain other text besides the file name, and they can be in any order.
182 // Elements are separated by semicolons. Sometimes the file names are contained in quotes.
183 public static String getFileNameFromContentDisposition(Context context, String contentDispositionString, String urlString) {
184 // Define a file name string.
185 String fileNameString;
187 // Only process the content disposition string if it isn't null.
188 if (contentDispositionString != null) { // The content disposition is not null.
189 // Check to see if the content disposition contains a file name.
190 if (contentDispositionString.contains("filename=")) { // The content disposition contains a filename.
191 // Get the part of the content disposition after `filename=`.
192 fileNameString = contentDispositionString.substring(contentDispositionString.indexOf("filename=") + 9);
194 // Remove any `;` and anything after it. This removes any entries after the filename.
195 if (fileNameString.contains(";")) {
196 // Remove the first `;` and everything after it.
197 fileNameString = fileNameString.substring(0, fileNameString.indexOf(";") - 1);
200 // Remove any `"` at the beginning of the string.
201 if (fileNameString.startsWith("\"")) {
202 // Remove the first character.
203 fileNameString = fileNameString.substring(1);
206 // Remove any `"` at the end of the string.
207 if (fileNameString.endsWith("\"")) {
208 // Remove the last character.
209 fileNameString = fileNameString.substring(0, fileNameString.length() - 1);
211 } else { // The content disposition does not contain a filename.
212 // Get the file name string from the URL.
213 fileNameString = getFileNameFromUrl(context, urlString);
215 } else { // The content disposition is null.
216 // Get the file name string from the URL.
217 fileNameString = getFileNameFromUrl(context, urlString);
220 // Return the file name string.
221 return fileNameString;
224 private static String getFileNameFromUrl(Context context, String urlString) {
225 // Convert the URL string to a URI.
226 Uri uri = Uri.parse(urlString);
228 // Get the last path segment.
229 String lastPathSegment = uri.getLastPathSegment();
231 // Use a default file name if the last path segment is null.
232 if (lastPathSegment == null) {
233 lastPathSegment = context.getString(R.string.file);
236 // Return the last path segment as the file name.
237 return lastPathSegment;