X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Ffragments%2FWebViewTabFragment.java;h=1acf3b5412f216f728324c1cd3b33f3dce1c23f6;hp=8ad76d7cbed9eb854614efd7775df19c9adeda17;hb=b82022327701273b1b56419e8d6042895c0bc7b9;hpb=84989e138cb593d5a2f70be848db4889aaa8da88 diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java index 8ad76d7c..1acf3b54 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/WebViewTabFragment.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Soren Stoutner . + * Copyright © 2019-2020 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -24,6 +24,7 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ProgressBar; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; @@ -31,17 +32,32 @@ import androidx.fragment.app.Fragment; import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.views.NestedScrollWebView; +import java.util.Calendar; + public class WebViewTabFragment extends Fragment { + // Set a unique ID for this tab based on the time it was created. + public long fragmentId = Calendar.getInstance().getTimeInMillis(); + // The public interface is used to send information back to the parent activity. public interface NewTabListener { - void initializeWebView(NestedScrollWebView nestedScrollWebView, int tabNumber); + void initializeWebView(NestedScrollWebView nestedScrollWebView, int pageNumber, ProgressBar progressBar, String url, Boolean restoringState); } // The new tab listener is used in `onAttach()` and `onCreateView()`. private NewTabListener newTabListener; + // Define the bundle constants. + private final static String CREATE_NEW_PAGE = "create_new_page"; + private final static String PAGE_NUMBER = "page_number"; + private final static String URL = "url"; + private final static String SAVED_STATE = "saved_state"; + private final static String SAVED_NESTED_SCROLL_WEBVIEW_STATE = "saved_nested_scroll_webview_state"; + + // Define the class views. + NestedScrollWebView nestedScrollWebView; + @Override - public void onAttach(Context context) { + public void onAttach(@NonNull Context context) { // Run the default commands. super.onAttach(context); @@ -49,44 +65,109 @@ public class WebViewTabFragment extends Fragment { newTabListener = (NewTabListener) context; } - public static WebViewTabFragment createTab(int tabNumber) { - // Create a bundle. - Bundle bundle = new Bundle(); + public static WebViewTabFragment createPage(int pageNumber, String url) { + // Create an arguments bundle. + Bundle argumentsBundle = new Bundle(); - // Store the tab number in the bundle. - bundle.putInt("tab_number", tabNumber); + // Store the argument in the bundle. + argumentsBundle.putBoolean(CREATE_NEW_PAGE, true); + argumentsBundle.putInt(PAGE_NUMBER, pageNumber); + argumentsBundle.putString(URL, url); // Create a new instance of the WebView tab fragment. WebViewTabFragment webViewTabFragment = new WebViewTabFragment(); - // Add the bundle to the fragment. - webViewTabFragment.setArguments(bundle); + // Add the arguments bundle to the fragment. + webViewTabFragment.setArguments(argumentsBundle); // Return the new fragment. return webViewTabFragment; } - @Override - public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) { - // Get the arguments. - Bundle arguments = getArguments(); - - // Remove the incorrect lint warning that the arguments might be null. - assert arguments != null; + public static WebViewTabFragment restorePage(Bundle savedState, Bundle savedNestedScrollWebViewState) { + // Create an arguments bundle + Bundle argumentsBundle = new Bundle(); - // Get the variables from the arguments - int tabNumber = arguments.getInt("tab_number"); + // Store the saved states in the arguments bundle. + argumentsBundle.putBundle(SAVED_STATE, savedState); + argumentsBundle.putBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE, savedNestedScrollWebViewState); - // 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. - View tabView = layoutInflater.inflate(R.layout.nestedscroll_webview, container, false); + // Create a new instance of the WebView tab fragment. + WebViewTabFragment webViewTabFragment = new WebViewTabFragment(); - // Get a handle for the nested scroll WebView. - NestedScrollWebView nestedScrollWebView = tabView.findViewById(R.id.nestedscroll_webview); + // Add the arguments bundle to the fragment. + webViewTabFragment.setArguments(argumentsBundle); - // Request the main activity initialize the WebView. - newTabListener.initializeWebView(nestedScrollWebView, tabNumber); + // Return the new fragment. + return webViewTabFragment; + } - // Return the tab view. - return tabView; + @Override + public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) { + // Check to see if the fragment is being restarted. + if (savedInstanceState == null) { // The fragment is not being restarted. Load and configure a new fragment. + // Get the arguments. + Bundle arguments = getArguments(); + + // Remove the incorrect lint warning that the arguments might be null. + assert arguments != null; + + // Check to see if a new page is being created. + if (arguments.getBoolean(CREATE_NEW_PAGE)) { // A new page is being created. + // Get the variables from the arguments + int pageNumber = arguments.getInt(PAGE_NUMBER); + String url = arguments.getString(URL); + + // 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. + View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false); + + // Get handles for the views. + nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview); + ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar); + + // Store the WebView fragment ID in the nested scroll WebView. + nestedScrollWebView.setWebViewFragmentId(fragmentId); + + // Request the main activity initialize the WebView. + newTabListener.initializeWebView(nestedScrollWebView, pageNumber, progressBar, url, false); + + // Return the new page view. + return newPageView; + } else { // A page is being restored. + // Get the saved states from the arguments. + Bundle savedState = arguments.getBundle(SAVED_STATE); + Bundle savedNestedScrollWebViewState = arguments.getBundle(SAVED_NESTED_SCROLL_WEBVIEW_STATE); + + // Remove the incorrect lint warning below that the saved nested scroll WebView state might be null. + assert savedNestedScrollWebViewState != null; + + // 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. + View newPageView = layoutInflater.inflate(R.layout.webview_framelayout, container, false); + + // Get handles for the views. + nestedScrollWebView = newPageView.findViewById(R.id.nestedscroll_webview); + ProgressBar progressBar = newPageView.findViewById(R.id.progress_bar); + + // Store the WebView fragment ID in the nested scroll WebView. + nestedScrollWebView.setWebViewFragmentId(fragmentId); + + // Restore the nested scroll WebView state. + nestedScrollWebView.restoreNestedScrollWebViewState(savedNestedScrollWebViewState); + + // Restore the WebView state. + nestedScrollWebView.restoreState(savedState); + + // Initialize the WebView. + newTabListener.initializeWebView(nestedScrollWebView, 0, progressBar, null, true); + + // Return the new page view. + return newPageView; + } + } else { // The fragment is being restarted. + // Return null. Otherwise, the fragment will be inflated and initialized by the OS on a restart, discarded, and then recreated with saved settings by Privacy Browser. + return null; + } } } \ No newline at end of file