package me.teafry.voipmessages; import android.app.Activity; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RadialGradient; import android.graphics.Shader; import android.view.HapticFeedbackConstants; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; /** * Hold the paperclip and the composer swells upward into a bubble while * camera, photo and microphone (no microphone on a Google Voice chat, which * can't send voice notes) float out from under the clip, jostle each * other apart and settle above and to the right of it. The thumb stays down: * a soft spotlight follows it inside the bubble, leans toward whichever icon * it's near (so it needn't be exact), and that icon gets a glowing blue ring. * Let go on one to attach that; let go on none and they all slip back under * the clip as the bubble sinks. * *

The way out is a little physics: each icon is sprung to a home spot and * the icons push each other away, launched a beat apart in nearly the same * direction so they bump and fan out. The bubble's height just chases the * icons. The way back is a plain ease-in, since they're only going away. * *

Lives on the window's decor while open; the paperclip's own touch * listener feeds it ({@link #move}, {@link #release}) since the touch began there. * After a quick tap ({@link #stay}) it takes the next touch itself instead. */ final class AttachBubble extends View { interface OnPick { void pick(int which); } static final int CAMERA = 0, PHOTO = 1, VOICE = 2; private static final Glyph.Kind[] GLYPHS = {Glyph.Kind.CAMERA, Glyph.Kind.PHOTO, Glyph.Kind.MIC}; private static final String[] LABELS = {"Camera", "Photo", "Voice note"}; // Physics, in dp and seconds (tuned offline: fanned out by ~180 ms, at rest by ~500 ms). /** Where each icon comes to rest: degrees above the horizontal, and distance from the clip. */ private static final float[] HOME_DEG = {90, 62, 34}; private static final float HOME_DIST = 178; /** When each leaves the clip (s) and which way it's thrown (degrees). */ private static final float[] LAUNCH_S = {0, 0.03f, 0.06f}, LAUNCH_DEG = {70, 62, 54}; private static final float LAUNCH_SPEED = 700, SPRING = 260, DAMPING = 22; /** Push between icons, softened so two near each other aren't flung. */ private static final float REPEL = 2.4e6f, REPEL_SOFT = 36; private static final float RETREAT_S = 0.2f; private final View anchor, base; private final OnPick onPick; /** How many icons: the first two, or all three with the microphone. */ private final int n; private final float dp; private final float iconR, snapR, deadR, spotR, margin, corner, fillet; private final Paint panel = new Paint(Paint.ANTI_ALIAS_FLAG), disk = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint glyph = new Paint(Paint.ANTI_ALIAS_FLAG), label = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint spot = new Paint(Paint.ANTI_ALIAS_FLAG), scrim = new Paint(); private final Paint ring = new Paint(Paint.ANTI_ALIAS_FLAG), glow = new Paint(Paint.ANTI_ALIAS_FLAG); private final RadialGradient spotShader, glowShader; private final Matrix m = new Matrix(); private final Path shape = new Path(); private float cx, cy, top; // the clip's centre and the composer's top edge, in px here private float width; // the bubble's right edge private float tx, ty; // the thumb private boolean touched; // the thumb has moved since the bubble opened private boolean sticky; // opened by a tap: the next touch picks or closes // Icon state, in dp from the clip's centre with y up. private final float[] px = new float[3], py = new float[3], vx = new float[3], vy = new float[3]; private final float[] fromX = new float[3], fromY = new float[3]; private float age; // s since opening private float retreat = -1; // s since letting go on nothing, or -1 private boolean picked, settled, gone; private float rise; // how far the bubble stands above the composer, px private long lastFrame; private int selected = -1; private float ringT; // the selection ring's pop, 0 .. 1 AttachBubble(Activity a, View anchor, View base, boolean voice, OnPick onPick) { super(a); n = voice ? 3 : 2; this.anchor = anchor; this.base = base; this.onPick = onPick; dp = a.getResources().getDisplayMetrics().density; iconR = 36 * dp; snapR = 92 * dp; deadR = 56 * dp; spotR = 80 * dp; margin = 18 * dp; corner = 84 * dp; fillet = 28 * dp; panel.setColor(Ui.PANEL); scrim.setColor(0x33000000); disk.setColor(Ui.INPUT); glyph.setColor(Ui.TEXT); glyph.setTextAlign(Paint.Align.CENTER); label.setTextSize(20 * dp); label.setTextAlign(Paint.Align.CENTER); label.setColor(Ui.TEXT); label.setFakeBoldText(true); ring.setStyle(Paint.Style.STROKE); ring.setStrokeWidth(4 * dp); ring.setColor(0xFF4F8BFF); spotShader = new RadialGradient(0, 0, spotR, new int[] {0x55FFFFFF, 0x1CFFFFFF, 0x00FFFFFF}, new float[] {0, 0.45f, 1}, Shader.TileMode.CLAMP); spot.setShader(spotShader); float g = iconR + 22 * dp; glowShader = new RadialGradient(0, 0, g, new int[] {0x002C6BED, 0x002C6BED, 0xAA2C6BED, 0x002C6BED}, new float[] {0, (iconR - 2 * dp) / g, (iconR + 4 * dp) / g, 1}, Shader.TileMode.CLAMP); glow.setShader(glowShader); } /** Put it on the window and start the icons on their way out. */ void show(Activity a) { ViewGroup decor = (ViewGroup) a.getWindow().getDecorView(); decor.addView(this, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); int[] at = new int[2], me = new int[2]; decor.getLocationOnScreen(me); anchor.getLocationOnScreen(at); cx = at[0] - me[0] + anchor.getWidth() / 2f; cy = at[1] - me[1] + anchor.getHeight() / 2f; base.getLocationOnScreen(at); top = at[1] - me[1]; // Wide enough for the right-most icon at rest, plus room around it. width = cx + (float) Math.cos(Math.toRadians(HOME_DEG[n - 1])) * HOME_DIST * dp + iconR + margin; tx = cx; ty = cy; performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); invalidate(); } private float iconX(int i) { return cx + px[i] * dp; } private float iconY(int i) { return cy - py[i] * dp; } /** How far out of the clip an icon is, 0 .. 1: it grows as it emerges. */ private float emerged(int i) { return Math.min(1, (float) Math.hypot(px[i], py[i]) / (HOME_DIST * 0.6f)); } private void step(float dt) { if (retreat >= 0) { retreat += dt; float k = Math.min(1, retreat / RETREAT_S), e = k * k; // ease in: away, not bouncing for (int i = 0; i < n; i++) { px[i] = fromX[i] * (1 - e); py[i] = fromY[i] * (1 - e); } if (k >= 1) gone = true; return; } // Substeps keep the springs steady whatever the frame rate. for (float left = dt; left > 0; left -= 1 / 240f) { float h = Math.min(left, 1 / 240f); float before = age; age += h; float speed = 0; for (int i = 0; i < n; i++) { if (age < LAUNCH_S[i]) continue; if (before < LAUNCH_S[i] || before == 0) { // just launched double a = Math.toRadians(LAUNCH_DEG[i]); vx[i] = (float) Math.cos(a) * LAUNCH_SPEED; vy[i] = (float) Math.sin(a) * LAUNCH_SPEED; } double home = Math.toRadians(HOME_DEG[i]); float fx = SPRING * ((float) Math.cos(home) * HOME_DIST - px[i]) - DAMPING * vx[i]; float fy = SPRING * ((float) Math.sin(home) * HOME_DIST - py[i]) - DAMPING * vy[i]; for (int j = 0; j < n; j++) { if (j == i || age < LAUNCH_S[j]) continue; float dx = px[i] - px[j], dy = py[i] - py[j]; float d = Math.max(0.001f, (float) Math.hypot(dx, dy)); float push = REPEL / (d * d + REPEL_SOFT * REPEL_SOFT); fx += push * dx / d; fy += push * dy / d; } vx[i] += fx * h; vy[i] += fy * h; speed = Math.max(speed, (float) Math.hypot(vx[i], vy[i])); } for (int i = 0; i < n; i++) { if (age < LAUNCH_S[i]) continue; px[i] += vx[i] * h; py[i] += vy[i] * h; } if (age > 0.3f && speed < 4) settled = true; } } /** The thumb, in screen coordinates. */ void move(float rawX, float rawY) { if (retreat >= 0 || picked) return; int[] me = new int[2]; getLocationOnScreen(me); tx = rawX - me[0]; ty = rawY - me[1]; if (Math.hypot(tx - cx, ty - cy) > 12 * dp) touched = true; invalidate(); } /** Nearest icon within reach of the thumb, once the thumb has left the clip. */ private void updateSelection() { int pick = -1; if (touched && Math.hypot(tx - cx, ty - cy) > deadR) { float best = snapR; for (int i = 0; i < n; i++) { if (emerged(i) < 0.6f) continue; float d = (float) Math.hypot(tx - iconX(i), ty - iconY(i)); if (d < best) { best = d; pick = i; } } } if (pick == selected) return; selected = pick; ringT = 0; if (pick >= 0) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK); } boolean dragged() { return touched; } /** Opened by a quick tap: stay out until the next touch. */ void stay() { sticky = true; } @Override public boolean onTouchEvent(MotionEvent e) { if (!sticky) return true; switch (e.getActionMasked()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: move(e.getRawX(), e.getRawY()); break; case MotionEvent.ACTION_UP: release(); break; case MotionEvent.ACTION_CANCEL: dismiss(); break; default: break; } return true; } /** The thumb came up: attach what it was on, or tuck everything away. */ void release() { if (retreat >= 0 || picked) return; updateSelection(); int pick = selected; if (pick < 0) { dismiss(); return; } // A quick fade, then the chosen thing opens. picked = true; animate().alpha(0).setDuration(140).withEndAction(() -> { remove(); onPick.pick(pick); }).start(); } /** Icons back under the clip, bubble sinking with them. */ void dismiss() { if (retreat >= 0 || picked) return; selected = -1; retreat = 0; for (int i = 0; i < n; i++) { fromX[i] = px[i]; fromY[i] = py[i]; } invalidate(); } private void remove() { ViewGroup parent = (ViewGroup) getParent(); if (parent != null) parent.removeView(this); } @Override protected void onDraw(Canvas c) { long now = System.nanoTime(); float dt = lastFrame == 0 ? 0 : Math.min(1 / 30f, (now - lastFrame) / 1e9f); lastFrame = now; if (!picked) step(dt); if (gone) { remove(); return; } if (!picked && retreat < 0) updateSelection(); // The bubble stands as tall as the icons need, catching up quickly. float need = 0; for (int i = 0; i < n; i++) need = Math.max(need, top - (iconY(i) - iconR * emerged(i) - margin)); rise += (need - rise) * (1 - (float) Math.exp(-dt * 28)); if (retreat >= 0) rise = Math.min(rise, need); // Everything above the composer dims a little; the bubble is the composer, grown. c.drawRect(0, 0, getWidth(), top, scrim); if (rise > 1) { buildShape(); c.drawPath(shape, panel); } // The spotlight: under the thumb, leaning toward a chosen icon, kept inside the bubble. if (touched && retreat < 0 && !picked && rise > 1) { float sx = tx, sy = ty; if (selected >= 0) { sx += (iconX(selected) - sx) * 0.4f; sy += (iconY(selected) - sy) * 0.4f; } c.save(); c.clipPath(shape); m.setTranslate(sx, sy); spotShader.setLocalMatrix(m); c.drawCircle(sx, sy, spotR, spot); c.restore(); } // The icons, then the clip drawn again on top so they come out from under it. if (selected >= 0) ringT = Math.min(1, ringT + dt / 0.16f); float pop = 1 - (1 - ringT) * (1 - ringT); for (int i = 0; i < n; i++) { float s = emerged(i); if (s <= 0.01f) continue; float x = iconX(i), y = iconY(i); if (i == selected) { m.setTranslate(x, y); glowShader.setLocalMatrix(m); glow.setAlpha((int) (255 * pop)); c.drawCircle(x, y, iconR + 22 * dp, glow); } c.drawCircle(x, y, iconR * s, disk); if (i == selected) { ring.setAlpha((int) (255 * pop)); c.drawCircle(x, y, (iconR + 4 * dp) * (0.8f + 0.2f * pop), ring); } glyph.setAlpha((int) (255 * s)); c.save(); c.scale(s, s, x, y); Glyph.draw(c, GLYPHS[i], x, y, 44 * dp, glyph); c.restore(); } int[] at = new int[2], me = new int[2]; anchor.getLocationOnScreen(at); getLocationOnScreen(me); c.save(); c.translate(at[0] - me[0], at[1] - me[1]); anchor.draw(c); c.restore(); // What letting go will do, above the icon. if (selected >= 0 && retreat < 0) { label.setAlpha((int) (255 * pop)); c.drawText(LABELS[selected], iconX(selected), iconY(selected) - iconR - 14 * dp, label); } if (!settled || retreat >= 0 || (selected >= 0 && ringT < 1) || Math.abs(need - rise) > 0.5f) postInvalidateOnAnimation(); } /** * The composer's top edge bulging up over the clip: square to the screen's * left edge, round at the top, and flowing back into the edge on the right. */ private void buildShape() { float t = top - rise, r = Math.min(corner, rise), f = Math.min(fillet, rise); shape.rewind(); shape.moveTo(0, top + 1); shape.lineTo(0, t); shape.lineTo(width - r, t); shape.quadTo(width, t, width, t + r); shape.lineTo(width, top - f); shape.quadTo(width, top, width + f, top); shape.lineTo(width + f, top + 1); shape.close(); } }