Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Merge PR #221: launcher: optional CPU, memory, and storage line

Gathin239709489f8932
diff --cc README.md
index 19ca5c366,40101fb8c..dfb1b46f2
--- a/README.md
+++ b/README.md
@@@ -97,8 -96,8 +97,10 @@@ screenshots pulled from the TV. With th
- **A top shelf** naming the focused app in large type, with an ambient glow in a colour taken from its artwork.
- **The rest of your apps in a grid** that slides up when you press Down.
- **One floating glass highlight** that springs from tile to tile. The focused tile lifts and shows its name.
+- **Three frosted buttons**: Inputs (HDMI ports under the names you gave them in the TV's settings, AV, Live TV), Wi-Fi or Ethernet with the network name, Settings.
+- **A greeting, a thin clock and the date** in [Outfit](https://fonts.google.com/specimen/Outfit), and a matching Clock screensaver.
+ - **Three frosted buttons**: Inputs (HDMI ports, AV, Live TV), Wi-Fi or Ethernet with the network name, Settings.
+ - **A greeting, a thin clock and the date** in [Outfit](https://fonts.google.com/specimen/Outfit), and a matching Clock screensaver. Set `show_stats` in config or a theme for one line under the clock: CPU, free memory and free storage. It stays off unless you turn it on, and it refreshes only while Home is on screen.
- **Themes** that change colours, wallpaper, dock and columns without a rebuild, and **module cards** that let other apps plug a tile in, only with your say-so.
And a TV that stops working against you: no sponsored rows, no "Discover" nags, no analytics
diff --cc launcher/app/src/main/java/com/gathin/tvhome/Config.java
index 5f5176703,e874a2d40..eac8f1302
--- a/launcher/app/src/main/java/com/gathin/tvhome/Config.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Config.java
@@@ -52,14 -52,10 +52,16 @@@ final class Config
final boolean moduleCards;
/** Calmer motion for weak GPUs (config.xml, reduced_motion): stiff spring, short fades, no replay. */
final boolean reducedMotion;
+ /** The focused dock app's name in large type above the dock (config.xml, dock_title). */
+ final boolean dockTitle;
+ /** The dock as its own row: band, no names, parked low (config.xml dock_row). false: one plain grid. */
+ final boolean dockRow;
+ /** Reorder button in the top bar (config.xml reorder_button); the Settings long-press menu offers it either way. */
+ final boolean reorderButton;
final String vpnApp;
final boolean showHomeSettingsShortcut;
+ /** One line under the clock: CPU, free memory, free storage. Off unless config or a theme turns it on. */
+ final boolean showStats;
/** Background colour behind everything, #RRGGBB; black unless a theme says otherwise. */
final int backgroundColor;
/** Time-of-day backdrop, 0 to 100 (config.xml, sky_intensity); 0 keeps the plain background. */
@@@ -78,8 -74,8 +80,8 @@@
private Config(List<String> dockApps, boolean fillDock, List<String> hiddenApps, String liveTvPackage,
String liveTvAction, String sourcePanelService, String sourcePanelAction, String inputAction,
int columns, int rowsBelowDock, boolean animations, boolean advancedEffects,
- boolean shamsiDate, boolean moduleCards, boolean reducedMotion, String vpnApp,
+ boolean shamsiDate, boolean moduleCards, boolean reducedMotion, boolean dockTitle, boolean dockRow, boolean reorderButton, String vpnApp,
- boolean showHomeSettingsShortcut, int backgroundColor, int skyIntensity,
+ boolean showHomeSettingsShortcut, boolean showStats, int backgroundColor, int skyIntensity,
int[] settingsKeycodes, boolean settingsKeyPanel, File wallpaper, boolean overlayApplied,
Festival festival) {
this.dockApps = Collections.unmodifiableList(dockApps);
@@@ -97,11 -93,9 +99,12 @@@
this.shamsiDate = shamsiDate;
this.moduleCards = moduleCards;
this.reducedMotion = reducedMotion;
+ this.dockTitle = dockTitle;
+ this.dockRow = dockRow;
+ this.reorderButton = reorderButton;
this.vpnApp = vpnApp;
this.showHomeSettingsShortcut = showHomeSettingsShortcut;
+ this.showStats = showStats;
this.backgroundColor = backgroundColor;
this.skyIntensity = skyIntensity;
this.settingsKeycodes = settingsKeycodes;
@@@ -133,11 -127,9 +136,12 @@@
boolean shamsi = res.getBoolean(R.bool.shamsi_date);
boolean moduleCards = res.getBoolean(R.bool.module_cards);
boolean reducedMotion = res.getBoolean(R.bool.reduced_motion);
+ boolean dockTitle = res.getBoolean(R.bool.dock_title);
+ boolean dockRow = res.getBoolean(R.bool.dock_row);
+ boolean reorderButton = res.getBoolean(R.bool.reorder_button);
String vpn = res.getString(R.string.vpn_app).trim();
boolean homeSettings = res.getBoolean(R.bool.show_home_settings_shortcut);
+ boolean showStats = res.getBoolean(R.bool.show_stats);
int background = 0xFF000000;
int skyIntensity = res.getInteger(R.integer.sky_intensity);
int[] settingsKeycodes = res.getIntArray(R.array.settings_key_keycodes);
@@@ -164,11 -156,9 +168,12 @@@
shamsi = bool(json, "shamsi_date", shamsi);
moduleCards = bool(json, "module_cards", moduleCards);
reducedMotion = bool(json, "reduced_motion", reducedMotion);
+ dockTitle = bool(json, "dock_title", dockTitle);
+ dockRow = bool(json, "dock_row", dockRow);
+ reorderButton = bool(json, "reorder_button", reorderButton);
vpn = string(json, "vpn_app", vpn);
homeSettings = bool(json, "show_home_settings_shortcut", homeSettings);
+ showStats = bool(json, "show_stats", showStats);
background = color(json, "background_color", background);
skyIntensity = integer(json, "sky_intensity", skyIntensity, 0, 100);
settingsKeycodes = intArray(json, "settings_key_keycodes", settingsKeycodes);
@@@ -192,7 -182,7 +197,7 @@@
return new Config(dock, fillDock, hidden, liveTv, liveTvAction, panelService, panelAction, inputAction,
Math.max(1, columns), Math.max(0, rowsBelow), animations, advancedEffects,
- shamsi, moduleCards, reducedMotion, dockTitle, dockRow, reorderButton, vpn, homeSettings, background,
- shamsi, moduleCards, reducedMotion, vpn, homeSettings, showStats, background,
++ shamsi, moduleCards, reducedMotion, dockTitle, dockRow, reorderButton, vpn, homeSettings, showStats, background,
Math.max(0, Math.min(100, skyIntensity)), settingsKeycodes, settingsKeyPanel, wallpaper, applied,
festival);
}
diff --cc launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
index 8c9068880,966b25b36..f1c8cfa9f
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@@ -1107,6 -841,175 +1119,178 @@@ public class MainActivity extends Activ
else glass.setAlpha(0f);
}
+ // ---- optional system stats (config.xml show_stats) --------------------------------------------
+
+ /** Background reader. Started only while Home is resumed and show_stats is on. */
+ private HandlerThread statsThread;
- private Handler statsBg;
- private final Runnable statsTask = new Runnable() {
- @Override
- public void run() {
- if (statsBg == null) return;
- String line = readStatsLine();
- handler.post(() -> {
- if (showStats && stats != null) stats.setText(line);
- });
- if (statsBg != null) statsBg.postDelayed(this, STATS_MS);
- }
- };
++ /** The current run's handler; a run whose handler is no longer this one stops (volatile: read off-thread). */
++ private volatile Handler statsBg;
+
+ private void startStats() {
+ if (statsThread != null) return;
+ stats.setVisibility(View.VISIBLE);
+ cpuTotalPrev = -1;
+ idlePrev = -1;
+ procStatDead = false;
+ statsThread = new HandlerThread("stats");
+ statsThread.start();
- statsBg = new Handler(statsThread.getLooper());
- statsBg.post(statsTask);
++ // Each start gets its own handler, held by its task: stopStats on the main thread can clear the field
++ // at any moment without the reader tripping over it.
++ Handler bg = new Handler(statsThread.getLooper());
++ statsBg = bg;
++ bg.post(new Runnable() {
++ @Override
++ public void run() {
++ if (statsBg != bg) return;
++ String line = readStatsLine();
++ handler.post(() -> {
++ if (showStats && stats != null && statsBg == bg) stats.setText(line);
++ });
++ if (statsBg == bg) bg.postDelayed(this, STATS_MS);
++ }
++ });
+ }
+
+ private void stopStats() {
+ if (statsThread == null) return;
+ statsBg.removeCallbacksAndMessages(null);
+ statsThread.quit();
+ statsThread = null;
+ statsBg = null;
+ stats.setVisibility(View.GONE);
+ }
+
+ /** One quiet line, e.g. "CPU 8% · Memory 430 MB free · Storage 3.1 GB free". Runs off the main thread. */
+ private String readStatsLine() {
+ int cpu = cpuPercent();
+ if (cpu < 0) {
+ try {
+ Thread.sleep(200); // second sample, so the first line already has a CPU figure
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return "";
+ }
+ cpu = cpuPercent();
+ }
+ StringBuilder line = new StringBuilder(cpu >= 0
+ ? getString(R.string.stats_cpu, cpu) : getString(R.string.stats_cpu_unknown));
+ long mem = memAvailableMb();
+ if (mem >= 0) line.append(" · ").append(getString(R.string.stats_memory, mem));
+ String free = formatGb(Environment.getDataDirectory().getFreeSpace());
+ if (!free.isEmpty()) line.append(" · ").append(getString(R.string.stats_storage, free));
+ return line.toString();
+ }
+
+ // -1: need another sample. -2: this source cannot be read (Android 9 hides /proc/stat from apps).
+ private static final int CPU_UNREADABLE = -2;
+ private long cpuTotalPrev = -1, cpuIdlePrev = -1;
+ private boolean procStatDead;
+ private long idlePrev = -1, wallPrev = -1;
+
+ private int cpuPercent() {
+ if (!procStatDead) {
+ int fromStat = cpuFromProcStat();
+ if (fromStat != CPU_UNREADABLE) return fromStat;
+ procStatDead = true;
+ idlePrev = -1;
+ }
+ return cpuFromIdle();
+ }
+
+ private int cpuFromProcStat() {
+ String s = readFirstLine("/proc/stat");
+ if (s == null || !s.startsWith("cpu")) return CPU_UNREADABLE;
+ String[] t = s.trim().split("\\s+");
+ if (t.length < 5) return CPU_UNREADABLE;
+ try {
+ long user = Long.parseLong(t[1]), nice = Long.parseLong(t[2]), sys = Long.parseLong(t[3]);
+ long idle = Long.parseLong(t[4]);
+ long iowait = t.length > 5 ? Long.parseLong(t[5]) : 0;
+ long irq = t.length > 6 ? Long.parseLong(t[6]) : 0;
+ long soft = t.length > 7 ? Long.parseLong(t[7]) : 0;
+ long total = user + nice + sys + idle + iowait + irq + soft;
+ long idleAll = idle + iowait;
+ if (cpuTotalPrev < 0) {
+ cpuTotalPrev = total;
+ cpuIdlePrev = idleAll;
+ return -1;
+ }
+ long dt = total - cpuTotalPrev, di = idleAll - cpuIdlePrev;
+ cpuTotalPrev = total;
+ cpuIdlePrev = idleAll;
+ if (dt <= 0) return -1;
+ int busy = (int) Math.round((dt - di) * 100.0 / dt);
+ return Math.max(0, Math.min(100, busy));
+ } catch (NumberFormatException e) {
+ return CPU_UNREADABLE;
+ }
+ }
+
+ /** Busy percent from cpuidle time (microseconds) across the cores. -1 until the second sample. */
+ private int cpuFromIdle() {
+ long idle = 0;
+ int cores = 0;
+ for (int i = 0; i < 8; i++) {
+ boolean any = false;
+ for (int state = 0; state < 8; state++) {
+ String t = readFirstLine("/sys/devices/system/cpu/cpu" + i
+ + "/cpuidle/state" + state + "/time");
+ if (t == null) break;
+ try {
+ idle += Long.parseLong(t.trim());
+ any = true;
+ } catch (NumberFormatException e) {
+ break;
+ }
+ }
+ if (!any) break;
+ cores++;
+ }
+ if (cores == 0) return -1;
+ long wall = SystemClock.elapsedRealtime() * 1000L;
+ if (idlePrev < 0) {
+ idlePrev = idle;
+ wallPrev = wall;
+ return -1;
+ }
+ long di = idle - idlePrev;
+ long dw = (wall - wallPrev) * cores;
+ idlePrev = idle;
+ wallPrev = wall;
+ if (dw <= 0) return -1;
+ int busy = (int) Math.round((dw - di) * 100.0 / dw);
+ return Math.max(0, Math.min(100, busy));
+ }
+
+ private long memAvailableMb() {
+ try (java.io.BufferedReader r = new java.io.BufferedReader(new java.io.FileReader("/proc/meminfo"))) {
+ String line;
+ while ((line = r.readLine()) != null) {
+ if (line.startsWith("MemAvailable:")) {
+ String[] t = line.split("\\s+");
+ return t.length >= 2 ? Long.parseLong(t[1]) / 1024 : -1;
+ }
+ }
+ } catch (IOException | NumberFormatException ignored) {
+ }
+ return -1;
+ }
+
+ private static String readFirstLine(String path) {
+ try (java.io.BufferedReader r = new java.io.BufferedReader(new java.io.FileReader(path))) {
+ return r.readLine();
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ private static String formatGb(long bytes) {
+ if (bytes <= 0) return "";
+ double gb = bytes / (1024.0 * 1024 * 1024);
+ return gb >= 10 ? String.format(Locale.US, "%.0f GB", gb)
+ : String.format(Locale.US, "%.1f GB", gb);
+ }
+
// ---- navigation ------------------------------------------------------------------------------
@Override
diff --cc launcher/app/src/main/res/values-zh-rCN/strings.xml
index 3ce33cfc5,bc871c98b..3fe279c6b
--- a/launcher/app/src/main/res/values-zh-rCN/strings.xml
+++ b/launcher/app/src/main/res/values-zh-rCN/strings.xml
@@@ -33,8 -33,9 +33,12 @@@
<string name="not_available">此电视不支持该功能</string>
<string name="cant_open">无法打开 %1$s</string>
<string name="date_format">M月d日 EEEE</string>
+ <string name="stats_cpu">CPU %1$d%%</string>
+ <string name="stats_cpu_unknown">CPU —</string>
+ <string name="stats_memory">内存剩余 %1$d MB</string>
+ <string name="stats_storage">存储剩余 %1$s</string>
<string name="dream_name">时钟</string>
+ <string name="reorder">调整应用顺序</string>
+ <string name="reorder_hint">按确定选中应用,方向键移动,按返回放下。再按一次返回完成。</string>
+ <string name="reorder_not_saved">无法保存新顺序</string>
</resources>