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:
parent
0d1a11d943
commit
c04dcca60d
10 changed files with 147 additions and 28 deletions
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
118
app/src/main/java/tv/luma/launcher/UpdateInstaller.java
Normal file
118
app/src/main/java/tv/luma/launcher/UpdateInstaller.java
Normal 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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue