Commit 8efd08e8 authored by jyx's avatar jyx

代码优化

parent b116c2fc
......@@ -14,8 +14,9 @@ public class TzTaskBean {
private int coin;
private int state;
private String currentPkgName;
private String downLoadPkgName;
public TzTaskBean(String icon, String title,String description, boolean isShCpd,int coin, int state, String currentPkgName) {
public TzTaskBean(String icon, String title,String description, boolean isShCpd,int coin, int state, String downLoadPkgName) {
this.icon = icon;
this.title = title;
this.description = description;
......@@ -23,7 +24,15 @@ public class TzTaskBean {
this.coralAd = coralAd;
this.coin = coin;
this.state = state;
this.currentPkgName = currentPkgName;
this.downLoadPkgName = downLoadPkgName;
}
public String getDownLoadPkgName() {
return downLoadPkgName;
}
public void setDownLoadPkgName(String downLoadPkgName) {
this.downLoadPkgName = downLoadPkgName;
}
public String getDescription() {
......
......@@ -218,7 +218,7 @@ class TaskActivity : BaseActivity(), TaskView,
if (!mFakeTaskList[i].isShCpd && !mFakeTaskList[j].isShCpd) {
// 数据重复
if (i != j && mFakeTaskList[i].currentPkgName == mFakeTaskList[j].currentPkgName) {
if (i != j && mFakeTaskList[i].downLoadPkgName == mFakeTaskList[j].downLoadPkgName) {
flag = false
break
}
......@@ -227,7 +227,7 @@ class TaskActivity : BaseActivity(), TaskView,
if (flag) {
// 是否已经安装此app
if (!DeviceInfo.instance.isLoadingPkg(mFakeTaskList[i].currentPkgName)) {
if (!DeviceInfo.instance.isLoadingPkg(mFakeTaskList[i].downLoadPkgName)) {
cpdLoadCountTime--
newPkgData.add(mFakeTaskList[i])
}
......@@ -470,8 +470,9 @@ class TaskActivity : BaseActivity(), TaskView,
taskPresenter.cmtImp(trackerBean.clikc[0])
}
// 圣于地开始下载
DownloadApkManager.setOnMyDownloadListener(this@TaskActivity)
DownloadApkManager.downloadApk(this@TaskActivity, mFakeTaskList[position].app_url, mFakeTaskList[position].currentPkgName)
DownloadApkManager.downloadApk(this@TaskActivity, mFakeTaskList[position].app_url, mFakeTaskList[position].downLoadPkgName)
}
})
taskCpdDialog?.setData(mFakeTaskList[position].title, mFakeTaskList[position].description, mFakeTaskList[position].icon, needUseTime, mCoin)
......
package com.mints.goodmoney.ui.fragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.download.CoralDownload
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.common.DeviceInfo
import com.mints.goodmoney.manager.AppTryPlayManager
import com.mints.goodmoney.manager.DownloadApkManager
import com.mints.goodmoney.mvp.model.CpdModelBean
import com.mints.goodmoney.mvp.model.TzTaskBean
import com.mints.goodmoney.mvp.presenters.TaskPresenter
import com.mints.goodmoney.mvp.views.TaskView
import com.mints.goodmoney.service.AppInstallService
import com.mints.goodmoney.ui.activitys.AwardActivity
import com.mints.goodmoney.ui.adapter.TaskAdapter
import com.mints.goodmoney.ui.adapter.listener.OnItemChildClickListener
import com.mints.goodmoney.ui.fragment.base.BaseFragment
import com.mints.goodmoney.ui.widgets.CustomDialogAsApple
import com.mints.goodmoney.ui.widgets.DialogListener
import com.mints.goodmoney.ui.widgets.TaskCpdDialog
import com.mints.goodmoney.ui.widgets.TaskDialog
import com.mints.goodmoney.utils.*
import com.mints.goodmoney.utils.rxutil.CommonRxTask
import com.mints.goodmoney.utils.rxutil.RxjavaUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.tz.sdk.coral.ad.CoralAD
import com.tz.sdk.coral.callback.h5.DownloadProcess
import kotlinx.android.synthetic.main.activity_task.*
import kotlinx.android.synthetic.main.header_layout.*
class TaskCpdFragment : BaseFragment(), TaskView,
OnRefreshListener,
CoralDownload.OnAdLoadListener,
OnItemChildClickListener,
View.OnClickListener,
DownloadApkManager.OnMyDownloadListener {
private val taskPresenter by lazy { TaskPresenter() }
private var mTaskAdapter: TaskAdapter? = null
private val mFakeTaskList: MutableList<TzTaskBean> = arrayListOf()
// 任务弹框
private var taskDialog: TaskDialog? = null
private var taskCpdDialog: TaskCpdDialog? = null
private lateinit var cdaa: CustomDialogAsApple
// 天卓下载广告请求
private var mCoralDownload: CoralDownload? = null
private var mDownloadProcess: DownloadProcess? = null
// APP下载回调广播
private var mReceiverBroadcastReceiver: BroadcastReceiver? = null
// 试完时间不足弹窗
private var tryTimeOut = false
// 第二次拉取重复不拉取
private var isTwiceGetCpd = false
private val outNetIp by lazy { SPUtil.getInstance().getString(Constant.OUT_NET_IP) }
companion object {
const val TASK_COIN = "task_coin"
const val TASK_USE_MAX = "task_use_max"
const val TASK_USE_COMPLETE = "task_use_complete"
}
private var mCoin = 0
private var needUseTime = -1
private var shMax = 3 //珊瑚 广告最大填充数
private var cpdMax = 3 //CPD 广告最大填充数
private var shLoadCountTime = 0 // 珊瑚 加载广告次数
private var cpdLoadCountTime = 0 // CPD 加载广告次数
// override fun getBundleExtras(extras: Bundle) {
// super.getBundleExtras(extras)
// mCoin = extras.getInt(TaskActivity.TASK_COIN)
// }
override fun initViewsAndEvents() {
taskPresenter.attachView(this)
tv_title.text = "下载试玩任务"
iv_left_icon.visibility = View.VISIBLE
iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
mCoralDownload = CoralDownload().setOnAdLoadListener(this)
CacheUtil.clearAllCache(context)
initView()
initListener()
loadData()
}
private fun loadCPDData() {
taskPresenter.getCpdDetail(outNetIp)
}
private fun initView() {
val gridLayoutManager = GridLayoutManager(mContext, 3)
recy_task.layoutManager = gridLayoutManager
recy_task.addItemDecoration(SpacesItemDecoration(3, SpacesItemDecoration.px2dp(10f)))
mTaskAdapter = TaskAdapter(mFakeTaskList, mContext)
recy_task.adapter = mTaskAdapter
recy_task.setItemViewCacheSize(20)
mTaskAdapter?.setOnItemChildClickListener(this)
iv_left_icon.setOnClickListener(this)
}
private fun initListener() {
srl_task.setOnRefreshListener(this)
}
override fun onResume() {
super.onResume()
DownloadApkManager.tryOnceInstallApk()
try {
if (needUseTime != -1) {
refreshDemoTask()
}
} catch (e: Exception) {
LogUtil.e(e.printStackTrace())
}
}
override fun getContentViewLayoutID() = R.layout.activity_task
override fun getShCpdTimeSuc(time: Int, CPD: Int, CPD_SYD: Int) {
needUseTime = time
shMax = CPD
cpdMax = CPD_SYD
if ((shMax + cpdMax) <= 0) {
hideLoading()
showToast("今日任务已完成,请明日再试")
// finish()
return
}
isTwiceGetCpd = false
shLoadCountTime = 0
cpdLoadCountTime = 0
mFakeTaskList.clear()
if (shMax > 0 && (cpdMax > 0 || cpdMax == 0)) {
for (i in 0 until shMax) {
mCoralDownload?.pull()
}
} else if (cpdMax > 0) {
for (i in 0 until cpdMax) {
loadCPDData()
}
}
}
override fun getCpdModelSuc(dataBean: CpdModelBean.DataBean) {
for (material in dataBean.materials) {
val tzTaskBean = TzTaskBean(material.icons, material.title, material.description, false, mCoin, 0, material.app_apk_name)
tzTaskBean.app_url = material.app_url
tzTaskBean.trackerBean = material.tracker
mFakeTaskList.add(tzTaskBean)
}
// 多拉取的CPD广告
if (cpdLoadCountTime++ >= cpdMax - 1) {
getCpdRepeatPkg()
}
}
override fun getCpdModelFail() {
if (cpdLoadCountTime++ >= cpdMax - 1) {
notifyDataAndShowAd()
}
}
override fun onRefresh(refreshLayout: RefreshLayout) {
loadData()
}
/**
* 遍历去重APP包名
*/
private fun getCpdRepeatPkg() {
if (isTwiceGetCpd) {
if (cpdLoadCountTime >= cpdMax) {
notifyDataAndShowAd()
}
return
}
cpdLoadCountTime = cpdMax
RxjavaUtil.executeRxTask<MutableList<TzTaskBean>>(object : CommonRxTask<MutableList<TzTaskBean>>() {
override fun doInIOThread() {
val newPkgData = mutableListOf<TzTaskBean>()
for (i in 0 until mFakeTaskList.size) {
if (mFakeTaskList[i].isShCpd) {
newPkgData.add(mFakeTaskList[i])
} else {
var flag = true
for (j in 0 until mFakeTaskList.size) {
if (!mFakeTaskList[i].isShCpd && !mFakeTaskList[j].isShCpd) {
// 数据重复
if (i != j && mFakeTaskList[i].currentPkgName == mFakeTaskList[j].currentPkgName) {
flag = false
break
}
}
}
if (flag) {
// 是否已经安装此app
if (!DeviceInfo.instance.isLoadingPkg(mFakeTaskList[i].currentPkgName)) {
cpdLoadCountTime--
newPkgData.add(mFakeTaskList[i])
}
}
}
}
t = newPkgData
}
override fun doInUIThread() {
mFakeTaskList.clear()
mFakeTaskList.addAll(t)
// 重新获取数据
if (cpdLoadCountTime in 1..cpdMax) {
isTwiceGetCpd = true
for (i in 0 until cpdLoadCountTime) {
loadCPDData()
}
cpdLoadCountTime = cpdMax - cpdLoadCountTime
return
} else {
notifyDataAndShowAd()
}
}
})
}
private fun notifyDataAndShowAd() {
hideLoading()
srl_task.finishRefresh()
mTaskAdapter?.notifyDataSetChanged()
if (mFakeTaskList.size == 0) {
iv_empty.visibility = View.VISIBLE
return
} else {
iv_empty.visibility = View.GONE
}
for (tzTaskBean in mFakeTaskList) {
if (!tzTaskBean.isShCpd) {
val trackerBean = tzTaskBean.trackerBean
if (trackerBean != null) {
LogUtil.d("CPD -> show")
// 上报CPD显示
taskPresenter.cmtImp(trackerBean.show[0])
}
}
}
}
override fun onLoadSuccess(dataList: MutableList<CoralAD>?) {
dataList?.let {
for (coralAD in it) {
val tzTaskBean = TzTaskBean(coralAD.icon, coralAD.title, coralAD.description, true, mCoin, 0, null)
tzTaskBean.coralAd = coralAD
mFakeTaskList.add(tzTaskBean)
}
}
// 多拉取的广告
if (shLoadCountTime++ >= shMax - 1) {
if (cpdMax == 0) {
notifyDataAndShowAd()
return
}
for (i in 0 until cpdMax) {
loadCPDData()
}
}
}
override fun onLoadFail() {
// 次数刷新完 且未拿到数据 提示
if (shLoadCountTime++ >= shMax - 1) {
if (cpdMax == 0) {
notifyDataAndShowAd()
return
}
for (i in 0 until cpdMax) {
loadCPDData()
}
}
}
override fun onSDKDownloadStart() {
taskDialog?.dismiss()
registerBroad()
}
override fun onSDKDownloaded() {
// SDK上报下载完成
// mDownloadProcess?.reportDownloadSuccess(localFilePath, false)
}
override fun onSDKInstalled() {
// SDK上报安装完成
// mDownloadProcess?.reportInstallSuccess(false)
}
override fun onGetDownloadProcess(downloadProcess: DownloadProcess?) {
// 点击之后回调下载数据
mDownloadProcess = downloadProcess
// 跳转去下载
val downloadUrl = downloadProcess!!.downloadUrl
val packageName = downloadProcess.packageName
DownloadApkManager.setOnMyDownloadListener(this)
DownloadApkManager.downloadApk(requireActivity(), downloadUrl, packageName)
}
override fun onDestroy() {
mCoralDownload?.destroy()
mDownloadProcess = null
DownloadApkManager.destroy()
if (mReceiverBroadcastReceiver != null) {
mContext.stopService(Intent(context, AppInstallService::class.java))
mContext.unregisterReceiver(mReceiverBroadcastReceiver)
}
taskPresenter.detachView()
super.onDestroy()
}
// override fun onBackPressed() {
// super.onBackPressed()
// backDialog()
// }
override fun onItemChildClick(view: View?, position: Int) {
when (mFakeTaskList[position].state) {
// 0 立即下载, 1 去试玩, 2 领取奖励 3 已领取 4 继续试玩
0 -> {
tryTimeOut = false
if (AppUtil.isOpenUsageStats()) {
clickDialog(position)
} else {
AppTryPlayManager.openAppUsageStats(requireActivity())
}
}
1 -> {
mTaskAdapter?.let {
val currentPkgName = mFakeTaskList[it.getPosition()].currentPkgName
if (currentPkgName == null) {
showToast("任务异常,请重试~")
mFakeTaskList[it.getPosition()].state = 0
it.notifyItemChanged(it.getPosition())
} else {
if (AppUtil.checkPackInfo(mContext, currentPkgName)) {
AppTryPlayManager.toTryPlay(currentPkgName)
} else {
showToast("未找到当前APP、可能被卸载,请重试~")
// 重置当前列表状态
mFakeTaskList[it.getPosition()].state = 0
it.notifyItemChanged(it.getPosition())
}
}
}
}
2 -> {
val bundle = Bundle()
bundle.putInt(Constant.MAIN_CUR_COIN, mCoin)
mTaskAdapter?.let {
if (mFakeTaskList[it.getPosition()].isShCpd) {
bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_CPD)
} else {
bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_CPD_SYD)
}
}
readyGo(AwardActivity::class.java, bundle)
// finish()
}
3 -> {
showToast("当前任务已完成,试试其他任务吧!")
}
4 -> {
mTaskAdapter?.let {
val currentPkgName = mFakeTaskList[it.getPosition()].currentPkgName
if (currentPkgName == null) {
showToast("任务异常,请重试~")
mFakeTaskList[it.getPosition()].state = 0
it.notifyItemChanged(it.getPosition())
} else {
if (AppUtil.checkPackInfo(mContext, currentPkgName)) {
AppTryPlayManager.toTryPlay(currentPkgName)
} else {
showToast("未找到当前APP、可能被卸载,请重试~")
// 重置当前列表状态
mFakeTaskList[it.getPosition()].state = 0
it.notifyItemChanged(it.getPosition())
}
}
}
}
}
}
private fun clickDialog(position: Int) {
if (needUseTime == -1) return
if (mFakeTaskList[position].isShCpd) {
taskDialog = TaskDialog(requireActivity())
taskDialog?.show()
taskDialog?.setData(mFakeTaskList[position].coralAd, needUseTime, mCoin)
} else {
taskCpdDialog = TaskCpdDialog(requireActivity(), object : DialogListener() {
override fun onClick(v: View?) {
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
LogUtil.d("CPD -> clikc")
// 上报CPD点击
taskPresenter.cmtImp(trackerBean.clikc[0])
}
DownloadApkManager.setOnMyDownloadListener(this@TaskCpdFragment)
DownloadApkManager.downloadApk(requireActivity(), mFakeTaskList[position].app_url, mFakeTaskList[position].currentPkgName)
}
})
taskCpdDialog?.setData(mFakeTaskList[position].title, mFakeTaskList[position].description, mFakeTaskList[position].icon, needUseTime, mCoin)
taskCpdDialog?.show()
}
}
/**
* 注册试玩任务监听
*/
private fun registerBroad() {
if (mReceiverBroadcastReceiver != null) return
mContext.startService(Intent(context, AppInstallService::class.java))
mReceiverBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null) return
try {
taskDialog?.dismiss()
taskCpdDialog?.dismiss()
val pkg = intent.getStringExtra("pkg")
if (mTaskAdapter == null) return
val position = mTaskAdapter!!.getPosition()
mFakeTaskList[position].currentPkgName = pkg
mFakeTaskList[position].state = 1
mTaskAdapter!!.notifyItemChanged(position)
if (mFakeTaskList[position].isShCpd) {
// 上报珊瑚安装完成
mDownloadProcess?.reportInstallSuccess(false)
} else {
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
// 上报CPD开始安装
LogUtil.d("CPD -> install_start")
taskPresenter.cmtImp(trackerBean.install_start[0])
// 上报CPD安装完成
LogUtil.d("CPD -> install_finish")
taskPresenter.cmtImp(trackerBean.install_finish[0])
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
val intentFilter = IntentFilter()
intentFilter.addAction(AppInstallService.APP_INSTALL_ACTION)
mContext.registerReceiver(this.mReceiverBroadcastReceiver, intentFilter)
}
/**
* 刷新高额任务
*/
private fun refreshDemoTask() {
mTaskAdapter?.let {
val position = it.getPosition()
if (mFakeTaskList.size == 0 || mFakeTaskList[position].state == 3) return
val currentPkgName = mFakeTaskList[position].currentPkgName
if (currentPkgName != null) {
// 时间为0
if (needUseTime == 0) {
mFakeTaskList[position].state = 2
it.notifyItemChanged(position)
if (mFakeTaskList[position].isShCpd) {
// 上报珊瑚激活完成
mDownloadProcess?.reportAppActivated()
} else {
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
// 上报CPD激活完成
LogUtil.d( "CPD -> install_start")
taskPresenter.cmtImp(trackerBean.install_start[0])
LogUtil.d("CPD -> install_finish")
taskPresenter.cmtImp(trackerBean.install_finish[0])
LogUtil.d("CPD -> activation")
taskPresenter.cmtImp(trackerBean.activation[0])
}
}
return
}
if (AppTryPlayManager.getTryPlayIsOK(currentPkgName, needUseTime)) {
// 完成试玩任务
mFakeTaskList[position].state = 2
it.notifyItemChanged(position)
if (mFakeTaskList[position].isShCpd) {
// 上报珊瑚激活完成
mDownloadProcess?.reportAppActivated()
} else {
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
// 上报CPD激活完成
LogUtil.d( "CPD -> activation")
taskPresenter.cmtImp(trackerBean.activation[0])
}
}
} else {
if (AppTryPlayManager.getTryPlayIsOK(currentPkgName, 5)) {
// 试玩时间不足
if (tryTimeOut) {
return
}
tryTimeOut = true
mFakeTaskList[position].state = 4
it.notifyItemChanged(it.getPosition())
val bundle = Bundle()
bundle.putInt(Constant.MAIN_CUR_COIN, 0)
bundle.putString(Constant.MAIN_CARRIER_TYPE, Constant.CARRIER_CPD)
readyGo(AwardActivity::class.java, bundle)
} else {
// 试玩时间不足5秒
mFakeTaskList[position].state = 1
it.notifyItemChanged(it.getPosition())
}
}
}
}
}
private fun loadData() {
taskPresenter.getShCpdTime()
}
private fun backDialog() {
cdaa = CustomDialogAsApple(context, object : DialogListener() {
override fun onClick(v: View) {
if (cdaa.isShowing) {
cdaa.dismiss()
}
when (v.id) {
R.id.dialog_btn_right -> {
// finish()
}
}
}
})
cdaa.setTitle("提示")
cdaa.setContent("退出当前页面将会重置当前任务状态")
cdaa.setLeft("取消")
cdaa.setRight("确定")
cdaa.show()
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.iv_left_icon -> {
backDialog()
}
}
}
/**
* 开始下载
*/
override fun onDownloadStart() {
taskDialog?.dismiss()
taskCpdDialog?.dismiss()
val position = mTaskAdapter!!.getPosition()
if (mFakeTaskList[position].isShCpd) {
// 上报珊瑚开始下载
mDownloadProcess?.reportDownloadStart(false)
} else {
LogUtil.d("CPD -> onDownloadStart")
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
// 上报CPD下载开始
taskPresenter.cmtImp(trackerBean.download_start[0])
}
}
registerBroad()
}
/**
* 下载成功
*/
override fun onDownloadSuccess(path: String) {
val position = mTaskAdapter!!.getPosition()
if (mFakeTaskList[position].isShCpd) {
// 上报珊瑚下载成功
mDownloadProcess?.reportDownloadSuccess(path, false)
} else {
LogUtil.d("CPD -> onDownloadSuccess")
val trackerBean = mFakeTaskList[position].trackerBean
if (trackerBean != null) {
// 上报CPD下载成功
taskPresenter.cmtImp(trackerBean.download_finish[0])
}
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/header_layout" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/srl_task"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_F8F"
app:srlAccentColor="@color/gray"
app:srlPrimaryColor="@color/color_50E8E8E8">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recy_task"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_record_empty"
android:visibility="gone" />
</FrameLayout>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment