|
|
@@ -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);
|
|
|
+ }
|
|
|
}
|