]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/AddDomainDialog.java
Create a dark theme for the `AlertDialogs`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / AddDomainDialog.java
1 /*
2  * Copyright © 2017 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 // We have to use `AppCompatDialogFragment` instead of `DialogFragment` or an error is produced on API <= 22.
30 import android.support.annotation.NonNull;
31 import android.support.v7.app.AppCompatDialogFragment;
32 import android.view.KeyEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.widget.EditText;
36
37 import com.stoutner.privacybrowser.R;
38 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
39
40 public class AddDomainDialog extends AppCompatDialogFragment {
41     // The public interface is used to send information back to the parent activity.
42     public interface AddDomainListener {
43         void onAddDomain(AppCompatDialogFragment dialogFragment);
44     }
45
46     // `addDomainListener` is used in `onAttach()` and `onCreateDialog()`.
47     private AddDomainListener addDomainListener;
48
49
50     public void onAttach(Context context) {
51         super.onAttach(context);
52
53         // Get a handle for `AddDomainListener` from `context`.
54         try {
55             addDomainListener = (AddDomainListener) context;
56         } catch(ClassCastException exception) {
57             throw new ClassCastException(context.toString() + " must implement `AddDomainListener`.");
58         }
59     }
60
61     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
62     @SuppressLint("InflateParams")
63     @Override
64     @NonNull
65     public Dialog onCreateDialog(Bundle savedInstanceState) {
66         // Use `AlertDialog.Builder` to create the `AlertDialog`.
67         AlertDialog.Builder dialogBuilder;
68
69         // Set the style according to the theme.
70         if (MainWebViewActivity.darkTheme) {
71             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
72         } else {
73             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
74         }
75
76         // Set the title.
77         dialogBuilder.setTitle(R.string.add_domain);
78
79         // Set the view.  The parent view is `null` because it will be assigned by the `AlertDialog`.
80         dialogBuilder.setView(getActivity().getLayoutInflater().inflate(R.layout.add_domain_dialog, null));
81
82         // Set an `onClick()` listener for the negative button.
83         dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
84             @Override
85             public void onClick(DialogInterface dialog, int which) {
86                 // Do nothing.  The `AlertDialog` will close automatically.
87             }
88         });
89
90         // Set an `onClick()` listener for the positive button.
91         dialogBuilder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
92             @Override
93             public void onClick(DialogInterface dialog, int which) {
94                 // Return the `DialogFragment` to the parent activity on add.
95                 addDomainListener.onAddDomain(AddDomainDialog.this);
96             }
97         });
98
99         // Create an `AlertDialog` from the `AlertDialog.Builder`.
100         final AlertDialog alertDialog = dialogBuilder.create();
101
102         // Remove the warning below the `setSoftInputMode` might produce `java.lang.NullPointerException`.
103         assert alertDialog.getWindow() != null;
104
105         // Show the keyboard when the `AlertDialog` is displayed on the screen.
106         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
107
108         // We need to show the `AlertDialog` before we can edit the contents.
109         alertDialog.show();
110
111         // Get a handle for `domain_name_edittext`.
112         EditText addDomainEditText = (EditText) alertDialog.findViewById(R.id.domain_name_edittext);
113
114         // Get the current domain from `formattedUrlString`.
115         Uri currentUri = Uri.parse(MainWebViewActivity.formattedUrlString);
116         addDomainEditText.setText(currentUri.getHost());
117
118         // Allow the `enter` key on the keyboard to create the domain from `add_domain_edittext`.
119         addDomainEditText.setOnKeyListener(new View.OnKeyListener() {
120             public boolean onKey(View view, int keyCode, KeyEvent event) {
121                 // If the event is a key-down on the `enter` key, select the `PositiveButton` `Add`.
122                 if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
123                     // Trigger `addDomainListener` and return the `DialogFragment` to the parent activity.
124                     addDomainListener.onAddDomain(AddDomainDialog.this);
125                     // Manually dismiss the `AlertDialog`.
126                     alertDialog.dismiss();
127                     // Consume the event.
128                     return true;
129                 } else { // If any other key was pressed, do not consume the event.
130                     return false;
131                 }
132             }
133         });
134
135         // `onCreateDialog()` requires the return of an `AlertDialog`.
136         return alertDialog;
137     }
138 }