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.content.Context;
23 import android.os.Bundle;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ProgressBar;
29 import androidx.annotation.NonNull;
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 public class WebViewTabFragment extends Fragment {
38 // Set a unique ID for this tab based on the time it was created.
39 public long fragmentId = Calendar.getInstance().getTimeInMillis();
41 // The public interface is used to send information back to the parent activity.
42 public interface NewTabListener {
43 void initializeWebView(NestedScrollWebView nestedScrollWebView, int pageNumber, ProgressBar progressBar, String url, Boolean restoringState);
46 // The new tab listener is used in `onAttach()` and `onCreateView()`.
47 private NewTabListener newTabListener;
49 // Define the bundle constants.
50 private final static String CREATE_NEW_PAGE = "create_new_page";
51 private final static String PAGE_NUMBER = "page_number";
52 private final static String URL = "url";
53 private final static String SAVED_STATE = "saved_state";
54 private final static String SAVED_NESTED_SCROLL_WEBVIEW_STATE = "saved_nested_scroll_webview_state";
56 // Define the class views.
57 NestedScrollWebView nestedScrollWebView;
60 public void onAttach(@NonNull Context context) {
61 // Run the default commands.
62 super.onAttach(context);
64 // Get a handle for the new tab listener from the launching context.
65 newTabListener = (NewTabListener) context;
68 public static WebViewTabFragment createPage(int pageNumber, String url) {
69 // Create an arguments bundle.
70 Bundle argumentsBundle = new Bundle();
72 // Store the argument in the bundle.
73 argumentsBundle.putBoolean(CREATE_NEW_PAGE, true);
74 argumentsBundle.putInt(PAGE_NUMBER, pageNumber);
75 argumentsBundle.putString(URL, url);
77 // Create a new instance of the WebView tab fragment.
78 WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
80 // Add the arguments bundle to the fragment.
81 webViewTabFragment.setArguments(argumentsBundle);
83 // Return the new fragment.
84 return webViewTabFragment;
87 public static WebViewTabFragment restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
88 // Create an arguments bundle
89 Bundle argumentsBundle = new Bundle();
91 // Store the saved states in the arguments bundle.
92 argumentsBundle.putBundle(SAVED_STATE, savedState);
93 argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState);
95 // Create a new instance of the WebView tab fragment.
96 WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
98 // Add the arguments bundle to the fragment.
99 webViewTabFragment.setArguments(argumentsBundle);
101 // Return the new fragment.
102 return webViewTabFragment;
106 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
107 // Check to see if the fragment is being restarted.
108 if (savedInstanceState == null) { // The fragment is not being restarted. Load and configure a new fragment.
109 // Get the arguments.
110 Bundle arguments = getArguments();
112 // Remove the incorrect lint warning that the arguments might be null.
113 assert arguments != null;
115 // Check to see if a new page is being created.
116 if (arguments.getBoolean(CREATE_NEW_PAGE)) { // A new page is being created.
117 // Get the variables from the arguments
118 int pageNumber = arguments.getInt(PAGE_NUMBER);
119 String url = arguments.getString(URL);
121 // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
122 // The fragment will take care of attaching the root automatically.
123 View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false);
125 // Get handles for the views.
126 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
127 ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
129 // Store the WebView fragment ID in the nested scroll WebView.
130 nestedScrollWebView.setWebViewFragmentId(fragmentId);
132 // Request the main activity initialize the WebView.
133 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false);
135 // Return the new page view.
137 } else { // A page is being restored.
138 // Get the saved states from the arguments.
139 Bundle savedState = arguments.getBundle(SAVED_STATE);
140 Bundle savedNestedScrollWebViewState = arguments.getBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE);
142 // Remove the incorrect lint warning below that the saved nested scroll WebView state might be null.
143 assert savedNestedScrollWebViewState != null;
145 // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container.
146 // The fragment will take care of attaching the root automatically.
147 View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false);
149 // Get handles for the views.
150 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
151 ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
153 // Store the WebView fragment ID in the nested scroll WebView.
154 nestedScrollWebView.setWebViewFragmentId(fragmentId);
156 // Restore the nested scroll WebView state.
157 nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState);
159 // Restore the WebView state.
160 nestedScrollWebView.restoreState(savedState);
162 // Initialize the WebView.
163 newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, null, true);
165 // Return the new page view.
168 } else { // The fragment is being restarted.
169 // 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.