]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
5fb604ae185b0a558bdf6cf6847c3ba2cfd5f9a4
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / WebViewPagerAdapter.java
1 /*
2  * Copyright © 2019-2020 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.adapters;
21
22 import android.os.Bundle;
23
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;
29
30 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
31
32 import java.util.LinkedList;
33
34 public class WebViewPagerAdapter extends FragmentPagerAdapter {
35     // The WebView fragments list contains all the WebViews.
36     private final LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
37
38     // Define the constructor.
39     public WebViewPagerAdapter(FragmentManager fragmentManager) {
40         // Run the default commands.
41         super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
42     }
43
44     @Override
45     public int getCount() {
46         // Return the number of pages.
47         return webViewFragmentsList.size();
48     }
49
50     @Override
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);
57         } else {
58             // The tab has been deleted.
59             return POSITION_NONE;
60         }
61     }
62
63     @Override
64     @NonNull
65     public Fragment getItem(int pageNumber) {
66         // Get the fragment for a particular page.  Page numbers are 0 indexed.
67         return webViewFragmentsList.get(pageNumber);
68     }
69
70     @Override
71     public long getItemId(int position) {
72         // Return the unique ID for this page.
73         return webViewFragmentsList.get(position).fragmentId;
74     }
75
76     public int getPositionForId(long fragmentId) {
77         // Initialize the position variable.
78         int position = -1;
79
80         // Initialize the while counter.
81         int i = 0;
82
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.
88                 position = i;
89             }
90
91             // Increment the counter.
92             i++;
93         }
94
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.
98         if (position == -1) {
99             position = webViewFragmentsList.size() - 1;
100         }
101
102         // Return the position.
103         return position;
104     }
105
106     public void addPage(int pageNumber, ViewPager webViewPager, String url, boolean moveToNewPage) {
107         // Add a new page.
108         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
109
110         // Update the view pager.
111         notifyDataSetChanged();
112
113         // Move to the new page if indicated.
114         if (moveToNewPage) {
115             webViewPager.setCurrentItem(pageNumber);
116         }
117     }
118
119     public void restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
120         // Restore the page.
121         webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState));
122
123         // Update the view pager.
124         notifyDataSetChanged();
125     }
126
127     public boolean deletePage(int pageNumber, ViewPager webViewPager) {
128         // Delete the page.
129         webViewFragmentsList.remove(pageNumber);
130
131         // Update the view pager.
132         notifyDataSetChanged();
133
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);
136     }
137
138     public WebViewTabFragment getPageFragment(int pageNumber) {
139         // Return the page fragment.
140         return webViewFragmentsList.get(pageNumber);
141     }
142 }