From: Soren Stoutner Date: Mon, 25 Jan 2021 21:16:46 +0000 (-0700) Subject: Fix audio and video sometimes playing after a tab is closed. https://redmine.stoutne... X-Git-Tag: v3.7~13 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=d83bd21f11673e7a2332805918885d431ce1938f;p=PrivacyBrowserAndroid.git Fix audio and video sometimes playing after a tab is closed. https://redmine.stoutner.com/issues/638 --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 326a8570..4180e28c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -4854,16 +4854,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } private void closeCurrentTab() { - // Pause the current WebView. This prevents buffered audio from playing after the tab is closed. - currentWebView.onPause(); - // Get the current tab number. int currentTabNumber = tabLayout.getSelectedTabPosition(); // Delete the current tab. tabLayout.removeTabAt(currentTabNumber); - // Delete the current page. If the selected page number did not change during the delete, it will return true, meaning that the current WebView must be reset. + // Delete the current page. If the selected page number did not change during the delete (because the newly selected tab has has same number as the previously deleted tab), it will return true, + // meaning that the current WebView must be reset. Otherwise it will happen automatically as the selected tab number changes. if (webViewPagerAdapter.deletePage(currentTabNumber, webViewPager)) { setCurrentWebView(currentTabNumber); } @@ -5022,13 +5020,13 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get the WebView tab fragment. WebViewTabFragment webViewTabFragment = webViewPagerAdapter.getPageFragment(i); - // Get the fragment view. - View fragmentView = webViewTabFragment.getView(); + // Get the WebView fragment view. + View webViewFragmentView = webViewTabFragment.getView(); // Only clear the cache if the WebView exists. - if (fragmentView != null) { + if (webViewFragmentView != null) { // Get the nested scroll WebView from the tab fragment. - NestedScrollWebView nestedScrollWebView = fragmentView.findViewById(R.id.nestedscroll_webview); + NestedScrollWebView nestedScrollWebView = webViewFragmentView.findViewById(R.id.nestedscroll_webview); // Clear the cache for this WebView. nestedScrollWebView.clearCache(true); diff --git a/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java index 5fb604ae..15cfc2cc 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java +++ b/app/src/main/java/com/stoutner/privacybrowser/adapters/WebViewPagerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019-2020 Soren Stoutner . + * Copyright © 2019-2021 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -20,6 +20,7 @@ package com.stoutner.privacybrowser.adapters; import android.os.Bundle; +import android.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; @@ -27,7 +28,9 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager; +import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.fragments.WebViewTabFragment; +import com.stoutner.privacybrowser.views.NestedScrollWebView; import java.util.LinkedList; @@ -125,13 +128,32 @@ public class WebViewPagerAdapter extends FragmentPagerAdapter { } public boolean deletePage(int pageNumber, ViewPager webViewPager) { + // Get the WebView tab fragment. + WebViewTabFragment webViewTabFragment = webViewFragmentsList.get(pageNumber); + + // Get the WebView frame layout. + FrameLayout webViewFrameLayout = (FrameLayout) webViewTabFragment.getView(); + + // Get a handle for the nested scroll WebView. + NestedScrollWebView nestedScrollWebView = webViewFrameLayout.findViewById(R.id.nestedscroll_webview); + + // Pause the current WebView. + nestedScrollWebView.onPause(); + + // Remove all the views from the frame layout. + webViewFrameLayout.removeAllViews(); + + // Destroy the current WebView. + nestedScrollWebView.destroy(); + // Delete the page. webViewFragmentsList.remove(pageNumber); // Update the view pager. notifyDataSetChanged(); - // Return true if the selected page number did not change after the delete. This will cause the calling method to reset the current WebView to the new contents of this page number. + // Return true if the selected page number did not change after the delete (because the newly selected tab has has same number as the previously deleted tab). + // This will cause the calling method to reset the current WebView to the new contents of this page number. return (webViewPager.getCurrentItem() == pageNumber); }