Sfoglia il codice sorgente

优化下载游戏与预约游戏状态

zengjiebin 8 anni fa
parent
commit
55931c8dcb

+ 161 - 6
app/src/main/java/com/kfzs/duanduan/fragment/FgtFindChild.java

@@ -1,6 +1,7 @@
 package com.kfzs.duanduan.fragment;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v4.widget.SwipeRefreshLayout;
@@ -14,11 +15,16 @@ import android.widget.ImageView;
 import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
 import com.kfzs.android.view.tag.FlowLayout;
 import com.kfzs.android.view.tag.TagAdapter;
 import com.kfzs.android.view.tag.TagFlowLayout;
 import com.kfzs.duanduan.BaseCompatFragment;
+import com.kfzs.duanduan.services.DownloadTaskService;
 import com.sheep.gamegroup.model.entity.BaseMessage;
+import com.sheep.gamegroup.model.entity.FindApp;
 import com.sheep.gamegroup.model.entity.FindItem;
 import com.sheep.gamegroup.model.entity.FindTag;
 import com.sheep.gamegroup.model.entity.HomeListEntity;
@@ -34,9 +40,15 @@ import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import butterknife.BindView;
 import butterknife.ButterKnife;
@@ -44,6 +56,9 @@ import rx.android.schedulers.AndroidSchedulers;
 import rx.functions.Action1;
 import rx.schedulers.Schedulers;
 
+import static android.content.Intent.ACTION_PACKAGE_ADDED;
+import static android.content.Intent.ACTION_PACKAGE_REMOVED;
+
 /**
  * Created by realicing on 2018/6/27.
  * realicing@sina.com
@@ -66,6 +81,8 @@ public class FgtFindChild extends BaseCompatFragment {
         if(bundle != null){
             type = bundle.getInt("type", 0);
         }
+        EventBus.getDefault().register(this);
+        Aria.download(this).register();
     }
 
     private Activity activity;
@@ -130,12 +147,8 @@ public class FgtFindChild extends BaseCompatFragment {
                 if(item.isGame()){
                     find_item_download.setVisibility(View.VISIBLE);
                     find_item_pf_label.setText(Html.fromHtml(String.format(Locale.CHINA, "评分:<font color='%s'>%.1f</font>", "#29d6fd", item.getScore())));
-                    find_item_download.setOnClickListener(new View.OnClickListener() {
-                        @Override
-                        public void onClick(View view) {
-                            G.showToast("您点击了下载游戏");
-                        }
-                    });
+                    FindApp findApp = item.getApplication();
+                    updateView(activity, findApp, find_item_download);
                 } else {
                     find_item_download.setVisibility(View.GONE);
                     find_item_pf_label.setVisibility(View.GONE);
@@ -288,4 +301,146 @@ public class FgtFindChild extends BaseCompatFragment {
         view_list.getAdapter().notifyDataSetChanged();
     }
     private List<FindItem> list = ListUtil.emptyList();
+
+
+
+    private Map<String, FindApp> findAppMap = new HashMap<>();
+    //获取任务对应的findApp
+    private FindApp getFindAppByKey(String key) {
+        if(findAppMap.containsKey(key))
+            return findAppMap.get(key);
+        return null;
+    }
+    private Map<String, TextView> downLoadTextViewMap = new HashMap<>();
+    //获取任务对应的TextView来更新进度
+    private TextView getTextViewByTask(DownloadTask task) {
+        return getTextViewByKey(task.getKey());
+    }
+    private TextView getTextViewByKey(String key) {
+        if(downLoadTextViewMap.containsKey(key))
+            return downLoadTextViewMap.get(key);
+        return null;
+    }
+
+    //更新按钮状态与添加点击事件
+    private void updateView(Activity activity, FindApp findApp, TextView textView) {
+        if(findApp.isCanDonload()){//可下载
+            downLoadTextViewMap.put(findApp.getDownload_link(), textView);
+            downLoadTextViewMap.put(findApp.getPackage_name(), textView);
+            findAppMap.put(findApp.getDownload_link(),findApp);
+            findAppMap.put(findApp.getPackage_name(), findApp);
+            findApp.getFindAppHelper().updateDownloadTaskView(activity, findApp, textView);
+        } else {//预约下载
+            findApp.getFindAppHelper().updateReservationView(activity, findApp, textView);
+        }
+    }
+    //下载状态监听
+
+    @Download.onPre void onPre(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("准备下载中");
+        }
+    }
+
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("开始下载中");
+        }
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("恢复下载中");
+        }
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(String.format(Locale.CHINA, "%d%%", Math.abs(task.getPercent())));
+        }
+    }
+
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.CONTINUE_DOWNLOAD);
+        }
+    }
+
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
+
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("已经取消");
+        }
+    }
+
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.FAIL_DOWNLOAD);
+        }
+    }
+
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.START_INSTALL);
+        }
+        FindApp findApp = getFindAppByKey(task.getKey());
+        if(findApp != null) {
+            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+        }
+    }
+
+    @Subscribe
+    public void onEventMainThread(Intent intent) {
+        if(intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")){
+            String packageName = intent.getDataString().replace("package:","");
+
+            TextView textView = getTextViewByKey(packageName);
+            if(textView != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        textView.setText(CommonUtil.GAME_OEPN);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            textView.setText(CommonUtil.START_DOWNLOAD);
+                        } else {
+                            textView.setText(CommonUtil.START_INSTALL);
+                        }
+                        break;
+                }
+            }
+
+            FindApp findApp = getFindAppByKey(packageName);
+            if(findApp != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        findApp.updateState(DownloadTaskService.STATUS_INSTALLED);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            findApp.updateState(DownloadTaskService.STATUS_INIT);
+                        } else {
+                            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+                        }
+                        break;
+                }
+            }
+        }
+    }
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().unregister(this);
+    }
 }

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/model/api/ApiService.java

@@ -633,7 +633,7 @@ public interface ApiService {
      * 应用 下载/预约 时先调用此接口生成记录
      */
     @GET("app/find/applications/downloads")
