package com.yeshi.makemoney.video.app.utils.videos;
|
|
import android.animation.ValueAnimator;
|
import android.app.Activity;
|
import android.app.Dialog;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.graphics.drawable.AnimationDrawable;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.view.animation.AccelerateDecelerateInterpolator;
|
import android.view.animation.Animation;
|
import android.widget.Toast;
|
|
import com.androidquery.AQuery;
|
import com.demo.lib.common.dialog.DialogUtil;
|
import com.demo.lib.common.util.common.DimenUtils;
|
import com.demo.lib.common.util.ui.LoadingDialogUtil;
|
import com.demo.library_ad.AdUtil;
|
import com.demo.library_ad.RewardAdUtil;
|
import com.yeshi.makemoney.video.R;
|
import com.yeshi.makemoney.video.app.entity.goldcorn.GoldCornTaskInfo;
|
import com.yeshi.makemoney.video.app.ui.dialog.GoldCornDoubleDialog;
|
import com.yeshi.makemoney.video.app.ui.subview.CircleProgressView;
|
import com.yeshi.makemoney.video.app.utils.AppConfigUtil;
|
import com.yeshi.makemoney.video.app.utils.UserUtil;
|
import com.yeshi.makemoney.video.app.utils.api.BasicTextHttpResponseHandler;
|
import com.yeshi.makemoney.video.app.utils.api.HttpApiUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
public class DrawVideoHBManager {
|
|
private final String TAG = "DrawVideoHBManager";
|
|
private final int MAX_CIRCLE = 6;
|
|
private AQuery mAquery;
|
private Context mContext;
|
private CircleProgressView progressView;
|
private Activity mActivity;
|
private Dialog mDialog;
|
|
//加倍按钮倒计时
|
Timer doubleTimer;
|
int doubleTime = 0;
|
|
private boolean reward = false;
|
|
|
/**
|
* 初始化
|
*
|
* @param view
|
*/
|
public void init(View view, Activity activity) {
|
|
mAquery = new AQuery(view);
|
mContext = view.getContext();
|
mActivity = activity;
|
progressView = (CircleProgressView) mAquery.id(R.id.cb_process).getView();
|
|
mAquery.id(R.id.fl_hb).clicked(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
refreshTaskInfo();
|
}
|
});
|
|
//加倍领取金币先消失
|
mAquery.id(R.id.fl_double_btn).visibility(View.GONE);
|
mAquery.id(R.id.fl_double_btn).clicked(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (mDialog != null) {
|
DialogUtil.dismiss(mDialog);
|
}
|
|
if (VideoGoldCornUtil.isDoubleNotify(mContext)) {
|
startDoubleTask();
|
return;
|
}
|
|
|
//TODO 弹框
|
mDialog = new GoldCornDoubleDialog.Builder(mActivity).setGoldCorn(VideoGoldCornUtil.getVideoPlayCircle(mContext) * VideoGoldCornUtil.getPrice(mContext)).setCallBack(new GoldCornDoubleDialog.GoldCornDoubleCallBack() {
|
@Override
|
public void onPositive(boolean notify) {
|
startDoubleTask();
|
}
|
|
@Override
|
public void onCancel() {
|
DialogUtil.dismiss(mDialog);
|
}
|
}).create();
|
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
|
@Override
|
public void onShow(DialogInterface dialog) {
|
//暂停计费
|
DrawVideoGoldCornManager.getInstance().pause();
|
}
|
});
|
|
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
@Override
|
public void onDismiss(DialogInterface dialog) {
|
//恢复计费
|
DrawVideoGoldCornManager.getInstance().continu();
|
}
|
});
|
|
|
DialogUtil.show(mDialog);
|
}
|
});
|
|
//初始化总的金币数量
|
if (VideoGoldCornUtil.getPrice(mContext) != null) {
|
setTotalGoldCornText(0, VideoGoldCornUtil.getVideoPlayCircle(mContext));
|
}
|
initAnimation();
|
}
|
|
/**
|
* 进度回调
|
*
|
* @param process
|
*/
|
public void onProcess(int process) {
|
|
mActivity.runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
progressView.setProgress(process);
|
|
//达到100%
|
if (process == 1000) {
|
int oldCircle = VideoGoldCornUtil.getVideoPlayCircle(mContext);
|
//增加圈数
|
VideoGoldCornUtil.addVideoPlayCircle(mContext, 1);
|
//更新总的金币数显示
|
mAquery.id(R.id.tv_total_goldcorn).getView().postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
setTotalGoldCornText(oldCircle, VideoGoldCornUtil.getVideoPlayCircle(mContext));
|
}
|
}, 1000);
|
|
|
//进度条消失,然后播放动画
|
mAquery.id(R.id.fl_hb).getView().postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
mAquery.id(R.id.cb_process).visibility(View.INVISIBLE);
|
hbHide();
|
}
|
}, 500);
|
}
|
}
|
});
|
|
}
|
|
/**
|
* 刷新任务信息
|
*/
|
public void refreshTaskInfo() {
|
//获取任务信息
|
DrawVideoGoldCornManager.getInstance().getTaskInfo(mContext, new DrawVideoGoldCornManager.ITaskInfoResult() {
|
@Override
|
public void onResult(GoldCornTaskInfo info) {
|
if (info != null) {
|
if (info.isFinish()) {
|
//任务完成后要隐藏覆盖物
|
mAquery.id(R.id.fl_cover).visibility(View.GONE);
|
} else {
|
mAquery.id(R.id.fl_cover).visibility(View.VISIBLE);
|
}
|
|
}
|
}
|
});
|
}
|
|
|
/**
|
* 销毁数据
|
*/
|
public void destory() {
|
if (doubleTimer != null) {
|
doubleTimer.cancel();
|
doubleTimer = null;
|
}
|
//todo 移除callback
|
// mAquery.id(R.id.tv_total_goldcorn).getView().removeCallbacks();
|
}
|
|
/**
|
* 开始加倍任务
|
*/
|
private void startDoubleTask() {
|
if (reward) {
|
uploadData(true);
|
return;
|
}
|
|
LoadingDialogUtil.show(mActivity);
|
AdUtil.AD_TYPE type = AdUtil.getAdType(mContext, "rewardAd");
|
if (type == null) {
|
Toast.makeText(mContext, "广告配置文件加载失败,请稍后再试", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
String pid = null;
|
if (type == AdUtil.AD_TYPE.csj) {
|
pid = mContext.getResources().getString(R.string.ad_csj_pid_reward);
|
} else {
|
pid = mContext.getResources().getString(R.string.ad_gdt_pid_reward);
|
}
|
|
|
RewardAdUtil.loadAD(type, pid, mActivity, new RewardAdUtil.RewardAdListener() {
|
@Override
|
public void onADLoad() {
|
Log.i(TAG, "onADLoad");
|
}
|
|
@Override
|
public void onVideoCached() {
|
Log.i(TAG, "onVideoCached");
|
}
|
|
@Override
|
public void onADShow() {
|
Log.i(TAG, "onADShow");
|
mActivity.runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
LoadingDialogUtil.dismiss();
|
}
|
});
|
|
}
|
|
@Override
|
public void onAdClose() {
|
Log.i(TAG, "onAdClose");
|
if (reward) {
|
uploadData(true);
|
}
|
}
|
|
@Override
|
public void onReward() {
|
Log.i(TAG, "onReward");
|
reward = true;
|
}
|
|
@Override
|
public void onError(String msg) {
|
Log.i(TAG, "onError:" + msg);
|
mActivity.runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
LoadingDialogUtil.dismiss();
|
}
|
});
|
}
|
});
|
}
|
|
/**
|
* 上传数据
|
*
|
* @param isDouble
|
*/
|
boolean isUploading = false;
|
|
/**
|
* 上报数据
|
*
|
* @param isDouble
|
*/
|
private void uploadData(boolean isDouble) {
|
//如果没有登录就返回
|
if (!UserUtil.isLogin(mContext)) {
|
return;
|
}
|
|
|
if (isUploading) {
|
return;
|
}
|
isUploading = true;
|
|
int circle = VideoGoldCornUtil.getVideoPlayCircle(mContext);
|
HttpApiUtil.watchDrawVideo(mContext, circle * DrawVideoGoldCornManager.CIRCLE_TIME / 1000, isDouble, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (isDouble) {
|
LoadingDialogUtil.show(mActivity);
|
}
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (jsonObject.optInt("code") == 0) {
|
int goldCorn = jsonObject.optJSONObject("data").optInt("goldCorn");
|
int leftCount = jsonObject.optJSONObject("data").optInt("leftCount");
|
if (leftCount == 0) {
|
refreshTaskInfo();
|
}
|
|
uploadSuccess(goldCorn, circle);
|
} else {
|
//TODO 业务错误
|
Toast.makeText(mContext, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
|
}
|
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
isUploading = false;
|
LoadingDialogUtil.dismiss();
|
}
|
});
|
}
|
|
//上传成功
|
private synchronized void uploadSuccess(int goldCorn, int oldCircle) {
|
//激励视频观看设置为未观看
|
reward = false;
|
VideoGoldCornUtil.showInComeToast(goldCorn, mContext);
|
int leftCircle = VideoGoldCornUtil.getVideoPlayCircle(mContext) - oldCircle;
|
if (leftCircle < MAX_CIRCLE) {
|
//隐藏倒计时按钮
|
mAquery.id(R.id.fl_double_btn).visibility(View.GONE);
|
}
|
resetDubleTimer();
|
VideoGoldCornUtil.setVideoPlayCircle(leftCircle, mContext);
|
setTotalGoldCornText(oldCircle, leftCircle);
|
//弹框消失
|
if (mDialog != null) {
|
DialogUtil.dismiss(mDialog);
|
}
|
}
|
|
|
private void resetDubleTimer() {
|
//加倍按钮倒计时设置为空
|
doubleTime = 0;
|
mAquery.id(R.id.fl_double_btn).clickable(true);
|
if (doubleTimer != null) {
|
doubleTimer.cancel();
|
doubleTimer = null;
|
}
|
}
|
|
|
/**
|
* 隐藏红包图像显示
|
*/
|
private void hbHide() {
|
mAquery.id(R.id.iv_hb).animate(R.anim.anim_hb_hidden, new Animation.AnimationListener() {
|
|
@Override
|
public void onAnimationStart(Animation animation) {
|
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
mAquery.id(R.id.iv_hb).visibility(View.GONE);
|
|
mAquery.id(R.id.fl_goldcorn).visibility(View.VISIBLE);
|
mAquery.id(R.id.fl_goldcorn).animate(R.anim.anim_goldcorn_show, new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
//开始掉金币
|
startGoldCornDown();
|
|
mAquery.id(R.id.tv_goldcorn).getView().postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
//开始展示钱
|
mAquery.id(R.id.tv_goldcorn).text(VideoGoldCornUtil.getPrice(mContext) + "");
|
mAquery.id(R.id.tv_goldcorn).visibility(View.VISIBLE);
|
mAquery.id(R.id.tv_goldcorn).animate(R.anim.anim_goldcorn_show);
|
mAquery.id(R.id.iv_goldcorn_bg).visibility(View.VISIBLE);
|
AnimationDrawable animationDrawable = (AnimationDrawable) mAquery.id(R.id.iv_goldcorn_bg).getImageView().getDrawable();
|
animationDrawable.start();
|
}
|
}, 500);
|
|
|
//2秒后从新开始循环
|
mAquery.id(R.id.fl_goldcorn).getView().postDelayed(new Runnable() {
|
@Override
|
public void run() {
|
initAnimation();
|
DrawVideoGoldCornManager.getInstance().restart();
|
}
|
}, 2000);
|
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
|
}
|
});
|
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
|
}
|
});
|
|
}
|
|
|
/**
|
* 设置总的金币数量
|
*
|
* @param fromCircle
|
* @param toCircle
|
*/
|
private void setTotalGoldCornText(int fromCircle, int toCircle) {
|
int price = VideoGoldCornUtil.getPrice(mContext);
|
ValueAnimator valueAnimator = new ValueAnimator().ofInt(price * fromCircle, price * toCircle);
|
valueAnimator.setDuration(500);
|
//监听值的变化
|
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
@Override
|
public void onAnimationUpdate(ValueAnimator animation) {
|
int currentV = (Integer) animation.getAnimatedValue();
|
mAquery.id(R.id.tv_total_goldcorn).text("+" + currentV);
|
}
|
});
|
valueAnimator.start();
|
//是否需要展示加倍按钮
|
if (VideoGoldCornUtil.getVideoPlayCircle(mContext) >= MAX_CIRCLE && doubleTimer == null) {
|
mAquery.id(R.id.fl_double_btn).visibility(View.VISIBLE);
|
doubleTimer = new Timer();
|
doubleTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
|
mActivity.runOnUiThread(new Runnable() {
|
@Override
|
public void run() {
|
if (DrawVideoGoldCornManager.getInstance().isCounting()) {
|
mAquery.id(R.id.pb_hide).getProgressBar().setProgress(doubleTime);
|
doubleTime++;
|
if (doubleTime > 120) {
|
mAquery.id(R.id.fl_double_btn).clickable(false);
|
//超过2分钟自动上报
|
uploadData(false);
|
}
|
}
|
}
|
});
|
}
|
}, 0, 1000);
|
}
|
}
|
|
|
/**
|
* 将动画初始化到最初的状态
|
*/
|
private void initAnimation() {
|
for (int i = leftDPs.length - 1; i >= 0; i--) {
|
int resId = mContext.getResources().getIdentifier("iv_goldcorn_" + (i + 1), "id", mContext.getPackageName());
|
mAquery.id(resId).margin(leftDPs[i], -20, 0, 0);
|
}
|
if (mAquery.id(R.id.fl_goldcorn).getView().getVisibility() == View.VISIBLE) {
|
mAquery.id(R.id.fl_goldcorn).animate(R.anim.anim_goldcorn_hidden, new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
mAquery.id(R.id.fl_goldcorn).visibility(View.GONE);
|
mAquery.id(R.id.cb_process).visibility(View.VISIBLE);
|
mAquery.id(R.id.iv_hb).animate(R.anim.anim_hb_show, new Animation.AnimationListener() {
|
@Override
|
public void onAnimationStart(Animation animation) {
|
|
}
|
|
@Override
|
public void onAnimationEnd(Animation animation) {
|
mAquery.id(R.id.iv_hb).visibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
|
}
|
});
|
|
}
|
|
@Override
|
public void onAnimationRepeat(Animation animation) {
|
|
}
|
});
|
}
|
|
mAquery.id(R.id.tv_goldcorn).visibility(View.GONE);
|
progressView.setProgress(0);
|
}
|
|
final float[] leftDPs = new float[]{-5, +5, 0, -3, +3};
|
final float[] topDPs = new float[]{32 - 10, 32 - 10, 30 - 10, 40 - 10, 40 - 10};
|
|
/**
|
* 开始金币掉落动画
|
*/
|
private void startGoldCornDown() {
|
|
for (int i = leftDPs.length - 1; i >= 0; i--) {
|
startGoldCornDown("iv_goldcorn_" + (i + 1), leftDPs[i], topDPs[i]);
|
}
|
}
|
|
private void startGoldCornDown(String id, float leftDP, float topDP) {
|
int resId = mContext.getResources().getIdentifier(id, "id", mContext.getPackageName());
|
mAquery.id(resId).margin(leftDP, -20, 0, 0);
|
ValueAnimator anim = ValueAnimator.ofFloat(-20, topDP);
|
anim.setDuration(300);
|
anim.setRepeatCount(0);
|
anim.setStartDelay((int) (Math.random() * 300));
|
//先加速再减速
|
anim.setInterpolator(new AccelerateDecelerateInterpolator());
|
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
@Override
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
float currentValue = (float) animation.getAnimatedValue();
|
// 获得改变后的值
|
mAquery.id(resId).margin(leftDP, currentValue, 0, 0);
|
}
|
});
|
anim.start();
|
}
|
|
}
|