X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fdialogs%2FUrlHistoryDialog.java;h=366566f76b9bd3d5392478735f4d3c424caf814f;hp=9b9d9f610ca82346a56e05b25b86c06369e6283a;hb=1a9be53186a8a4f16017c6dcc1f2f1e85289358e;hpb=012e5595c82d6e8d0b8a46f1ef18a02a56341182 diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java b/app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java index 9b9d9f61..366566f7 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2018 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -22,19 +22,13 @@ 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; -import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.content.ContextCompat; -// `AppCompatDialogFragment` must be used instead of `DialogFragment` or an error is produced on API <= 22. -// `android.support.v7.app.AlertDialog` also uses more of the horizontal screen real estate versus `android.app.AlertDialog's` smaller width. -import android.support.v7.app.AppCompatDialogFragment; -import android.util.Base64; +import android.preference.PreferenceManager; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; @@ -42,166 +36,131 @@ import android.webkit.WebBackForwardList; import android.widget.AdapterView; import android.widget.ListView; +import androidx.annotation.NonNull; +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. + import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.activities.MainWebViewActivity; import com.stoutner.privacybrowser.adapters.HistoryArrayAdapter; import com.stoutner.privacybrowser.definitions.History; +import com.stoutner.privacybrowser.fragments.WebViewTabFragment; +import com.stoutner.privacybrowser.views.NestedScrollWebView; -import java.io.ByteArrayOutputStream; import java.util.ArrayList; -public class UrlHistoryDialog extends AppCompatDialogFragment{ - - // `historyArrayList` and `currentPageId` pass information from `onCreate()` to `onCreateDialog()`. - private final ArrayList historyArrayList = new ArrayList<>(); - private int currentPageId; - - public static UrlHistoryDialog loadBackForwardList(Context context, WebBackForwardList webBackForwardList) { - // Create `argumentsBundle`. +public class UrlHistoryDialog extends DialogFragment{ + public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) { + // Create an arguments bundle. Bundle argumentsBundle = new Bundle(); - // Store `currentPageIndex`. - int currentPageIndex = webBackForwardList.getCurrentIndex(); - - // Setup `urlArrayList` and `iconArrayList`. - ArrayList urlArrayList = new ArrayList<>(); - ArrayList iconBase64StringArrayList = new ArrayList<>(); - - // Get the default favorite icon `Drawable`. - Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(context, R.drawable.world); - - // Convert `defaultFavoriteIconDrawable` to a `BitmapDrawable`. - BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable; + // Store the WebView fragment ID in the bundle. + argumentsBundle.putLong("webview_fragment_id", webViewFragmentId); - // Remove the incorrect lint error that `getBitmap()` might be null. - assert defaultFavoriteIconBitmapDrawable != null; + // Create a new instance of the URL history dialog. + UrlHistoryDialog urlHistoryDialog = new UrlHistoryDialog(); - // Extract a `Bitmap` from `defaultFavoriteIconBitmapDrawable`. - Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap(); + // Add the arguments bundle to this instance. + urlHistoryDialog.setArguments(argumentsBundle); - // Populate `urlArrayList` and `iconArrayList` from `webBackForwardList`. - for (int i=0; i < webBackForwardList.getSize(); i++) { - // Store the URL. - urlArrayList.add(webBackForwardList.getItemAtIndex(i).getUrl()); - - // Create a variable to store the icon `Bitmap`. - Bitmap iconBitmap; - - // Store the icon `Bitmap`. - if (webBackForwardList.getItemAtIndex(i).getFavicon() == null) { - // If `webBackForwardList` does not have a favorite icon, use Privacy Browser's default world icon. - iconBitmap = defaultFavoriteIcon; - } else { // Get the icon from `webBackForwardList`. - iconBitmap = webBackForwardList.getItemAtIndex(i).getFavicon(); - } + // Return the new URL history dialog. + return urlHistoryDialog; + } - // Create a `ByteArrayOutputStream`. - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + @Override + @NonNull + // `@SuppressLing("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; - // Remove the incorrect lint error that `compress()` might be null; - assert iconBitmap != null; + // Get the activity's layout inflater. + LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - // Convert the favorite icon `Bitmap` to a `ByteArrayOutputStream`. `100` is the compression quality, which is ignored by `PNG`. - iconBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); + // Get the arguments. + Bundle arguments = getArguments(); - // Convert the favorite icon `ByteArrayOutputStream` to a `byte[]`. - byte[] byteArray = byteArrayOutputStream.toByteArray(); + // Remove the incorrect lint error that arguments might be null. + assert arguments != null; - // Encode the favorite icon `byte[]` as a Base64 `String`. - String iconBase64String = Base64.encodeToString(byteArray, Base64.DEFAULT); + // Get the WebView fragment ID from the arguments. + long webViewFragmentId = arguments.getLong("webview_fragment_id"); - // Store the favorite icon Base64 `String` in `iconBase64StringArrayList`. - iconBase64StringArrayList.add(iconBase64String); - } + // Get the current position of this WebView fragment. + int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId); - // Store the variables in the `Bundle`. - argumentsBundle.putInt("Current_Page", currentPageIndex); - argumentsBundle.putStringArrayList("URL_History", urlArrayList); - argumentsBundle.putStringArrayList("Favorite_Icons", iconBase64StringArrayList); + // Get the WebView tab fragment. + WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition); - // Add `argumentsBundle` to this instance of `UrlHistoryDialog`. - UrlHistoryDialog thisUrlHistoryDialog = new UrlHistoryDialog(); - thisUrlHistoryDialog.setArguments(argumentsBundle); - return thisUrlHistoryDialog; - } + // Get the fragment view. + View fragmentView = webViewTabFragment.getView(); - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + // Remove the incorrect lint warning below that the fragment view might be null. + assert fragmentView != null; - // Remove the incorrect lint error that `getArguments()` might be null. - assert getArguments() != null; + // Get a handle for the current WebView. + NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview); - // Get the `ArrayLists` from the `Arguments`. - ArrayList urlStringArrayList = getArguments().getStringArrayList("URL_History"); - ArrayList favoriteIconBase64StringArrayList = getArguments().getStringArrayList("Favorite_Icons"); + // Get the web back forward list from the WebView. + WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList(); - // Remove the lint warning below that the `ArrayLists` might be `null`. - assert urlStringArrayList != null; - assert favoriteIconBase64StringArrayList != null; + // Store the current page index. + int currentPageIndex = webBackForwardList.getCurrentIndex(); - // Populate `historyArrayList`. We go down from `urlStringArrayList.size()` so that the newest entries are at the top. `-1` is needed because `historyArrayList` is zero-based. - for (int i=urlStringArrayList.size() -1; i >= 0; i--) { - // Decode the favorite icon Base64 `String` to a `byte[]`. - byte[] favoriteIconByteArray = Base64.decode(favoriteIconBase64StringArrayList.get(i), Base64.DEFAULT); + // Remove the lint warning below that `getContext()` might be null. + assert getContext() != null; - // Convert the favorite icon `byte[]` to a `Bitmap`. `0` is the starting offset. - Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21. + Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world); - // Store the favorite icon and the URL in `historyEntry`. - History historyEntry = new History(favoriteIconBitmap, urlStringArrayList.get(i)); + // Convert the default favorite icon drawable to a `BitmapDrawable`. + BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable; - // Add this history entry to `historyArrayList`. - historyArrayList.add(historyEntry); - } + // Remove the incorrect lint error that `getBitmap()` might be null. + assert defaultFavoriteIconBitmapDrawable != null; - // Get the original current page ID. - int originalCurrentPageId = getArguments().getInt("Current_Page"); + // Extract a bitmap from the default favorite icon bitmap drawable. + Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap(); - // Subtract `originalCurrentPageId` from the array size because we reversed the order of the array so that the newest entries are at the top. `-1` is needed because the array is zero-based. - currentPageId = urlStringArrayList.size() - 1 - originalCurrentPageId; - } + // Create a history array list. + ArrayList historyArrayList = new ArrayList<>(); - // The public interface is used to send information back to the parent activity. - public interface UrlHistoryListener { - // Send back the number of steps to move forward or back. - void onUrlHistoryEntrySelected(int moveBackOrForwardSteps); + // 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. + for (int i=webBackForwardList.getSize() -1; i >= 0; i--) { + // Create a variable to store the favorite icon bitmap. + Bitmap favoriteIconBitmap; - // Clear the history. - void onClearHistory(); - } + // Determine the favorite icon bitmap + if (webBackForwardList.getItemAtIndex(i).getFavicon() == null) { + // If the web back forward list does not have a favorite icon, use Privacy Browser's default world icon. + favoriteIconBitmap = defaultFavoriteIcon; + } else { // Use the icon from the web back forward list. + favoriteIconBitmap = webBackForwardList.getItemAtIndex(i).getFavicon(); + } - // `urlHistoryListener` is used in `onAttach()` and `onCreateDialog()`. - private UrlHistoryListener urlHistoryListener; + // Store the favorite icon and the URL in history entry. + History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl()); - @Override - public void onAttach(Context context) { - super.onAttach(context); - - // Check to make sure tha the parent activity implements the listener. - try { - urlHistoryListener = (UrlHistoryListener) context; - } catch (ClassCastException exception) { - throw new ClassCastException(context.toString() + " must implement UrlHistoryListener."); + // Add this history entry to the history array list. + historyArrayList.add(historyEntry); } - } - @Override - @NonNull - // `@SuppressLing("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(); + // Subtract the original current page ID from the array size because the order of the array is reversed so that the newest entries are at the top. `-1` is needed because the array is zero-based. + 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 (MainWebViewActivity.darkTheme) { + if (darkTheme) { dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark); } else { dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight); @@ -213,10 +172,10 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`. dialogBuilder.setView(layoutInflater.inflate(R.layout.url_history_dialog, null)); - // Set an `onClick()` listener on the negative button. + // Setup the clear history button. dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> { // Clear the history. - urlHistoryListener.onClearHistory(); + nestedScrollWebView.clearHistory(); }); // Set an `onClick()` listener on the positive button. @@ -228,7 +187,7 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ final AlertDialog alertDialog = dialogBuilder.create(); // Disable screenshots if not allowed. - if (!MainWebViewActivity.allowScreenshots) { + if (!allowScreenshots) { // Remove the warning below that `getWindow()` might be null. assert alertDialog.getWindow() != null; @@ -239,7 +198,7 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ //The alert dialog must be shown before the contents can be modified. alertDialog.show(); - // Instantiate a `HistoryArrayAdapter`. + // Instantiate a history array adapter. HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId); // Get a handle for the list view. @@ -255,15 +214,21 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ // Only consume the click if it is not on the `currentPageId`. if (itemId != currentPageId) { - // Go forward or back to `itemId`. - urlHistoryListener.onUrlHistoryEntrySelected(currentPageId - itemId); + // Reset the current domain name so that navigation works if third-party requests are blocked. + nestedScrollWebView.resetCurrentDomainName(); + + // Set navigating history so that the domain settings are applied when the new URL is loaded. + nestedScrollWebView.setNavigatingHistory(true); + + // Load the history entry. + nestedScrollWebView.goBackOrForward(currentPageId - itemId); - // Dismiss the `Dialog`. + // Dismiss the alert dialog. alertDialog.dismiss(); } }); - // `onCreateDialog` requires the return of an `AlertDialog`. + // Return the alert dialog. return alertDialog; } } \ No newline at end of file