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.Context;
26 import android.content.DialogInterface;
27 import android.content.SharedPreferences;
28 import android.graphics.Bitmap;
29 import android.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.preference.PreferenceManager;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.WindowManager;
36 import android.webkit.WebBackForwardList;
37 import android.widget.AdapterView;
38 import android.widget.ListView;
39 import android.widget.TextView;
41 import androidx.annotation.NonNull;
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 // Remove the incorrect lint warning that `getActivity()` might be null.
95 assert getActivity() != null;
97 // Get the activity's layout inflater.
98 LayoutInflater layoutInflater = getActivity().getLayoutInflater();
100 // Get the arguments.
101 Bundle arguments = getArguments();
103 // Remove the incorrect lint error that arguments might be null.
104 assert arguments != null;
106 // Get the WebView fragment ID from the arguments.
107 long webViewFragmentId = arguments.getLong("webview_fragment_id");
109 // Get the current position of this WebView fragment.
110 int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId);
112 // Get the WebView tab fragment.
113 WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
115 // Get the fragment view.
116 View fragmentView = webViewTabFragment.getView();
118 // Remove the incorrect lint warning below that the fragment view might be null.
119 assert fragmentView != null;
121 // Get a handle for the current WebView.
122 NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
124 // Get the web back forward list from the WebView.
125 WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList();
127 // Store the current page index.
128 int currentPageIndex = webBackForwardList.getCurrentIndex();
130 // Remove the lint warning below that `getContext()` might be null.
131 assert getContext() != null;
133 // Get the default favorite icon drawable. `ContextCompat` must be used until the minimum API >= 21.
134 Drawable defaultFavoriteIconDrawable = ContextCompat.getDrawable(getContext(), R.drawable.world);
136 // Convert the default favorite icon drawable to a `BitmapDrawable`.
137 BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable;
139 // Remove the incorrect lint error that `getBitmap()` might be null.
140 assert defaultFavoriteIconBitmapDrawable != null;
142 // Extract a bitmap from the default favorite icon bitmap drawable.
143 Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap();
145 // Create a history array list.
146 ArrayList<History> historyArrayList = new ArrayList<>();
148 // 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.
149 for (int i=webBackForwardList.getSize() -1; i >= 0; i--) {
150 // Create a variable to store the favorite icon bitmap.
151 Bitmap favoriteIconBitmap;
153 // Determine the favorite icon bitmap
154 if (webBackForwardList.getItemAtIndex(i).getFavicon() == null) {
155 // If the web back forward list does not have a favorite icon, use Privacy Browser's default world icon.
156 favoriteIconBitmap = defaultFavoriteIcon;
157 } else { // Use the icon from the web back forward list.
158 favoriteIconBitmap = webBackForwardList.getItemAtIndex(i).getFavicon();
161 // Store the favorite icon and the URL in history entry.
162 History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl());
164 // Add this history entry to the history array list.
165 historyArrayList.add(historyEntry);
168 // 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.
169 int currentPageId = webBackForwardList.getSize() - 1 - currentPageIndex;
171 // Use an alert dialog builder to create the alert dialog.
172 AlertDialog.Builder dialogBuilder;
174 // Get a handle for the shared preferences.
175 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
177 // Get the screenshot and theme preferences.
178 boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
179 boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
181 // Set the style according to the theme.
183 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
185 dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
189 dialogBuilder.setTitle(R.string.history);
191 // Set the view. The parent view is `null` because it will be assigned by `AlertDialog`.
192 dialogBuilder.setView(layoutInflater.inflate(R.layout.url_history_dialog, null));
194 // Setup the clear history button.
195 dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> {
196 // Clear the history.
197 nestedScrollWebView.clearHistory();
200 // Set an `onClick()` listener on the positive button.
201 dialogBuilder.setPositiveButton(R.string.close, (DialogInterface dialog, int which) -> {
202 // Do nothing if `Close` is clicked. The `Dialog` will automatically close.
205 // Create an alert dialog from the alert dialog builder.
206 final AlertDialog alertDialog = dialogBuilder.create();
208 // Disable screenshots if not allowed.
209 if (!allowScreenshots) {
210 // Remove the warning below that `getWindow()` might be null.
211 assert alertDialog.getWindow() != null;
213 // Disable screenshots.
214 alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
217 //The alert dialog must be shown before the contents can be modified.
220 // Instantiate a history array adapter.
221 HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId);
223 // Get a handle for the list view.
224 ListView listView = alertDialog.findViewById(R.id.history_listview);
226 // Set the list view adapter.
227 listView.setAdapter(historyArrayAdapter);
229 // Listen for clicks on entries in the list view.
230 listView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
231 // Convert the long ID to an int.
232 int itemId = (int) id;
234 // Only consume the click if it is not on the `currentPageId`.
235 if (itemId != currentPageId) {
236 // Get a handle for the URL text view.
237 TextView urlTextView = view.findViewById(R.id.history_url_textview);
240 String url = urlTextView.getText().toString();
242 // Invoke the navigate history listener in the calling activity. These commands cannot be run here because they need access to `applyDomainSettings()`.
243 navigateHistoryListener.navigateHistory(url, currentPageId - itemId);
245 // Dismiss the alert dialog.
246 alertDialog.dismiss();
250 // Return the alert dialog.