feat: 将畅玩存储权限检测移到安装时

This commit is contained in:
juntao
2022-06-17 15:37:24 +08:00
parent f37200f902
commit c4035d319e
11 changed files with 88 additions and 62 deletions

View File

@ -382,17 +382,15 @@ public class BindingAdapters {
return;
}
}
VHelper.checkVSpaceBeforeAction(v.getContext(), gameEntity, () -> {
GamePermissionDialogFragment.show((AppCompatActivity) v.getContext(), gameEntity, gameEntity.getInfo(), () -> {
BrowserInstallHelper.showBrowserInstallHintDialog(v.getContext(), gameEntity.isVGame(), () -> {
PackageCheckDialogFragment.show((AppCompatActivity) v.getContext(), gameEntity, () -> {
DownloadDialogHelper.findAvailableDialogAndShow(v.getContext(), gameEntity, apk, () -> {
CertificationDialog.showCertificationDialog(v.getContext(), gameEntity, () -> {
DialogUtils.showVersionNumberDialog(v.getContext(), gameEntity, () -> {
DialogUtils.showOverseaDownloadDialog(v.getContext(), gameEntity, () -> {
DialogUtils.checkDownload(v.getContext(), apk.getSize(),
isSubscribe -> download(progressBar, gameEntity, traceEvent, isSubscribe, entrance, location));
});
GamePermissionDialogFragment.show((AppCompatActivity) v.getContext(), gameEntity, gameEntity.getInfo(), () -> {
BrowserInstallHelper.showBrowserInstallHintDialog(v.getContext(), gameEntity.isVGame(), () -> {
PackageCheckDialogFragment.show((AppCompatActivity) v.getContext(), gameEntity, () -> {
DownloadDialogHelper.findAvailableDialogAndShow(v.getContext(), gameEntity, apk, () -> {
CertificationDialog.showCertificationDialog(v.getContext(), gameEntity, () -> {
DialogUtils.showVersionNumberDialog(v.getContext(), gameEntity, () -> {
DialogUtils.showOverseaDownloadDialog(v.getContext(), gameEntity, () -> {
DialogUtils.checkDownload(v.getContext(), apk.getSize(),
isSubscribe -> download(progressBar, gameEntity, traceEvent, isSubscribe, entrance, location));
});
});
});
@ -433,7 +431,7 @@ public class BindingAdapters {
}
if (gameEntity.isVGame()) {
VHelper.launch((AppCompatActivity) v.getContext(), gameEntity.getApk().get(0).getPackageName());
VHelper.installOrLaunch((AppCompatActivity) v.getContext(), gameEntity.getApk().get(0).getPackageName());
return;
}

View File

@ -761,7 +761,7 @@ object DownloadItemUtils {
}
if (gameEntity.isVGame()) {
VHelper.launch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
VHelper.installOrLaunch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
return
}

View File

@ -31,7 +31,6 @@ import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.common.util.PackageInstaller;
import com.gh.common.util.PackageUtils;
import com.gh.gamecenter.common.utils.PermissionHelper;
import com.gh.common.util.RealNameHelper;
import com.gh.common.util.ReservationHelper;
import com.gh.gamecenter.core.utils.StringUtils;
import com.gh.common.view.DownloadProgressBar;
@ -189,37 +188,35 @@ public class DetailViewHolder {
case NORMAL:
DataLogUtils.uploadGameLog(mViewHolder.context, mGameEntity.getId(), mGameEntity.getName(), mEntrance);
case PLUGIN:
VHelper.checkVSpaceBeforeAction(mViewHolder.context, mGameEntity, () -> {
GamePermissionDialogFragment.show((AppCompatActivity) mViewHolder.context, mGameEntity, mGameEntity.getInfo(), () -> {
PermissionHelper.checkStoragePermissionBeforeAction(mViewHolder.context, () -> {
if (mGameEntity.getApk().size() == 1) {
ApkEntity apk = mGameEntity.getApk().get(0);
BrowserInstallHelper.showBrowserInstallHintDialog(mViewHolder.context, mGameEntity.isVGame(), () -> {
PackageCheckDialogFragment.show((AppCompatActivity) mViewHolder.context, mGameEntity, () -> {
DownloadDialogHelper.findAvailableDialogAndShow(mViewHolder.context, mGameEntity, apk, () -> {
CertificationDialog.showCertificationDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showVersionNumberDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showOverseaDownloadDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.checkDownload(mViewHolder.context, apk.getSize(), this::download);
});
GamePermissionDialogFragment.show((AppCompatActivity) mViewHolder.context, mGameEntity, mGameEntity.getInfo(), () -> {
PermissionHelper.checkStoragePermissionBeforeAction(mViewHolder.context, () -> {
if (mGameEntity.getApk().size() == 1) {
ApkEntity apk = mGameEntity.getApk().get(0);
BrowserInstallHelper.showBrowserInstallHintDialog(mViewHolder.context, mGameEntity.isVGame(), () -> {
PackageCheckDialogFragment.show((AppCompatActivity) mViewHolder.context, mGameEntity, () -> {
DownloadDialogHelper.findAvailableDialogAndShow(mViewHolder.context, mGameEntity, apk, () -> {
CertificationDialog.showCertificationDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showVersionNumberDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showOverseaDownloadDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.checkDownload(mViewHolder.context, apk.getSize(), this::download);
});
});
});
});
});
} else {
CertificationDialog.showCertificationDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showVersionNumberDialog(mViewHolder.context, mGameEntity, () -> {
DownloadDialog.showDownloadDialog(
mViewHolder.context,
mGameEntity,
mTraceEvent,
StringUtils.buildString(mEntrance, "+(", mName, "[", mTitle, "])"),
mName + ":" + mTitle);
});
});
} else {
CertificationDialog.showCertificationDialog(mViewHolder.context, mGameEntity, () -> {
DialogUtils.showVersionNumberDialog(mViewHolder.context, mGameEntity, () -> {
DownloadDialog.showDownloadDialog(
mViewHolder.context,
mGameEntity,
mTraceEvent,
StringUtils.buildString(mEntrance, "+(", mName, "[", mTitle, "])"),
mName + ":" + mTitle);
});
}
});
});
}
});
});
break;
@ -240,7 +237,7 @@ public class DetailViewHolder {
}
if (mGameEntity.isVGame()) {
VHelper.launch((AppCompatActivity) mViewHolder.context, mGameEntity.getApk().get(0).getPackageName());
VHelper.installOrLaunch((AppCompatActivity) mViewHolder.context, mGameEntity.getApk().get(0).getPackageName());
return;
}

View File

@ -268,6 +268,10 @@ data class GameEntity(
// 本地字段,使用镜像信息
var useMirrorInfo: Boolean = false,
// 本地字段,最后打开时间
var lastPlayedTime: Long = 0,
// 本地字段,曝光用
var displayContent: String = "",
var isPlatformRecommend: Boolean = false,

View File

@ -123,7 +123,7 @@ class HomeRecentVGameAdapter(context: Context) : DiffUtilAdapter<GameEntity>(con
when (downloadEntity.status) {
DownloadStatus.done -> {
binding.root.setOnClickListener {
VHelper.launch(binding.root.context, downloadEntity.packageName)
VHelper.installOrLaunch(binding.root.context, downloadEntity.packageName)
}
}
DownloadStatus.pause,

View File

@ -22,6 +22,7 @@ import com.gh.gamecenter.common.constant.ItemViewType
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.core.utils.NumberUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.ToastUtils
import com.gh.gamecenter.databinding.ItemVgameDownloadManagerBinding
@ -97,10 +98,13 @@ class VDownloadManagerAdapter(context: Context, private val mViewModel: VDownloa
when (holder) {
is VGameItemViewHolder -> {
val gameEntity = mEntityList[position]
val totalPlayedTimeString = NumberUtils.transSimpleUsageTime(gameEntity.playedTime)
BindingAdapters.setGameName(holder.binding.nameTv, gameEntity, false, true)
holder.binding.iconIv.displayGameIcon(gameEntity)
holder.binding.descTv.text = "已畅玩999分钟"
holder.binding.descTv.goneIf(mViewModel.isTypeDownloaded())
holder.binding.descTv.text = "已畅玩$totalPlayedTimeString"
(holder.binding.selectIv.layoutParams as ConstraintLayout.LayoutParams).apply {
width = 20F.dip2px()
@ -279,7 +283,7 @@ class VDownloadManagerAdapter(context: Context, private val mViewModel: VDownloa
btnText = context.getString(R.string.launch)
setOnClickListener {
CurrentActivityHolder.getCurrentActivity()?.let {
VHelper.launch(it, downloadEntity.packageName)
VHelper.installOrLaunch(it, downloadEntity.packageName)
}
}
}

View File

@ -72,6 +72,10 @@ class VDownloadManagerViewModel(application: Application) :
}
}
fun isTypeDownloaded() : Boolean {
return type == TYPE_DOWNLOADED
}
companion object {
const val TYPE = "type"
const val TYPE_DOWNLOADED = "type_downloaded"

View File

@ -1,7 +1,6 @@
package com.gh.vspace
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
@ -201,7 +200,7 @@ object VHelper {
return
}
checkStoragePermissionBeforeAction(context, callback)
callback.invoke()
} else {
callback.invoke()
}
@ -271,15 +270,15 @@ object VHelper {
/**
* 安装新应用
*/
fun install(activity: Activity, filePath: String) {
Utils.log(LOG_TAG, "尝试安装新应用")
fun install(context: Context, filePath: String) {
Utils.log(LOG_TAG, "尝试安装新应用 $filePath")
if (showDialogIfVSpaceIsNeeded(activity)) {
if (showDialogIfVSpaceIsNeeded(context)) {
return
}
val installClosure: () -> Unit = {
checkStoragePermissionBeforeAction(activity) {
checkStoragePermissionBeforeAction(context) {
// 安装过程会比较漫长,所以得放在工作线程运行
AppExecutor.ioExecutor.execute {
try {
@ -315,16 +314,29 @@ object VHelper {
}
/**
* 启动应用
* 安装或启动应用
*/
@JvmStatic
fun launch(context: Context, packageName: String) {
Utils.log(LOG_TAG, "打开应用$packageName")
fun installOrLaunch(context: Context, packageName: String) {
Utils.log(LOG_TAG, "检测是需要安装还是启动 $packageName")
DownloadManager.getInstance().getDownloadEntitySnapshotByPackageName(packageName)?.let {
mLastSuccessfullyLaunchedGame = Pair(System.currentTimeMillis(), it.gameId)
if (showDialogIfVSpaceIsNeeded(context)) {
return
}
if (isInstalled(packageName)) {
launch(context, packageName)
} else {
install(context, getDownloadEntitySnapshotByPackageName(packageName)?.path ?: "")
}
}
/**
* 启动应用
*/
fun launch(context: Context, packageName: String) {
Utils.log(LOG_TAG, "打开应用 $packageName")
if (showDialogIfVSpaceIsNeeded(context)) {
return
}
@ -335,6 +347,10 @@ object VHelper {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
DownloadManager.getInstance().getDownloadEntitySnapshotByPackageName(packageName)?.let {
mLastSuccessfullyLaunchedGame = Pair(System.currentTimeMillis(), it.gameId)
}
updateLastPlayedTime(packageName)
} catch (e: Exception) {
ToastUtils.toast(e.localizedMessage ?: "")
@ -342,9 +358,11 @@ object VHelper {
}
if (mDelegateManager.isConnectAidlInterface) {
launchClosure.invoke()
checkStoragePermissionBeforeAction(context, launchClosure)
} else {
connectService(launchClosure)
checkStoragePermissionBeforeAction(context) {
connectService(launchClosure)
}
}
}
@ -360,7 +378,7 @@ object VHelper {
PackageObserver.onPackageChanged(EBPackage("卸载", packageName, "unknown"))
}
Utils.log(LOG_TAG, "安装新应用结果 -> $result")
Utils.log(LOG_TAG, "卸载应用结果 -> $result")
} catch (e: Exception) {
ToastUtils.toast(e.localizedMessage ?: "")
}
@ -387,7 +405,7 @@ object VHelper {
}
/**
* 根据包名获取 `下载实例`
* 根据包名获取下载快照
*/
fun getDownloadEntitySnapshotByPackageName(packageName: String): DownloadEntity? {
return DownloadManager.getInstance().getDownloadEntitySnapshotByPackageName(packageName)
@ -500,7 +518,8 @@ object VHelper {
)
)
icon = downloadEntity.icon
playedTime = if (lastPlayedTimeString == "") 0L else lastPlayedTimeString.toLong()
lastPlayedTime = if (lastPlayedTimeString == "") 0L else lastPlayedTimeString.toLong()
playedTime = mInstalledInfoList.firstOrNull { it.packageName == getUniquePackageName() }?.appTotalPlayTime ?: 0
downloadStatus = "smooth"
setEntryMap(DownloadManager.getInstance().getEntryMap(name))
}

View File

@ -144,7 +144,7 @@ object VLoadCompleteWindowHelper {
}
}
mBinding.launchTv.setOnClickListener {
VHelper.launch(activity, mAdapter.gameEntityList[mBinding.viewPager.currentItem].getApk()[0].packageName)
VHelper.installOrLaunch(activity, mAdapter.gameEntityList[mBinding.viewPager.currentItem].getApk()[0].packageName)
}
}
}

View File

@ -78,7 +78,7 @@ class VSpaceLoadingFragment : BaseFragment<Any>() {
VHelper.getInstallationLiveData().observe(viewLifecycleOwner) {
val currentPackageName = mGame?.getApk()?.firstOrNull()?.packageName
if (it == currentPackageName) {
VHelper.launch(requireActivity(), currentPackageName!!)
VHelper.installOrLaunch(requireActivity(), currentPackageName!!)
requireActivity().finish()
}
}