fix: 注册PackageInstallerModule原生模块(MainApplication+ReactPackage)
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,24 @@
|
|||||||
package dev.yuzu.gca
|
package dev.yuzu.gca
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import com.facebook.react.PackageList
|
||||||
|
import com.facebook.react.ReactApplication
|
||||||
|
import com.facebook.react.ReactNativeHost
|
||||||
|
import com.facebook.react.ReactPackage
|
||||||
|
import com.facebook.react.defaults.DefaultReactNativeHost
|
||||||
|
import com.facebook.soloader.SoLoader
|
||||||
|
|
||||||
class MainApplication : Application()
|
class MainApplication : Application(), ReactApplication {
|
||||||
|
override val reactNativeHost: ReactNativeHost =
|
||||||
|
object : DefaultReactNativeHost(this) {
|
||||||
|
override fun getPackages(): List<ReactPackage> =
|
||||||
|
PackageList(this).packages.apply { add(PackageInstallerPackage()) }
|
||||||
|
override fun getJSMainModuleName() = "index"
|
||||||
|
override fun getUseDeveloperSupport() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreate() {
|
||||||
|
super.onCreate()
|
||||||
|
SoLoader.init(this, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package dev.yuzu.gca
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageInstaller
|
||||||
|
import com.facebook.react.bridge.*
|
||||||
|
import java.io.File
|
||||||
|
import java.security.MessageDigest
|
||||||
|
|
||||||
|
class PackageInstallerModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
|
||||||
|
override fun getName() = "PackageInstallerModule"
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
fun installApk(filePath: String, expectedSha256: String, promise: Promise) {
|
||||||
|
try {
|
||||||
|
val context = reactApplicationContext
|
||||||
|
val apkFile = File(filePath)
|
||||||
|
if (!apkFile.exists()) { promise.reject("NOT_FOUND", "APK not found"); return }
|
||||||
|
|
||||||
|
if (!context.packageManager.canRequestPackageInstalls()) {
|
||||||
|
promise.reject("PERMISSION", "Install permission not granted"); return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SHA256 verify
|
||||||
|
val sha256 = MessageDigest.getInstance("SHA-256")
|
||||||
|
apkFile.inputStream().use { input ->
|
||||||
|
val buf = ByteArray(8192); var read: Int
|
||||||
|
while (input.read(buf).also { read = it } != -1) sha256.update(buf, 0, read)
|
||||||
|
}
|
||||||
|
if (sha256.digest().joinToString("") { "%02x".format(it) } != expectedSha256) {
|
||||||
|
promise.reject("SHA256", "SHA256 mismatch"); return
|
||||||
|
}
|
||||||
|
|
||||||
|
val pi = context.packageManager.packageInstaller
|
||||||
|
val sessionId = pi.createSession(PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL))
|
||||||
|
val session = pi.openSession(sessionId)
|
||||||
|
try {
|
||||||
|
apkFile.inputStream().use { input ->
|
||||||
|
session.openWrite("package", 0, apkFile.length()).use { output ->
|
||||||
|
input.copyTo(output); session.fsync(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)
|
||||||
|
?: run { promise.reject("NO_INTENT", "No launch intent"); return }
|
||||||
|
session.commit(PendingIntent.getActivity(context, 0, launchIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE).intentSender)
|
||||||
|
promise.resolve("installed")
|
||||||
|
} finally { session.close() }
|
||||||
|
} catch (e: Exception) { promise.reject("ERROR", e.message) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package dev.yuzu.gca
|
||||||
|
|
||||||
|
import com.facebook.react.ReactPackage
|
||||||
|
import com.facebook.react.bridge.NativeModule
|
||||||
|
import com.facebook.react.bridge.ReactApplicationContext
|
||||||
|
import com.facebook.react.uimanager.ViewManager
|
||||||
|
|
||||||
|
class PackageInstallerPackage : ReactPackage {
|
||||||
|
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> =
|
||||||
|
listOf(PackageInstallerModule(reactContext))
|
||||||
|
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> = emptyList()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user