diff --git a/lawnchair/src/app/lawnchair/ui/StretchRelativeLayout.kt b/lawnchair/src/app/lawnchair/ui/StretchRelativeLayout.kt new file mode 100644 index 0000000000..34359e60f4 --- /dev/null +++ b/lawnchair/src/app/lawnchair/ui/StretchRelativeLayout.kt @@ -0,0 +1,113 @@ +package app.lawnchair.ui + +import android.content.Context +import android.graphics.Canvas +import android.util.AttributeSet +import android.util.SparseBooleanArray +import android.widget.EdgeEffect +import android.widget.RelativeLayout +import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener +import androidx.recyclerview.widget.RecyclerView +import kotlin.math.round + +@Suppress("LeakingThis") +open class StretchRelativeLayout @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : RelativeLayout(context, attrs, defStyleAttr) { + private val effect = StretchEdgeEffect { shift -> this.shift = shift } + private var shift = 0f + set(value) { + if (field != value) { + field = value + invalidate() + } + } + @JvmField + protected val mSpringViews = SparseBooleanArray() + + init { + setWillNotDraw(false) + } + + override fun draw(canvas: Canvas) { + val height = height.toFloat() + val scaleY = StretchEdgeEffect.getScale(shift, height) + if (scaleY != 1f) { + val save = canvas.save() + canvas.scale(1f, scaleY, 0f, StretchEdgeEffect.getPivot(shift, height)) + super.draw(canvas) + canvas.restoreToCount(save) + } else { + super.draw(canvas) + } + } + + fun addSpringView(id: Int) { + mSpringViews.put(id, true) + } + + fun removeSpringView(id: Int) { + mSpringViews.delete(id) + } + + open fun getCanvasClipTopForOverscroll() = 0 + + protected open fun setDampedScrollShift(shift: Float) { + + } + + protected open fun finishWithShiftAndVelocity( + shift: Float, velocity: Float, + listener: OnAnimationEndListener + ) { + effect.onAbsorb(round(velocity * 800f / 135f)) + effect.addEndListener(listener) + } + + fun createEdgeEffectFactory(): RecyclerView.EdgeEffectFactory { + return StretchEdgeEffectFactory() + } + + private inner class StretchEdgeEffectFactory : RecyclerView.EdgeEffectFactory() { + override fun createEdgeEffect(view: RecyclerView, direction: Int): EdgeEffect { + return when (direction) { + DIRECTION_TOP, DIRECTION_BOTTOM -> EdgeEffectProxy(context, direction) + else -> super.createEdgeEffect(view, direction) + } + } + } + + private inner class EdgeEffectProxy(context: Context, private val direction: Int) : EdgeEffect(context) { + private val multiplier = when (direction) { + RecyclerView.EdgeEffectFactory.DIRECTION_TOP -> 1f + RecyclerView.EdgeEffectFactory.DIRECTION_BOTTOM -> -1f + else -> throw IllegalArgumentException("invalid direction $direction") + } + + override fun isFinished(): Boolean { + return when (direction) { + RecyclerView.EdgeEffectFactory.DIRECTION_TOP -> shift <= 0f + RecyclerView.EdgeEffectFactory.DIRECTION_BOTTOM -> shift >= 0f + else -> true + } + } + + override fun onPull(deltaDistance: Float) { + effect.onPull(deltaDistance * height) + } + + override fun onPull(deltaDistance: Float, displacement: Float) { + effect.onPull(deltaDistance * height * multiplier) + } + + override fun onAbsorb(velocity: Int) { + effect.onAbsorb(velocity * multiplier) + } + + override fun onRelease() { + effect.onRelease() + } + } +} diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java index dd869ee13f..e4e1c95d20 100644 --- a/src/com/android/launcher3/allapps/AllAppsContainerView.java +++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java @@ -67,16 +67,16 @@ import com.android.launcher3.util.MultiValueAlpha; import com.android.launcher3.util.MultiValueAlpha.AlphaProperty; import com.android.launcher3.util.Themes; import com.android.launcher3.views.RecyclerViewFastScroller; -import com.android.launcher3.views.SpringRelativeLayout; import java.util.ArrayList; import app.lawnchair.allapps.LawnchairAlphabeticalAppsList; +import app.lawnchair.ui.StretchRelativeLayout; /** * The all apps view container. */ -public class AllAppsContainerView extends SpringRelativeLayout implements DragSource, +public class AllAppsContainerView extends StretchRelativeLayout implements DragSource, Insettable, OnDeviceProfileChangeListener { private static final float FLING_VELOCITY_MULTIPLIER = 135f; diff --git a/src/com/android/launcher3/views/TopRoundedCornerView.java b/src/com/android/launcher3/views/TopRoundedCornerView.java index 7888b08630..5081d8b9dc 100644 --- a/src/com/android/launcher3/views/TopRoundedCornerView.java +++ b/src/com/android/launcher3/views/TopRoundedCornerView.java @@ -26,10 +26,12 @@ import android.widget.FrameLayout; import com.android.launcher3.R; import com.android.launcher3.util.Themes; +import app.lawnchair.ui.StretchRelativeLayout; + /** * View with top rounded corners. */ -public class TopRoundedCornerView extends SpringRelativeLayout { +public class TopRoundedCornerView extends StretchRelativeLayout { private final RectF mRect = new RectF(); private final Path mClipPath = new Path();