fix(update): 声明 REQUEST_INSTALL_PACKAGES 权限并加固安装流程
Some checks are pending
Release / build (push) Waiting to run

- 缺少该权限时部分 ROM 启动系统安装器会被拒绝/闪退
- install() 整体容错:FileProvider URI 失败、ACTION_VIEW 失败均捕获并回退 ACTION_INSTALL_PACKAGE
- 版本升至 1.6.8
This commit is contained in:
Liuxinyu176 2026-10-09 09:38:52 +08:00
parent e2bf924246
commit 5adf053b6f
3 changed files with 24 additions and 11 deletions

View File

@ -16,8 +16,8 @@ android {
applicationId = "com.rainy.token"
minSdk = 31
targetSdk = 35
versionCode = 17
versionName = "1.6.7"
versionCode = 18
versionName = "1.6.8"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {

View File

@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

View File

@ -80,19 +80,31 @@ class UpdateViewModel @Inject constructor(
}
fun install(file: File) {
if (context.packageManager.canRequestPackageInstalls()) {
_uiState.update { it.copy(phase = Phase.Ready(file)) }
val authority = context.packageName + ".fileprovider"
val uri = FileProvider.getUriForFile(context, authority, file)
val intent = Intent(Intent.ACTION_VIEW).apply {
if (!context.packageManager.canRequestPackageInstalls()) {
_uiState.update { it.copy(phase = Phase.NeedInstallPermission) }
return
}
_uiState.update { it.copy(phase = Phase.Ready(file)) }
val authority = context.packageName + ".fileprovider"
val uri = try {
FileProvider.getUriForFile(context, authority, file)
} catch (e: Exception) {
_uiState.update { it.copy(phase = Phase.Error("安装文件无效:" + (e.message ?: "未知错误"))) }
return
}
val actionView = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
}
val started = runCatching { context.startActivity(actionView) }
if (started.isFailure) {
val actionInstall = Intent(Intent.ACTION_INSTALL_PACKAGE).apply {
setDataAndType(uri, "application/vnd.android.package-archive")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
}
runCatching { context.startActivity(intent) }.onFailure { e ->
_uiState.update { it.copy(phase = Phase.Error(e.message ?: "cannot start installer")) }
runCatching { context.startActivity(actionInstall) }.onFailure { e ->
_uiState.update { it.copy(phase = Phase.Error(e.message ?: "无法启动安装器")) }
}
} else {
_uiState.update { it.copy(phase = Phase.NeedInstallPermission) }
}
}
}