|
|
@@ -33,15 +33,17 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
private Context mContext;
|
|
|
private final LayoutInflater mInflater;
|
|
|
private DownloadManager downloadManager;
|
|
|
+
|
|
|
public DownListAdapter(Context context) {
|
|
|
- mContext=context;
|
|
|
- mInflater=LayoutInflater.from(mContext);
|
|
|
- downloadManager=DownloadManager.getInstance();
|
|
|
+ mContext = context;
|
|
|
+ mInflater = LayoutInflater.from(mContext);
|
|
|
+ downloadManager = DownloadManager.getInstance();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getCount() {
|
|
|
- if (downloadManager == null) return 0;
|
|
|
+ if (downloadManager == null)
|
|
|
+ return 0;
|
|
|
return downloadManager.getDownloadListCount();
|
|
|
}
|
|
|
|
|
|
@@ -64,18 +66,18 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
view = mInflater.inflate(R.layout.down_item, null);
|
|
|
holder = new DownloadItemViewHolder(view, downloadInfo);
|
|
|
view.setTag(holder);
|
|
|
- holder.refresh();
|
|
|
} else {
|
|
|
holder = (DownloadItemViewHolder) view.getTag();
|
|
|
holder.update(downloadInfo);
|
|
|
}
|
|
|
-
|
|
|
+ holder.refreshkem();
|
|
|
if (downloadInfo.getState().value() < DownloadState.FINISHED.value()) {
|
|
|
try {
|
|
|
downloadManager.startDownload(
|
|
|
+ downloadInfo.getPkgName(),
|
|
|
downloadInfo.getUrl(),
|
|
|
downloadInfo.getLabel(),
|
|
|
- downloadInfo.getFileSavePath(),"",
|
|
|
+ downloadInfo.getFileSavePath(), "",
|
|
|
downloadInfo.isAutoResume(),
|
|
|
downloadInfo.isAutoRename(),
|
|
|
holder);
|
|
|
@@ -86,24 +88,23 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
return view;
|
|
|
}
|
|
|
|
|
|
- class DownloadItemViewHolder extends DownloadViewHolder implements View.OnClickListener{
|
|
|
+ class DownloadItemViewHolder extends DownloadViewHolder implements View.OnClickListener {
|
|
|
|
|
|
private ImageView ImageView;
|
|
|
- private TextView tv_game_name;
|
|
|
- private ImageView iv_state;
|
|
|
- private ImageView iv_remove;
|
|
|
+ private TextView tv_game_name;
|
|
|
+ private ImageView iv_state;
|
|
|
+ private ImageView iv_remove;
|
|
|
private ProgressBar pb_download;
|
|
|
- private TextView tv_prorgess;
|
|
|
+ private TextView tv_prorgess;
|
|
|
|
|
|
public DownloadItemViewHolder(View view, DownloadInfo downloadInfo) {
|
|
|
super(view, downloadInfo);
|
|
|
- ImageView=(ImageView) view.findViewById(R.id.img_icon);
|
|
|
- tv_game_name=(TextView) view.findViewById(R.id.tv_game_name);
|
|
|
- iv_state=(ImageView) view.findViewById(R.id.iv_state);
|
|
|
- pb_download=(ProgressBar) view.findViewById(R.id.pb_download);
|
|
|
- iv_remove=(ImageView)view.findViewById(R.id.iv_remove);
|
|
|
- tv_prorgess=(TextView) view.findViewById(R.id.tv_prorgess);
|
|
|
- refresh();
|
|
|
+ ImageView = (ImageView) view.findViewById(R.id.img_icon);
|
|
|
+ tv_game_name = (TextView) view.findViewById(R.id.tv_game_name);
|
|
|
+ iv_state = (ImageView) view.findViewById(R.id.iv_state);
|
|
|
+ pb_download = (ProgressBar) view.findViewById(R.id.pb_download);
|
|
|
+ iv_remove = (ImageView) view.findViewById(R.id.iv_remove);
|
|
|
+ tv_prorgess = (TextView) view.findViewById(R.id.tv_prorgess);
|
|
|
Glide.with(mContext)
|
|
|
.load(downloadInfo.getIcon())
|
|
|
.placeholder(R.drawable.icon)
|
|
|
@@ -112,13 +113,11 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
iv_state.setOnClickListener(this);
|
|
|
iv_remove.setOnClickListener(this);
|
|
|
tv_prorgess.setOnClickListener(this);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void update(DownloadInfo downloadInfo) {
|
|
|
super.update(downloadInfo);
|
|
|
- refresh();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -150,10 +149,11 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
public void onCancelled(Callback.CancelledException cex) {
|
|
|
refresh();
|
|
|
}
|
|
|
+
|
|
|
public void refresh() {
|
|
|
tv_game_name.setText(downloadInfo.getLabel());
|
|
|
pb_download.setProgress(downloadInfo.getProgress());
|
|
|
- tv_prorgess.setText(downloadInfo.getProgress()+"%");
|
|
|
+ tv_prorgess.setText(downloadInfo.getProgress() + "%");
|
|
|
DownloadState state = downloadInfo.getState();
|
|
|
switch (state) {
|
|
|
case WAITING:
|
|
|
@@ -165,9 +165,13 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
iv_state.setImageResource(R.drawable.icon_begin);
|
|
|
break;
|
|
|
case FINISHED:
|
|
|
- PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
iv_state.setVisibility(View.GONE);
|
|
|
- tv_prorgess.setText("完成");
|
|
|
+ if (PackageUtil.isAppInstalled(mContext, getDownloadInfo().getPkgName())) {
|
|
|
+ tv_prorgess.setText("已安装");
|
|
|
+ } else {
|
|
|
+ tv_prorgess.setText("安 装");
|
|
|
+ PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
+ }
|
|
|
break;
|
|
|
default:
|
|
|
iv_state.setImageResource(R.drawable.icon_begin);
|
|
|
@@ -175,67 +179,95 @@ public class DownListAdapter extends BaseAdapter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ public void refreshkem() {
|
|
|
+ tv_game_name.setText(downloadInfo.getLabel());
|
|
|
+ pb_download.setProgress(downloadInfo.getProgress());
|
|
|
+ tv_prorgess.setText(downloadInfo.getProgress() + "%");
|
|
|
+ DownloadState state = downloadInfo.getState();
|
|
|
+ switch (state) {
|
|
|
+ case WAITING:
|
|
|
+ case STARTED:
|
|
|
+ iv_state.setImageResource(R.drawable.icon_stop);
|
|
|
+ break;
|
|
|
+ case ERROR:
|
|
|
+ case STOPPED:
|
|
|
+ iv_state.setImageResource(R.drawable.icon_begin);
|
|
|
+ break;
|
|
|
+ case FINISHED:
|
|
|
+ iv_state.setVisibility(View.GONE);
|
|
|
+ if (PackageUtil.isAppInstalled(mContext, getDownloadInfo().getPkgName())) {
|
|
|
+ tv_prorgess.setText("已安装");
|
|
|
+ } else {
|
|
|
+ tv_prorgess.setText("安 装");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ iv_state.setImageResource(R.drawable.icon_begin);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
@Override
|
|
|
public void onClick(View view) {
|
|
|
- switch (view.getId()){
|
|
|
- case R.id.iv_state:
|
|
|
- DownloadState state = downloadInfo.getState();
|
|
|
- switch (state) {
|
|
|
- case WAITING:
|
|
|
- case STARTED:
|
|
|
- downloadManager.stopDownload(downloadInfo);
|
|
|
-
|
|
|
- break;
|
|
|
- case ERROR:
|
|
|
- case STOPPED:
|
|
|
- try {
|
|
|
- downloadManager.startDownload(
|
|
|
- downloadInfo.getUrl(),
|
|
|
- downloadInfo.getLabel(),
|
|
|
- downloadInfo.getFileSavePath(),"",
|
|
|
- downloadInfo.isAutoResume(),
|
|
|
- downloadInfo.isAutoRename(),
|
|
|
- this);
|
|
|
- } catch (DbException ex) {
|
|
|
- Toast.makeText(x.app(), "添加下载失败", Toast.LENGTH_LONG).show();
|
|
|
- }
|
|
|
- break;
|
|
|
- case FINISHED:
|
|
|
- Toast.makeText(x.app(), "已经下载完成", Toast.LENGTH_LONG).show();
|
|
|
- PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- break;
|
|
|
- case R.id.iv_remove:
|
|
|
- try {
|
|
|
- downloadManager.removeDownload(downloadInfo);
|
|
|
- notifyDataSetChanged();
|
|
|
- } catch (DbException e) {
|
|
|
- Toast.makeText(x.app(), "移除任务失败", Toast.LENGTH_LONG).show();
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case R.id.tv_prorgess:
|
|
|
- DownloadState states = downloadInfo.getState();
|
|
|
-
|
|
|
- switch (states){
|
|
|
- case FINISHED:
|
|
|
- PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- }
|
|
|
+ switch (view.getId()) {
|
|
|
+ case R.id.iv_state:
|
|
|
+ DownloadState state = downloadInfo.getState();
|
|
|
+ switch (state) {
|
|
|
+ case WAITING:
|
|
|
+ case STARTED:
|
|
|
+ downloadManager.stopDownload(downloadInfo);
|
|
|
+
|
|
|
+ break;
|
|
|
+ case ERROR:
|
|
|
+ case STOPPED:
|
|
|
+ try {
|
|
|
+ downloadManager.startDownload(
|
|
|
+ downloadInfo.getPkgName(),
|
|
|
+ downloadInfo.getUrl(),
|
|
|
+ downloadInfo.getLabel(),
|
|
|
+ downloadInfo.getFileSavePath(), "",
|
|
|
+ downloadInfo.isAutoResume(),
|
|
|
+ downloadInfo.isAutoRename(),
|
|
|
+ this);
|
|
|
+ } catch (DbException ex) {
|
|
|
+ Toast.makeText(x.app(), "添加下载失败", Toast.LENGTH_LONG).show();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case FINISHED:
|
|
|
+ Toast.makeText(x.app(), "已经下载完成", Toast.LENGTH_LONG).show();
|
|
|
+ PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case R.id.iv_remove:
|
|
|
+ try {
|
|
|
+ downloadManager.removeDownload(downloadInfo);
|
|
|
+ File file = new File(downloadInfo.getFileSavePath());
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ notifyDataSetChanged();
|
|
|
+ } catch (DbException e) {
|
|
|
+ Toast.makeText(x.app(), "移除任务失败", Toast.LENGTH_LONG).show();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case R.id.tv_prorgess:
|
|
|
+ DownloadState states = downloadInfo.getState();
|
|
|
+ switch (states) {
|
|
|
+ case FINISHED:
|
|
|
+ PackageUtil.installApk(mContext, getDownloadInfo().getFileSavePath());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
|
|
|
|