]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.kt
154ea2dd20849b76d730407fad3ac5929ae8bc2f
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / WebViewTabFragment.kt
1 /*
2  * Copyright © 2019-2020,2022-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.fragments
21
22 import android.annotation.SuppressLint
23 import android.content.Context
24 import android.os.Bundle
25 import android.view.LayoutInflater
26 import android.view.View
27 import android.view.ViewGroup
28 import android.widget.ProgressBar
29
30 import androidx.fragment.app.Fragment
31
32 import com.stoutner.privacybrowser.R
33 import com.stoutner.privacybrowser.views.NestedScrollWebView
34
35 import java.util.Calendar
36
37 // Define the class constants.
38 private const val CREATE_NEW_PAGE = "create_new_page"
39 private const val PAGE_NUMBER = "page_number"
40 private const val URL = "url"
41 private const val SAVED_STATE = "saved_state"
42 private const val SAVED_NESTED_SCROLL_WEBVIEW_STATE = "saved_nested_scroll_webview_state"
43
44 class WebViewTabFragment : Fragment() {
45     // Define the public variables.
46     var fragmentId = Calendar.getInstance().timeInMillis
47
48     // The public interface is used to send information back to the parent activity.
49     interface NewTabListener {
50         @SuppressLint("ClickableViewAccessibility")
51         fun initializeWebView(nestedScrollWebView: NestedScrollWebView, pageNumber: Int, progressBar: ProgressBar, url: String, restoringState: Boolean)
52     }
53
54     // Declare the class variables.
55     private lateinit var newTabListener: NewTabListener
56
57     // Declare the class views.
58     private lateinit var nestedScrollWebView: NestedScrollWebView
59
60     companion object {
61         fun createPage(pageNumber: Int, url: String?): WebViewTabFragment {
62             // Create an arguments bundle.
63             val argumentsBundle = Bundle()
64
65             // Store the argument in the bundle.
66             argumentsBundle.putBoolean(CREATE_NEW_PAGE, true)
67             argumentsBundle.putInt(PAGE_NUMBER, pageNumber)
68             argumentsBundle.putString(URL, url)
69
70             // Create a new instance of the WebView tab fragment.
71             val webViewTabFragment = WebViewTabFragment()
72
73             // Add the arguments bundle to the fragment.
74             webViewTabFragment.arguments = argumentsBundle
75
76             // Return the new fragment.
77             return webViewTabFragment
78         }
79
80         fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle): WebViewTabFragment {
81             // Create an arguments bundle
82             val argumentsBundle = Bundle()
83
84             // Store the saved states in the arguments bundle.
85             argumentsBundle.putBundle(SAVED_STATE, savedState)
86             argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState)
87
88             // Create a new instance of the WebView tab fragment.
89             val webViewTabFragment = WebViewTabFragment()
90
91             // Add the arguments bundle to the fragment.
92             webViewTabFragment.arguments = argumentsBundle
93
94             // Return the new fragment.
95             return webViewTabFragment
96         }
97     }
98
99     override fun onAttach(context: Context) {
100         // Run the default commands.
101         super.onAttach(context)
102
103         // Get a handle for the new tab listener from the launching context.
104         newTabListener = context as NewTabListener
105     }
106
107     override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
108         // Check to see if the fragment is being restarted.
109         return if (savedInstanceState == null) {  // The fragment is not being restarted.  Load and configure a new fragment.
110             // Check to see if a new page is being created.
111             if (requireArguments().getBoolean(CREATE_NEW_PAGE)) {  // A new page is being created.
112                 // Get the variables from the arguments
113                 val pageNumber = requireArguments().getInt(PAGE_NUMBER)
114                 val url = requireArguments().getString(URL)!!
115
116                 // Inflate the tab's WebView.  Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
117                 // The fragment will take care of attaching the root automatically.
118                 val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false)
119
120                 // Get handles for the views.
121                 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
122                 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
123
124                 // Store the WebView fragment ID in the nested scroll WebView.
125                 nestedScrollWebView.webViewFragmentId = fragmentId
126
127                 // Request the main activity initialize the WebView.
128                 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false)
129
130                 // Return the new page view.
131                 newPageView
132             } else {  // A page is being restored.
133                 // Get the saved states from the arguments.
134                 val savedState = requireArguments().getBundle(SAVED_STATE)
135                 val savedNestedScrollWebViewState = requireArguments().getBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE)!!
136
137                 // Inflate the tab's WebView.  Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
138                 // The fragment will take care of attaching the root automatically.
139                 val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false)
140
141                 // Get handles for the views.
142                 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
143                 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
144
145                 // Store the WebView fragment ID in the nested scroll WebView.
146                 nestedScrollWebView.webViewFragmentId = fragmentId
147
148                 // Restore the nested scroll WebView state.
149                 nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState)
150
151                 // Restore the WebView state.
152                 nestedScrollWebView.restoreState(savedState!!)
153
154                 // Initialize the WebView.
155                 newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, "", true)
156
157                 // Return the new page view.
158                 newPageView
159             }
160         } else {  // The fragment is being restarted.
161             // Return null.  Otherwise, the fragment will be inflated and initialized by the OS on a restart, discarded, and then recreated with saved settings by Privacy Browser.
162             null
163         }
164     }
165 }