fix: 修复误判断造成的重复下载问题

This commit is contained in:
juntao
2022-06-23 14:13:31 +08:00
parent 559164ebce
commit 0af479e441
3 changed files with 55 additions and 39 deletions

View File

@ -103,6 +103,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
val vGameList = arrayListOf<GameEntity>()
for (entity in entityList) {
// TODO 添加更多的信息供页面确定是否有更新
vGameList.add(VHelper.toGameEntity(entity))
}

View File

@ -40,9 +40,9 @@ class VDownloadManagerViewModel(application: Application) :
val gameEntity = VHelper.toGameEntity(downloadEntity)
if (gameEntity.playedTime != 0L) {
gameEntity.des = "已畅玩${NumberUtils.transSimpleUsageTime(gameEntity.playedTime)}"
gameEntity.des = "已畅玩${NumberUtils.transSimpleUsageTime(gameEntity.playedTime / 1000 / 1000)}"
} else {
gameEntity.des = VHelper.getAppOccupiedSpace(downloadEntity.packageName).toProperReadableSize()
gameEntity.des = "已占用${VHelper.getAppOccupiedSpace(downloadEntity.packageName).toProperReadableSize()}"
}
vGameList.add(gameEntity)

View File

@ -1,6 +1,7 @@
package com.gh.vspace
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
@ -27,10 +28,7 @@ import com.gh.gamecenter.core.runOnUiThread
import com.gh.gamecenter.core.utils.EmptyCallback
import com.gh.gamecenter.core.utils.GsonUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.entity.ApkEntity
import com.gh.gamecenter.entity.AppEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.GameUpdateEntity
import com.gh.gamecenter.entity.*
import com.gh.gamecenter.eventbus.EBPackage
import com.gh.gamecenter.eventbus.EBReuse
import com.gh.gamecenter.packagehelper.PackageRepository
@ -45,6 +43,7 @@ import com.lightgame.utils.Utils
import io.reactivex.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus
import java.io.File
import java.util.*
object VHelper {
@ -59,6 +58,9 @@ object VHelper {
private var mUpdateEntity: AppEntity? = null
// 当前正卡在安装中的 VA 游戏,避免重复调用安装
private val mInstallingVaPathSet by lazy { Collections.synchronizedSet(hashSetOf<String>()) }
private val mPackageObserver by lazy {
PackageObserver.PackageChangeListener {
val vaConfig = Config.getVSettingEntity()?.va ?: return@PackageChangeListener
@ -78,6 +80,7 @@ object VHelper {
}
}
@SuppressLint("CheckResult")
@JvmStatic
fun init(context: Context) {
val config = Config.getVSettingEntity()?.va
@ -86,23 +89,8 @@ object VHelper {
) {
connectService()
val installedVersionName =
PackageUtils.getVersionNameByPackageName(config.arch64?.packageName)
val installedVersionCode =
PackageUtils.getVersionCodeByPackageName(config.arch64?.packageName)
RetrofitManager.getInstance()
.vApi
.getPackageUpdate(
installedVersionName,
installedVersionCode,
config.arch64?.packageName
)
.subscribeOn(Schedulers.io())
.subscribe(object : BiResponse<AppEntity>() {
override fun onSuccess(data: AppEntity) {
mUpdateEntity = data
}
})
// 检查畅玩助手组件更新
checkVSpaceUpdate(config.arch64!!)
}
PackageObserver.registerPackageChangeChangeListener(mPackageObserver)
}
@ -335,6 +323,9 @@ object VHelper {
downloadEntity: DownloadEntity,
isManualInstall: Boolean = false
) {
// 正在安装中,忽略重复调用
if (mInstallingVaPathSet.contains(downloadEntity.path)) return
Utils.log(LOG_TAG, "尝试安装新应用 ${downloadEntity.path}")
if (showDialogIfVSpaceIsNeeded(context)) {
@ -357,6 +348,8 @@ object VHelper {
// 安装过程会比较漫长,所以得放在工作线程运行
AppExecutor.ioExecutor.execute {
try {
mInstallingVaPathSet.add(downloadEntity.path)
val result = VirtualAppManager.get().installGame(downloadEntity.path)
if (result.status == 0) {
updateInstalledList()
@ -369,6 +362,8 @@ object VHelper {
)
}
mInstallingVaPathSet.remove(downloadEntity.path)
mInstallationLiveData.postValue(result.packageName)
Utils.log(LOG_TAG, "安装新应用结果 -> " + result.status)
} catch (e: Exception) {
@ -398,25 +393,20 @@ object VHelper {
return
}
checkStoragePermissionBeforeAction(context) {
if (isInstalled(packageName)) {
launch(context, packageName)
} else {
if (isInstalled(packageName)) {
launch(context, packageName)
} else {
checkStoragePermissionBeforeAction(context) {
val downloadEntity = getDownloadEntitySnapshotByPackageName(packageName)
if (downloadEntity != null) {
runOnIoThread {
if (File(downloadEntity.path).exists()) {
install(context, downloadEntity, true)
} else {
// 重新下载
runOnUiThread {
ToastUtils.toast("检测到数据异常,将会为你重新下载安装")
updateOrReDownload(downloadEntity, null)
}
}
if (File(downloadEntity.path).exists()) {
install(context, downloadEntity, true)
} else {
// 重新下载
updateOrReDownload(downloadEntity, null)
}
} else {
ToastUtils.toast("找不到下载文件")
ToastUtils.toast("该游戏已损坏,请重新下载")
}
}
}
@ -566,7 +556,10 @@ object VHelper {
* @param originDownloadEntity 旧的下载实体
* @param updateEntity 更新内容,当 updateEntity 为空时重新下载
*/
fun updateOrReDownload(originDownloadEntity: DownloadEntity, updateEntity: GameUpdateEntity? = null) {
fun updateOrReDownload(
originDownloadEntity: DownloadEntity,
updateEntity: GameUpdateEntity? = null
) {
Utils.log(LOG_TAG, "更新应用${originDownloadEntity.packageName}")
DownloadManager.getInstance().cancel(originDownloadEntity.url)
@ -636,4 +629,26 @@ object VHelper {
}
}
@SuppressLint("CheckResult")
private fun checkVSpaceUpdate(config: VSetting.VaArch) {
val installedVersionName =
PackageUtils.getVersionNameByPackageName(config.packageName)
val installedVersionCode =
PackageUtils.getVersionCodeByPackageName(config.packageName)
RetrofitManager.getInstance()
.vApi
.getPackageUpdate(
installedVersionName,
installedVersionCode,
config.packageName
)
.subscribeOn(Schedulers.io())
.subscribe(object : BiResponse<AppEntity>() {
override fun onSuccess(data: AppEntity) {
mUpdateEntity = data
}
})
}
}