]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/ViewRequestDialog.java
Switch to the new Day/Night theme. https://redmine.stoutner.com/issues/522
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / ViewRequestDialog.java
index 6d6401f3af24428ea0f6e457d33c39ac817ff3fb..0ff75520ce7f70c6962d1de9cb9b380e63de0f05 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018-2019 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2018-2020 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
 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.preference.PreferenceManager;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.Button;
 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.helpers.BlocklistHelper;
 
 public class ViewRequestDialog extends DialogFragment {
     // The public interface is used to send information back to the parent activity.
@@ -44,10 +47,10 @@ public class ViewRequestDialog extends DialogFragment {
         void onNext(int id);
     }
 
-    // `viewRequestListener` is used in `onAttach()` and `onCreateDialog()`.
+    // The view request listener is used in `onAttach()` and `onCreateDialog()`.
     private ViewRequestListener viewRequestListener;
 
-    public void onAttach(Context context) {
+    public void onAttach(@NonNull Context context) {
         // Run the default commands.
         super.onAttach(context);
 
@@ -60,9 +63,9 @@ public class ViewRequestDialog extends DialogFragment {
         Bundle bundle = new Bundle();
 
         // Store the request details.
-        bundle.putInt("ID", id);
-        bundle.putBoolean("Is Last Request", isLastRequest);
-        bundle.putStringArray("Request Details", requestDetails);
+        bundle.putInt("id", id);
+        bundle.putBoolean("is_last_request", isLastRequest);
+        bundle.putStringArray("request_details", requestDetails);
 
         // Add the bundle to the dialog.
         ViewRequestDialog viewRequestDialog = new ViewRequestDialog();
@@ -81,20 +84,21 @@ public class ViewRequestDialog extends DialogFragment {
         assert getArguments() != null;
 
         // Get the info from the bundle.
-        int id = getArguments().getInt("ID");
-        boolean isLastRequest = getArguments().getBoolean("Is Last Request");
-        String[] requestDetails = getArguments().getStringArray("Request Details");
+        int id = getArguments().getInt("id");
+        boolean isLastRequest = getArguments().getBoolean("is_last_request");
+        String[] requestDetails = getArguments().getStringArray("request_details");
 
         // Use an alert dialog builder to create the alert dialog.
-        AlertDialog.Builder dialogBuilder;
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
 
-        // Set the style and icon according to the theme.
-        if (MainWebViewActivity.darkTheme) {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
-            dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
+        // 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.block_ads_enabled_night);
         } else {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
-            dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
+            dialogBuilder.setIcon(R.drawable.block_ads_enabled_day);
         }
 
         // Create the dialog title.
@@ -130,8 +134,14 @@ public class ViewRequestDialog extends DialogFragment {
         // Create an alert dialog from the alert dialog builder.
         final 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 (!MainWebViewActivity.allowScreenshots) {
+        if (!allowScreenshots) {
             // Remove the warning below that `getWindow()` might be null.
             assert alertDialog.getWindow() != null;
 
@@ -156,6 +166,18 @@ public class ViewRequestDialog extends DialogFragment {
         Button previousButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
         Button nextButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
 
+        // Remove the incorrect lint warnings below that the views might be null.
+        assert requestDisposition != null;
+        assert requestUrl != null;
+        assert requestBlockListLabel != null;
+        assert requestBlockList != null;
+        assert requestSubListLabel != null;
+        assert requestSubList != null;
+        assert requestBlockListEntriesLabel != null;
+        assert requestBlockListEntries != null;
+        assert requestBlockListOriginalEntryLabel != null;
+        assert requestBlockListOriginalEntry != null;
+
         // Disable the previous button if the first resource request is displayed.
         previousButton.setEnabled(!(id == 1));
 
@@ -163,8 +185,8 @@ public class ViewRequestDialog extends DialogFragment {
         nextButton.setEnabled(!isLastRequest);
 
         // Set the request action text.
-        switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_DISPOSITION])) {
-            case MainWebViewActivity.REQUEST_DEFAULT:
+        switch (requestDetails[BlocklistHelper.REQUEST_DISPOSITION]) {
+            case BlocklistHelper.REQUEST_DEFAULT:
                 // Set the text.
                 requestDisposition.setText(R.string.default_allowed);
 
@@ -172,36 +194,36 @@ public class ViewRequestDialog extends DialogFragment {
                 requestDisposition.setBackgroundColor(getResources().getColor(R.color.transparent));
                 break;
 
-            case MainWebViewActivity.REQUEST_ALLOWED:
+            case BlocklistHelper.REQUEST_ALLOWED:
                 // Set the text.
                 requestDisposition.setText(R.string.allowed);
 
                 // Set the background color.
-                if (MainWebViewActivity.darkTheme) {
+                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_700_50));
                 } else {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.blue_100));
                 }
                 break;
 
-            case MainWebViewActivity.REQUEST_THIRD_PARTY:
+            case BlocklistHelper.REQUEST_THIRD_PARTY:
                 // Set the text.
                 requestDisposition.setText(R.string.third_party_blocked);
 
                 // Set the background color.
-                if (MainWebViewActivity.darkTheme) {
+                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_700_50));
                 } else {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.yellow_100));
                 }
                 break;
 
-            case MainWebViewActivity.REQUEST_BLOCKED:
+            case BlocklistHelper.REQUEST_BLOCKED:
                 // Set the text.
                 requestDisposition.setText(R.string.blocked);
 
                 // Set the background color.
-                if (MainWebViewActivity.darkTheme) {
+                if (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_700_40));
                 } else {
                     requestDisposition.setBackgroundColor(getResources().getColor(R.color.red_100));
@@ -210,7 +232,7 @@ public class ViewRequestDialog extends DialogFragment {
         }
 
         // Display the request URL.
-        requestUrl.setText(requestDetails[MainWebViewActivity.REQUEST_URL]);
+        requestUrl.setText(requestDetails[BlocklistHelper.REQUEST_URL]);
 
         // Modify the dialog based on the request action.
         if (requestDetails.length == 2) {  // A default request.
@@ -225,93 +247,97 @@ public class ViewRequestDialog extends DialogFragment {
             requestBlockListOriginalEntry.setVisibility(View.GONE);
         } else {  // A blocked or allowed request.
             // Set the text on the text views.
-            requestBlockList.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST]);
-            requestBlockListEntries.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ENTRIES]);
-            requestBlockListOriginalEntry.setText(requestDetails[MainWebViewActivity.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]);
+            requestBlockList.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST]);
+            requestBlockListEntries.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ENTRIES]);
+            requestBlockListOriginalEntry.setText(requestDetails[BlocklistHelper.REQUEST_BLOCKLIST_ORIGINAL_ENTRY]);
 
             // Set the sublist text.
