X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FHttpAuthenticationDialog.java;h=9d7c0f3822d6b5ab199711bad7b3609318c69c1b;hp=6110ed541b8dadf73b3226f8cef7a7935b79a388;hb=a9b4d8c78a305c2602ced2058702254ea4e3b79b;hpb=0a5d2eabceeafb49a957598538aa74d4f11dfce0 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 6110ed54..9d7c0f38 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-2018 Soren Stoutner . + * Copyright © 2017-2020 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -20,83 +20,91 @@ package com.stoutner.privacybrowser.dialogs; import android.annotation.SuppressLint; -import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.content.res.Configuration; 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; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; +import android.view.Window; import android.view.WindowManager; +import android.webkit.HttpAuthHandler; import android.widget.EditText; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; + import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.MainWebViewActivity; +import com.stoutner.privacybrowser.fragments.WebViewTabFragment; +import com.stoutner.privacybrowser.views.NestedScrollWebView; -public class HttpAuthenticationDialog extends AppCompatDialogFragment{ - - // The private variables are used in `onCreate()` and `onCreateDialog()`. - private String httpAuthHost; - private String httpAuthRealm; +public class HttpAuthenticationDialog extends DialogFragment{ + // Define the class variables. + private EditText usernameEditText; + private EditText passwordEditText; - public static HttpAuthenticationDialog displayDialog(String host, String realm) { - // Store the strings in a `Bundle`. + public static HttpAuthenticationDialog displayDialog(String host, String realm, long webViewFragmentId) { + // Create an arguments bundle. Bundle argumentsBundle = new Bundle(); - argumentsBundle.putString("Host", host); - argumentsBundle.putString("Realm", realm); - // Add `argumentsBundle` to this instance of `HttpAuthenticationDialog`. + // Store the variables in the bundle. + argumentsBundle.putString("host", host); + argumentsBundle.putString("realm", realm); + argumentsBundle.putLong("webview_fragment_id", webViewFragmentId); + + // Create a new instance of the HTTP authentication dialog. HttpAuthenticationDialog thisHttpAuthenticationDialog = new HttpAuthenticationDialog(); + + // Add the arguments bundle to the new dialog. thisHttpAuthenticationDialog.setArguments(argumentsBundle); + + // Return the new dialog. return thisHttpAuthenticationDialog; } + // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`. + @SuppressLint("InflateParams") @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + @NonNull + public Dialog onCreateDialog(Bundle savedInstanceState) { + // Get a handle for the arguments. + Bundle arguments = getArguments(); - // Remove the incorrect lint warnings that `getString()` might be null. - assert getArguments() != null; + // Remove the incorrect lint warning below that arguments might be null. + assert arguments != null; - // Save the host and realm in class variables. - httpAuthHost = getArguments().getString("Host"); - httpAuthRealm = getArguments().getString("Realm"); - } + // Get the variables from the bundle. + String httpAuthHost = arguments.getString("host"); + String httpAuthRealm = arguments.getString("realm"); + long webViewFragmentId = arguments.getLong("webview_fragment_id"); - // The public interface is used to send information back to the parent activity. - public interface HttpAuthenticationListener { - void onHttpAuthenticationCancel(); + // Get the current position of this WebView fragment. + int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId); - void onHttpAuthenticationProceed(AppCompatDialogFragment dialogFragment); - } + // Get the WebView tab fragment. + WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition); - // `httpAuthenticationListener` is used in `onAttach()` and `onCreateDialog()` - private HttpAuthenticationListener httpAuthenticationListener; + // Get the fragment view. + View fragmentView = webViewTabFragment.getView(); - public void onAttach(Context context) { - super.onAttach(context); + // Remove the incorrect lint warning below that the fragment view might be null. + assert fragmentView != null; - // Get a handle for `httpAuthenticationListener` from `context`. - try { - httpAuthenticationListener = (HttpAuthenticationListener) context; - } catch(ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement `HttpAuthenticationListener`."); - } - } + // Get a handle for the current WebView. + NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview); + + // Get a handle for the HTTP authentication handler. + HttpAuthHandler httpAuthHandler = nestedScrollWebView.getHttpAuthHandler(); - // `@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 warning that `getActivity()` might be null. assert getActivity() != null; @@ -104,21 +112,16 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ LayoutInflater layoutInflater = getActivity().getLayoutInflater(); // Use an alert dialog builder to create the alert dialog. - AlertDialog.Builder dialogBuilder; + AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialog); - // Set the style according to the theme. - if (MainWebViewActivity.darkTheme) { - // Set the dialog theme. - dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark); + // Get the current theme status. + int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - // Set the icon. - dialogBuilder.setIcon(R.drawable.lock_dark); + // Set the icon according to the theme. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { + dialogBuilder.setIcon(R.drawable.lock_night); } else { - // Set the dialog theme. - dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight); - - // Set the icon. - dialogBuilder.setIcon(R.drawable.lock_light); + dialogBuilder.setIcon(R.drawable.lock_day); } // Set the title. @@ -127,50 +130,67 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ // Set the layout. The parent view is `null` because it will be assigned by `AlertDialog`. dialogBuilder.setView(layoutInflater.inflate(R.layout.http_authentication_dialog, null)); - // Setup the negative button. + // Setup the close button. dialogBuilder.setNegativeButton(R.string.close, (DialogInterface dialog, int which) -> { - // Call `onHttpAuthenticationCancel()` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationCancel(); + // Cancel the HTTP authentication request. + httpAuthHandler.cancel(); + + // Reset the HTTP authentication handler. + nestedScrollWebView.resetHttpAuthHandler(); }); - // Setup the positive button. + // Setup the proceed button. dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> { - // Call `onHttpAuthenticationProceed()` and return the `DialogFragment` to the parent activity. - httpAuthenticationListener.onHttpAuthenticationProceed(HttpAuthenticationDialog.this); + // Send the login information + login(httpAuthHandler); + + // Reset the HTTP authentication handler. + nestedScrollWebView.resetHttpAuthHandler(); }); // Create an alert dialog from the alert dialog builder. final AlertDialog alertDialog = dialogBuilder.create(); - // Remove the warning below that `getWindow()` might be null. - assert alertDialog.getWindow() != null; + // Get the alert dialog window. + Window dialogWindow = alertDialog.getWindow(); + + // Remove the incorrect lint warning below that the dialog window might be null. + assert dialogWindow != null; + + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + + // Get the screenshot preference. + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); // Disable screenshots if not allowed. - if (!MainWebViewActivity.allowScreenshots) { + 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); + // Display the keyboard. + dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); // The alert dialog needs to be shown before the contents can be modified. alertDialog.show(); - // Get handles for the views in `alertDialog`. + // Get handles for the views. 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); + usernameEditText = alertDialog.findViewById(R.id.http_authentication_username); + passwordEditText = alertDialog.findViewById(R.id.http_authentication_password); + + // Remove the incorrect lint warnings below that the views might be null. + assert realmTextView != null; + assert hostTextView != null; // 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) { - //noinspection deprecation + // Set the realm text color according to the theme. The deprecated `getResources()` must be used until API >= 23. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { realmTextView.setTextColor(getResources().getColor(R.color.gray_300)); } else { - //noinspection deprecation realmTextView.setTextColor(getResources().getColor(R.color.black)); } @@ -181,12 +201,10 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ // Create a blue `ForegroundColorSpan`. ForegroundColorSpan blueColorSpan; - // Set `blueColorSpan` according to the theme. The deprecated `getColor()` must be used until API >= 23. - if (MainWebViewActivity.darkTheme) { - //noinspection deprecation - blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400)); + // Set the blue color span according to the theme. The deprecated `getResources()` must be used until API >= 23. + if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { + blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.violet_500)); } else { - //noinspection deprecation blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700)); } @@ -200,10 +218,10 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ 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); + // Send the login information. + login(httpAuthHandler); - // Manually dismiss the `AlertDialog`. + // Manually dismiss the alert dialog. alertDialog.dismiss(); // Consume the event. @@ -217,10 +235,10 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ 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); + // Send the login information. + login(httpAuthHandler); - // Manually dismiss the `AlertDialog`. + // Manually dismiss the alert dialog. alertDialog.dismiss(); // Consume the event. @@ -230,7 +248,12 @@ public class HttpAuthenticationDialog extends AppCompatDialogFragment{ } }); - // `onCreateDialog()` requires the return of an `AlertDialog`. + // Return the alert dialog. return alertDialog; } -} + + private void login(HttpAuthHandler httpAuthHandler) { + // Send the login information. + httpAuthHandler.proceed(usernameEditText.getText().toString(), passwordEditText.getText().toString()); + } +} \ No newline at end of file