feat: support silent app updates and background downloading

This commit is contained in:
zhoubinjia committed 2026-04-29 11:27:27 +08:00
1 parent 7ffd01f6f2
commit 20982b8969
5 files changed
+249 -28

No files matched your search

@@ -4,17 +4,28 @@ import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.core.content.FileProvider
import androidx.core.net.toUri
import com.hinnka.mycamera.BuildConfig
import com.hinnka.mycamera.utils.PLog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.util.concurrent.TimeUnit
@@ -28,11 +39,21 @@ object AppUpdateManager {
private const val TAG = "AppUpdateManager"
private const val VERSION_CHECK_URL = "https://camera-api.hinnka.com/api/version/check"
private const val APK_MIME_TYPE = "application/vnd.android.package-archive"
private const val APK_MAGIC_0 = 'P'.code.toByte()
private const val APK_MAGIC_1 = 'K'.code.toByte()
private const val HTTP_RANGE_NOT_SATISFIABLE = 416
private const val MAX_REDIRECTS = 5
private val client = OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build()
private val downloadScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val downloadLock = Any()
private var activeDownload: Pair<String, Deferred<File>>? = null
private var activeSilentCheck: Deferred<Unit>? = null
private val _readyApk = MutableStateFlow<File?>(null)
val readyApk: StateFlow<File?> = _readyApk.asStateFlow()
suspend fun checkForUpdate(currentVersion: String = BuildConfig.VERSION_NAME): AppUpdateRelease? =
withContext(Dispatchers.IO) {
@@ -45,7 +66,7 @@ object AppUpdateManager {
runCatching {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
PLog.w(TAG, "Version check failed: code=${response.code}")
PLog.w(TAG, "Version check failed: code=${response.code} url=${request.url}")
return@withContext null
}
@@ -81,39 +102,41 @@ object AppUpdateManager {
}
suspend fun downloadApk(context: Context, release: AppUpdateRelease): File =
withContext(Dispatchers.IO) {
runCatching {
val request = Request.Builder()
.url(release.downloadUrl)
.get()
.build()
val updateDir = File(context.cacheDir, "updates").apply { mkdirs() }
val apkFile = File(updateDir, release.fileName.sanitizeApkFileName())
getOrStartDownload(context.applicationContext, release).await()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw IllegalStateException("Download failed: code=${response.code}")
}
response.body?.byteStream()?.use { input ->
apkFile.outputStream().use { output ->
input.copyTo(output)
}
} ?: throw IllegalStateException("Download body is empty")
fun startSilentUpdate(context: Context) {
val appContext = context.applicationContext
synchronized(downloadLock) {
if (activeSilentCheck?.isActive == true) return
activeSilentCheck = downloadScope.async {
runCatching {
val release = checkForUpdate() ?: return@async
val apkFile = findDownloadedApk(appContext, release)
?: getOrStartDownload(appContext, release).await()
_readyApk.value = apkFile
}.onFailure { error ->
PLog.e(TAG, "Silent update failed", error)
}
PLog.d(TAG, "APK downloaded: ${apkFile.absolutePath}, size=${apkFile.length()}")
apkFile
}.onFailure { error ->
PLog.e(TAG, "APK download error", error)
}.getOrThrow()
}
}
}
fun findDownloadedApk(context: Context, release: AppUpdateRelease): File? {
val apkFile = resolveApkFile(context.applicationContext, release)
return apkFile.takeIf { it.isCompleteApk() }
}
fun consumeReadyApk(apkFile: File?) {
if (apkFile == null || _readyApk.value == apkFile) {
_readyApk.value = null
}
}
fun startInstall(context: Context, apkFile: File): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !context.packageManager.canRequestPackageInstalls()) {
if (!context.packageManager.canRequestPackageInstalls()) {
val permissionIntent = Intent(
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
Uri.parse("package:${context.packageName}")
"package:${context.packageName}".toUri()
).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
@@ -152,4 +175,119 @@ object AppUpdateManager {
val safeName = replace(Regex("[^A-Za-z0-9._-]"), "_")
return if (safeName.endsWith(".apk", ignoreCase = true)) safeName else "$safeName.apk"
}
private fun getOrStartDownload(context: Context, release: AppUpdateRelease): Deferred<File> {
val key = "${release.downloadUrl}|${release.fileName}"
synchronized(downloadLock) {
activeDownload?.let { (activeKey, activeDeferred) ->
if (activeKey == key && activeDeferred.isActive) return activeDeferred
}
val deferred = downloadScope.async {
runCatching {
val apkFile = resolveApkFile(context, release)
if (apkFile.isCompleteApk()) {
PLog.d(TAG, "Reuse downloaded APK: ${apkFile.absolutePath}, size=${apkFile.length()}")
return@async apkFile
}
downloadApkFile(release.downloadUrl, apkFile)
if (!apkFile.isCompleteApk()) {
throw IllegalStateException("Downloaded APK is invalid: ${apkFile.absolutePath}")
}
PLog.d(TAG, "APK downloaded: ${apkFile.absolutePath}, size=${apkFile.length()}")
apkFile
}.onFailure { error ->
PLog.e(TAG, "APK download error", error)
}.getOrThrow()
}
activeDownload = key to deferred
return deferred
}
}
private fun resolveApkFile(context: Context, release: AppUpdateRelease): File {
val updateDir = File(
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
?: File(context.filesDir, Environment.DIRECTORY_DOWNLOADS),
"updates"
).apply { mkdirs() }
return File(updateDir, release.fileName.sanitizeApkFileName())
}
private fun downloadApkFile(downloadUrl: String, apkFile: File) {
val partFile = File("${apkFile.absolutePath}.part")
var connection: HttpURLConnection? = null
try {
val existingBytes = partFile.takeIf { it.exists() }?.length()?.coerceAtLeast(0L) ?: 0L
connection = openDownloadConnection(downloadUrl, existingBytes)
val responseCode = connection.responseCode
if (responseCode == HTTP_RANGE_NOT_SATISFIABLE && apkFile.isCompleteApk()) {
partFile.delete()
return
}
val append = existingBytes > 0L && responseCode == HttpURLConnection.HTTP_PARTIAL
if (responseCode !in listOf(HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_PARTIAL)) {
throw IllegalStateException("Download failed: code=$responseCode")
}
if (!append && partFile.exists()) {
partFile.delete()
}
connection.inputStream.use { input ->
FileOutputStream(partFile, append).use { output ->
input.copyTo(output)
}
}
if (!partFile.isCompleteApk()) {
throw IllegalStateException("Downloaded partial file is not a valid APK")
}
if (apkFile.exists()) apkFile.delete()
if (!partFile.renameTo(apkFile)) {
partFile.copyTo(apkFile, overwrite = true)
partFile.delete()
}
} finally {
connection?.disconnect()
}
}
private fun openDownloadConnection(downloadUrl: String, existingBytes: Long): HttpURLConnection {
var url = URL(downloadUrl)
repeat(MAX_REDIRECTS) {
val connection = (url.openConnection() as HttpURLConnection).apply {
instanceFollowRedirects = false
connectTimeout = 15_000
readTimeout = 0
requestMethod = "GET"
setRequestProperty("Accept", APK_MIME_TYPE)
if (existingBytes > 0L) {
setRequestProperty("Range", "bytes=$existingBytes-")
}
}
val responseCode = connection.responseCode
if (responseCode in 300..399) {
val location = connection.getHeaderField("Location")
connection.disconnect()
if (location.isNullOrBlank()) {
throw IllegalStateException("Download redirect missing location")
}
url = URL(url, location)
} else {
return connection
}
}
throw IllegalStateException("Too many download redirects")
}
private fun File.isCompleteApk(): Boolean {
if (!exists() || length() < 2L) return false
return inputStream().use { input ->
input.read().toByte() == APK_MAGIC_0 && input.read().toByte() == APK_MAGIC_1
}
}
}
@@ -1,6 +1,8 @@
package com.hinnka.mycamera.update
import android.content.Context
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.io.File
data class AppUpdateRelease(
@@ -10,11 +12,17 @@ data class AppUpdateRelease(
)
object AppUpdateManager {
val readyApk: StateFlow<File?> = MutableStateFlow(null)
suspend fun checkForUpdate(currentVersion: String = ""): AppUpdateRelease? = null
fun startSilentUpdate(context: Context) = Unit
suspend fun downloadApk(context: Context, release: AppUpdateRelease): File {
throw UnsupportedOperationException("App updates are disabled for the Google flavor.")
}
fun consumeReadyApk(apkFile: File?) = Unit
fun startInstall(context: Context, apkFile: File): Boolean = false
}
@@ -25,16 +25,19 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -48,6 +51,9 @@ import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
@@ -66,6 +72,7 @@ import com.hinnka.mycamera.ui.settings.FrameEditorScreen
import com.hinnka.mycamera.ui.settings.FrameManagementScreen
import com.hinnka.mycamera.ui.settings.SettingsScreen
import com.hinnka.mycamera.ui.theme.PhotonCameraTheme
import com.hinnka.mycamera.update.AppUpdateManager
import com.hinnka.mycamera.utils.BuglyHelper
import com.hinnka.mycamera.utils.OrientationObserver
import com.hinnka.mycamera.viewmodel.CameraViewModel
@@ -207,6 +214,7 @@ class MainActivity : ComponentActivity() {
}
)
}
AppUpdateInstallPrompt()
}
}
}
@@ -332,6 +340,70 @@ private fun StartupComposeReadyEffect() {
}
}
@Composable
private fun AppUpdateInstallPrompt() {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
var readyApk by remember { mutableStateOf<java.io.File?>(null) }
LaunchedEffect(lifecycleOwner) {
lifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
AppUpdateManager.readyApk.collect { apkFile ->
readyApk = apkFile
}
}
}
val apkFile = readyApk ?: return
AlertDialog(
onDismissRequest = {
AppUpdateManager.consumeReadyApk(apkFile)
readyApk = null
},
title = {
Text(
text = stringResource(R.string.update_ready_title),
style = MaterialTheme.typography.titleLarge
)
},
text = {
Text(
text = stringResource(R.string.update_ready_message),
style = MaterialTheme.typography.bodyMedium
)
},
confirmButton = {
TextButton(
onClick = {
val installStarted = AppUpdateManager.startInstall(context, apkFile)
if (installStarted) {
AppUpdateManager.consumeReadyApk(apkFile)
readyApk = null
} else {
android.widget.Toast.makeText(
context,
R.string.update_install_permission_hint,
android.widget.Toast.LENGTH_LONG
).show()
}
}
) {
Text(stringResource(R.string.update_install_now))
}
},
dismissButton = {
TextButton(
onClick = {
AppUpdateManager.consumeReadyApk(apkFile)
readyApk = null
}
) {
Text(stringResource(R.string.cancel))
}
}
)
}
@Composable
fun NavigationHost(
cameraViewModel: CameraViewModel,
@@ -8,6 +8,7 @@ import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.phantom.PhantomService
import com.hinnka.mycamera.phantom.PhantomShortcutActivity
import com.hinnka.mycamera.screencapture.PhantomPipPreviewCoordinator
import com.hinnka.mycamera.update.AppUpdateManager
import com.hinnka.mycamera.utils.BuglyHelper
import com.hinnka.mycamera.utils.DeviceUtil
import com.hinnka.mycamera.utils.StartupTrace
@@ -33,6 +34,7 @@ class MyCameraApplication : Application() {
phantomService = StartupTrace.measure("PhantomService()") {
PhantomService(this)
}
AppUpdateManager.startSilentUpdate(this)
val userPreferencesRepository = contentRepository.userPreferencesRepository
MainScope().launch {
+1
View File
@@ -3,4 +3,5 @@
<files-path name="my_photos" path="photos/" />
<cache-path name="shared_images" path="shared/" />
<cache-path name="updates" path="updates/" />
<external-files-path name="downloaded_updates" path="Download/updates/" />
</paths>