2 * Copyright © 2016-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.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;
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.
48 import com.stoutner.privacybrowser.R;
50 public class SaveLogcatDialog extends DialogFragment {
51 // Instantiate the class variables.
52 private SaveLogcatListener saveLogcatListener;
53 private Context parentContext;
55 // The public interface is used to send information back to the parent activity.
56 public interface SaveLogcatListener {
57 void onSaveLogcat(DialogFragment dialogFragment);
60 public void onAttach(Context context) {
61 // Run the default commands.
62 super.onAttach(context);
64 // Store a handle for the context.
65 parentContext = context;
67 // Get a handle for `SaveLogcatListener` from the launching context.
68 saveLogcatListener = (SaveLogcatListener) context;
71 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
72 @SuppressLint("InflateParams")
75 public Dialog onCreateDialog(Bundle savedInstanceState) {
76 // Use an alert dialog builder to create the alert dialog.
77 AlertDialog.Builder dialogBuilder;
79 // Get a handle for the shared preferences.
80 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
82 // Get the screenshot and theme preferences.
83 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
84 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
86 // Set the style according to the theme.
88 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
90 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
94 dialogBuilder.setTitle(R.string.save_logcat);
96 // Remove the incorrect lint warning that `getActivity().getLayoutInflater()` might be null.
97 assert getActivity() != null;
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));
102 // Set the icon according to the theme.
104 dialogBuilder.setIcon(R.drawable.save_dialog_dark);
106 dialogBuilder.setIcon(R.drawable.save_dialog_light);
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.
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);
120 // Create an alert dialog from the builder.
121 AlertDialog alertDialog = dialogBuilder.create();
123 // Remove the incorrect lint warning below that `getWindow().addFlags()` might be null.
124 assert alertDialog.getWindow() != null;
126 // Disable screenshots if not allowed.
127 if (!allowScreenshots) {
128 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
131 // The alert dialog must be shown before items in the layout can be modified.
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);
140 // Create a string for the default file path.
141 String defaultFilePath;
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);
152 // Display the default file path.
153 fileNameEditText.setText(defaultFilePath);
155 // Update the status of the save button when the file name changes.
156 fileNameEditText.addTextChangedListener(new TextWatcher() {
158 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
163 public void onTextChanged(CharSequence s, int start, int before, int count) {
168 public void afterTextChanged(Editable s) {
169 // Enable the save button if a file name exists.
170 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
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);
179 // Set the intent MIME type to include all files so that everything is visible.
180 browseIntent.setType("*/*");
182 // Set the initial file name.
183 browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.privacy_browser_logcat_txt));
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());
190 // Request a file that can be opened.
191 browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
193 // Launch the file picker. There is only one `startActivityForResult()`, so the request code is simply set to 0.
194 startActivityForResult(browseIntent, 0);
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);
202 // Return the alert dialog.