151 lines
5.6 KiB
Kotlin
151 lines
5.6 KiB
Kotlin
package com.gh.common.util
|
|
|
|
import android.content.Context
|
|
import android.os.Build
|
|
import com.gh.gamecenter.R
|
|
import com.gh.gamecenter.common.utils.DialogHelper
|
|
import com.gh.gamecenter.common.utils.SensorsBridge
|
|
import com.gh.gamecenter.common.utils.toResString
|
|
import com.gh.gamecenter.core.utils.EmptyCallback
|
|
import com.gh.gamecenter.feature.entity.ApkEntity
|
|
import com.gh.gamecenter.feature.entity.GameEntity
|
|
|
|
object DownloadDialogHelper {
|
|
|
|
/**
|
|
* [callback] 执行点击关闭以后会执行的代码块,没有弹窗也会执行这个代码块
|
|
*/
|
|
@JvmStatic
|
|
fun findAvailableDialogAndShow(
|
|
context: Context,
|
|
gameEntity: GameEntity,
|
|
apkEntity: ApkEntity,
|
|
callback: EmptyCallback
|
|
) {
|
|
val dialog = retrieveAvailableDialog(gameEntity, apkEntity)
|
|
if (dialog != null) {
|
|
SensorsBridge.trackGameDownloadDialogShow(
|
|
gameId = gameEntity.id,
|
|
gameName = gameEntity.name ?: "",
|
|
gameType = gameEntity.categoryChinese
|
|
)
|
|
DialogHelper.showDialogWithHtmlContent(
|
|
context,
|
|
dialog.title,
|
|
dialog.content,
|
|
"继续下载",
|
|
"取消",
|
|
confirmClickCallback = {
|
|
SensorsBridge.trackGameDownloadDialogClick(
|
|
buttonName = "继续下载",
|
|
gameId = gameEntity.id ,
|
|
gameName = gameEntity.name ?: "",
|
|
gameType = gameEntity.categoryChinese
|
|
)
|
|
callback.onCallback()
|
|
},
|
|
cancelClickCallback = {
|
|
SensorsBridge.trackGameDownloadDialogClick(
|
|
buttonName = "取消",
|
|
gameId = gameEntity.id ,
|
|
gameName = gameEntity.name ?: "",
|
|
gameType = gameEntity.categoryChinese
|
|
)
|
|
},
|
|
touchOutsideCallback = {
|
|
SensorsBridge.trackGameDownloadDialogClick(
|
|
buttonName = "关闭弹窗",
|
|
gameId = gameEntity.id ,
|
|
gameName = gameEntity.name ?: "",
|
|
gameType = gameEntity.categoryChinese
|
|
)
|
|
}
|
|
)
|
|
} else {
|
|
callback.onCallback()
|
|
}
|
|
}
|
|
|
|
private fun retrieveAvailableDialog(gameEntity: GameEntity, apkEntity: ApkEntity): GameEntity.Dialog? {
|
|
val downloadDialog = if (gameEntity.shouldUseMirrorInfo()) {
|
|
gameEntity.obtainMirrorData()?.downloadDialog
|
|
} else {
|
|
gameEntity.downloadDialog
|
|
}
|
|
|
|
if (downloadDialog.isNullOrEmpty()) return null
|
|
for (dialog in downloadDialog) {
|
|
val versionName = PackageUtils.getGhVersionName()
|
|
val noticeVersions = dialog.rule.noticeVersions
|
|
if (noticeVersions.isEmpty() || noticeVersions.contains(versionName)) {
|
|
// 共有 8 种可能
|
|
// 1. 不指定包名、机型和系统版本
|
|
if (dialog.rule.packageName.isEmpty()
|
|
&& dialog.rule.models.isEmpty()
|
|
&& dialog.rule.systemVersions.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 2. 指定包名不管机型和系统
|
|
if (dialog.rule.packageName == apkEntity.packageName
|
|
&& dialog.rule.models.isEmpty()
|
|
&& dialog.rule.systemVersions.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 3. 指定机型不管包名和系统
|
|
if (dialog.rule.models.contains(Build.MODEL)
|
|
&& dialog.rule.packageName.isEmpty()
|
|
&& dialog.rule.systemVersions.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 4. 指定系统不管包名和机型
|
|
if (dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
|
|
&& dialog.rule.packageName.isEmpty()
|
|
&& dialog.rule.models.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 5. 指定包名和机型不管系统
|
|
if (dialog.rule.packageName == apkEntity.packageName
|
|
&& dialog.rule.models.contains(Build.MODEL)
|
|
&& dialog.rule.systemVersions.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 6. 指定包名和系统不管机型
|
|
if (dialog.rule.packageName == apkEntity.packageName
|
|
&& dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
|
|
&& dialog.rule.models.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 7. 指定机型和系统不管包名
|
|
if (dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
|
|
&& dialog.rule.models.contains(Build.MODEL)
|
|
&& dialog.rule.packageName.isEmpty()
|
|
) {
|
|
return dialog
|
|
}
|
|
|
|
// 8.指定包名、机型和系统版本
|
|
if (dialog.rule.packageName == apkEntity.packageName
|
|
&& dialog.rule.models.contains(Build.MODEL)
|
|
&& dialog.rule.systemVersions.contains(Build.VERSION.RELEASE)
|
|
) {
|
|
return dialog
|
|
}
|
|
} else return null
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
} |