Realized deleting DownloadInfo

This commit is contained in:
seven332 committed 2015-08-24 16:57:23 +08:00
1 parent 0eff20b45a
commit 4cefcd8822
6 files changed
+129 -68

No files matched your search

@@ -43,7 +43,7 @@ public class GalleryDir {
return String.format("%08d.%s", index + 1, extension);
}
private static String getDirname(GalleryBase galleryBase) {
public static String getDirname(GalleryBase galleryBase) {
String dirname = DBUtils.getDirname(galleryBase.gid);
if (dirname == null) {
dirname = FileUtils.ensureFilename(galleryBase.gid + "-" + EhUtils.getSuitableTitle(galleryBase));
@@ -58,7 +58,10 @@ public class GalleryDir {
public void ensureUniFile() {
if (mUniFile == null) {
mUniFile = Settings.getImageDownloadLocation().createDirectory(mDirname);
UniFile downloadDir = Settings.getImageDownloadLocation();
if (downloadDir != null) {
mUniFile = downloadDir.createDirectory(mDirname);
}
}
}
@@ -90,6 +90,13 @@ public class GallerySpider implements GalleryProvider, SpiderQueen.SpiderListene
}
}
public static void forceSetMode(GalleryBase galleryBase, ImageHandler.Mode mode) {
GallerySpider gs = mMap.get(galleryBase.gid);
if (gs != null) {
gs.setMode(mode);
}
}
private GallerySpider(GalleryBase galleryBase, ImageHandler.Mode mode) throws IOException {
mGalleryBase = galleryBase;
mMode = mode;
@@ -25,11 +25,14 @@ import com.hippo.ehviewer.Constants;
import com.hippo.ehviewer.client.data.DownloadInfo;
import com.hippo.ehviewer.client.data.DownloadLabel;
import com.hippo.ehviewer.client.data.GalleryBase;
import com.hippo.ehviewer.gallery.GalleryDir;
import com.hippo.ehviewer.gallery.GallerySpider;
import com.hippo.ehviewer.gallery.ImageHandler;
import com.hippo.ehviewer.gallery.SpiderQueen;
import com.hippo.ehviewer.util.DBUtils;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Settings;
import com.hippo.unifile.UniFile;
import com.hippo.yorozuya.AssertUtils;
import com.hippo.yorozuya.Messenger;
import com.hippo.yorozuya.SimpleHandler;
@@ -370,67 +373,28 @@ public class DownloadManager {
}
}
void startAllDownload() {
for (DownloadInfo info : mDownloadInfos) {
if (info.state == DownloadInfo.STATE_NONE ||
(info.state == DownloadInfo.STATE_FINISH && info.legacy != 0)) {
info.state = DownloadInfo.STATE_WAIT;
// Add to wait list
mWaitList.add(info);
// Notify
Messenger.getInstance().notify(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(info.galleryBase.gid, OPS_UPDATE));
// Update DownloadInfo to DB
DBUtils.updateDownloadInfo(info);
}
}
ensureDownload();
}
public void stopDownload(int gid) {
// Check current task
if (mCurrentTask != null && mCurrentTask.galleryBase.gid == gid) {
// Stop current
stopCurrentDownloadInternal();
// Continue
if (stopDownloadInternal(gid)) {
Messenger.getInstance().notifyAtOnce(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(gid, OPS_UPDATE));
ensureDownload();
return;
}
// Check in wait list
if (!mWaitList.isEmpty()) {
for (Iterator<DownloadInfo> iter = mWaitList.iterator(); iter.hasNext();) {
DownloadInfo info = iter.next();
if (info.galleryBase.gid == gid) {
// Remove from wait list
iter.remove();
info.state = DownloadInfo.STATE_NONE;
// Notify
Messenger.getInstance().notify(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(gid, OPS_UPDATE));
// Update in DB
DBUtils.updateDownloadInfo(info);
return;
}
}
}
}
public void stopCurrentDownload() {
stopCurrentDownloadInternal();
ensureDownload();
// Get current task gid
int gid = mCurrentTask == null ? 0 : mCurrentTask.galleryBase.gid;
if (stopCurrentDownloadInternal()) {
Messenger.getInstance().notifyAtOnce(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(gid, OPS_UPDATE));
ensureDownload();
}
}
public void stopAllDownload() {
// Stop all in wait list
for (DownloadInfo info : mWaitList) {
info.state = DownloadInfo.STATE_NONE;
// Notify
Messenger.getInstance().notify(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(info.galleryBase.gid, OPS_UPDATE));
// Update in DB
DBUtils.updateDownloadInfo(info);
}
@@ -438,28 +402,90 @@ public class DownloadManager {
// Stop current
stopCurrentDownloadInternal();
// Notify
Messenger.getInstance().notifyAtOnce(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(0, OPS_ALL_CHANGE));
}
private void stopCurrentDownloadInternal() {
// Update in DB
// Update listener
// No Messenger
// No ensureDownload
private boolean stopDownloadInternal(int gid) {
// Check current task
if (mCurrentTask != null && mCurrentTask.galleryBase.gid == gid) {
// Stop current
return stopCurrentDownloadInternal();
}
// Check in wait list
for (Iterator<DownloadInfo> iter = mWaitList.iterator(); iter.hasNext();) {
DownloadInfo info = iter.next();
if (info.galleryBase.gid == gid) {
// Remove from wait list
iter.remove();
info.state = DownloadInfo.STATE_NONE;
// Update in DB
DBUtils.updateDownloadInfo(info);
return true;
}
}
return false;
}
// Update in DB
// Update listener
// No Messenger
// No ensureDownload
private boolean stopCurrentDownloadInternal() {
if (mCurrentTask == null) {
return;
return false;
}
mCurrentTask.state = DownloadInfo.STATE_NONE;
int gid = mCurrentTask.galleryBase.gid;
// Notify
Messenger.getInstance().notify(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(gid, OPS_UPDATE));
// Update in DB
DBUtils.updateDownloadInfo(mCurrentTask);
// Listener
if (mDownloadUpdateListener != null) {
mDownloadUpdateListener.onCancel(mUpdateInfo);
}
// Update in DB
DBUtils.updateDownloadInfo(mCurrentTask);
// Release
releaseCurrent();
return true;
}
public void deleteDownloadInfo(DownloadInfo... infos) {
for (DownloadInfo info : infos) {
// Stop
stopDownloadInternal(info.galleryBase.gid);
// removed download info from list
mDownloadInfos.remove(info);
List<DownloadInfo> infoList = getDownloadListInternal(info.label, false);
if (infoList != null) {
infoList.remove(info);
}
// remove download info from DB
DBUtils.removeDownloadLabel(info.galleryBase.gid);
// Set mode read for available gallery spider
GallerySpider.forceSetMode(info.galleryBase, ImageHandler.Mode.READ);
// TODO do it in new thread
// Delete files
UniFile downloadLocation = Settings.getImageDownloadLocation();
if (downloadLocation != null) {
UniFile dir = downloadLocation.findFile(GalleryDir.getDirname(info.galleryBase));
if (dir != null) {
dir.delete();
}
}
}
Messenger.getInstance().notify(Constants.MESSENGER_ID_UPDATE_DOWNLOAD,
makeMix(0, OPS_ALL_CHANGE));
}
public static boolean isDefaultLabel(String label) {
@@ -114,7 +114,7 @@ public class DownloadService extends Service implements DownloadManager.Download
mDownloadManager.startDownload(gb, tag);
}
} else if (ACTION_START_ALL.equals(action)) {
mDownloadManager.startAllDownload();
//mDownloadManager.startAllDownload();
} else if (ACTION_STOP.equals(action)) {
int gid = intent.getIntExtra(KEY_GID, 0);
if (gid != 0) {
@@ -23,6 +23,7 @@ import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
@@ -371,20 +372,25 @@ public class DownloadScene extends AppbarScene implements DrawerProvider,
@Override
public void onClick(View v) {
Log.d("TAG", "mDeleteListener 1");
if (mRecyclerView.inCustomChoice()) {
return;
}
Log.d("TAG", "mDeleteListener 2");
int position = getAdapterPosition(v);
if (position >= 0) {
// Add to cache
DownloadInfo info = mDownloadInfos.get(position);
mGidPositionMap.add(info.galleryBase.gid, position);
Intent intent = new Intent(getStageActivity(), DownloadService.class);
intent.setAction(DownloadService.ACTION_DELETE);
intent.putExtra(DownloadService.KEY_GID, info.galleryBase.gid);
getStageActivity().startService(intent);
Log.d("TAG", "mDeleteListener 3");
// Delete
DownloadManager.getInstance().deleteDownloadInfo(info);
}
}
};
@@ -581,6 +587,9 @@ public class DownloadScene extends AppbarScene implements DrawerProvider,
if (mMainFab == v) {
mRecyclerView.outOfCustomChoiceMode();
} else if (mStart == v) {
// Out of choice mode
mRecyclerView.outOfCustomChoiceMode();
SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions();
for (int i = 0, n = checkedState.size(); i < n; i++) {
if (checkedState.valueAt(i)) {
@@ -592,9 +601,10 @@ public class DownloadScene extends AppbarScene implements DrawerProvider,
getStageActivity().startService(intent);
}
}
} else if (mStop == v) {
// Out of choice mode
mRecyclerView.outOfCustomChoiceMode();
} else if (mStop == v) {
DownloadManager manager = DownloadManager.getInstance();
SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions();
for (int i = 0, n = checkedState.size(); i < n; i++) {
@@ -603,10 +613,21 @@ public class DownloadScene extends AppbarScene implements DrawerProvider,
manager.stopDownload(mDownloadInfos.get(position).galleryBase.gid);
}
}
} else if (mDelete == v) {
// Out of choice mode
mRecyclerView.outOfCustomChoiceMode();
} else if (mDelete == v) {
// TODO
List<DownloadInfo> infos = new ArrayList<>();
SparseBooleanArray checkedState = mRecyclerView.getCheckedItemPositions();
for (int i = 0, n = checkedState.size(); i < n; i++) {
if (checkedState.valueAt(i)) {
int position = checkedState.keyAt(i);
infos.add(mDownloadInfos.get(position));
}
}
// Delete
DownloadManager.getInstance().deleteDownloadInfo(infos.toArray(new DownloadInfo[infos.size()]));
} else if (mMove == v) {
// Get available target label
boolean hasDefaultLabel = false;
@@ -229,6 +229,10 @@ public class DBUtils {
dao.update(obj);
}
public static void removeDownloadInfo(int gid) {
sDaoSession.getDownloadInfoObjDao().deleteByKey((long) gid);
}
public static List<String> getAllDownloadLabel() {
DownloadLabelObjDao dao = sDaoSession.getDownloadLabelObjDao();
List<DownloadLabelObj> list = dao.queryBuilder().orderAsc(DownloadLabelObjDao.Properties.Time).list();