Skip to content

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

Add-on: Sources

מקס חיים26da8fdee55e
diff --git a/addons/README.md b/addons/README.md
index c3f00031d..cf30bbd58 100644
--- a/addons/README.md
+++ b/addons/README.md
@@ -20,6 +20,7 @@ the core bigger or change how it behaves. You install an add-on only if you want
| [TV Static](tv-static/) | An off-the-air moment each time the screen turns on: the picture opens from a bright line, snow and hiss, colour bars or an old "no signal" screen for 3 to 10 s, then fades into what was on screen; the card sets style, sound and length | [@luyzfernando08](https://github.com/luyzfernando08) | Xiaomi Mi Box S (Android TV 9), Philco by Skyworth (Android TV 14) | foreground service (screen-on watcher), start at boot, display over other apps | <img src="tv-static/screenshot.jpg" width="240" alt="TV Static on a TV"> |
| [Brightness](brightness/) | Dims the picture below the TV's own minimum over every app (and the picture settings on Realtek TVs), with a warm night mode on a schedule and a one-press normal screen | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | overlay, foreground service, start at boot | <img src="brightness/screenshot.jpg" width="240" alt="Brightness on a TV"> |
| [Sleep Timer](sleep-timer/) | A sleep timer: 15 to 120 minutes, a one-minute countdown over what is playing with "15 more minutes", then standby; over HDMI-CEC the TV turns off with the box | [@luyzfernando08](https://github.com/luyzfernando08) | Xiaomi Mi Box S (Android TV 9) | accessibility (lock-screen action only), display over other apps, exact alarms | <img src="sleep-timer/screenshot.jpg" width="240" alt="Sleep Timer on a TV"> |
+| [Sources](sources/) | The TV's sources by the names devices give themselves over HDMI-CEC ("PlayStation 5", not "HDMI 2"), with their state, and Live TV; OK switches | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | None | <img src="sources/screenshot.jpg" width="240" alt="Sources on a TV"> |
A widget, an info tile, a storage or smart-home card: if it can live as its own small app, it can live
here. See the rules below.
diff --git a/addons/sources/.gitignore b/addons/sources/.gitignore
new file mode 100644
index 000000000..a7e04a21b
--- /dev/null
+++ b/addons/sources/.gitignore
@@ -0,0 +1,4 @@
+.gradle/
+build/
+app/build/
+local.properties
diff --git a/addons/sources/README.md b/addons/sources/README.md
new file mode 100644
index 000000000..7fd72d00c
--- /dev/null
+++ b/addons/sources/README.md
@@ -0,0 +1,50 @@
+# Sources
+
+The TV's sources by the names the devices give themselves: "PlayStation 5" and "Apple TV" instead of "HDMI 2" and
+"HDMI 3". HDMI-CEC devices report their names to the TV, which lists each one as an input under its port; this
+screen shows those names (or the name you gave a port in the TV's settings), connected ones first in full
+brightness, and OK switches to one. Live TV comes last. For the [Clean Your TV](../../README.md) launcher it
+declares a [module card](../../docs/modules.md): once you answer **Show card** its tile reads "Sources" with a
+green glow. On any other Android TV home it is an ordinary app. ("Inputs" is a name the launcher keeps for its own
+menu, hence Sources.)
+
+![Sources on a TV](screenshot.jpg)
+
+A port with one device is one row under that device's name, with "HDMI 2 · connected" under it; a port with
+several (a receiver and the console behind it) has a row for each; a port with nothing on it keeps its own name
+and is dimmed.
+
+## Permissions
+
+None. It reads the TV's input list through `TvInputManager`, which needs no permission; package visibility is
+limited to TV input services. No internet permission: nothing leaves the TV. No background service, accounts,
+telemetry, ads or libraries. Nothing is stored.
+
+## Build and install
+
+JDK 17 and Android SDK platform 34 with build-tools 34.0.0, as for the launcher. From the repo root, with ADB on:
+
+```bash
+TV_HOST=<tv ip> addons/sources/install.sh
+```
+
+To build only: `cd addons/sources && ./gradlew assembleRelease`; the JVM tests run with `./gradlew testReleaseUnitTest`.
+
+## Limits
+
+- **Device names need HDMI-CEC** on both the TV and the device (it has brand names: Anynet+, BRAVIA Sync, SimpLink,
+ EasyLink…). Without it the row keeps the port's name.
+- **Switching** opens the input through the TV's own input app, as the launcher's Inputs menu does; a TV without
+ one says so.
+- **"Connected"** is what the TV reports for each port. Some TVs report a port as connected (or connected, in
+ standby) whenever anything is plugged in, whether or not it is on; the MOKA R8G reports its AV input as
+ connected with nothing in it.
+- **One card.** The card spec allows one card per app, so the devices are rows inside, not cards of their own.
+
+## Tested on
+
+MOKA R8G (Android 14), on a home app that implements the module-card spec: the card prompt and the card, three HDMI ports the TV reports as connected in standby, AV and Live TV. No HDMI-CEC device was attached, so device names are covered only by the JVM tests (ports, devices behind a port, the owner's own names, standby).
+
+## Maintainer
+
+[@maxhayim](https://github.com/maxhayim)
diff --git a/addons/sources/app/build.gradle b/addons/sources/app/build.gradle
new file mode 100644
index 000000000..d349038ec
--- /dev/null
+++ b/addons/sources/app/build.gradle
@@ -0,0 +1,36 @@
+plugins {
+ id 'com.android.application'
+}
+
+android {
+ namespace 'io.github.maxhayim.sources'
+ compileSdk 34
+
+ defaultConfig {
+ applicationId 'io.github.maxhayim.sources'
+ minSdk 26
+ targetSdk 34
+ versionCode 1
+ versionName '0.1'
+ }
+
+ buildTypes {
+ release {
+ minifyEnabled true
+ shrinkResources true
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
+ // Sideloaded over ADB, never published: signed with the debug key, like the launcher.
+ signingConfig signingConfigs.debug
+ }
+ }
+
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_17
+ targetCompatibility JavaVersion.VERSION_17
+ }
+}
+
+dependencies {
+ // No libraries in the APK. Test only: plain JVM tests in src/test.
+ testImplementation 'junit:junit:4.13.2'
+}
diff --git a/addons/sources/app/proguard-rules.pro b/addons/sources/app/proguard-rules.pro
new file mode 100644
index 000000000..a4435d69a
--- /dev/null
+++ b/addons/sources/app/proguard-rules.pro
@@ -0,0 +1,2 @@
+# Keep class and method names: readable stack traces matter more than the last few KB in a sideloaded app.
+-dontobfuscate
diff --git a/addons/sources/app/src/main/AndroidManifest.xml b/addons/sources/app/src/main/AndroidManifest.xml
new file mode 100644
index 000000000..7d6716ec7
--- /dev/null
+++ b/addons/sources/app/src/main/AndroidManifest.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <uses-feature android:name="android.software.leanback" android:required="false" />
+ <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
+ <uses-feature android:name="android.software.live_tv" android:required="false" />
+
+ <!-- The TV's input services (HDMI, tuner), so Android lets this app see them. -->
+ <queries>
+ <intent>
+ <action android:name="android.media.tv.TvInputService" />
+ </intent>
+ </queries>
+
+ <application
+ android:allowBackup="false"
+ android:banner="@drawable/banner"
+ android:icon="@mipmap/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@android:style/Theme.DeviceDefault.NoActionBar">
+
+ <activity
+ android:name=".MainActivity"
+ android:exported="true"
+ android:launchMode="singleTask">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
+ </intent-filter>
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="com.gathin.tvhome.category.DOCK_CARD" />
+ </intent-filter>
+ <meta-data android:name="com.gathin.tvhome.card" android:resource="@xml/module_card" />
+ </activity>
+ </application>
+</manifest>
diff --git a/addons/sources/app/src/main/java/io/github/maxhayim/sources/MainActivity.java b/addons/sources/app/src/main/java/io/github/maxhayim/sources/MainActivity.java
new file mode 100644
index 000000000..df9fe0427
--- /dev/null
+++ b/addons/sources/app/src/main/java/io/github/maxhayim/sources/MainActivity.java
@@ -0,0 +1,118 @@
+package io.github.maxhayim.sources;
+
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
+import android.content.Intent;
+import android.media.tv.TvContract;
+import android.media.tv.TvInputInfo;
+import android.media.tv.TvInputManager;
+import android.os.Bundle;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+import android.widget.ScrollView;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * The tile and module card open this: the TV's sources by the names the devices give themselves over HDMI-CEC
+ * ("PlayStation 5", not "HDMI 2"), connected ones first in brightness, and OK switches to one. Then Live TV.
+ * It reads the TV's own input list; nothing is stored and nothing runs when it is closed.
+ */
+public class MainActivity extends Activity {
+ private LinearLayout rows;
+ private TextView status;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ LinearLayout page = new LinearLayout(this);
+ page.setOrientation(LinearLayout.VERTICAL);
+ page.setBackgroundColor(0xFF0D1117);
+ page.setPadding(px(48), px(32), px(48), px(24));
+ TextView title = new TextView(this);
+ title.setText(R.string.app_name);
+ title.setTextSize(28);
+ title.setTextColor(0xFFFFFFFF);
+ status = new TextView(this);
+ status.setTextSize(15);
+ status.setTextColor(0xFF8B949E);
+ status.setPadding(0, 0, 0, px(16));
+ page.addView(title);
+ page.addView(status);
+ ScrollView scroll = new ScrollView(this);
+ rows = new LinearLayout(this);
+ rows.setOrientation(LinearLayout.VERTICAL);
+ scroll.addView(rows);
+ page.addView(scroll, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f));
+ setContentView(page);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ build();
+ }
+
+ private void build() {
+ rows.removeAllViews();
+ TvInputManager tim = getSystemService(TvInputManager.class);
+ List<Sources.Input> inputs = new ArrayList<>();
+ TvInputInfo tuner = null;
+ if (tim != null) {
+ for (TvInputInfo info : tim.getTvInputList()) {
+ if (info.isPassthroughInput()) {
+ CharSequence custom = info.loadCustomLabel(this);
+ int state = tim.getInputState(info.getId());
+ inputs.add(new Sources.Input(info.getId(), info.getParentId(), String.valueOf(info.loadLabel(this)),
+ custom == null ? "" : custom.toString(), info.getType() == TvInputInfo.TYPE_HDMI,
+ state != TvInputManager.INPUT_STATE_DISCONNECTED,
+ state == TvInputManager.INPUT_STATE_CONNECTED_STANDBY));
+ } else if (info.getType() == TvInputInfo.TYPE_TUNER && tuner == null) {
+ tuner = info;
+ }
+ }
+ }
+ List<Sources.Row> list = Sources.rows(inputs);
+ Row first = null;
+ int connected = 0;
+ for (Sources.Row r : list) {
+ Row row = add(new Row(this, r.name, r.detail, () -> open(new Intent(Intent.ACTION_VIEW,
+ TvContract.buildChannelUriForPassthroughInput(r.id)))));
+ row.setAlpha(r.connected ? 1f : 0.55f);
+ if (r.connected) connected++;
+ if (first == null) first = row;
+ }
+ if (tuner != null) {
+ final TvInputInfo live = tuner;
+ Row row = add(new Row(this, "Live TV", "The TV's antenna or cable tuner.", () -> open(
+ new Intent(Intent.ACTION_VIEW, TvContract.buildChannelsUriForInput(live.getId())))));
+ if (first == null) first = row;
+ }
+ if (first == null) {
+ add(new Row(this, "No sources", "This TV reports no HDMI or tuner inputs.", null));
+ status.setText("");
+ } else {
+ status.setText(connected == 1 ? "1 device connected." : connected + " devices connected.");
+ first.requestFocus();
+ }
+ }
+
+ private Row add(Row r) {
+ rows.addView(r, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
+ return r;
+ }
+
+ private void open(Intent i) {
+ try {
+ startActivity(i);
+ } catch (ActivityNotFoundException | SecurityException e) {
+ status.setText("This TV has no app that opens that source.");
+ }
+ }
+
+ private int px(int dp) {
+ return Math.round(dp * getResources().getDisplayMetrics().density);
+ }
+}
diff --git a/addons/sources/app/src/main/java/io/github/maxhayim/sources/Row.java b/addons/sources/app/src/main/java/io/github/maxhayim/sources/Row.java
new file mode 100644
index 000000000..1acc59a05
--- /dev/null
+++ b/addons/sources/app/src/main/java/io/github/maxhayim/sources/Row.java
@@ -0,0 +1,58 @@
+package io.github.maxhayim.sources;
+
+import android.content.Context;
+import android.view.Gravity;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+/**
+ * One settings row: a name with a line saying what it does on the left, its value on the right; OK runs it.
+ */
+final class Row extends LinearLayout {
+ private final TextView value;
+ private final TextView desc;
+
+ Row(Context context, String name, String description, Runnable onClick) {
+ super(context);
+ float dp = context.getResources().getDisplayMetrics().density;
+ setOrientation(HORIZONTAL);
+ setGravity(Gravity.CENTER_VERTICAL);
+ int ph = Math.round(16 * dp), pv = Math.round(10 * dp);
+ setPadding(ph, pv, ph, pv);
+ setBackgroundResource(R.drawable.row_background);
+ setFocusable(onClick != null);
+ setClickable(onClick != null);
+ if (onClick != null) setOnClickListener(v -> onClick.run());
+
+ LinearLayout text = new LinearLayout(context);
+ text.setOrientation(VERTICAL);
+ TextView label = new TextView(context);
+ label.setText(name);
+ label.setTextSize(17);
+ label.setTextColor(0xFFFFFFFF);
+ text.addView(label);
+ desc = new TextView(context);
+ desc.setTextSize(12);
+ desc.setTextColor(0xFF8B949E);
+ desc.setPadding(0, Math.round(2 * dp), Math.round(16 * dp), 0);
+ text.addView(desc);
+ setDetail(description);
+ addView(text, new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
+
+ value = new TextView(context);
+ value.setTextSize(17);
+ value.setTextColor(0xFFC9D1D9);
+ value.setGravity(Gravity.END);
+ value.setMinWidth(Math.round(90 * dp));
+ addView(value, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
+ }
+
+ void setDetail(String d) {
+ desc.setText(d == null ? "" : d);
+ desc.setVisibility(d == null || d.isEmpty() ? GONE : VISIBLE);
+ }
+
+ void setValue(String v) {
+ value.setText(v);
+ }
+}
diff --git a/addons/sources/app/src/main/java/io/github/maxhayim/sources/Sources.java b/addons/sources/app/src/main/java/io/github/maxhayim/sources/Sources.java
new file mode 100644
index 000000000..507c2a428
--- /dev/null
+++ b/addons/sources/app/src/main/java/io/github/maxhayim/sources/Sources.java
@@ -0,0 +1,107 @@
+package io.github.maxhayim.sources;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Turns the TV's input list into rows, with no Android types, so the JVM tests cover it. Each HDMI-CEC device
+ * the TV knows is a child input of its port, labelled with the name the device gives itself; a port with
+ * devices becomes one row per device, a port without one keeps its own name.
+ */
+final class Sources {
+ /** One TvInputInfo, reduced to what the rows need. */
+ static final class Input {
+ final String id;
+ final String parentId;
+ final String label;
+ /** The name the owner gave it in the TV's settings; empty when none. */
+ final String customLabel;
+ final boolean hdmi;
+ final boolean connected;
+ /** Connected, but the device is asleep (TvInputManager.INPUT_STATE_CONNECTED_STANDBY). */
+ final boolean standby;
+
+ Input(String id, String parentId, String label, String customLabel, boolean hdmi, boolean connected) {
+ this(id, parentId, label, customLabel, hdmi, connected, false);
+ }
+
+ Input(String id, String parentId, String label, String customLabel, boolean hdmi, boolean connected,
+ boolean standby) {
+ this.id = id;
+ this.parentId = parentId;
+ this.label = label == null ? "" : label;
+ this.customLabel = customLabel == null ? "" : customLabel;
+ this.hdmi = hdmi;
+ this.connected = connected;
+ this.standby = standby;
+ }
+
+ String name() {
+ return !customLabel.isEmpty() ? customLabel : label;
+ }
+ }
+
+ /** One row: open input id. */
+ static final class Row {
+ final String id;
+ final String name;
+ final String detail;
+ final boolean connected;
+
+ Row(String id, String name, String detail, boolean connected) {
+ this.id = id;
+ this.name = name;
+ this.detail = detail;
+ this.connected = connected;
+ }
+ }
+
+ private Sources() {
+ }
+
+ static List<Row> rows(Collection<Input> inputs) {
+ List<Input> ports = new ArrayList<>();
+ for (Input i : inputs) if (i.parentId == null) ports.add(i);
+ // HDMI first, by port number, then the rest by name
+ ports.sort((a, b) -> {
+ if (a.hdmi != b.hdmi) return a.hdmi ? -1 : 1;
+ int pa = portNumber(a.label), pb = portNumber(b.label);
+ if (pa != pb) return Integer.compare(pa, pb);
+ return a.name().compareToIgnoreCase(b.name());
+ });
+ List<Row> out = new ArrayList<>();
+ for (Input port : ports) {
+ List<Input> devices = new ArrayList<>();
+ for (Input i : inputs) if (port.id.equals(i.parentId)) devices.add(i);
+ if (devices.isEmpty()) {
+ out.add(new Row(port.id, port.name(), detail(port.label, port.name(), port.connected, port.standby),
+ port.connected));
+ continue;
+ }
+ for (Input d : devices) {
+ out.add(new Row(d.id, d.name(), detail(port.label, d.name(), true, d.standby), true));
+ }
+ }
+ return out;
+ }
+
+ /** "HDMI 2" under a device's own name; the connection state where the port is the name. */
+ static String detail(String portLabel, String name, boolean connected, boolean standby) {
+ String state = !connected ? "nothing connected" : standby ? "connected, in standby" : "connected";
+ if (portLabel.isEmpty() || portLabel.equalsIgnoreCase(name)) return state;
+ return portLabel + " · " + state;
+ }
+
+ /** 2 for "HDMI 2" or "HDMI2"; Integer.MAX_VALUE when the label has no number. */
+ static int portNumber(String label) {
+ String digits = label.toLowerCase(Locale.ROOT).replaceAll("[^0-9]", "");
+ if (digits.isEmpty()) return Integer.MAX_VALUE;
+ try {
+ return Integer.parseInt(digits.length() > 3 ? digits.substring(0, 3) : digits);
+ } catch (NumberFormatException e) {
+ return Integer.MAX_VALUE;
+ }
+ }
+}
diff --git a/addons/sources/app/src/main/res/drawable/banner.xml b/addons/sources/app/src/main/res/drawable/banner.xml
new file mode 100644
index 000000000..a4713002b
--- /dev/null
+++ b/addons/sources/app/src/main/res/drawable/banner.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 16:9 banner: a screen with three plugs feeding it. Own art. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="320dp" android:height="180dp"
+ android:viewportWidth="320" android:viewportHeight="180">
+ <path android:fillColor="#101418" android:pathData="M0,0h320v180h-320z" />
+ <path android:strokeColor="#3DDC84" android:strokeWidth="8" android:fillColor="#00000000" android:pathData="M150,40h120v84h-120z" />
+ <path android:fillColor="#FFFFFF" android:pathData="M50,56h56v14h-56zM50,86h56v14h-56zM50,116h56v14h-56z" />
+ <path android:strokeColor="#FFFFFF" android:strokeWidth="4" android:fillColor="#00000000" android:pathData="M106,63h44M106,93h44M106,123h44" />
+</vector>
diff --git a/addons/sources/app/src/main/res/drawable/ic_foreground.xml b/addons/sources/app/src/main/res/drawable/ic_foreground.xml
new file mode 100644
index 000000000..c110aeb2d
--- /dev/null
+++ b/addons/sources/app/src/main/res/drawable/ic_foreground.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- A screen with a plug inside the adaptive icon's safe zone. Own art. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="108dp" android:height="108dp"
+ android:viewportWidth="108" android:viewportHeight="108">
+ <path android:strokeColor="#3DDC84" android:strokeWidth="4" android:fillColor="#00000000" android:pathData="M48,38h28v22h-28z" />
+ <path android:fillColor="#FFFFFF" android:pathData="M32,46h12v6h-12z" />
+</vector>
diff --git a/addons/sources/app/src/main/res/drawable/row_background.xml b/addons/sources/app/src/main/res/drawable/row_background.xml
new file mode 100644
index 000000000..03ccab9f5
--- /dev/null
+++ b/addons/sources/app/src/main/res/drawable/row_background.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- A settings row: transparent until focused, then a glass highlight like the launcher's. -->
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+ <item android:state_focused="true">
+ <shape android:shape="rectangle">
+ <solid android:color="#33FFFFFF" />
+ <stroke android:width="1dp" android:color="#55FFFFFF" />
+ <corners android:radius="12dp" />
+ </shape>
+ </item>
+ <item>
+ <shape android:shape="rectangle">
+ <solid android:color="#00000000" />
+ <corners android:radius="12dp" />
+ </shape>
+ </item>
+</selector>
diff --git a/addons/sources/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/addons/sources/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
new file mode 100644
index 000000000..0a99b20b1
--- /dev/null
+++ b/addons/sources/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
+ <background android:drawable="@android:color/black" />
+ <foreground android:drawable="@drawable/ic_foreground" />
+</adaptive-icon>
diff --git a/addons/sources/app/src/main/res/values/strings.xml b/addons/sources/app/src/main/res/values/strings.xml
new file mode 100644
index 000000000..4302e8c45
--- /dev/null
+++ b/addons/sources/app/src/main/res/values/strings.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+ <string name="app_name">Sources</string>
+</resources>
diff --git a/addons/sources/app/src/main/res/xml/module_card.xml b/addons/sources/app/src/main/res/xml/module_card.xml
new file mode 100644
index 000000000..6fe529715
--- /dev/null
+++ b/addons/sources/app/src/main/res/xml/module_card.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="utf-8"?>
+<card spec="1" label="Sources" caption="Devices by name" accent="#3DDC84" />
diff --git a/addons/sources/app/src/test/java/io/github/maxhayim/sources/SourcesTest.java b/addons/sources/app/src/test/java/io/github/maxhayim/sources/SourcesTest.java
new file mode 100644
index 000000000..4e49ccab3
--- /dev/null
+++ b/addons/sources/app/src/test/java/io/github/maxhayim/sources/SourcesTest.java
@@ -0,0 +1,62 @@
+package io.github.maxhayim.sources;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class SourcesTest {
+ private static Sources.Input port(String id, String label, boolean connected) {
+ return new Sources.Input(id, null, label, "", true, connected);
+ }
+
+ @Test
+ public void portsInOrderWithTheirState() {
+ List<Sources.Row> rows = Sources.rows(Arrays.asList(port("h3", "HDMI 3", false), port("h1", "HDMI 1", true),
+ new Sources.Input("av", null, "AV", "", false, false)));
+ assertEquals("HDMI 1", rows.get(0).name);
+ assertEquals("connected", rows.get(0).detail);
+ assertEquals("HDMI 3", rows.get(1).name);
+ assertEquals("nothing connected", rows.get(1).detail);
+ assertEquals("AV", rows.get(2).name);
+ }
+
+ @Test
+ public void cecDevicesReplaceTheirPort() {
+ List<Sources.Row> rows = Sources.rows(Arrays.asList(port("h1", "HDMI 1", true), port("h2", "HDMI 2", true),
+ new Sources.Input("h2/ps5", "h2", "PlayStation 5", "", true, true)));
+ assertEquals(2, rows.size());
+ assertEquals("PlayStation 5", rows.get(1).name);
+ assertEquals("HDMI 2 · connected", rows.get(1).detail);
+ assertEquals("h2/ps5", rows.get(1).id);
+ }
+
+ @Test
+ public void severalDevicesBehindOnePort() {
+ List<Sources.Row> rows = Sources.rows(Arrays.asList(port("h1", "HDMI 1", true),
+ new Sources.Input("a", "h1", "Receiver", "", true, true), new Sources.Input("b", "h1", "Apple TV", "", true, true)));
+ assertEquals(2, rows.size());
+ }
+
+ @Test
+ public void theOwnersNameWins() {
+ List<Sources.Row> rows = Sources.rows(Arrays.asList(new Sources.Input("h1", null, "HDMI 1", "Cable box", true, true)));
+ assertEquals("Cable box", rows.get(0).name);
+ assertEquals("HDMI 1 · connected", rows.get(0).detail);
+ }
+
+ @Test
+ public void standbyIsSaid() {
+ List<Sources.Row> rows = Sources.rows(Arrays.asList(new Sources.Input("h1", null, "HDMI1", "", true, true, true)));
+ assertEquals("connected, in standby", rows.get(0).detail);
+ }
+
+ @Test
+ public void portNumbers() {
+ assertEquals(2, Sources.portNumber("HDMI 2"));
+ assertEquals(10, Sources.portNumber("HDMI10"));
+ assertEquals(Integer.MAX_VALUE, Sources.portNumber("AV"));
+ }
+}
diff --git a/addons/sources/build.gradle b/addons/sources/build.gradle
new file mode 100644
index 000000000..48978475f
--- /dev/null
+++ b/addons/sources/build.gradle
@@ -0,0 +1,3 @@
+plugins {
+ id 'com.android.application' version '8.5.2' apply false
+}
diff --git a/addons/sources/gradle.properties b/addons/sources/gradle.properties
new file mode 100644
index 000000000..18825f968
--- /dev/null
+++ b/addons/sources/gradle.properties
@@ -0,0 +1,3 @@
+org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
+android.useAndroidX=true
+android.nonTransitiveRClass=true
diff --git a/addons/sources/gradle/wrapper/gradle-wrapper.jar b/addons/sources/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 000000000..2c3521197
Binary files /dev/null and b/addons/sources/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/addons/sources/gradle/wrapper/gradle-wrapper.properties b/addons/sources/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 000000000..77f5d0ba6
--- /dev/null
+++ b/addons/sources/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,8 @@
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
+distributionSha256Sum=d725d707bfabd4dfdc958c624003b3c80accc03f7037b5122c4b1d0ef15cecab
+networkTimeout=10000
+validateDistributionUrl=true
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
diff --git a/addons/sources/gradlew b/addons/sources/gradlew
new file mode 100755
index 000000000..f5feea6d6
--- /dev/null
+++ b/addons/sources/gradlew
@@ -0,0 +1,252 @@
+#!/bin/sh
+
+#
+# Copyright © 2015-2021 the original authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+##############################################################################
+#
+# Gradle start up script for POSIX generated by Gradle.
+#
+# Important for running:
+#
+# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
+# noncompliant, but you have some other compliant shell such as ksh or
+# bash, then to run this script, type that shell name before the whole
+# command line, like:
+#
+# ksh Gradle
+#
+# Busybox and similar reduced shells will NOT work, because this script
+# requires all of these POSIX shell features:
+# * functions;
+# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
+# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
+# * compound commands having a testable exit status, especially «case»;
+# * various built-in commands including «command», «set», and «ulimit».
+#
+# Important for patching:
+#
+# (2) This script targets any POSIX shell, so it avoids extensions provided
+# by Bash, Ksh, etc; in particular arrays are avoided.
+#
+# The "traditional" practice of packing multiple parameters into a
+# space-separated string is a well documented source of bugs and security
+# problems, so this is (mostly) avoided, by progressively accumulating
+# options in "$@", and eventually passing that to Java.
+#
+# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
+# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
+# see the in-line comments for details.
+#
+# There are tweaks for specific operating systems such as AIX, CygWin,
+# Darwin, MinGW, and NonStop.
+#
+# (3) This script is generated from the Groovy template
+# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
+# within the Gradle project.
+#
+# You can find Gradle at https://github.com/gradle/gradle/.
+#
+##############################################################################
+
+# Attempt to set APP_HOME
+
+# Resolve links: $0 may be a link
+app_path=$0
+
+# Need this for daisy-chained symlinks.
+while
+ APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
+ [ -h "$app_path" ]
+do
+ ls=$( ls -ld "$app_path" )
+ link=${ls#*' -> '}
+ case $link in #(
+ /*) app_path=$link ;; #(
+ *) app_path=$APP_HOME$link ;;
+ esac
+done
+
+# This is normally unused
+# shellcheck disable=SC2034
+APP_BASE_NAME=${0##*/}
+# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
+APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
+' "$PWD" ) || exit
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD=maximum
+
+warn () {
+ echo "$*"
+} >&2
+
+die () {
+ echo
+ echo "$*"
+ echo
+ exit 1
+} >&2
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+nonstop=false
+case "$( uname )" in #(
+ CYGWIN* ) cygwin=true ;; #(
+ Darwin* ) darwin=true ;; #(
+ MSYS* | MINGW* ) msys=true ;; #(
+ NONSTOP* ) nonstop=true ;;
+esac
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+ # IBM's JDK on AIX uses strange locations for the executables
+ JAVACMD=$JAVA_HOME/jre/sh/java
+ else
+ JAVACMD=$JAVA_HOME/bin/java
+ fi
+ if [ ! -x "$JAVACMD" ] ; then
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+else
+ JAVACMD=java
+ if ! command -v java >/dev/null 2>&1
+ then
+ die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+ fi
+fi
+
+# Increase the maximum file descriptors if we can.
+if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
+ case $MAX_FD in #(
+ max*)
+ # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ MAX_FD=$( ulimit -H -n ) ||
+ warn "Could not query maximum file descriptor limit"
+ esac
+ case $MAX_FD in #(
+ '' | soft) :;; #(
+ *)
+ # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
+ # shellcheck disable=SC2039,SC3045
+ ulimit -n "$MAX_FD" ||
+ warn "Could not set maximum file descriptor limit to $MAX_FD"
+ esac
+fi
+
+# Collect all arguments for the java command, stacking in reverse order:
+# * args from the command line
+# * the main class name
+# * -classpath
+# * -D...appname settings
+# * --module-path (only if needed)
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
+
+# For Cygwin or MSYS, switch paths to Windows format before running java
+if "$cygwin" || "$msys" ; then
+ APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
+ CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
+
+ JAVACMD=$( cygpath --unix "$JAVACMD" )
+
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
+ for arg do
+ if
+ case $arg in #(
+ -*) false ;; # don't mess with options #(
+ /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
+ [ -e "$t" ] ;; #(
+ *) false ;;
+ esac
+ then
+ arg=$( cygpath --path --ignore --mixed "$arg" )
+ fi
+ # Roll the args list around exactly as many times as the number of
+ # args, so each arg winds up back in the position where it started, but
+ # possibly modified.
+ #
+ # NB: a `for` loop captures its iteration list before it begins, so
+ # changing the positional parameters here affects neither the number of
+ # iterations, nor the values presented in `arg`.
+ shift # remove old arg
+ set -- "$@" "$arg" # push replacement arg
+ done
+fi
+
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
+
+# Collect all arguments for the java command:
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
+# and any embedded shellness will be escaped.
+# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
+# treated as '${Hostname}' itself on the command line.
+
+set -- \
+ "-Dorg.gradle.appname=$APP_BASE_NAME" \
+ -classpath "$CLASSPATH" \
+ org.gradle.wrapper.GradleWrapperMain \
+ "$@"
+
+# Stop when "xargs" is not available.
+if ! command -v xargs >/dev/null 2>&1
+then
+ die "xargs is not available"
+fi
+
+# Use "xargs" to parse quoted args.
+#
+# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
+#
+# In Bash we could simply go:
+#
+# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
+# set -- "${ARGS[@]}" "$@"
+#
+# but POSIX shell has neither arrays nor command substitution, so instead we
+# post-process each arg (as a line of input to sed) to backslash-escape any
+# character that might be a shell metacharacter, then use eval to reverse
+# that process (while maintaining the separation between arguments), and wrap
+# the whole thing up as a single "set" statement.
+#
+# This will of course break if any of these variables contains a newline or
+# an unmatched quote.
+#
+
+eval "set -- $(
+ printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
+ xargs -n1 |
+ sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
+ tr '\n' ' '
+ )" '"$@"'
+
+exec "$JAVACMD" "$@"
diff --git a/addons/sources/gradlew.bat b/addons/sources/gradlew.bat
new file mode 100644
index 000000000..9d21a2183
--- /dev/null
+++ b/addons/sources/gradlew.bat
@@ -0,0 +1,94 @@
+@rem
+@rem Copyright 2015 the original author or authors.
+@rem
+@rem Licensed under the Apache License, Version 2.0 (the "License");
+@rem you may not use this file except in compliance with the License.
+@rem You may obtain a copy of the License at
+@rem
+@rem https://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+@rem SPDX-License-Identifier: Apache-2.0
+@rem
+
+@if "%DEBUG%"=="" @echo off
+@rem ##########################################################################
+@rem
+@rem Gradle startup script for Windows
+@rem
+@rem ##########################################################################
+
+@rem Set local scope for the variables with windows NT shell
+if "%OS%"=="Windows_NT" setlocal
+
+set DIRNAME=%~dp0
+if "%DIRNAME%"=="" set DIRNAME=.
+@rem This is normally unused
+set APP_BASE_NAME=%~n0
+set APP_HOME=%DIRNAME%
+
+@rem Resolve any "." and ".." in APP_HOME to make it shorter.
+for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
+
+@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
+
+@rem Find java.exe
+if defined JAVA_HOME goto findJavaFromJavaHome
+
+set JAVA_EXE=java.exe
+%JAVA_EXE% -version >NUL 2>&1
+if %ERRORLEVEL% equ 0 goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:findJavaFromJavaHome
+set JAVA_HOME=%JAVA_HOME:"=%
+set JAVA_EXE=%JAVA_HOME%/bin/java.exe
+
+if exist "%JAVA_EXE%" goto execute
+
+echo. 1>&2
+echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
+echo. 1>&2
+echo Please set the JAVA_HOME variable in your environment to match the 1>&2
+echo location of your Java installation. 1>&2
+
+goto fail
+
+:execute
+@rem Setup the command line
+
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
+
+
+@rem Execute Gradle
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
+
+:end
+@rem End local scope for the variables with windows NT shell
+if %ERRORLEVEL% equ 0 goto mainEnd
+
+:fail
+rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
+rem the _cmd.exe /c_ return code!
+set EXIT_CODE=%ERRORLEVEL%
+if %EXIT_CODE% equ 0 set EXIT_CODE=1
+if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
+exit /b %EXIT_CODE%
+
+:mainEnd
+if "%OS%"=="Windows_NT" endlocal
+
+:omega
diff --git a/addons/sources/install.sh b/addons/sources/install.sh
new file mode 100755
index 000000000..1a6960944
--- /dev/null
+++ b/addons/sources/install.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# Build Sources and install it on the TV.
+# TV_HOST=<tv ip> addons/sources/install.sh
+set -euo pipefail
+: "${TV_HOST:?set TV_HOST to the TV IP address}"
+cd "$(dirname "$0")"
+TOOLS="$(cd ../../tools && pwd)"
+PY=$TOOLS/venv/bin/python
+[ -x "$PY" ] || PY=$TOOLS/venv/Scripts/python.exe
+export MSYS_NO_PATHCONV=1
+PKG=io.github.maxhayim.sources
+# Script paths stay relative: with MSYS_NO_PATHCONV, Git Bash would hand Windows Python a /c/... path.
+tv() { "$PY" -I ../../tools/tv.py "$@"; }
+
+[ -f local.properties ] || { [ -n "${ANDROID_HOME:-}" ] && echo "sdk.dir=$ANDROID_HOME" > local.properties; } || true
+if [ -n "${JAVA_HOME:-}" ]; then export PATH="$JAVA_HOME/bin:$PATH"; fi
+./gradlew assembleRelease -q --console=plain
+
+"$PY" -I ../../tools/b64push.py app/build/outputs/apk/release/app-release.apk /data/local/tmp/sources.apk
+OUT=$(tv 'pm install -r /data/local/tmp/sources.apk; rm -f /data/local/tmp/sources.apk')
+echo "$OUT"
+case "$OUT" in *Success*) ;; *) echo "ERROR: pm install failed" >&2; exit 1 ;; esac
+
+tv "am start -n $PKG/.MainActivity >/dev/null; echo opened"
+echo "Installed. Sources is open on the TV; press Home and answer Show card for the card."
diff --git a/addons/sources/screenshot.jpg b/addons/sources/screenshot.jpg
new file mode 100644
index 000000000..2afc72826
Binary files /dev/null and b/addons/sources/screenshot.jpg differ
diff --git a/addons/sources/settings.gradle b/addons/sources/settings.gradle
new file mode 100644
index 000000000..efc5e9d40
--- /dev/null
+++ b/addons/sources/settings.gradle
@@ -0,0 +1,16 @@
+pluginManagement {
+ repositories {
+ google()
+ mavenCentral()
+ gradlePluginPortal()
+ }
+}
+dependencyResolutionManagement {
+ repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+ repositories {
+ google()
+ mavenCentral()
+ }
+}
+rootProject.name = "sources"
+include ':app'