]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/asynctasks/SaveAboutVersionImage.java
Simplify the SaveWebpageDialog. https://redmine.stoutner.com/issues/769
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / SaveAboutVersionImage.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.database.Cursor;
24 import android.graphics.Bitmap;
25 import android.graphics.Canvas;
26 import android.net.Uri;
27 import android.os.AsyncTask;
28 import android.os.Build;
29 import android.provider.OpenableColumns;
30 import android.widget.LinearLayout;
31
32 import com.google.android.material.snackbar.Snackbar;
33
34 import com.stoutner.privacybrowser.R;
35
36 import java.io.ByteArrayOutputStream;
37 import java.io.OutputStream;
38 import java.lang.ref.WeakReference;
39
40 public class SaveAboutVersionImage extends AsyncTask<Void, Void, String> {
41     // Declare the class constants.
42     private final String SUCCESS = "Success";
43
44     // Declare the weak references.
45     private final WeakReference<Activity> activityWeakReference;
46     private final WeakReference<LinearLayout> aboutVersionLinearLayoutWeakReference;
47
48     // Declare the class variables.
49     private Snackbar savingImageSnackbar;
50     private Bitmap aboutVersionBitmap;
51     private final Uri fileUri;
52     private final String fileNameString;
53
54     // The public constructor.
55     public SaveAboutVersionImage(Activity activity, Uri fileUri, LinearLayout aboutVersionLinearLayout) {
56         // Populate the weak references.
57         activityWeakReference = new WeakReference<>(activity);
58         aboutVersionLinearLayoutWeakReference = new WeakReference<>(aboutVersionLinearLayout);
59
60         // Store the class variables.
61         this.fileUri = fileUri;
62
63         // Query the exact file name if the API >= 26.
64         if (Build.VERSION.SDK_INT >= 26) {
65             // Get a cursor from the content resolver.
66             Cursor contentResolverCursor = activity.getContentResolver().query(fileUri, null, null, null);
67
68             // Move to the first row.
69             contentResolverCursor.moveToFirst();
70
71             // Get the file name from the cursor.
72             fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
73
74             // Close the cursor.
75             contentResolverCursor.close();
76         } else {
77             // Use the URI last path segment as the file name string.
78             fileNameString = fileUri.getLastPathSegment();
79         }
80     }
81
82     // `onPreExecute()` operates on the UI thread.
83     @Override
84     protected void onPreExecute() {
85         // Get handles for the activity and the linear layout.
86         Activity activity = activityWeakReference.get();
87         LinearLayout aboutVersionLinearLayout = aboutVersionLinearLayoutWeakReference.get();
88
89         // Abort if the activity or the linear layout is gone.
90         if ((activity == null) || activity.isFinishing() || aboutVersionLinearLayout == null) {
91             return;
92         }
93
94         // Create a saving image snackbar.
95         savingImageSnackbar = Snackbar.make(aboutVersionLinearLayout, activity.getString(R.string.processing_image) + "  " + fileNameString, Snackbar.LENGTH_INDEFINITE);
96
97         // Display the saving image snackbar.
98         savingImageSnackbar.show();
99
100         // Create the about version bitmap.  This can be replaced by PixelCopy once the minimum API >= 26.
101         // Once the Minimum API >= 26 Bitmap.Config.RBGA_F16 can be used instead of ARGB_8888.  The linear layout commands must be run on the UI thread.
102         aboutVersionBitmap = Bitmap.createBitmap(aboutVersionLinearLayout.getWidth(), aboutVersionLinearLayout.getHeight(), Bitmap.Config.ARGB_8888);
103
104         // Create a canvas.
105         Canvas aboutVersionCanvas = new Canvas(aboutVersionBitmap);
106
107         // Draw the current about version onto the bitmap.  The linear layout commands must be run on the UI thread.
108         aboutVersionLinearLayout.draw(aboutVersionCanvas);
109     }
110
111     @Override
112     protected String doInBackground(Void... Void) {
113         // Get a handle for the activity.
114         Activity activity = activityWeakReference.get();
115
116         // Abort if the activity is gone.
117         if (((activity == null) || activity.isFinishing())) {
118             return "";
119         }
120
121         // Create an about version PNG byte array output stream.
122         ByteArrayOutputStream aboutVersionByteArrayOutputStream = new ByteArrayOutputStream();
123
124         // Convert the bitmap to a PNG.  `0` is for lossless compression (the only option for a PNG).  This compression takes a long time.  Once the minimum API >= 30 this could be replaced with WEBP_LOSSLESS.
125         aboutVersionBitmap.compress(Bitmap.CompressFormat.PNG, 0, aboutVersionByteArrayOutputStream);
126
127         // Create a file creation disposition string.
128         String fileCreationDisposition = SUCCESS;
129
130         try {
131             // Open an output stream.
132             OutputStream outputStream = activity.getContentResolver().openOutputStream(fileUri);
133
134             // Write the webpage image to the image file.
135             aboutVersionByteArrayOutputStream.writeTo(outputStream);
136
137             // Close the output stream.
138             outputStream.close();
139         } catch (Exception exception) {
140             // Store the error in the file creation disposition string.
141             fileCreationDisposition = exception.toString();
142         }
143
144         // return the file creation disposition string.
145         return fileCreationDisposition;
146     }
147
148     // `onPostExecute()` operates on the UI thread.
149     @Override
150     protected void onPostExecute(String fileCreationDisposition) {
151         // Get handles for the weak references.
152         Activity activity = activityWeakReference.get();
153         LinearLayout aboutVersionLinearLayout = aboutVersionLinearLayoutWeakReference.get();
154
155         // Abort if the activity is gone.
156         if ((activity == null) || activity.isFinishing()) {
157             return;
158         }
159
160         // Dismiss the saving image snackbar.
161         savingImageSnackbar.dismiss();
162
163         // Display a file creation disposition snackbar.
164         if (fileCreationDisposition.equals(SUCCESS)) {
165             // Create a file saved snackbar.
166             Snackbar.make(aboutVersionLinearLayout, activity.getString(R.string.file_saved) + "  " + fileNameString, Snackbar.LENGTH_SHORT).show();
167         } else {
168             Snackbar.make(aboutVersionLinearLayout, activity.getString(R.string.error_saving_file) + "  " + fileCreationDisposition, Snackbar.LENGTH_INDEFINITE).show();
169         }
170     }
171 }