]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt
Switch the FragmentPagerAdapters to FragmentStateAdapters. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / WebViewStateAdapter.kt
1 /*
2  * Copyright © 2019-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.adapters
21
22 import android.os.Bundle
23 import android.widget.FrameLayout
24
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
30
31 import com.stoutner.privacybrowser.R
32 import com.stoutner.privacybrowser.fragments.WebViewTabFragment
33 import com.stoutner.privacybrowser.views.NestedScrollWebView
34
35 import java.util.LinkedList
36
37 class WebViewStateAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
38     // Define the class variables.
39     private val webViewFragmentsList = LinkedList<WebViewTabFragment>()
40
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]
45     }
46
47     override fun containsItem(itemId: Long): Boolean {
48         // Initialize the position variable.
49         var position = -1
50
51         // Initialize the while counter.
52         var i = 0
53
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.
59                 position = i
60             }
61
62             // Increment the counter.
63             i++
64         }
65
66         // Return true if the item was found in the WebView fragments list.
67         return (position != -1)
68     }
69
70     // Get the number of tabs.
71     override fun getItemCount(): Int {
72         // Return the number of pages.
73         return webViewFragmentsList.size
74     }
75
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.
82             NO_ID
83     }
84
85     fun addPage(pageNumber: Int, webViewViewPager2: ViewPager2, url: String, moveToNewPage: Boolean) {
86         // Add a new page.
87         webViewFragmentsList.add(WebViewTabFragment.createPage(pageNumber, url))
88
89         // Update the view pager.
90         notifyItemInserted(pageNumber)
91
92         // Move to the new page if indicated.
93         if (moveToNewPage)
94             webViewViewPager2.currentItem = pageNumber
95     }
96
97     fun deletePage(pageNumber: Int, webViewPager2: ViewPager2): Boolean {
98         // Get the WebView tab fragment.
99         val webViewTabFragment = webViewFragmentsList[pageNumber]
100
101         // Get the WebView frame layout.
102         val webViewFrameLayout = (webViewTabFragment.view as FrameLayout)
103
104         // Get a handle for the nested scroll WebView.
105         val nestedScrollWebView = webViewFrameLayout.findViewById<NestedScrollWebView>(R.id.nestedscroll_webview)
106
107         // Pause the current WebView.
108         nestedScrollWebView.onPause()
109
110         // Remove all the views from the frame layout.
111         webViewFrameLayout.removeAllViews()
112
113         // Destroy the current WebView.
114         nestedScrollWebView.destroy()
115
116         // Delete the page.
117         webViewFragmentsList.removeAt(pageNumber)
118
119         // Update the view pager.
120         notifyItemRemoved(pageNumber)
121
122         // 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).
123         // This will cause the calling method to reset the current WebView to the new contents of this page number.
124         return (webViewPager2.currentItem == pageNumber)
125     }
126
127     fun getPageFragment(pageNumber: Int): WebViewTabFragment {
128         // Return the page fragment.
129         return webViewFragmentsList[pageNumber]
130     }
131
132     fun getPositionForId(fragmentId: Long): Int {
133         // Initialize the position variable.
134         var position = -1
135
136         // Initialize the while counter.
137         var i = 0
138
139         // Find the current position of the WebView fragment with the given ID.
140         while ((position < 0) && (i < webViewFragmentsList.size)) {
141             // Check to see if the tab ID of this WebView matches the page ID.
142             if (webViewFragmentsList[i].fragmentId == fragmentId) {
143                 // Store the position if they are a match.
144                 position = i
145             }
146
147             // Increment the counter.
148             i++
149         }
150
151         // Set the position to be the last tab if it is not found.
152         // 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.
153         // 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.
154         if (position == -1)
155             position = (webViewFragmentsList.size - 1)
156
157         // Return the position.
158         return position
159     }
160
161     fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle) {
162         // Restore the page.
163         webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState))
164
165         // Update the view pager.  The position is zero indexed.
166         notifyItemInserted(webViewFragmentsList.size - 1)
167     }
168 }