159 lines
6.6 KiB
Java
159 lines
6.6 KiB
Java
package dev.ultra.launcher;
|
|
|
|
import android.app.PendingIntent;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageInstaller;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Build;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
/**
|
|
* The network and installer half of self-update. Which release is newer, when to check and
|
|
* whether to offer a build the user cancelled are decided by the page (updater/ in MoonBit);
|
|
* this only fetches URLs and hands an APK to PackageInstaller.
|
|
*/
|
|
final class Updater {
|
|
static final String EXTRA_VERSION = "dev.ultra.launcher.VERSION";
|
|
|
|
interface Done { void result(boolean ok, String text); }
|
|
|
|
private Updater() {}
|
|
|
|
private static SharedPreferences prefs(Context c) {
|
|
return c.getSharedPreferences("updater", Context.MODE_PRIVATE);
|
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
|
static int installedVersion(Context c) {
|
|
try {
|
|
PackageInfo p = c.getPackageManager().getPackageInfo(c.getPackageName(), 0);
|
|
return (int) (Build.VERSION.SDK_INT >= 28 ? p.getLongVersionCode() : p.versionCode);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static int declinedVersion(Context c) {
|
|
return (int) prefs(c).getLong("declined", -1);
|
|
}
|
|
|
|
/** GET a URL as text on a background thread. */
|
|
static void get(Context c, String url, Done done) {
|
|
new Thread(() -> {
|
|
try {
|
|
HttpURLConnection conn = open(url);
|
|
try (InputStream in = conn.getInputStream()) {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
byte[] buf = new byte[8192];
|
|
for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n);
|
|
done.result(true, new String(out.toByteArray(), StandardCharsets.UTF_8));
|
|
} finally {
|
|
conn.disconnect();
|
|
}
|
|
} catch (IOException | RuntimeException e) {
|
|
Log.w("ultra", "GET " + url, e);
|
|
done.result(false, String.valueOf(e.getMessage()));
|
|
}
|
|
}, "updater-get").start();
|
|
}
|
|
|
|
/** Stream the APK at url into a PackageInstaller session and commit it. */
|
|
static void install(Context ctx, String url, int version) {
|
|
Context c = ctx.getApplicationContext();
|
|
new Thread(() -> {
|
|
try {
|
|
installNow(c, url, version);
|
|
} catch (IOException | RuntimeException e) {
|
|
Log.w("ultra", "update install", e);
|
|
toast(c, "Launcher update failed");
|
|
}
|
|
}, "updater-install").start();
|
|
}
|
|
|
|
private static void installNow(Context c, String url, int version) throws IOException {
|
|
PackageInstaller pi = c.getPackageManager().getPackageInstaller();
|
|
PackageInstaller.SessionParams params =
|
|
new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
|
|
params.setAppPackageName(c.getPackageName());
|
|
if (Build.VERSION.SDK_INT >= 31) {
|
|
params.setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED);
|
|
}
|
|
int id = pi.createSession(params);
|
|
try (PackageInstaller.Session s = pi.openSession(id)) {
|
|
HttpURLConnection conn = open(url);
|
|
try (InputStream in = conn.getInputStream();
|
|
OutputStream out = s.openWrite("ultra-launcher.apk", 0, conn.getContentLengthLong())) {
|
|
byte[] buf = new byte[64 * 1024];
|
|
for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n);
|
|
s.fsync(out);
|
|
} finally {
|
|
conn.disconnect();
|
|
}
|
|
Intent done = new Intent(c, InstallReceiver.class).putExtra(EXTRA_VERSION, (long) version);
|
|
int flags = PendingIntent.FLAG_UPDATE_CURRENT
|
|
| (Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0);
|
|
s.commit(PendingIntent.getBroadcast(c, id, done, flags).getIntentSender());
|
|
} catch (IOException | RuntimeException e) {
|
|
pi.abandonSession(id);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
private static HttpURLConnection open(String url) throws IOException {
|
|
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
|
conn.setConnectTimeout(15000);
|
|
conn.setReadTimeout(30000);
|
|
conn.setUseCaches(false);
|
|
conn.setInstanceFollowRedirects(true);
|
|
int code = conn.getResponseCode();
|
|
if (code != HttpURLConnection.HTTP_OK) {
|
|
conn.disconnect();
|
|
throw new IOException("HTTP " + code + " for " + url);
|
|
}
|
|
return conn;
|
|
}
|
|
|
|
private static void toast(Context c, String msg) {
|
|
new android.os.Handler(android.os.Looper.getMainLooper())
|
|
.post(() -> Toast.makeText(c, msg, Toast.LENGTH_LONG).show());
|
|
}
|
|
|
|
/** Hears back from the install session: shows Android's confirm screen, or notes the outcome. */
|
|
public static final class InstallReceiver extends BroadcastReceiver {
|
|
@Override
|
|
@SuppressWarnings("deprecation")
|
|
public void onReceive(Context c, Intent intent) {
|
|
int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE);
|
|
switch (status) {
|
|
case PackageInstaller.STATUS_PENDING_USER_ACTION:
|
|
Intent confirm = intent.getParcelableExtra(Intent.EXTRA_INTENT);
|
|
if (confirm != null) c.startActivity(confirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
|
break;
|
|
case PackageInstaller.STATUS_SUCCESS:
|
|
break; // the new build replaces this process
|
|
case PackageInstaller.STATUS_FAILURE_ABORTED:
|
|
prefs(c).edit().putLong("declined", intent.getLongExtra(EXTRA_VERSION, -1)).apply();
|
|
break;
|
|
default:
|
|
String why = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
|
|
Log.w("ultra", "update install failed: " + status + " " + why);
|
|
Toast.makeText(c, "Launcher update failed" + (why == null ? "" : ": " + why),
|
|
Toast.LENGTH_LONG).show();
|
|
}
|
|
}
|
|
}
|
|
}
|