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=4811ea2dae1853bee544ae24b55b56dc8b30b896;hb=1a9be53186a8a4f16017c6dcc1f2f1e85289358e;hpb=61a76e491469916f2f30aebb47b98cda7cceb557 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 4811ea2d..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-2017 Soren Stoutner . + * Copyright © 2016-2019 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -22,168 +22,149 @@ 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; -// We have to use `AppCompatDialogFragment` 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; 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{ +public class UrlHistoryDialog extends DialogFragment{ + public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) { + // Create an arguments bundle. + Bundle argumentsBundle = new Bundle(); - // `historyArrayList` and `currentPageId` pass information from `onCreate()` to `onCreateDialog()`. - private ArrayList historyArrayList = new ArrayList<>(); - private int currentPageId; + // Store the WebView fragment ID in the bundle. + argumentsBundle.putLong("webview_fragment_id", webViewFragmentId); - public static UrlHistoryDialog loadBackForwardList(Context context, WebBackForwardList webBackForwardList) { - // Create `argumentsBundle`. - Bundle argumentsBundle = new Bundle(); + // Create a new instance of the URL history dialog. + UrlHistoryDialog urlHistoryDialog = new UrlHistoryDialog(); - // Store `currentPageIndex`. - int currentPageIndex = webBackForwardList.getCurrentIndex(); + // Add the arguments bundle to this instance. + urlHistoryDialog.setArguments(argumentsBundle); - // Setup `urlArrayList` and `iconArrayList`. - ArrayList urlArrayList = new ArrayList<>(); - ArrayList iconBase64StringArrayList = new ArrayList<>(); + // Return the new URL history dialog. + return urlHistoryDialog; + } - // Get the default favorite icon `Drawable`. - Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(context, R.drawable.world); + @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; - // Convert `defaultFavoriteIconDrawable` to a `BitmapDrawable`. - BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable; + // Get the activity's layout inflater. + LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - // Extract a `Bitmap` from `defaultFavoriteIconBitmapDrawable`. - Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap(); + // Get the arguments. + Bundle arguments = getArguments(); - // Populate `urlArrayList` and `iconArrayList` from `webBackForwardList`. - for (int i=0; i < webBackForwardList.getSize(); i++) { - // Store the URL. - urlArrayList.add(webBackForwardList.getItemAtIndex(i).getUrl()); + // Remove the incorrect lint error that arguments might be null. + assert arguments != null; - // Create a variable to store the icon `Bitmap`. - Bitmap iconBitmap; + // Get the WebView fragment ID from the arguments. + long webViewFragmentId = arguments.getLong("webview_fragment_id"); - // 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(); - } + // Get the current position of this WebView fragment. + int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId); - // Create a `ByteArrayOutputStream`. - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + // Get the WebView tab fragment. + WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition); - // 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 fragment view. + View fragmentView = webViewTabFragment.getView(); - // Convert the favorite icon `ByteArrayOutputStream` to a `byte[]`. - byte[] byteArray = byteArrayOutputStream.toByteArray(); + // Remove the incorrect lint warning below that the fragment view might be null. + assert fragmentView != null; - // Encode the favorite icon `byte[]` as a Base64 `String`. - String iconBase64String = Base64.encodeToString(byteArray, Base64.DEFAULT); + // Get a handle for the current WebView. + NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview); - // Store the favorite icon Base64 `String` in `iconBase64StringArrayList`. - iconBase64StringArrayList.add(iconBase64String); - } + // Get the web back forward list from the WebView. + WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList(); - // Store the variables in the `Bundle`. - argumentsBundle.putInt("Current_Page", currentPageIndex); - argumentsBundle.putStringArrayList("URL_History", urlArrayList); - argumentsBundle.putStringArrayList("Favorite_Icons", iconBase64StringArrayList); + // Store the current page index. + int currentPageIndex = webBackForwardList.getCurrentIndex(); - // Add `argumentsBundle` to this instance of `UrlHistoryDialog`. - UrlHistoryDialog thisUrlHistoryDialog = new UrlHistoryDialog(); - thisUrlHistoryDialog.setArguments(argumentsBundle); - return thisUrlHistoryDialog; - } + // Remove the lint warning below that `getContext()` might be null. + assert getContext() != null; - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21. + Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world); + + // Convert the default favorite icon drawable to a `BitmapDrawable`. + BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable; + + // Remove the incorrect lint error that `getBitmap()` might be null. + assert defaultFavoriteIconBitmapDrawable != null; - // Get the `ArrayLists` from the `Arguments`. - ArrayList urlStringArrayList = getArguments().getStringArrayList("URL_History"); - ArrayList favoriteIconBase64StringArrayList = getArguments().getStringArrayList("Favorite_Icons"); + // Extract a bitmap from the default favorite icon bitmap drawable. + Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap(); - // Remove the lint warning below that the `ArrayLists` might be `null`. - assert urlStringArrayList != null; - assert favoriteIconBase64StringArrayList != null; + // Create a history array list. + ArrayList historyArrayList = new ArrayList<>(); - // 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); + // 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; - // Convert the favorite icon `byte[]` to a `Bitmap`. `0` is the starting offset. - Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + // 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(); + } - // Store the favorite icon and the URL in `historyEntry`. - History historyEntry = new History(favoriteIconBitmap, urlStringArrayList.get(i)); + // Store the favorite icon and the URL in history entry. + History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl()); - // Add this history entry to `historyArrayList`. + // Add this history entry to the history array list. historyArrayList.add(historyEntry); } - // Get the original current page ID. - int originalCurrentPageId = getArguments().getInt("Current_Page"); + // 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; - // 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; - } - - // 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); + // Use an alert dialog builder to create the alert dialog. + AlertDialog.Builder dialogBuilder; - // Clear the history. - void onClearHistory(); - } + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext()); - // `urlHistoryListener` is used in `onAttach()` and `onCreateDialog()`. - private UrlHistoryListener urlHistoryListener; + // Get the screenshot and theme preferences. + boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); - @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."); + // 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); } - } - - @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) { - // Get the activity's layout inflater. - LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - - // Use `AlertDialog.Builder` to create the `AlertDialog`. `R.style.lightAlertDialog` formats the color of the button text. - AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.LightAlertDialog); // Set the title. dialogBuilder.setTitle(R.string.history); @@ -191,66 +172,63 @@ 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. - dialogBuilder.setNegativeButton(R.string.clear_history, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Clear the history. - urlHistoryListener.onClearHistory(); - } + // Setup the clear history button. + dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> { + // Clear the history. + nestedScrollWebView.clearHistory(); }); // Set an `onClick()` listener on the positive button. - dialogBuilder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - // Do nothing if `Close` is clicked. The `Dialog` will automatically close. - } + dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> { + // Do nothing if `Close` is clicked. The `Dialog` will automatically close. }); - // Create an `AlertDialog` from the `AlertDialog.Builder`. + // Create an alert dialog from the alert dialog builder. final AlertDialog alertDialog = dialogBuilder.create(); - // We need to show `alertDialog` before we can modify the contents. - alertDialog.show(); + // Disable screenshots if not allowed. + if (!allowScreenshots) { + // Remove the warning below that `getWindow()` might be null. + assert alertDialog.getWindow() != null; - // Instantiate a `HistoryArrayAdapter`. - final HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId); + // Disable screenshots. + alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + + //The alert dialog must be shown before the contents can be modified. + alertDialog.show(); - // Get a handle for `listView`. - ListView listView = (ListView) alertDialog.findViewById(R.id.history_listview); + // Instantiate a history array adapter. + HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId); - // Remove the warning below that `listView` might be `null`. - assert listView != null; + // Get a handle for the list view. + ListView listView = alertDialog.findViewById(R.id.history_listview); - // Set the adapter on `listView`. + // Set the list view adapter. listView.setAdapter(historyArrayAdapter); - // Listen for clicks on entries in `listView`. - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - // Convert the `long` `id` to an `int`. - int itemId = (int) id; + // Listen for clicks on entries in the list view. + listView.setOnItemClickListener((AdapterView parent, View view, int position, long id) -> { + // Convert the long ID to an int. + int itemId = (int) id; - // Only enable the click if it is not on the `currentPageId`. - if (itemId != currentPageId) { - // Get the history entry for this `itemId`. - History historyEntry = historyArrayAdapter.getItem(itemId); + // 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(); - // Remove the lint warning below that `historyEntry` might be `null`. - assert historyEntry != null; + // Set navigating history so that the domain settings are applied when the new URL is loaded. + nestedScrollWebView.setNavigatingHistory(true); - // Send the history entry URL to be loaded in `mainWebView`. - urlHistoryListener.onUrlHistoryEntrySelected(currentPageId - itemId); + // Load the history entry. + nestedScrollWebView.goBackOrForward(currentPageId - itemId); - // Dismiss the `Dialog`. - alertDialog.dismiss(); - } + // 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