]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java
8ad76d7cbed9eb854614efd7775df19c9adeda17
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / WebViewTabFragment.java
1 /*
2  * Copyright © 2019 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
28 import androidx.annotation.NonNull;
29 import androidx.fragment.app.Fragment;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.views.NestedScrollWebView;
33
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);
38     }
39
40     // The new tab listener is used in `onAttach()` and `onCreateView()`.
41     private NewTabListener newTabListener;
42
43     @Override
44     public void onAttach(Context context) {
45         // Run the default commands.
46         super.onAttach(context);
47
48         // Get a handle for the new tab listener from the launching context.
49         newTabListener = (NewTabListener) context;
50     }
51
52     public static WebViewTabFragment createTab(int tabNumber) {
53         // Create a bundle.
54         Bundle bundle = new Bundle();
55
56         // Store the tab number in the bundle.
57         bundle.putInt("tab_number", tabNumber);
58
59         // Create a new instance of the WebView tab fragment.
60         WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
61
62         // Add the bundle to the fragment.
63         webViewTabFragment.setArguments(bundle);
64
65         // Return the new fragment.
66         return webViewTabFragment;
67     }
68
69     @Override
70     public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
71         // Get the arguments.
72         Bundle arguments = getArguments();
73
74         // Remove the incorrect lint warning that the arguments might be null.
75         assert arguments != null;
76
77         // Get the variables from the arguments
78         int tabNumber = arguments.getInt("tab_number");
79
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);
82
83         // Get a handle for the nested scroll WebView.
84         NestedScrollWebView nestedScrollWebView = tabView.findViewById(R.id.nestedscroll_webview);
85
86         // Request the main activity initialize the WebView.
87         newTabListener.initializeWebView(nestedScrollWebView, tabNumber);
88
89         // Return the tab view.
90         return tabView;
91     }
92 }