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