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;
28 import androidx.annotation.NonNull;
29 import androidx.fragment.app.Fragment;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.views.NestedScrollWebView;
34 public class WebViewTabFragment extends Fragment {
35 // The public interface is used to send information back to the parent activity.
36 public interface NewTabListener {
37 void initializeWebView(NestedScrollWebView nestedScrollWebView, int tabNumber);
40 // The new tab listener is used in `onAttach()` and `onCreateView()`.
41 private NewTabListener newTabListener;
44 public void onAttach(Context context) {
45 // Run the default commands.
46 super.onAttach(context);
48 // Get a handle for the new tab listener from the launching context.
49 newTabListener = (NewTabListener) context;
52 public static WebViewTabFragment createTab(int tabNumber) {
54 Bundle bundle = new Bundle();
56 // Store the tab number in the bundle.
57 bundle.putInt("tab_number", tabNumber);
59 // Create a new instance of the WebView tab fragment.
60 WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
62 // Add the bundle to the fragment.
63 webViewTabFragment.setArguments(bundle);
65 // Return the new fragment.
66 return webViewTabFragment;
70 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
72 Bundle arguments = getArguments();
74 // Remove the incorrect lint warning that the arguments might be null.
75 assert arguments != null;
77 // Get the variables from the arguments
78 int tabNumber = arguments.getInt("tab_number");
80 // 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.
81 View tabView = layoutInflater.inflate(R.layout.nestedscroll_webview, container, false);
83 // Get a handle for the nested scroll WebView.
84 NestedScrollWebView nestedScrollWebView = tabView.findViewById(R.id.nestedscroll_webview);
86 // Request the main activity initialize the WebView.
87 newTabListener.initializeWebView(nestedScrollWebView, tabNumber);
89 // Return the tab view.