2 * Copyright © 2017-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.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;
38 import androidx.annotation.NonNull;
39 // The AndroidX dialog fragment must be used or an error is produced on API <=22.
40 import androidx.fragment.app.DialogFragment;
42 import com.stoutner.privacybrowser.R;
43 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
44 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
46 public class AddDomainDialog extends DialogFragment {
47 // The public interface is used to send information back to the parent activity.
48 public interface AddDomainListener {
49 void onAddDomain(DialogFragment dialogFragment);
52 // `addDomainListener` is used in `onAttach()` and `onCreateDialog()`.
53 private AddDomainListener addDomainListener;
56 public void onAttach(Context context) {
57 // Run the default commands.
58 super.onAttach(context);
60 // Get a handle for the listener from the launching context.
61 addDomainListener = (AddDomainListener) context;
64 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
65 @SuppressLint("InflateParams")
68 public Dialog onCreateDialog(Bundle savedInstanceState) {
69 // Use an alert dialog builder to create the alert dialog.
70 AlertDialog.Builder dialogBuilder;
72 // Set the style according to the theme.
73 if (MainWebViewActivity.darkTheme) {
74 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
76 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
80 dialogBuilder.setTitle(R.string.add_domain);
82 // Remove the incorrect lint warning below that `getActivity()` might be null.
83 assert getActivity() != null;
85 // Set the view. The parent view is `null` because it will be assigned by the alert dialog.
86 dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.add_domain_dialog, null));
88 // Set a listener for the negative button.
89 dialogBuilder.setNegativeButton(R.string.cancel, (DialogInterface dialog, int which) -> {
90 // Do nothing. The `AlertDialog` will close automatically.
93 // Set a listener for the positive button.
94 dialogBuilder.setPositiveButton(R.string.add, (DialogInterface dialog, int which) -> {
95 // Return the `DialogFragment` to the parent activity on add.
96 addDomainListener.onAddDomain(AddDomainDialog.this);
99 // Create an alert dialog from the builder.
100 final AlertDialog alertDialog = dialogBuilder.create();
102 // Remove the warning below that `getWindow()` might be null.
103 assert alertDialog.getWindow() != null;
105 // Disable screenshots if not allowed.
106 if (!MainWebViewActivity.allowScreenshots) {
107 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
110 // Show the keyboard when the alert dialog is displayed on the screen.
111 alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
113 // The alert dialog must be shown before the contents can be edited.
116 // Initialize `domainsDatabaseHelper`. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
117 final DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
119 // Get handles for the views in the alert dialog.
120 final EditText addDomainEditText = alertDialog.findViewById(R.id.domain_name_edittext);
121 final TextView domainNameAlreadyExistsTextView = alertDialog.findViewById(R.id.domain_name_already_exists_textview);
122 final Button addButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
124 // Update the status of the warning text and the add button.
125 addDomainEditText.addTextChangedListener(new TextWatcher() {
127 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
132 public void onTextChanged(CharSequence s, int start, int before, int count) {
137 public void afterTextChanged(Editable s) {
138 if (domainsDatabaseHelper.getCursorForDomainName(addDomainEditText.getText().toString()).getCount() >0) { // The domain already exists.
139 // Show the warning text.
140 domainNameAlreadyExistsTextView.setVisibility(View.VISIBLE);
142 // Disable the add button.
143 addButton.setEnabled(false);
144 } else { // The domain do not yet exist.
145 // Hide the warning text.
146 domainNameAlreadyExistsTextView.setVisibility(View.GONE);
148 // Enable the add button.
149 addButton.setEnabled(true);
154 // Get the current domain from `formattedUrlString`.
155 Uri currentUri = Uri.parse(MainWebViewActivity.formattedUrlString);
156 addDomainEditText.setText(currentUri.getHost());
158 // Allow the `enter` key on the keyboard to create the domain from `add_domain_edittext`.
159 addDomainEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
160 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Add`.
161 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
162 // Trigger `addDomainListener` and return the `DialogFragment` to the parent activity.
163 addDomainListener.onAddDomain(AddDomainDialog.this);
164 // Manually dismiss the `AlertDialog`.
165 alertDialog.dismiss();
166 // Consume the event.
168 } else { // If any other key was pressed, do not consume the event.
173 // `onCreateDialog()` requires the return of an `AlertDialog`.