2 * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.dialogs;
22 import android.annotation.SuppressLint;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.DialogInterface;
26 import android.content.SharedPreferences;
27 import android.graphics.Bitmap;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.os.Bundle;
31 import android.preference.PreferenceManager;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.webkit.WebBackForwardList;
36 import android.widget.AdapterView;
37 import android.widget.ListView;
39 import androidx.annotation.NonNull;
40 import androidx.core.content.ContextCompat;
41 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
43 import com.stoutner.privacybrowser.R;
44 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
45 import com.stoutner.privacybrowser.adapters.HistoryArrayAdapter;
46 import com.stoutner.privacybrowser.definitions.History;
47 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
48 import com.stoutner.privacybrowser.views.NestedScrollWebView;
50 import java.util.ArrayList;
52 public class UrlHistoryDialog extends DialogFragment{
53 public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) {
54 // Create an arguments bundle.
55 Bundle argumentsBundle = new Bundle();
57 // Store the WebView fragment ID in the bundle.
58 argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
60 // Create a new instance of the URL history dialog.
61 UrlHistoryDialog urlHistoryDialog = new UrlHistoryDialog();
63 // Add the arguments bundle to this instance.
64 urlHistoryDialog.setArguments(argumentsBundle);
66 // Return the new URL history dialog.
67 return urlHistoryDialog;
72 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
73 @SuppressLint("InflateParams")
74 public Dialog onCreateDialog(Bundle savedInstanceState) {
75 // Remove the incorrect lint warning that `getActivity()` might be null.
76 assert getActivity() != null;
78 // Get the activity's layout inflater.
79 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
82 Bundle arguments = getArguments();
84 // Remove the incorrect lint error that arguments might be null.
85 assert arguments != null;
87 // Get the WebView fragment ID from the arguments.
88 long webViewFragmentId = arguments.getLong("webview_fragment_id");
90 // Get the current position of this WebView fragment.
91 int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId);
93 // Get the WebView tab fragment.
94 WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
96 // Get the fragment view.
97 View fragmentView = webViewTabFragment.getView();
99 // Remove the incorrect lint warning below that the fragment view might be null.
100 assert fragmentView != null;
102 // Get a handle for the current WebView.
103 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
105 // Get the web back forward list from the WebView.
106 WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList();
108 // Store the current page index.
109 int currentPageIndex = webBackForwardList.getCurrentIndex();
111 // Remove the lint warning below that `getContext()` might be null.
112 assert getContext() != null;
114 // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21.
115 Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world);
117 // Convert the default favorite icon drawable to a `BitmapDrawable`.
118 BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable;
120 // Remove the incorrect lint error that `getBitmap()` might be null.
121 assert defaultFavoriteIconBitmapDrawable != null;
123 // Extract a bitmap from the default favorite icon bitmap drawable.
124 Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap();
126 // Create a history array list.
127 ArrayList<History> historyArrayList = new ArrayList<>();
129 // 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.
130 for (int i=webBackForwardList.getSize() -1; i >= 0; i--) {
131 // Create a variable to store the favorite icon bitmap.
132 Bitmap favoriteIconBitmap;
134 // Determine the favorite icon bitmap
135 if (webBackForwardList.getItemAtIndex(i).getFavicon() == null) {
136 // If the web back forward list does not have a favorite icon, use Privacy Browser's default world icon.
137 favoriteIconBitmap = defaultFavoriteIcon;
138 } else { // Use the icon from the web back forward list.
139 favoriteIconBitmap = webBackForwardList.getItemAtIndex(i).getFavicon();
142 // Store the favorite icon and the URL in history entry.
143 History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl());
145 // Add this history entry to the history array list.
146 historyArrayList.add(historyEntry);
149 // 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.
150 int currentPageId = webBackForwardList.getSize() - 1 - currentPageIndex;
152 // Use an alert dialog builder to create the alert dialog.
153 AlertDialog.Builder dialogBuilder;
155 // Get a handle for the shared preferences.
156 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
158 // Get the screenshot and theme preferences.
159 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
160 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
162 // Set the style according to the theme.
164 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
166 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
170 dialogBuilder.setTitle(R.string.history);
172 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
173 dialogBuilder.setView(layoutInflater.inflate(R.layout.url_history_dialog, null));
175 // Setup the clear history button.
176 dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> {
177 // Clear the history.
178 nestedScrollWebView.clearHistory();
181 // Set an `onClick()` listener on the positive button.
182 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
183 // Do nothing if `Close` is clicked. The `Dialog` will automatically close.
186 // Create an alert dialog from the alert dialog builder.
187 final AlertDialog alertDialog = dialogBuilder.create();
189 // Disable screenshots if not allowed.
190 if (!allowScreenshots) {
191 // Remove the warning below that `getWindow()` might be null.
192 assert alertDialog.getWindow() != null;
194 // Disable screenshots.
195 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
198 //The alert dialog must be shown before the contents can be modified.
201 // Instantiate a history array adapter.
202 HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId);
204 // Get a handle for the list view.
205 ListView listView = alertDialog.findViewById(R.id.history_listview);
207 // Set the list view adapter.
208 listView.setAdapter(historyArrayAdapter);
210 // Listen for clicks on entries in the list view.
211 listView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
212 // Convert the long ID to an int.
213 int itemId = (int) id;
215 // Only consume the click if it is not on the `currentPageId`.
216 if (itemId != currentPageId) {
217 // Reset the current domain name so that navigation works if third-party requests are blocked.
218 nestedScrollWebView.resetCurrentDomainName();
220 // Set navigating history so that the domain settings are applied when the new URL is loaded.
221 nestedScrollWebView.setNavigatingHistory(true);
223 // Load the history entry.
224 nestedScrollWebView.goBackOrForward(currentPageId - itemId);
226 // Dismiss the alert dialog.
227 alertDialog.dismiss();
231 // Return the alert dialog.