]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/AddDomainDialog.java
59c36ede3ae0270e8c9899663bef095ab018487c
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / AddDomainDialog.java
1 /*
2  * Copyright © 2017-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.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.Context;
26 import android.content.DialogInterface;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.text.Editable;
30 import android.text.TextWatcher;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.WindowManager;
34 import android.widget.Button;
35 import android.widget.EditText;
36 import android.widget.TextView;
37
38 import androidx.annotation.NonNull;
39 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
40
41 import com.stoutner.privacybrowser.R;
42 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
43 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
44
45 public class AddDomainDialog extends DialogFragment {
46     // The public interface is used to send information back to the parent activity.
47     public interface AddDomainListener {
48         void onAddDomain(DialogFragment dialogFragment);
49     }
50
51     // The add domain listener is used in `onAttach()` and `onCreateDialog()`.
52     private AddDomainListener addDomainListener;
53
54     @Override
55     public void onAttach(Context context) {
56         // Run the default commands.
57         super.onAttach(context);
58
59         // Get a handle for the listener from the launching context.
60         addDomainListener = (AddDomainListener) context;
61     }
62
63     public static AddDomainDialog addDomain(String url) {
64         // Create an arguments bundle.
65         Bundle argumentsBundle = new Bundle();
66
67         // Store the URL in the bundle.
68         argumentsBundle.putString("url", url);
69
70         // Create a new instance of the dialog.
71         AddDomainDialog addDomainDialog = new AddDomainDialog();
72
73         // Add the bundle to the dialog.
74         addDomainDialog.setArguments(argumentsBundle);
75
76         // Return the new dialog.
77         return addDomainDialog;
78     }
79
80     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
81     @SuppressLint("InflateParams")
82     @Override
83     @NonNull
84     public Dialog onCreateDialog(Bundle savedInstanceState) {
85         // Get the arguments.
86         Bundle arguments = getArguments();
87
88         // Remove the incorrect lint warning below that the arguments might be null.
89         assert arguments != null;
90
91         // Get the URL from the bundle.
92         String url = arguments.getString("url");
93
94         // Use an alert dialog builder to create the alert dialog.
95         AlertDialog.Builder dialogBuilder;
96
97         // Set the style according to the theme.
98         if (MainWebViewActivity.darkTheme) {
99             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
100         } else {
101             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
102         }
103
104         // Set the title.
105         dialogBuilder.setTitle(R.string.add_domain);
106
107         // Remove the incorrect lint warning below that `getActivity()` might be null.
108         assert getActivity() != null;
109
110         // Set the view.  The parent view is `null` because it will be assigned by the alert dialog.
111         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.add_domain_dialog, null));
112
113         // Set a listener for the negative button.
114         dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
115             // Do nothing.  The alert dialog will close automatically.
116         });
117
118         // Set a listener for the positive button.
119         dialogBuilder.setPositiveButton(R.string.add, (DialogInterface dialog, int which) -> {
120             // Return the dialog fragment to the parent activity on add.
121             addDomainListener.onAddDomain(this);
122         });
123
124         // Create an alert dialog from the builder.
125         final AlertDialog alertDialog = dialogBuilder.create();
126
127         // Remove the warning below that `getWindow()` might be null.
128         assert alertDialog.getWindow() != null;
129
130         // Disable screenshots if not allowed.
131         if (!MainWebViewActivity.allowScreenshots) {
132             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
133         }
134
135         // Show the keyboard when the alert dialog is displayed on the screen.
136         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
137
138         // The alert dialog must be shown before the contents can be edited.
139         alertDialog.show();
140
141         // Initialize `domainsDatabaseHelper`.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
142         final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
143
144         // Get handles for the views in the alert dialog.
145         final EditText addDomainEditText = alertDialog.findViewById(R.id.domain_name_edittext);
146         final TextView domainNameAlreadyExistsTextView = alertDialog.findViewById(R.id.domain_name_already_exists_textview);
147         final Button addButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
148
149         //  Update the status of the warning text and the add button.
150         addDomainEditText.addTextChangedListener(new TextWatcher() {
151             @Override
152             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
153                 // Do nothing.
154             }
155
156             @Override
157             public void onTextChanged(CharSequence s, int start, int before, int count) {
158                 // Do nothing.
159             }
160
161             @Override
162             public void afterTextChanged(Editable s) {
163                 if (domainsDatabaseHelper.getCursorForDomainName(addDomainEditText.getText().toString()).getCount() >0) {  // The domain already exists.
164                     // Show the warning text.
165                     domainNameAlreadyExistsTextView.setVisibility(View.VISIBLE);
166
167                     // Disable the add button.
168                     addButton.setEnabled(false);
169                 } else {  // The domain do not yet exist.
170                     // Hide the warning text.
171                     domainNameAlreadyExistsTextView.setVisibility(View.GONE);
172
173                     // Enable the add button.
174                     addButton.setEnabled(true);
175                 }
176             }
177         });
178
179         // Convert the URL to a URI.
180         Uri currentUri = Uri.parse(url);
181
182         // Display the host in the add domain edit text.
183         addDomainEditText.setText(currentUri.getHost());
184
185         // Allow the enter key on the keyboard to create the domain from the add domain edit text.
186         addDomainEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
187             // If the event is a key-down on the enter key, select the `PositiveButton` `Add`.
188             if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
189                 // Trigger `addDomainListener` and return the dialog fragment to the parent activity.
190                 addDomainListener.onAddDomain(this);
191
192                 // Manually dismiss the alert dialog.
193                 alertDialog.dismiss();
194
195                 // Consume the event.
196                 return true;
197             } else { // If any other key was pressed, do not consume the event.
198                 return false;
199             }
200         });
201
202         // Return the alert dialog.
203         return alertDialog;
204     }
205 }