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 com.stoutner.privacybrowser.fragments.WebViewTabFragment;
24 import java.util.LinkedList;
26 import androidx.annotation.NonNull;
27 import androidx.fragment.app.Fragment;
28 import androidx.fragment.app.FragmentManager;
29 import androidx.fragment.app.FragmentPagerAdapter;
31 public class WebViewPagerAdapter extends FragmentPagerAdapter {
32 // The WebView fragments list contains all the WebViews.
33 private LinkedList<WebViewTabFragment> webViewFragmentsList = new LinkedList<>();
35 // Define the constructor.
36 public WebViewPagerAdapter(FragmentManager fragmentManager){
37 // Run the default commands.
38 super(fragmentManager);
42 public int getCount() {
43 // Return the number of pages.
44 return webViewFragmentsList.size();
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);
55 // The tab has been deleted.
61 public Fragment getItem(int pageNumber) {
62 // Get the fragment for a particular page. Page numbers are 0 indexed.
63 return webViewFragmentsList.get(pageNumber);
67 public long getItemId(int position) {
68 // Return the unique ID for this page.
69 return webViewFragmentsList.get(position).fragmentId;
72 public int getPositionForId(long fragmentId) {
73 // Initialize the position variable.
76 // Initialize the while counter.
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.
87 // Increment the counter.
91 // Return the position.
95 public void addPage(int pageNumber) {
97 webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber));
99 // Update the view pager.
100 notifyDataSetChanged();
103 public void deletePage(int pageNumber) {
105 webViewFragmentsList.remove(pageNumber);
107 // Update the view pager.
108 notifyDataSetChanged();
111 public WebViewTabFragment getPageFragment(int pageNumber) {
112 return webViewFragmentsList.get(pageNumber);