LimitActivity.java
7.29 KB
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
package com.cnlive.downloadfile;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.cnlive.downloadfile.adapter.LimitDownloadAdapter;
import com.cnlive.downloadfile.download.limit.DownLoadService;
import com.cnlive.downloadfile.download.limit.DownloadInfo;
import com.cnlive.downloadfile.download.limit.DownloadLimitManager;
import com.cnlive.downloadfile.download.limit.DownloadStatus;
import com.cnlive.downloadfile.download.util.NetWorkUtil;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_MOBILE;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_UN_NETWORK;
import static com.cnlive.downloadfile.download.util.CheckNetworkStatusChangeListener.Status.TYPE_WIFI;
public class LimitActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView recycler_view;
private LimitDownloadAdapter mAdapter;
private List<DownloadInfo> mData = new ArrayList<>();
private Button allStartPause, allClear;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
EventBus.getDefault().register(this);
recycler_view = findViewById(R.id.recycler_view);
allStartPause = findViewById(R.id.start_pause);
allClear = findViewById(R.id.clear);
allStartPause.setOnClickListener(this);
allClear.setOnClickListener(this);
if (DownloadLimitManager.getInstance().getCurremtDownload() == null) {
allStartPause.setText("全部开始");
} else {
allStartPause.setText("全部暂停");
}
mData = MyApplication.getInstances().getDaoSession().loadAll(DownloadInfo.class);
mAdapter = new LimitDownloadAdapter(this, mData);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
recycler_view.setAdapter(mAdapter);
// 取消item刷新的动画
((SimpleItemAnimator) recycler_view.getItemAnimator()).setSupportsChangeAnimations(false);
mAdapter.setDelectClick(new LimitDownloadAdapter.DelectClick() {
@Override
public void onDelectClickListener(DownloadInfo downloadInfo, int position) {
//删除该下载
Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_DELECT);
intent.putExtra("fileUrl", downloadInfo);
startService(intent);
mData.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void update(DownloadInfo info) {
if (DownloadStatus.DOWNLOAD.equals(info.getDownloadStatus())) {
allStartPause.setText("全部暂停");
Log.e("ssssss", "update: "+"dowing");
mAdapter.updateProgress(info);
} else if (DownloadStatus.DOWNLOAD_OVER.equals(info.getDownloadStatus())) {
for (int i = 0; i < mData.size(); i++) {
if (mData.get(i).getUrl().equals(info.getUrl())) {
mData.set(i, info);
MyApplication.getInstances().getDaoSession().delete(info);
mData.remove(i);
mAdapter.notifyItemRemoved(i);
mAdapter.notifyDataSetChanged();
break;
}
}
if (DownloadLimitManager.getInstance().getCurremtDownload() == null) {
allStartPause.setText("全部开始");
}
} else if (DownloadStatus.DOWNLOAD_PAUSE.equals(info.getDownloadStatus())) {
mAdapter.updateProgress(info);
boolean isAllPause = true;
for(int i = 0;i < mData.size();i++){
if(!mData.get(i).getDownloadStatus().equals(DownloadStatus.DOWNLOAD_PAUSE)){
isAllPause = false;
break;
}
}
if(isAllPause){
allStartPause.setText("全部开始");
}
} else if (DownloadStatus.DOWNLOAD_WAIT.equals(info.getDownloadStatus())) {
mAdapter.updateProgress(info);
} else if (DownloadStatus.DOWNLOAD_ERROR.equals(info.getDownloadStatus())) {
Toast.makeText(this, "下载出错", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_pause:
if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_UN_NETWORK)){
Toast.makeText(LimitActivity.this,"无网络",Toast.LENGTH_SHORT).show();
return;
}
if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_WIFI)){
allPause();
return;
}
if(NetWorkUtil.getNetworkConnectionType(LimitActivity.this).equals(TYPE_MOBILE)){
//弹框...保存用户习惯,拒绝或者是允许,允许的话是仅允许一次还是都允许
}
break;
case R.id.clear:
Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
intent.setAction(DownLoadService.ACION_CLEAR);
startService(intent);
mData.clear();
mAdapter.notifyDataSetChanged();
break;
}
}
private void allPause(){
if (allStartPause.getText().toString().equals("全部开始")) {
allStartPause.setText("全部暂停");
Log.e("ssssss", "update: "+"全部暂停");
Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_ALLSTART);
startService(intent);
for(int i = 0;i < mData.size();i++){
mData.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_WAIT);
}
mAdapter.notifyDataSetChanged();
} else {
//全部暂停,先进行暂停,再开始
Log.e("ssssss", "update: "+"全部开始");
Intent intent = new Intent(LimitActivity.this, DownLoadService.class);
intent.setAction(DownLoadService.ACTION_ALLPAUSE);
startService(intent);
for(int i = 0;i < mData.size();i++){
mData.get(i).setDownloadStatus(DownloadStatus.DOWNLOAD_PAUSE);
}
mAdapter.notifyDataSetChanged();
}
}
}