]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java
Make pinned settings tab aware.
[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 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);
44     }
45
46     // The new tab listener is used in `onAttach()` and `onCreateView()`.
47     private NewTabListener newTabListener;
48
49     @Override
50     public void onAttach(Context context) {
51         // Run the default commands.
52         super.onAttach(context);
53
54         // Get a handle for the new tab listener from the launching context.
55         newTabListener = (NewTabListener) context;
56     }
57
58     public static WebViewTabFragment createPage(int pageNumber) {
59         // Create a bundle.
60         Bundle bundle = new Bundle();
61
62         // Store the page number in the bundle.
63         bundle.putInt("page_number", pageNumber);
64
65         // Create a new instance of the WebView tab fragment.
66         WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
67
68         // Add the bundle to the fragment.
69         webViewTabFragment.setArguments(bundle);
70
71         // Return the new fragment.
72         return webViewTabFragment;
73     }
74
75     @Override
76     public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
77         // Get the arguments.
78         Bundle arguments = getArguments();
79
80         // Remove the incorrect lint warning that the arguments might be null.
81         assert arguments != null;
82
83         // Get the variables from the arguments
84         int pageNumber = arguments.getInt("page_number");
85
86         // 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.
87         View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false);
88
89         // Get handles for the views.
90         NestedScrollWebView nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
91         ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
92
93         // Store the WebView fragment ID in the nested scroll WebView.
94         nestedScrollWebView.setWebViewFragmentId(fragmentId);
95
96         // Request the main activity initialize the WebView.
97         newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar);
98
99         // Return the new page view.
100         return newPageView;
101     }
102 }