fix(update): 声明 REQUEST_INSTALL_PACKAGES 权限并加固安装流程
Some checks are pending
Release / build (push) Waiting to run
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:
parent
e2bf924246
commit
5adf053b6f
@ -16,8 +16,8 @@ android {
|
|||||||
applicationId = "com.rainy.token"
|
applicationId = "com.rainy.token"
|
||||||
minSdk = 31
|
minSdk = 31
|
||||||
targetSdk = 35
|
targetSdk = 35
|
||||||
versionCode = 17
|
versionCode = 18
|
||||||
versionName = "1.6.7"
|
versionName = "1.6.8"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<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="com.android.launcher.permission.INSTALL_SHORTCUT" />
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
|
|
||||||
|
|||||||
@ -80,19 +80,31 @@ class UpdateViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun install(file: File) {
|
fun install(file: File) {
|
||||||
if (context.packageManager.canRequestPackageInstalls()) {
|
if (!context.packageManager.canRequestPackageInstalls()) {
|
||||||
|
_uiState.update { it.copy(phase = Phase.NeedInstallPermission) }
|
||||||
|
return
|
||||||
|
}
|
||||||
_uiState.update { it.copy(phase = Phase.Ready(file)) }
|
_uiState.update { it.copy(phase = Phase.Ready(file)) }
|
||||||
val authority = context.packageName + ".fileprovider"
|
val authority = context.packageName + ".fileprovider"
|
||||||
val uri = FileProvider.getUriForFile(context, authority, file)
|
val uri = try {
|
||||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
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")
|
setDataAndType(uri, "application/vnd.android.package-archive")
|
||||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
}
|
}
|
||||||
runCatching { context.startActivity(intent) }.onFailure { e ->
|
val started = runCatching { context.startActivity(actionView) }
|
||||||
_uiState.update { it.copy(phase = Phase.Error(e.message ?: "cannot start installer")) }
|
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(actionInstall) }.onFailure { e ->
|
||||||
|
_uiState.update { it.copy(phase = Phase.Error(e.message ?: "无法启动安装器")) }
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
_uiState.update { it.copy(phase = Phase.NeedInstallPermission) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user