918 lines
44 KiB
Kotlin
918 lines
44 KiB
Kotlin
package com.gh.common.util
|
||
|
||
import android.content.Context
|
||
import android.graphics.Color
|
||
import android.os.Message
|
||
import android.text.TextUtils
|
||
import android.view.View
|
||
import android.widget.TextView
|
||
import androidx.appcompat.app.AppCompatActivity
|
||
import androidx.collection.ArrayMap
|
||
import androidx.core.content.ContextCompat
|
||
import androidx.recyclerview.widget.RecyclerView
|
||
import com.gh.common.constant.Config
|
||
import com.gh.common.dialog.CertificationDialog
|
||
import com.gh.common.dialog.DeviceRemindDialog
|
||
import com.gh.common.dialog.PackageCheckDialogFragment
|
||
import com.gh.common.exposure.ExposureEvent
|
||
import com.gh.common.filter.RegionSettingHelper
|
||
import com.gh.common.history.HistoryHelper
|
||
import com.gh.common.repository.ReservationRepository
|
||
import com.gh.common.simulator.SimulatorDownloadManager
|
||
import com.gh.common.simulator.SimulatorGameManager
|
||
import com.gh.common.xapk.XapkInstaller
|
||
import com.gh.common.xapk.XapkUnzipStatus
|
||
import com.gh.download.DownloadManager
|
||
import com.gh.download.dialog.DownloadDialog
|
||
import com.gh.download.server.BrowserInstallHelper
|
||
import com.gh.gamecenter.DownloadManagerActivity
|
||
import com.gh.gamecenter.R
|
||
import com.gh.gamecenter.WebActivity
|
||
import com.gh.gamecenter.adapter.viewholder.GameViewHolder
|
||
import com.gh.gamecenter.common.constant.Constants
|
||
import com.gh.gamecenter.common.constant.EntranceConsts
|
||
import com.gh.gamecenter.common.utils.*
|
||
import com.gh.gamecenter.core.utils.*
|
||
import com.gh.gamecenter.entity.GameEntity
|
||
import com.gh.gamecenter.entity.LinkEntity
|
||
import com.gh.gamecenter.entity.PluginLocation
|
||
import com.gh.gamecenter.gamedetail.dialog.GamePermissionDialogFragment
|
||
import com.gh.gamecenter.manager.PackagesManager
|
||
import com.gh.gamecenter.teenagermode.TeenagerModeActivity
|
||
import com.gh.vspace.VDownloadManagerActivity
|
||
import com.gh.vspace.VHelper
|
||
import com.lightgame.download.DownloadConfig
|
||
import com.lightgame.download.DownloadEntity
|
||
import com.lightgame.download.DownloadStatus
|
||
import com.lightgame.download.FileUtils
|
||
import com.lightgame.utils.Utils
|
||
import java.io.File
|
||
import java.util.concurrent.LinkedBlockingQueue
|
||
|
||
/**
|
||
* todo 下载判断不能以按钮文案为判断条件,否则按钮文案修改时又要修改判断逻辑
|
||
*/
|
||
object DownloadItemUtils {
|
||
// 更新下载进度条
|
||
fun processDate(
|
||
gameEntity: GameEntity,
|
||
downloadEntity: DownloadEntity,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
index: Int
|
||
) {
|
||
if (gameEntity.id != downloadEntity.gameId) {
|
||
adapter?.notifyItemChanged(index)
|
||
return
|
||
}
|
||
var queue = DownloadManager.getInstance().getQueue(downloadEntity.name)
|
||
if (queue == null) {
|
||
queue = LinkedBlockingQueue()
|
||
DownloadManager.getInstance().putQueue(downloadEntity.name, queue)
|
||
}
|
||
val platform = downloadEntity.platform
|
||
val entryMap: ArrayMap<String, DownloadEntity>
|
||
val status = downloadEntity.status
|
||
if (status == DownloadStatus.pause || status == DownloadStatus.cancel || status == DownloadStatus.done) {
|
||
queue.remove(platform)
|
||
entryMap = ArrayMap()
|
||
gameEntity.setEntryMap(entryMap)
|
||
entryMap[platform] = downloadEntity
|
||
adapter?.notifyItemChanged(index)
|
||
} else {
|
||
if (!queue.contains(platform) && !TextUtils.isEmpty(platform)) {
|
||
queue.offer(platform)
|
||
if (AppDebugConfig.IS_DEBUG) {
|
||
AppDebugConfig.logMethodWithParams(DownloadItemUtils::class.java, queue.size, gameEntity.brief, downloadEntity.platform, index)
|
||
}
|
||
// 有两个平台同时下载的时候启用
|
||
if (queue.size == 2) {
|
||
//TODO fuck this
|
||
val msg = Message.obtain()
|
||
msg.obj = downloadEntity.name
|
||
msg.what = DownloadConfig.DOWNLOAD_ROLL
|
||
DownloadManager.getInstance().sendMessageDelayed(msg, 3000)
|
||
}
|
||
}
|
||
if (platform != null && platform == queue.peek()) {
|
||
entryMap = ArrayMap()
|
||
gameEntity.setEntryMap(entryMap)
|
||
entryMap[platform] = downloadEntity
|
||
if (DownloadStatus.pause != DownloadManager.getInstance().getStatus(downloadEntity.url)) {
|
||
adapter?.notifyItemChanged(index)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 下载按钮显示为查看,并且不提供下载功能
|
||
fun updateItemWithViewOnlyStyle(holder: GameViewHolder) {
|
||
holder.gameDownloadBtn.visibility = View.VISIBLE
|
||
holder.gameDownloadBtn.text = "查看"
|
||
holder.gameDownloadBtn.isClickable = false
|
||
}
|
||
|
||
// 根据预约状态更新下载按钮
|
||
fun updateItemWithReserveStatus(holder: GameViewHolder, gameEntity: GameEntity) {
|
||
if ("download" == gameEntity.reserveStatus) {
|
||
// 已上线
|
||
updateItem(holder.gameDes.context, gameEntity, holder, false)
|
||
} else if ("appointment" == gameEntity.reserveStatus) {
|
||
// 已预约
|
||
holder.gameDownloadBtn.visibility = View.VISIBLE
|
||
holder.gameDownloadBtn.text = "已预约"
|
||
holder.gameDownloadBtn.setTextColor(R.color.text_subtitleDesc.toColor(holder.gameDownloadBtn.context))
|
||
holder.gameDownloadBtn.background = R.drawable.button_round_f5f5f5.toDrawable(holder.gameDownloadBtn.context)
|
||
updateItemViewStatus(holder, false, null, null)
|
||
}
|
||
}
|
||
|
||
fun updateItem(
|
||
context: Context, gameEntity: GameEntity, holder: GameViewHolder,
|
||
isShowPlatform: Boolean, hideDownloadBtnIfNoAvailableContent: Boolean
|
||
) {
|
||
updateItem(context, gameEntity, holder, isShowPlatform, PluginLocation.only_game, hideDownloadBtnIfNoAvailableContent, null)
|
||
}
|
||
|
||
fun updateItem(
|
||
context: Context, gameEntity: GameEntity, holder: GameViewHolder,
|
||
isShowPlatform: Boolean, briefStyle: String?
|
||
) {
|
||
updateItem(context, gameEntity, holder, isShowPlatform, PluginLocation.only_game, false, briefStyle)
|
||
}
|
||
|
||
@JvmStatic
|
||
@JvmOverloads
|
||
fun updateItem(
|
||
context: Context,
|
||
gameEntity: GameEntity,
|
||
holder: GameViewHolder,
|
||
isShowPlatform: Boolean,
|
||
pluginLocation: PluginLocation? = PluginLocation.only_game,
|
||
hideDownloadBtnIfNoAvailableContent: Boolean = false,
|
||
briefStyle: String? = null,
|
||
isShowRecommendStar: Boolean = false
|
||
) {
|
||
// 显示预约
|
||
if (gameEntity.isReservable) {
|
||
updateItemViewStatus(holder, false, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
|
||
updateDownloadButton(context, holder.gameDownloadBtn, gameEntity, hideDownloadBtnIfNoAvailableContent, pluginLocation)
|
||
return
|
||
}
|
||
if (gameEntity.getApk().isEmpty() || gameEntity.downloadOffStatus != null) {
|
||
updateItemViewStatus(holder, false, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
|
||
} else if (gameEntity.getApk().size == 1) {
|
||
updateNormalItem(context, holder, gameEntity, isShowPlatform, briefStyle, isShowRecommendStar)
|
||
} else {
|
||
updatePluginItem(context, holder, gameEntity, isShowPlatform, briefStyle, isShowRecommendStar)
|
||
}
|
||
updateDownloadButton(context, holder.gameDownloadBtn, gameEntity, hideDownloadBtnIfNoAvailableContent, pluginLocation)
|
||
}
|
||
|
||
fun updateDownloadButton(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
hideDownloadBtnIfNoAvailableContent: Boolean = false,
|
||
pluginLocation: PluginLocation? = PluginLocation.only_game
|
||
) {
|
||
downloadBtn.background = R.drawable.download_button_normal_style.toDrawable(context)
|
||
// 控制是否显示下载按钮
|
||
downloadBtn.goneIf(!Config.isShowDownload(gameEntity.id) || context.getString(R.string.app_name) == gameEntity.name)
|
||
// 青少年模式或者需要特殊处理显示查看
|
||
if (SPUtils.getBoolean(Constants.SP_TEENAGER_MODE) || gameEntity.isSpecialDownload()) {
|
||
downloadBtn.text = "查看"
|
||
return
|
||
}
|
||
if (gameEntity.isReservable) {
|
||
downloadBtn.apply {
|
||
if (!ReservationRepository.thisGameHasBeenReserved(gameEntity.id)) {
|
||
text = "预约"
|
||
setTextColor(Color.WHITE)
|
||
setBackgroundResource(R.drawable.button_reserve)
|
||
} else {
|
||
text = "已预约"
|
||
setTextColor(R.color.text_subtitleDesc.toColor())
|
||
setBackgroundResource(R.drawable.bg_shape_f5_radius_999)
|
||
}
|
||
}
|
||
return
|
||
}
|
||
if (gameEntity.getApk().isEmpty() || gameEntity.downloadOffStatus != null) {
|
||
val h5LinkEntity = gameEntity.h5Link
|
||
val offStatus = gameEntity.downloadOffStatus
|
||
downloadBtn.apply {
|
||
if (h5LinkEntity != null) {
|
||
text = if ("play" == h5LinkEntity.type) {
|
||
context.getString(R.string.start_playing)
|
||
} else {
|
||
context.getString(R.string.check)
|
||
}
|
||
isClickable = true
|
||
} else {
|
||
if ("dialog" == offStatus) {
|
||
text = context.getString(R.string.check)
|
||
} else if ("updating" == offStatus) {
|
||
text = context.getString(R.string.updating)
|
||
setTextColor(R.color.white.toColor())
|
||
setBackgroundResource(R.drawable.download_button_updating_style)
|
||
} else {
|
||
text = context.getString(R.string.none)
|
||
setTextColor(R.color.text_subtitleDesc.toColor())
|
||
setBackgroundResource(R.drawable.bg_shape_f5_radius_999)
|
||
if (hideDownloadBtnIfNoAvailableContent) {
|
||
visibility = View.GONE
|
||
}
|
||
}
|
||
downloadBtn.isClickable = false
|
||
}
|
||
}
|
||
} else if (gameEntity.getApk().size == 1) {
|
||
var downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot(gameEntity)
|
||
|
||
if (downloadEntity == null && gameEntity.isVGame()) {
|
||
downloadEntity = VHelper.getDownloadEntitySnapshot(gameEntity.id, gameEntity.getUniquePackageName())
|
||
}
|
||
|
||
if (downloadEntity != null) {
|
||
downloadBtn.apply {
|
||
val status = downloadEntity.status
|
||
if (status == DownloadStatus.downloading) {
|
||
setText(R.string.downloading)
|
||
setBackgroundResource(R.drawable.game_item_btn_downloading_style)
|
||
setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style))
|
||
} else if (status == DownloadStatus.waiting) {
|
||
setText(R.string.waiting)
|
||
setBackgroundResource(R.drawable.button_round_border_eeeeee)
|
||
setTextColor(ContextCompat.getColorStateList(context, R.color.text_subtitleDesc))
|
||
} else if (status == DownloadStatus.pause || status == DownloadStatus.timeout || status == DownloadStatus.neterror || status == DownloadStatus.subscribe || status == DownloadStatus.overflow) {
|
||
setText(R.string.downloading)
|
||
setBackgroundResource(R.drawable.game_item_btn_downloading_style)
|
||
setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style))
|
||
} else if (status == DownloadStatus.done) {
|
||
val xapkStatus = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_STATUS]
|
||
if (XapkUnzipStatus.UNZIPPING.name == xapkStatus) {
|
||
setText(R.string.unzipping)
|
||
return
|
||
}
|
||
if (downloadEntity.isSimulatorGame() && gameEntity.simulator != null) {
|
||
GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
|
||
} else if (downloadEntity.isVGame()) {
|
||
if (PackagesManager.isCanUpdate(downloadEntity.gameId, downloadEntity.packageName)) {
|
||
setText(R.string.update)
|
||
} else {
|
||
setText(R.string.launch)
|
||
}
|
||
} else {
|
||
setText(R.string.install)
|
||
}
|
||
setTextColor(Color.WHITE)
|
||
if (downloadEntity.isPluggable && PackagesManager.isInstalled(downloadEntity.packageName)) {
|
||
setBackgroundResource(R.drawable.download_button_pluggable_style)
|
||
} else {
|
||
setBackgroundResource(R.drawable.download_button_normal_style)
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
|
||
}
|
||
} else {
|
||
GameUtils.setDownloadBtnStatus(context, gameEntity, downloadBtn, pluginLocation)
|
||
}
|
||
}
|
||
|
||
// 更新正常的条目,只有一个apk包
|
||
private fun updateNormalItem(
|
||
context: Context, holder: GameViewHolder, gameEntity: GameEntity,
|
||
isShowPlatform: Boolean, briefStyle: String?,
|
||
isShowRecommendStar: Boolean = false
|
||
) {
|
||
val entryMap: ArrayMap<String, DownloadEntity> = gameEntity.getEntryMap()
|
||
val apkEntity = gameEntity.getApk()[0]
|
||
if (entryMap.isNotEmpty()) {
|
||
val downloadEntity = entryMap[apkEntity.getPlatform()]
|
||
if (downloadEntity != null) {
|
||
if (downloadEntity.isSimulatorGame()) {
|
||
if (downloadEntity.status != DownloadStatus.done) {
|
||
// 更改进度条和提示文本的状态
|
||
changeStatus(context, holder, downloadEntity, isShowPlatform, true)
|
||
return
|
||
}
|
||
} else {
|
||
// 更改进度条和提示文本的状态
|
||
changeStatus(context, holder, downloadEntity, isShowPlatform, true, isShowRecommendStar)
|
||
return
|
||
}
|
||
}
|
||
}
|
||
updateItemViewStatus(holder, false, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
|
||
}
|
||
|
||
// 更新插件的条目,有多个apk包
|
||
private fun updatePluginItem(
|
||
context: Context, holder: GameViewHolder, gameEntity: GameEntity,
|
||
isShowPlatform: Boolean, briefStyle: String?,
|
||
isShowRecommendStar: Boolean = false
|
||
) {
|
||
val entryMap = gameEntity.getEntryMap()
|
||
if (entryMap.isNotEmpty()) {
|
||
val queue = DownloadManager.getInstance().getQueue(gameEntity.name)
|
||
val downloadEntity = if (queue != null && !queue.isEmpty()) {
|
||
entryMap[queue.peek()]
|
||
} else {
|
||
entryMap[entryMap.keyAt(0)]
|
||
}
|
||
if (downloadEntity != null) {
|
||
// 更改进度条和提示文本的状态
|
||
changeStatus(context, holder, downloadEntity, isShowPlatform, false, isShowRecommendStar)
|
||
return
|
||
}
|
||
}
|
||
updateItemViewStatus(holder, false, briefStyle, gameEntity.columnRecommend, isShowRecommendStar)
|
||
}
|
||
|
||
// 更改进度条和提示文本的状态
|
||
fun changeStatus(
|
||
context: Context, holder: GameViewHolder, downloadEntity: DownloadEntity,
|
||
isShowPlatform: Boolean, isNormal: Boolean, isShowRecommendStar: Boolean = false
|
||
) {
|
||
val status = downloadEntity.status
|
||
// 畅玩游戏下载完成时不再需要显示进度条
|
||
val shouldShowDownload = !(downloadEntity.isVGame() && status == DownloadStatus.done)
|
||
val platform = PlatformUtils.getInstance(context).getPlatformName(downloadEntity.platform)
|
||
|
||
updateItemViewStatus(holder, shouldShowDownload, null, null, isShowRecommendStar)
|
||
holder.gameProgressbar.progressDrawable = R.drawable.progressbar_bg_style.toDrawable()
|
||
|
||
if (status == DownloadStatus.downloading) {
|
||
if (DownloadStatus.pause != DownloadManager.getInstance().getStatus(downloadEntity.url)) {
|
||
holder.gameProgressbar.progress = (downloadEntity.percent * 10).toInt()
|
||
if (isShowPlatform && platform != null) {
|
||
holder.gameDownloadSpeed.text = String.format(
|
||
"%s - %s(剩%s)", platform,
|
||
SpeedUtils.getSpeed(downloadEntity.speed),
|
||
SpeedUtils.getRemainTime(downloadEntity.size, downloadEntity.progress, downloadEntity.speed * 1024)
|
||
)
|
||
} else {
|
||
holder.gameDownloadSpeed.text = String.format(
|
||
"%s(剩%s)", SpeedUtils.getSpeed(downloadEntity.speed),
|
||
SpeedUtils.getRemainTime(downloadEntity.size, downloadEntity.progress, downloadEntity.speed * 1024)
|
||
)
|
||
}
|
||
holder.gameDownloadPercentage.text = downloadEntity.percent.toString() + "%"
|
||
}
|
||
} else if (status == DownloadStatus.waiting) {
|
||
holder.gameProgressbar.progress = (downloadEntity.percent * 10).toInt()
|
||
if (isShowPlatform && platform != null) {
|
||
holder.gameDownloadSpeed.text = String.format("%s - 等待", platform)
|
||
} else {
|
||
holder.gameDownloadSpeed.text = "等待"
|
||
}
|
||
holder.gameDownloadPercentage.text = downloadEntity.percent.toString() + "%"
|
||
} else if (status == DownloadStatus.pause || status == DownloadStatus.timeout || status == DownloadStatus.neterror || status == DownloadStatus.subscribe || status == DownloadStatus.overflow) {
|
||
holder.gameProgressbar.progress = (downloadEntity.percent * 10).toInt()
|
||
if (isShowPlatform && platform != null) {
|
||
holder.gameDownloadSpeed.text = String.format("%s - 暂停", platform)
|
||
} else {
|
||
holder.gameDownloadSpeed.text = "暂停"
|
||
}
|
||
holder.gameDownloadPercentage.text = downloadEntity.percent.toString() + "%"
|
||
} else if (status == DownloadStatus.done) {
|
||
val xapkStatus = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_STATUS]
|
||
if (XapkUnzipStatus.UNZIPPING.name == xapkStatus) {
|
||
val percent = downloadEntity.meta[XapkInstaller.XAPK_UNZIP_PERCENT]
|
||
holder.gameProgressbar.progressDrawable = context.resources.getDrawable(R.drawable.progressbar_xapk_style)
|
||
holder.gameDownloadSpeed.setText(R.string.unzipping)
|
||
holder.gameProgressbar.progress = (java.lang.Float.valueOf(percent) * 10).toInt()
|
||
holder.gameDownloadPercentage.text = "$percent%"
|
||
return
|
||
}
|
||
holder.gameProgressbar.progress = 1000
|
||
if (isShowPlatform && platform != null) {
|
||
holder.gameDownloadSpeed.text = String.format("%s - 下载完成", platform)
|
||
} else {
|
||
holder.gameDownloadSpeed.text = "下载完成"
|
||
}
|
||
holder.gameDownloadPercentage.setText(R.string.hundred_percent)
|
||
}
|
||
}
|
||
|
||
private fun updateItemViewStatus(
|
||
holder: GameViewHolder,
|
||
showDownload: Boolean,
|
||
briefStyle: String?,
|
||
recommendStyle: LinkEntity?,
|
||
isShowRecommendStar: Boolean = false
|
||
) {
|
||
if (showDownload) {
|
||
if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
|
||
holder.gameDes.visibility = View.GONE
|
||
holder.gameProgressbar.visibility = View.VISIBLE
|
||
holder.gameInfo.visibility = View.VISIBLE
|
||
if (holder.recommendContainer != null) {
|
||
holder.recommendContainer!!.visibility = View.GONE
|
||
}
|
||
if (holder.recommendStarInfo != null) {
|
||
holder.recommendStarInfo!!.visibility = View.GONE
|
||
}
|
||
} else {
|
||
holder.gameProgressbar.visibility = View.GONE
|
||
holder.gameInfo.visibility = View.GONE
|
||
|
||
// 推荐指数优先,现暂时为游戏单详情列表游戏使用
|
||
if (isShowRecommendStar && holder.recommendStarInfo != null) {
|
||
holder.recommendStarInfo!!.visibility = View.VISIBLE
|
||
if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
|
||
holder.gameDes.visibility = View.GONE
|
||
return
|
||
}
|
||
|
||
// 推荐优先,有推荐内容时不执行下面的 star 和 brief 代码块
|
||
if (briefStyle != null && recommendStyle != null && briefStyle.contains("recommend")) {
|
||
if (holder.recommendContainer != null) {
|
||
holder.recommendContainer!!.visibility = View.VISIBLE
|
||
}
|
||
if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
|
||
holder.gameDes.visibility = View.GONE
|
||
holder.recommendTv.text = recommendStyle.text
|
||
if (TextUtils.isEmpty(recommendStyle.icon)) {
|
||
holder.recommendIv.visibility = View.GONE
|
||
holder.recommendContainer?.setPadding(6F.dip2px(), 0, 8F.dip2px(), 0)
|
||
} else {
|
||
holder.recommendIv.visibility = View.VISIBLE
|
||
ImageUtils.display(holder.recommendIv, recommendStyle.icon)
|
||
}
|
||
return
|
||
} else {
|
||
if (holder.recommendContainer != null) {
|
||
holder.recommendContainer!!.visibility = View.GONE
|
||
}
|
||
}
|
||
if (briefStyle != null && briefStyle.contains("star")) {
|
||
if (holder.gameRating != null) holder.gameRating!!.visibility = View.VISIBLE
|
||
} else {
|
||
if (holder.gameRating != null) holder.gameRating!!.visibility = View.GONE
|
||
}
|
||
|
||
// 缺省情况下回落到游戏简介
|
||
if (TextUtils.isEmpty(briefStyle) || briefStyle!!.contains("brief") || briefStyle.contains("recommend")) {
|
||
holder.gameDes.visibility = View.VISIBLE
|
||
} else {
|
||
holder.gameDes.visibility = View.GONE
|
||
}
|
||
}
|
||
}
|
||
|
||
@JvmStatic
|
||
fun setOnClickListener(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
entrance: String,
|
||
location: String
|
||
) {
|
||
setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, null)
|
||
}
|
||
|
||
@JvmStatic
|
||
fun setOnClickListener(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
entrance: String,
|
||
location: String,
|
||
traceEvent: ExposureEvent?
|
||
) {
|
||
setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, traceEvent, null)
|
||
}
|
||
|
||
/**
|
||
* @param clickCallback 供那些需要知道点击回调的地方使用,触发具体响应才会回调
|
||
*/
|
||
@JvmStatic
|
||
fun setOnClickListener(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
entrance: String,
|
||
location: String,
|
||
traceEvent: ExposureEvent?,
|
||
clickCallback: EmptyCallback?
|
||
) {
|
||
setOnClickListener(context, downloadBtn, gameEntity, position, adapter, entrance, location, traceEvent, clickCallback, null, null)
|
||
}
|
||
|
||
/**
|
||
* @param allStateClickCallback 供那些需要知道点击回调的地方使用,只要触发点击动作就会回调
|
||
*/
|
||
@JvmStatic
|
||
fun setOnClickListener(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
entrance: String,
|
||
location: String,
|
||
traceEvent: ExposureEvent?,
|
||
clickCallback: EmptyCallback?,
|
||
refreshCallback: EmptyCallback?,
|
||
allStateClickCallback: EmptyCallback?
|
||
) {
|
||
val gamePermissionDialogFragment = (context as AppCompatActivity).supportFragmentManager.findFragmentByTag(
|
||
GamePermissionDialogFragment::class.java.name
|
||
) as GamePermissionDialogFragment?
|
||
gamePermissionDialogFragment?.dismissAllowingStateLoss()
|
||
if (SPUtils.getBoolean(Constants.SP_TEENAGER_MODE)) {
|
||
downloadBtn.setOnClickListener {
|
||
DialogHelper.showDialog(
|
||
context,
|
||
"提示",
|
||
"当前处于儿童/青少年模式, \n暂不提供游戏下载",
|
||
"退出青少年模式",
|
||
"关闭",
|
||
{ context.startActivity(TeenagerModeActivity.getIntent(context)) },
|
||
{},
|
||
DialogHelper.Config(
|
||
centerTitle = true,
|
||
centerContent = true
|
||
)
|
||
)
|
||
}
|
||
return
|
||
}
|
||
if (gameEntity.isSpecialDownload()) {
|
||
val info = RegionSettingHelper.getGameSpecialDownloadInfo(gameEntity.id) ?: return
|
||
downloadBtn.setOnClickListener {
|
||
DialogHelper.showDialog(
|
||
context,
|
||
"提示",
|
||
info.hintText,
|
||
"前往论坛",
|
||
"",
|
||
{
|
||
if (info.bbsId.isNotBlank()) {
|
||
if (info.topId.isNotBlank()) {
|
||
val data = hashMapOf(EntranceConsts.KEY_TOP_ID to info.topId)
|
||
PageSwitchDataHelper.pushCurrentPageData(data)
|
||
}
|
||
DirectUtils.directForumDetail(context, info.bbsId, entrance)
|
||
}
|
||
},
|
||
{},
|
||
DialogHelper.Config(
|
||
centerTitle = true,
|
||
centerContent = true
|
||
)
|
||
)
|
||
}
|
||
return
|
||
}
|
||
if (gameEntity.isReservable) {
|
||
if (!ReservationRepository.thisGameHasBeenReserved(gameEntity.id)) {
|
||
downloadBtn.setOnClickListener {
|
||
allStateClickCallback?.onCallback()
|
||
CheckLoginUtils.checkLogin(context, entrance) {
|
||
clickCallback?.onCallback()
|
||
ReservationHelper.reserve(context, gameEntity.id, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
LogUtils.logReservation(gameEntity, traceEvent)
|
||
adapter?.notifyItemChanged(position)
|
||
refreshCallback?.onCallback()
|
||
}
|
||
})
|
||
}
|
||
}
|
||
} else {
|
||
downloadBtn.setOnClickListener {
|
||
allStateClickCallback?.onCallback()
|
||
clickCallback?.onCallback()
|
||
if ("download" == gameEntity.reserveStatus) {
|
||
ReservationHelper.showDeleteReservationDialog(context, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
ReservationHelper.deleteReservation(gameEntity, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
adapter?.notifyItemChanged(position)
|
||
refreshCallback?.onCallback()
|
||
}
|
||
})
|
||
}
|
||
})
|
||
} else {
|
||
ReservationHelper.showCancelReservationDialog(context, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
ReservationHelper.cancelReservation(gameEntity, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
adapter?.notifyItemChanged(position)
|
||
refreshCallback?.onCallback()
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}
|
||
}
|
||
}
|
||
return
|
||
}
|
||
if (gameEntity.getApk().size == 0 && gameEntity.h5Link != null) {
|
||
downloadBtn.setOnClickListener {
|
||
allStateClickCallback?.onCallback()
|
||
MtaHelper.onEvent("H5页面", "入口", "列表页_" + gameEntity.name)
|
||
val linkEntity = gameEntity.h5Link
|
||
val isPlay = "play" == linkEntity!!.type // 是否为开始玩
|
||
if (isPlay) {
|
||
HistoryHelper.insertGameEntity(gameEntity)
|
||
}
|
||
val i = WebActivity.getIntentForWebGame(context, gameEntity.h5Link!!.link, gameEntity.name, isPlay, linkEntity.closeButton)
|
||
context.startActivity(i)
|
||
}
|
||
} else if (gameEntity.getApk().size == 1) {
|
||
downloadBtn.setOnClickListener {
|
||
val clickRunnable = object : EmptyCallback {
|
||
override fun onCallback() {
|
||
allStateClickCallback?.onCallback()
|
||
clickCallback?.onCallback()
|
||
onNormalClick(context, downloadBtn, gameEntity, position, adapter, entrance, location, traceEvent, refreshCallback)
|
||
}
|
||
}
|
||
|
||
// 启动不需要请求存储权限
|
||
if (downloadBtn.text.toString() == context.getString(R.string.launch)) {
|
||
clickRunnable.onCallback()
|
||
} else {
|
||
PermissionHelper.checkStoragePermissionBeforeAction(context, clickRunnable)
|
||
}
|
||
}
|
||
} else {
|
||
downloadBtn.setOnClickListener { view: View ->
|
||
allStateClickCallback?.onCallback()
|
||
clickCallback?.onCallback()
|
||
GamePermissionDialogFragment.show(context, gameEntity, gameEntity.info) {
|
||
PermissionHelper.checkStoragePermissionBeforeAction(context, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
CertificationDialog.showCertificationDialog(context, gameEntity) {
|
||
DialogUtils.showVersionNumberDialog(context, gameEntity) {
|
||
DownloadDialog.showDownloadDialog(view.context, gameEntity, traceEvent, entrance, location)
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@JvmOverloads
|
||
fun onNormalClick(
|
||
context: Context,
|
||
downloadBtn: TextView,
|
||
gameEntity: GameEntity,
|
||
position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?,
|
||
entrance: String,
|
||
location: String,
|
||
traceEvent: ExposureEvent? = null,
|
||
refreshCallback: EmptyCallback? = null
|
||
) {
|
||
val str = downloadBtn.text.toString()
|
||
if (gameEntity.getApk().isEmpty()) return
|
||
val apk = gameEntity.getApk().safelyGetInRelease(0) ?: return
|
||
if (str == context.getString(R.string.download)) {
|
||
GamePermissionDialogFragment.show((context as AppCompatActivity), gameEntity, gameEntity.info) {
|
||
BrowserInstallHelper.showBrowserInstallHintDialog(context, gameEntity.isVGame(), object : EmptyCallback {
|
||
override fun onCallback() {
|
||
PackageCheckDialogFragment.show(context, gameEntity) {
|
||
DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
CertificationDialog.showCertificationDialog(context, gameEntity) {
|
||
DialogUtils.showOverseaDownloadDialog(context, gameEntity) {
|
||
DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
|
||
download(context, gameEntity, downloadBtn, entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
DataLogUtils.uploadGameLog(context, gameEntity.id, gameEntity.name, entrance)
|
||
} else if (str == context.getString(R.string.attempt)) {
|
||
GamePermissionDialogFragment.show((context as AppCompatActivity), gameEntity, gameEntity.info) {
|
||
BrowserInstallHelper.showBrowserInstallHintDialog(context, gameEntity.isVGame(), object : EmptyCallback {
|
||
override fun onCallback() {
|
||
PackageCheckDialogFragment.show(context, gameEntity) {
|
||
DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
CertificationDialog.showCertificationDialog(context, gameEntity) {
|
||
DialogUtils.showVersionNumberDialog(context, gameEntity) {
|
||
DialogUtils.showOverseaDownloadDialog(context, gameEntity) {
|
||
DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
|
||
download(context, gameEntity, downloadBtn, entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
DataLogUtils.uploadGameLog(context, gameEntity.id, gameEntity.name, entrance)
|
||
} else if (str == context.getString(R.string.smooth)) {
|
||
GamePermissionDialogFragment.show((context as AppCompatActivity), gameEntity, gameEntity.info) {
|
||
PackageCheckDialogFragment.show(context, gameEntity) {
|
||
DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
CertificationDialog.showCertificationDialog(context, gameEntity) {
|
||
DialogUtils.showVersionNumberDialog(context, gameEntity) {
|
||
DialogUtils.showOverseaDownloadDialog(context, gameEntity) {
|
||
VHelper.validateVSpaceBeforeAction(context, gameEntity, true) {
|
||
DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
|
||
download(context, gameEntity, downloadBtn, entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
} else if (str.contains("化")) {
|
||
if (entrance.contains("我的游戏")) {
|
||
MtaHelper.onEvent("我的游戏_启动", "插件化", gameEntity.name)
|
||
}
|
||
if (gameEntity.pluggableCollection != null) {
|
||
DownloadDialog.showDownloadDialog(context, gameEntity, traceEvent, entrance, location)
|
||
} else {
|
||
DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
CertificationDialog.showCertificationDialog(context, gameEntity) {
|
||
DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
|
||
plugin(context, gameEntity, downloadBtn, entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
}
|
||
}
|
||
})
|
||
}
|
||
} else if (str == context.getString(R.string.install)) {
|
||
val downloadEntity = SimulatorGameManager.findDownloadEntityByUrl(apk.url)
|
||
if (gameEntity.simulator != null) {
|
||
val isInstalled = PackageUtils.isInstalledFromAllPackage(context, gameEntity.simulator!!.apk!!.packageName)
|
||
if (downloadEntity != null && SimulatorGameManager.isSimulatorGame(gameEntity) && !isInstalled) {
|
||
SimulatorDownloadManager.getInstance().showDownloadDialog(
|
||
context, gameEntity.simulator,
|
||
SimulatorDownloadManager.SimulatorLocation.LAUNCH, gameEntity.id, gameEntity.name!!, null
|
||
)
|
||
return
|
||
}
|
||
}
|
||
|
||
if (gameEntity.isVGame()) {
|
||
VHelper.installOrLaunch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
|
||
} else {
|
||
install(context, gameEntity, position, adapter, refreshCallback)
|
||
}
|
||
} else if (str == context.getString(R.string.launch)) {
|
||
EnergyTaskHelper.postEnergyTask("play_game", gameEntity.id, gameEntity.getApk()[0].packageName)
|
||
|
||
//启动模拟器游戏
|
||
if (SimulatorGameManager.isSimulatorGame(gameEntity)) {
|
||
val downloadEntity = SimulatorGameManager.findDownloadEntityByUrl(gameEntity.getApk()[0].url)
|
||
if (downloadEntity != null) {
|
||
val file = File(downloadEntity.path)
|
||
if (!file.exists()) {
|
||
download(context, gameEntity, downloadBtn, entrance, location, false, traceEvent)
|
||
return
|
||
}
|
||
SimulatorGameManager.launchSimulatorGame(downloadEntity, gameEntity)
|
||
}
|
||
return
|
||
}
|
||
|
||
if (gameEntity.isVGame()) {
|
||
VHelper.installOrLaunch((context as AppCompatActivity), gameEntity.getUniquePackageName() ?: "")
|
||
return
|
||
}
|
||
|
||
if (entrance.contains("我的游戏")) {
|
||
MtaHelper.onEvent("我的游戏_启动", "启动", gameEntity.name)
|
||
}
|
||
PackageUtils.launchApplicationByPackageName(context, gameEntity.getApk()[0].packageName)
|
||
} else if (str == context.getString(R.string.update)) {
|
||
if (entrance.contains("我的游戏")) {
|
||
MtaHelper.onEvent("我的游戏_启动", "更新", gameEntity.name)
|
||
}
|
||
if (gameEntity.isVGame()) {
|
||
VHelper.updateOrReDownload(gameEntity)
|
||
return
|
||
}
|
||
DownloadDialogHelper.findAvailableDialogAndShow(context, gameEntity, apk, object : EmptyCallback {
|
||
override fun onCallback() {
|
||
DialogUtils.checkDownload(context, apk.size) { isSubscribe: Boolean ->
|
||
update(context, gameEntity, entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
}
|
||
})
|
||
} else {
|
||
if (gameEntity.isVGame()) {
|
||
context.startActivity(VDownloadManagerActivity.getIntent(context, true))
|
||
} else {
|
||
context.startActivity(
|
||
DownloadManagerActivity.getDownloadMangerIntent(
|
||
context,
|
||
apk.url, entrance + "+(" + location.split(":").toTypedArray()[0] + ")"
|
||
)
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
//下载
|
||
private fun download(
|
||
context: Context,
|
||
gameEntity: GameEntity,
|
||
downloadBtn: TextView,
|
||
entrance: String,
|
||
location: String,
|
||
isSubscribe: Boolean,
|
||
traceEvent: ExposureEvent?
|
||
) {
|
||
val msg = FileUtils.isCanDownload(context, gameEntity.getApk()[0].size)
|
||
if (TextUtils.isEmpty(msg)) {
|
||
DownloadManager.createDownload(context, gameEntity, context.getString(R.string.download), entrance, location, isSubscribe, traceEvent)
|
||
Utils.toast(context, gameEntity.name + "已加入下载队列")
|
||
downloadBtn.setText(R.string.downloading)
|
||
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style)
|
||
downloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style))
|
||
DeviceRemindDialog.showDeviceRemindDialog(context, gameEntity)
|
||
} else {
|
||
Utils.toast(context, msg)
|
||
}
|
||
}
|
||
|
||
//插件化
|
||
private fun plugin(
|
||
context: Context, gameEntity: GameEntity, downloadBtn: TextView, entrance: String,
|
||
location: String, isSubscribe: Boolean, traceEvent: ExposureEvent?
|
||
) {
|
||
val msg = FileUtils.isCanDownload(context, gameEntity.getApk()[0].size)
|
||
if (TextUtils.isEmpty(msg)) {
|
||
DownloadManager.createDownload(context, gameEntity, "插件化", entrance, location, isSubscribe, traceEvent)
|
||
Utils.toast(context, gameEntity.name + "已加入下载队列")
|
||
downloadBtn.setText(R.string.downloading)
|
||
downloadBtn.setBackgroundResource(R.drawable.game_item_btn_downloading_style)
|
||
downloadBtn.setTextColor(ContextCompat.getColorStateList(context, R.color.text_downloading_style))
|
||
} else {
|
||
Utils.toast(context, msg)
|
||
}
|
||
}
|
||
|
||
//安装
|
||
private fun install(
|
||
context: Context, gameEntity: GameEntity, position: Int,
|
||
adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder?>?, refreshCallback: EmptyCallback?
|
||
) {
|
||
val apkEntity = gameEntity.getApk()[0]
|
||
val downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot(gameEntity)
|
||
if (downloadEntity != null) {
|
||
val path = downloadEntity.path
|
||
when {
|
||
FileUtils.isEmptyFile(path) -> {
|
||
Utils.toast(context, R.string.install_failure_hint)
|
||
DownloadManager.getInstance().cancel(downloadEntity.url)
|
||
gameEntity.getEntryMap().remove(apkEntity.getPlatform())
|
||
adapter?.notifyItemChanged(position)
|
||
refreshCallback?.onCallback()
|
||
}
|
||
PackageUtils.isCanPluggable(apkEntity) -> {
|
||
DialogHelper.showPluginDialog(context) { PackageInstaller.uninstall(context, path) }
|
||
}
|
||
else -> {
|
||
PackageInstaller.install(context, downloadEntity)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//更新
|
||
private fun update(
|
||
context: Context,
|
||
gameEntity: GameEntity,
|
||
entrance: String,
|
||
location: String,
|
||
isSubscribe: Boolean,
|
||
traceEvent: ExposureEvent?
|
||
) {
|
||
DownloadManager.createDownload(context, gameEntity, "更新", entrance, location, isSubscribe, traceEvent)
|
||
}
|
||
} |