2018-03-07 10:57:10 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 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;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.widget.EdgeEffect;
|
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
|
|
2018-08-14 15:21:45 -07:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView.EdgeEffectFactory;
|
2018-03-07 10:57:10 -08:00
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
import com.android.launcher3.Utilities;
|
2018-03-07 10:57:10 -08:00
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
/**
|
|
|
|
|
* View group to allow rendering overscroll effect in a child at the parent level
|
|
|
|
|
*/
|
|
|
|
|
public class SpringRelativeLayout extends RelativeLayout {
|
2018-03-07 10:57:10 -08:00
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
private final EdgeEffect mEdgeGlowTop;
|
|
|
|
|
private final EdgeEffect mEdgeGlowBottom;
|
2018-03-07 10:57:10 -08:00
|
|
|
|
|
|
|
|
public SpringRelativeLayout(Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SpringRelativeLayout(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SpringRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
2021-03-29 12:26:05 -07:00
|
|
|
mEdgeGlowTop = Utilities.ATLEAST_S
|
|
|
|
|
? new EdgeEffect(context, attrs) : new EdgeEffect(context);
|
|
|
|
|
mEdgeGlowBottom = Utilities.ATLEAST_S
|
|
|
|
|
? new EdgeEffect(context, attrs) : new EdgeEffect(context);
|
|
|
|
|
setWillNotDraw(false);
|
2018-06-14 15:57:59 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-07 10:57:10 -08:00
|
|
|
@Override
|
2021-03-29 12:26:05 -07:00
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
|
super.draw(canvas);
|
|
|
|
|
if (!mEdgeGlowTop.isFinished()) {
|
|
|
|
|
final int restoreCount = canvas.save();
|
|
|
|
|
canvas.translate(0, 0);
|
|
|
|
|
mEdgeGlowTop.setSize(getWidth(), getHeight());
|
|
|
|
|
if (mEdgeGlowTop.draw(canvas)) {
|
|
|
|
|
postInvalidateOnAnimation();
|
|
|
|
|
}
|
|
|
|
|
canvas.restoreToCount(restoreCount);
|
2018-04-23 15:11:00 -07:00
|
|
|
}
|
2021-03-29 12:26:05 -07:00
|
|
|
if (!mEdgeGlowBottom.isFinished()) {
|
|
|
|
|
final int restoreCount = canvas.save();
|
|
|
|
|
final int width = getWidth();
|
|
|
|
|
final int height = getHeight();
|
|
|
|
|
canvas.translate(-width, height);
|
|
|
|
|
canvas.rotate(180, width, 0);
|
|
|
|
|
mEdgeGlowBottom.setSize(width, height);
|
|
|
|
|
if (mEdgeGlowBottom.draw(canvas)) {
|
|
|
|
|
postInvalidateOnAnimation();
|
|
|
|
|
}
|
|
|
|
|
canvas.restoreToCount(restoreCount);
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
/**
|
|
|
|
|
* Absorbs the velocity as a result for swipe-up fling
|
|
|
|
|
*/
|
|
|
|
|
protected void absorbSwipeUpVelocity(int velocity) {
|
|
|
|
|
mEdgeGlowBottom.onAbsorb(velocity);
|
|
|
|
|
invalidate();
|
2018-05-22 23:12:10 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-07 10:57:10 -08:00
|
|
|
public EdgeEffectFactory createEdgeEffectFactory() {
|
2021-03-29 12:26:05 -07:00
|
|
|
return new ProxyEdgeEffectFactory();
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
private class ProxyEdgeEffectFactory extends EdgeEffectFactory {
|
2018-03-07 10:57:10 -08:00
|
|
|
|
|
|
|
|
@NonNull @Override
|
|
|
|
|
protected EdgeEffect createEdgeEffect(RecyclerView view, int direction) {
|
|
|
|
|
switch (direction) {
|
|
|
|
|
case DIRECTION_TOP:
|
2021-03-29 12:26:05 -07:00
|
|
|
return new EdgeEffectProxy(getContext(), mEdgeGlowTop);
|
2018-03-07 10:57:10 -08:00
|
|
|
case DIRECTION_BOTTOM:
|
2021-03-29 12:26:05 -07:00
|
|
|
return new EdgeEffectProxy(getContext(), mEdgeGlowBottom);
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
return super.createEdgeEffect(view, direction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
private class EdgeEffectProxy extends EdgeEffect {
|
2018-03-07 10:57:10 -08:00
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
private final EdgeEffect mParent;
|
2018-03-07 10:57:10 -08:00
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
EdgeEffectProxy(Context context, EdgeEffect parent) {
|
2018-03-07 10:57:10 -08:00
|
|
|
super(context);
|
2021-03-29 12:26:05 -07:00
|
|
|
mParent = parent;
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean draw(Canvas canvas) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 12:26:05 -07:00
|
|
|
private void invalidateParentScrollEffect() {
|
|
|
|
|
if (!mParent.isFinished()) {
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 10:57:10 -08:00
|
|
|
@Override
|
|
|
|
|
public void onAbsorb(int velocity) {
|
2021-03-29 12:26:05 -07:00
|
|
|
mParent.onAbsorb(velocity);
|
|
|
|
|
invalidateParentScrollEffect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPull(float deltaDistance) {
|
|
|
|
|
mParent.onPull(deltaDistance);
|
|
|
|
|
invalidateParentScrollEffect();
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPull(float deltaDistance, float displacement) {
|
2021-03-29 12:26:05 -07:00
|
|
|
mParent.onPull(deltaDistance, displacement);
|
|
|
|
|
invalidateParentScrollEffect();
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRelease() {
|
2021-03-29 12:26:05 -07:00
|
|
|
mParent.onRelease();
|
|
|
|
|
invalidateParentScrollEffect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void finish() {
|
|
|
|
|
mParent.finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isFinished() {
|
|
|
|
|
return mParent.isFinished();
|
2018-03-07 10:57:10 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|