diff --git a/README.md b/README.md index d73c65d..a431099 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A native, remote-first launcher built for the onn. 4K Plus Streaming device (Android 14, 2 GB RAM). It is installed alongside Projectivy as `tv.luma.launcher`. Home now opens Luma; Projectivy remains installed and enabled as a fallback. -Version **1.1.1** adds updates directly from the TV and includes the wake fix introduced in 1.0.1. Every resume restores missing images, including when Android briefly pauses the launcher without stopping it. Already-loaded tiles and images are kept on pause-only resumes; leaving the launcher still releases image memory. +Version **1.1.3** adds updates directly from the TV and includes the wake fix introduced in 1.0.1. Every resume restores missing images, including when Android briefly pauses the launcher without stopping it. Already-loaded tiles and images are kept on pause-only resumes; leaving the launcher still releases image memory. ## Use it @@ -82,7 +82,7 @@ The default public HTTPS feed is: https://git.elijahkuntz.com/Elijah/Luma/raw/branch/main/output/update.json ``` -No SSH key or Forgejo account token is stored on the TV. For branch-based Forgejo URLs, Luma resolves the branch to a commit first, then reads both the manifest and APK from that immutable commit. It bounds metadata to 64 KiB and APK downloads to 32 MiB. Before opening the installer, it checks the expected SHA-256 and file size, package name, increasing version code, Android compatibility, and the installed signing certificate. A different signing key is rejected. The provider grants read-only access to exactly the verified APK URI. Interrupted downloads are removed; completed APKs are removed after the updated app starts. +No SSH key or Forgejo account token is stored on the TV. For branch-based Forgejo URLs, Luma resolves the branch to a commit first, then reads both the manifest and APK from that immutable commit. It bounds metadata to 64 KiB and APK downloads to 32 MiB. Before opening the installer, it checks the expected SHA-256 and file size, package name, increasing version code, Android compatibility, and the installed signing certificate. A different signing key is rejected. The verified APK is copied into a private Android PackageInstaller session marked as a downloaded file, with user confirmation required. Interrupted downloads are removed; completed APKs are removed after the updated app starts. ### Publish an update diff --git a/app/build.gradle b/app/build.gradle index 4dc5049..a3deafe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId 'tv.luma.launcher' minSdk 26 targetSdk 34 - versionCode 4 - versionName '1.1.1' + versionCode 6 + versionName '1.1.3' } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8; targetCompatibility JavaVersion.VERSION_1_8 } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b269b3d..6dad4a5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,4 +1,4 @@ - + @@ -37,5 +37,6 @@ + diff --git a/app/src/main/java/tv/luma/launcher/MainActivity.java b/app/src/main/java/tv/luma/launcher/MainActivity.java index 1d20679..3b38f5c 100644 --- a/app/src/main/java/tv/luma/launcher/MainActivity.java +++ b/app/src/main/java/tv/luma/launcher/MainActivity.java @@ -1035,28 +1035,23 @@ public final class MainActivity extends Activity { if (foreground && currentDialog == dialog) progress.setText("Downloading ยท " + percent + "%"); })); + if (getPackageManager().canRequestPackageInstalls()) { + if (!foreground || g != generation || currentDialog != dialog) return; + UpdateInstaller.install(this, release, updater.apk()); + handler.post( + () -> { + if (foreground && g == generation && currentDialog == dialog) { + updateBusy = false; + progress.setText("Confirm the update in Android's installer."); + } + }); + return; + } handler.post( () -> { if (!foreground || g != generation || currentDialog != dialog) return; updateBusy = false; - if (!getPackageManager().canRequestPackageInstalls()) { - installUpdate(); - return; - } - dismiss(); - Intent installer = - new Intent(Intent.ACTION_VIEW) - .setDataAndType( - UpdateProvider.URI, "application/vnd.android.package-archive") - .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - .putExtra(Intent.EXTRA_RETURN_RESULT, true); - installer.setClipData(ClipData.newRawUri("Luma update", UpdateProvider.URI)); - try { - startActivityForResult(installer, 74); - } catch (RuntimeException e) { - updatesSettings(); - toast("Android's APK installer is unavailable"); - } + installUpdate(); }); } catch (Exception e) { handler.post( diff --git a/app/src/main/java/tv/luma/launcher/UpdateInstaller.java b/app/src/main/java/tv/luma/launcher/UpdateInstaller.java new file mode 100644 index 0000000..3e76343 --- /dev/null +++ b/app/src/main/java/tv/luma/launcher/UpdateInstaller.java @@ -0,0 +1,118 @@ +package tv.luma.launcher; + +import android.app.*; +import android.content.*; +import android.content.pm.PackageInstaller; +import android.content.pm.PackageManager; +import android.os.Build; +import java.io.*; + +/** Stages a downloaded self-update through Android's user-confirmed session API. */ +public final class UpdateInstaller extends BroadcastReceiver { + private static final String STATUS = "tv.luma.launcher.UPDATE_INSTALL_STATUS"; + + static void install(Context context, Updater.Release release, File apk) throws Exception { + PackageInstaller installer = context.getPackageManager().getPackageInstaller(); + android.content.SharedPreferences prefs = + context.getSharedPreferences("updates", Context.MODE_PRIVATE); + int old = prefs.getInt("session", -1); + if (old >= 0) + try { + installer.abandonSession(old); + } catch (Exception ignored) { + } + PackageInstaller.SessionParams parameters = + new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL); + parameters.setAppPackageName(context.getPackageName()); + parameters.setSize(apk.length()); + parameters.setInstallReason(PackageManager.INSTALL_REASON_USER); + if (Build.VERSION.SDK_INT >= 31) + parameters.setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_REQUIRED); + if (Build.VERSION.SDK_INT >= 33) + parameters.setPackageSource(PackageInstaller.PACKAGE_SOURCE_DOWNLOADED_FILE); + int id = installer.createSession(parameters); + boolean committed = false; + try (PackageInstaller.Session session = installer.openSession(id)) { + try (InputStream input = new FileInputStream(apk); + OutputStream output = session.openWrite("Luma.apk", 0, apk.length())) { + byte[] buffer = new byte[32768]; + int n; + while ((n = input.read(buffer)) != -1) { + if (Thread.currentThread().isInterrupted()) + throw new InterruptedIOException("Installation cancelled."); + output.write(buffer, 0, n); + } + session.fsync(output); + } + Intent result = new Intent(context, UpdateInstaller.class).setAction(STATUS); + int flags = PendingIntent.FLAG_UPDATE_CURRENT; + if (Build.VERSION.SDK_INT >= 31) flags |= PendingIntent.FLAG_MUTABLE; + PendingIntent receiver = PendingIntent.getBroadcast(context, id, result, flags); + prefs + .edit() + .putInt("session", id) + .putLong("installing", release.code) + .putLong("installStarted", System.currentTimeMillis()) + .apply(); + session.commit(receiver.getIntentSender()); + committed = true; + } finally { + if (!committed) { + installer.abandonSession(id); + prefs.edit().remove("session").remove("installing").apply(); + } + } + } + + @Override + public void onReceive(Context context, Intent intent) { + if (!STATUS.equals(intent.getAction())) return; + int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE); + android.content.SharedPreferences prefs = + context.getSharedPreferences("updates", Context.MODE_PRIVATE); + if (status == PackageInstaller.STATUS_PENDING_USER_ACTION) { + Intent confirm = + Build.VERSION.SDK_INT >= 33 + ? intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent.class) + : intent.getParcelableExtra(Intent.EXTRA_INTENT); + if (confirm != null) + try { + context.startActivity(confirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + } catch (RuntimeException error) { + prefs.edit().putString("error", "Open Updates again to confirm installation.").apply(); + } + } else if (status == PackageInstaller.STATUS_SUCCESS) { + prefs.edit().remove("session").remove("error").apply(); + UpdateJob.schedule(context); + returnHome(context); + } else { + prefs + .edit() + .remove("session") + .remove("installing") + .putString( + "error", + status == PackageInstaller.STATUS_FAILURE_ABORTED + ? "Installation cancelled." + : "Android could not install the update. Check free space and try again.") + .apply(); + } + } + + static void returnHome(Context context) { + android.content.SharedPreferences prefs = + context.getSharedPreferences("updates", Context.MODE_PRIVATE); + long requested = prefs.getLong("installing", 0); + if (requested <= 0 + || requested > new Updater(context).installedCode() + || System.currentTimeMillis() - prefs.getLong("installStarted", 0) > 5 * 60000L) return; + try { + context.startActivity( + new Intent(context, MainActivity.class) + .setAction(Intent.ACTION_MAIN) + .addCategory(Intent.CATEGORY_HOME) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + } catch (RuntimeException ignored) { + } + } +} diff --git a/app/src/main/java/tv/luma/launcher/UpdateReceiver.java b/app/src/main/java/tv/luma/launcher/UpdateReceiver.java index 2473ca8..11e0208 100644 --- a/app/src/main/java/tv/luma/launcher/UpdateReceiver.java +++ b/app/src/main/java/tv/luma/launcher/UpdateReceiver.java @@ -6,6 +6,9 @@ import android.content.*; public final class UpdateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) UpdateJob.schedule(context); + if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) { + UpdateJob.schedule(context); + UpdateInstaller.returnHome(context); + } } } diff --git a/app/src/main/java/tv/luma/launcher/Updater.java b/app/src/main/java/tv/luma/launcher/Updater.java index b1c8d84..0ab4254 100644 --- a/app/src/main/java/tv/luma/launcher/Updater.java +++ b/app/src/main/java/tv/luma/launcher/Updater.java @@ -178,6 +178,7 @@ final class Updater { .apply(); if (release.code <= installedCode()) { deleteDownload(); + prefs.edit().remove("installing").remove("session").apply(); context.getSystemService(NotificationManager.class).cancel(NOTICE); return null; } @@ -207,6 +208,7 @@ final class Updater { if (new JSONObject(prefs.getString("release", "")).getLong("versionCode") <= installedCode()) { deleteDownload(); + prefs.edit().remove("installing").remove("session").apply(); context.getSystemService(NotificationManager.class).cancel(NOTICE); } } catch (Exception ignored) { diff --git a/output/Luma.apk b/output/Luma.apk index 66693d3..7a35b7d 100644 Binary files a/output/Luma.apk and b/output/Luma.apk differ diff --git a/output/Luma.apk.idsig b/output/Luma.apk.idsig index b44ca8f..8963cba 100644 Binary files a/output/Luma.apk.idsig and b/output/Luma.apk.idsig differ diff --git a/output/update.json b/output/update.json index a46d51e..4c8835c 100644 --- a/output/update.json +++ b/output/update.json @@ -1,11 +1,11 @@ { "schema": 1, "packageName": "tv.luma.launcher", - "versionCode": 4, - "versionName": "1.1.1", + "versionCode": 6, + "versionName": "1.1.3", "minSdk": 26, "apk": "Luma.apk", - "size": 2749526, - "sha256": "b145da41c95c927562f41d6052443723e377c7c40e775c078be0aa41ffefd19b", + "size": 2753622, + "sha256": "e7fb522d03db6e30f57abfde5424f34516229eac90c4c148aef283de2528d6a3", "notes": "Adds scheduled update checks, TV notifications, and downloading/installing Luma updates directly from Settings. Includes the fix for missing app images and wallpaper after TV wake. Your appearance, screensaver, app order, and Home routing settings are preserved." }