]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
Use WebView's new built-in dark theme. https://redmine.stoutner.com/issues/366
[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 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     @NonNull
63     public Fragment getItem(int pageNumber) {
64         // Get the fragment for a particular page.  Page numbers are 0 indexed.
65         return webViewFragmentsList.get(pageNumber);
66     }
67
68     @Override
69     public long getItemId(int position) {
70         // Return the unique ID for this page.
71         return webViewFragmentsList.get(position).fragmentId;
72     }
73
74     public int getPositionForId(long fragmentId) {
75         // Initialize the position variable.
76         int position = -1;
77
78         // Initialize the while counter.
79         int i = 0;
80
81         // Find the current position of the WebView fragment with the given ID.
82         while (position < 0 && i < webViewFragmentsList.size()) {
83             // Check to see if the tab ID of this WebView matches the page ID.
84             if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
85                 // Store the position if they are a match.
86                 position = i;
87             }
88
89             // Increment the counter.
90             i++;
91         }
92
93         // Set the position to be the last tab if it is not found.
94         // 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.
95         // In that case, the last tab should be the one it is looking for.
96         if (position == -1) {
97             position = webViewFragmentsList.size() - 1;
98         }
99
100         // Return the position.
101         return position;
102     }
103
104     public void addPage(int pageNumber, ViewPager webViewPager, String url, boolean moveToTab) {
105         // Add a new page.
106         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
107
108         // Update the view pager.
109         notifyDataSetChanged();
110
111         // Move to the new page if it isn't the first one.
112         if (pageNumber > 0 && moveToTab) {
113             webViewPager.setCurrentItem(pageNumber);
114         }
115     }
116
117     public boolean deletePage(int pageNumber, ViewPager webViewPager) {
118         // Delete the page.
119         webViewFragmentsList.remove(pageNumber);
120
121         // Update the view pager.
122         notifyDataSetChanged();
123
124         // 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.
125         return (webViewPager.getCurrentItem() == pageNumber);
126     }
127
128     public WebViewTabFragment getPageFragment(int pageNumber) {
129         return webViewFragmentsList.get(pageNumber);
130     }
131 }