]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java
1acf3b5412f216f728324c1cd3b33f3dce1c23f6
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / WebViewTabFragment.java
1 /*
2  * Copyright © 2019-2020 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser 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 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.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.fragments;
21
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;
28
29 import androidx.annotation.NonNull;
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 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();
40
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);
44     }
45
46     // The new tab listener is used in `onAttach()` and `onCreateView()`.
47     private NewTabListener newTabListener;
48
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";
55
56     // Define the class views.
57     NestedScrollWebView nestedScrollWebView;
58
59     @Override
60     public void onAttach(@NonNull Context context) {
61         // Run the default commands.
62         super.onAttach(context);
63
64         // Get a handle for the new tab listener from the launching context.
65         newTabListener = (NewTabListener) context;
66     }
67
68     public static WebViewTabFragment createPage(int pageNumber, String url) {
69         // Create an arguments bundle.
70         Bundle argumentsBundle = new Bundle();
71
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);
76
77         // Create a new instance of the WebView tab fragment.
78         WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
79
80         // Add the arguments bundle to the fragment.
81         webViewTabFragment.setArguments(argumentsBundle);
82
83         // Return the new fragment.
84         return webViewTabFragment;
85     }
86
87     public static WebViewTabFragment restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) {
88         // Create an arguments bundle
89         Bundle argumentsBundle = new Bundle();
90
91         // Store the saved states in the arguments bundle.
92         argumentsBundle.putBundle(SAVED_STATE, savedState);
93         argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState);
94
95         // Create a new instance of the WebView tab fragment.
96         WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
97
98         // Add the arguments bundle to the fragment.
99         webViewTabFragment.setArguments(argumentsBundle);
100
101         // Return the new fragment.
102         return webViewTabFragment;
103     }
104
105     @Override
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();
111
112             // Remove the incorrect lint warning that the arguments might be null.
113             assert arguments != null;
114
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);
120
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);
124
125                 // Get handles for the views.
126                 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
127                 ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
128
129                 // Store the WebView fragment ID in the nested scroll WebView.
130                 nestedScrollWebView.setWebViewFragmentId(fragmentId);
131
132                 // Request the main activity initialize the WebView.
133                 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false);
134
135                 // Return the new page view.
136                 return newPageView;
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);
141
142                 // Remove the incorrect lint warning below that the saved nested scroll WebView state might be null.
143                 assert savedNestedScrollWebViewState != null;
144
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);
148
149                 // Get handles for the views.
150                 nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
151                 ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
152
153                 // Store the WebView fragment ID in the nested scroll WebView.
154                 nestedScrollWebView.setWebViewFragmentId(fragmentId);
155
156                 // Restore the nested scroll WebView state.
157                 nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState);
158
159                 // Restore the WebView state.
160                 nestedScrollWebView.restoreState(savedState);
161
162                 // Initialize the WebView.
163                 newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, null, true);
164
165                 // Return the new page view.
166                 return newPageView;
167             }
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.
170             return null;
171         }
172     }
173 }