Desktop: Signal vibes or travewave, chosen in Preferences → Appearance. Rabbit: white on pure black, 22/28/32 sp text, outlined buttons and dim-edged text boxes, and drawn icons instead of emoji (rabbit.html). Pulling the chat list further once the search box shows stretches a bubble that pops at the mark and starts the check, then a spinner until it's done.
215 lines
8.0 KiB
Java
215 lines
8.0 KiB
Java
package me.teafry.voipmessages;
|
|
|
|
import android.animation.ValueAnimator;
|
|
import android.content.Context;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Paint;
|
|
import android.graphics.RectF;
|
|
import android.view.HapticFeedbackConstants;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.animation.DecelerateInterpolator;
|
|
import android.view.animation.OvershootInterpolator;
|
|
import android.widget.AbsListView;
|
|
import android.widget.FrameLayout;
|
|
|
|
/**
|
|
* Pull down on a list that's already at its top (the search box showing)
|
|
* to check for new messages. The pull is a rubber band: the further you go
|
|
* the harder it gets. A bubble grows in the gap and stretches, then pops at
|
|
* the mark, and the check starts right then; let go and the list springs
|
|
* back to a spinner until the check is done. (The framework has no
|
|
* SwipeRefreshLayout without androidx.)
|
|
*/
|
|
final class PullRefresh extends FrameLayout {
|
|
interface OnRefresh {
|
|
void refresh();
|
|
}
|
|
|
|
private final AbsListView list;
|
|
private final Bubble bubble;
|
|
private final int threshold, slop, reach, hold;
|
|
private float startY = -1;
|
|
private boolean pulling, refreshing, popped;
|
|
private ValueAnimator settle;
|
|
OnRefresh onRefresh;
|
|
|
|
PullRefresh(Context c, AbsListView list) {
|
|
super(c);
|
|
this.list = list;
|
|
threshold = Ui.dp(c, 150);
|
|
slop = Ui.dp(c, 24);
|
|
reach = Ui.dp(c, 230); // the most the list ever moves
|
|
hold = Ui.dp(c, 84); // the gap the spinner sits in
|
|
bubble = new Bubble(c, this);
|
|
addView(bubble, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
addView(list, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
}
|
|
|
|
private boolean atTop() {
|
|
return list.getChildCount() == 0 || (list.getFirstVisiblePosition() == 0 && list.getChildAt(0).getTop() >= list.getPaddingTop());
|
|
}
|
|
|
|
/** How far the list sits below its place: the gap the bubble draws in. */
|
|
float gap() {
|
|
return list.getTranslationY();
|
|
}
|
|
|
|
private void moveTo(float y) {
|
|
list.setTranslationY(y);
|
|
bubble.invalidate();
|
|
}
|
|
|
|
private void animateTo(float y, boolean springy) {
|
|
if (settle != null) settle.cancel();
|
|
settle = ValueAnimator.ofFloat(gap(), y);
|
|
settle.setDuration(springy ? 420 : 260);
|
|
settle.setInterpolator(springy ? new OvershootInterpolator(1.6f) : new DecelerateInterpolator());
|
|
settle.addUpdateListener(a -> moveTo((float) a.getAnimatedValue()));
|
|
settle.start();
|
|
}
|
|
|
|
@Override
|
|
public boolean onInterceptTouchEvent(MotionEvent e) {
|
|
if (refreshing) return false;
|
|
switch (e.getActionMasked()) {
|
|
case MotionEvent.ACTION_DOWN:
|
|
startY = atTop() ? e.getY() : -1;
|
|
pulling = false;
|
|
break;
|
|
case MotionEvent.ACTION_MOVE:
|
|
if (startY >= 0 && e.getY() - startY > slop && atTop()) {
|
|
pulling = true;
|
|
popped = false;
|
|
if (settle != null) settle.cancel();
|
|
return true;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean onTouchEvent(MotionEvent e) {
|
|
if (!pulling) return false;
|
|
float d = Math.max(0, e.getY() - startY - slop);
|
|
switch (e.getActionMasked()) {
|
|
case MotionEvent.ACTION_MOVE:
|
|
// Rubber band: easy at first, ever stiffer towards reach.
|
|
moveTo(reach * (1 - (float) Math.exp(-d / reach)));
|
|
bubble.progress = Math.min(1f, d / threshold);
|
|
if (!popped && d >= threshold && onRefresh != null) {
|
|
popped = true;
|
|
refreshing = true;
|
|
bubble.pop();
|
|
performHapticFeedback(HapticFeedbackConstants.CONFIRM);
|
|
announceForAccessibility("Checking for messages");
|
|
onRefresh.refresh();
|
|
}
|
|
return true;
|
|
case MotionEvent.ACTION_UP:
|
|
case MotionEvent.ACTION_CANCEL:
|
|
pulling = false;
|
|
if (refreshing) animateTo(hold, false);
|
|
else {
|
|
bubble.progress = 0;
|
|
animateTo(0, true);
|
|
}
|
|
return true;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void setRefreshing(boolean on) {
|
|
if (on || !refreshing) return; // only a pull starts one
|
|
refreshing = false;
|
|
if (pulling) return; // still held: it springs back on release
|
|
bubble.progress = 0;
|
|
animateTo(0, true);
|
|
}
|
|
|
|
boolean isRefreshing() {
|
|
return refreshing;
|
|
}
|
|
|
|
/** The bubble in the gap above the list: grows, stretches, pops, then spins. */
|
|
private static final class Bubble extends View {
|
|
private final PullRefresh owner;
|
|
private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG), shine = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
private final Paint ring = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
private final float r, dp;
|
|
private final RectF oval = new RectF();
|
|
float progress;
|
|
private long poppedAt = -1;
|
|
|
|
Bubble(Context c, PullRefresh owner) {
|
|
super(c);
|
|
this.owner = owner;
|
|
dp = Ui.dp(c, 1);
|
|
r = 20 * dp;
|
|
fill.setColor(Ui.ACCENT);
|
|
shine.setColor(0xCCFFFFFF);
|
|
ring.setColor(Ui.TEXT);
|
|
ring.setStyle(Paint.Style.STROKE);
|
|
ring.setStrokeWidth(3 * dp);
|
|
ring.setStrokeCap(Paint.Cap.ROUND);
|
|
}
|
|
|
|
void pop() {
|
|
poppedAt = System.currentTimeMillis();
|
|
invalidate();
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas c) {
|
|
float gap = owner.gap();
|
|
if (gap < 2 * dp) {
|
|
if (!owner.isRefreshing()) poppedAt = -1;
|
|
return;
|
|
}
|
|
float cx = getWidth() / 2f, cy = gap / 2f;
|
|
long since = poppedAt < 0 ? -1 : System.currentTimeMillis() - poppedAt;
|
|
if (since < 0) {
|
|
// Growing for the first half of the pull, then stretching
|
|
// tall and thin as it nears the mark.
|
|
float grow = Math.min(1f, progress * 2), stretch = Math.max(0f, progress * 2 - 1);
|
|
float w = r * grow * (1 - 0.22f * stretch), h = r * grow * (1 + 0.55f * stretch);
|
|
h = Math.min(h, gap / 2 - 4 * dp);
|
|
if (w <= 0 || h <= 0) return;
|
|
oval.set(cx - w, cy - h, cx + w, cy + h);
|
|
c.drawOval(oval, fill);
|
|
c.drawCircle(cx - w * 0.38f, cy - h * 0.42f, w * 0.2f, shine);
|
|
return;
|
|
}
|
|
if (since < 320) {
|
|
// Pop: a ring bursting outwards, and droplets flying off.
|
|
float t = since / 320f, ease = 1 - (1 - t) * (1 - t);
|
|
int alpha = (int) (255 * (1 - t));
|
|
ring.setAlpha(alpha);
|
|
fill.setAlpha(alpha);
|
|
c.drawCircle(cx, cy, r * (1 + 0.9f * ease), ring);
|
|
for (int i = 0; i < 8; i++) {
|
|
double a = Math.PI * i / 4 + Math.PI / 8;
|
|
float d = r * (1.2f + 1.6f * ease);
|
|
c.drawCircle(cx + (float) Math.cos(a) * d, cy + (float) Math.sin(a) * d, 4 * dp * (1 - 0.6f * t), fill);
|
|
}
|
|
ring.setAlpha(255);
|
|
fill.setAlpha(255);
|
|
postInvalidateOnAnimation();
|
|
return;
|
|
}
|
|
if (owner.isRefreshing()) {
|
|
// Checking: a turning arc.
|
|
float spin = ((since - 320) % 900) / 900f * 360;
|
|
float sr = 16 * dp;
|
|
oval.set(cx - sr, cy - sr, cx + sr, cy + sr);
|
|
c.drawArc(oval, spin, 270, false, ring);
|
|
postInvalidateOnAnimation();
|
|
}
|
|
}
|
|
}
|
|
}
|