Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
launcher: config.json overlay and wallpaper, so a theme changes the look without a rebuild
Gathin23a9fc0fcfb362
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.javaindex 0249de63b..7b7b88c3a 100644--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java@@ -92,18 +92,20 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {private final Context ctx;private final TileListener listener;private final int dockSize;- /** Dock order (res/values/config.xml, dock_apps). */+ /** Dock order (config.xml dock_apps, or a theme's config.json). */private final List<String> favourites;/** Never shown as tiles (res/values/config.xml, hidden_apps). */private final Set<String> hidden;+ private final boolean showHomeSettings;private final List<Entry> items = new ArrayList<>();- AppAdapter(Context ctx, TileListener listener, int dockSize) {+ AppAdapter(Context ctx, TileListener listener, int dockSize, Config cfg) {this.ctx = ctx;this.listener = listener;this.dockSize = dockSize;- this.favourites = Arrays.asList(ctx.getResources().getStringArray(R.array.dock_apps));- this.hidden = new HashSet<>(Arrays.asList(ctx.getResources().getStringArray(R.array.hidden_apps)));+ this.favourites = cfg.dockApps;+ this.hidden = new HashSet<>(cfg.hiddenApps);+ this.showHomeSettings = cfg.showHomeSettingsShortcut;// Tiles keep their identity across reload(), so a focused tile stays on its app instead of// its view being rebound to whatever app now sits in that slot.setHasStableIds(true);@@ -134,7 +136,7 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {return a.label.compareToIgnoreCase(b.label);});// Optional "Default home" tile (config.xml), only when this TV actually has a home-screen picker.- if (ctx.getResources().getBoolean(R.bool.show_home_settings_shortcut)+ if (showHomeSettings&& pm.resolveActivity(new Intent(Settings.ACTION_HOME_SETTINGS), 0) != null) {fresh.add(new Entry(ctx.getString(R.string.home_settings),new ComponentName(ctx, HomeSettingsActivity.class), null,diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Config.java b/launcher/app/src/main/java/com/gathin/tvhome/Config.javanew file mode 100644index 000000000..54a5b4562--- /dev/null+++ b/launcher/app/src/main/java/com/gathin/tvhome/Config.java@@ -0,0 +1,180 @@+package com.gathin.tvhome;++import android.content.Context;+import android.content.res.Resources;+import android.graphics.Color;+import android.util.Log;++import org.json.JSONArray;+import org.json.JSONObject;++import java.io.File;+import java.io.FileInputStream;+import java.io.InputStream;+import java.nio.charset.StandardCharsets;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.Collections;+import java.util.List;++/**+ * The launcher's settings: the values in res/values/config.xml, with an optional overlay read from+ * {@code config.json} in the app's external files directory+ * ({@code /sdcard/Android/data/com.gathin.tvhome/files/}). Every key in the JSON is optional and an+ * unknown or badly typed key is ignored, so a theme can change the dock and the look without a rebuild:+ * {@code tools/theme.sh themes/<name>} pushes a theme folder there and restarts Home.+ *+ * Read once in {@link MainActivity#onCreate}; nothing here touches the network.+ */+final class Config {+ private static final String TAG = "TvHome.Config";+ static final String FILE_NAME = "config.json";+ static final String WALLPAPER_NAME = "wallpaper.jpg";++ final List<String> dockApps;+ final List<String> hiddenApps;+ final String liveTvPackage;+ final String liveTvAction;+ final String sourcePanelService;+ final String sourcePanelAction;+ final int columns;+ final int rowsBelowDock;+ final boolean shamsiDate;+ final String vpnApp;+ final boolean showHomeSettingsShortcut;+ /** Background colour behind everything, #RRGGBB; black unless a theme says otherwise. */+ final int backgroundColor;+ /** Full-screen picture behind the launcher, or null for the plain colour. */+ final File wallpaper;+ /** Whether the overlay file was present and parsed. */+ final boolean overlayApplied;++ private Config(List<String> dockApps, List<String> hiddenApps, String liveTvPackage, String liveTvAction,+ String sourcePanelService, String sourcePanelAction, int columns, int rowsBelowDock,+ boolean shamsiDate, String vpnApp, boolean showHomeSettingsShortcut, int backgroundColor,+ File wallpaper, boolean overlayApplied) {+ this.dockApps = Collections.unmodifiableList(dockApps);+ this.hiddenApps = Collections.unmodifiableList(hiddenApps);+ this.liveTvPackage = liveTvPackage;+ this.liveTvAction = liveTvAction;+ this.sourcePanelService = sourcePanelService;+ this.sourcePanelAction = sourcePanelAction;+ this.columns = columns;+ this.rowsBelowDock = rowsBelowDock;+ this.shamsiDate = shamsiDate;+ this.vpnApp = vpnApp;+ this.showHomeSettingsShortcut = showHomeSettingsShortcut;+ this.backgroundColor = backgroundColor;+ this.wallpaper = wallpaper;+ this.overlayApplied = overlayApplied;+ }++ /** The folder the theme tools push into; null when external storage is unavailable. */+ static File dir(Context ctx) {+ return ctx.getExternalFilesDir(null);+ }++ static Config load(Context ctx) {+ Resources res = ctx.getResources();+ List<String> dock = new ArrayList<>(Arrays.asList(res.getStringArray(R.array.dock_apps)));+ List<String> hidden = new ArrayList<>(Arrays.asList(res.getStringArray(R.array.hidden_apps)));+ String liveTv = res.getString(R.string.live_tv_package).trim();+ String liveTvAction = res.getString(R.string.live_tv_action).trim();+ String panelService = res.getString(R.string.source_panel_service).trim();+ String panelAction = res.getString(R.string.source_panel_action).trim();+ int columns = res.getInteger(R.integer.columns);+ int rowsBelow = res.getInteger(R.integer.rows_below_dock);+ boolean shamsi = res.getBoolean(R.bool.shamsi_date);+ String vpn = res.getString(R.string.vpn_app).trim();+ boolean homeSettings = res.getBoolean(R.bool.show_home_settings_shortcut);+ int background = 0xFF000000;+ boolean applied = false;++ File dir = dir(ctx);+ File file = dir != null ? new File(dir, FILE_NAME) : null;+ JSONObject json = read(file);+ if (json != null) {+ applied = true;+ dock = stringList(json, "dock_apps", dock);+ hidden = stringList(json, "hidden_apps", hidden);+ liveTv = string(json, "live_tv_package", liveTv);+ liveTvAction = string(json, "live_tv_action", liveTvAction);+ panelService = string(json, "source_panel_service", panelService);+ panelAction = string(json, "source_panel_action", panelAction);+ columns = integer(json, "columns", columns, 1, 8);+ rowsBelow = integer(json, "rows_below_dock", rowsBelow, 0, 3);+ shamsi = bool(json, "shamsi_date", shamsi);+ vpn = string(json, "vpn_app", vpn);+ homeSettings = bool(json, "show_home_settings_shortcut", homeSettings);+ background = color(json, "background_color", background);+ }+ File wallpaper = dir != null ? new File(dir, WALLPAPER_NAME) : null;+ if (wallpaper != null && !(wallpaper.isFile() && wallpaper.length() > 0)) wallpaper = null;+ // A theme can also switch the wallpaper off while the file is still there.+ if (json != null && json.has("wallpaper") && !bool(json, "wallpaper", true)) wallpaper = null;++ return new Config(dock, hidden, liveTv, liveTvAction, panelService, panelAction,+ Math.max(1, columns), Math.max(0, rowsBelow), shamsi, vpn, homeSettings, background,+ wallpaper, applied);+ }++ /** Reads and parses the overlay; any problem means "no overlay", never a crash on the home screen. */+ private static JSONObject read(File file) {+ if (file == null || !file.isFile()) return null;+ if (file.length() > 64 * 1024) {+ Log.w(TAG, "ignoring " + file + ": larger than 64 KB");+ return null;+ }+ try (InputStream in = new FileInputStream(file)) {+ byte[] buf = new byte[(int) file.length()];+ int n = 0, r;+ while (n < buf.length && (r = in.read(buf, n, buf.length - n)) > 0) n += r;+ String text = new String(buf, 0, n, StandardCharsets.UTF_8);+ if (text.startsWith("")) text = text.substring(1); // Windows editors add a BOM+ return new JSONObject(text);+ } catch (Exception e) {+ Log.w(TAG, "ignoring " + file + ": " + e.getMessage());+ return null;+ }+ }++ private static String string(JSONObject o, String key, String fallback) {+ Object v = o.opt(key);+ return v instanceof String ? ((String) v).trim() : fallback;+ }++ private static boolean bool(JSONObject o, String key, boolean fallback) {+ Object v = o.opt(key);+ return v instanceof Boolean ? (Boolean) v : fallback;+ }++ private static int integer(JSONObject o, String key, int fallback, int min, int max) {+ Object v = o.opt(key);+ if (!(v instanceof Number)) return fallback;+ int i = ((Number) v).intValue();+ return i < min || i > max ? fallback : i;+ }++ private static int color(JSONObject o, String key, int fallback) {+ Object v = o.opt(key);+ if (!(v instanceof String)) return fallback;+ try {+ int c = Color.parseColor(((String) v).trim());+ return c | 0xFF000000; // always opaque: the window behind is black anyway+ } catch (IllegalArgumentException e) {+ return fallback;+ }+ }++ private static List<String> stringList(JSONObject o, String key, List<String> fallback) {+ Object v = o.opt(key);+ if (!(v instanceof JSONArray)) return fallback;+ JSONArray a = (JSONArray) v;+ List<String> out = new ArrayList<>();+ for (int i = 0; i < a.length(); i++) {+ Object item = a.opt(i);+ if (item instanceof String && !((String) item).trim().isEmpty()) out.add(((String) item).trim());+ }+ return out;+ }+}diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.javaindex 9dfa9335b..b03823724 100644--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java@@ -46,6 +46,7 @@ import androidx.dynamicanimation.animation.SpringForce;import androidx.recyclerview.widget.GridLayoutManager;import androidx.recyclerview.widget.RecyclerView;+import java.io.File;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;@@ -144,13 +145,17 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);density = getResources().getDisplayMetrics().density;- COLUMNS = Math.max(1, getResources().getInteger(R.integer.columns));- liveTvPkg = getString(R.string.live_tv_package).trim();- liveTvAction = getString(R.string.live_tv_action).trim();- sourcePanelService = getString(R.string.source_panel_service).trim();- sourcePanelAction = getString(R.string.source_panel_action).trim();+ // config.xml, overlaid by config.json from a pushed theme (see Config and tools/theme.sh).+ Config cfg = Config.load(this);+ COLUMNS = cfg.columns;+ liveTvPkg = cfg.liveTvPackage;+ liveTvAction = cfg.liveTvAction;+ sourcePanelService = cfg.sourcePanelService;+ sourcePanelAction = cfg.sourcePanelAction;root = findViewById(R.id.root);+ root.setBackgroundColor(cfg.backgroundColor);+ if (cfg.wallpaper != null) showWallpaper(cfg.wallpaper);glass = findViewById(R.id.glass);ambient = findViewById(R.id.ambient);heroName = findViewById(R.id.heroName);@@ -163,14 +168,14 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {greeting = findViewById(R.id.greeting);btnWifi = findViewById(R.id.btnWifi);shamsiDate = findViewById(R.id.shamsiDate);- shamsi = getResources().getBoolean(R.bool.shamsi_date);+ shamsi = cfg.shamsiDate;if (shamsi) {shamsiDate.setVisibility(View.VISIBLE);TextView date = findViewById(R.id.date);date.setTextSize(11f);date.setPadding(0, (int) (4 * density), 0, 0);}- vpnApp = getString(R.string.vpn_app).trim();+ vpnApp = cfg.vpnApp;grid = findViewById(R.id.grid);grid.setLayoutManager(new GridLayoutManager(this, COLUMNS));@@ -179,12 +184,12 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {grid.addItemDecoration(new DockDecoration(COLUMNS, density));grid.setLayoutAnimation(new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.tile_enter), 0.06f));- adapter = new AppAdapter(this, this, COLUMNS);+ adapter = new AppAdapter(this, this, COLUMNS, cfg);grid.setAdapter(adapter);// tvOS layout: push the dock row towards the bottom of the screen, leaving room for// rows_below_dock grid rows under it; the rest of the grid stays below until you press Down.- int rowsBelowDock = Math.max(0, getResources().getInteger(R.integer.rows_below_dock));+ int rowsBelowDock = cfg.rowsBelowDock;grid.addOnLayoutChangeListener((v, l, t, r, b, ol, ot, or, ob) -> {int h = b - t;// Item height follows the tile size, which follows the column count (AppAdapter.tileWidthPx).@@ -270,6 +275,32 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {}}+ /**+ * A theme's full-screen picture behind everything. Decoded once, downsampled to the screen so a 4K+ * JPEG costs a 1080p bitmap at most (about 8 MB), which is the price of a wallpaper on a 1 GB TV.+ */+ private void showWallpaper(File file) {+ try {+ android.util.DisplayMetrics dm = getResources().getDisplayMetrics();+ android.graphics.BitmapFactory.Options opts = new android.graphics.BitmapFactory.Options();+ opts.inJustDecodeBounds = true;+ android.graphics.BitmapFactory.decodeFile(file.getPath(), opts);+ int sample = 1;+ while (opts.outWidth / (sample * 2) >= dm.widthPixels && opts.outHeight / (sample * 2) >= dm.heightPixels) sample *= 2;+ opts.inJustDecodeBounds = false;+ opts.inSampleSize = sample;+ opts.inPreferredConfig = android.graphics.Bitmap.Config.RGB_565; // no alpha needed, half the memory+ android.graphics.Bitmap bmp = android.graphics.BitmapFactory.decodeFile(file.getPath(), opts);+ if (bmp == null) return;+ android.widget.ImageView wall = new android.widget.ImageView(this);+ wall.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);+ wall.setImageBitmap(bmp);+ root.addView(wall, 0, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));+ } catch (Exception | OutOfMemoryError e) {+ // A bad or huge picture must never take the home screen down; the plain colour stays.+ }+ }+@Overrideprotected void onResume() {super.onResume();diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xmlindex 0a4cff6d4..9f0c57a75 100644--- a/launcher/app/src/main/res/values/config.xml+++ b/launcher/app/src/main/res/values/config.xml@@ -1,6 +1,8 @@<?xml version="1.0" encoding="utf-8"?><!--Launcher configuration. Edit this file, then run launcher/build-and-install.sh.+ Or leave it and push a theme instead: tools/theme.sh themes/<name> puts a config.json (same keys, see+ themes/README.md) and an optional wallpaper on the TV, and the launcher overlays it at start.Package names: on the TV run `pm list packages` (tools/tv.py 'pm list packages').--><resources>