233 lines
7.4 KiB
Java
233 lines
7.4 KiB
Java
package tv.luma.launcher;
|
|
|
|
import android.animation.ValueAnimator;
|
|
import android.content.Context;
|
|
import android.graphics.*;
|
|
import android.view.*;
|
|
import android.view.animation.DecelerateInterpolator;
|
|
import android.widget.*;
|
|
|
|
/** Real focusable view: native D-pad navigation and accessibility labels. */
|
|
final class AppTile extends LinearLayout {
|
|
private final ArtView art;
|
|
private final TextView label;
|
|
private boolean motion = true;
|
|
private String labelMode = "always";
|
|
// One small shared texture replaces live elevation-shadow geometry on every focus frame.
|
|
private static Bitmap shadowMask;
|
|
private final Paint shadowPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
|
|
private final RectF shadowBounds = new RectF();
|
|
private float shadowAmount;
|
|
private ValueAnimator shadowAnimation;
|
|
|
|
interface Focus {
|
|
void changed(boolean hasFocus);
|
|
}
|
|
|
|
AppTile(Context c, String name, int width, boolean compact) {
|
|
super(c);
|
|
setOrientation(VERTICAL);
|
|
setGravity(Gravity.CENTER_HORIZONTAL);
|
|
setFocusable(true);
|
|
setClickable(true);
|
|
setClipChildren(false);
|
|
setClipToPadding(false);
|
|
setWillNotDraw(false);
|
|
if (shadowMask == null) {
|
|
shadowMask = Bitmap.createBitmap(192, 122, Bitmap.Config.ARGB_8888);
|
|
Canvas canvas = new Canvas(shadowMask);
|
|
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
p.setColor(Color.WHITE);
|
|
p.setShadowLayer(8, 0, 4, Color.WHITE);
|
|
canvas.drawRoundRect(new RectF(16, 16, 176, 106), 12, 12, p);
|
|
}
|
|
setContentDescription(name);
|
|
setPadding(Ui.dp(c, 5), Ui.dp(c, 7), Ui.dp(c, 5), Ui.dp(c, 7));
|
|
art = new ArtView(c, name);
|
|
int height = Math.round((width - 10) * 9f / 16f);
|
|
addView(art, new LinearLayout.LayoutParams(Ui.dp(c, width - 10), Ui.dp(c, height)));
|
|
label = Ui.text(c, name, compact ? 11 : 12, Ui.MUTED);
|
|
label.setSingleLine(true);
|
|
label.setEllipsize(android.text.TextUtils.TruncateAt.END);
|
|
label.setGravity(Gravity.CENTER);
|
|
label.setPadding(0, Ui.dp(c, 9), 0, 0);
|
|
addView(label, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, Ui.dp(c, 28)));
|
|
setOnFocusChangeListener(
|
|
(v, f) -> {
|
|
animate().cancel();
|
|
animate()
|
|
.scaleX(f ? 1.065f : 1f)
|
|
.scaleY(f ? 1.065f : 1f)
|
|
.setDuration(motion ? 160 : 0)
|
|
.setInterpolator(new DecelerateInterpolator())
|
|
.start();
|
|
label.setTextColor(f ? Ui.WHITE : Ui.MUTED);
|
|
updateLabel();
|
|
art.focus = f;
|
|
if (shadowAnimation != null) shadowAnimation.cancel();
|
|
if (motion) {
|
|
shadowAnimation = ValueAnimator.ofFloat(shadowAmount, f ? 1 : 0);
|
|
shadowAnimation.setDuration(160);
|
|
shadowAnimation.addUpdateListener(
|
|
a -> {
|
|
shadowAmount = (float) a.getAnimatedValue();
|
|
invalidate();
|
|
});
|
|
shadowAnimation.start();
|
|
} else {
|
|
shadowAmount = f ? 1 : 0;
|
|
invalidate();
|
|
}
|
|
art.invalidate();
|
|
if (focus != null) focus.changed(f);
|
|
});
|
|
}
|
|
|
|
private Focus focus;
|
|
|
|
void onFocused(Focus f) {
|
|
focus = f;
|
|
}
|
|
|
|
void setMotion(boolean enabled) {
|
|
motion = enabled;
|
|
}
|
|
|
|
void setLabelMode(String mode) {
|
|
labelMode = mode;
|
|
updateLabel();
|
|
}
|
|
|
|
private void updateLabel() {
|
|
label.setVisibility(
|
|
labelMode.equals("never")
|
|
? View.GONE
|
|
: labelMode.equals("focused") && !hasFocus() ? View.INVISIBLE : View.VISIBLE);
|
|
}
|
|
|
|
void setAccent(int color) {
|
|
shadowPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
|
|
invalidate();
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
if (shadowAmount <= 0 || art.getWidth() == 0) return;
|
|
float inset = Ui.dp(getContext(), 2);
|
|
float scale = (art.getWidth() - 2 * inset) / 160f;
|
|
float left = art.getLeft() + inset - 16 * scale;
|
|
float top = art.getTop() + inset - 16 * scale;
|
|
shadowBounds.set(left, top, left + 192 * scale, top + 122 * scale);
|
|
shadowPaint.setAlpha(Math.round(150 * shadowAmount));
|
|
canvas.drawBitmap(shadowMask, null, shadowBounds, shadowPaint);
|
|
}
|
|
|
|
void stopAnimations() {
|
|
animate().cancel();
|
|
art.animate().cancel();
|
|
if (shadowAnimation != null) shadowAnimation.cancel();
|
|
shadowAmount = hasFocus() ? 1 : 0;
|
|
}
|
|
|
|
void bitmap(Bitmap b) {
|
|
art.bitmap = b;
|
|
art.invalidate();
|
|
}
|
|
|
|
boolean hasImage() {
|
|
return art.bitmap != null && !art.bitmap.isRecycled();
|
|
}
|
|
|
|
void progress(float value) {
|
|
art.progress = value;
|
|
art.invalidate();
|
|
}
|
|
|
|
private static final class ArtView extends View {
|
|
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
|
|
final RectF rect = new RectF();
|
|
final Path clip = new Path();
|
|
Bitmap bitmap;
|
|
boolean focus;
|
|
String fallback;
|
|
float progress;
|
|
|
|
ArtView(Context c, String s) {
|
|
super(c);
|
|
fallback = s;
|
|
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
|
|
setElevation(0);
|
|
setOutlineProvider(
|
|
new ViewOutlineProvider() {
|
|
@Override
|
|
public void getOutline(View view, Outline outline) {
|
|
outline.setRoundRect(
|
|
2,
|
|
2,
|
|
Math.max(3, view.getWidth() - 2),
|
|
Math.max(3, view.getHeight() - 2),
|
|
Ui.dp(c, 12));
|
|
}
|
|
});
|
|
}
|
|
|
|
protected void onSizeChanged(int w, int h, int ow, int oh) {
|
|
rect.set(
|
|
Ui.dp(getContext(), 2),
|
|
Ui.dp(getContext(), 2),
|
|
w - Ui.dp(getContext(), 2),
|
|
h - Ui.dp(getContext(), 2));
|
|
clip.reset();
|
|
clip.addRoundRect(rect, Ui.dp(getContext(), 12), Ui.dp(getContext(), 12), Path.Direction.CW);
|
|
}
|
|
|
|
protected void onDraw(Canvas c) {
|
|
c.save();
|
|
c.clipPath(clip);
|
|
p.setColor(0xFF1B263B);
|
|
c.drawRect(rect, p);
|
|
if (bitmap != null) {
|
|
float scale =
|
|
Math.max(rect.width() / bitmap.getWidth(), rect.height() / bitmap.getHeight());
|
|
float w = bitmap.getWidth() * scale, h = bitmap.getHeight() * scale;
|
|
c.drawBitmap(
|
|
bitmap,
|
|
null,
|
|
new RectF(
|
|
rect.centerX() - w / 2,
|
|
rect.centerY() - h / 2,
|
|
rect.centerX() + w / 2,
|
|
rect.centerY() + h / 2),
|
|
p);
|
|
} else {
|
|
p.setColor(Ui.WHITE);
|
|
p.setTextSize(Ui.dp(getContext(), 17));
|
|
p.setTypeface(Typeface.create("sans-serif-medium", 0));
|
|
p.setTextAlign(Paint.Align.CENTER);
|
|
c.drawText(
|
|
fallback.length() > 15 ? fallback.substring(0, 14) + "…" : fallback,
|
|
rect.centerX(),
|
|
rect.centerY() + Ui.dp(getContext(), 5),
|
|
p);
|
|
}
|
|
if (progress > 0) {
|
|
p.setColor(0xA0080D18);
|
|
c.drawRect(rect.left, rect.bottom - Ui.dp(getContext(), 4), rect.right, rect.bottom, p);
|
|
p.setColor(Ui.GOLD);
|
|
c.drawRect(
|
|
rect.left,
|
|
rect.bottom - Ui.dp(getContext(), 4),
|
|
rect.left + rect.width() * progress,
|
|
rect.bottom,
|
|
p);
|
|
}
|
|
c.restore();
|
|
p.setStyle(Paint.Style.STROKE);
|
|
p.setStrokeWidth(Ui.dp(getContext(), focus ? 1.5f : 1));
|
|
p.setColor(focus ? 0xDBF4E8D8 : 0x26FFFFFF);
|
|
c.drawRoundRect(rect, Ui.dp(getContext(), 12), Ui.dp(getContext(), 12), p);
|
|
p.setStyle(Paint.Style.FILL);
|
|
}
|
|
}
|
|
}
|