-    Observable<BaseMessage> getDownloads(@Query("id") int id);
+    Observable<BaseMessage> recordAppDownloads(@Query("id") int id);
     /**
      * 取消  应用 下载/预约
      */

+ 35 - 1
app/src/main/java/com/sheep/gamegroup/model/entity/FindApp.java

@@ -1,6 +1,7 @@
 package com.sheep.gamegroup.model.entity;
 
 import com.sheep.gamegroup.model.api.ITask;
+import com.sheep.gamegroup.util.FindAppHelper;
 
 /**
  * Created by realicing on 2018/6/28.
@@ -47,6 +48,8 @@ public class FindApp implements ITask{
 
     private int download;//1:可以下载
 
+    private int record;//1:可记录 其他:不可记录
+
     public void setUpdated_at(int updated_at){
         this.updated_at = updated_at;
     }
@@ -170,6 +173,15 @@ public class FindApp implements ITask{
         this.download = download;
     }
 
+    public int getRecord() {
+        return record;
+    }
+
+    public void setRecord(int record) {
+        this.record = record;
+    }
+
+
 
 
 
@@ -187,7 +199,7 @@ public class FindApp implements ITask{
 
     @Override
     public int getTask_type() {
-        return 3;
+        return 0;
     }
 
     @Override
@@ -227,4 +239,26 @@ public class FindApp implements ITask{
     public boolean isCanDonload(){
         return download == 1;//可以下载
     }
+
+    /**
+     * 是否可以记录
+     * @return
+     */
+    public boolean isCanRecord() {
+        return record == 1;//1:可记录 其他:不可记录
+    }
+
+    private FindAppHelper findAppHelper = new FindAppHelper();
+
+    public FindAppHelper getFindAppHelper() {
+        return findAppHelper;
+    }
+
+    /**
+     * 更新下载状态
+     * @param status
+     */
+    public void updateState(int status) {
+        findAppHelper.updateState(status);
+    }
 }

+ 17 - 1
app/src/main/java/com/sheep/gamegroup/model/entity/FindItem.java

