]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewStateAdapter.kt
First wrong button text in View Headers in night theme. 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, private val bottomAppBar: Boolean) : 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(pagePosition: Int, url: String) {
86         // Add a new page.
87         webViewFragmentsList.add(pagePosition, WebViewTabFragment.createPage(pagePosition, url, bottomAppBar))
88
89         // Update the view pager.
90         notifyItemInserted(pagePosition)
91     }
92
93     fun deletePage(pageNumber: Int, webViewPager2: ViewPager2): Boolean {
94         // Get the WebView tab fragment.
95         val webViewTabFragment = webViewFragmentsList[pageNumber]
96
97         // Get the WebView frame layout.
98         val webViewFrameLayout = (webViewTabFragment.view as FrameLayout)
99
100         // Get a handle for the nested scroll WebView.
101         val nestedScrollWebView = webViewFrameLayout.findViewById<NestedScrollWebView>(R.id.nestedscroll_webview)
102
103         // Pause the current WebView.
104         nestedScrollWebView.onPause()
105
106         // Remove all the views from the frame layout.
107         webViewFrameLayout.removeAllViews()
108
109         // Destroy the current WebView.
110         nestedScrollWebView.destroy()
111
112         // Delete the page.
113         webViewFragmentsList.removeAt(pageNumber)
114
115         // Update the view pager.
116         notifyItemRemoved(pageNumber)
117
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)
121     }
122
123     fun getPageFragment(pageNumber: Int): WebViewTabFragment {
124         // Return the page fragment.
125         return webViewFragmentsList[pageNumber]
126     }
127
128     fun getPositionForId(fragmentId: Long): Int {
129         // Initialize the position variable.
130         var position = -1
131
132         // Initialize the while counter.
133         var i = 0
134
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.
140                 position = i
141             }
142
143             // Increment the counter.
144             i++
145         }
146
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.
150         if (position == -1)
151             position = (webViewFragmentsList.size - 1)
152
153         // Return the position.
154         return position
155     }
156
157     fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle) {
158         // Restore the page.
159         webViewFragmentsList.add(WebViewTabFragment.restorePage(savedState, savedNestedScrollWebViewState, bottomAppBar))
160
161         // Update the view pager.  The position is zero indexed.
162         notifyItemInserted(webViewFragmentsList.size - 1)
163     }
164 }