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.pm.PackageManager;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.os.Environment;
33 import android.provider.DocumentsContract;
34 import android.text.Editable;
35 import android.text.TextWatcher;
36 import android.view.View;
37 import android.view.WindowManager;
38 import android.widget.Button;
39 import android.widget.EditText;
40 import android.widget.TextView;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
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 public class SaveLogcatDialog extends DialogFragment {
50 // Instantiate the class variables.
51 private SaveLogcatListener saveLogcatListener;
52 private Context parentContext;
54 // The public interface is used to send information back to the parent activity.
55 public interface SaveLogcatListener {
56 void onSaveLogcat(DialogFragment dialogFragment);
59 public void onAttach(Context context) {
60 // Run the default commands.
61 super.onAttach(context);
63 // Store a handle for the context.
64 parentContext = context;
66 // Get a handle for `SaveLogcatListener` from the launching context.
67 saveLogcatListener = (SaveLogcatListener) context;
70 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
71 @SuppressLint("InflateParams")
74 public Dialog onCreateDialog(Bundle savedInstanceState) {
75 // Use an alert dialog builder to create the alert dialog.
76 AlertDialog.Builder dialogBuilder;
78 // Set the style according to the theme.
79 if (MainWebViewActivity.darkTheme) {
80 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
82 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
86 dialogBuilder.setTitle(R.string.save_logcat);
88 // Remove the incorrect lint warning that `getActivity().getLayoutInflater()` might be null.
89 assert getActivity() != null;
91 // Set the view. The parent view is null because it will be assigned by the alert dialog.
92 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.save_logcat_dialog, null));
94 // Set the icon according to the theme.
95 if (MainWebViewActivity.darkTheme) {
96 dialogBuilder.setIcon(R.drawable.save_dialog_dark);
98 dialogBuilder.setIcon(R.drawable.save_dialog_light);
101 // Set the cancel button listener.
102 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
103 // Do nothing. The alert dialog will close automatically.
106 // Set the save button listener.
107 dialogBuilder.setPositiveButton(R.string.save, (DialogInterface dialog, int which) -> {
108 // Return the dialog fragment to the parent activity.
109 saveLogcatListener.onSaveLogcat(this);
112 // Create an alert dialog from the builder.
113 AlertDialog alertDialog = dialogBuilder.create();
115 // Remove the incorrect lint warning below that `getWindow().addFlags()` might be null.
116 assert alertDialog.getWindow() != null;
118 // Disable screenshots if not allowed.
119 if (!MainWebViewActivity.allowScreenshots) {
120 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
123 // The alert dialog must be shown before items in the layout can be modified.
126 // Get handles for the layout items.
127 EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext);
128 Button browseButton = alertDialog.findViewById(R.id.browse_button);
129 TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview);
130 Button saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
132 // Create a string for the default file path.
133 String defaultFilePath;
135 // Set the default file path according to the storage permission state.
136 if (ContextCompat.checkSelfPermission(parentContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
137 // Set the default file path to use the external public directory.
138 defaultFilePath = Environment.getExternalStorageDirectory() + "/" + getString(R.string.privacy_browser_logcat_txt);
139 } else { // The storage permission has not been granted.
140 // Set the default file path to use the external private directory.
141 defaultFilePath = parentContext.getExternalFilesDir(null) + "/" + getString(R.string.privacy_browser_logcat_txt);
144 // Display the default file path.
145 fileNameEditText.setText(defaultFilePath);
147 // Update the status of the save button when the file name changes.
148 fileNameEditText.addTextChangedListener(new TextWatcher() {
150 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
155 public void onTextChanged(CharSequence s, int start, int before, int count) {
160 public void afterTextChanged(Editable s) {
161 // Enable the save button if a file name exists.
162 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
166 // Handle clicks on the browse button.
167 browseButton.setOnClickListener((View view) -> {
168 // Create the file picker intent.
169 Intent browseIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
171 // Set the intent MIME type to include all files so that everything is visible.
172 browseIntent.setType("*/*");
174 // Set the initial file name.
175 browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.privacy_browser_logcat_txt));
177 // Set the initial directory if the minimum API >= 26.
178 if (Build.VERSION.SDK_INT >= 26) {
179 browseIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.getExternalStorageDirectory());
182 // Request a file that can be opened.
183 browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
185 // Launch the file picker. There is only one `startActivityForResult()`, so the request code is simply set to 0.
186 startActivityForResult(browseIntent, 0);
189 // Hide the storage permission text view on API < 23 as permissions on older devices are automatically granted.
190 if (Build.VERSION.SDK_INT < 23) {
191 storagePermissionTextView.setVisibility(View.GONE);
194 // Return the alert dialog.