]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/SaveLogcatDialog.java
Add a logcat activity. https://redmine.stoutner.com/issues/264
[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.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.support.annotation.NonNull;
35 import android.support.v4.content.ContextCompat;
36 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.  It is also required for the browser button to work correctly.
37 import android.support.v7.app.AppCompatDialogFragment;
38 import android.text.Editable;
39 import android.text.TextWatcher;
40 import android.view.View;
41 import android.view.WindowManager;
42 import android.widget.Button;
43 import android.widget.EditText;
44 import android.widget.TextView;
45
46 import com.stoutner.privacybrowser.R;
47 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
48
49 public class SaveLogcatDialog extends AppCompatDialogFragment {
50     // Instantiate the class variables.
51     private SaveLogcatListener saveLogcatListener;
52     private Context parentContext;
53
54     // The public interface is used to send information back to the parent activity.
55     public interface SaveLogcatListener {
56         void onSaveLogcat(AppCompatDialogFragment dialogFragment);
57     }
58
59     public void onAttach(Context context) {
60         // Run the default commands.
61         super.onAttach(context);
62
63         // Store a handle for the context.
64         parentContext = context;
65
66         // Get a handle for `SaveLogcatListener` from the launching context.
67         saveLogcatListener = (SaveLogcatListener) context;
68     }
69
70     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
71     @SuppressLint("InflateParams")
72     @Override
73     @NonNull
74     public Dialog onCreateDialog(Bundle savedInstanceState) {
75         // Use an alert dialog builder to create the alert dialog.
76         AlertDialog.Builder dialogBuilder;
77
78         // Set the style according to the theme.
79         if (MainWebViewActivity.darkTheme) {
80             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
81         } else {
82             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
83         }
84
85         // Set the title.
86         dialogBuilder.setTitle(R.string.save_logcat);
87
88         // Remove the incorrect lint warning that `getActivity().getLayoutInflater()` might be null.
89         assert getActivity() != null;
90
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));
93
94         // Set the icon according to the theme.
95         if (MainWebViewActivity.darkTheme) {
96             dialogBuilder.setIcon(R.drawable.save_dialog_dark);
97         } else {
98             dialogBuilder.setIcon(R.drawable.save_dialog_light);
99         }
100
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.
104         });
105
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);
110         });
111
112         // Create an alert dialog from the builder.
113         AlertDialog alertDialog = dialogBuilder.create();
114
115         // Remove the incorrect lint warning below that `getWindow().addFlags()` might be null.
116         assert alertDialog.getWindow() != null;
117
118         // Disable screenshots if not allowed.
119         if (!MainWebViewActivity.allowScreenshots) {
120             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
121         }
122
123         // The alert dialog must be shown before items in the layout can be modified.
124         alertDialog.show();
125
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);
131
132         // Create a string for the default file path.
133         String defaultFilePath;
134
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);
142         }
143
144         // Display the default file path.
145         fileNameEditText.setText(defaultFilePath);
146
147         // Update the status of the save button when the file name changes.
148         fileNameEditText.addTextChangedListener(new TextWatcher() {
149             @Override
150             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
151                 // Do nothing.
152             }
153
154             @Override
155             public void onTextChanged(CharSequence s, int start, int before, int count) {
156                 // Do nothing.
157             }
158
159             @Override
160             public void afterTextChanged(Editable s) {
161                 // Enable the save button if a file name exists.
162                 saveButton.setEnabled(!fileNameEditText.getText().toString().isEmpty());
163             }
164         });
165
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);
170
171             // Set the intent MIME type to include all files so that everything is visible.
172             browseIntent.setType("*/*");
173
174             // Set the initial file name.
175             browseIntent.putExtra(Intent.EXTRA_TITLE, getString(R.string.privacy_browser_logcat_txt));
176
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());
180             }
181
182             // Request a file that can be opened.
183             browseIntent.addCategory(Intent.CATEGORY_OPENABLE);
184
185             // Launch the file picker.  There is only one `startActivityForResult()`, so the request code is simply set to 0.
186             startActivityForResult(browseIntent, 0);
187         });
188
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);
192         }
193
194         // Return the alert dialog.
195         return alertDialog;
196     }
197 }