This commit is contained in:
1 parent
c89c6a7e53
commit
72c5f75939
6 files changed
+77
-70
No files matched your search
@@ -14,6 +14,7 @@ import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -26,19 +27,20 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Updates the launcher from the newest release on V's fileapi forge (nandi.uk/launcher),
|
||||
* which .forge/jobs.toml builds on every commit to main. Each release holds
|
||||
* version.json ({"versionCode": N, "commit": SHA}) next to ultra-launcher.apk, with N the
|
||||
* APK's versionCode. When N is newer than the installed
|
||||
* build, the APK is streamed into a PackageInstaller session and committed. Android then asks
|
||||
* the user to confirm (or, on Android 12+ once this app installed itself, may not ask at all).
|
||||
* Updates the launcher from the newest release on the Gitea forge (experiments/launcher
|
||||
* on git.site.latha.org), which .gitea/workflows/launcher.yml builds on every commit to
|
||||
* main. Each release holds version.json ({"versionCode": N, "commit": SHA}) next to
|
||||
* ultra-launcher.apk, with N the APK's versionCode; the latest-release API names their
|
||||
* download URLs. When N is newer than the installed build, the APK is streamed into a
|
||||
* PackageInstaller session and committed. Android then asks the user to confirm (or, on
|
||||
* Android 12+ once this app installed itself, may not ask at all).
|
||||
*
|
||||
* The home screen checks on start at most every CHECK_EVERY; the home menu's
|
||||
* "Check for updates" checks now and reports what it found.
|
||||
*/
|
||||
final class Updater {
|
||||
static final String BASE =
|
||||
"https://fileapi.fastapicloud.dev/api/forge/repos/nandi.uk/launcher/releases/latest/assets/";
|
||||
static final String LATEST =
|
||||
"https://git.site.latha.org/api/v1/repos/experiments/launcher/releases/latest";
|
||||
static final long CHECK_EVERY = 6 * 60 * 60 * 1000L;
|
||||
static final String EXTRA_VERSION = "dev.ultra.launcher.VERSION";
|
||||
|
||||
@@ -75,7 +77,10 @@ final class Updater {
|
||||
}
|
||||
|
||||
private static void run(Context c, boolean manual) throws Exception {
|
||||
JSONObject v = new JSONObject(new String(get(BASE + "version.json"), StandardCharsets.UTF_8));
|
||||
JSONObject release = new JSONObject(new String(get(LATEST), StandardCharsets.UTF_8));
|
||||
String versionUrl = assetUrl(release, "version.json");
|
||||
String apkUrl = assetUrl(release, "ultra-launcher.apk");
|
||||
JSONObject v = new JSONObject(new String(get(versionUrl), StandardCharsets.UTF_8));
|
||||
long latest = v.getLong("versionCode");
|
||||
long installed = installedVersion(c);
|
||||
if (latest <= installed) {
|
||||
@@ -85,7 +90,17 @@ final class Updater {
|
||||
// An install the user cancelled isn't offered again until there is a newer one, or they ask.
|
||||
if (!manual && latest == prefs(c).getLong("declined", -1)) return;
|
||||
toast(c, "Downloading launcher update (build " + latest + ")…");
|
||||
install(c, latest);
|
||||
install(c, apkUrl, latest);
|
||||
}
|
||||
|
||||
/** The download URL of the named asset in a release's JSON. */
|
||||
private static String assetUrl(JSONObject release, String name) throws Exception {
|
||||
JSONArray assets = release.getJSONArray("assets");
|
||||
for (int i = 0; i < assets.length(); i++) {
|
||||
JSONObject a = assets.getJSONObject(i);
|
||||
if (name.equals(a.getString("name"))) return a.getString("browser_download_url");
|
||||
}
|
||||
throw new IOException("no " + name + " asset in the latest release");
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -94,7 +109,7 @@ final class Updater {
|
||||
return Build.VERSION.SDK_INT >= 28 ? p.getLongVersionCode() : p.versionCode;
|
||||
}
|
||||
|
||||
private static void install(Context c, long version) throws IOException {
|
||||
private static void install(Context c, String apkUrl, long version) throws IOException {
|
||||
PackageInstaller pi = c.getPackageManager().getPackageInstaller();
|
||||
PackageInstaller.SessionParams params =
|
||||
new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
|
||||
@@ -104,7 +119,7 @@ final class Updater {
|
||||
}
|
||||
int id = pi.createSession(params);
|
||||
try (PackageInstaller.Session s = pi.openSession(id)) {
|
||||
HttpURLConnection conn = open(BASE + "ultra-launcher.apk");
|
||||
HttpURLConnection conn = open(apkUrl);
|
||||
try (InputStream in = conn.getInputStream();
|
||||
OutputStream out = s.openWrite("ultra-launcher.apk", 0, conn.getContentLengthLong())) {
|
||||
byte[] buf = new byte[64 * 1024];
|
||||
@@ -128,7 +143,7 @@ final class Updater {
|
||||
conn.setConnectTimeout(15000);
|
||||
conn.setReadTimeout(30000);
|
||||
conn.setUseCaches(false);
|
||||
conn.setInstanceFollowRedirects(true); // github.com redirects assets to its download host
|
||||
conn.setInstanceFollowRedirects(true); // asset downloads redirect to their storage host
|
||||
int code = conn.getResponseCode();
|
||||
if (code != HttpURLConnection.HTTP_OK) {
|
||||
conn.disconnect();
|
||||
|
||||
Reference in new issue
Block a user