161 lines
6.1 KiB
Kotlin
161 lines
6.1 KiB
Kotlin
package com.gh.common.util
|
||
|
||
import androidx.lifecycle.DefaultLifecycleObserver
|
||
import androidx.lifecycle.LifecycleOwner
|
||
import com.gh.download.DownloadManager
|
||
import com.gh.download.PackageObserver
|
||
import com.gh.gamecenter.common.utils.NewFlatLogUtils
|
||
import com.gh.gamecenter.common.utils.SensorsBridge
|
||
import com.gh.gamecenter.eventbus.EBPackage
|
||
import com.gh.gamecenter.manager.PackagesManager
|
||
import com.gh.gamecenter.packagehelper.PackageRepository
|
||
import com.lightgame.utils.Utils
|
||
import org.greenrobot.eventbus.EventBus
|
||
|
||
object PackageChangeHelper : DefaultLifecycleObserver {
|
||
|
||
private const val TAG = "PackageChangeHelper"
|
||
|
||
private const val INSTALL_PENDING = 1
|
||
private const val UNINSTALL_PENDING = 2
|
||
private const val UPDATE_PENDING = 3
|
||
|
||
// <包名,pending 类型,应用版本> Triple
|
||
private var pendingPackagePair: Triple<String, Int, String>? = null
|
||
private var pendingGhId: String ? = null
|
||
|
||
/**
|
||
* 添加一个等待中,待确定是否已成功安装的应用
|
||
*/
|
||
fun addInstallPendingPackage(packageName: String) {
|
||
val installData = PackagesManager.getInstalledData(packageName)
|
||
|
||
if (installData == null) {
|
||
Utils.log(TAG, "添加了: $packageName 包名等待安装成功")
|
||
pendingPackagePair = Triple(packageName, INSTALL_PENDING, "")
|
||
} else {
|
||
Utils.log(TAG, "添加了: $packageName 包名等待安装更新成功")
|
||
|
||
val ghId = PackageUtils.getGhId(packageName)
|
||
|
||
// 记录光环插件相关信息,用于安装成功后的处理
|
||
if (ghId != null) {
|
||
pendingGhId = ghId.toString()
|
||
}
|
||
|
||
pendingPackagePair = Triple(packageName, UPDATE_PENDING, installData.version)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 添加一个等待中,待确定是否已成功卸载的应用
|
||
*/
|
||
fun addUninstallPendingPackage(packageName: String) {
|
||
Utils.log(TAG, "添加了: $packageName 包名等待卸载成功")
|
||
pendingPackagePair = Triple(packageName, UNINSTALL_PENDING, "")
|
||
}
|
||
|
||
override fun onResume(owner: LifecycleOwner) {
|
||
super.onResume(owner)
|
||
|
||
if (pendingPackagePair != null) {
|
||
val packageName = pendingPackagePair?.first ?: return
|
||
val isInstallPending = pendingPackagePair?.second == INSTALL_PENDING
|
||
val isUninstallPending = pendingPackagePair?.second == UNINSTALL_PENDING
|
||
val isUpdatePending = pendingPackagePair?.second == UPDATE_PENDING
|
||
|
||
val pendingVersion = pendingPackagePair?.third ?: ""
|
||
|
||
val installedVersionName = PackageUtils.getVersionNameByPackageName(packageName)
|
||
val isInstalled = installedVersionName != null
|
||
|
||
if (isInstallPending && isInstalled) {
|
||
pendingPackagePair = null
|
||
pendingGhId = null
|
||
|
||
PackageRepository.addInstalledGame(packageName)
|
||
|
||
performInstallSuccessAction(packageName)
|
||
} else if (isUninstallPending && !isInstalled) {
|
||
pendingPackagePair = null
|
||
pendingGhId = null
|
||
|
||
performUninstallSuccessAction(packageName)
|
||
} else if (isUpdatePending) {
|
||
val isUpdateValid = if (installedVersionName != pendingVersion) {
|
||
true
|
||
} else {
|
||
!pendingGhId.isNullOrEmpty() && pendingGhId != PackageUtils.getGhId(packageName).toString()
|
||
}
|
||
|
||
pendingPackagePair = null
|
||
pendingGhId = null
|
||
|
||
if (isUpdateValid) {
|
||
performUninstallSuccessAction(packageName)
|
||
performInstallSuccessAction(packageName)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
fun addInstall(packageName: String) {
|
||
performInstallSuccessAction(packageName)
|
||
}
|
||
|
||
fun addUpdate(packageName: String) {
|
||
performUninstallSuccessAction(packageName)
|
||
performInstallSuccessAction(packageName)
|
||
}
|
||
|
||
/**
|
||
* 对应包名安装成功后的操作,继承至 PackageChangeBroadcastObserver
|
||
*/
|
||
private fun performInstallSuccessAction(packageName: String, withLog: Boolean = true) {
|
||
Utils.log(TAG, "安装了: $packageName 包名的程序")
|
||
val downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshotByPackageName(packageName)
|
||
val gameId = if (downloadEntity != null && downloadEntity.gameId != null) downloadEntity.gameId else ""
|
||
val gameName = if (downloadEntity != null && downloadEntity.name != null) downloadEntity.name else ""
|
||
|
||
if (withLog) {
|
||
NewFlatLogUtils.logGameInstallComplete(gameId, gameName)
|
||
SensorsBridge.trackInstallGameFinish(gameId, gameName)
|
||
}
|
||
|
||
InstallUtils.getInstance().removeInstall(packageName)
|
||
PackageHelper.refreshLocalPackageList()
|
||
|
||
val versionName = PackageUtils.getVersionNameByPackageName(packageName)
|
||
val installEb = EBPackage(EBPackage.TYPE_INSTALLED, packageName, versionName)
|
||
|
||
PackageObserver.onPackageChanged(installEb)
|
||
EventBus.getDefault().post(installEb)
|
||
}
|
||
|
||
fun addUninstall(packageName: String) {
|
||
performUninstallSuccessAction(packageName)
|
||
}
|
||
|
||
/**
|
||
* 对应包名卸载成功后的操作,继承至 PackageChangeBroadcastObserver
|
||
*/
|
||
private fun performUninstallSuccessAction(packageName: String, withLog: Boolean = true) {
|
||
Utils.log(TAG, "卸载了: $packageName 包名的程序")
|
||
val install = PackagesManager.getInstalledData(packageName)
|
||
val gameId = if (install?.id != null) install.id else ""
|
||
val gameName = if (install?.name != null) install.name else ""
|
||
|
||
if (withLog) {
|
||
NewFlatLogUtils.logGameUninstallComplete(gameId!!, gameName!!)
|
||
SensorsBridge.trackUnloadGameFinish(gameId, gameName)
|
||
}
|
||
|
||
InstallUtils.getInstance().removeUninstall(packageName)
|
||
PackageHelper.refreshLocalPackageList()
|
||
|
||
val uninstallEb = EBPackage(EBPackage.TYPE_UNINSTALLED, packageName, "")
|
||
PackageObserver.onPackageChanged(uninstallEb)
|
||
EventBus.getDefault().post(uninstallEb)
|
||
}
|
||
|
||
} |