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.os.AsyncTask;
24 import android.webkit.CookieManager;
26 import com.google.android.material.snackbar.Snackbar;
27 import com.stoutner.privacybrowser.R;
28 import com.stoutner.privacybrowser.views.NoSwipeViewPager;
30 import java.io.BufferedInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.lang.ref.WeakReference;
37 import java.net.HttpURLConnection;
40 public class SaveUrl extends AsyncTask<String, Void, String> {
41 // Define a weak reference to the calling activity.
42 private WeakReference<Activity> activityWeakReference;
44 // Define a success string constant.
45 private final String SUCCESS = "Success";
47 // Define the class variables.
48 private String filePathString;
49 private String userAgent;
50 private boolean cookiesEnabled;
51 private Snackbar savingFileSnackbar;
53 // The public constructor.
54 public SaveUrl(Activity activity, String filePathString, String userAgent, boolean cookiesEnabled) {
55 // Populate the weak reference to the calling activity.
56 activityWeakReference = new WeakReference<>(activity);
58 // Store the class variables.
59 this.filePathString = filePathString;
60 this.userAgent = userAgent;
61 this.cookiesEnabled = cookiesEnabled;
64 // `onPreExecute()` operates on the UI thread.
66 protected void onPreExecute() {
67 // Get a handle for the activity.
68 Activity activity = activityWeakReference.get();
70 // Abort if the activity is gone.
71 if ((activity==null) || activity.isFinishing()) {
75 // Get a handle for the no swipe view pager.
76 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
78 // Create a saving file snackbar.
79 savingFileSnackbar = Snackbar.make(noSwipeViewPager, R.string.saving_file, Snackbar.LENGTH_INDEFINITE);
81 // Display the saving file snackbar.
82 savingFileSnackbar.show();
86 protected String doInBackground(String... urlToSave) {
87 // Get a handle for the activity.
88 Activity activity = activityWeakReference.get();
90 // Abort if the activity is gone.
91 if ((activity == null) || activity.isFinishing()) {
95 // Define a save disposition string.
96 String saveDisposition = SUCCESS;
98 // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
100 // Get the URL from the main activity.
101 URL url = new URL(urlToSave[0]);
103 // Open a connection to the URL. No data is actually sent at this point.
104 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
106 // Add the user agent to the header property.
107 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
109 // Add the cookies if they are enabled.
110 if (cookiesEnabled) {
111 // Get the cookies for the current domain.
112 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
114 // Only add the cookies if they are not null.
115 if (cookiesString != null) {
116 // Add the cookies to the header property.
117 httpUrlConnection.setRequestProperty("Cookie", cookiesString);
121 // 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.
123 // Get the response code, which causes the connection to the server to be made.
124 httpUrlConnection.getResponseCode();
126 // Get the response body stream.
127 InputStream inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
130 File file = new File(filePathString);
132 // Delete the file if it exists.
134 //noinspection ResultOfMethodCallIgnored
138 // Create a new file.
139 //noinspection ResultOfMethodCallIgnored
140 file.createNewFile();
142 // Create an output file stream.
143 OutputStream outputStream = new FileOutputStream(file);
145 // Initialize the conversion buffer byte array.
146 byte[] conversionBufferByteArray = new byte[1024];
148 // Define the buffer length variable.
151 // 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.
152 while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) { // Proceed while the amount of data stored in the buffer in > 0.
153 // Write the contents of the conversion buffer to the output stream.
154 outputStream.write(conversionBufferByteArray, 0, bufferLength);
157 // Close the input stream.
160 // Close the output stream.
161 outputStream.close();
163 // Disconnect the HTTP URL connection.
164 httpUrlConnection.disconnect();
166 } catch (IOException exception) {
167 // Store the error in the save disposition string.
168 saveDisposition = exception.toString();
171 // Return the save disposition string.
172 return saveDisposition;
175 // `onPostExecute()` operates on the UI thread.
177 protected void onPostExecute(String saveDisposition) {
178 // Get a handle for the activity.
179 Activity activity = activityWeakReference.get();
181 // Abort if the activity is gone.
182 if ((activity == null) || activity.isFinishing()) {
186 // Get a handle for the no swipe view pager.
187 NoSwipeViewPager noSwipeViewPager = activity.findViewById(R.id.webviewpager);
189 // Dismiss the saving file snackbar.
190 savingFileSnackbar.dismiss();
192 // Display a save disposition snackbar.
193 if (saveDisposition.equals(SUCCESS)) {
194 Snackbar.make(noSwipeViewPager, R.string.file_saved, Snackbar.LENGTH_SHORT).show();
196 Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file) + " " + saveDisposition, Snackbar.LENGTH_INDEFINITE).show();