@@ -52,6 +52,8 @@ public class FindItem {
 
     private int type;//类型 1:游戏 2:咨询 3:转跳 4:任务 5:栏目
 
+    private FindApp application;
+
     public void setUpdated_at(int updated_at){
         this.updated_at = updated_at;
     }
@@ -169,11 +171,21 @@ public class FindItem {
     public void setScore(float score) {
         this.score = score;
     }
-
     public float getScore() {
         return score;
     }
 
+    public FindApp getApplication() {
+        return application;
+    }
+
+    public void setApplication(FindApp application) {
+        this.application = application;
+    }
+
+
+
+
 
 
     //是否是游戏,游戏要显示下载游戏与评分
@@ -181,4 +193,8 @@ public class FindItem {
         return type == 1;//类型 1:游戏 2:咨询 3:转跳 4:任务 5:栏目
     }
 
+
+
+
+
 }

+ 1 - 0
app/src/main/java/com/sheep/gamegroup/util/CommonUtil.java

@@ -85,6 +85,7 @@ public class CommonUtil {
     public static String GAME_RECHAGE = "充值游戏";
     public static String GAME_DOWNLOAD = "下载游戏";
     public static String GAME_INSTALL = "安装游戏";
+    public static String GAME_OEPN = "启动游戏";
     private static CommonUtil commonUtil;
 
     public static CommonUtil getInstance(){

+ 131 - 44
app/src/main/java/com/sheep/gamegroup/util/FindAppHelper.java

@@ -1,19 +1,30 @@
 package com.sheep.gamegroup.util;
 
 import android.app.Activity;
+import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.View;
 import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTarget;
+import com.arialyy.aria.core.inf.IEntity;
+import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
+import com.kfzs.duanduan.event.BigEvent;
+import com.kfzs.duanduan.event.EventTypes;
 import com.kfzs.duanduan.services.DownloadTaskService;
+import com.kfzs.duanduan.utils.ApkUtils;
+import com.sheep.gamegroup.model.api.ITask;
 import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.FindApp;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.view.activity.TaskDetailAct;
+import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 
 import java.util.Locale;
 
@@ -26,79 +37,155 @@ import rx.schedulers.Schedulers;
  * realicing@sina.com
  */
 public class FindAppHelper {
-    private boolean isCommitAppDownloadsSuccess = false;//是否提交预约
-    private Activity activity;
-    private FindApp findApp;
-    private TextView find_game_down_tv;
-    public void updateView(Activity activity, final FindApp findApp, final TextView find_game_down_tv) {
-        this.activity = activity;
-        this.findApp = findApp;
-        this.find_game_down_tv = find_game_down_tv;
 
-        if(findApp.isCanDonload()){//可下载
-            if(mDownloadTaskService == null){
-                mDownloadTaskService = new DownloadTaskService(activity);
+    private int downLoadType;
+    /**
+     * 更新游戏下载与响应点击
+     * @param activity
+     * @param findApp
+     * @param down_tv
+     */
+    public void updateDownloadTaskView(final Activity activity, final FindApp findApp, final TextView down_tv) {
+        if(mDownloadTaskService == null){
+            mDownloadTaskService = new DownloadTaskService(activity);
+        }
+        TaskDetailAct.setBtnStr(findApp, mDownloadTaskService, new Action1<Object>() {
+            @Override
+            public void call(Object o) {
+                if(o instanceof Integer){
+                    downLoadType = (int) o;
+                } else if( o instanceof String){
+                    down_tv.setText((String) o);
+                } else if( o instanceof Boolean){
+                    down_tv.setEnabled((Boolean) o);
+                }
             }
-            TaskDetailAct.setBtnStr(findApp, mDownloadTaskService, new Action1<Object>() {
-                @Override
-                public void call(Object o) {
-//                    find_game_down_tv.setText("立即下载");
+        });
+        down_tv.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                if(findApp.isCanRecord()){//可以记录
+                    recordAppDownloads(activity, findApp, down_tv);
                 }
-            });
-            find_game_down_tv.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View view) {
-                    commitAppDownloads(false, findApp);
-                    downloadApp(findApp);
+                downloadApp(activity, findApp);
+            }
+        });
+    }
+
+    private DownloadTaskService mDownloadTaskService;
+    private DownloadTarget downloadTarget;
+    //下载应用
+    private void downloadApp(Activity activity, ITask task) {
+        switch (downLoadType){
+            case DownloadTaskService.STATUS_INIT://可以下载
+                if(TextUtils.isEmpty(task.getDownload_link()) || !task.getDownload_link().contains("http")){
+                    G.showToast(R.string.error_download_link);
+                    return;
                 }
-            });
-        } else {//预约下载
-            find_game_down_tv.setText("预约下载");
-            find_game_down_tv.setOnClickListener(new View.OnClickListener() {
+                this.downloadTarget = Aria.download(activity).load(task.getDownload_link());
+                final DownLoadInfo downLoadInfo = mDownloadTaskService.getDownloadTaskByUrl(task);
+                if(this.downloadTarget != null) {
+                    if (this.downloadTarget.getTaskState() == IEntity.STATE_RUNNING) {
+                        this.downloadTarget.stop();
+                    } else if (this.downloadTarget.getTaskState() == IEntity.STATE_FAIL) {
+                        this.downloadTarget.cancel(true);
+                        this.downloadTarget.removeRecord();
+                    } else {
+                        mDownloadTaskService.start(downLoadInfo);
+                    }
+                }
+                break;
+            case DownloadTaskService.STATUS_FINISH://已经下载完成,可以安装
+                String path = PackageUtil.isExistsFile(task.getPackage_names());
+                if(TextUtils.isEmpty(path)){//数据库保存有下载完成的状态,但是应用已经被删除,需要修复其下载状态
+                    if(TextUtils.isEmpty(task.getDownload_link()) || !task.getDownload_link().contains("http")){
+                        G.showToast(R.string.error_download_link);
+                        return;
+                    }
+                    this.downloadTarget = Aria.download(activity).load(task.getDownload_link());
+                    if(this.downloadTarget != null) {
+                        this.downloadTarget.cancel(true);
+                        this.downloadTarget.removeRecord();
+                        mDownloadTaskService.start(mDownloadTaskService.getDownloadTaskByUrl(task));
+                        mDownloadTaskService.setDownloadTaskStatus(task.getDownload_link(), DownloadTaskService.STATUS_INIT);
+                        downLoadType = DownloadTaskService.STATUS_INIT;
+                    }
+                } else {
+                    ApkUtils.installApk(activity, PackageUtil.isExistsFile(task.getPackage_names()));
+                }
+                break;
+            case DownloadTaskService.STATUS_INSTALLED://已经安装,可以试玩
+                PackageUtil.startApp(activity, task.getPackage_names());
+                break;
+        }
+    }
+
+
+    /**
+     * 更新预约下载状态与响应点击
+     * @param activity
+     * @param findApp
+     * @param reservation_tv
+     */
+    public void updateReservationView(final Activity activity, final FindApp findApp, final TextView reservation_tv) {
+        if(findApp.isCanRecord()) {
+            reservation_tv.setEnabled(true);
+            reservation_tv.setText("预约下载");
+            reservation_tv.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
-                    if(isCommitAppDownloadsSuccess){//已经提交成功
-                    } else {//没有提交过或者提交失败的
-                        find_game_down_tv.setEnabled(false);
-                        commitAppDownloads(true, findApp);
-                    }
+                    reservation_tv.setEnabled(false);
+                    recordAppDownloads(activity, findApp, reservation_tv);
                 }
             });
+        } else {
+            reservation_tv.setEnabled(false);
+            reservation_tv.setText("已经预约");
         }
     }
-    private DownloadTaskService mDownloadTaskService;
-    //下载应用
-    private void downloadApp(FindApp findApp) {
-    }
 
-    //点击预约下载与立即下载时,需要调用这个接口记录到服务器
-    private void commitAppDownloads(final boolean isNeedReservation, final FindApp findApp) {
-        SheepApp.getInstance().getNetComponent().getApiService().getDownloads(findApp.getId())
+
+    /**
+     * 点击预约下载与立即下载时,需要调用这个接口记录到服务器
+     * @param findApp
+     */
+    private void recordAppDownloads(final Activity activity, final FindApp findApp, final TextView textView) {
+        SheepApp.getInstance().getNetComponent().getApiService().recordAppDownloads(findApp.getId())
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                     @Override
                     public void onNext(BaseMessage baseMessage) {
                         LogUtil.println("baseMessage onNext "+ JSON.toJSONString(baseMessage));
-                        if(isNeedReservation){//提交预约成功
-                            find_game_down_tv.setText("预约成功");
-                            find_game_down_tv.setEnabled(false);
+                        findApp.setRecord(2);//设置为不可记录
+                        if(findApp.isCanDonload()){
+                            //提交下载成功
+                            textView.setEnabled(true);
+                        } else {//提交预约成功
+                            textView.setEnabled(false);
+                            textView.setText("已经预约");
                             ViewUtil.showMsgDialog(activity, new DialogConfig().setTitle("预约成功")
                                     .setMsg(String.format(Locale.CHINA, "请在%s准时到小绵羊下载哦", TimeUtil.TimeStamp2Date(findApp.getDownload_at(), "yyyy年MM月dd日\tHH时mm分")))
                                     .setMsgGravity(Gravity.START).setBtnLeftText("我知道了"));
-                        } else {//提交下载成功
-                            find_game_down_tv.setEnabled(true);
                         }
                     }
 
                     @Override
                     public void onError(BaseMessage baseMessage) {
                         LogUtil.println("baseMessage onError "+JSON.toJSONString(baseMessage));
-                        if(isNeedReservation) {
+                        if(!findApp.isCanDonload()){
                             G.showToast("预约失败");
                         }
-                        find_game_down_tv.setEnabled(true);
+                        textView.setEnabled(true);
                     }
                 });
     }
+
+    /**
+     * 更新下载状态
+     * @param status
+     */
+    public void updateState(int status) {
+        downLoadType = status;
+    }
 }

+ 157 - 7
app/src/main/java/com/sheep/gamegroup/view/activity/ActFindGame.java

@@ -1,12 +1,13 @@
 package com.sheep.gamegroup.view.activity;
 
+import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.design.widget.AppBarLayout;
 import android.support.v7.widget.AppCompatRatingBar;
 import android.support.v7.widget.Toolbar;
 import android.text.TextUtils;
-import android.view.Gravity;
 import android.view.View;
 import android.webkit.WebView;
 import android.widget.ImageView;
@@ -19,13 +20,16 @@ import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.bumptech.glide.Glide;
-import com.bumptech.glide.request.RequestOptions;
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
+import com.kfzs.duanduan.services.DownloadTaskService;
+import com.kfzs.duanduan.utils.InstallButtonUtils;
 import com.kfzs.duanduan.utils.StatusBarUtils;
 import com.kfzs.duanduan.utils.dlg.HelperUtils;
+import com.kfzs.duanduan.view.KFProgressButton;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.model.entity.BaseMessage;
-import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.FindApp;
 import com.sheep.gamegroup.model.entity.FindAppScore;
 import com.sheep.gamegroup.model.entity.MyFindAppCore;
@@ -33,7 +37,6 @@ import com.sheep.gamegroup.model.entity.UserEntity;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
 import com.sheep.gamegroup.util.CommonUtil;
 import com.sheep.gamegroup.util.Conversion2;
-import com.sheep.gamegroup.util.FindAppHelper;
 import com.sheep.gamegroup.util.GlideImageLoader;
 import com.sheep.gamegroup.util.ImageGlarryDrawable;
 import com.sheep.gamegroup.util.ListUtil;
@@ -44,17 +47,28 @@ import com.sheep.gamegroup.view.customview.SheepViewPager;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
+import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
 
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+
+import java.io.File;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import butterknife.BindView;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.functions.Action1;
 import rx.schedulers.Schedulers;
 
+import static android.content.Intent.ACTION_PACKAGE_ADDED;
+import static android.content.Intent.ACTION_PACKAGE_REMOVED;
+
 /**
  * Created by realicing on 2018/5/10.
  * realicing@sina.com
@@ -78,6 +92,10 @@ public class ActFindGame extends BaseActivity {
         TitleBarUtils.getInstance()
                 .setTitle(this,"加载中")
                 .setTitleFinish(this);
+
+        EventBus.getDefault().register(this);
+        Aria.download(this).register();
+
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, HelperUtils.BAR_HEIGH);
         status_height_view.setLayoutParams(params);
         setSupportActionBar((Toolbar) findViewById(R.id.toolbar_act_game_details));
@@ -116,6 +134,7 @@ public class ActFindGame extends BaseActivity {
         resetMyAppScore();
         resetUserInfo();
     }
+
     private boolean mIsExpanded = false;//是否是折叠状态
 
     @Override
@@ -269,8 +288,10 @@ public class ActFindGame extends BaseActivity {
     RatingBar find_game_xin;
     @BindView(R.id.find_game_down_tv)
     TextView find_game_down_tv;
-    private FindAppHelper findAppHelper = new FindAppHelper();
+
+    private FindApp findApp;
     private void loadData(final FindApp findApp) {
+        this.findApp = findApp;
         //游戏简介与下载
         ViewUtil.setImage(find_game_iv, findApp.getIcon());
         ViewUtil.setText(find_game_name_tv, findApp.getName());
@@ -281,7 +302,9 @@ public class ActFindGame extends BaseActivity {
             ViewUtil.setText(find_game_info_tv, String.format(Locale.CHINA, "厂商:%s | 版本号:%s\n包体大小:%sM", findApp.getManufacturer(), findApp.getVersions(), findApp.getPackage_size()));
 
         find_game_xin.setRating(findApp.getScore()/2);
-        findAppHelper.updateView(this, findApp, find_game_down_tv);
+
+        updateView(this,findApp, find_game_down_tv);
+
         //游戏图片banner
         String pictures = findApp.getPictures();
         if(!TextUtils.isEmpty(pictures) && pictures.contains(";")){
@@ -428,4 +451,131 @@ public class ActFindGame extends BaseActivity {
     }
 
 
+    private Map<String, TextView> downLoadTextViewMap = new HashMap<>();
+    //获取任务对应的TextView来更新进度
+    private TextView getTextViewByTask(DownloadTask task) {
+        return getTextViewByKey(task.getKey());
+    }
+    private TextView getTextViewByKey(String key) {
+        if(downLoadTextViewMap.containsKey(key))
+            return downLoadTextViewMap.get(key);
+        return null;
+    }
+
+    //更新按钮状态与添加点击事件
+    private void updateView(Activity activity, FindApp findApp, TextView textView) {
+        if(findApp.isCanDonload()){//可下载
+            downLoadTextViewMap.put(findApp.getDownload_link(), textView);
+            downLoadTextViewMap.put(findApp.getPackage_name(), textView);
+            findApp.getFindAppHelper().updateDownloadTaskView(activity, findApp, textView);
+        } else {//预约下载
+            findApp.getFindAppHelper().updateReservationView(activity, findApp, textView);
+        }
+    }
+    //下载状态监听
+
+    @Download.onPre void onPre(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("准备下载中");
+        }
+    }
+
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("开始下载中");
+        }
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("恢复下载中");
+        }
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(String.format(Locale.CHINA, "%d%%", Math.abs(task.getPercent())));
+        }
+    }
+
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.CONTINUE_DOWNLOAD);
+        }
+    }
+
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
+
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("已经取消");
+        }
+    }
+
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.FAIL_DOWNLOAD);
+        }
+    }
+
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.START_INSTALL);
+        }
+        if(findApp != null && TextUtils.equals(task.getKey(), findApp.getDownload_link()))
+            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+    }
+
+    @Subscribe
+    public void onEventMainThread(Intent intent) {
+        if(intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")){
+            String packageName = intent.getDataString().replace("package:","");
+
+            TextView textView = getTextViewByKey(packageName);
+            if(textView != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        textView.setText(CommonUtil.GAME_OEPN);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            textView.setText(CommonUtil.START_DOWNLOAD);
+                        } else {
+                            textView.setText(CommonUtil.START_INSTALL);
+                        }
+                        break;
+                }
+            }
+
+            if(findApp != null && TextUtils.equals(packageName, findApp.getPackage_names())){
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        findApp.updateState(DownloadTaskService.STATUS_INSTALLED);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            findApp.updateState(DownloadTaskService.STATUS_INIT);
+                        } else {
+                            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+                        }
+                        break;
+                }
+            }
+        }
+    }
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().unregister(this);
+    }
 }

+ 150 - 4
app/src/main/java/com/sheep/gamegroup/view/activity/ActFindInformation.java

@@ -1,5 +1,7 @@
 package com.sheep.gamegroup.view.activity;
 
+import android.app.Activity;
+import android.content.Intent;
 import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
@@ -11,6 +13,10 @@ import android.widget.ImageView;
 import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
+import com.kfzs.duanduan.services.DownloadTaskService;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.FindApp;
@@ -19,6 +25,7 @@ import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
 import com.sheep.gamegroup.model.entity.TaskEty;
 import com.sheep.gamegroup.model.entity.TaskReleaseEty;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
+import com.sheep.gamegroup.util.CommonUtil;
 import com.sheep.gamegroup.util.FindAppHelper;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.ListUtil;
@@ -31,16 +38,25 @@ import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
 
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import butterknife.BindView;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.functions.Action1;
 import rx.schedulers.Schedulers;
 
+import static android.content.Intent.ACTION_PACKAGE_ADDED;
+import static android.content.Intent.ACTION_PACKAGE_REMOVED;
+
 /**
  * Created by realicing on 2018/5/10.
  * realicing@sina.com
@@ -76,6 +92,9 @@ public class ActFindInformation extends BaseActivity {
                     }
                 });
 
+        EventBus.getDefault().register(this);
+        Aria.download(this).register();
+
         refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
@@ -171,7 +190,6 @@ public class ActFindInformation extends BaseActivity {
             }
         });
     }
-    private FindAppHelper findAppHelper = new FindAppHelper();
     private void loadGame(View itemView) {
         itemView.setVisibility(View.VISIBLE);
         ImageView find_information_game_icon = (ImageView)itemView.findViewById(R.id.find_information_game_icon);
@@ -188,10 +206,9 @@ public class ActFindInformation extends BaseActivity {
             ViewUtil.setText(find_information_game_time, String.format(Locale.CHINA, "开放时间:%s", TimeUtil.TimeStamp2Date(findApp.getDownload_at(), "yyyy-MM-dd\tHH:mm")));
         else
             ViewUtil.setText(find_information_game_time, String.format(Locale.CHINA, "厂商:%s \n包体大小:%sM", findApp.getManufacturer(),findApp.getPackage_size()));
-//        ViewUtil.setText(find_information_game_surplus, String.format(Locale.CHINA, "厂商:%s", findApp.getManufacturer()));
-//        ViewUtil.setText(find_information_game_time, String.format(Locale.CHINA, "包体大小:%sM", findApp.getPackage_size()));
         find_information_game_yuan.setVisibility(View.GONE);
-        findAppHelper.updateView(this, findApp, find_information_game_task);
+
+        updateView(this,findApp, find_information_game_task);
 
         itemView.setOnClickListener(new View.OnClickListener() {
             @Override
@@ -322,4 +339,133 @@ public class ActFindInformation extends BaseActivity {
         refresh.setRefreshing(false);
         view_list.getAdapter().notifyDataSetChanged();
     }
+
+
+    private Map<String, TextView> downLoadTextViewMap = new HashMap<>();
+    //获取任务对应的TextView来更新进度
+    private TextView getTextViewByTask(DownloadTask task) {
+        return getTextViewByKey(task.getKey());
+    }
+    private TextView getTextViewByKey(String key) {
+        if(downLoadTextViewMap.containsKey(key))
+            return downLoadTextViewMap.get(key);
+        return null;
+    }
+
+    //更新按钮状态与添加点击事件
+    private void updateView(Activity activity, FindApp findApp, TextView textView) {
+        if(findApp.isCanDonload()){//可下载
+            downLoadTextViewMap.put(findApp.getDownload_link(), textView);
+            downLoadTextViewMap.put(findApp.getPackage_name(), textView);
+            findApp.getFindAppHelper().updateDownloadTaskView(activity, findApp, textView);
+        } else {//预约下载
+            findApp.getFindAppHelper().updateReservationView(activity, findApp, textView);
+        }
+    }
+
+    //下载状态监听
+
+    @Download.onPre void onPre(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("准备下载中");
+        }
+    }
+
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("开始下载中");
+        }
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("恢复下载中");
+        }
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(String.format(Locale.CHINA, "%d%%", Math.abs(task.getPercent())));
+        }
+    }
+
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.CONTINUE_DOWNLOAD);
+        }
+    }
+
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
+
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("已经取消");
+        }
+    }
+
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.FAIL_DOWNLOAD);
+        }
+    }
+
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.START_INSTALL);
+        }
+        if(findApp != null && TextUtils.equals(task.getKey(), findApp.getDownload_link()))
+            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+    }
+    @Subscribe
+    public void onEventMainThread(Intent intent) {
+        if(intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")){
+            String packageName = intent.getDataString().replace("package:","");
+
+            TextView textView = getTextViewByKey(packageName);
+            if(textView != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        textView.setText(CommonUtil.GAME_OEPN);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            textView.setText(CommonUtil.START_DOWNLOAD);
+                        } else {
+                            textView.setText(CommonUtil.START_INSTALL);
+                        }
+                        break;
+                }
+            }
+
+            if(findApp != null && TextUtils.equals(packageName, findApp.getPackage_names())){
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        findApp.updateState(DownloadTaskService.STATUS_INSTALLED);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            findApp.updateState(DownloadTaskService.STATUS_INIT);
+                        } else {
+                            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+                        }
+                        break;
+                }
+            }
+        }
+    }
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().unregister(this);
+    }
 }

+ 164 - 24
app/src/main/java/com/sheep/gamegroup/view/activity/ActReservation.java

@@ -1,18 +1,25 @@
 package com.sheep.gamegroup.view.activity;
 
+import android.content.Intent;
 import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.text.TextUtils;
 import android.view.View;
 import android.widget.ImageView;
 import android.widget.TextView;
 
 import com.alibaba.fastjson.JSON;
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
+import com.kfzs.duanduan.services.DownloadTaskService;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.FindApp;
 import com.sheep.gamegroup.model.entity.FindAppReservation;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
+import com.sheep.gamegroup.util.CommonUtil;
 import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.ListUtil;
 import com.sheep.gamegroup.util.LogUtil;
@@ -24,16 +31,25 @@ import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
 
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 
 import butterknife.BindView;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.functions.Action1;
 import rx.schedulers.Schedulers;
 
+import static android.content.Intent.ACTION_PACKAGE_ADDED;
+import static android.content.Intent.ACTION_PACKAGE_REMOVED;
+
 /**
  * Created by realicing on 2018/5/10.
  * realicing@sina.com
@@ -59,6 +75,9 @@ public class ActReservation extends BaseActivity {
                 .setTitle(this, "游戏预约")
                 .setTitleFinish(this);
 
+        EventBus.getDefault().register(this);
+        Aria.download(this).register();
+
         refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
@@ -95,7 +114,6 @@ public class ActReservation extends BaseActivity {
             }
         });
     }
-
     private void loadItem(View itemView, final FindAppReservation item) {
         ImageView find_information_game_icon = (ImageView) itemView.findViewById(R.id.find_information_game_icon);
         TextView find_information_game_name = (TextView) itemView.findViewById(R.id.find_information_game_name);
@@ -110,15 +128,11 @@ public class ActReservation extends BaseActivity {
             ViewUtil.setText(find_information_game_name, findApp.getName());
             ViewUtil.setText(find_information_game_time, String.format(Locale.CHINA, "开放时间:%s", TimeUtil.TimeStamp2Date(findApp.getDownload_at(), "yyyy-MM-dd\tHH:mm")));
             if(findApp.isCanDonload()){//可以下载
-                find_information_game_bt1.setEnabled(true);
-                find_information_game_bt1.setText("立即下载");
-                find_information_game_bt1.setOnClickListener(new View.OnClickListener() {
-                    @Override
-                    public void onClick(View view) {
-                        G.showToast("点击立即下载");
-                    }
-                });
-
+                downLoadTextViewMap.put(findApp.getDownload_link(), find_information_game_bt1);
+                downLoadTextViewMap.put(findApp.getPackage_name(), find_information_game_bt1);
+                findAppMap.put(findApp.getDownload_link(),findApp);
+                findAppMap.put(findApp.getPackage_name(), findApp);
+                findApp.getFindAppHelper().updateDownloadTaskView(this, findApp, find_information_game_bt1);
                 find_information_game_bt2.setEnabled(false);
             } else {
                 find_information_game_bt1.setEnabled(false);
@@ -131,9 +145,9 @@ public class ActReservation extends BaseActivity {
                     public void onClick(View view) {
                         find_information_game_bt2.setEnabled(false);
                         if(item.isCancel()){//已经取消预约则进行预约
-                            commitAppDownloads(find_information_game_bt2, findApp.getId());
+                            recordAppDownloads(find_information_game_bt2, findApp.getId(), item);
                         } else {//取消预约
-                            deleteDownloads(find_information_game_bt2, item.getId());
+                            deleteDownloads(find_information_game_bt2, item);
                         }
                     }
                 });
@@ -154,8 +168,8 @@ public class ActReservation extends BaseActivity {
     }
 
     //预约下载
-    private void commitAppDownloads(final TextView find_information_game_bt2, int id) {
-        SheepApp.getInstance().getNetComponent().getApiService().getDownloads(id)
+    private void recordAppDownloads(final TextView find_information_game_bt2, int id, final FindAppReservation item) {
+        SheepApp.getInstance().getNetComponent().getApiService().recordAppDownloads(id)
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
@@ -164,6 +178,7 @@ public class ActReservation extends BaseActivity {
                         LogUtil.println("baseMessage onNext "+JSON.toJSONString(baseMessage));
                         find_information_game_bt2.setEnabled(true);
                         find_information_game_bt2.setText("取消预约");
+                        item.setCancel(false);
                         G.showToast("预约成功");
                     }
 
@@ -176,8 +191,8 @@ public class ActReservation extends BaseActivity {
                 });
     }
     //取消预约
-    private void deleteDownloads(final TextView find_information_game_bt2, int id) {
-        SheepApp.getInstance().getNetComponent().getApiService().deleteDownloads(id)
+    private void deleteDownloads(final TextView find_information_game_bt2, final FindAppReservation item) {
+        SheepApp.getInstance().getNetComponent().getApiService().deleteDownloads(item.getId())
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
@@ -186,6 +201,7 @@ public class ActReservation extends BaseActivity {
                         LogUtil.println("baseMessage onNext "+JSON.toJSONString(baseMessage));
                         find_information_game_bt2.setEnabled(true);
                         find_information_game_bt2.setText("立即预约");
+                        item.setCancel(true);
                         G.showToast("取消预约成功");
                     }
 
@@ -227,19 +243,17 @@ public class ActReservation extends BaseActivity {
                     public void onNext(BaseMessage baseMessage) {
                         LogUtil.println("baseMessage onNext " + JSON.toJSONString(baseMessage));
                         List<FindAppReservation> newList = baseMessage.getDataList(FindAppReservation.class);
-                        if (ListUtil.isEmpty(newList)) {
-                        } else {
-                            list.clear();
-                            ListUtil.addAll(list, newList);
-                            if(!list.isEmpty())
-                                list.add(null);//底部圆角
-                            notifyDataSetChanged();
-                        }
+                        list.clear();
+                        ListUtil.addAll(list, newList);
+                        if(!list.isEmpty())
+                            list.add(null);//底部圆角
+                        notifyDataSetChanged();
                     }
 
                     @Override
                     public void onError(BaseMessage baseMessage) {
                         LogUtil.println("baseMessage onError " + JSON.toJSONString(baseMessage));
+                        list.clear();
                         notifyDataSetChanged();
                     }
                 });
@@ -252,4 +266,130 @@ public class ActReservation extends BaseActivity {
         refresh.setRefreshing(false);
         view_list.getAdapter().notifyDataSetChanged();
     }
+
+    private Map<String, FindApp> findAppMap = new HashMap<>();
+    //获取任务对应的findApp
+    private FindApp getFindAppByKey(String key) {
+        if(findAppMap.containsKey(key))
+            return findAppMap.get(key);
+        return null;
+    }
+    private Map<String, TextView> downLoadTextViewMap = new HashMap<>();
+    //获取任务对应的TextView来更新进度
+    private TextView getTextViewByTask(DownloadTask task) {
+        return getTextViewByKey(task.getKey());
+    }
+    private TextView getTextViewByKey(String key) {
+        if(downLoadTextViewMap.containsKey(key))
+            return downLoadTextViewMap.get(key);
+        return null;
+    }
+    //下载状态监听
+
+    @Download.onPre void onPre(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("准备下载中");
+        }
+    }
+
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("开始下载中");
+        }
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("恢复下载中");
+        }
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(String.format(Locale.CHINA, "%d%%", Math.abs(task.getPercent())));
+        }
+    }
+
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.CONTINUE_DOWNLOAD);
+        }
+    }
+
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
+
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText("已经取消");
+        }
+    }
+
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.FAIL_DOWNLOAD);
+        }
+    }
+
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        TextView textView = getTextViewByTask(task);
+        if(textView != null) {
+            textView.setText(CommonUtil.START_INSTALL);
+        }
+        FindApp findApp = getFindAppByKey(task.getKey());
+        if(findApp != null) {
+            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+        }
+    }
+
+    @Subscribe
+    public void onEventMainThread(Intent intent) {
+        if(intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")){
+            String packageName = intent.getDataString().replace("package:","");
+
+            TextView textView = getTextViewByKey(packageName);
+            if(textView != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        textView.setText(CommonUtil.GAME_OEPN);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            textView.setText(CommonUtil.START_DOWNLOAD);
+                        } else {
+                            textView.setText(CommonUtil.START_INSTALL);
+                        }
+                        break;
+                }
+            }
+            FindApp findApp = getFindAppByKey(packageName);
+            if(findApp != null) {
+                switch (intent.getAction()){
+                    case ACTION_PACKAGE_ADDED:
+                        findApp.updateState(DownloadTaskService.STATUS_INSTALLED);
+                        break;
+                    case ACTION_PACKAGE_REMOVED:
+                        String path = PackageUtil.isExistsFile(packageName);
+                        if(TextUtils.isEmpty(path)) {
+                            findApp.updateState(DownloadTaskService.STATUS_INIT);
+                        } else {
+                            findApp.updateState(DownloadTaskService.STATUS_FINISH);
+                        }
+                        break;
+                }
+            }
+        }
+    }
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().unregister(this);
+    }
 }

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/view/adapter/TryMakeMoneyAdp.java

@@ -610,7 +610,7 @@ public class TryMakeMoneyAdp extends AdpCommonRecy<RecyleObj> {
                         if(item == null){
                             return;
                         }
-                        UMConfigUtils.IdEvent.TASK.commit(item.getId());
+                        UMConfigUtils.IdEvent.TASK.commit(item.getRelease_task_id());
 
                         TaskEty taskEty = etyList.get(i).getRelease_task().getTask();
                         taskEty.setRunTask(1);

+ 2 - 0
app/src/main/res/layout/find_reservation_item_center.xml

@@ -2,6 +2,8 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
+    android:paddingStart="@dimen/content_padding"
+    android:paddingEnd="@dimen/content_padding"
     android:background="@color/white"
     android:orientation="vertical">