admin
2022-05-06 da620c6334f99535e0748555ae75feed368cf8b4
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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.demo.library_flutter.utils.FlutterPageUtil;
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 com.yeshi.makemoney.video.app.utils.ui.JumpPageUtil;
 
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.ll_double).clicked(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( mAquery.id(R.id.fl_double_btn).getView().getVisibility()!=View.VISIBLE){
                    Toast.makeText(mContext, "未加倍金币,会自动入账。", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //加倍领取金币先消失
        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 (!UserUtil.isLogin(mContext)) {
            //去登录
            FlutterPageUtil.jumpPage("LoginPage", null, null);
            return;
        }
 
 
        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();
    }
 
}