2 * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.adapters;
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;
28 import com.stoutner.privacybrowser.fragments.WebViewTabFragment;
30 import java.util.LinkedList;
32 public class WebViewPagerAdapter extends FragmentPagerAdapter {
33 // The WebView fragments list contains all the WebViews.
34 private LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
36 // Define the constructor.
37 public WebViewPagerAdapter(FragmentManager fragmentManager){
38 // Run the default commands.
39 super(fragmentManager);
43 public int getCount() {
44 // Return the number of pages.
45 return webViewFragmentsList.size();
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);
56 // The tab has been deleted.
62 public Fragment getItem(int pageNumber) {
63 // Get the fragment for a particular page. Page numbers are 0 indexed.
64 return webViewFragmentsList.get(pageNumber);
68 public long getItemId(int position) {
69 // Return the unique ID for this page.
70 return webViewFragmentsList.get(position).fragmentId;
73 public int getPositionForId(long fragmentId) {
74 // Initialize the position variable.
77 // Initialize the while counter.
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.
88 // Increment the counter.
92 // Return the position.
96 public void addPage(int pageNumber, ViewPager webViewPager) {
98 webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber));
100 // Update the view pager.
101 notifyDataSetChanged();
103 // Move to the new page if it isn't the first one.
104 if (pageNumber > 0) {
105 webViewPager.setCurrentItem(pageNumber);
109 public void deletePage(int pageNumber) {
111 webViewFragmentsList.remove(pageNumber);
113 // Update the view pager.
114 notifyDataSetChanged();
117 public WebViewTabFragment getPageFragment(int pageNumber) {
118 return webViewFragmentsList.get(pageNumber);