-            switch (Integer.valueOf(requestDetails[MainWebViewActivity.REQUEST_SUBLIST])) {
-                case MainWebViewActivity.MAIN_WHITELIST:
+            switch (requestDetails[BlocklistHelper.REQUEST_SUBLIST]) {
+                case BlocklistHelper.MAIN_WHITELIST:
                     requestSubList.setText(R.string.main_whitelist);
                     break;
 
-                case MainWebViewActivity.FINAL_WHITELIST:
+                case BlocklistHelper.FINAL_WHITELIST:
                     requestSubList.setText(R.string.final_whitelist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_WHITELIST:
+                case BlocklistHelper.DOMAIN_WHITELIST:
                     requestSubList.setText(R.string.domain_whitelist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_INITIAL_WHITELIST:
+                case BlocklistHelper.DOMAIN_INITIAL_WHITELIST:
                     requestSubList.setText(R.string.domain_initial_whitelist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_FINAL_WHITELIST:
+                case BlocklistHelper.DOMAIN_FINAL_WHITELIST:
                     requestSubList.setText(R.string.domain_final_whitelist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_WHITELIST:
+                case BlocklistHelper.THIRD_PARTY_WHITELIST:
                     requestSubList.setText(R.string.third_party_whitelist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_DOMAIN_WHITELIST:
+                case BlocklistHelper.THIRD_PARTY_DOMAIN_WHITELIST:
                     requestSubList.setText(R.string.third_party_domain_whitelist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
+                case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_WHITELIST:
                     requestSubList.setText(R.string.third_party_domain_initial_whitelist);
                     break;
 
-                case MainWebViewActivity.MAIN_BLACKLIST:
+                case BlocklistHelper.MAIN_BLACKLIST:
                     requestSubList.setText(R.string.main_blacklist);
                     break;
 
-                case MainWebViewActivity.INITIAL_BLACKLIST:
+                case BlocklistHelper.INITIAL_BLACKLIST:
                     requestSubList.setText(R.string.initial_blacklist);
                     break;
 
-                case MainWebViewActivity.FINAL_BLACKLIST:
+                case BlocklistHelper.FINAL_BLACKLIST:
                     requestSubList.setText(R.string.final_blacklist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_BLACKLIST:
+                case BlocklistHelper.DOMAIN_BLACKLIST:
                     requestSubList.setText(R.string.domain_blacklist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_INITIAL_BLACKLIST:
+                case BlocklistHelper.DOMAIN_INITIAL_BLACKLIST:
                     requestSubList.setText(R.string.domain_initial_blacklist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_FINAL_BLACKLIST:
+                case BlocklistHelper.DOMAIN_FINAL_BLACKLIST:
                     requestSubList.setText(R.string.domain_final_blacklist);
                     break;
 
-                case MainWebViewActivity.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
+                case BlocklistHelper.DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
                     requestSubList.setText(R.string.domain_regular_expression_blacklist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_BLACKLIST:
+                case BlocklistHelper.THIRD_PARTY_BLACKLIST:
                     requestSubList.setText(R.string.third_party_blacklist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_DOMAIN_BLACKLIST:
+                case BlocklistHelper.THIRD_PARTY_INITIAL_BLACKLIST:
+                    requestSubList.setText(R.string.third_party_initial_blacklist);
+                    break;
+
+                case BlocklistHelper.THIRD_PARTY_DOMAIN_BLACKLIST:
                     requestSubList.setText(R.string.third_party_domain_blacklist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
+                case BlocklistHelper.THIRD_PARTY_DOMAIN_INITIAL_BLACKLIST:
                     requestSubList.setText(R.string.third_party_domain_initial_blacklist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
+                case BlocklistHelper.THIRD_PARTY_REGULAR_EXPRESSION_BLACKLIST:
                     requestSubList.setText(R.string.third_party_regular_expression_blacklist);
                     break;
 
-                case MainWebViewActivity.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
+                case BlocklistHelper.THIRD_PARTY_DOMAIN_REGULAR_EXPRESSION_BLACKLIST:
                     requestSubList.setText(R.string.third_party_domain_regular_expression_blacklist);
                     break;
 
-                case MainWebViewActivity.REGULAR_EXPRESSION_BLACKLIST:
+                case BlocklistHelper.REGULAR_EXPRESSION_BLACKLIST:
                     requestSubList.setText(R.string.regular_expression_blacklist);
                     break;
             }
@@ -320,4 +346,4 @@ public class ViewRequestDialog extends DialogFragment {
         // `onCreateDialog` requires the return of an alert dialog.
         return alertDialog;
     }
-}
+}
\ No newline at end of file