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