package com.yeshi.makemoney.video.app.utils.videos;
|
|
import android.content.Context;
|
import android.widget.Toast;
|
|
import com.google.gson.Gson;
|
import com.yeshi.makemoney.video.app.entity.goldcorn.GoldCornTaskInfo;
|
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.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
public class DrawVideoGoldCornManager {
|
public final static int SPACE = 40;
|
|
//一圈10s
|
final static int CIRCLE_TIME = 10 * 1000;
|
|
//播放时间
|
Long playTime = 0L;
|
//视频播放状态
|
boolean videoPlaying = false;
|
//手动暂停
|
boolean paused = false;
|
|
private boolean finish = true;
|
|
|
private static DrawVideoGoldCornManager instance;
|
private Timer timer;
|
|
public static DrawVideoGoldCornManager getInstance() {
|
if (instance == null) {
|
instance = new DrawVideoGoldCornManager();
|
}
|
return instance;
|
}
|
|
/**
|
* 初始化实例
|
*/
|
public void init(final PlayTimerCallBack callBack) {
|
playTime = 0L;
|
timer = new Timer();
|
timer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (finish) {
|
return;
|
}
|
|
if (playTime == null || paused) {
|
return;
|
}
|
if (videoPlaying) {
|
playTime += SPACE;
|
if (playTime % CIRCLE_TIME == 0L) {
|
//转满一圈后先暂停,然后回调
|
stop();
|
if (callBack != null) {
|
callBack.onProcess(1000);
|
}
|
} else {
|
int process = new BigDecimal(playTime).divide(new BigDecimal(CIRCLE_TIME), 3, RoundingMode.FLOOR).multiply(new BigDecimal(1000)).intValue();
|
callBack.onProcess(process);
|
}
|
}
|
}
|
}, SPACE, SPACE);
|
}
|
|
|
public void videoPlay() {
|
videoPlaying = true;
|
}
|
|
|
public void videoPause() {
|
videoPlaying = false;
|
}
|
|
/**
|
* 暂停计时
|
*/
|
public void pause() {
|
paused = true;
|
}
|
|
public void stop() {
|
playTime = null;
|
}
|
|
/**
|
* 重新开始计时
|
*/
|
public void restart() {
|
playTime = 0L;
|
}
|
|
|
public void continu() {
|
paused = false;
|
}
|
|
|
/**
|
* 销毁实例
|
*/
|
public void destory() {
|
if (timer != null) {
|
playTime = null;
|
videoPlaying = false;
|
timer.cancel();
|
timer = null;
|
}
|
}
|
|
|
public boolean isFinish() {
|
return finish;
|
}
|
|
/**
|
* 获取播放时间
|
*
|
* @return
|
*/
|
public long getPlayTime() {
|
return playTime;
|
}
|
|
public interface PlayTimerCallBack {
|
|
/**
|
* 播放进度百分比
|
*
|
* @param process
|
*/
|
public void onProcess(int process);
|
|
|
}
|
|
/**
|
* 是否正在计数
|
*
|
* @return
|
*/
|
public boolean isCounting() {
|
|
return playTime != null && !paused && videoPlaying;
|
}
|
|
|
public void getTaskInfo(Context context, final ITaskInfoResult result) {
|
HttpApiUtil.getTaskInfo(context, "watchVideo", new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (jsonObject == null || jsonObject.optInt("code") != 0) {
|
result.onResult(null);
|
return;
|
}
|
|
JSONObject data = jsonObject.optJSONObject("data");
|
|
GoldCornTaskInfo info = new Gson().fromJson(data.toString(), GoldCornTaskInfo.class);
|
finish = info.isFinish();
|
VideoGoldCornUtil.setPrice(context, info.getPrice());
|
result.onResult(info);
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
result.onResult(null);
|
Toast.makeText(context, "网络请求出错", Toast.LENGTH_SHORT).show();
|
}
|
});
|
}
|
|
public interface ITaskInfoResult {
|
|
public void onResult(GoldCornTaskInfo info);
|
|
}
|
|
|
}
|