2 * Copyright © 2019 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.dialogs;
22 import android.Manifest;
23 import android.annotation.SuppressLint;
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.content.Intent;
30 import android.content.SharedPreferences;
31 import android.content.pm.PackageManager;
32 import android.os.Build;
33 import android.os.Bundle;
34 import android.os.Environment;
35 import android.provider.DocumentsContract;
36 import android.text.Editable;
37 import android.text.TextWatcher;
38 import android.view.View;
39 import android.view.WindowManager;
40 import android.widget.Button;
41 import android.widget.EditText;
42 import android.widget.TextView;
44 import androidx.annotation.NonNull;
45 import androidx.core.content.ContextCompat;
46 import androidx.fragment.app.DialogFragment;
47 import androidx.preference.PreferenceManager;
49 import com.stoutner.privacybrowser.R;
50 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
52 public class SaveWebpageDialog extends DialogFragment {
53 // Define the save type constants.
54 public static final int ARCHIVE = 0;
55 public static final int IMAGE = 1;
57 // Define the save webpage listener.
58 private SaveWebpageListener saveWebpageListener;
60 // The public interface is used to send information back to the parent activity.
61 public interface SaveWebpageListener {
62 void onSaveWebpage(int saveType, DialogFragment dialogFragment);
66 public void onAttach(@NonNull Context context) {
67 // Run the default commands.
68 super.onAttach(context);
70 // Get a handle for the save webpage listener from the launching context.
71 saveWebpageListener = (SaveWebpageListener) context;
74 public static SaveWebpageDialog saveWebpage(int saveType) {
75 // Create an arguments bundle.
76 Bundle argumentsBundle = new Bundle();
78 // Store the save type in the bundle.
79 argumentsBundle.putInt("save_type", saveType);
81 // Create a new instance of the save webpage dialog.
82 SaveWebpageDialog saveWebpageDialog = new SaveWebpageDialog();
84 // Add the arguments bundle to the new dialog.
85 saveWebpageDialog.setArguments(argumentsBundle);
87 // Return the new dialog.
88 return saveWebpageDialog;
91 // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
92 @SuppressLint("InflateParams")
95 public Dialog onCreateDialog(Bundle savedInstanceState) {
96 // Get a handle for the arguments.
97 Bundle arguments = getArguments();
99 // Remove the incorrect lint warning that the arguments might be null.
100 assert arguments != null;
102 // Get the save type.
103 int saveType = arguments.getInt("save_type");
105 // Get a handle for the activity and the context.
106 Activity activity = getActivity();
107 Context context = getContext();
109 // Remove the incorrect lint warnings below that the activity and context might be null.
110 assert activity != null;
111 assert context != null;
113 // Get a handle for the shared preferences.
114 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
116 // Get the screenshot and theme preferences.
117 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
118 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
120 // Use an alert dialog builder to create the alert dialog.
121 AlertDialog.Builder dialogBuilder;
123 // Set the style and icon according to the theme.
126 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
128 // Set the icon according to the save type.
131 dialogBuilder.setIcon(R.drawable.dom_storage_cleared_dark);
135 dialogBuilder.setIcon(R.drawable.images_enabled_dark);
140 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
142 // Set the icon according to the save type.
145 dialogBuilder.setIcon(R.drawable.dom_storage_cleared_light);
149 dialogBuilder.setIcon(R.drawable.images_enabled_light);
154 // Set the title according to the type.
157 dialogBuilder.setTitle(R.string.save_archive);
161 dialogBuilder.setTitle(R.string.save_image);
165 // Set the view. The parent view is null because it will be assigned by the alert dialog.
166 dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.save_dialog, null));
168 // Set the cancel button listener. Using `null` as the listener closes the dialog without doing anything else.
169 dialogBuilder.setNegativeButton(R.string.cancel, null);
171 // Set the save button listener.
172 dialogBuilder.setPositiveButton(R.string.save, (DialogInterface dialog, int which) -> {
173 // Return the dialog fragment to the parent activity.
174 saveWebpageListener.onSaveWebpage(saveType, this);
177 // Create an alert dialog from the builder.
178 AlertDialog alertDialog = dialogBuilder.create();
180 // Remove the incorrect lint warning below that `getWindow()` might be null.
181 assert alertDialog.getWindow() != null;
183 // Disable screenshots if not allowed.
184 if (!allowScreenshots) {
185 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
188 // The alert dialog must be shown before items in the layout can be modified.
191 // Get handles for the layout items.
192 EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext);
193 Button browseButton = alertDialog.findViewById(R.id.browse_button);
194 TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview);
195 Button saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
197 // Create a default file name string.
198 String defaultFileName = "";
200 // Set the default file name according to the type.
203 defaultFileName = getString(R.string.webpage_mht);
207 defaultFileName = getString(R.string.webpage_png);
211 // Create a string for the default file path.
212 String defaultFilePath;
214 // Set the default file path according to the storage permission state.
215 if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
216 // Set the default file path to use the external public directory.
217 defaultFilePath = Environment.getExternalStorageDirectory() + "/" + defaultFileName;
218 } else { // The storage permission has not been granted.
219 // Set the default file path to use the external private directory.
220 defaultFilePath = context.getExternalFilesDir(null) + "/" + defaultFileName;
223 // Display the default file path.
224 fileNameEditText.setText(defaultFilePath);
226 // Update the status of the save button when the file name changes.
227 fileNameEditText.addTextChangedListener(new TextWatcher() {
229 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
234 public void onTextChanged(CharSequence s, int start, int before, int count) {
239 public void afterTextChanged(Editable s) {
240 // // Enable the save button if a file name exists.
241 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
245 // Handle clicks on the browse button.
246 browseButton.setOnClickListener((View view) -> {
247 // Create the file picker intent.
248 Intent browseIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
250 // Set the intent MIME type to include all files so that everything is visible.
251 browseIntent.setType("*/*");
253 // Set the initial file name according to the type.
256 browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.webpage_mht));
260 browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.webpage_png));
264 // Set the initial directory if the minimum API >= 26.
265 if (Build.VERSION.SDK_INT >= 26) {
266 browseIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.getExternalStorageDirectory());
269 // Request a file that can be opened.
270 browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
272 // Start the file picker. This must be started under `activity` so that the request code is returned correctly.
273 activity.startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_SAVE_WEBPAGE_REQUEST_CODE);
276 // Hide the storage permission text view on API < 23 as permissions on older devices are automatically granted.
277 if (Build.VERSION.SDK_INT < 23) {
278 storagePermissionTextView.setVisibility(View.GONE);
281 // Return the alert dialog.