Use user-confirmed package sessions for TV updates

Stage the verified download with Android PackageInstaller rather than launching a loose APK file. Label the package source accurately, require user confirmation, and return to Luma after a self-update.
This commit is contained in:
Elijah Kuntz 2026-10-18 09:00:55 -07:00
parent 0d1a11d943
commit c04dcca60d
10 changed files with 147 additions and 28 deletions

View file

@ -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

View file

@ -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 }
}

View file

@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="4" android:versionName="1.1.1">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="6" android:versionName="1.1.3">
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="34" />
<uses-feature android:name="android.software.leanback" android:required="true" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
@ -37,5 +37,6 @@
<intent-filter><action android:name="android.intent.action.MY_PACKAGE_REPLACED" /></intent-filter>
</receiver>
<provider android:name=".UpdateProvider" android:authorities="tv.luma.launcher.updates" android:exported="false" android:grantUriPermissions="true" />
<receiver android:name=".UpdateInstaller" android:exported="false" />
</application>
</manifest>

View file

@ -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(

View file

@ -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) {
}
}
}

View file

@ -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);
}
}
}

View file

@ -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) {

Binary file not shown.

Binary file not shown.

View file

@ -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."
}