X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FHttpAuthenticationDialog.java;h=c04bf8fff7db41a68c05763388c2de66c8cd74b3;hb=f0393ca22075be3e5fe9199c7db87381256236fa;hp=993c8c3d59d7369ab9a45e4c1b308b89a8ad5dcf;hpb=e75f230075b4059be6a9b6d27d8b6b202c74a6ff;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java index 993c8c3d..c04bf8ff 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2017 Soren Stoutner . + * Copyright © 2017-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -24,10 +24,9 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; +import android.content.SharedPreferences; import android.os.Bundle; -import android.support.annotation.NonNull; -// `AppCompatDialogFragment` is used instead of `DialogFragment` to avoid an error on API <=22. -import android.support.v7.app.AppCompatDialogFragment; +import android.preference.PreferenceManager; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.ForegroundColorSpan; @@ -38,14 +37,28 @@ import android.view.WindowManager; import android.widget.EditText; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22. + import com.stoutner.privacybrowser.R; -import com.stoutner.privacybrowser.activities.MainWebViewActivity; -public class HttpAuthenticationDialog extends AppCompatDialogFragment{ +public class HttpAuthenticationDialog extends DialogFragment{ + // `httpAuthenticationListener` is used in `onAttach()` and `onCreateDialog()` + private HttpAuthenticationListener httpAuthenticationListener; - // The private variables are used in `onCreate()` and `onCreateDialog()`. - private String httpAuthHost; - private String httpAuthRealm; + // The public interface is used to send information back to the parent activity. + public interface HttpAuthenticationListener { + void onHttpAuthenticationCancel(); + + void onHttpAuthenticationProceed(DialogFragment dialogFragment); + } + + public void onAttach(Context context) { + super.onAttach(context); + + // Get a handle for `httpAuthenticationListener` from `context`. + httpAuthenticationListener = (HttpAuthenticationListener) context; + } public static HttpAuthenticationDialog displayDialog(String host, String realm) { // Store the strings in a `Bundle`. @@ -59,49 +72,36 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ return thisHttpAuthenticationDialog; } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Save the host and realm in class variables. - httpAuthHost = getArguments().getString("Host"); - httpAuthRealm = getArguments().getString("Realm"); - } - - // The public interface is used to send information back to the parent activity. - public interface HttpAuthenticationListener { - void onHttpAuthenticationCancel(); - - void onHttpAuthenticationProceed(AppCompatDialogFragment dialogFragment); - } - - // `httpAuthenticationListener` is used in `onAttach()` and `onCreateDialog()` - private HttpAuthenticationListener httpAuthenticationListener; - - public void onAttach(Context context) { - super.onAttach(context); - - // Get a handle for `httpAuthenticationListener` from `context`. - try { - httpAuthenticationListener = (HttpAuthenticationListener) context; - } catch(ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement `HttpAuthenticationListener`."); - } - } - // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. @SuppressLint("InflateParams") @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { + // Remove the incorrect lint warnings that `getString()` might be null. + assert getArguments() != null; + + // Get the host and realm variables from the bundle. + String httpAuthHost = getArguments().getString("Host"); + String httpAuthRealm = getArguments().getString("Realm"); + + // Remove the incorrect lint warning that `getActivity()` might be null. + assert getActivity() != null; + // Get the activity's layout inflater. LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - // Use `AlertDialog.Builder` to create the `AlertDialog`. + // Use an alert dialog builder to create the alert dialog. AlertDialog.Builder dialogBuilder; + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + + // Get the screenshot and theme preferences. + boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + // Set the style according to the theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { // Set the dialog theme. dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark); @@ -122,46 +122,45 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ dialogBuilder.setView(layoutInflater.inflate(R.layout.http_authentication_dialog, null)); // Setup the negative button. - dialogBuilder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Call `onHttpAuthenticationCancel()` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationCancel(); - } + dialogBuilder.setNegativeButton(R.string.close, (DialogInterface dialog, int which) -> { + // Call `onHttpAuthenticationCancel()` and return the `DialogFragment` to the parent activity. + httpAuthenticationListener.onHttpAuthenticationCancel(); }); // Setup the positive button. - dialogBuilder.setPositiveButton(R.string.proceed, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Call `onHttpAuthenticationProceed()` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); - } + dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> { + // Call `onHttpAuthenticationProceed()` and return the `DialogFragment` to the parent activity. + httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); }); - // Create an `AlertDialog` from the `AlertDialog.Builder`. + // Create an alert dialog from the alert dialog builder. final AlertDialog alertDialog = dialogBuilder.create(); - // Remove the warning below that `setSoftInputMode` might produce `java.lang.NullPointerException`. + // Remove the warning below that `getWindow()` might be null. assert alertDialog.getWindow() != null; + // Disable screenshots if not allowed. + if (!allowScreenshots) { + alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + // Show the keyboard when the `AlertDialog` is displayed on the screen. alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); - // The `AlertDialog` needs to be shown before `setOnKeyListener()` can be called. + // The alert dialog needs to be shown before the contents can be modified. alertDialog.show(); // Get handles for the views in `alertDialog`. - TextView realmTextView = (TextView) alertDialog.findViewById(R.id.http_authentication_realm); - TextView hostTextView = (TextView) alertDialog.findViewById(R.id.http_authentication_host); - EditText usernameEditText = (EditText) alertDialog.findViewById(R.id.http_authentication_username); - EditText passwordEditText = (EditText) alertDialog.findViewById(R.id.http_authentication_password); + TextView realmTextView = alertDialog.findViewById(R.id.http_authentication_realm); + TextView hostTextView = alertDialog.findViewById(R.id.http_authentication_host); + EditText usernameEditText = alertDialog.findViewById(R.id.http_authentication_username); + EditText passwordEditText = alertDialog.findViewById(R.id.http_authentication_password); // Set the realm text. realmTextView.setText(httpAuthRealm); // Set the realm text color according to the theme. The deprecated `.getColor()` must be used until API >= 23. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { //noinspection deprecation realmTextView.setTextColor(getResources().getColor(R.color.gray_300)); } else { @@ -177,7 +176,7 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ ForegroundColorSpan blueColorSpan; // Set `blueColorSpan` according to the theme. The deprecated `getColor()` must be used until API >= 23. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { //noinspection deprecation blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400)); } else { @@ -192,44 +191,40 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ hostTextView.setText(hostStringBuilder); // Allow the `enter` key on the keyboard to trigger `onHttpAuthenticationProceed` from `usernameEditText`. - usernameEditText.setOnKeyListener(new View.OnKeyListener() { - public boolean onKey(View view, int keyCode, KeyEvent event) { - // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`. - if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { - // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); - - // Manually dismiss the `AlertDialog`. - alertDialog.dismiss(); - - // Consume the event. - return true; - } else { // If any other key was pressed, do not consume the event. - return false; - } + usernameEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> { + // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`. + if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { + // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity. + httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); + + // Manually dismiss the `AlertDialog`. + alertDialog.dismiss(); + + // Consume the event. + return true; + } else { // If any other key was pressed, do not consume the event. + return false; } }); // Allow the `enter` key on the keyboard to trigger `onHttpAuthenticationProceed()` from `passwordEditText`. - passwordEditText.setOnKeyListener(new View.OnKeyListener() { - public boolean onKey(View view, int keyCode, KeyEvent event) { - // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`. - if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { - // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); - - // Manually dismiss the `AlertDialog`. - alertDialog.dismiss(); - - // Consume the event. - return true; - } else { // If any other key was pressed, do not consume the event. - return false; - } + passwordEditText.setOnKeyListener((View view, int keyCode, KeyEvent event) -> { + // If the event is a key-down on the `enter` key, call `onHttpAuthenticationProceed()`. + if ((keyCode == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) { + // Trigger `onHttpAuthenticationProceed` and return the `DialogFragment` to the parent activity. + httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); + + // Manually dismiss the `AlertDialog`. + alertDialog.dismiss(); + + // Consume the event. + return true; + } else { // If any other key was pressed, do not consume the event. + return false; } }); // `onCreateDialog()` requires the return of an `AlertDialog`. return alertDialog; } -} +} \ No newline at end of file