|
|
@@ -0,0 +1,301 @@
|
|
|
+package com.sheep.gamegroup.module.task;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.support.v4.content.ContextCompat;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.kfzs.duanduan.cardview.ScreenUtil;
|
|
|
+import com.sheep.gamegroup.absBase.BaseActivity;
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+import com.sheep.gamegroup.model.entity.Release_task;
|
|
|
+import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
|
|
|
+import com.sheep.gamegroup.model.util.SheepSubscriber;
|
|
|
+import com.sheep.gamegroup.module.task.helper.ADVideoHelper;
|
|
|
+import com.sheep.gamegroup.module.task.helper.VideoAd;
|
|
|
+import com.sheep.gamegroup.util.CommonUtil;
|
|
|
+import com.sheep.gamegroup.util.DeviceUtil;
|
|
|
+import com.sheep.gamegroup.util.GlideImageLoader;
|
|
|
+import com.sheep.gamegroup.util.LocationUtils;
|
|
|
+import com.sheep.gamegroup.util.LogUtil;
|
|
|
+import com.sheep.gamegroup.util.UMConfigUtils;
|
|
|
+import com.sheep.gamegroup.util.ViewUtil;
|
|
|
+import com.sheep.gamegroup.view.activity.helper.SplashHelper;
|
|
|
+import com.sheep.gamegroup.view.customview.SheepGSYVideoView;
|
|
|
+import com.sheep.jiuyan.samllsheep.BuildConfig;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
+import com.shuyu.gsyvideoplayer.GSYVideoManager;
|
|
|
+import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
|
|
|
+import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
|
|
|
+import com.umeng.socialize.utils.Log;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+
|
|
|
+public class VideoTaskActivity extends BaseActivity {
|
|
|
+
|
|
|
+ @BindView(R.id.video_player_view)
|
|
|
+ SheepGSYVideoView videoPlayerView;
|
|
|
+
|
|
|
+ private int releaseTaskId = -1;
|
|
|
+
|
|
|
+ private Release_task releaseTask;
|
|
|
+ private TaskAcceptedEty acceptedTask;
|
|
|
+
|
|
|
+ private VideoAd currentVideoAd;
|
|
|
+ private int watchCount = 0;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected int getLayoutId() {
|
|
|
+ return R.layout.activity_video_task;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBackPressed() {
|
|
|
+ super.onBackPressed();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ videoPlayerView.onVideoResume();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPause() {
|
|
|
+ super.onPause();
|
|
|
+ videoPlayerView.onVideoPause();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ if (checkLocationPermission()) {
|
|
|
+ loadReleaseTask();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initView() {
|
|
|
+ hideSystemNavBar();
|
|
|
+ hideSystemStatusBar();
|
|
|
+ getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
+ videoPlayerView.setOnTouchListener(new View.OnTouchListener() {
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View v, MotionEvent event) {
|
|
|
+ if (event.getAction() == MotionEvent.ACTION_UP && currentVideoAd != null) {
|
|
|
+ currentVideoAd.clickX = (int) event.getRawX();
|
|
|
+ currentVideoAd.clickY = (int) event.getRawY();
|
|
|
+ showDownloadConfirmDialog();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initData() {
|
|
|
+ releaseTaskId = getIntent().getIntExtra("task_id", -1);
|
|
|
+ if (checkLocationPermission()) {
|
|
|
+ loadReleaseTask();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadReleaseTask() {
|
|
|
+ CommonUtil.getInstance().getTaskDesc(releaseTaskId, rt -> {
|
|
|
+ if (rt == null) {
|
|
|
+ G.showToast("没有该任务");
|
|
|
+ }
|
|
|
+ releaseTask = rt;
|
|
|
+ if (releaseTask.getAccepted_task_id() > 0) {
|
|
|
+ loadAcceptedTask();
|
|
|
+ } else {
|
|
|
+ LogUtil.logI("---------------没有接受任务---------------");
|
|
|
+ acceptTask();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadAcceptedTask() {
|
|
|
+ CommonUtil.getInstance().getAcceptedTask(releaseTaskId, at -> {
|
|
|
+ if (at == null) {
|
|
|
+ acceptTask();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ acceptedTask = at;
|
|
|
+ ViewUtil.showConfirmDialog(VideoTaskActivity.this,
|
|
|
+ "提示",
|
|
|
+ "你需要观看" + releaseTask.getTask().getVideo_num() + "个视频, 即可获得" + releaseTask.getBonusText() + "元",
|
|
|
+ "放弃", "继续观看",
|
|
|
+ d -> finish(),
|
|
|
+ d -> loadAd()).show();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadAd() {
|
|
|
+ ADVideoHelper.loadVideoAD((ret, ad, msg) -> {
|
|
|
+ if (ret) {
|
|
|
+ currentVideoAd = ad;
|
|
|
+ playVideo();
|
|
|
+ } else {
|
|
|
+ G.showToast(msg);
|
|
|
+ finish();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void playVideo() {
|
|
|
+ GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
|
|
|
+ gsyVideoOption.setIsTouchWiget(false)
|
|
|
+ .setRotateViewAuto(true)
|
|
|
+ .setLockLand(true)
|
|
|
+ .setAutoFullWithSize(true)
|
|
|
+ .setShowFullAnimation(false)
|
|
|
+ .setUrl(currentVideoAd.videoUrl)
|
|
|
+ .setNeedLockFull(true)
|
|
|
+ .setCacheWithPlay(false)
|
|
|
+ .setVideoAllCallBack(new GSYSampleCallBack() {
|
|
|
+ @Override
|
|
|
+ public void onPrepared(String url, Object... objects) {
|
|
|
+ super.onPrepared(url, objects);
|
|
|
+ LogUtil.println("initPlay", "onPrepared", url, objects);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAutoComplete(String url, Object... objects) {
|
|
|
+ super.onAutoComplete(url, objects);
|
|
|
+ watchCount++;
|
|
|
+ if (watchCount >= releaseTask.getTask().getVideo_num()) {
|
|
|
+ commitAutoTask();
|
|
|
+ } else {
|
|
|
+ ViewUtil.showConfirmDialog(VideoTaskActivity.this,
|
|
|
+ "提示",
|
|
|
+ "你需要观看" + (releaseTask.getTask().getVideo_num() - watchCount) + "个视频, 即可获得" + releaseTask.getBonusText() + "元",
|
|
|
+ "放弃", "继续观看",
|
|
|
+ d -> finish(),
|
|
|
+ d -> loadAd()).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPlayError(String url, Object... objects) {
|
|
|
+ super.onPlayError(url, objects);
|
|
|
+ long curPosition = GSYVideoManager.instance().getCurPlayerManager().getCurrentPosition();
|
|
|
+ LogUtil.println("initPlay", "onPlayError", url, curPosition);
|
|
|
+ }
|
|
|
+ }).build(videoPlayerView);
|
|
|
+ videoPlayerView.startPlayLogic();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void acceptTask() {
|
|
|
+ String device_id = DeviceUtil.getDeviceId(SheepApp.getInstance());
|
|
|
+ UMConfigUtils.Event.TASK_ACCEPT.onEvent("device_id", device_id, "release_task_id", releaseTaskId);
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ object.put("device_id", device_id);
|
|
|
+ object.put("release_task_id", releaseTaskId);
|
|
|
+ object.put("create_time", System.currentTimeMillis() / 1000L);
|
|
|
+ object.put("app_version", String.valueOf(BuildConfig.VERSION_CODE));
|
|
|
+ SheepApp.getInstance()
|
|
|
+ .getNetComponent()
|
|
|
+ .getApiService()
|
|
|
+ .acceptedTask(object)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ G.showToast(baseMessage.getErrorMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
+ releaseTask.setIs_running(true);
|
|
|
+ releaseTask.getTask().setRelease_task_id(releaseTask.getId());
|
|
|
+ loadAcceptedTask();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void commitAutoTask() {
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ String gps = LocationUtils.getInstance().getLongitudeLatitude();
|
|
|
+ params.put("gps", gps);
|
|
|
+ params.put("release_task_id", releaseTaskId);
|
|
|
+ params.put("create_time", System.currentTimeMillis() / 1000L);//这里添加时间戳,防止用户抓包后重复使用
|
|
|
+ SheepApp.getInstance().getNetComponent().getApiService().commitAutoTask(params)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new SheepSubscriber<BaseMessage>(getApplicationContext()) {
|
|
|
+ @Override
|
|
|
+ public void onNext(BaseMessage baseMessage) {
|
|
|
+ CommonUtil.getInstance().updateAutoCheck(null);
|
|
|
+ //这里领取了奖励,要刷新用户信息
|
|
|
+ CommonUtil.getInstance().updateUserInfo(null);
|
|
|
+ String amount = releaseTask.getBonusText();
|
|
|
+ try {
|
|
|
+ Map h = JSON.parseObject(baseMessage.getData() + "");
|
|
|
+
|
|
|
+ amount = h.get("amount") + "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ ViewUtil.showMsgDialog(VideoTaskActivity.this, String.format(Locale.CHINA, "奖励已完成,获得奖励%s元", TextUtils.isEmpty(amount) ? releaseTask.getBonusText() : amount), "恭喜你", true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(BaseMessage baseMessage) {
|
|
|
+ G.showToast(baseMessage);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showDownloadConfirmDialog() {
|
|
|
+ if (currentVideoAd == null) return;
|
|
|
+ View rootView = View.inflate(this, R.layout.video_ad_confirm, null);
|
|
|
+ ImageView app_logo_iv = rootView.findViewById(R.id.app_logo_iv);
|
|
|
+ TextView app_name_tv = rootView.findViewById(R.id.app_name_tv);
|
|
|
+ TextView app_desc_tv = rootView.findViewById(R.id.app_desc_tv);
|
|
|
+ int radius = ScreenUtil.dip2px(this, 8);
|
|
|
+ GlideImageLoader.roundedCornersImage(app_logo_iv, currentVideoAd.videoUrl, radius);
|
|
|
+ app_name_tv.setText(currentVideoAd.title);
|
|
|
+ app_desc_tv.setText(currentVideoAd.desc);
|
|
|
+ ViewUtil.showConfirmDialog(this, "下载", rootView, "取消", "立即下载",
|
|
|
+ d -> d.dismiss(),
|
|
|
+ d -> Log.e("adf", "download")).show();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkLocationPermission() {
|
|
|
+ List<String> permissions = new ArrayList<String>();
|
|
|
+ boolean result = true;
|
|
|
+ if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
|
|
|
+ result = false;
|
|
|
+ }
|
|
|
+ if (permissions.size() > 0) {
|
|
|
+ String[] permissionArrays = new String[permissions.size()];
|
|
|
+ permissionArrays = permissions.toArray(permissionArrays);
|
|
|
+ ActivityCompat.requestPermissions(this,
|
|
|
+ permissionArrays,
|
|
|
+ 1111);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|