From bf4c90b121fd5c1feb86133923f03add9901bf62 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sat, 17 Oct 2015 21:50:52 -0400 Subject: [PATCH] Fixed bugs in showTab, attempt to improve full-screen video handling. --- .../lightning/activity/BrowserActivity.java | 98 ++++++++++++------- .../lightning/activity/TabsManager.java | 8 +- .../lightning/controller/UIController.java | 2 + .../lightning/view/LightningChromeClient.java | 38 +------ .../lightning/view/LightningWebClient.java | 16 +-- 5 files changed, 86 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index 0c40515..97bf3e1 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -4,6 +4,7 @@ package acr.browser.lightning.activity; +import android.animation.Animator; import android.animation.ArgbEvaluator; import android.animation.LayoutTransition; import android.animation.ValueAnimator; @@ -996,13 +997,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } if (current > position) { tabsManager.deleteTab(position); - showTab(current - 1); mEventBus.post(new BrowserEvents.TabsChanged()); } else if (tabsManager.size() > position + 1) { if (current == position) { showTab(position + 1); tabsManager.deleteTab(position); - showTab(position); mEventBus.post(new BrowserEvents.TabsChanged()); } else { tabsManager.deleteTab(position); @@ -1011,7 +1010,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements if (current == position) { showTab(position - 1); tabsManager.deleteTab(position); - showTab(position - 1); mEventBus.post(new BrowserEvents.TabsChanged()); } else { tabsManager.deleteTab(position); @@ -1078,13 +1076,12 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements } @Override - public void onBackPressed() { + public synchronized void onBackPressed() { final LightningView currentTab = tabsManager.getCurrentTab(); if (mDrawerLayout.isDrawerOpen(mDrawerLeft)) { mDrawerLayout.closeDrawer(mDrawerLeft); } else if (mDrawerLayout.isDrawerOpen(mDrawerRight)) { - mEventBus - .post(new BrowserEvents.UserPressedBack()); + mEventBus.post(new BrowserEvents.UserPressedBack()); } else { if (currentTab != null) { Log.d(Constants.TAG, "onBackPressed"); @@ -1097,7 +1094,11 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements currentTab.goBack(); } } else { - deleteTab(tabsManager.positionOf(currentTab)); + if (mCustomView != null || mCustomViewCallback != null) { + onHideCustomView(); + } else { + deleteTab(tabsManager.positionOf(currentTab)); + } } } else { Log.e(Constants.TAG, "This shouldn't happen ever"); @@ -1511,17 +1512,26 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); - this.startActivityForResult(chooserIntent, 1); + startActivityForResult(chooserIntent, 1); } @Override - public void onShowCustomView(View view, CustomViewCallback callback) { + public synchronized void onShowCustomView(View view, CustomViewCallback callback) { + int requestedOrientation = mOriginalOrientation = getRequestedOrientation(); + onShowCustomView(view, callback, requestedOrientation); + } + + @Override + public synchronized void onShowCustomView(final View view, CustomViewCallback callback, int requestedOrientation) { final LightningView currentTab = tabsManager.getCurrentTab(); - if (view == null) { - return; - } - if (mCustomView != null && callback != null) { - callback.onCustomViewHidden(); + if (view == null || mCustomView != null) { + if (callback != null) { + try { + callback.onCustomViewHidden(); + } catch (Exception e) { + Log.e(Constants.TAG, "Error hiding custom view", e); + } + } return; } try { @@ -1530,30 +1540,46 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements Log.e(Constants.TAG, "WebView is not allowed to keep the screen on"); } mOriginalOrientation = getRequestedOrientation(); - FrameLayout decor = (FrameLayout) getWindow().getDecorView(); + mCustomViewCallback = callback; + mCustomView = view; + + setRequestedOrientation(requestedOrientation); + final FrameLayout decorView = (FrameLayout) getWindow().getDecorView(); + mFullscreenContainer = new FrameLayout(this); mFullscreenContainer.setBackgroundColor(ContextCompat.getColor(this, android.R.color.black)); - mCustomView = view; - mFullscreenContainer.addView(mCustomView, COVER_SCREEN_PARAMS); - decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS); - setFullscreen(true, true); - if (currentTab != null) { - currentTab.setVisibility(View.GONE); - } if (view instanceof FrameLayout) { if (((FrameLayout) view).getFocusedChild() instanceof VideoView) { mVideoView = (VideoView) ((FrameLayout) view).getFocusedChild(); mVideoView.setOnErrorListener(new VideoCompletionListener()); mVideoView.setOnCompletionListener(new VideoCompletionListener()); } + } else if (view instanceof VideoView) { + mVideoView = (VideoView) view; + mVideoView.setOnErrorListener(new VideoCompletionListener()); + mVideoView.setOnCompletionListener(new VideoCompletionListener()); + } + decorView.addView(mFullscreenContainer, COVER_SCREEN_PARAMS); + mFullscreenContainer.addView(mCustomView, COVER_SCREEN_PARAMS); + decorView.requestLayout(); + setFullscreen(true, true); + if (currentTab != null) { + currentTab.setVisibility(View.INVISIBLE); } - mCustomViewCallback = callback; } @Override public void onHideCustomView() { final LightningView currentTab = tabsManager.getCurrentTab(); if (mCustomView == null || mCustomViewCallback == null || currentTab == null) { + if (mCustomViewCallback != null) { + try { + mCustomViewCallback.onCustomViewHidden(); + } catch (Exception e) { + Log.e(Constants.TAG, "Error hiding custom view", e); + } + mCustomViewCallback = null; + } return; } Log.d(Constants.TAG, "onHideCustomView"); @@ -1564,25 +1590,31 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements Log.e(Constants.TAG, "WebView is not allowed to keep the screen on"); } setFullscreen(mPreferences.getHideStatusBarEnabled(), false); - FrameLayout decor = (FrameLayout) getWindow().getDecorView(); - if (decor != null) { - decor.removeView(mFullscreenContainer); - } - - if (API < Build.VERSION_CODES.KITKAT) { - try { - mCustomViewCallback.onCustomViewHidden(); - } catch (Throwable ignored) { - + if (mFullscreenContainer != null) { + ViewGroup parent = (ViewGroup) mFullscreenContainer.getParent(); + if (parent != null) { + parent.removeView(mFullscreenContainer); } + mFullscreenContainer.removeAllViews(); } + mFullscreenContainer = null; mCustomView = null; if (mVideoView != null) { + Log.d(Constants.TAG, "VideoView is being stopped"); + mVideoView.stopPlayback(); mVideoView.setOnErrorListener(null); mVideoView.setOnCompletionListener(null); mVideoView = null; } + if (mCustomViewCallback != null) { + try { + mCustomViewCallback.onCustomViewHidden(); + } catch (Exception e) { + Log.e(Constants.TAG, "Error hiding custom view", e); + } + } + mCustomViewCallback = null; setRequestedOrientation(mOriginalOrientation); } diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index 859bbb3..a177a5c 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -165,6 +165,9 @@ public class TabsManager { return null; } final LightningView tab = mWebViewList.remove(position); + if (mCurrentTab == tab) { + mCurrentTab = null; + } tab.onDestroy(); return tab; } @@ -227,8 +230,11 @@ public class TabsManager { return null; } else { final LightningView tab = mWebViewList.get(position); - mCurrentTab = tab; + if (tab != null) { + mCurrentTab = tab; + } return tab; } } + } diff --git a/app/src/main/java/acr/browser/lightning/controller/UIController.java b/app/src/main/java/acr/browser/lightning/controller/UIController.java index 1e29a6c..2dbafd2 100644 --- a/app/src/main/java/acr/browser/lightning/controller/UIController.java +++ b/app/src/main/java/acr/browser/lightning/controller/UIController.java @@ -35,6 +35,8 @@ public interface UIController { void onShowCustomView(View view, CustomViewCallback callback); + void onShowCustomView(View view, CustomViewCallback callback, int requestedOrienation); + void onHideCustomView(); void onCreateWindow(Message resultMsg); diff --git a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java index 990b0e5..35d234b 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningChromeClient.java @@ -193,6 +193,9 @@ class LightningChromeClient extends WebChromeClient { */ @Override public Bitmap getDefaultVideoPoster() { + if (mActivity == null) { + return null; + } final Resources resources = mActivity.getResources(); return BitmapFactory.decodeResource(resources, android.R.drawable.spinner_background); } @@ -213,50 +216,17 @@ class LightningChromeClient extends WebChromeClient { @Override public void onHideCustomView() { mUIController.onHideCustomView(); - super.onHideCustomView(); } @Override public void onShowCustomView(View view, CustomViewCallback callback) { - // While these lines might look like they work, in practice, - // Full-screen videos won't work correctly. I may test this out some - // more - // if (view instanceof FrameLayout) { - // FrameLayout frame = (FrameLayout) view; - // if (frame.getFocusedChild() instanceof VideoView) { - // VideoView video = (VideoView) frame.getFocusedChild(); - // video.stopPlayback(); - // frame.removeView(video); - // video.setVisibility(View.GONE); - // } - // } else { mUIController.onShowCustomView(view, callback); - - // } - - super.onShowCustomView(view, callback); } @SuppressWarnings("deprecation") @Override public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { - // While these lines might look like they work, in practice, - // Full-screen videos won't work correctly. I may test this out some - // more - // if (view instanceof FrameLayout) { - // FrameLayout frame = (FrameLayout) view; - // if (frame.getFocusedChild() instanceof VideoView) { - // VideoView video = (VideoView) frame.getFocusedChild(); - // video.stopPlayback(); - // frame.removeView(video); - // video.setVisibility(View.GONE); - // } - // } else { - mUIController.onShowCustomView(view, callback); - - // } - - super.onShowCustomView(view, requestedOrientation, callback); + mUIController.onShowCustomView(view, callback, requestedOrientation); } } diff --git a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java index 1bc5df5..fec911b 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningWebClient.java @@ -1,6 +1,6 @@ package acr.browser.lightning.view; -import android.annotation.SuppressLint; +import android.annotation.TargetApi; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.DialogInterface; @@ -64,18 +64,18 @@ class LightningWebClient extends WebViewClient { mIntentUtils = new IntentUtils(activity); } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - if (mAdBlock.isAd(request.getUrl().toString())) { - ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); - return new WebResourceResponse("text/plain", "utf-8", EMPTY); - } + if (mAdBlock.isAd(request.getUrl().toString())) { + ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); + return new WebResourceResponse("text/plain", "utf-8", EMPTY); } return super.shouldInterceptRequest(view, request); } @SuppressWarnings("deprecation") + @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (mAdBlock.isAd(url)) { @@ -85,7 +85,7 @@ class LightningWebClient extends WebViewClient { return null; } - @SuppressLint("NewApi") + @TargetApi(Build.VERSION_CODES.KITKAT) @Override public void onPageFinished(WebView view, String url) { if (view.isShown()) { @@ -163,7 +163,7 @@ class LightningWebClient extends WebViewClient { private boolean mIsRunning = false; private float mZoomScale = 0.0f; - @SuppressLint("NewApi") + @TargetApi(Build.VERSION_CODES.KITKAT) @Override public void onScaleChanged(final WebView view, final float oldScale, final float newScale) { if (view.isShown() && mLightningView.mPreferences.getTextReflowEnabled() &&