2017-11-29 18:39:49 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 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.launcher3.views;
|
|
|
|
|
|
2020-02-10 09:26:20 -08:00
|
|
|
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
|
|
|
|
|
2017-11-29 18:39:49 +00:00
|
|
|
import static com.android.launcher3.anim.Interpolators.scrollInterpolatorForVelocity;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.animation.PropertyValuesHolder;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.util.Property;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.animation.Interpolator;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.AbstractFloatingView;
|
|
|
|
|
import com.android.launcher3.Launcher;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
|
|
|
|
import com.android.launcher3.anim.Interpolators;
|
2019-08-15 17:24:07 -07:00
|
|
|
import com.android.launcher3.touch.BaseSwipeDetector;
|
|
|
|
|
import com.android.launcher3.touch.SingleAxisSwipeDetector;
|
2017-11-29 18:39:49 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extension of AbstractFloatingView with common methods for sliding in from bottom
|
|
|
|
|
*/
|
|
|
|
|
public abstract class AbstractSlideInView extends AbstractFloatingView
|
2019-08-15 17:24:07 -07:00
|
|
|
implements SingleAxisSwipeDetector.Listener {
|
2017-11-29 18:39:49 +00:00
|
|
|
|
2019-12-09 14:55:56 -08:00
|
|
|
protected static final Property<AbstractSlideInView, Float> TRANSLATION_SHIFT =
|
2017-11-29 18:39:49 +00:00
|
|
|
new Property<AbstractSlideInView, Float>(Float.class, "translationShift") {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Float get(AbstractSlideInView view) {
|
|
|
|
|
return view.mTranslationShift;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(AbstractSlideInView view, Float value) {
|
|
|
|
|
view.setTranslationShift(value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
protected static final float TRANSLATION_SHIFT_CLOSED = 1f;
|
|
|
|
|
protected static final float TRANSLATION_SHIFT_OPENED = 0f;
|
|
|
|
|
|
|
|
|
|
protected final Launcher mLauncher;
|
2019-08-15 17:24:07 -07:00
|
|
|
protected final SingleAxisSwipeDetector mSwipeDetector;
|
2017-11-29 18:39:49 +00:00
|
|
|
protected final ObjectAnimator mOpenCloseAnimator;
|
|
|
|
|
|
|
|
|
|
protected View mContent;
|
2021-01-19 13:12:10 -06:00
|
|
|
protected final View mColorScrim;
|
2017-11-29 18:39:49 +00:00
|
|
|
protected Interpolator mScrollInterpolator;
|
|
|
|
|
|
|
|
|
|
// range [0, 1], 0=> completely open, 1=> completely closed
|
|
|
|
|
protected float mTranslationShift = TRANSLATION_SHIFT_CLOSED;
|
|
|
|
|
|
|
|
|
|
protected boolean mNoIntercept;
|
|
|
|
|
|
|
|
|
|
public AbstractSlideInView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
mLauncher = Launcher.getLauncher(context);
|
|
|
|
|
|
|
|
|
|
mScrollInterpolator = Interpolators.SCROLL_CUBIC;
|
2019-08-15 17:24:07 -07:00
|
|
|
mSwipeDetector = new SingleAxisSwipeDetector(context, this,
|
|
|
|
|
SingleAxisSwipeDetector.VERTICAL);
|
2017-11-29 18:39:49 +00:00
|
|
|
|
2018-07-13 11:03:04 -07:00
|
|
|
mOpenCloseAnimator = ObjectAnimator.ofPropertyValuesHolder(this);
|
2017-11-29 18:39:49 +00:00
|
|
|
mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
mSwipeDetector.finishedScrolling();
|
2018-05-15 13:55:57 -07:00
|
|
|
announceAccessibilityChanges();
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
});
|
2020-02-10 09:26:20 -08:00
|
|
|
int scrimColor = getScrimColor(mLauncher);
|
|
|
|
|
mColorScrim = scrimColor != -1 ? createColorScrim(mLauncher, scrimColor) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void attachToContainer() {
|
|
|
|
|
if (mColorScrim != null) {
|
|
|
|
|
getPopupContainer().addView(mColorScrim);
|
|
|
|
|
}
|
|
|
|
|
getPopupContainer().addView(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a scrim color for a sliding view. if returned value is -1, no scrim is added.
|
|
|
|
|
*/
|
|
|
|
|
protected int getScrimColor(Context context) {
|
|
|
|
|
return -1;
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void setTranslationShift(float translationShift) {
|
|
|
|
|
mTranslationShift = translationShift;
|
|
|
|
|
mContent.setTranslationY(mTranslationShift * mContent.getHeight());
|
2020-02-10 09:26:20 -08:00
|
|
|
if (mColorScrim != null) {
|
|
|
|
|
mColorScrim.setAlpha(1 - mTranslationShift);
|
|
|
|
|
}
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
|
|
|
|
|
if (mNoIntercept) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int directionsToDetectScroll = mSwipeDetector.isIdleState() ?
|
2019-08-15 17:24:07 -07:00
|
|
|
SingleAxisSwipeDetector.DIRECTION_NEGATIVE : 0;
|
2017-11-29 18:39:49 +00:00
|
|
|
mSwipeDetector.setDetectableScrollConditions(
|
|
|
|
|
directionsToDetectScroll, false);
|
|
|
|
|
mSwipeDetector.onTouchEvent(ev);
|
|
|
|
|
return mSwipeDetector.isDraggingOrSettling()
|
2018-07-13 11:03:04 -07:00
|
|
|
|| !getPopupContainer().isEventOverView(mContent, ev);
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onControllerTouchEvent(MotionEvent ev) {
|
|
|
|
|
mSwipeDetector.onTouchEvent(ev);
|
2018-08-15 19:58:01 +01:00
|
|
|
if (ev.getAction() == MotionEvent.ACTION_UP && mSwipeDetector.isIdleState()
|
|
|
|
|
&& !isOpeningAnimationRunning()) {
|
2017-11-29 18:39:49 +00:00
|
|
|
// If we got ACTION_UP without ever starting swipe, close the panel.
|
2018-07-13 11:03:04 -07:00
|
|
|
if (!getPopupContainer().isEventOverView(mContent, ev)) {
|
2017-11-29 18:39:49 +00:00
|
|
|
close(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-15 19:58:01 +01:00
|
|
|
private boolean isOpeningAnimationRunning() {
|
|
|
|
|
return mIsOpen && mOpenCloseAnimator.isRunning();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 17:24:07 -07:00
|
|
|
/* SingleAxisSwipeDetector.Listener */
|
2017-11-29 18:39:49 +00:00
|
|
|
|
|
|
|
|
@Override
|
2019-12-19 11:50:55 -08:00
|
|
|
public void onDragStart(boolean start, float startDisplacement) { }
|
2017-11-29 18:39:49 +00:00
|
|
|
|
|
|
|
|
@Override
|
2018-09-12 15:47:06 -07:00
|
|
|
public boolean onDrag(float displacement) {
|
2017-11-29 18:39:49 +00:00
|
|
|
float range = mContent.getHeight();
|
|
|
|
|
displacement = Utilities.boundToRange(displacement, 0, range);
|
|
|
|
|
setTranslationShift(displacement / range);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-08-15 17:24:07 -07:00
|
|
|
public void onDragEnd(float velocity) {
|
|
|
|
|
if ((mSwipeDetector.isFling(velocity) && velocity > 0) || mTranslationShift > 0.5f) {
|
2017-11-29 18:39:49 +00:00
|
|
|
mScrollInterpolator = scrollInterpolatorForVelocity(velocity);
|
2019-08-15 17:24:07 -07:00
|
|
|
mOpenCloseAnimator.setDuration(BaseSwipeDetector.calculateDuration(
|
2017-11-29 18:39:49 +00:00
|
|
|
velocity, TRANSLATION_SHIFT_CLOSED - mTranslationShift));
|
|
|
|
|
close(true);
|
|
|
|
|
} else {
|
|
|
|
|
mOpenCloseAnimator.setValues(PropertyValuesHolder.ofFloat(
|
|
|
|
|
TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED));
|
|
|
|
|
mOpenCloseAnimator.setDuration(
|
2019-08-15 17:24:07 -07:00
|
|
|
BaseSwipeDetector.calculateDuration(velocity, mTranslationShift))
|
2017-11-29 18:39:49 +00:00
|
|
|
.setInterpolator(Interpolators.DEACCEL);
|
|
|
|
|
mOpenCloseAnimator.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void handleClose(boolean animate, long defaultDuration) {
|
2019-08-29 23:11:30 -07:00
|
|
|
if (!mIsOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!animate) {
|
2017-11-29 18:39:49 +00:00
|
|
|
mOpenCloseAnimator.cancel();
|
|
|
|
|
setTranslationShift(TRANSLATION_SHIFT_CLOSED);
|
|
|
|
|
onCloseComplete();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mOpenCloseAnimator.setValues(
|
|
|
|
|
PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_CLOSED));
|
|
|
|
|
mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
onCloseComplete();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (mSwipeDetector.isIdleState()) {
|
|
|
|
|
mOpenCloseAnimator
|
|
|
|
|
.setDuration(defaultDuration)
|
|
|
|
|
.setInterpolator(Interpolators.ACCEL);
|
|
|
|
|
} else {
|
|
|
|
|
mOpenCloseAnimator.setInterpolator(mScrollInterpolator);
|
|
|
|
|
}
|
|
|
|
|
mOpenCloseAnimator.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void onCloseComplete() {
|
|
|
|
|
mIsOpen = false;
|
2018-07-13 11:03:04 -07:00
|
|
|
getPopupContainer().removeView(this);
|
2020-02-10 09:26:20 -08:00
|
|
|
if (mColorScrim != null) {
|
|
|
|
|
getPopupContainer().removeView(mColorScrim);
|
|
|
|
|
}
|
2018-07-13 11:03:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected BaseDragLayer getPopupContainer() {
|
|
|
|
|
return mLauncher.getDragLayer();
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|
2020-02-10 09:26:20 -08:00
|
|
|
|
2021-04-06 03:03:33 -05:00
|
|
|
protected View createColorScrim(Context context, int bgColor) {
|
2020-02-10 09:26:20 -08:00
|
|
|
View view = new View(context);
|
|
|
|
|
view.forceHasOverlappingRendering(false);
|
|
|
|
|
view.setBackgroundColor(bgColor);
|
|
|
|
|
|
|
|
|
|
BaseDragLayer.LayoutParams lp = new BaseDragLayer.LayoutParams(MATCH_PARENT, MATCH_PARENT);
|
|
|
|
|
lp.ignoreInsets = true;
|
|
|
|
|
view.setLayoutParams(lp);
|
|
|
|
|
|
|
|
|
|
return view;
|
|
|
|
|
}
|
2017-11-29 18:39:49 +00:00
|
|
|
}
|