]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveLogcatDialog.java
Implement saving a webpage as an image. https://redmine.stoutner.com/issues/187
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / SaveLogcatDialog.java
1 /*
2  * Copyright © 2016-2019 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.dialogs;
21
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.preference.PreferenceManager;
36 import android.provider.DocumentsContract;
37 import android.text.Editable;
38 import android.text.TextWatcher;
39 import android.view.View;
40 import android.view.WindowManager;
41 import android.widget.Button;
42 import android.widget.EditText;
43 import android.widget.TextView;
44
45 import androidx.annotation.NonNull;
46 import androidx.core.content.ContextCompat;
47 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment is required or an error is produced on API <=22.  It is also required for the browse button to work correctly.
48
49 import com.stoutner.privacybrowser.R;
50
51 public class SaveLogcatDialog extends DialogFragment {
52     // Define the save logcat listener.
53     private SaveLogcatListener saveLogcatListener;
54
55     // The public interface is used to send information back to the parent activity.
56     public interface SaveLogcatListener {
57         void onSaveLogcat(DialogFragment dialogFragment);
58     }
59
60     @Override
61     public void onAttach(Context context) {
62         // Run the default commands.
63         super.onAttach(context);
64
65         // Get a handle for save logcat listener from the launching context.
66         saveLogcatListener = (SaveLogcatListener) context;
67     }
68
69     // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
70     @SuppressLint("InflateParams")
71     @Override
72     @NonNull
73     public Dialog onCreateDialog(Bundle savedInstanceState) {
74         // Get a handle for the shared preferences.
75         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
76
77         // Get the screenshot and theme preferences.
78         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
79         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
80
81         // Use an alert dialog builder to create the alert dialog.
82         AlertDialog.Builder dialogBuilder;
83
84         // Get a handle for the activity.
85         Activity activity = getActivity();
86
87         // Remove the incorrect lint warning below that the activity might be null.
88         assert activity != null;
89
90         // Set the style according to the theme.
91         if (darkTheme) {
92             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
93         } else {
94             dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
95         }
96
97         // Set the title.
98         dialogBuilder.setTitle(R.string.save_logcat);
99
100         // Set the icon according to the theme.
101         if (darkTheme) {
102             dialogBuilder.setIcon(R.drawable.save_dialog_dark);
103         } else {
104             dialogBuilder.setIcon(R.drawable.save_dialog_light);
105         }
106
107         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
108         dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.save_dialog, null));
109
110         // Set the cancel button listener.
111         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
112             // Do nothing.  The alert dialog will close automatically.
113         });
114
115         // Set the save button listener.
116         dialogBuilder.setPositiveButton(R.string.save, (DialogInterface dialog, int which) -> {
117             // Return the dialog fragment to the parent activity.
118             saveLogcatListener.onSaveLogcat(this);
119         });
120
121         // Create an alert dialog from the builder.
122         AlertDialog alertDialog = dialogBuilder.create();
123
124         // Remove the incorrect lint warning below that `getWindow()` might be null.
125         assert alertDialog.getWindow() != null;
126
127         // Disable screenshots if not allowed.
128         if (!allowScreenshots) {
129             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
130         }
131
132         // The alert dialog must be shown before items in the layout can be modified.
133         alertDialog.show();
134
135         // Get handles for the layout items.
136         EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext);
137         Button browseButton = alertDialog.findViewById(R.id.browse_button);
138         TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview);
139         Button saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
140
141         // Create a string for the default file path.
142         String defaultFilePath;
143
144         // Get a handle for the context.
145         Context context = getContext();
146
147         // Remove the incorrect lint warning below that context might be null.
148         assert context != null;
149
150         // Set the default file path according to the storage permission state.
151         if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {  // The storage permission has been granted.
152             // Set the default file path to use the external public directory.
153             defaultFilePath = Environment.getExternalStorageDirectory() + "/" + getString(R.string.privacy_browser_logcat_txt);
154         } else {  // The storage permission has not been granted.
155             // Set the default file path to use the external private directory.
156             defaultFilePath = context.getExternalFilesDir(null) + "/" + getString(R.string.privacy_browser_logcat_txt);
157         }
158
159         // Display the default file path.
160         fileNameEditText.setText(defaultFilePath);
161
162         // Update the status of the save button when the file name changes.
163         fileNameEditText.addTextChangedListener(new TextWatcher() {
164             @Override
165             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
166                 // Do nothing.
167             }
168
169             @Override
170             public void onTextChanged(CharSequence s, int start, int before, int count) {
171                 // Do nothing.
172             }
173
174             @Override
175             public void afterTextChanged(Editable s) {
176                 // Enable the save button if a file name exists.
177                 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
178             }
179         });
180
181         // Handle clicks on the browse button.
182         browseButton.setOnClickListener((View view) -> {
183             // Create the file picker intent.
184             Intent browseIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
185
186             // Set the intent MIME type to include all files so that everything is visible.
187             browseIntent.setType("*/*");
188
189             // Set the initial file name.
190             browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.privacy_browser_logcat_txt));
191
192             // Set the initial directory if the minimum API >= 26.
193             if (Build.VERSION.SDK_INT >= 26) {
194                 browseIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.getExternalStorageDirectory());
195             }
196
197             // Request a file that can be opened.
198             browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
199
200             // Launch the file picker.  There is only one `startActivityForResult()`, so the request code is simply set to 0, but it must be run under `activity` so the request code is correct.
201             activity.startActivityForResult(browseIntent, 0);
202         });
203
204         // Hide the storage permission text view on API < 23 as permissions on older devices are automatically granted.
205         if (Build.VERSION.SDK_INT < 23) {
206             storagePermissionTextView.setVisibility(View.GONE);
207         }
208
209         // Return the alert dialog.
210         return alertDialog;
211     }
212 }