调整部分点击逻辑

This commit is contained in:
juntao
2022-05-17 18:24:27 +08:00
parent 2f35e6ac56
commit 417633b623
32 changed files with 232 additions and 78 deletions

View File

@ -111,6 +111,9 @@ public class DetailDownloadUtils {
downloadText = status + getDownloadSizeText(viewHolder);
} else if (viewHolder.context.getString(R.string.install).equals(status)) {
downloadText = viewHolder.context.getString(R.string.install);
} else if (viewHolder.context.getString(R.string.download).equals(status)
&& VHelper.isVGame(viewHolder.gameEntity)) {
downloadText = viewHolder.context.getString(R.string.smooth_launch);
} else {
downloadText = status + (TextUtils.isEmpty(downloadAddWord) ? "" : downloadAddWord) + getDownloadSizeText(viewHolder);
}
@ -187,8 +190,13 @@ public class DetailDownloadUtils {
viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.INSTALL_NORMAL);
}
} else if (VHelper.isVGame(viewHolder.gameEntity)) {
viewHolder.mDownloadPb.setText(R.string.smooth_launch);
viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.LAUNCH_OR_OPEN);
if (VHelper.isInstalled(downloadEntity.getPackageName())) {
viewHolder.mDownloadPb.setText(R.string.smooth_launch);
viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.LAUNCH_OR_OPEN);
} else {
viewHolder.mDownloadPb.setText(R.string.smooth_launch);
viewHolder.mDownloadPb.setDownloadType(DownloadProgressBar.DownloadType.INSTALL_NORMAL);
}
} else {
if (SPUtils.getBoolean(Constants.SP_USE_BROWSER_TO_INSTALL)) {
viewHolder.mDownloadPb.setText(R.string.browser_install_install);

View File

@ -148,10 +148,6 @@ public class GameUtils {
return context.getString(R.string.launch);
}
if ("smooth".equals(gameEntity.getDownloadStatus())) {
return context.getString(R.string.smooth);
}
if (doneCount != 0) {
return context.getString(R.string.install);
} else if (pluginCount != 0 && !SimulatorGameManager.isSimulatorGame(gameEntity)) {

View File

@ -8,6 +8,8 @@ import com.gh.gamecenter.common.loghub.LoghubUtils
import com.gh.gamecenter.core.runOnIoThread
import com.gh.common.util.*
import com.gh.download.server.BrowserInstallHelper
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.core.runOnIoThread
import com.gh.gamecenter.core.utils.ThirdPartyPackageHelper
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.entity.GameDigestEntity
@ -34,6 +36,16 @@ object PackageObserver {
private val mPackageViewModel: PackageViewModel
by lazy { PackageViewModel(HaloApp.getInstance().application, PackageRepository) }
private val mPackageChangeListenerList: ArrayList<PackageChangeListener> = arrayListOf()
fun registerPackageChangeChangeListener(listener: PackageChangeListener) {
mPackageChangeListenerList.add(listener)
}
fun unregisterPackageChangeChangeListener(listener: PackageChangeListener) {
mPackageChangeListenerList.remove(listener)
}
@JvmStatic
fun onPackageChanged(busFour: EBPackage) {
val application = HaloApp.getInstance().application
@ -121,6 +133,11 @@ object PackageObserver {
// 更新已安装游戏
deleteInstalledPackage(packageName)
}
for (packageChangeListener in mPackageChangeListenerList) {
packageChangeListener.onChanged(busFour)
}
DataCollectionUtils.uploadInorunstall(application, busFour.type, busFour.packageName)
}
@ -181,6 +198,9 @@ object PackageObserver {
e.printStackTrace()
}
LoghubUtils.log(wrapperObject, "halo-api-device-installed", true)
}
fun interface PackageChangeListener {
fun onChanged(data:EBPackage)
}
}

View File

@ -10,7 +10,7 @@ class VDownloadManagerActivity: ToolBarActivity() {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().replace(R.id.placeholder, VDownloadManagerFragment()).commitAllowingStateLoss()
supportFragmentManager.beginTransaction().replace(R.id.placeholder, VDownloadManagerWrapperFragment()).commitAllowingStateLoss()
}
}

View File

@ -1,9 +1,7 @@
package com.gh.vspace
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ethanhua.skeleton.Skeleton
import com.gh.download.DownloadManager
@ -24,8 +22,6 @@ import com.lightgame.download.DownloadEntity
class VDownloadManagerFragment :
ListFragment<GameEntity, VDownloadManagerViewModel>(), IBatchDelete {
private var mManageMenu: MenuItem? = null
private val mAdapter by lazy { VDownloadManagerAdapter(requireContext(), provideListViewModel()) }
private val mViewModel: VDownloadManagerViewModel by lazy { viewModelProvider() }
private val mBinding by lazy { FragmentListBaseSkeletonBinding.inflate(layoutInflater) }
@ -43,25 +39,6 @@ class VDownloadManagerFragment :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initMenu(R.menu.menu_manage)
mManageMenu = getItemMenu(R.id.layout_menu_manage)
mManageMenu?.actionView?.setOnClickListener {
when (mViewModel.currentOptionLiveData.value) {
ManageOption.OPTION_MANAGER -> {
mViewModel.currentOptionLiveData.value = ManageOption.OPTION_CANCEL_SELECT
}
ManageOption.OPTION_CANCEL_SELECT -> {
mViewModel.currentOptionLiveData.value = ManageOption.OPTION_MANAGER
}
}
changeOption(mViewModel.currentOptionLiveData.value ?: ManageOption.OPTION_MANAGER)
}
changeMenuTextByOption()
mViewModel.currentOptionLiveData.observe(this) {
changeMenuTextByOption()
}
}
override fun onResume() {
@ -84,7 +61,6 @@ class VDownloadManagerFragment :
override fun changeOption(option: ManageOption) {
mAdapter.changeOption(option)
changeMenuTextByOption()
}
override fun getItemDecoration(): RecyclerView.ItemDecoration {
@ -101,14 +77,4 @@ class VDownloadManagerFragment :
override fun shouldLoadMore() = false
private fun changeMenuTextByOption() {
(mManageMenu?.actionView as? TextView)?.apply {
text = when (mViewModel.currentOptionLiveData.value) {
ManageOption.OPTION_MANAGER -> "编辑"
ManageOption.OPTION_CANCEL_SELECT -> "取消"
else -> ""
}
}
}
}

View File

@ -1,13 +1,11 @@
package com.gh.vspace
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.gh.download.DownloadManager
import com.gh.gamecenter.baselist.ListViewModel
import com.gh.gamecenter.baselist.LoadType
import com.gh.gamecenter.entity.ApkEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.history.ManageOption
import com.gh.gamecenter.packagehelper.PackageRepository
import com.lightgame.download.DownloadEntity
import io.reactivex.Observable
@ -16,8 +14,6 @@ import io.reactivex.Single
class VDownloadManagerViewModel(application: Application) :
ListViewModel<GameEntity, GameEntity>(application) {
var currentOptionLiveData = MutableLiveData(ManageOption.OPTION_MANAGER)
override fun mergeResultLiveData() {
mResultLiveData.addSource(mListLiveData) {
mResultLiveData.postValue(it)

View File

@ -0,0 +1,82 @@
package com.gh.vspace
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseLazyTabFragment
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.history.IBatchDelete
import com.gh.gamecenter.history.ManageOption
class VDownloadManagerWrapperFragment: BaseLazyTabFragment() {
private var mLastPosition = 0
private var mManageMenu: MenuItem? = null
private val mViewModel: VDownloadManagerWrapperViewModel by lazy { viewModelProvider() }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initMenu(R.menu.menu_manage)
mManageMenu = getItemMenu(R.id.layout_menu_manage)
mManageMenu?.actionView?.setOnClickListener {
when (mViewModel.currentOptionLiveData.value) {
ManageOption.OPTION_MANAGER -> {
mViewModel.currentOptionLiveData.value = ManageOption.OPTION_CANCEL_SELECT
}
ManageOption.OPTION_CANCEL_SELECT -> {
mViewModel.currentOptionLiveData.value = ManageOption.OPTION_MANAGER
}
}
changeOption()
}
changeMenuTextByOption()
}
override fun initFragmentList(fragments: MutableList<Fragment>) {
fragments.add(VDownloadManagerFragment())
fragments.add(VDownloadManagerFragment())
}
override fun initTabTitleList(tabTitleList: MutableList<String>) {
tabTitleList.add("在玩")
tabTitleList.add("下载")
}
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
mViewModel.currentOptionLiveData.value = ManageOption.OPTION_MANAGER
changeOption()
mLastPosition = position
}
private fun changeMenuTextByOption() {
(mManageMenu?.actionView as? TextView)?.apply {
text = when (mViewModel.currentOptionLiveData.value) {
ManageOption.OPTION_MANAGER -> "管理"
ManageOption.OPTION_CANCEL_SELECT -> "取消"
else -> ""
}
}
}
fun changeOption() {
val fragments = childFragmentManager.fragments
mViewModel.currentOptionLiveData.value?.let {
fragments.forEachIndexed { index, fragment ->
if (fragment is IBatchDelete) {
if (index == mViewPager.currentItem) {
fragment.changeOption(it)
} else if (index == mLastPosition) {
fragment.changeOption(ManageOption.OPTION_MANAGER)
}
}
}
}
changeMenuTextByOption()
}
}

View File

@ -0,0 +1,11 @@
package com.gh.vspace
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gh.gamecenter.history.ManageOption
class VDownloadManagerWrapperViewModel : ViewModel() {
var currentOptionLiveData = MutableLiveData(ManageOption.OPTION_MANAGER)
}

View File

@ -22,13 +22,27 @@ object VHelper {
private const val LOG_TAG = "VSPACE"
private var mIsServiceConnected = false
private val mDelegateManager by lazy { VirtualAppManager.get() }
private var mInstalledInfoList: List<AppInstallerInfo> = arrayListOf()
private val mPackageObserver by lazy {
PackageObserver.PackageChangeListener {
val vaConfig = Config.getVSettingEntity()?.va ?: return@PackageChangeListener
val isVSpace = it.packageName == vaConfig.arch32?.packageName || it.packageName == vaConfig.arch64?.packageName
if (!isVSpace) return@PackageChangeListener
if (it.type == "安装") {
connectService()
} else if (it.type == "卸载") {
// TODO 执行卸载逻辑
}
}
}
@JvmStatic
fun init() {
connectService()
PackageObserver.registerPackageChangeChangeListener(mPackageObserver)
}
/**
@ -38,7 +52,6 @@ object VHelper {
mDelegateManager.connectService(object : RemoteConnectListener {
override fun onServiceConnectionSuccessed() {
Utils.log(LOG_TAG, "V 服务连接成功")
mIsServiceConnected = true
mInstalledInfoList = getInstalledList()
PackageRepository.addInstalledGames(getInstalledPackageList())
@ -46,7 +59,6 @@ object VHelper {
}
override fun onServiceConnectionFailed(failCode: Int) {
mIsServiceConnected = false
Utils.log(LOG_TAG, "V 服务连接失败")
// TODO 重试?
}
@ -70,6 +82,14 @@ object VHelper {
return gameEntity?.downloadStatus == "smooth"
}
/**
* 是否在 V 空间里已安装此包名游戏
*/
@JvmStatic
fun isInstalled(packageName: String?): Boolean {
return mInstalledInfoList.any { it.packageName == packageName }
}
/**
* 在执行 callback 前先检查组件是否已安装,是否可下载
*/
@ -91,7 +111,7 @@ object VHelper {
// val isArch64Installed = PackageUtils.isInstalled(context, Config.getVSettingEntity()?.va?.arch64?.packageName)
if (!isArch32Installed) {
VSpaceDialogFragment.showDownloadDialog(context, gameEntity)
VSpaceDialogFragment.showDownloadDialog(context)
return
}
@ -131,10 +151,17 @@ object VHelper {
* 获取已安装列表信息 (每次调用都会生成一份新的副本,放心使用)
*/
fun getInstalledList(): ArrayList<AppInstallerInfo> {
val list = mDelegateManager.installedGamesInfo
Utils.log(LOG_TAG, "已安装应用数量${list.size}")
var list: List<AppInstallerInfo> = arrayListOf()
return ArrayList(mDelegateManager.installedGamesInfo)
try {
list = mDelegateManager.installedGamesInfo
Utils.log(LOG_TAG, "已安装应用数量${list.size}")
} catch (e: Exception) {
Utils.log(LOG_TAG, "获取已安装应用数量异常 ${e.localizedMessage}")
}
return ArrayList(list)
}
/**
@ -158,7 +185,7 @@ object VHelper {
}
} catch (e: RuntimeException) {
e.printStackTrace()
Utils.log(LOG_TAG, "检查存储权限失败")
Utils.log(LOG_TAG, "检查存储权限失败 ${e.localizedMessage}")
}
}
@ -173,20 +200,13 @@ object VHelper {
val result = VirtualAppManager.get().installGame(filePath)
if (result.status == 0) {
updateInstalledList()
PackageObserver.onPackageChanged(
EBPackage(
"安装",
result.packageName,
"unknown"
)
)
}
Utils.log(LOG_TAG, "安装新应用结果 -> " + result.status)
}
}
}
if (mIsServiceConnected) {
if (mDelegateManager.isConnectAidlInterface) {
installClosure.invoke()
} else {
connectService(installClosure)
@ -199,6 +219,15 @@ object VHelper {
@JvmStatic
fun launch(activity: Activity, packageName: String) {
Utils.log(LOG_TAG, "打开应用$packageName")
val vaConfig = Config.getVSettingEntity()?.va ?: return
// TODO 检测 64 位
if (!PackageUtils.isInstalled(activity, vaConfig.arch32?.packageName)) {
VSpaceDialogFragment.showDownloadDialog(activity)
return
}
try {
val intent = mDelegateManager.getStartGameIntent(packageName)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

View File

@ -11,19 +11,22 @@ import androidx.core.text.bold
import androidx.core.text.color
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.gh.common.constant.Config
import com.gh.common.util.PackageInstaller
import com.gh.common.view.DownloadProgressBar
import com.gh.download.DownloadManager
import com.gh.download.PackageObserver
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDraggableDialogFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.DialogVspaceBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.eventbus.EBPackage
import com.lightgame.download.DataWatcher
import com.lightgame.download.DownloadEntity
import com.lightgame.download.DownloadStatus.*
@ -109,6 +112,15 @@ class VSpaceDialogFragment : BaseDraggableDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mViewModel = viewModelProvider<VSpaceDialogViewModel>()
mViewModel.packageLiveData.observe(this) {
if (it.packageName == Config.getVSettingEntity()?.va?.arch32?.packageName
&& it.type == "安装"
) {
dismissAllowingStateLoss()
}
}
val spanBuilder = SpannableStringBuilder()
.append("畅玩是一种更灵活、轻便的游戏方式\n")
.append("下载")
@ -120,6 +132,8 @@ class VSpaceDialogFragment : BaseDraggableDialogFragment() {
mBinding.descTv.text = spanBuilder
mBinding.downloadBtn.setOnClickListener {
val vSpaceInfo = Config.getVSettingEntity()?.va?.arch32!!
val name = "畅玩助手V" + vSpaceInfo.versionName
val downloadId = PackageInstaller.createDownloadId(name)
val downloadEntity = DownloadEntity()
downloadEntity.url = mDownloadUrl
@ -127,30 +141,28 @@ class VSpaceDialogFragment : BaseDraggableDialogFragment() {
downloadEntity.path = FileUtils.getDownloadPath(requireContext(), "畅玩助手V" + vSpaceInfo.versionName + ".apk");
downloadEntity.platform = "官方版"
downloadEntity.gameId = ""
downloadEntity.path = PackageInstaller.getDownloadPathWithId(downloadId, "apk")
downloadEntity.packageName = vSpaceInfo.packageName
DownloadManager.getInstance().cancel(mDownloadUrl, true, true)
DownloadManager.getInstance().pauseAll()
AppExecutor.uiExecutor.executeWithDelay({
DownloadManager.getInstance().add(downloadEntity)
}, 200)
}
}
override fun onResume() {
super.onResume()
override fun onStart() {
super.onStart()
DownloadManager.getInstance().addObserver(mDataWatcher)
}
override fun onPause() {
super.onPause()
override fun onStop() {
super.onStop()
DownloadManager.getInstance().removeObserver(mDataWatcher)
}
companion object {
@JvmStatic
fun showDownloadDialog(context: Context?, gameEntity: GameEntity) {
fun showDownloadDialog(context: Context?) {
val fragmentActivity: FragmentActivity = if (context is FragmentActivity) {
context
} else {
@ -165,12 +177,7 @@ class VSpaceDialogFragment : BaseDraggableDialogFragment() {
// 防止重复弹出
if (hasDialogDisplayedInCurrentActivity(fragmentActivity)) return
val downloadDialog = VSpaceDialogFragment().apply {
val bundle = Bundle()
bundle.putParcelable(GameEntity::class.java.simpleName, gameEntity)
bundle.putParcelable(EntranceConsts.KEY_TRACE_EVENT, gameEntity.exposureEvent)
arguments = bundle
}
val downloadDialog = VSpaceDialogFragment()
downloadDialog.show(
fragmentActivity.supportFragmentManager,
VSpaceDialogFragment::class.java.name
@ -186,4 +193,23 @@ class VSpaceDialogFragment : BaseDraggableDialogFragment() {
}
}
internal class VSpaceDialogViewModel: ViewModel() {
val packageLiveData = MutableLiveData<EBPackage>()
private val mPackageChangedListener by lazy {
PackageObserver.PackageChangeListener {
packageLiveData.postValue(it)
}
}
init {
PackageObserver.registerPackageChangeChangeListener(mPackageChangedListener)
}
override fun onCleared() {
super.onCleared()
PackageObserver.unregisterPackageChangeChangeListener(mPackageChangedListener)
}
}
}

View File

@ -12,5 +12,4 @@ class VSpaceLoadingFragment: BaseFragment<Any>() {
}