feat(garage_updates): an auto updater for garage apps
Asks Garage for the newest release of a project, downloads it with resume, checks the sha256, installs it beside the old one and restarts into it. Splash on garage_ui, a native stub and a first time installer for windows, bundle swap on mac, the system installer on android. No-op on web. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
444933b004
commit
d3b232f0ee
@@ -0,0 +1,9 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/workspace.xml
|
||||
/.idea/libraries
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.cxx
|
||||
@@ -0,0 +1,58 @@
|
||||
group = "net.imbenji.garage_updates"
|
||||
version = "1.0-SNAPSHOT"
|
||||
|
||||
buildscript {
|
||||
val kotlinVersion = "2.3.20"
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath("com.android.tools.build:gradle:9.0.1")
|
||||
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id("com.android.library")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "net.imbenji.garage_updates"
|
||||
|
||||
compileSdk = 36
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
getByName("main") {
|
||||
java.srcDirs("src/main/kotlin")
|
||||
}
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 24
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// FileProvider, the only way to hand the installer a file since android 7
|
||||
implementation("androidx.core:core:1.13.1")
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
rootProject.name = "garage_updates"
|
||||
@@ -0,0 +1,19 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- without this the system installer refuses an apk that came from us -->
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
|
||||
|
||||
<application>
|
||||
<!-- our own subclass + authority, so it cant collide with a file
|
||||
provider the app (or another plugin) allready declares -->
|
||||
<provider
|
||||
android:name="net.imbenji.garage_updates.GarageUpdatesFileProvider"
|
||||
android:authorities="${applicationId}.garage_updates.files"
|
||||
android:exported="false"
|
||||
android:grantUriPermissions="true">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/garage_updates_paths" />
|
||||
</provider>
|
||||
</application>
|
||||
</manifest>
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package net.imbenji.garage_updates
|
||||
|
||||
import androidx.core.content.FileProvider
|
||||
|
||||
// nothing in it. it exists so the manifest entry has a class name of its own
|
||||
class GarageUpdatesFileProvider : FileProvider()
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package net.imbenji.garage_updates
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.content.FileProvider
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import java.io.File
|
||||
|
||||
// The android half of garage_updates. Two things dart cant do for itself:
|
||||
// find out who installed the app, and hand an apk to the system installer.
|
||||
// The installer always asks the person, theres no silent path and we dont
|
||||
// want one.
|
||||
class GarageUpdatesPlugin : FlutterPlugin, MethodCallHandler {
|
||||
private lateinit var channel: MethodChannel
|
||||
private lateinit var context: Context
|
||||
|
||||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
context = binding.applicationContext
|
||||
channel = MethodChannel(binding.binaryMessenger, "net.imbenji.garage_updates")
|
||||
channel.setMethodCallHandler(this)
|
||||
}
|
||||
|
||||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: Result) {
|
||||
when (call.method) {
|
||||
"info" -> result.success(
|
||||
mapOf(
|
||||
"installer" to installerPackageName(),
|
||||
"cacheDir" to context.cacheDir.absolutePath,
|
||||
)
|
||||
)
|
||||
|
||||
"installerPackageName" -> result.success(installerPackageName())
|
||||
|
||||
"install" -> {
|
||||
val path = call.argument<String>("path")
|
||||
if (path == null) {
|
||||
result.error("bad_args", "install needs a path", null)
|
||||
return
|
||||
}
|
||||
install(File(path), result)
|
||||
}
|
||||
|
||||
else -> result.notImplemented()
|
||||
}
|
||||
}
|
||||
|
||||
// "com.android.vending" for a play store install, null for a sideload
|
||||
private fun installerPackageName(): String? {
|
||||
val pm = context.packageManager
|
||||
return try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
pm.getInstallSourceInfo(context.packageName).installingPackageName
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
pm.getInstallerPackageName(context.packageName)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "couldnt read the installer package name", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun install(apk: File, result: Result) {
|
||||
if (!apk.exists()) {
|
||||
result.error("missing", "No apk at ${apk.path}", null)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
val uri = FileProvider.getUriForFile(
|
||||
context,
|
||||
"${context.packageName}.garage_updates.files",
|
||||
apk,
|
||||
)
|
||||
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(uri, "application/vnd.android.package-archive")
|
||||
// the installer is another app, it needs lending the file
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
// we only have the application context, not an activity
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
|
||||
context.startActivity(intent)
|
||||
result.success(null)
|
||||
} catch (e: Exception) {
|
||||
// an apk outside the shared cache folder lands here too
|
||||
Log.e(TAG, "couldnt open the installer for ${apk.path}", e)
|
||||
result.error("install_failed", e.message ?: "The installer could not be opened", null)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "garage_updates"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- only the staging folder in the cache is ever shared, and only with the installer -->
|
||||
<paths>
|
||||
<cache-path name="garage_updates" path="garage_updates/" />
|
||||
</paths>
|
||||
Reference in New Issue
Block a user