]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java
Migrate five dialogs to Kotlin. https://redmine.stoutner.com/issues/604
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / UrlHistoryDialog.java
index 366566f76b9bd3d5392478735f4d3c424caf814f..3f8035e0b51e5aae9a19268ba36ded4b388536b0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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>.
  *
@@ -20,8 +20,8 @@
 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.graphics.Bitmap;
@@ -35,8 +35,10 @@ import android.view.WindowManager;
 import android.webkit.WebBackForwardList;
 import android.widget.AdapterView;
 import android.widget.ListView;
+import android.widget.TextView;
 
 import androidx.annotation.NonNull;
+import androidx.appcompat.app.AlertDialog;
 import androidx.core.content.ContextCompat;
 import androidx.fragment.app.DialogFragment;  // The AndroidX dialog fragment must be used or an error is produced on API <=22.
 
@@ -50,6 +52,23 @@ import com.stoutner.privacybrowser.views.NestedScrollWebView;
 import java.util.ArrayList;
 
 public class UrlHistoryDialog extends DialogFragment{
+    // The public interface is used to send information back to the parent activity.
+    public interface NavigateHistoryListener {
+        void navigateHistory(String url, int steps);
+    }
+
+    // The navigate history listener is used in `onAttach()` and `onCreateDialog()`.
+    private NavigateHistoryListener navigateHistoryListener;
+
+    @Override
+    public void onAttach(@NonNull Context context) {
+        // Run the default commands.
+        super.onAttach(context);
+
+        // Get a handle for the listener from the launching context.
+        navigateHistoryListener = (NavigateHistoryListener) context;
+    }
+
     public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) {
         // Create an arguments bundle.
         Bundle argumentsBundle = new Bundle();
@@ -69,14 +88,11 @@ public class UrlHistoryDialog extends DialogFragment{
 
     @Override
     @NonNull
-    // `@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")
     public Dialog onCreateDialog(Bundle savedInstanceState) {
-        // 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();
 
         // Get the arguments.
         Bundle arguments = getArguments();
@@ -126,7 +142,7 @@ public class UrlHistoryDialog extends DialogFragment{
         // Create a history array list.
         ArrayList<History> historyArrayList = new ArrayList<>();
 
-        // Populate the history array list, descending from `urlStringArrayList.size()` so that the newest entries are at the top.  `-1` is needed because the history array list is zero-based.
+        // Populate the history array list, descending from the end of the list so that the newest entries are at the top.  `-1` is needed because the history array list is zero-based.
         for (int i=webBackForwardList.getSize() -1; i >= 0; i--) {
             // Create a variable to store the favorite icon bitmap.
             Bitmap favoriteIconBitmap;
@@ -150,21 +166,7 @@ public class UrlHistoryDialog extends DialogFragment{
         int currentPageId = webBackForwardList.getSize() - 1 - currentPageIndex;
 
         // 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 (darkTheme) {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
-        } else {
-            dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
-        }
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
 
         // Set the title.
         dialogBuilder.setTitle(R.string.history);
@@ -186,6 +188,12 @@ public class UrlHistoryDialog 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 (!allowScreenshots) {
             // Remove the warning below that `getWindow()` might be null.
@@ -204,6 +212,9 @@ public class UrlHistoryDialog extends DialogFragment{
         // Get a handle for the list view.
         ListView listView = alertDialog.findViewById(R.id.history_listview);
 
+        // Remove the incorrect lint warning below that the view might be null.
+        assert listView != null;
+
         // Set the list view adapter.
         listView.setAdapter(historyArrayAdapter);
 
@@ -214,14 +225,14 @@ public class UrlHistoryDialog extends DialogFragment{
 
             // Only consume the click if it is not on the `currentPageId`.
             if (itemId != currentPageId) {
-                // Reset the current domain name so that navigation works if third-party requests are blocked.
-                nestedScrollWebView.resetCurrentDomainName();
+                // Get a handle for the URL text view.
+                TextView urlTextView = view.findViewById(R.id.history_url_textview);
 
-                // Set navigating history so that the domain settings are applied when the new URL is loaded.
-                nestedScrollWebView.setNavigatingHistory(true);
+                // Get the URL.
+                String url = urlTextView.getText().toString();
 
-                // Load the history entry.
-                nestedScrollWebView.goBackOrForward(currentPageId - itemId);
+                // Invoke the navigate history listener in the calling activity.  These commands cannot be run here because they need access to `applyDomainSettings()`.
+                navigateHistoryListener.navigateHistory(url, currentPageId - itemId);
 
                 // Dismiss the alert dialog.
                 alertDialog.dismiss();