]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
Fix a periodic crash while loading the SSL Certificate Error Dialog. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / WebViewPagerAdapter.java
1 /*
2  * Copyright © 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.adapters;
21
22 import androidx.annotation.NonNull;
23 import androidx.fragment.app.Fragment;
24 import androidx.fragment.app.FragmentManager;
25 import androidx.fragment.app.FragmentPagerAdapter;
26 import androidx.viewpager.widget.ViewPager;
27
28 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
29
30 import java.util.LinkedList;
31
32 public class WebViewPagerAdapter extends FragmentPagerAdapter {
33     // The WebView fragments list contains all the WebViews.
34     private LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
35
36     // Define the constructor.
37     public WebViewPagerAdapter(FragmentManager fragmentManager){
38         // Run the default commands.
39         super(fragmentManager);
40     }
41
42     @Override
43     public int getCount() {
44         // Return the number of pages.
45         return webViewFragmentsList.size();
46     }
47
48     @Override
49     public int getItemPosition(@NonNull Object object) {
50         //noinspection SuspiciousMethodCalls
51         if (webViewFragmentsList.contains(object)) {
52             // Return the current page position.
53             //noinspection SuspiciousMethodCalls
54             return webViewFragmentsList.indexOf(object);
55         } else {
56             // The tab has been deleted.
57             return POSITION_NONE;
58         }
59     }
60
61     @Override
62     public Fragment getItem(int pageNumber) {
63         // Get the fragment for a particular page.  Page numbers are 0 indexed.
64         return webViewFragmentsList.get(pageNumber);
65     }
66
67     @Override
68     public long getItemId(int position) {
69         // Return the unique ID for this page.
70         return webViewFragmentsList.get(position).fragmentId;
71     }
72
73     public int getPositionForId(long fragmentId) {
74         // Initialize the position variable.
75         int position = -1;
76
77         // Initialize the while counter.
78         int i = 0;
79
80         // Find the current position of the WebView fragment with the given ID.
81         while (position < 0 && i < webViewFragmentsList.size()) {
82             // Check to see if the tab ID of this WebView matches the page ID.
83             if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
84                 // Store the position if they are a match.
85                 position = i;
86             }
87
88             // Increment the counter.
89             i++;
90         }
91
92         // Set the position to be the last tab if it is not found.
93         // 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.
94         // In that case, the last tab should be the one it is looking for.
95         if (position == -1) {
96             position = webViewFragmentsList.size() - 1;
97         }
98
99         // Return the position.
100         return position;
101     }
102
103     public void addPage(int pageNumber, ViewPager webViewPager, String url) {
104         // Add a new page.
105         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
106
107         // Update the view pager.
108         notifyDataSetChanged();
109
110         // Move to the new page if it isn't the first one.
111         if (pageNumber > 0) {
112             webViewPager.setCurrentItem(pageNumber);
113         }
114     }
115
116     public boolean deletePage(int pageNumber, ViewPager webViewPager) {
117         // Delete the page.
118         webViewFragmentsList.remove(pageNumber);
119
120         // Update the view pager.
121         notifyDataSetChanged();
122
123         // 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.
124         return (webViewPager.getCurrentItem() == pageNumber);
125     }
126
127     public WebViewTabFragment getPageFragment(int pageNumber) {
128         return webViewFragmentsList.get(pageNumber);
129     }
130 }