|
| 1 | +package io.github.federicomameli1.sugarglass; |
| 2 | + |
| 3 | +import android.accessibilityservice.AccessibilityService; |
| 4 | +import android.app.KeyguardManager; |
| 5 | +import android.content.BroadcastReceiver; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.IntentFilter; |
| 9 | +import android.graphics.Bitmap; |
| 10 | +import android.graphics.Canvas; |
| 11 | +import android.graphics.Paint; |
| 12 | +import android.graphics.PixelFormat; |
| 13 | +import android.graphics.Typeface; |
| 14 | +import android.os.Handler; |
| 15 | +import android.os.Looper; |
| 16 | +import android.os.PowerManager; |
| 17 | +import android.view.Gravity; |
| 18 | +import android.view.WindowManager; |
| 19 | +import android.view.accessibility.AccessibilityEvent; |
| 20 | +import android.widget.ImageView; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * Experimental, off unless turned on in Accessibility: a small pill with the value and arrow at the bottom of the lock |
| 26 | + * screen. Apps cannot draw there; an accessibility overlay can. It reads nothing on screen |
| 27 | + * (no window content in its config), it only draws, and it never takes touches. |
| 28 | + */ |
| 29 | +public class LockPill extends AccessibilityService { |
| 30 | + |
| 31 | + // ponytail: polls the lock state while shown, since HyperOS tells apps about a fingerprint unlock ~0.7 s |
| 32 | + // late. Only while the pill is up (screen on, locked), so it costs nothing the rest of the time |
| 33 | + private static final long TICK_MS = 100; |
| 34 | + |
| 35 | + private final Handler handler = new Handler(Looper.getMainLooper()); |
| 36 | + private ImageView view; |
| 37 | + private String drawn = ""; |
| 38 | + private boolean pin; // the PIN pad is up: the pill would sit over it |
| 39 | + private boolean covered; // another app is shown over the lock screen |
| 40 | + |
| 41 | + private final Runnable tick = new Runnable() { |
| 42 | + @Override |
| 43 | + public void run() { |
| 44 | + check(); |
| 45 | + if (view != null) handler.postDelayed(this, TICK_MS); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + private final BroadcastReceiver screen = new BroadcastReceiver() { |
| 50 | + @Override |
| 51 | + public void onReceive(Context context, Intent intent) { |
| 52 | + pin = false; |
| 53 | + covered = false; |
| 54 | + check(); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void onServiceConnected() { |
| 60 | + IntentFilter f = new IntentFilter(Intent.ACTION_SCREEN_ON); |
| 61 | + f.addAction(Intent.ACTION_SCREEN_OFF); |
| 62 | + f.addAction(Intent.ACTION_USER_PRESENT); |
| 63 | + registerReceiver(screen, f); |
| 64 | + GlucoseService.pill = true; |
| 65 | + check(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onAccessibilityEvent(AccessibilityEvent event) { |
| 70 | + CharSequence from = event.getPackageName(); |
| 71 | + if (from == null || getPackageName().contentEquals(from)) return; // its own window showing up |
| 72 | + boolean system = "com.android.systemui".contentEquals(from); |
| 73 | + if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 74 | + // Something else over the lock screen (camera, a side panel app): out of its way. |
| 75 | + // Only the package name is read, never what the window says |
| 76 | + covered = !system; |
| 77 | + pin = false; |
| 78 | + } else if (system) { |
| 79 | + // On HyperOS the PIN pad shows as focus moving to its text field |
| 80 | + pin = "android.widget.EditText".contentEquals(event.getClassName()); |
| 81 | + } |
| 82 | + check(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * On the lock screen and not yet unlocked. A fingerprint or PIN makes the device unlocked before the |
| 87 | + * lock screen finishes going away, so with a secure lock that is the earlier, better signal. |
| 88 | + */ |
| 89 | + private boolean locked() { |
| 90 | + KeyguardManager k = getSystemService(KeyguardManager.class); |
| 91 | + return k.isKeyguardLocked() && (!k.isDeviceSecure() || k.isDeviceLocked()) |
| 92 | + && getSystemService(PowerManager.class).isInteractive(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onInterrupt() { |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onDestroy() { |
| 101 | + unregisterReceiver(screen); |
| 102 | + GlucoseService.pill = false; |
| 103 | + hide(); |
| 104 | + super.onDestroy(); |
| 105 | + } |
| 106 | + |
| 107 | + /** Shown only while the screen is on and locked, and there is something to show. */ |
| 108 | + private void check() { |
| 109 | + boolean locked = locked(); |
| 110 | + List<Glucose.Reading> r = GlucoseService.readings; |
| 111 | + if (!locked || pin || covered || r.isEmpty()) { |
| 112 | + hide(); |
| 113 | + return; |
| 114 | + } |
| 115 | + long now = System.currentTimeMillis(); |
| 116 | + Glucose.Reading last = r.get(0); |
| 117 | + boolean hidden = Glucose.hidden(last, now); // same rule as the widget: an old number never looks current |
| 118 | + Glucose.Config c = GlucoseService.config(this); |
| 119 | + String value = hidden ? "---" : Glucose.value(last.sgv, c); |
| 120 | + if (GlucoseService.demo(this)) value = "DEMO " + value; // made-up readings must never pass for real ones |
| 121 | + String direction = hidden || Glucose.stale(last, now) ? "" : last.direction; |
| 122 | + int hue = GlucoseService.color(this, last, now, c); |
| 123 | + String key = value + direction + hue; |
| 124 | + if (view == null) { |
| 125 | + view = new ImageView(this); |
| 126 | + WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 127 | + WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, |
| 128 | + WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY, |
| 129 | + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
| 130 | + | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, |
| 131 | + PixelFormat.TRANSLUCENT); |
| 132 | + lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; |
| 133 | + lp.y = Math.round(34 * getResources().getDisplayMetrics().density); // level with torch and camera |
| 134 | + getSystemService(WindowManager.class).addView(view, lp); |
| 135 | + drawn = ""; |
| 136 | + handler.postDelayed(tick, TICK_MS); |
| 137 | + } |
| 138 | + if (!key.equals(drawn)) { |
| 139 | + view.setImageBitmap(pill(value, direction, hidden ? getColor(R.color.stale) : hue)); |
| 140 | + view.setContentDescription(value + " " + Glucose.arrow(direction)); |
| 141 | + drawn = key; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void hide() { |
| 146 | + handler.removeCallbacks(tick); |
| 147 | + if (view == null) return; |
| 148 | + getSystemService(WindowManager.class).removeView(view); |
| 149 | + view = null; |
| 150 | + } |
| 151 | + |
| 152 | + /** Dark glass pill, white value, arrow in the range colour. */ |
| 153 | + private Bitmap pill(String value, String direction, int hue) { |
| 154 | + float d = getResources().getDisplayMetrics().density, h = 40 * d, pad = 16 * d, gap = 6 * d; |
| 155 | + Paint text = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 156 | + text.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL)); |
| 157 | + text.setTextSize(22 * d); |
| 158 | + text.setColor(0xFFFFFFFF); |
| 159 | + float arrow = 20 * d, arrowW = Art.arrowWidth(direction, arrow); |
| 160 | + float valueW = text.measureText(value), w = pad * 2 + valueW + (arrowW > 0 ? gap + arrowW : 0); |
| 161 | + Bitmap b = Bitmap.createBitmap(Math.round(w), Math.round(h), Bitmap.Config.ARGB_8888); |
| 162 | + Canvas canvas = new Canvas(b); |
| 163 | + Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 164 | + fill.setColor(0x73000000); |
| 165 | + canvas.drawRoundRect(0, 0, w, h, h / 2, h / 2, fill); |
| 166 | + Paint edge = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 167 | + edge.setStyle(Paint.Style.STROKE); |
| 168 | + edge.setStrokeWidth(1 * d); |
| 169 | + edge.setColor(0x47FFFFFF); |
| 170 | + canvas.drawRoundRect(d / 2, d / 2, w - d / 2, h - d / 2, h / 2, h / 2, edge); |
| 171 | + Paint.FontMetrics m = text.getFontMetrics(); |
| 172 | + canvas.drawText(value, pad, h / 2 - (m.ascent + m.descent) / 2, text); |
| 173 | + Art.arrow(canvas, direction, pad + valueW + gap + arrowW / 2, h / 2, arrow, hue); |
| 174 | + return b; |
| 175 | + } |
| 176 | +} |
0 commit comments