2 * Copyright © 2019-2020,2022-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.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 var fragmentId = Calendar.getInstance().timeInMillis
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, urlString: String, restoringState: Boolean)
54 // Declare the class variables.
55 private lateinit var newTabListener: NewTabListener
57 // Declare the class views.
58 private lateinit var nestedScrollWebView: NestedScrollWebView
61 fun createPage(pageNumber: Int, url: String?): WebViewTabFragment {
62 // Create an arguments bundle.
63 val argumentsBundle = Bundle()
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)
70 // Create a new instance of the WebView tab fragment.
71 val webViewTabFragment = WebViewTabFragment()
73 // Add the arguments bundle to the fragment.
74 webViewTabFragment.arguments = argumentsBundle
76 // Return the new fragment.
77 return webViewTabFragment
80 fun restorePage(savedState: Bundle, savedNestedScrollWebViewState: Bundle): WebViewTabFragment {
81 // Create an arguments bundle
82 val argumentsBundle = Bundle()
84 // Store the saved states in the arguments bundle.
85 argumentsBundle.putBundle(SAVED_STATE, savedState)
86 argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState)
88 // Create a new instance of the WebView tab fragment.
89 val webViewTabFragment = WebViewTabFragment()
91 // Add the arguments bundle to the fragment.
92 webViewTabFragment.arguments = argumentsBundle
94 // Return the new fragment.
95 return webViewTabFragment
99 override fun onAttach(context: Context) {
100 // Run the default commands.
101 super.onAttach(context)
103 // Get a handle for the new tab listener from the launching context.
104 newTabListener = context as NewTabListener
107 override fun onCreateView(layoutInflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
108 // Check to see if the fragment is being restarted without the app being killed.
109 return if (savedInstanceState == null) { // The fragment is not being restarted. It is either new or is being restored after the app was killed.
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)!!
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)
120 // Get handles for the views.
121 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
122 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
124 // Store the WebView fragment ID in the nested scroll WebView.
125 nestedScrollWebView.webViewFragmentId = fragmentId
127 // Request the main activity initialize the WebView.
128 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false)
130 // Return the new page view.
132 } else { // A page is being restored after the app was killed.
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)!!
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)
141 // Get handles for the views.
142 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview)
143 val progressBar = newPageView.findViewById<ProgressBar>(R.id.progress_bar)
145 // Store the WebView fragment ID in the nested scroll WebView.
146 nestedScrollWebView.webViewFragmentId = fragmentId
148 // Restore the nested scroll WebView state.
149 nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState)
151 // Restore the WebView state.
152 nestedScrollWebView.restoreState(savedState)
154 // Initialize the WebView.
155 newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, "", true)
157 // Return the new page view.
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.