X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fadapters%2FWebViewPagerAdapter.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fadapters%2FWebViewPagerAdapter.java;h=3bec00521955b29d8e5ea315d00eb0b6242d7239;hp=0000000000000000000000000000000000000000;hb=af807cce079aaae9cbf0430e7da946fcbe0c99c3;hpb=231b7c038227e36f96ed28e9c2ff8bde793133ec diff --git a/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java new file mode 100644 index 00000000..3bec0052 --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java @@ -0,0 +1,114 @@ +/* + * Copyright © 2019 Soren Stoutner . + * + * This file is part of Privacy Browser . + * + * Privacy Browser is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser. If not, see . + */ + +package com.stoutner.privacybrowser.adapters; + +import com.stoutner.privacybrowser.fragments.WebViewTabFragment; + +import java.util.LinkedList; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; + +public class WebViewPagerAdapter extends FragmentPagerAdapter { + // The WebView fragments list contains all the WebViews. + private LinkedList webViewFragmentsList = new LinkedList<>(); + + // Define the constructor. + public WebViewPagerAdapter(FragmentManager fragmentManager){ + // Run the default commands. + super(fragmentManager); + } + + @Override + public int getCount() { + // Return the number of pages. + return webViewFragmentsList.size(); + } + + @Override + public int getItemPosition(@NonNull Object object) { + //noinspection SuspiciousMethodCalls + if (webViewFragmentsList.contains(object)) { + // Return the current page position. + //noinspection SuspiciousMethodCalls + return webViewFragmentsList.indexOf(object); + } else { + // The tab has been deleted. + return POSITION_NONE; + } + } + + @Override + public Fragment getItem(int pageNumber) { + // Get the fragment for a particular page. Page numbers are 0 indexed. + return webViewFragmentsList.get(pageNumber); + } + + @Override + public long getItemId(int position) { + // Return the unique ID for this page. + return webViewFragmentsList.get(position).tabId; + } + + public int getPositionForId(long pageId) { + // Initialize the position variable. + int position = -1; + + // Initialize the while counter. + int i = 0; + + // Find the current position of the WebView fragment with the given ID. + while (position < 0 && i < webViewFragmentsList.size()) { + // Check to see if the tab ID of this WebView matches the page ID. + if (webViewFragmentsList.get(i).tabId == pageId) { + // Store the position if they are a match. + position = i; + } + + // Increment the counter. + i++; + } + + // Return the position. + return position; + } + + public void addPage(int pageNumber) { + // Add a new page. + webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber)); + + // Update the view pager. + notifyDataSetChanged(); + } + + public void deletePage(int pageNumber) { + // Delete the page. + webViewFragmentsList.remove(pageNumber); + + // Update the view pager. + notifyDataSetChanged(); + } + + public WebViewTabFragment getPageFragment(int pageNumber) { + return webViewFragmentsList.get(pageNumber); + } +} \ No newline at end of file