2 * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
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 public class WebViewTabFragment extends Fragment {
36 // The public interface is used to send information back to the parent activity.
37 public interface NewTabListener {
38 void initializeWebView(int tabNumber, ProgressBar progressBar, NestedScrollWebView nestedScrollWebView);
41 // The new tab listener is used in `onAttach()` and `onCreateView()`.
42 private NewTabListener newTabListener;
45 public void onAttach(Context context) {
46 // Run the default commands.
47 super.onAttach(context);
49 // Get a handle for the new tab listener from the launching context.
50 newTabListener = (NewTabListener) context;
53 public static WebViewTabFragment createTab(int tabNumber) {
55 Bundle bundle = new Bundle();
57 // Store the tab number in the bundle.
58 bundle.putInt("tab_number", tabNumber);
60 // Create a new instance of the WebView tab fragment.
61 WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
63 // Add the bundle to the fragment.
64 webViewTabFragment.setArguments(bundle);
66 // Return the new fragment.
67 return webViewTabFragment;
71 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
73 Bundle arguments = getArguments();
75 // Remove the incorrect lint warning that the arguments might be null.
76 assert arguments != null;
78 // Get the variables from the arguments
79 int tabNumber = arguments.getInt("tab_number");
81 // Inflate the tab's WebView. Setting false at the end of inflater.inflate does not attach the inflated layout as a child of container. The fragment will take care of attaching the root automatically.
82 View tabView = layoutInflater.inflate(R.layout.webview_framelayout, container, false);
84 // Get a handle for the nested scroll WebView.
85 NestedScrollWebView nestedScrollWebView = tabView.findViewById(R.id.nestedscroll_webview);
86 ProgressBar progressBar = tabView.findViewById(R.id.progress_bar);
88 // Request the main activity initialize the WebView.
89 newTabListener.initializeWebView(tabNumber, progressBar, nestedScrollWebView);
91 // Return the tab view.