admin
2022-04-29 67bdeebb4dc381a2f46f31e3027ebcc3243a8aeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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);
 
    }
 
 
}