2 * Copyright © 2019-2021 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.adapters;
22 import android.os.Bundle;
23 import android.widget.FrameLayout;
25 import androidx.annotation.NonNull;
26 import androidx.fragment.app.Fragment;
27 import androidx.fragment.app.FragmentManager;
28 import androidx.fragment.app.FragmentPagerAdapter;
29 import androidx.viewpager.widget.ViewPager;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
33 import com.stoutner.privacybrowser.views.NestedScrollWebView;
35 import java.util.LinkedList;
37 public class WebViewPagerAdapter extends FragmentPagerAdapter {
38 // The WebView fragments list contains all the WebViews.
39 private final LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
41 // Define the constructor.
42 public WebViewPagerAdapter(FragmentManager fragmentManager) {
43 // Run the default commands.
44 super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
48 public int getCount() {
49 // Return the number of pages.
50 return webViewFragmentsList.size();
54 public int getItemPosition(@NonNull Object object) {
55 //noinspection SuspiciousMethodCalls
56 if (webViewFragmentsList.contains(object)) {
57 // Return the current page position.
58 //noinspection SuspiciousMethodCalls
59 return webViewFragmentsList.indexOf(object);
61 // The tab has been deleted.
68 public Fragment getItem(int pageNumber) {
69 // Get the fragment for a particular page. Page numbers are 0 indexed.
70 return webViewFragmentsList.get(pageNumber);
74 public long getItemId(int position) {
75 // Return the unique ID for this page.
76 return webViewFragmentsList.get(position).fragmentId;
79 public int getPositionForId(long fragmentId) {
80 // Initialize the position variable.
83 // Initialize the while counter.
86 // Find the current position of the WebView fragment with the given ID.
87 while (position < 0 && i < webViewFragmentsList.size()) {
88 // Check to see if the tab ID of this WebView matches the page ID.
89 if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
90 // Store the position if they are a match.
94 // Increment the counter.
98 // Set the position to be the last tab if it is not found.
99 // Sometimes there is a race condition in populating the webView fragments list when resuming Privacy Browser and displaying an SSL certificate error while loading a new intent.
100 // In that case, the last tab should be the one it is looking for.
101 if (position == -1) {
102 position = webViewFragmentsList.size() - 1;
105 // Return the position.
109 public void addPage(int pageNumber, ViewPager webViewPager, String url, boolean moveToNewPage) {
111 webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
113 // Update the view pager.
114 notifyDataSetChanged();
116 // Move to the new page if indicated.
118 webViewPager.setCurrentItem(pageNumber);
122 public void restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
124 webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState));
126 // Update the view pager.
127 notifyDataSetChanged();
130 public boolean deletePage(int pageNumber, ViewPager webViewPager) {
131 // Get the WebView tab fragment.
132 WebViewTabFragment webViewTabFragment = webViewFragmentsList.get(pageNumber);
134 // Get the WebView frame layout.
135 FrameLayout webViewFrameLayout = (FrameLayout) webViewTabFragment.getView();
137 // Remove the warning below that the WebView frame layout might be null.
138 assert webViewFrameLayout != null;
140 // Get a handle for the nested scroll WebView.
141 NestedScrollWebView nestedScrollWebView = webViewFrameLayout.findViewById(R.id.nestedscroll_webview);
143 // Pause the current WebView.
144 nestedScrollWebView.onPause();
146 // Remove all the views from the frame layout.
147 webViewFrameLayout.removeAllViews();
149 // Destroy the current WebView.
150 nestedScrollWebView.destroy();
153 webViewFragmentsList.remove(pageNumber);
155 // Update the view pager.
156 notifyDataSetChanged();
158 // Return true if the selected page number did not change after the delete (because the newly selected tab has has same number as the previously deleted tab).
159 // This will cause the calling method to reset the current WebView to the new contents of this page number.
160 return (webViewPager.getCurrentItem() == pageNumber);
163 public WebViewTabFragment getPageFragment(int pageNumber) {
164 // Return the page fragment.
165 return webViewFragmentsList.get(pageNumber);