]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/UrlHistoryDialog.java
Fix applying domain settings when navigating history. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / UrlHistoryDialog.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs;
21
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;
40
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.
44
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;
51
52 import java.util.ArrayList;
53
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);
58     }
59
60     // The navigate history listener is used in `onAttach()` and `onCreateDialog()`.
61     private NavigateHistoryListener navigateHistoryListener;
62
63     @Override
64     public void onAttach(Context context) {
65         // Run the default commands.
66         super.onAttach(context);
67
68         // Get a handle for the listener from the launching context.
69         navigateHistoryListener = (NavigateHistoryListener) context;
70     }
71
72     public static UrlHistoryDialog loadBackForwardList(long webViewFragmentId) {
73         // Create an arguments bundle.
74         Bundle argumentsBundle = new Bundle();
75
76         // Store the WebView fragment ID in the bundle.
77         argumentsBundle.putLong("webview_fragment_id", webViewFragmentId);
78
79         // Create a new instance of the URL history dialog.
80         UrlHistoryDialog urlHistoryDialog = new UrlHistoryDialog();
81
82         // Add the arguments bundle to this instance.
83         urlHistoryDialog.setArguments(argumentsBundle);
84
85         // Return the new URL history dialog.
86         return urlHistoryDialog;
87     }
88
89     @Override
90     @NonNull
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;
96
97         // Get the activity's layout inflater.
98         LayoutInflater layoutInflater = getActivity().getLayoutInflater();
99
100         // Get the arguments.
101         Bundle arguments = getArguments();
102
103         // Remove the incorrect lint error that arguments might be null.
104         assert arguments != null;
105
106         // Get the WebView fragment ID from the arguments.
107         long webViewFragmentId = arguments.getLong("webview_fragment_id");
108
109         // Get the current position of this WebView fragment.
110         int webViewPosition = MainWebViewActivity.webViewPagerAdapter.getPositionForId(webViewFragmentId);
111
112         // Get the WebView tab fragment.
113         WebViewTabFragment webViewTabFragment = MainWebViewActivity.webViewPagerAdapter.getPageFragment(webViewPosition);
114
115         // Get the fragment view.
116         View fragmentView = webViewTabFragment.getView();
117
118         // Remove the incorrect lint warning below that the fragment view might be null.
119         assert fragmentView != null;
120
121         // Get a handle for the current WebView.
122         NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview);
123
124         // Get the web back forward list from the WebView.
125         WebBackForwardList webBackForwardList = nestedScrollWebView.copyBackForwardList();
126
127         // Store the current page index.
128         int currentPageIndex = webBackForwardList.getCurrentIndex();
129
130         // Remove the lint warning below that `getContext()` might be null.
131         assert getContext() != null;
132
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);
135
136         // Convert the default favorite icon drawable to a `BitmapDrawable`.
137         BitmapDrawable defaultFavoriteIconBitmapDrawable = (BitmapDrawable) defaultFavoriteIconDrawable;
138
139         // Remove the incorrect lint error that `getBitmap()` might be null.
140         assert defaultFavoriteIconBitmapDrawable != null;
141
142         // Extract a bitmap from the default favorite icon bitmap drawable.
143         Bitmap defaultFavoriteIcon = defaultFavoriteIconBitmapDrawable.getBitmap();
144
145         // Create a history array list.
146         ArrayList<History> historyArrayList = new ArrayList<>();
147
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;
152
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();
159             }
160
161             // Store the favorite icon and the URL in history entry.
162             History historyEntry = new History(favoriteIconBitmap, webBackForwardList.getItemAtIndex(i).getUrl());
163
164             // Add this history entry to the history array list.
165             historyArrayList.add(historyEntry);
166         }
167
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;
170
171         // Use an alert dialog builder to create the alert dialog.
172         AlertDialog.Builder dialogBuilder;
173
174         // Get a handle for the shared preferences.
175         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
176
177         // Get the screenshot and theme preferences.
178         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
179         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
180
181         // Set the style according to the theme.
182         if (darkTheme) {
183             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
184         } else {
185             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
186         }
187
188         // Set the title.
189         dialogBuilder.setTitle(R.string.history);
190
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));
193
194         // Setup the clear history button.
195         dialogBuilder.setNegativeButton(R.string.clear_history, (DialogInterface dialog, int which) -> {
196             // Clear the history.
197             nestedScrollWebView.clearHistory();
198         });
199
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.
203         });
204
205         // Create an alert dialog from the alert dialog builder.
206         final AlertDialog alertDialog = dialogBuilder.create();
207
208         // Disable screenshots if not allowed.
209         if (!allowScreenshots) {
210             // Remove the warning below that `getWindow()` might be null.
211             assert alertDialog.getWindow() != null;
212
213             // Disable screenshots.
214             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
215         }
216
217         //The alert dialog must be shown before the contents can be modified.
218         alertDialog.show();
219
220         // Instantiate a history array adapter.
221         HistoryArrayAdapter historyArrayAdapter = new HistoryArrayAdapter(getContext(), historyArrayList, currentPageId);
222
223         // Get a handle for the list view.
224         ListView listView = alertDialog.findViewById(R.id.history_listview);
225
226         // Set the list view adapter.
227         listView.setAdapter(historyArrayAdapter);
228
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;
233
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);
238
239                 // Get the URL.
240                 String url = urlTextView.getText().toString();
241
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);
244
245                 // Dismiss the alert dialog.
246                 alertDialog.dismiss();
247             }
248         });
249
250         // Return the alert dialog.
251         return alertDialog;
252     }
253 }