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