Add start all download items

This commit is contained in:
seven332 committed 2016-02-26 22:09:27 +08:00
1 parent bfe1853bb5
commit 9634843109
15 files changed
+106 -12

No files matched your search

@@ -79,14 +79,16 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener {
mLabelList = labels;
// Create list for each label
mMap = new HashMap<>();
HashMap<String, LinkedList<DownloadInfo>> map = new HashMap<>();
mMap = map;
for (String label : labels) {
mMap.put(label, new LinkedList<DownloadInfo>());
map.put(label, new LinkedList<DownloadInfo>());
}
// Create default for non tag
mDefaultInfoList = new LinkedList<>();
// Create all download info map
mAllInfoMap = new SparseArray<>();
SparseArray<DownloadInfo> allInfoMap = new SparseArray<>();
mAllInfoMap = allInfoMap;
// Fill download info list
LazyList<DownloadInfoRaw> lazyList = EhDB.getDownloadInfoLazyList();
@@ -97,15 +99,15 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener {
}
// Add to all info map
mAllInfoMap.put(info.galleryInfo.gid, info);
allInfoMap.put(info.galleryInfo.gid, info);
// Add to list
LinkedList<DownloadInfo> list = getInfoListForLabel(info.label);
if (list == null) {
list = new LinkedList<>();
mMap.put(info.label, list);
if (!mLabelList.contains(info.label)) {
mLabelList.add(info.label);
map.put(info.label, list);
if (!labels.contains(info.label)) {
labels.add(info.label);
}
}
list.add(info);
@@ -267,6 +269,34 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener {
}
}
void startAllDownload() {
boolean update = false;
// Start all STATE_NONE and STATE_FAILED item
SparseArray<DownloadInfo> allInfoMap = mAllInfoMap;
List<DownloadInfo> waitList = mWaitList;
for (int i = 0, n = allInfoMap.size(); i < n; i++) {
DownloadInfo info = allInfoMap.valueAt(i);
if (info.state == DownloadInfo.STATE_NONE || info.state == DownloadInfo.STATE_FAILED) {
update = true;
// Set state DownloadInfo.STATE_WAIT
info.state = DownloadInfo.STATE_WAIT;
// Add to wait list
waitList.add(info);
// Update in DB
EhDB.updateDownloadInfo(info);
}
}
if (update) {
// Notify Listener
if (mDownloadInfoListener != null) {
mDownloadInfoListener.onUpdateAll();
}
// Ensure download
ensureDownload();
}
}
void stopDownload(int gid) {
DownloadInfo info = stopDownloadInternal(gid);
if (info != null) {
@@ -132,11 +132,15 @@ public class DownloadService extends Service implements DownloadManager.Download
}
if (ACTION_START.equals(action)) {
GalleryInfo gi= intent.getParcelableExtra(KEY_GALLERY_INFO);
GalleryInfo gi = intent.getParcelableExtra(KEY_GALLERY_INFO);
String label = intent.getStringExtra(KEY_LABEL);
if (gi != null && mDownloadManager != null) {
mDownloadManager.startDownload(gi, label);
}
} else if (ACTION_START_ALL.equals(action)) {
if (mDownloadManager != null) {
mDownloadManager.startAllDownload();
}
} else if (ACTION_STOP.equals(action)) {
int gid = intent.getIntExtra(KEY_GID, -1);
if (gid != -1 && mDownloadManager != null) {
@@ -182,7 +186,7 @@ public class DownloadService extends Service implements DownloadManager.Download
.setAutoCancel(false)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setColor(getResources().getColor(R.color.colorPrimary))
.addAction(R.drawable.ic_stop_x24, getString(R.string.stat_download_action_stop_all), piStopAll);
.addAction(R.drawable.ic_pause_x24, getString(R.string.stat_download_action_stop_all), piStopAll);
}
private void ensureDownloadedBuilder() {
@@ -32,6 +32,7 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.TransitionInflater;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
@@ -191,6 +192,31 @@ public class DownloadScene extends ToolbarScene
finish();
}
@Override
public int getMenuResId() {
return R.menu.scene_download;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_start_all: {
Intent intent = new Intent(getActivity(), DownloadService.class);
intent.setAction(DownloadService.ACTION_START_ALL);
getActivity().startService(intent);
return true;
}
case R.id.action_stop_all: {
Intent intent = new Intent(getActivity(), DownloadService.class);
intent.setAction(DownloadService.ACTION_STOP_ALL);
getActivity().startService(intent);
return true;
}
}
return false;
}
@Override
public void onDestroyView() {
super.onDestroyView();