From f444b5c3ae46f8790c8f2a8481fb688b00a42d3b Mon Sep 17 00:00:00 2001 From: Abodunrinwa Toki Date: Tue, 16 Jun 2015 15:47:04 +0100 Subject: Fix floating toolbar flickers. -Flicker was a side effect the delay in hiding the toolbar when there is a down press on a TextView handle. The timing clashed with the time it takes for the FloatingActionMode to release a hide lock on a changing content rect. Extra flickers also happened if the ActionMode happened to be invalidated at about the same time. Since there was no longer a hide lock, the toolbar refreshed, getting rid of the "Select all" menu item. FIX. I believe we don't need the delayed hide on down press on a TextView handle. I've removed this. -I've also updated FloatingActionMode to ensure that hide locks overlap one another if necessary. The toolbar can only be visible if all locks are off. Bug: 21793687 Change-Id: I290ea45ab17d5862b453cb319afb4c8ffe084cd0 --- core/java/android/widget/Editor.java | 14 +--- .../android/internal/view/FloatingActionMode.java | 95 +++++++++++++--------- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java index cf6a018..f89ee91 100644 --- a/core/java/android/widget/Editor.java +++ b/core/java/android/widget/Editor.java @@ -244,15 +244,6 @@ public class Editor { final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier = new CursorAnchorInfoNotifier(); - private final Runnable mHideFloatingToolbar = new Runnable() { - @Override - public void run() { - if (mTextActionMode != null) { - mTextActionMode.hide(ActionMode.DEFAULT_HIDE_DURATION); - } - } - }; - private final Runnable mShowFloatingToolbar = new Runnable() { @Override public void run() { @@ -389,7 +380,6 @@ public class Editor { mTextView.removeCallbacks(mInsertionActionModeRunnable); } - mTextView.removeCallbacks(mHideFloatingToolbar); mTextView.removeCallbacks(mShowFloatingToolbar); destroyDisplayListsData(); @@ -1248,14 +1238,12 @@ public class Editor { private void hideFloatingToolbar() { if (mTextActionMode != null) { mTextView.removeCallbacks(mShowFloatingToolbar); - // Delay the "hide" a little bit just in case a "show" will happen almost immediately. - mTextView.postDelayed(mHideFloatingToolbar, 100); + mTextActionMode.hide(ActionMode.DEFAULT_HIDE_DURATION); } } private void showFloatingToolbar() { if (mTextActionMode != null) { - mTextView.removeCallbacks(mHideFloatingToolbar); // Delay "show" so it doesn't interfere with click confirmations // or double-clicks that could "dismiss" the floating toolbar. int delay = ViewConfiguration.getDoubleTapTimeout(); diff --git a/core/java/com/android/internal/view/FloatingActionMode.java b/core/java/com/android/internal/view/FloatingActionMode.java index 661dce1..99c1277 100644 --- a/core/java/com/android/internal/view/FloatingActionMode.java +++ b/core/java/com/android/internal/view/FloatingActionMode.java @@ -48,12 +48,14 @@ public class FloatingActionMode extends ActionMode { private final Runnable mMovingOff = new Runnable() { public void run() { mFloatingToolbarVisibilityHelper.setMoving(false); + mFloatingToolbarVisibilityHelper.updateToolbarVisibility(); } }; private final Runnable mHideOff = new Runnable() { public void run() { mFloatingToolbarVisibilityHelper.setHideRequested(false); + mFloatingToolbarVisibilityHelper.updateToolbarVisibility(); } }; @@ -87,6 +89,7 @@ public class FloatingActionMode extends ActionMode { } }); mFloatingToolbarVisibilityHelper = new FloatingToolbarVisibilityHelper(mFloatingToolbar); + mFloatingToolbarVisibilityHelper.activate(); } @Override @@ -108,8 +111,7 @@ public class FloatingActionMode extends ActionMode { public void invalidate() { checkToolbarInitialized(); mCallback.onPrepareActionMode(this, mMenu); - mFloatingToolbar.updateLayout(); - invalidateContentRect(); + invalidateContentRect(); // Will re-layout and show the toolbar if necessary. } @Override @@ -131,44 +133,43 @@ public class FloatingActionMode extends ActionMode { mContentRectOnWindow.set(mContentRect); mContentRectOnWindow.offset(mViewPosition[0], mViewPosition[1]); - // Make sure that content rect is not out of the view's visible bounds. - mContentRectOnWindow.set( - Math.max(mContentRectOnWindow.left, mViewRect.left), - Math.max(mContentRectOnWindow.top, mViewRect.top), - Math.min(mContentRectOnWindow.right, mViewRect.right), - Math.min(mContentRectOnWindow.bottom, mViewRect.bottom)); - - if (!mContentRectOnWindow.equals(mPreviousContentRectOnWindow)) { - if (!mPreviousContentRectOnWindow.isEmpty()) { - notifyContentRectMoving(); - } - mFloatingToolbar.setContentRect(mContentRectOnWindow); - mFloatingToolbar.updateLayout(); - } - mPreviousContentRectOnWindow.set(mContentRectOnWindow); if (isContentRectWithinBounds()) { mFloatingToolbarVisibilityHelper.setOutOfBounds(false); + // Make sure that content rect is not out of the view's visible bounds. + mContentRectOnWindow.set( + Math.max(mContentRectOnWindow.left, mViewRect.left), + Math.max(mContentRectOnWindow.top, mViewRect.top), + Math.min(mContentRectOnWindow.right, mViewRect.right), + Math.min(mContentRectOnWindow.bottom, mViewRect.bottom)); + + if (!mContentRectOnWindow.equals(mPreviousContentRectOnWindow)) { + // Content rect is moving. + mOriginatingView.removeCallbacks(mMovingOff); + mFloatingToolbarVisibilityHelper.setMoving(true); + mOriginatingView.postDelayed(mMovingOff, MOVING_HIDE_DELAY); + + mFloatingToolbar.setContentRect(mContentRectOnWindow); + mFloatingToolbar.updateLayout(); + } } else { mFloatingToolbarVisibilityHelper.setOutOfBounds(true); + mContentRectOnWindow.setEmpty(); } - } + mFloatingToolbarVisibilityHelper.updateToolbarVisibility(); - private boolean isContentRectWithinBounds() { - mScreenRect.set( - 0, - 0, - mContext.getResources().getDisplayMetrics().widthPixels, - mContext.getResources().getDisplayMetrics().heightPixels); - - return Rect.intersects(mContentRectOnWindow, mScreenRect) - && Rect.intersects(mContentRectOnWindow, mViewRect); + mPreviousContentRectOnWindow.set(mContentRectOnWindow); } - private void notifyContentRectMoving() { - mOriginatingView.removeCallbacks(mMovingOff); - mFloatingToolbarVisibilityHelper.setMoving(true); - mOriginatingView.postDelayed(mMovingOff, MOVING_HIDE_DELAY); + private boolean isContentRectWithinBounds() { + mScreenRect.set( + 0, + 0, + mContext.getResources().getDisplayMetrics().widthPixels, + mContext.getResources().getDisplayMetrics().heightPixels); + + return Rect.intersects(mContentRectOnWindow, mScreenRect) + && Rect.intersects(mContentRectOnWindow, mViewRect); } @Override @@ -184,6 +185,7 @@ public class FloatingActionMode extends ActionMode { mHideOff.run(); } else { mFloatingToolbarVisibilityHelper.setHideRequested(true); + mFloatingToolbarVisibilityHelper.updateToolbarVisibility(); mOriginatingView.postDelayed(mHideOff, duration); } } @@ -221,7 +223,7 @@ public class FloatingActionMode extends ActionMode { } /** - * @throws IlllegalStateException + * @throws IllegalStateException */ private void checkToolbarInitialized() { Preconditions.checkState(mFloatingToolbar != null); @@ -229,13 +231,14 @@ public class FloatingActionMode extends ActionMode { } private void reset() { + mFloatingToolbarVisibilityHelper.deactivate(); mOriginatingView.removeCallbacks(mMovingOff); mOriginatingView.removeCallbacks(mHideOff); } /** - * A helper that shows/hides the floating toolbar depending on certain states. + * A helper for showing/hiding the floating toolbar depending on certain states. */ private static final class FloatingToolbarVisibilityHelper { @@ -245,29 +248,45 @@ public class FloatingActionMode extends ActionMode { private boolean mMoving; private boolean mOutOfBounds; + private boolean mActive; + public FloatingToolbarVisibilityHelper(FloatingToolbar toolbar) { mToolbar = Preconditions.checkNotNull(toolbar); } + public void activate() { + mHideRequested = false; + mMoving = false; + mOutOfBounds = false; + + mActive = true; + } + + public void deactivate() { + mActive = false; + mToolbar.dismiss(); + } + public void setHideRequested(boolean hide) { mHideRequested = hide; - updateToolbarVisibility(); } public void setMoving(boolean moving) { mMoving = moving; - updateToolbarVisibility(); } public void setOutOfBounds(boolean outOfBounds) { mOutOfBounds = outOfBounds; - updateToolbarVisibility(); } - private void updateToolbarVisibility() { + public void updateToolbarVisibility() { + if (!mActive) { + return; + } + if (mHideRequested || mMoving || mOutOfBounds) { mToolbar.hide(); - } else if (mToolbar.isHidden()) { + } else { mToolbar.show(); } } -- cgit v1.1