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