| /* |
| * Copyright (C) 2015 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.systemui.stackdivider; |
| |
| import android.animation.Animator; |
| import android.animation.AnimatorListenerAdapter; |
| import android.animation.ValueAnimator; |
| import android.animation.ValueAnimator.AnimatorUpdateListener; |
| import android.annotation.Nullable; |
| import android.app.ActivityManager.StackId; |
| import android.content.Context; |
| import android.content.res.Configuration; |
| import android.graphics.Rect; |
| import android.graphics.Region.Op; |
| import android.hardware.display.DisplayManager; |
| import android.util.AttributeSet; |
| import android.util.MathUtils; |
| import android.view.Display; |
| import android.view.DisplayInfo; |
| import android.view.MotionEvent; |
| import android.view.PointerIcon; |
| import android.view.VelocityTracker; |
| import android.view.View; |
| import android.view.View.OnTouchListener; |
| import android.view.ViewTreeObserver.InternalInsetsInfo; |
| import android.view.ViewTreeObserver.OnComputeInternalInsetsListener; |
| import android.view.Window; |
| import android.view.WindowInsets; |
| import android.view.WindowManager; |
| import android.view.animation.AnimationUtils; |
| import android.view.animation.Interpolator; |
| import android.view.animation.PathInterpolator; |
| import android.widget.FrameLayout; |
| import android.widget.ImageButton; |
| |
| import com.android.systemui.R; |
| import com.android.systemui.stackdivider.DividerSnapAlgorithm.SnapTarget; |
| import com.android.systemui.statusbar.FlingAnimationUtils; |
| |
| import static android.view.PointerIcon.STYLE_HORIZONTAL_DOUBLE_ARROW; |
| import static android.view.PointerIcon.STYLE_VERTICAL_DOUBLE_ARROW; |
| |
| /** |
| * Docked stack divider. |
| */ |
| public class DividerView extends FrameLayout implements OnTouchListener, |
| OnComputeInternalInsetsListener { |
| |
| static final long TOUCH_ANIMATION_DURATION = 150; |
| static final long TOUCH_RELEASE_ANIMATION_DURATION = 200; |
| static final Interpolator TOUCH_RESPONSE_INTERPOLATOR = |
| new PathInterpolator(0.3f, 0f, 0.1f, 1f); |
| |
| private static final String TAG = "DividerView"; |
| |
| private static final int TASK_POSITION_SAME = Integer.MAX_VALUE; |
| private static final float DIM_START_FRACTION = 0.5f; |
| private static final float DIM_DAMP_FACTOR = 1.7f; |
| |
| private static final PathInterpolator SLOWDOWN_INTERPOLATOR = |
| new PathInterpolator(0.5f, 1f, 0.5f, 1f); |
| |
| private DividerHandleView mHandle; |
| private View mBackground; |
| private int mStartX; |
| private int mStartY; |
| private int mStartPosition; |
| private int mDockSide; |
| private final int[] mTempInt2 = new int[2]; |
| |
| private int mDividerInsets; |
| private int mDisplayWidth; |
| private int mDisplayHeight; |
| private int mDividerWindowWidth; |
| private int mDividerSize; |
| private int mTouchElevation; |
| |
| private final Rect mDockedRect = new Rect(); |
| private final Rect mDockedTaskRect = new Rect(); |
| private final Rect mOtherTaskRect = new Rect(); |
| private final Rect mOtherRect = new Rect(); |
| private final Rect mDockedInsetRect = new Rect(); |
| private final Rect mOtherInsetRect = new Rect(); |
| private final Rect mLastResizeRect = new Rect(); |
| private final WindowManagerProxy mWindowManagerProxy = WindowManagerProxy.getInstance(); |
| private Interpolator mFastOutSlowInInterpolator; |
| private DividerWindowManager mWindowManager; |
| private VelocityTracker mVelocityTracker; |
| private FlingAnimationUtils mFlingAnimationUtils; |
| private DividerSnapAlgorithm mSnapAlgorithm; |
| private final Rect mStableInsets = new Rect(); |
| |
| public DividerView(Context context) { |
| super(context); |
| } |
| |
| public DividerView(Context context, @Nullable AttributeSet attrs) { |
| super(context, attrs); |
| } |
| |
| public DividerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| super(context, attrs, defStyleAttr); |
| } |
| |
| public DividerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| int defStyleRes) { |
| super(context, attrs, defStyleAttr, defStyleRes); |
| } |
| |
| @Override |
| protected void onFinishInflate() { |
| super.onFinishInflate(); |
| mHandle = (DividerHandleView) findViewById(R.id.docked_divider_handle); |
| mBackground = findViewById(R.id.docked_divider_background); |
| mHandle.setOnTouchListener(this); |
| mDividerWindowWidth = getResources().getDimensionPixelSize( |
| com.android.internal.R.dimen.docked_stack_divider_thickness); |
| mDividerInsets = getResources().getDimensionPixelSize( |
| com.android.internal.R.dimen.docked_stack_divider_insets); |
| mDividerSize = mDividerWindowWidth - 2 * mDividerInsets; |
| mTouchElevation = getResources().getDimensionPixelSize( |
| R.dimen.docked_stack_divider_lift_elevation); |
| mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(), |
| android.R.interpolator.fast_out_slow_in); |
| mFlingAnimationUtils = new FlingAnimationUtils(getContext(), 0.3f); |
| updateDisplayInfo(); |
| boolean landscape = getResources().getConfiguration().orientation |
| == Configuration.ORIENTATION_LANDSCAPE; |
| mHandle.setPointerIcon(PointerIcon.getSystemIcon(getContext(), |
| landscape ? STYLE_HORIZONTAL_DOUBLE_ARROW : STYLE_VERTICAL_DOUBLE_ARROW)); |
| getViewTreeObserver().addOnComputeInternalInsetsListener(this); |
| } |
| |
| @Override |
| public WindowInsets onApplyWindowInsets(WindowInsets insets) { |
| mStableInsets.set(insets.getStableInsetLeft(), insets.getStableInsetTop(), |
| insets.getStableInsetRight(), insets.getStableInsetBottom()); |
| return super.onApplyWindowInsets(insets); |
| } |
| |
| public void setWindowManager(DividerWindowManager windowManager) { |
| mWindowManager = windowManager; |
| } |
| |
| public WindowManagerProxy getWindowManagerProxy() { |
| return mWindowManagerProxy; |
| } |
| |
| public boolean startDragging(boolean animate) { |
| mHandle.setTouching(true, animate); |
| mDockSide = mWindowManagerProxy.getDockSide(); |
| mSnapAlgorithm = new DividerSnapAlgorithm(getContext(), mFlingAnimationUtils, mDisplayWidth, |
| mDisplayHeight, mDividerSize, isHorizontalDivision(), mStableInsets); |
| if (mDockSide != WindowManager.DOCKED_INVALID) { |
| mWindowManagerProxy.setResizing(true); |
| mWindowManager.setSlippery(false); |
| liftBackground(); |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| public void stopDragging(int position, float velocity) { |
| mHandle.setTouching(false, true /* animate */); |
| fling(position, velocity); |
| mWindowManager.setSlippery(true); |
| releaseBackground(); |
| } |
| |
| public DividerSnapAlgorithm getSnapAlgorithm() { |
| return mSnapAlgorithm; |
| } |
| |
| @Override |
| public boolean onTouch(View v, MotionEvent event) { |
| convertToScreenCoordinates(event); |
| final int action = event.getAction() & MotionEvent.ACTION_MASK; |
| switch (action) { |
| case MotionEvent.ACTION_DOWN: |
| mVelocityTracker = VelocityTracker.obtain(); |
| mVelocityTracker.addMovement(event); |
| mStartX = (int) event.getX(); |
| mStartY = (int) event.getY(); |
| getLocationOnScreen(mTempInt2); |
| boolean result = startDragging(true /* animate */); |
| if (isHorizontalDivision()) { |
| mStartPosition = mTempInt2[1] + mDividerInsets; |
| } else { |
| mStartPosition = mTempInt2[0] + mDividerInsets; |
| } |
| return result; |
| case MotionEvent.ACTION_MOVE: |
| mVelocityTracker.addMovement(event); |
| int x = (int) event.getX(); |
| int y = (int) event.getY(); |
| if (mDockSide != WindowManager.DOCKED_INVALID) { |
| int position = calculatePosition(x, y); |
| SnapTarget snapTarget = mSnapAlgorithm.calculateSnapTarget(position, |
| 0 /* velocity */); |
| resizeStack(calculatePosition(x, y), snapTarget.position, snapTarget); |
| } |
| break; |
| case MotionEvent.ACTION_UP: |
| case MotionEvent.ACTION_CANCEL: |
| mVelocityTracker.addMovement(event); |
| |
| x = (int) event.getRawX(); |
| y = (int) event.getRawY(); |
| |
| mVelocityTracker.computeCurrentVelocity(1000); |
| int position = calculatePosition(x, y); |
| stopDragging(position, isHorizontalDivision() ? mVelocityTracker.getYVelocity() |
| : mVelocityTracker.getXVelocity()); |
| break; |
| } |
| return true; |
| } |
| |
| private void convertToScreenCoordinates(MotionEvent event) { |
| event.setLocation(event.getRawX(), event.getRawY()); |
| } |
| |
| private void fling(int position, float velocity) { |
| final SnapTarget snapTarget = mSnapAlgorithm.calculateSnapTarget(position, velocity); |
| |
| ValueAnimator anim = ValueAnimator.ofInt(position, snapTarget.position); |
| anim.addUpdateListener(new AnimatorUpdateListener() { |
| @Override |
| public void onAnimationUpdate(ValueAnimator animation) { |
| resizeStack((Integer) animation.getAnimatedValue(), |
| animation.getAnimatedFraction() == 1f |
| ? TASK_POSITION_SAME |
| : snapTarget.position, snapTarget); |
| } |
| }); |
| anim.addListener(new AnimatorListenerAdapter() { |
| @Override |
| public void onAnimationEnd(Animator animation) { |
| commitSnapFlags(snapTarget); |
| mWindowManagerProxy.setResizing(false); |
| mDockSide = WindowManager.DOCKED_INVALID; |
| } |
| }); |
| mFlingAnimationUtils.apply(anim, position, snapTarget.position, velocity); |
| anim.start(); |
| } |
| |
| private void commitSnapFlags(SnapTarget target) { |
| if (target.flag == SnapTarget.FLAG_NONE) { |
| return; |
| } |
| boolean dismissOrMaximize; |
| if (target.flag == SnapTarget.FLAG_DISMISS_START) { |
| dismissOrMaximize = mDockSide == WindowManager.DOCKED_LEFT |
| || mDockSide == WindowManager.DOCKED_TOP; |
| } else { |
| dismissOrMaximize = mDockSide == WindowManager.DOCKED_RIGHT |
| || mDockSide == WindowManager.DOCKED_BOTTOM; |
| } |
| if (dismissOrMaximize) { |
| mWindowManagerProxy.dismissDockedStack(); |
| } else { |
| mWindowManagerProxy.maximizeDockedStack(); |
| } |
| mWindowManagerProxy.setResizeDimLayer(false, -1, 0f); |
| } |
| |
| private void liftBackground() { |
| if (isHorizontalDivision()) { |
| mBackground.animate().scaleY(1.4f); |
| } else { |
| mBackground.animate().scaleX(1.4f); |
| } |
| mBackground.animate() |
| .setInterpolator(TOUCH_RESPONSE_INTERPOLATOR) |
| .setDuration(TOUCH_ANIMATION_DURATION) |
| .translationZ(mTouchElevation) |
| .start(); |
| |
| // Lift handle as well so it doesn't get behind the background, even though it doesn't |
| // cast shadow. |
| mHandle.animate() |
| .setInterpolator(TOUCH_RESPONSE_INTERPOLATOR) |
| .setDuration(TOUCH_ANIMATION_DURATION) |
| .translationZ(mTouchElevation) |
| .start(); |
| } |
| |
| private void releaseBackground() { |
| mBackground.animate() |
| .setInterpolator(mFastOutSlowInInterpolator) |
| .setDuration(TOUCH_RELEASE_ANIMATION_DURATION) |
| .translationZ(0) |
| .scaleX(1f) |
| .scaleY(1f) |
| .start(); |
| mHandle.animate() |
| .setInterpolator(mFastOutSlowInInterpolator) |
| .setDuration(TOUCH_RELEASE_ANIMATION_DURATION) |
| .translationZ(0) |
| .start(); |
| } |
| |
| @Override |
| protected void onConfigurationChanged(Configuration newConfig) { |
| super.onConfigurationChanged(newConfig); |
| updateDisplayInfo(); |
| } |
| |
| private void updateDisplayInfo() { |
| final DisplayManager displayManager = |
| (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); |
| Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY); |
| final DisplayInfo info = new DisplayInfo(); |
| display.getDisplayInfo(info); |
| mDisplayWidth = info.logicalWidth; |
| mDisplayHeight = info.logicalHeight; |
| } |
| |
| private int calculatePosition(int touchX, int touchY) { |
| return isHorizontalDivision() ? calculateYPosition(touchY) : calculateXPosition(touchX); |
| } |
| |
| public boolean isHorizontalDivision() { |
| return getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; |
| } |
| |
| private int calculateXPosition(int touchX) { |
| return mStartPosition + touchX - mStartX; |
| } |
| |
| private int calculateYPosition(int touchY) { |
| return mStartPosition + touchY - mStartY; |
| } |
| |
| public void calculateBoundsForPosition(int position, int dockSide, Rect outRect) { |
| outRect.set(0, 0, mDisplayWidth, mDisplayHeight); |
| switch (dockSide) { |
| case WindowManager.DOCKED_LEFT: |
| outRect.right = position; |
| break; |
| case WindowManager.DOCKED_TOP: |
| outRect.bottom = position; |
| break; |
| case WindowManager.DOCKED_RIGHT: |
| outRect.left = position + mDividerWindowWidth - 2 * mDividerInsets; |
| break; |
| case WindowManager.DOCKED_BOTTOM: |
| outRect.top = position + mDividerWindowWidth - 2 * mDividerInsets; |
| break; |
| } |
| if (outRect.left > outRect.right) { |
| outRect.left = outRect.right; |
| } |
| if (outRect.top > outRect.bottom) { |
| outRect.top = outRect.bottom; |
| } |
| if (outRect.right < outRect.left) { |
| outRect.right = outRect.left; |
| } |
| if (outRect.bottom < outRect.top) { |
| outRect.bottom = outRect.top; |
| } |
| } |
| |
| private int invertDockSide(int dockSide) { |
| switch (dockSide) { |
| case WindowManager.DOCKED_LEFT: |
| return WindowManager.DOCKED_RIGHT; |
| case WindowManager.DOCKED_TOP: |
| return WindowManager.DOCKED_BOTTOM; |
| case WindowManager.DOCKED_RIGHT: |
| return WindowManager.DOCKED_LEFT; |
| case WindowManager.DOCKED_BOTTOM: |
| return WindowManager.DOCKED_TOP; |
| default: |
| return WindowManager.DOCKED_INVALID; |
| } |
| } |
| |
| private void alignTopLeft(Rect containingRect, Rect rect) { |
| int width = rect.width(); |
| int height = rect.height(); |
| rect.set(containingRect.left, containingRect.top, |
| containingRect.left + width, containingRect.top + height); |
| } |
| |
| private void alignBottomRight(Rect containingRect, Rect rect) { |
| int width = rect.width(); |
| int height = rect.height(); |
| rect.set(containingRect.right - width, containingRect.bottom - height, |
| containingRect.right, containingRect.bottom); |
| } |
| |
| public void resizeStack(int position, int taskPosition, SnapTarget taskSnapTarget) { |
| calculateBoundsForPosition(position, mDockSide, mDockedRect); |
| |
| if (mDockedRect.equals(mLastResizeRect)) { |
| return; |
| } |
| |
| // Make sure shadows are updated |
| mBackground.invalidate(); |
| |
| mLastResizeRect.set(mDockedRect); |
| if (taskPosition != TASK_POSITION_SAME) { |
| calculateBoundsForPosition(position, invertDockSide(mDockSide), mOtherRect); |
| int dockSideInverted = invertDockSide(mDockSide); |
| int taskPositionDocked = |
| restrictDismissingTaskPosition(taskPosition, mDockSide, taskSnapTarget); |
| int taskPositionOther = |
| restrictDismissingTaskPosition(taskPosition, dockSideInverted, taskSnapTarget); |
| calculateBoundsForPosition(taskPositionDocked, mDockSide, mDockedTaskRect); |
| calculateBoundsForPosition(taskPositionOther, dockSideInverted, mOtherTaskRect); |
| alignTopLeft(mDockedRect, mDockedTaskRect); |
| alignTopLeft(mOtherRect, mOtherTaskRect); |
| mDockedInsetRect.set(mDockedTaskRect); |
| mOtherInsetRect.set(mOtherTaskRect); |
| if (dockSideTopLeft(mDockSide)) { |
| alignTopLeft(mDockedRect, mDockedInsetRect); |
| alignBottomRight(mOtherRect, mOtherInsetRect); |
| } else { |
| alignBottomRight(mDockedRect, mDockedInsetRect); |
| alignTopLeft(mOtherRect, mOtherInsetRect); |
| } |
| applyDismissingParallax(mDockedTaskRect, mDockSide, taskSnapTarget, position, |
| taskPositionDocked); |
| applyDismissingParallax(mOtherTaskRect, dockSideInverted, taskSnapTarget, position, |
| taskPositionOther); |
| mWindowManagerProxy.resizeDockedStack(mDockedRect, mDockedTaskRect, mDockedInsetRect, |
| mOtherTaskRect, mOtherInsetRect); |
| } else { |
| mWindowManagerProxy.resizeDockedStack(mDockedRect, null, null, null, null); |
| } |
| float fraction = mSnapAlgorithm.calculateDismissingFraction(position); |
| fraction = Math.max(0, |
| Math.min((fraction / DIM_START_FRACTION - 1f) / DIM_DAMP_FACTOR, 1f)); |
| mWindowManagerProxy.setResizeDimLayer(fraction != 0f, |
| getStackIdForDismissTarget(mSnapAlgorithm.getClosestDismissTarget(position)), |
| fraction); |
| } |
| |
| /** |
| * When the snap target is dismissing one side, make sure that the dismissing side doesn't get |
| * 0 size. |
| */ |
| private int restrictDismissingTaskPosition(int taskPosition, int dockSide, |
| SnapTarget snapTarget) { |
| if (snapTarget.flag == SnapTarget.FLAG_DISMISS_START && dockSideTopLeft(dockSide)) { |
| return mSnapAlgorithm.getFirstSplitTarget().position; |
| } else if (snapTarget.flag == SnapTarget.FLAG_DISMISS_END |
| && dockSideBottomRight(dockSide)) { |
| return mSnapAlgorithm.getLastSplitTarget().position; |
| } else { |
| return taskPosition; |
| } |
| } |
| |
| /** |
| * Applies a parallax to the task when dismissing. |
| */ |
| private void applyDismissingParallax(Rect taskRect, int dockSide, SnapTarget snapTarget, |
| int position, int taskPosition) { |
| float fraction = Math.min(1, Math.max(0, |
| mSnapAlgorithm.calculateDismissingFraction(position))); |
| SnapTarget dismissTarget = null; |
| SnapTarget splitTarget = null; |
| if ((snapTarget.flag == SnapTarget.FLAG_DISMISS_START |
| || snapTarget == mSnapAlgorithm.getFirstSplitTarget()) |
| && dockSideTopLeft(dockSide)) { |
| dismissTarget = mSnapAlgorithm.getDismissStartTarget(); |
| splitTarget = mSnapAlgorithm.getFirstSplitTarget(); |
| } else if ((snapTarget.flag == SnapTarget.FLAG_DISMISS_END |
| || snapTarget == mSnapAlgorithm.getLastSplitTarget()) |
| && dockSideBottomRight(dockSide)) { |
| dismissTarget = mSnapAlgorithm.getDismissEndTarget(); |
| splitTarget = mSnapAlgorithm.getLastSplitTarget(); |
| } |
| if (dismissTarget != null && fraction > 0f |
| && isDismissing(splitTarget, position, dockSide)) { |
| fraction = calculateParallaxDismissingFraction(fraction); |
| int offsetPosition = (int) (taskPosition + |
| fraction * (dismissTarget.position - splitTarget.position)); |
| int width = taskRect.width(); |
| int height = taskRect.height(); |
| switch (dockSide) { |
| case WindowManager.DOCKED_LEFT: |
| taskRect.left = offsetPosition - width; |
| taskRect.right = offsetPosition; |
| break; |
| case WindowManager.DOCKED_RIGHT: |
| taskRect.left = offsetPosition + mDividerSize; |
| taskRect.right = offsetPosition + width + mDividerSize; |
| break; |
| case WindowManager.DOCKED_TOP: |
| taskRect.top = offsetPosition - height; |
| taskRect.bottom = offsetPosition; |
| break; |
| case WindowManager.DOCKED_BOTTOM: |
| taskRect.top = offsetPosition + mDividerSize; |
| taskRect.bottom = offsetPosition + height + mDividerSize; |
| break; |
| } |
| } |
| } |
| |
| /** |
| * @return for a specified {@code fraction}, this returns an adjusted value that simulates a |
| * slowing down parallax effect |
| */ |
| private static float calculateParallaxDismissingFraction(float fraction) { |
| return SLOWDOWN_INTERPOLATOR.getInterpolation(fraction) / 3.5f; |
| } |
| |
| private static boolean isDismissing(SnapTarget snapTarget, int position, int dockSide) { |
| if (dockSide == WindowManager.DOCKED_TOP || dockSide == WindowManager.DOCKED_LEFT) { |
| return position < snapTarget.position; |
| } else { |
| return position > snapTarget.position; |
| } |
| } |
| |
| private int getStackIdForDismissTarget(SnapTarget dismissTarget) { |
| if (dismissTarget.flag == SnapTarget.FLAG_DISMISS_START && |
| (mDockSide == WindowManager.DOCKED_LEFT || mDockSide == WindowManager.DOCKED_TOP)) { |
| return StackId.DOCKED_STACK_ID; |
| } else { |
| return StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
| } |
| } |
| |
| /** |
| * @return true if and only if {@code dockSide} is top or left |
| */ |
| private static boolean dockSideTopLeft(int dockSide) { |
| return dockSide == WindowManager.DOCKED_TOP || dockSide == WindowManager.DOCKED_LEFT; |
| } |
| |
| /** |
| * @return true if and only if {@code dockSide} is bottom or right |
| */ |
| private static boolean dockSideBottomRight(int dockSide) { |
| return dockSide == WindowManager.DOCKED_BOTTOM || dockSide == WindowManager.DOCKED_RIGHT; |
| } |
| |
| @Override |
| public void onComputeInternalInsets(InternalInsetsInfo inoutInfo) { |
| inoutInfo.setTouchableInsets(InternalInsetsInfo.TOUCHABLE_INSETS_REGION); |
| inoutInfo.touchableRegion.set(mHandle.getLeft(), mHandle.getTop(), mHandle.getRight(), |
| mHandle.getBottom()); |
| inoutInfo.touchableRegion.op(mBackground.getLeft(), mBackground.getTop(), |
| mBackground.getRight(), mBackground.getBottom(), Op.UNION); |
| } |
| } |