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=13b8c3311e49b4233b540b1a379682b06e0fa3e4;hp=8a703929b6d12226b817dbe5319b7edebb6aca31;hb=f0393ca22075be3e5fe9199c7db87381256236fa;hpb=49655ec36b1119810105b04a81e7ef38933213f0 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 8a703929..13b8c331 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 . * @@ -24,65 +24,95 @@ 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.preference.PreferenceManager; import android.util.Base64; 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 java.io.ByteArrayOutputStream; import java.util.ArrayList; -public class UrlHistoryDialog extends AppCompatDialogFragment{ - - // `historyArrayList` and `currentPageId` pass information from `onCreate()` to `onCreateDialog()`. +public class UrlHistoryDialog extends DialogFragment{ + // Declare the class variables. private final ArrayList historyArrayList = new ArrayList<>(); private int currentPageId; + // Create a URL history listener. + private UrlHistoryListener urlHistoryListener; + + + // 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); + + // Clear the history. + void onClearHistory(); + } + + @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."); + } + } + + public static UrlHistoryDialog loadBackForwardList(Context context, WebBackForwardList webBackForwardList) { - // Create `argumentsBundle`. + // Create an arguments bundle. Bundle argumentsBundle = new Bundle(); - // Store `currentPageIndex`. + // Store the current page index. int currentPageIndex = webBackForwardList.getCurrentIndex(); - // Setup `urlArrayList` and `iconArrayList`. + // Setup the URL array list and the icon array list. ArrayList urlArrayList = new ArrayList<>(); ArrayList iconBase64StringArrayList = new ArrayList<>(); - // Get the default favorite icon `Drawable`. + // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21. Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(context, R.drawable.world); - // Convert `defaultFavoriteIconDrawable` to a `BitmapDrawable`. + // Convert the default favorite icon drawable to a `BitmapDrawable`. BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable; - // Extract a `Bitmap` from `defaultFavoriteIconBitmapDrawable`. + // Remove the incorrect lint error that `getBitmap()` might be null. + assert defaultFavoriteIconBitmapDrawable != null; + + // Extract a `Bitmap` from the default favorite icon `BitmapDrawable`. Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap(); - // Populate `urlArrayList` and `iconArrayList` from `webBackForwardList`. + // Populate the URL array list and the icon array list 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`. + // Create a variable to store the icon bitmap. Bitmap iconBitmap; - // Store the icon `Bitmap`. + // 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; @@ -93,6 +123,9 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ // Create a `ByteArrayOutputStream`. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + // Remove the incorrect lint error that `compress()` might be null; + assert iconBitmap != null; + // 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); @@ -111,7 +144,7 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ argumentsBundle.putStringArrayList("URL_History", urlArrayList); argumentsBundle.putStringArrayList("Favorite_Icons", iconBase64StringArrayList); - // Add `argumentsBundle` to this instance of `UrlHistoryDialog`. + // Add the arguments bundle to this instance of `UrlHistoryDialog`. UrlHistoryDialog thisUrlHistoryDialog = new UrlHistoryDialog(); thisUrlHistoryDialog.setArguments(argumentsBundle); return thisUrlHistoryDialog; @@ -121,6 +154,9 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // Remove the incorrect lint error that `getArguments()` might be null. + assert getArguments() != null; + // Get the `ArrayLists` from the `Arguments`. ArrayList urlStringArrayList = getArguments().getStringArrayList("URL_History"); ArrayList favoriteIconBase64StringArrayList = getArguments().getStringArrayList("Favorite_Icons"); @@ -151,43 +187,29 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ 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); - - // Clear the history. - void onClearHistory(); - } - - // `urlHistoryListener` is used in `onAttach()` and `onCreateDialog()`. - private UrlHistoryListener urlHistoryListener; - - @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."); - } - } - @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(); - // Use `AlertDialog.Builder` to create the `AlertDialog`. + // 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); @@ -200,52 +222,52 @@ public class UrlHistoryDialog extends AppCompatDialogFragment{ 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(); - } + dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> { + // Clear the history. + urlHistoryListener.onClearHistory(); }); // 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. + // Disable screenshots if not allowed. + if (!allowScreenshots) { + // Remove the warning below that `getWindow()` might be null. + assert alertDialog.getWindow() != null; + + // Disable screenshots. + alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + + //The alert dialog must be shown before the contents can be modified. alertDialog.show(); // Instantiate a `HistoryArrayAdapter`. - final HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId); + HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId); - // Get a handle for `listView`. - ListView listView = (ListView) alertDialog.findViewById(R.id.history_listview); + // 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; - - // Only consume the click if it is not on the `currentPageId`. - if (itemId != currentPageId) { - // Go forward or back to `itemId`. - urlHistoryListener.onUrlHistoryEntrySelected(currentPageId - itemId); - - // Dismiss the `Dialog`. - alertDialog.dismiss(); - } + // 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 consume the click if it is not on the `currentPageId`. + if (itemId != currentPageId) { + // Go forward or back to `itemId`. + urlHistoryListener.onUrlHistoryEntrySelected(currentPageId - itemId); + + // Dismiss the `Dialog`. + alertDialog.dismiss(); } });