]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java
Make pinned settings tab aware.
[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 com.stoutner.privacybrowser.fragments.WebViewTabFragment;
23
24 import java.util.LinkedList;
25
26 import androidx.annotation.NonNull;
27 import androidx.fragment.app.Fragment;
28 import androidx.fragment.app.FragmentManager;
29 import androidx.fragment.app.FragmentPagerAdapter;
30
31 public class WebViewPagerAdapter extends FragmentPagerAdapter {
32     // The WebView fragments list contains all the WebViews.
33     private LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
34
35     // Define the constructor.
36     public WebViewPagerAdapter(FragmentManager fragmentManager){
37         // Run the default commands.
38         super(fragmentManager);
39     }
40
41     @Override
42     public int getCount() {
43         // Return the number of pages.
44         return webViewFragmentsList.size();
45     }
46
47     @Override
48     public int getItemPosition(@NonNull Object object) {
49         //noinspection SuspiciousMethodCalls
50         if (webViewFragmentsList.contains(object)) {
51             // Return the current page position.
52             //noinspection SuspiciousMethodCalls
53             return webViewFragmentsList.indexOf(object);
54         } else {
55             // The tab has been deleted.
56             return POSITION_NONE;
57         }
58     }
59
60     @Override
61     public Fragment getItem(int pageNumber) {
62         // Get the fragment for a particular page.  Page numbers are 0 indexed.
63         return webViewFragmentsList.get(pageNumber);
64     }
65
66     @Override
67     public long getItemId(int position) {
68         // Return the unique ID for this page.
69         return webViewFragmentsList.get(position).fragmentId;
70     }
71
72     public int getPositionForId(long fragmentId) {
73         // Initialize the position variable.
74         int position = -1;
75
76         // Initialize the while counter.
77         int i = 0;
78
79         // Find the current position of the WebView fragment with the given ID.
80         while (position < 0 && i < webViewFragmentsList.size()) {
81             // Check to see if the tab ID of this WebView matches the page ID.
82             if (webViewFragmentsList.get(i).fragmentId == fragmentId) {
83                 // Store the position if they are a match.
84                 position = i;
85             }
86
87             // Increment the counter.
88             i++;
89         }
90
91         // Return the position.
92         return position;
93     }
94
95     public void addPage(int pageNumber) {
96         // Add a new page.
97         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber));
98
99         // Update the view pager.
100         notifyDataSetChanged();
101     }
102
103     public void deletePage(int pageNumber) {
104         // Delete the page.
105         webViewFragmentsList.remove(pageNumber);
106
107         // Update the view pager.
108         notifyDataSetChanged();
109     }
110
111     public WebViewTabFragment getPageFragment(int pageNumber) {
112         return webViewFragmentsList.get(pageNumber);
113     }
114 }