X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FHttpAuthenticationDialog.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FHttpAuthenticationDialog.java;h=0000000000000000000000000000000000000000;hb=bc2e180db377eedadbe1ea455d8fb311ead8f9d6;hp=9d7c0f3822d6b5ab199711bad7b3609318c69c1b;hpb=a9b4d8c78a305c2602ced2058702254ea4e3b79b;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 deleted file mode 100644 index 9d7c0f38..00000000 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/HttpAuthenticationDialog.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright © 2017-2020 Soren Stoutner . - * - * This file is part of Privacy Browser . - * - * Privacy Browser is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Privacy Browser is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Privacy Browser. If not, see . - */ - -package com.stoutner.privacybrowser.dialogs; - -import android.annotation.SuppressLint; -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.os.Bundle; -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 DialogFragment{ - // Define the class variables. - private EditText usernameEditText; - private EditText passwordEditText; - - public static HttpAuthenticationDialog displayDialog(String host, String realm, long webViewFragmentId) { - // Create an arguments bundle. - Bundle argumentsBundle = new Bundle(); - - // 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 - @NonNull - public Dialog onCreateDialog(Bundle savedInstanceState) { - // Get a handle for the arguments. - Bundle arguments = getArguments(); - - // Remove the incorrect lint warning below that arguments might be null. - assert arguments != null; - - // Get the variables from the bundle. - String httpAuthHost = arguments.getString("host"); - String httpAuthRealm = arguments.getString("realm"); - long webViewFragmentId = arguments.getLong("webview_fragment_id"); - - // Get the current position of this WebView fragment. - int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId); - - // Get the WebView tab fragment. - WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition); - - // Get the fragment view. - View fragmentView = webViewTabFragment.getView(); - - // Remove the incorrect lint warning below that the fragment view might be null. - assert fragmentView != null; - - // 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(); - - // Remove the incorrect lint warning that `getActivity()` might be null. - assert getActivity() != null; - - // Get the activity's layout inflater. - LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - - // Use an alert dialog builder to create the alert dialog. - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialog); - - // Get the current theme status. - int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - - // Set the icon according to the theme. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { - dialogBuilder.setIcon(R.drawable.lock_night); - } else { - dialogBuilder.setIcon(R.drawable.lock_day); - } - - // Set the title. - dialogBuilder.setTitle(R.string.http_authentication); - - // 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 close button. - dialogBuilder.setNegativeButton(R.string.close, (DialogInterface dialog, int which) -> { - // Cancel the HTTP authentication request. - httpAuthHandler.cancel(); - - // Reset the HTTP authentication handler. - nestedScrollWebView.resetHttpAuthHandler(); - }); - - // Setup the proceed button. - dialogBuilder.setPositiveButton(R.string.proceed, (DialogInterface dialog, int which) -> { - // 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(); - - // 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 (!allowScreenshots) { - alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); - } - - // 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. - TextView realmTextView = alertDialog.findViewById(R.id.http_authentication_realm); - TextView hostTextView = alertDialog.findViewById(R.id.http_authentication_host); - 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 `getResources()` must be used until API >= 23. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) { - realmTextView.setTextColor(getResources().getColor(R.color.gray_300)); - } else { - realmTextView.setTextColor(getResources().getColor(R.color.black)); - } - - // Initialize the host label and the `SpannableStringBuilder`. - String hostLabel = getString(R.string.host) + " "; - SpannableStringBuilder hostStringBuilder = new SpannableStringBuilder(hostLabel + httpAuthHost); - - // Create a blue `ForegroundColorSpan`. - ForegroundColorSpan blueColorSpan; - - // 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 { - blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700)); - } - - // Setup the span to display the host name in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction. - hostStringBuilder.setSpan(blueColorSpan, hostLabel.length(), hostStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); - - // Set the host text. - hostTextView.setText(hostStringBuilder); - - // Allow the `enter` key on the keyboard to trigger `onHttpAuthenticationProceed` from `usernameEditText`. - 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)) { - // Send the login information. - login(httpAuthHandler); - - // Manually dismiss the alert dialog. - 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((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)) { - // Send the login information. - login(httpAuthHandler); - - // Manually dismiss the alert dialog. - alertDialog.dismiss(); - - // Consume the event. - return true; - } else { // If any other key was pressed, do not consume the event. - return false; - } - }); - - // 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