2 * Copyright 2019-2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android 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 Android 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 Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.adapters
22 import android.os.Bundle
23 import android.widget.FrameLayout
25 import androidx.fragment.app.Fragment
26 import androidx.fragment.app.FragmentActivity
27 import androidx.recyclerview.widget.RecyclerView.NO_ID
28 import androidx.viewpager2.adapter.FragmentStateAdapter
29 import androidx.viewpager2.widget.ViewPager2
31 import com.stoutner.privacybrowser.R
32 import com.stoutner.privacybrowser.fragments.WebViewTabFragment
33 import com.stoutner.privacybrowser.views.NestedScrollWebView
35 import java.util.LinkedList
37 class WebViewStateAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
38 // Define the class variables.
39 private val webViewFragmentsList = LinkedList<WebViewTabFragment>()
41 // Get a page fragment.
42 override fun createFragment(pageNumber: Int): Fragment {
43 // Get the fragment for a particular page. Page numbers are 0 indexed.
44 return webViewFragmentsList[pageNumber]
47 override fun containsItem(itemId: Long): Boolean {
48 // Initialize the position variable.
51 // Initialize the while counter.
54 // Find the current position of the WebView fragment with the given ID.
55 while ((position < 0) && (i < webViewFragmentsList.size)) {
56 // Check to see if the tab ID of this WebView matches the page ID.
57 if (webViewFragmentsList[i].fragmentId == itemId) {
58 // Store the position if they are a match.
62 // Increment the counter.
66 // Return true if the item was found in the WebView fragments list.
67 return (position != -1)
70 // Get the number of tabs.
71 override fun getItemCount(): Int {
72 // Return the number of pages.
73 return webViewFragmentsList.size
76 // Get the unique ID for the item.
77 override fun getItemId(position: Int): Long {
78 // Return the unique ID for this page.
79 return if ((position >= 0) && (position < webViewFragmentsList.size)) // The position is 0 based, so it is contained in the WebView fragment list.
80 webViewFragmentsList[position].fragmentId
81 else // The item does not exist.
85 fun addPage(pagePosition: Int, url: String) {
87 webViewFragmentsList.add(pagePosition, WebViewTabFragment.createPage(pagePosition, url))
89 // Update the view pager.
90 notifyItemInserted(pagePosition)
93 fun deletePage(pageNumber: Int, webViewPager2: ViewPager2): Boolean {
94 // Get the WebView tab fragment.
95 val webViewTabFragment = webViewFragmentsList[pageNumber]
97 // Get the WebView frame layout.
98 val webViewFrameLayout = (webViewTabFragment.view as FrameLayout)
100 // Get a handle for the nested scroll WebView.
101 val nestedScrollWebView = webViewFrameLayout.findViewById<NestedScrollWebView>(R.id.nestedscroll_webview)
103 // Pause the current WebView.
104 nestedScrollWebView.onPause()
106 // Remove all the views from the frame layout.
107 webViewFrameLayout.removeAllViews()
109 // Destroy the current WebView.
110 nestedScrollWebView.destroy()
113 webViewFragmentsList.removeAt(pageNumber)
115 // Update the view pager.
116 notifyItemRemoved(pageNumber)
118 // Return true if the selected page number did not change after the delete (because the newly selected tab has has same number as the previously deleted tab).
119 // This will cause the calling method to reset the current WebView to the new contents of this page number.
120 return (webViewPager2.currentItem == pageNumber)
123 fun getPageFragment(pageNumber: Int): WebViewTabFragment {
124 // Return the page fragment.
125 return webViewFragmentsList[pageNumber]
128 fun getPositionForId(fragmentId: Long): Int {
129 // Initialize the position variable.
132 // Initialize the while counter.
135 // Find the current position of the WebView fragment with the given ID.
136 while ((position < 0) && (i < webViewFragmentsList.size)) {
137 // Check to see if the tab ID of this WebView matches the page ID.
138 if (webViewFragmentsList[i].fragmentId == fragmentId) {
139 // Store the position if they are a match.
143 // Increment the counter.
147 // Set the position to be the last tab if it is not found.
148 // Sometimes there is a race condition in populating the webView fragments list when resuming Privacy Browser and displaying an SSL certificate error while loading a new intent.
149 // In that case, the last tab should be the one it is looking for, which is one less than the size because it is zero based.
151 position = (webViewFragmentsList.size - 1)
153 // Return the position.
157 fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle) {
159 webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState))
161 // Update the view pager. The position is zero indexed.
162 notifyItemInserted(webViewFragmentsList.size - 1)