2 * Copyright © 2019-2020,2022 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.fragments
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
30 import androidx.fragment.app.Fragment
32 import com.stoutner.privacybrowser.R
33 import com.stoutner.privacybrowser.views.NestedScrollWebView
35 import java.util.Calendar
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"
44 class WebViewTabFragment : Fragment() {
45 // Define the public variables.
46 @JvmField // TODO. `@JvmField` can be removed once the entire project has been converted to Kotlin.
47 var fragmentId = Calendar.getInstance().timeInMillis
49 // The public interface is used to send information back to the parent activity.
50 interface NewTabListener {
51 @SuppressLint("ClickableViewAccessibility")
52 fun initializeWebView(nestedScrollWebView: NestedScrollWebView, pageNumber: Int, progressBar: ProgressBar, url: String, restoringState: Boolean)
55 // Declare the class variables.
56 private lateinit var newTabListener: NewTabListener
58 // Declare the class views.
59 private lateinit var nestedScrollWebView: NestedScrollWebView
62 @JvmStatic // TODO. `@JvmStatic` can be removed once the entire project has been converted to Kotlin.
63 fun createPage(pageNumber: Int, url: String?): WebViewTabFragment {
64 // Create an arguments bundle.
65 val argumentsBundle = Bundle()
67 // Store the argument in the bundle.
68 argumentsBundle.putBoolean(CREATE_NEW_PAGE, true)
69 argumentsBundle.putInt(PAGE_NUMBER, pageNumber)
70 argumentsBundle.putString(URL, url)
72 // Create a new instance of the WebView tab fragment.
73 val webViewTabFragment = WebViewTabFragment()
75 // Add the arguments bundle to the fragment.
76 webViewTabFragment.arguments = argumentsBundle
78 // Return the new fragment.
79 return webViewTabFragment
82 @JvmStatic // TODO. `@JvmStatic` can be removed once the entire project has been converted to Kotlin.
83 fun restorePage(savedState: Bundle?, savedNestedScrollWebViewState: Bundle?): WebViewTabFragment {
84 // Create an arguments bundle
85 val argumentsBundle = Bundle()
87 // Store the saved states in the arguments bundle.
88 argumentsBundle.putBundle(SAVED_STATE, savedState)
89 argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState)
91 // Create a new instance of the WebView tab fragment.
92 val webViewTabFragment = WebViewTabFragment()
94 // Add the arguments bundle to the fragment.
95 webViewTabFragment.arguments = argumentsBundle
97 // Return the new fragment.
98 return webViewTabFragment
102 override fun onAttach(context: Context) {
103 // Run the default commands.
104 super.onAttach(context)
106 // Get a handle for the new tab listener from the launching context.
107 newTabListener = context as NewTabListener
110 override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
111 // Check to see if the fragment is being restarted.
112 return if (savedInstanceState == null) { // The fragment is not being restarted. Load and configure a new fragment.
113 // Check to see if a new page is being created.
114 if (requireArguments().getBoolean(CREATE_NEW_PAGE)) { // A new page is being created.
115 // Get the variables from the arguments
116 val pageNumber = requireArguments().getInt(PAGE_NUMBER)
117 val url = requireArguments().getString(URL)!!
119 // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
120 // The fragment will take care of attaching the root automatically.
121 val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false)
123 // Get handles for the views.
124 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
125 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
127 // Store the WebView fragment ID in the nested scroll WebView.
128 nestedScrollWebView.webViewFragmentId = fragmentId
130 // Request the main activity initialize the WebView.
131 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false)
133 // Return the new page view.
135 } else { // A page is being restored.
136 // Get the saved states from the arguments.
137 val savedState = requireArguments().getBundle(SAVED_STATE)
138 val savedNestedScrollWebViewState = requireArguments().getBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE)!!
140 // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
141 // The fragment will take care of attaching the root automatically.
142 val newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false)
144 // Get handles for the views.
145 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
146 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
148 // Store the WebView fragment ID in the nested scroll WebView.
149 nestedScrollWebView.webViewFragmentId = fragmentId
151 // Restore the nested scroll WebView state.
152 nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState)
154 // Restore the WebView state.
155 nestedScrollWebView.restoreState(savedState!!)
157 // Initialize the WebView.
158 newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, "", true)
160 // Return the new page view.
163 } else { // The fragment is being restarted.
164 // 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.