/*
- * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
*
import android.annotation.SuppressLint;
import android.app.Activity;
-import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
+import android.content.res.Configuration;
import android.net.Uri;
import android.net.http.SslCertificate;
import android.net.http.SslError;
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 androidx.appcompat.app.AlertDialog;
+import androidx.fragment.app.DialogFragment;
import com.stoutner.privacybrowser.R;
import com.stoutner.privacybrowser.activities.MainWebViewActivity;
return thisSslCertificateErrorDialog;
}
- // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
+ // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
@SuppressLint("InflateParams")
@Override
@NonNull
// Get a handle for the SSL error handler.
SslErrorHandler sslErrorHandler = nestedScrollWebView.getSslErrorHandler();
- // Remove the incorrect lint warning that `getActivity()` might be null.
- assert getActivity() != null;
-
// Get the activity's layout inflater.
- LayoutInflater layoutInflater = getActivity().getLayoutInflater();
+ LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
// 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);
+ AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
- // Set the style and icon according to the theme.
- if (darkTheme) {
- // Set the style.
- 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.ssl_certificate_enabled_dark);
+ // Set the icon according to the theme.
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_night);
} else {
- // Set the style.
- dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
-
- // Set the icon.
- dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_light);
+ dialogBuilder.setIcon(R.drawable.ssl_certificate_enabled_day);
}
// Set the title.
// Create an alert dialog from the alert dialog builder.
AlertDialog alertDialog = dialogBuilder.create();
+ // 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) {
// Remove the warning below that `getWindow()` might be null.
TextView startDateTextView = alertDialog.findViewById(R.id.start_date);
TextView endDateTextView = alertDialog.findViewById(R.id.end_date);
+ // Remove the incorrect lint warnings below that the views might be null.
+ assert primaryErrorTextView != null;
+ assert urlTextView != null;
+ assert issuedToCNameTextView != null;
+ assert issuedToONameTextView != null;
+ assert issuedToUNameTextView != null;
+ assert issuedByTextView != null;
+ assert issuedByCNameTextView != null;
+ assert issuedByONameTextView != null;
+ assert issuedByUNameTextView != null;
+ assert validDatesTextView != null;
+ assert startDateTextView != null;
+ assert endDateTextView != null;
+
// Setup the common strings.
String urlLabel = getString(R.string.url_label) + " ";
String cNameLabel = getString(R.string.common_name) + " ";
SpannableStringBuilder startDateStringBuilder = new SpannableStringBuilder(startDateLabel + startDate);
SpannableStringBuilder endDateStringBuilder = new SpannableStringBuilder((endDateLabel + endDate));
- // Create a red foreground color span. The deprecated `getResources().getColor` must be used until the minimum API >= 23.
- ForegroundColorSpan redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
-
- // Create a blue `ForegroundColorSpan`.
+ // Define the color spans.
ForegroundColorSpan blueColorSpan;
+ ForegroundColorSpan redColorSpan;
- // Set a blue color span according to the theme. The deprecated `getResources().getColor` must be used until the minimum API >= 23.
- if (darkTheme) {
- blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_400));
- } else {
+ // Set the color spans according to the theme. The deprecated `getResources()` must be used until the minimum API >= 23.
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) {
blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.blue_700));
+ redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_a700));
+ } else {
+ blueColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.violet_500));
+ redColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.red_900));
}
// Setup the spans to display the certificate information in blue. `SPAN_INCLUSIVE_INCLUSIVE` allows the span to grow in either direction.
case SslError.SSL_UNTRUSTED:
// Change the issued by text view text to red. The deprecated `getResources().getColor` must be used until the minimum API >= 23.
- issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ issuedByTextView.setTextColor(getResources().getColor(R.color.red_900));
+ } else {
+ issuedByTextView.setTextColor(getResources().getColor(R.color.red_a700));
+ }
// Change the issued by span color to red.
issuedByCNameStringBuilder.setSpan(redColorSpan, cNameLabel.length(), issuedByCNameStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
case SslError.SSL_DATE_INVALID:
// Change the valid dates text view text to red. The deprecated `getResources().getColor` must be used until the minimum API >= 23.
- validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ validDatesTextView.setTextColor(getResources().getColor(R.color.red_900));
+ } else {
+ validDatesTextView.setTextColor(getResources().getColor(R.color.red_a700));
+ }
// Change the date span colors to red.
startDateStringBuilder.setSpan(redColorSpan, startDateLabel.length(), startDateStringBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
// Add each IP address to the string builder.
for (InetAddress inetAddress : inetAddressesArray) {
- if (ipAddresses.length() == 0) { // This is the first IP address.
- // Add the IP Address to the string builder.
- ipAddresses.append(inetAddress.getHostAddress());
- } else { // This is not the first IP address.
+ // Check to see if this is not the first IP address.
+ if (ipAddresses.length() > 0) {
// Add a line break to the string builder first.
ipAddresses.append("\n");
-
- // Add the IP address to the string builder.
- ipAddresses.append(inetAddress.getHostAddress());
}
+
+ // Add the IP Address to the string builder.
+ ipAddresses.append(inetAddress.getHostAddress());
}
} catch (UnknownHostException exception) {
// Do nothing.
// Create a spannable string builder.
SpannableStringBuilder ipAddressesStringBuilder = new SpannableStringBuilder(ipAddressesLabel + ipAddresses);
- // Get a handle for the shared preferences.
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
-
- // Get the screenshot and theme preferences.
- boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
-
// Create a blue foreground color span.
ForegroundColorSpan blueColorSpan;
+ // Get the current theme status.
+ int currentThemeStatus = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
+
// Set the blue color span according to the theme. The deprecated `getColor()` must be used until the minimum API >= 23.
- if (darkTheme) {
- blueColorSpan = new ForegroundColorSpan(activity.getResources().getColor(R.color.blue_400));
+ if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
+ blueColorSpan = new ForegroundColorSpan(activity.getResources().getColor(R.color.violet_500));
} else {
blueColorSpan = new ForegroundColorSpan(activity.getResources().getColor(R.color.blue_700));
}
// Get a handle for the IP addresses text view.
TextView ipAddressesTextView = alertDialog.findViewById(R.id.ip_addresses);
+ // Remove the incorrect lint warning below that the view might be null.
+ assert ipAddressesTextView != null;
+
// Populate the IP addresses text view.
ipAddressesTextView.setText(ipAddresses);
}