]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
256e654236d7fe5b0ed7fdb0e7680c9882d0b0bb
[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         // Return the position.
93         return position;
94     }
95
96     public void addPage(int pageNumber, ViewPager webViewPager, String url) {
97         // Add a new page.
98         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url));
99
100         // Update the view pager.
101         notifyDataSetChanged();
102
103         // Move to the new page if it isn't the first one.
104         if (pageNumber > 0) {
105             webViewPager.setCurrentItem(pageNumber);
106         }
107     }
108
109     public boolean deletePage(int pageNumber, ViewPager webViewPager) {
110         // Delete the page.
111         webViewFragmentsList.remove(pageNumber);
112
113         // Update the view pager.
114         notifyDataSetChanged();
115
116         // 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.
117         return (webViewPager.getCurrentItem() == pageNumber);
118     }
119
120     public WebViewTabFragment getPageFragment(int pageNumber) {
121         return webViewFragmentsList.get(pageNumber);
122     }
123 }