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.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;
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.
49 import com.stoutner.privacybrowser.R;
51 public class SaveLogcatDialog extends DialogFragment {
52 // Define the save logcat listener.
53 private SaveLogcatListener saveLogcatListener;
55 // The public interface is used to send information back to the parent activity.
56 public interface SaveLogcatListener {
57 void onSaveLogcat(DialogFragment dialogFragment);
61 public void onAttach(Context context) {
62 // Run the default commands.
63 super.onAttach(context);
65 // Get a handle for save logcat listener from the launching context.
66 saveLogcatListener = (SaveLogcatListener) context;
69 // `@SuppressLing("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
70 @SuppressLint("InflateParams")
73 public Dialog onCreateDialog(Bundle savedInstanceState) {
74 // Get a handle for the shared preferences.
75 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
77 // Get the screenshot and theme preferences.
78 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
79 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
81 // Use an alert dialog builder to create the alert dialog.
82 AlertDialog.Builder dialogBuilder;
84 // Get a handle for the activity.
85 Activity activity = getActivity();
87 // Remove the incorrect lint warning below that the activity might be null.
88 assert activity != null;
90 // Set the style according to the theme.
92 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogDark);
94 dialogBuilder = new AlertDialog.Builder(activity, R.style.PrivacyBrowserAlertDialogLight);
98 dialogBuilder.setTitle(R.string.save_logcat);
100 // Set the icon according to the theme.
102 dialogBuilder.setIcon(R.drawable.save_dialog_dark);
104 dialogBuilder.setIcon(R.drawable.save_dialog_light);
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));
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.
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);
121 // Create an alert dialog from the builder.
122 AlertDialog alertDialog = dialogBuilder.create();
124 // Remove the incorrect lint warning below that `getWindow()` might be null.
125 assert alertDialog.getWindow() != null;
127 // Disable screenshots if not allowed.
128 if (!allowScreenshots) {
129 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
132 // The alert dialog must be shown before items in the layout can be modified.
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);
141 // Create a string for the default file path.
142 String defaultFilePath;
144 // Get a handle for the context.
145 Context context = getContext();
147 // Remove the incorrect lint warning below that context might be null.
148 assert context != null;
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);
159 // Display the default file path.
160 fileNameEditText.setText(defaultFilePath);
162 // Update the status of the save button when the file name changes.
163 fileNameEditText.addTextChangedListener(new TextWatcher() {
165 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
170 public void onTextChanged(CharSequence s, int start, int before, int count) {
175 public void afterTextChanged(Editable s) {
176 // Enable the save button if a file name exists.
177 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
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);
186 // Set the intent MIME type to include all files so that everything is visible.
187 browseIntent.setType("*/*");
189 // Set the initial file name.
190 browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.privacy_browser_logcat_txt));
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());
197 // Request a file that can be opened.
198 browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
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);
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);
209 // Return the alert dialog.