2 * Copyright © 2016-2020 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.Dialog;
24 import android.content.Context;
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;
38 import android.widget.TextView;
40 import androidx.annotation.NonNull;
41 import androidx.appcompat.app.AlertDialog;
42 import androidx.core.content.ContextCompat;
43 import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22.
45 import com.stoutner.privacybrowser.R;
46 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
47 import com.stoutner.privacybrowser.adapters.HistoryArrayAdapter;
48 import com.stoutner.privacybrowser.definitions.History;
49 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
50 import com.stoutner.privacybrowser.views.NestedScrollWebView;
52 import java.util.ArrayList;
54 public class UrlHistoryDialog extends DialogFragment{
55 // The public interface is used to send information back to the parent activity.
56 public interface NavigateHistoryListener {
57 void navigateHistory(String url, int steps);
60 // The navigate history listener is used in `onAttach()` and `onCreateDialog()`.
61 private NavigateHistoryListener navigateHistoryListener;
64 public void onAttach(@NonNull Context context) {
65 // Run the default commands.
66 super.onAttach(context);
68 // Get a handle for the listener from the launching context.
69 navigateHistoryListener = (NavigateHistoryListener) context;
72 public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) {
73 // Create an arguments bundle.
74 Bundle argumentsBundle = new Bundle();
76 // Store the WebView fragment ID in the bundle.
77 argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
79 // Create a new instance of the URL history dialog.
80 UrlHistoryDialog urlHistoryDialog = new UrlHistoryDialog();
82 // Add the arguments bundle to this instance.
83 urlHistoryDialog.setArguments(argumentsBundle);
85 // Return the new URL history dialog.
86 return urlHistoryDialog;
91 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the `AlertDialog`.
92 @SuppressLint("InflateParams")
93 public Dialog onCreateDialog(Bundle savedInstanceState) {
94 // Get the activity's layout inflater.
95 LayoutInflater layoutInflater = requireActivity().getLayoutInflater();
98 Bundle arguments = getArguments();
100 // Remove the incorrect lint error that arguments might be null.
101 assert arguments != null;
103 // Get the WebView fragment ID from the arguments.
104 long webViewFragmentId = arguments.getLong("webview_fragment_id");
106 // Get the current position of this WebView fragment.
107 int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId);
109 // Get the WebView tab fragment.
110 WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
112 // Get the fragment view.
113 View fragmentView = webViewTabFragment.getView();
115 // Remove the incorrect lint warning below that the fragment view might be null.
116 assert fragmentView != null;
118 // Get a handle for the current WebView.
119 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
121 // Get the web back forward list from the WebView.
122 WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList();
124 // Store the current page index.
125 int currentPageIndex = webBackForwardList.getCurrentIndex();
127 // Remove the lint warning below that `getContext()` might be null.
128 assert getContext() != null;
130 // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21.
131 Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world);
133 // Convert the default favorite icon drawable to a `BitmapDrawable`.
134 BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable;
136 // Remove the incorrect lint error that `getBitmap()` might be null.
137 assert defaultFavoriteIconBitmapDrawable != null;
139 // Extract a bitmap from the default favorite icon bitmap drawable.
140 Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap();
142 // Create a history array list.
143 ArrayList<History> historyArrayList = new ArrayList<>();
145 // 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.
146 for (int i=webBackForwardList.getSize() -1; i >= 0; i--) {
147 // Create a variable to store the favorite icon bitmap.
148 Bitmap favoriteIconBitmap;
150 // Determine the favorite icon bitmap
151 if (webBackForwardList.getItemAtIndex(i).getFavicon() == null) {
152 // If the web back forward list does not have a favorite icon, use Privacy Browser's default world icon.
153 favoriteIconBitmap = defaultFavoriteIcon;
154 } else { // Use the icon from the web back forward list.
155 favoriteIconBitmap = webBackForwardList.getItemAtIndex(i).getFavicon();
158 // Store the favorite icon and the URL in history entry.
159 History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl());
161 // Add this history entry to the history array list.
162 historyArrayList.add(historyEntry);
165 // 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.
166 int currentPageId = webBackForwardList.getSize() - 1 - currentPageIndex;
168 // Use an alert dialog builder to create the alert dialog.
169 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog);
172 dialogBuilder.setTitle(R.string.history);
174 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
175 dialogBuilder.setView(layoutInflater.inflate(R.layout.url_history_dialog, null));
177 // Setup the clear history button.
178 dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> {
179 // Clear the history.
180 nestedScrollWebView.clearHistory();
183 // Set an `onClick()` listener on the positive button.
184 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
185 // Do nothing if `Close` is clicked. The `Dialog` will automatically close.
188 // Create an alert dialog from the alert dialog builder.
189 final AlertDialog alertDialog = dialogBuilder.create();
191 // Get a handle for the shared preferences.
192 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
194 // Get the screenshot preference.
195 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
197 // Disable screenshots if not allowed.
198 if (!allowScreenshots) {
199 // Remove the warning below that `getWindow()` might be null.
200 assert alertDialog.getWindow() != null;
202 // Disable screenshots.
203 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
206 //The alert dialog must be shown before the contents can be modified.
209 // Instantiate a history array adapter.
210 HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId);
212 // Get a handle for the list view.
213 ListView listView = alertDialog.findViewById(R.id.history_listview);
215 // Remove the incorrect lint warning below that the view might be null.
216 assert listView != null;
218 // Set the list view adapter.
219 listView.setAdapter(historyArrayAdapter);
221 // Listen for clicks on entries in the list view.
222 listView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
223 // Convert the long ID to an int.
224 int itemId = (int) id;
226 // Only consume the click if it is not on the `currentPageId`.
227 if (itemId != currentPageId) {
228 // Get a handle for the URL text view.
229 TextView urlTextView = view.findViewById(R.id.history_url_textview);
232 String url = urlTextView.getText().toString();
234 // Invoke the navigate history listener in the calling activity. These commands cannot be run here because they need access to `applyDomainSettings()`.
235 navigateHistoryListener.navigateHistory(url, currentPageId - itemId);
237 // Dismiss the alert dialog.
238 alertDialog.dismiss();
242 // Return the alert dialog.