]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java
46af64e73bf361eee8ebf393f56568938cfd9016
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / HttpAuthenticationDialog.java
1 /*
2  * Copyright © 2017-2018 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 import android.support.annotation.NonNull;
29 // `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22.
30 import android.support.v7.app.AppCompatDialogFragment;
31 import android.text.SpannableStringBuilder;
32 import android.text.Spanned;
33 import android.text.style.ForegroundColorSpan;
34 import android.view.KeyEvent;
35 import android.view.LayoutInflater;
36 import android.view.View;
37 import android.view.WindowManager;
38 import android.widget.EditText;
39 import android.widget.TextView;
40
41 import com.stoutner.privacybrowser.R;
42 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
43
44 public class HttpAuthenticationDialog extends AppCompatDialogFragment{
45     // `httpAuthenticationListener` is used in `onAttach()` and `onCreateDialog()`
46     private HttpAuthenticationListener httpAuthenticationListener;
47
48     // The public interface is used to send information back to the parent activity.
49     public interface HttpAuthenticationListener {
50         void onHttpAuthenticationCancel();
51
52         void onHttpAuthenticationProceed(AppCompatDialogFragment dialogFragment);
53     }
54
55     public void onAttach(Context context) {
56         super.onAttach(context);
57
58         // Get a handle for `httpAuthenticationListener` from `context`.
59         httpAuthenticationListener = (HttpAuthenticationListener) context;
60     }
61
62     public static HttpAuthenticationDialog displayDialog(String host, String realm) {
63         // Store the strings in a `Bundle`.
64         Bundle argumentsBundle = new Bundle();
65         argumentsBundle.putString("Host", host);
66         argumentsBundle.putString("Realm", realm);
67
68         // Add `argumentsBundle` to this instance of `HttpAuthenticationDialog`.
69         HttpAuthenticationDialog thisHttpAuthenticationDialog = new HttpAuthenticationDialog();
70         thisHttpAuthenticationDialog.setArguments(argumentsBundle);
71         return thisHttpAuthenticationDialog;
72     }
73
74     // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
75     @SuppressLint("InflateParams")
76     @Override
77     @NonNull
78     public Dialog onCreateDialog(Bundle savedInstanceState) {
79         // Remove the incorrect lint warnings that `getString()` might be null.
80         assert getArguments() != null;
81
82         // Get the host and realm variables from the bundle.
83         String httpAuthHost = getArguments().getString("Host");
84         String httpAuthRealm = getArguments().getString("Realm");
85
86         // Remove the incorrect lint warning that `getActivity()` might be null.
87         assert getActivity() != null;
88
89         // Get the activity's layout inflater.
90         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
91
92         // Use an alert dialog builder to create the alert dialog.
93         AlertDialog.Builder dialogBuilder;
94
95         // Set the style according to the theme.
96         if (MainWebViewActivity.darkTheme) {
97             // Set the dialog theme.
98             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
99
100             // Set the icon.
101             dialogBuilder.setIcon(R.drawable.lock_dark);
102         } else {
103             // Set the dialog theme.
104             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
105
106             // Set the icon.
107             dialogBuilder.setIcon(R.drawable.lock_light);
108         }
109
110         // Set the title.
111         dialogBuilder.setTitle(R.string.http_authentication);
112
113         // Set the layout.  The parent view is `null` because it will be assigned by `AlertDialog`.
114         dialogBuilder.setView(layoutInflater.inflate(R.layout.http_authentication_dialog, null));
115
116         // Setup the negative button.
117         dialogBuilder.setNegativeButton(R.string.close, (DialogInterface dialog, int which) -> {
118             // Call `onHttpAuthenticationCancel()` and return the `DialogFragment` to the parent activity.
119             httpAuthenticationListener.onHttpAuthenticationCancel();
120         });
121
122         // Setup the positive button.
123         dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> {
124             // Call `onHttpAuthenticationProceed()` and return the `DialogFragment` to the parent activity.
125             httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this);
126         });
127
128         // Create an alert dialog from the alert dialog builder.
129         final AlertDialog alertDialog = dialogBuilder.create();
130
131         // Remove the warning below that `getWindow()` might be null.
132         assert alertDialog.getWindow() != null;
133
134         // Disable screenshots if not allowed.
135         if (!MainWebViewActivity.allowScreenshots) {
136             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
137         }
138
139         // Show the keyboard when the `AlertDialog` is displayed on the screen.
140         alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
141
142         // The alert dialog needs to be shown before the contents can be modified.
143         alertDialog.show();
144
145         // Get handles for the views in `alertDialog`.
146         TextView realmTextView = alertDialog.findViewById(R.id.http_authentication_realm);
147         TextView hostTextView = alertDialog.findViewById(R.id.http_authentication_host);
148         EditText usernameEditText = alertDialog.findViewById(R.id.http_authentication_username);
149         EditText passwordEditText = alertDialog.findViewById(R.id.http_authentication_password);
150
151         // Set the realm text.
152         realmTextView.setText(httpAuthRealm);
153
154         // Set the realm text color according to the theme.  The deprecated `.getColor()` must be used until API >= 23.
155         if (MainWebViewActivity.darkTheme) {
156             //noinspection deprecation
157             realmTextView.setTextColor(getResources().getColor(R.color.gray_300));
158         } else {
159             //noinspection deprecation
160             realmTextView.setTextColor(getResources().getColor(R.color.black));
161         }
162
163         // Initialize the host label and the `SpannableStringBuilder`.
164         String hostLabel = getString(R.string.host) + "  ";
165         SpannableStringBuilder hostStringBuilder = new SpannableStringBuilder(hostLabel + httpAuthHost);
166
167         // Create a blue `ForegroundColorSpan`.
168         ForegroundColorSpan blueColorSpan;
169
170         // Set `blueColorSpan` according to the theme.  The deprecated `getColor()` must be used until API >= 23.
171         if (MainWebViewActivity.darkTheme) {
172             //noinspection deprecation
173             blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
174         } else {
175             //noinspection deprecation
176             blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
177         }
178
179         // Setup the span to display the host name in blue.  `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
180         hostStringBuilder.setSpan(blueColorSpan, hostLabel.length(), hostStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
181
182         // Set the host text.
183         hostTextView.setText(hostStringBuilder);
184
185         // Allow the `enter` key on the keyboard to trigger `onHttpAuthenticationProceed` from `usernameEditText`.
186         usernameEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
187             // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`.
188             if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
189                 // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity.
190                 httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this);
191
192                 // Manually dismiss the `AlertDialog`.
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         // Allow the `enter` key on the keyboard to trigger `onHttpAuthenticationProceed()` from `passwordEditText`.
203         passwordEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> {
204             // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`.
205             if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
206                 // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity.
207                 httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this);
208
209                 // Manually dismiss the `AlertDialog`.
210                 alertDialog.dismiss();
211
212                 // Consume the event.
213                 return true;
214             } else {  // If any other key was pressed, do not consume the event.
215                 return false;
216             }
217         });
218
219         // `onCreateDialog()` requires the return of an `AlertDialog`.
220         return alertDialog;
221     }
222 }