]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/OpenDialog.java
Switch to the new Day/Night theme. https://redmine.stoutner.com/issues/522
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / OpenDialog.java
1 /*
2  * Copyright © 2019-2020 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.Activity;
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.content.res.Configuration;
32 import android.os.Build;
33 import android.os.Bundle;
34 import android.os.Environment;
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.appcompat.app.AlertDialog;
46 import androidx.core.content.ContextCompat;
47 import androidx.fragment.app.DialogFragment;
48 import androidx.preference.PreferenceManager;
49
50 import com.stoutner.privacybrowser.R;
51 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
52 import com.stoutner.privacybrowser.helpers.DownloadLocationHelper;
53
54 import java.io.File;
55
56 public class OpenDialog extends DialogFragment {
57     // Define the open listener.
58     private OpenListener openListener;
59
60     // The public interface is used to send information back to the parent activity.
61     public interface OpenListener {
62         void onOpen(DialogFragment dialogFragment);
63     }
64
65     @Override
66     public void onAttach(@NonNull Context context) {
67         // Run the default commands.
68         super.onAttach(context);
69
70         // Get a handle for the open listener from the launching context.
71         openListener = (OpenListener) context;
72     }
73
74     // `@SuppressLint("InflateParams")` removes the warning about using null as the parent view group when inflating the alert dialog.
75     @SuppressLint("InflateParams")
76     @Override
77     @NonNull
78     public Dialog onCreateDialog(Bundle savedInstanceState) {
79         // Get a handle for the activity and the context.
80         Activity activity = requireActivity();
81         Context context = requireContext();
82
83         // Use an alert dialog builder to create the alert dialog.
84         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context, R.style.PrivacyBrowserAlertDialog);
85
86         // Get the current theme status.
87         int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
88
89         // Set the icon according to the theme.
90         if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
91             dialogBuilder.setIcon(R.drawable.proxy_enabled_night);
92         } else {
93             dialogBuilder.setIcon(R.drawable.proxy_enabled_day);
94         }
95
96         // Set the title.
97         dialogBuilder.setTitle(R.string.open);
98
99         // Set the view.  The parent view is null because it will be assigned by the alert dialog.
100         dialogBuilder.setView(activity.getLayoutInflater().inflate(R.layout.open_dialog, null));
101
102         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
103         dialogBuilder.setNegativeButton(R.string.cancel, null);
104
105         // Set the open button listener.
106         dialogBuilder.setPositiveButton(R.string.open, (DialogInterface dialog, int which) -> {
107             // Return the dialog fragment to the parent activity.
108             openListener.onOpen(this);
109         });
110
111         // Create an alert dialog from the builder.
112         AlertDialog alertDialog = dialogBuilder.create();
113
114         // Remove the incorrect lint warning below that the window might be null.
115         assert alertDialog.getWindow() != null;
116
117         // Get a handle for the shared preferences.
118         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
119
120         // Get the screenshot preference.
121         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
122
123         // Disable screenshots if not allowed.
124         if (!allowScreenshots) {
125             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
126         }
127
128         // The alert dialog must be shown before items in the layout can be modified.
129         alertDialog.show();
130
131         // Get handles for the layout items.
132         EditText fileNameEditText = alertDialog.findViewById(R.id.file_name_edittext);
133         Button browseButton = alertDialog.findViewById(R.id.browse_button);
134         TextView fileDoesNotExistTextView = alertDialog.findViewById(R.id.file_does_not_exist_textview);
135         TextView storagePermissionTextView = alertDialog.findViewById(R.id.storage_permission_textview);
136         Button openButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
137
138         // Remove the incorrect lint warnings below that the views might be null.
139         assert fileNameEditText != null;
140         assert browseButton != null;
141         assert fileDoesNotExistTextView != null;
142         assert storagePermissionTextView != null;
143
144         // Update the status of the open button when the file name changes.
145         fileNameEditText.addTextChangedListener(new TextWatcher() {
146             @Override
147             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
148                 // Do nothing.
149             }
150
151             @Override
152             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
153                 // Do nothing.
154             }
155
156             @Override
157             public void afterTextChanged(Editable editable) {
158                 // Get the current file name.
159                 String fileNameString = fileNameEditText.getText().toString();
160
161                 // Convert the file name string to a file.
162                 File file = new File(fileNameString);
163
164                 // Check to see if the file exists.
165                 if (file.exists()) {  // The file exists.
166                     // Hide the notification that the file does not exist.
167                     fileDoesNotExistTextView.setVisibility(View.GONE);
168
169                     // Enable the open button.
170                     openButton.setEnabled(true);
171                 } else {  // The file does not exist.
172                     // Show the notification that the file does not exist.
173                     fileDoesNotExistTextView.setVisibility(View.VISIBLE);
174
175                     // Disable the open button.
176                     openButton.setEnabled(false);
177                 }
178             }
179         });
180
181         // Instantiate the download location helper.
182         DownloadLocationHelper downloadLocationHelper = new DownloadLocationHelper();
183
184         // Get the default file path.
185         String defaultFilePath = downloadLocationHelper.getDownloadLocation(context) + "/";
186
187         // Display the default file path.
188         fileNameEditText.setText(defaultFilePath);
189
190         // Move the cursor to the end of the default file path.
191         fileNameEditText.setSelection(defaultFilePath.length());
192
193         // Hide the storage permission text view if the permission has already been granted.
194         if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
195             storagePermissionTextView.setVisibility(View.GONE);
196         }
197
198         // Handle clicks on the browse button.
199         browseButton.setOnClickListener((View view) -> {
200             // Create the file picker intent.
201             Intent browseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
202
203             // Set the intent MIME type to include all files so that everything is visible.
204             browseIntent.setType("*/*");
205
206             // Set the initial directory if the minimum API >= 26.
207             if (Build.VERSION.SDK_INT >= 26) {
208                 browseIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.getExternalStorageDirectory());
209             }
210
211             // Start the file picker.  This must be started under `activity` to that the request code is returned correctly.
212             activity.startActivityForResult(browseIntent, MainWebViewActivity.BROWSE_OPEN_REQUEST_CODE);
213         });
214
215         // Return the alert dialog.
216         return alertDialog;
217     }
218 }