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 import java.util.Calendar;
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();
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, String url);
46 // The new tab listener is used in `onAttach()` and `onCreateView()`.
47 private NewTabListener newTabListener;
50 public void onAttach(@NonNull Context context) {
51 // Run the default commands.
52 super.onAttach(context);
54 // Get a handle for the new tab listener from the launching context.
55 newTabListener = (NewTabListener) context;
58 public static WebViewTabFragment createPage(int pageNumber, String url) {
60 Bundle bundle = new Bundle();
62 // Store the page number and URL in the bundle.
63 bundle.putInt("page_number", pageNumber);
64 bundle.putString("url", url);
66 // Create a new instance of the WebView tab fragment.
67 WebViewTabFragment webViewTabFragment = new WebViewTabFragment();
69 // Add the bundle to the fragment.
70 webViewTabFragment.setArguments(bundle);
72 // Return the new fragment.
73 return webViewTabFragment;
77 public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
79 Bundle arguments = getArguments();
81 // Remove the incorrect lint warning that the arguments might be null.
82 assert arguments != null;
84 // Get the variables from the arguments
85 int pageNumber = arguments.getInt("page_number");
86 String url = arguments.getString("url");
88 // 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.
89 View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false);
91 // Get handles for the views.
92 NestedScrollWebView nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview);
93 ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar);
95 // Store the WebView fragment ID in the nested scroll WebView.
96 nestedScrollWebView.setWebViewFragmentId(fragmentId);
98 // Request the main activity initialize the WebView.
99 newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url);
101 // Return the new page view.