2 * Copyright © 2019-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.adapters;
22 import android.os.Bundle;
24 import androidx.annotation.NonNull;
25 import androidx.fragment.app.Fragment;
26 import androidx.fragment.app.FragmentManager;
27 import androidx.fragment.app.FragmentPagerAdapter;
28 import androidx.viewpager.widget.ViewPager;
30 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
32 import java.util.LinkedList;
34 public class WebViewPagerAdapter extends FragmentPagerAdapter {
35 // The WebView fragments list contains all the WebViews.
36 private final LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
38 // Define the constructor.
39 public WebViewPagerAdapter(FragmentManager fragmentManager) {
40 // Run the default commands.
41 super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
45 public int getCount() {
46 // Return the number of pages.
47 return webViewFragmentsList.size();
51 public int getItemPosition(@NonNull Object object) {
52 //noinspection SuspiciousMethodCalls
53 if (webViewFragmentsList.contains(object)) {
54 // Return the current page position.
55 //noinspection SuspiciousMethodCalls
56 return webViewFragmentsList.indexOf(object);
58 // The tab has been deleted.
65 public Fragment getItem(int pageNumber) {
66 // Get the fragment for a particular page. Page numbers are 0 indexed.
67 return webViewFragmentsList.get(pageNumber);
71 public long getItemId(int position) {
72 // Return the unique ID for this page.
73 return webViewFragmentsList.get(position).fragmentId;
76 public int getPositionForId(long fragmentId) {
77 // Initialize the position variable.
80 // Initialize the while counter.
83 // Find the current position of the WebView fragment with the given ID.
84 while (position < 0 && i < webViewFragmentsList.size()) {
85 // Check to see if the tab ID of this WebView matches the page ID.
86 if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
87 // Store the position if they are a match.
91 // Increment the counter.
95 // Set the position to be the last tab if it is not found.
96 // 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.
97 // In that case, the last tab should be the one it is looking for.
99 position = webViewFragmentsList.size() - 1;
102 // Return the position.
106 public void addPage(int pageNumber, ViewPager webViewPager, String url, boolean moveToNewPage) {
108 webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
110 // Update the view pager.
111 notifyDataSetChanged();
113 // Move to the new page if indicated.
115 webViewPager.setCurrentItem(pageNumber);
119 public void restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
121 webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState));
123 // Update the view pager.
124 notifyDataSetChanged();
127 public boolean deletePage(int pageNumber, ViewPager webViewPager) {
129 webViewFragmentsList.remove(pageNumber);
131 // Update the view pager.
132 notifyDataSetChanged();
134 // Return true if the selected page number did not change after the delete. This will cause the calling method to reset the current WebView to the new contents of this page number.
135 return (webViewPager.getCurrentItem() == pageNumber);
138 public WebViewTabFragment getPageFragment(int pageNumber) {
139 // Return the page fragment.
140 return webViewFragmentsList.get(pageNumber);