305 lines
12 KiB
Kotlin
305 lines
12 KiB
Kotlin
package com.gh.common.dialog
|
|
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.LinearLayout
|
|
import android.widget.RelativeLayout
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.core.content.ContextCompat
|
|
import androidx.fragment.app.FragmentTransaction
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.gh.base.BaseRecyclerViewHolder
|
|
import com.gh.common.constant.Constants
|
|
import com.gh.common.util.*
|
|
import com.gh.common.view.CustomLinkMovementMethod
|
|
import com.gh.gamecenter.R
|
|
import com.gh.gamecenter.databinding.FragmentPackageCheckBinding
|
|
import com.gh.gamecenter.databinding.PackageCheckItemBinding
|
|
import com.gh.gamecenter.entity.DetectionObjectEntity
|
|
import com.gh.gamecenter.entity.PackageDialogEntity
|
|
import com.gh.gamecenter.eventbus.EBPackage
|
|
import com.halo.assistant.HaloApp
|
|
import com.lightgame.adapter.BaseRecyclerAdapter
|
|
import com.lightgame.dialog.BaseDialogFragment
|
|
import io.reactivex.disposables.Disposable
|
|
import org.greenrobot.eventbus.EventBus
|
|
import org.greenrobot.eventbus.Subscribe
|
|
import org.greenrobot.eventbus.ThreadMode
|
|
|
|
/**
|
|
* 包名检测弹窗
|
|
*/
|
|
class PackageCheckDialogFragment : BaseDialogFragment() {
|
|
|
|
private lateinit var binding: FragmentPackageCheckBinding
|
|
private var mTotalWidth = 0f
|
|
private val mDuration = 3000
|
|
private var mDisposable: Disposable? = null
|
|
private var mAdapter: PackageCheckAdapter? = null
|
|
var packageDialogEntity: PackageDialogEntity? = null
|
|
var callBack: DialogUtils.ConfirmListener? = null
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
EventBus.getDefault().register(this)
|
|
}
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
binding = FragmentPackageCheckBinding.inflate(inflater, container, false)
|
|
return binding.root
|
|
}
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
super.onViewCreated(view, savedInstanceState)
|
|
packageDialogEntity?.let {
|
|
changeParams(it.detectionObjects.size)
|
|
|
|
binding.packageRv.layoutManager = LinearLayoutManager(requireContext())
|
|
mAdapter = PackageCheckAdapter(requireContext(), it.detectionObjects)
|
|
binding.packageRv.adapter = mAdapter
|
|
|
|
binding.titleTv.text = it.title
|
|
binding.contentTv.text = it.content
|
|
|
|
val spanBuilder = SpanBuilder(it.linkHintText).build()
|
|
it.links.forEachIndexed { index, link ->
|
|
val linkSpan = SpanBuilder(link.title ?: "").click(0, (link.title
|
|
?: "").length, R.color.theme_font, true) {
|
|
DirectUtils.directToLinkPage(requireContext(), link, "包名检测弹窗", "")
|
|
}.build()
|
|
spanBuilder.append(linkSpan)
|
|
if (index != it.links.size - 1) {
|
|
spanBuilder.append("、")
|
|
}
|
|
}
|
|
binding.linkHintTv.text = spanBuilder
|
|
binding.linkHintTv.movementMethod = CustomLinkMovementMethod.getInstance()
|
|
|
|
when (it.level) {
|
|
"HINT_SKIP" -> {
|
|
binding.cancelTv.text = "取消"
|
|
binding.noRemindAgainCb.visibility = View.GONE
|
|
}
|
|
"ALWAYS_HINT" -> {
|
|
binding.cancelTv.text = "我知道了"
|
|
binding.noRemindAgainCb.visibility = View.GONE
|
|
}
|
|
else -> {
|
|
binding.cancelTv.text = "我知道了"
|
|
binding.noRemindAgainCb.visibility = View.VISIBLE
|
|
}
|
|
}
|
|
initListener(it)
|
|
}
|
|
checkPackage()
|
|
}
|
|
|
|
private fun changeParams(size: Int) {
|
|
val params = binding.packageRv.layoutParams as LinearLayout.LayoutParams
|
|
params.height = if (size > 3) (28f.dip2px() * 3.5).toInt() else 28f.dip2px() * size
|
|
binding.packageRv.layoutParams = params
|
|
}
|
|
|
|
private fun initListener(entity: PackageDialogEntity) {
|
|
binding.downloadBtn.setOnClickListener {
|
|
if (binding.noRemindAgainCb.isChecked) {
|
|
SPUtils.setBoolean(Constants.SP_PACKAGE_CHECK, true)
|
|
}
|
|
val isAllPackageInstalled = isAllPackageInstalled(entity)
|
|
if (isAllPackageInstalled) {
|
|
callBack?.onConfirm()
|
|
dismissAllowingStateLoss()
|
|
} else {
|
|
val packageLink = entity.links.find { it.buttonLink }
|
|
if (packageLink != null) {
|
|
DirectUtils.directToLinkPage(requireContext(), packageLink, "包名检测弹窗", "")
|
|
}
|
|
}
|
|
}
|
|
|
|
binding.cancelTv.setOnClickListener {
|
|
if (entity.level != "HINT_SKIP") {
|
|
callBack?.onConfirm()
|
|
}
|
|
if (binding.noRemindAgainCb.isChecked) {
|
|
SPUtils.setBoolean(Constants.SP_PACKAGE_CHECK, true)
|
|
}
|
|
dismissAllowingStateLoss()
|
|
}
|
|
}
|
|
|
|
private fun checkPackage() {
|
|
var index = 0
|
|
mTotalWidth = (DisplayUtils.getScreenWidth() - 108f.dip2px()).toFloat()
|
|
mDisposable = rxTimer(1) {
|
|
val width = (mTotalWidth / mDuration) * it
|
|
val params = binding.progressView.layoutParams as RelativeLayout.LayoutParams
|
|
params.width = width.toInt()
|
|
binding.progressView.layoutParams = params
|
|
|
|
packageDialogEntity?.detectionObjects?.let { objects ->
|
|
if (objects.isNotEmpty()) {
|
|
val averageTime = if (objects.size == 1) {
|
|
mDuration
|
|
} else {
|
|
mDuration / objects.size
|
|
}
|
|
if (it != 0L && it % averageTime == 0L && index < objects.size) {
|
|
mAdapter?.notifyPackages()
|
|
binding.packageRv.smoothScrollToPosition(index)
|
|
index++
|
|
}
|
|
}
|
|
}
|
|
|
|
if (it >= mDuration) {
|
|
mDisposable?.dispose()
|
|
binding.downloadBtn.isEnabled = true
|
|
binding.progressText.text = "检测完成"
|
|
binding.progressView.background = ContextCompat.getDrawable(requireContext(), R.drawable.package_check_complete_bg)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onStart() {
|
|
super.onStart()
|
|
val width = requireContext().resources.displayMetrics.widthPixels - 60F.dip2px()
|
|
val height = ViewGroup.LayoutParams.WRAP_CONTENT
|
|
dialog?.window?.setLayout(width, height)
|
|
dialog?.setCanceledOnTouchOutside(true)
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
packageDialogEntity?.let {
|
|
if (isAllPackageInstalled(it)) {
|
|
callBack?.onConfirm()
|
|
dismissAllowingStateLoss()
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onDestroyView() {
|
|
super.onDestroyView()
|
|
EventBus.getDefault().unregister(this)
|
|
if (mDisposable?.isDisposed == false) {
|
|
mDisposable?.dispose()
|
|
}
|
|
}
|
|
|
|
//安装、卸载事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
fun onEventMainThread(busFour: EBPackage) {
|
|
if ("安装" == busFour.type || "卸载" == busFour.type) {
|
|
mAdapter?.notifyDataSetChanged()
|
|
}
|
|
}
|
|
|
|
class PackageCheckAdapter(val context: Context, val entities: ArrayList<DetectionObjectEntity>) : BaseRecyclerAdapter<RecyclerView.ViewHolder>(context) {
|
|
private var index = -1
|
|
|
|
fun notifyPackages() {
|
|
index++
|
|
notifyDataSetChanged()
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
return PackageCheckViewHolder(PackageCheckItemBinding.bind(LayoutInflater.from(context).inflate(R.layout.package_check_item, parent, false)))
|
|
}
|
|
|
|
override fun getItemCount(): Int = entities.size
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
if (holder is PackageCheckViewHolder) {
|
|
val entity = entities[position]
|
|
holder.binding.entity = entity
|
|
if (position <= index) {
|
|
var isAllInstalled = false
|
|
entity.packages.forEach { packageName ->
|
|
val isInstalled = PackageUtils.getInstalledPackages(context, 0).find { it.packageName == packageName } != null
|
|
if (isInstalled) {
|
|
isAllInstalled = true
|
|
return@forEach
|
|
}
|
|
}
|
|
if (isAllInstalled) {
|
|
holder.binding.statusTv.text = "已安装"
|
|
holder.binding.statusTv.setTextColor(ContextCompat.getColor(context, R.color.theme_font))
|
|
} else {
|
|
holder.binding.statusTv.text = "未安装"
|
|
holder.binding.statusTv.setTextColor(ContextCompat.getColor(context, R.color.text_FF4147))
|
|
}
|
|
holder.binding.statusTv.visibility = View.VISIBLE
|
|
} else {
|
|
holder.binding.statusTv.visibility = View.GONE
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class PackageCheckViewHolder(val binding: PackageCheckItemBinding) : BaseRecyclerViewHolder<DetectionObjectEntity>(binding.root)
|
|
|
|
companion object {
|
|
@JvmStatic
|
|
fun show(activity: AppCompatActivity, packageDialogEntity: PackageDialogEntity?, callBack: DialogUtils.ConfirmListener) {
|
|
if (packageDialogEntity == null) {
|
|
callBack.onConfirm()
|
|
return
|
|
}
|
|
if (isAllPackageInstalled(packageDialogEntity)) {
|
|
callBack.onConfirm()
|
|
return
|
|
}
|
|
val isChoose = SPUtils.getBoolean(Constants.SP_PACKAGE_CHECK, false)
|
|
if (packageDialogEntity.level == "OPTIONAL_HINT" && isChoose) {
|
|
callBack.onConfirm()
|
|
return
|
|
}
|
|
|
|
var dialogFragment = activity.supportFragmentManager.findFragmentByTag(PackageCheckDialogFragment::class.java.simpleName) as? PackageCheckDialogFragment
|
|
if (dialogFragment == null) {
|
|
dialogFragment = PackageCheckDialogFragment()
|
|
dialogFragment.packageDialogEntity = packageDialogEntity
|
|
dialogFragment.callBack = callBack
|
|
|
|
dialogFragment.show(activity.supportFragmentManager, PackageCheckDialogFragment::class.java.simpleName)
|
|
} else {
|
|
dialogFragment.packageDialogEntity = packageDialogEntity
|
|
dialogFragment.callBack = callBack
|
|
|
|
val transaction: FragmentTransaction = activity.supportFragmentManager.beginTransaction()
|
|
transaction.show(dialogFragment)
|
|
transaction.commit()
|
|
}
|
|
|
|
}
|
|
|
|
fun isAllPackageInstalled(packageDialogEntity: PackageDialogEntity): Boolean {
|
|
var isAllInstalled = true
|
|
|
|
val isPackagesInstall: (ArrayList<String>) -> Boolean = { packages ->
|
|
var isPackagesInstalled = false
|
|
packages.forEach {packageName->
|
|
val isInstalled = PackageUtils.getInstalledPackages(HaloApp.getInstance().application, 0).find { it.packageName == packageName } != null
|
|
if (isInstalled) {
|
|
isPackagesInstalled = true
|
|
return@forEach
|
|
}
|
|
}
|
|
isPackagesInstalled
|
|
}
|
|
|
|
packageDialogEntity.detectionObjects.forEach loop@{ obj ->
|
|
if (!isPackagesInstall(obj.packages)) {
|
|
isAllInstalled = false
|
|
return isAllInstalled
|
|
}
|
|
}
|
|
return isAllInstalled
|
|
}
|
|
}
|
|
} |