2 * Copyright 2020-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.asynctasks;
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;
32 import com.google.android.material.snackbar.Snackbar;
34 import com.stoutner.privacybrowser.R;
36 import java.io.ByteArrayOutputStream;
37 import java.io.OutputStream;
38 import java.lang.ref.WeakReference;
40 public class SaveAboutVersionImage extends AsyncTask<Void, Void, String> {
41 // Declare the class constants.
42 private final String SUCCESS = "Success";
44 // Declare the weak references.
45 private final WeakReference<Activity> activityWeakReference;
46 private final WeakReference<LinearLayout> aboutVersionLinearLayoutWeakReference;
48 // Declare the class variables.
49 private Snackbar savingImageSnackbar;
50 private Bitmap aboutVersionBitmap;
51 private final Uri fileUri;
52 private final String fileNameString;
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);
60 // Store the class variables.
61 this.fileUri = fileUri;
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);
68 // Move to the first row.
69 contentResolverCursor.moveToFirst();
71 // Get the file name from the cursor.
72 fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
75 contentResolverCursor.close();
77 // Use the URI last path segment as the file name string.
78 fileNameString = fileUri.getLastPathSegment();
82 // `onPreExecute()` operates on the UI thread.
84 protected void onPreExecute() {
85 // Get handles for the activity and the linear layout.
86 Activity activity = activityWeakReference.get();
87 LinearLayout aboutVersionLinearLayout = aboutVersionLinearLayoutWeakReference.get();
89 // Abort if the activity or the linear layout is gone.
90 if ((activity == null) || activity.isFinishing() || aboutVersionLinearLayout == null) {
94 // Create a saving image snackbar.
95 savingImageSnackbar = Snackbar.make(aboutVersionLinearLayout, activity.getString(R.string.processing_image) + " " + fileNameString, Snackbar.LENGTH_INDEFINITE);
97 // Display the saving image snackbar.
98 savingImageSnackbar.show();
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);
105 Canvas aboutVersionCanvas = new Canvas(aboutVersionBitmap);
107 // Draw the current about version onto the bitmap. The linear layout commands must be run on the UI thread.
108 aboutVersionLinearLayout.draw(aboutVersionCanvas);
112 protected String doInBackground(Void... Void) {
113 // Get a handle for the activity.
114 Activity activity = activityWeakReference.get();
116 // Abort if the activity is gone.
117 if (((activity == null) || activity.isFinishing())) {
121 // Create an about version PNG byte array output stream.
122 ByteArrayOutputStream aboutVersionByteArrayOutputStream = new ByteArrayOutputStream();
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);
127 // Create a file creation disposition string.
128 String fileCreationDisposition = SUCCESS;
131 // Open an output stream.
132 OutputStream outputStream = activity.getContentResolver().openOutputStream(fileUri);
134 // Write the webpage image to the image file.
135 aboutVersionByteArrayOutputStream.writeTo(outputStream);
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();
144 // return the file creation disposition string.
145 return fileCreationDisposition;
148 // `onPostExecute()` operates on the UI thread.
150 protected void onPostExecute(String fileCreationDisposition) {
151 // Get handles for the weak references.
152 Activity activity = activityWeakReference.get();
153 LinearLayout aboutVersionLinearLayout = aboutVersionLinearLayoutWeakReference.get();
155 // Abort if the activity is gone.
156 if ((activity == null) || activity.isFinishing()) {
160 // Dismiss the saving image snackbar.
161 savingImageSnackbar.dismiss();
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.saved, fileNameString), Snackbar.LENGTH_SHORT).show();
168 Snackbar.make(aboutVersionLinearLayout, activity.getString(R.string.error_saving_file, fileNameString, fileCreationDisposition), Snackbar.LENGTH_INDEFINITE).show();