From a4b4de08dafdb53a58e9b600d54d6eb89275ad0f Mon Sep 17 00:00:00 2001 From: seven332 Date: Fri, 26 Feb 2016 17:01:40 +0800 Subject: [PATCH] Add delete download item --- .../com/hippo/app/CheckBoxDialogBuilder.java | 48 ++++++ .../main/java/com/hippo/ehviewer/EhDB.java | 60 ++++++- .../java/com/hippo/ehviewer/Settings.java | 12 +- .../ehviewer/download/DownloadManager.java | 149 +++++++++++------- .../ehviewer/download/DownloadService.java | 6 + .../com/hippo/ehviewer/spider/SpiderDen.java | 15 +- .../ehviewer/ui/scene/DownloadScene.java | 87 ++++++++-- .../res/layout/dialog_checkbox_builder.xml | 49 ++++++ app/src/main/res/values/strings.xml | 3 + build.gradle | 2 +- .../ehviewer/daogenerator/EhDaoGenerator.java | 9 ++ 11 files changed, 359 insertions(+), 81 deletions(-) create mode 100644 app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java create mode 100644 app/src/main/res/layout/dialog_checkbox_builder.xml diff --git a/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java b/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java new file mode 100644 index 000000000..4efa86406 --- /dev/null +++ b/app/src/main/java/com/hippo/app/CheckBoxDialogBuilder.java @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.app; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.support.v7.app.AlertDialog; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.CheckBox; +import android.widget.TextView; + +import com.hippo.ehviewer.R; + +public class CheckBoxDialogBuilder extends AlertDialog.Builder { + + private final CheckBox mCheckBox; + + @SuppressLint("InflateParams") + public CheckBoxDialogBuilder(Context context, String message, String checkText, boolean checked) { + super(context); + View view = LayoutInflater.from(context).inflate(R.layout.dialog_checkbox_builder, null); + setView(view); + TextView messageView = (TextView) view.findViewById(R.id.message); + mCheckBox = (CheckBox) view.findViewById(R.id.checkbox); + messageView.setText(message); + mCheckBox.setText(checkText); + mCheckBox.setChecked(checked); + } + + public boolean isChecked() { + return mCheckBox.isChecked(); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/EhDB.java b/app/src/main/java/com/hippo/ehviewer/EhDB.java index eb1a1eec7..2d9d2b332 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhDB.java +++ b/app/src/main/java/com/hippo/ehviewer/EhDB.java @@ -20,12 +20,16 @@ import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.Log; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.ListUrlBuilder; import com.hippo.ehviewer.dao.DaoMaster; import com.hippo.ehviewer.dao.DaoSession; +import com.hippo.ehviewer.dao.DownloadDirnameDao; +import com.hippo.ehviewer.dao.DownloadDirnameRaw; import com.hippo.ehviewer.dao.DownloadInfoDao; import com.hippo.ehviewer.dao.DownloadInfoRaw; import com.hippo.ehviewer.dao.DownloadLabelDao; @@ -268,7 +272,7 @@ public class EhDB { } } - private static void addGalleryInfo(GalleryInfo galleryInfo) { + private static synchronized void addGalleryInfo(GalleryInfo galleryInfo) { GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); GalleryInfoRaw raw = dao.load((long) galleryInfo.gid); if (raw == null) { @@ -291,7 +295,7 @@ public class EhDB { } } - private static void removeGalleryInfo(int gid) { + private static synchronized void removeGalleryInfo(int gid) { GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); GalleryInfoRaw raw = dao.load((long) gid); int reference = raw.getReference(); @@ -305,7 +309,7 @@ public class EhDB { } } - public static GalleryInfo getGalleryInfo(int gid) { + public static synchronized GalleryInfo getGalleryInfo(int gid) { GalleryInfoDao dao = sDaoSession.getGalleryInfoDao(); GalleryInfoRaw raw = dao.load((long) gid); if (raw != null) { @@ -325,7 +329,8 @@ public class EhDB { } } - public static List getAllDownloadLabelList() { + @NonNull + public static synchronized List getAllDownloadLabelList() { DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); List list = dao.queryBuilder().orderAsc(DownloadLabelDao.Properties.Time).list(); List result = new ArrayList<>(list.size()); @@ -335,12 +340,12 @@ public class EhDB { return result; } - public static LazyList getDownloadInfoLazyList() { + public static synchronized LazyList getDownloadInfoLazyList() { return sDaoSession.getDownloadInfoDao().queryBuilder() .orderAsc(DownloadInfoDao.Properties.Date).listLazy(); } - public static void addDownloadInfo(DownloadInfo info) { + public static synchronized void addDownloadInfo(DownloadInfo info) { // Add gallery info first addGalleryInfo(info.galleryInfo); // Add download info @@ -354,7 +359,7 @@ public class EhDB { dao.insert(raw); } - public static void updateDownloadInfo(DownloadInfo info) { + public static synchronized void updateDownloadInfo(DownloadInfo info) { DownloadInfoDao dao = sDaoSession.getDownloadInfoDao(); DownloadInfoRaw raw = dao.load((long) info.galleryInfo.gid); if (raw == null) { @@ -368,4 +373,45 @@ public class EhDB { dao.update(raw); } } + + public static synchronized void removeDownloadInfo(int gid) { + DownloadInfoDao dao = sDaoSession.getDownloadInfoDao(); + if (null != dao.load((long) gid)) { + removeGalleryInfo(gid); + dao.deleteByKey((long) gid); + } + } + + @Nullable + public static synchronized String getDownloadDirname(int gid) { + DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); + DownloadDirnameRaw raw = dao.load((long) gid); + if (raw != null) { + return raw.getDirname(); + } else { + return null; + } + } + + /** + * Insert or update + */ + public static synchronized void putDownloadDirname(int gid, String dirname) { + DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); + DownloadDirnameRaw raw = dao.load((long) gid); + if (raw != null) { // Update + raw.setDirname(dirname); + dao.update(raw); + } else { // Insert + raw = new DownloadDirnameRaw(); + raw.setGid((long) gid); + raw.setDirname(dirname); + dao.insert(raw); + } + } + + public static synchronized void removeDownloadDirname(int gid) { + DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); + dao.deleteByKey((long) gid); + } } diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.java b/app/src/main/java/com/hippo/ehviewer/Settings.java index 5447ea5ed..5a931a164 100644 --- a/app/src/main/java/com/hippo/ehviewer/Settings.java +++ b/app/src/main/java/com/hippo/ehviewer/Settings.java @@ -117,7 +117,7 @@ public class Settings { putBoolean(KEY_SHOW_WARNING, value); } - public static final String KEY_RECENT_DOWNLOAD_LABEL = "recent_download_label"; + private static final String KEY_RECENT_DOWNLOAD_LABEL = "recent_download_label"; private static final String DEFAULT_RECENT_DOWNLOAD_LABEL = null; public static String getRecentDownloadLabel() { @@ -128,6 +128,16 @@ public class Settings { putString(KEY_RECENT_DOWNLOAD_LABEL, value); } + private static final String KEY_REMOVE_IMAGE_FILES = "include_pic"; + private static final boolean DEFAULT_REMOVE_IMAGE_FILES = true; + + public static boolean getRemoveImageFiles() { + return getBoolean(KEY_REMOVE_IMAGE_FILES, DEFAULT_REMOVE_IMAGE_FILES); + } + + public static void putRemoveImageFiles(boolean value) { + putBoolean(KEY_REMOVE_IMAGE_FILES, value); + } public static EhConfig getEhConfig() { return sEhConfig; diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java index 4f29cfb78..525ac4bed 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -45,18 +45,21 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { private final Context mContext; + @NonNull private final Map> mMap; // All labels without default label + @NonNull private final List mLabelList; // Store download info with default label + @NonNull private final LinkedList mDefaultInfoList; - // Store all download info - private final LinkedList mAllInfoList; // For quick search + @NonNull private final SparseArray mAllInfoMap; // Store download info wait to start + @NonNull private final LinkedList mWaitList; - + @NonNull private final SpeedReminder mSpeedReminder; private DownloadListener mDownloadListener; @@ -82,8 +85,6 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } // Create default for non tag mDefaultInfoList = new LinkedList<>(); - // Create all download info list - mAllInfoList = new LinkedList<>(); // Create all download info map mAllInfoMap = new SparseArray<>(); @@ -95,18 +96,17 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { continue; } - // Add to all info list - mAllInfoList.add(info); // Add to all info map mAllInfoMap.put(info.galleryInfo.gid, info); // Add to list - List list = null; - if (info.label != null) { - list = mMap.get(info.label); - } + LinkedList list = getInfoListForLabel(info.label); if (list == null) { - list = mDefaultInfoList; + list = new LinkedList<>(); + mMap.put(info.label, list); + if (!mLabelList.contains(info.label)) { + mLabelList.add(info.label); + } } list.add(info); } @@ -119,7 +119,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } @Nullable - private DownloadInfo createDownloadInfo(DownloadInfoRaw raw) { + private static DownloadInfo createDownloadInfo(DownloadInfoRaw raw) { GalleryInfo gi = EhDB.getGalleryInfo((int) (long) raw.getGid()); if (gi == null) { Log.d(TAG, "Can't get GalleryInfo: " + raw.getGid()); @@ -139,6 +139,15 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { return info; } + @Nullable + private LinkedList getInfoListForLabel(String label) { + if (label == null) { + return mDefaultInfoList; + } else { + return mMap.get(label); + } + } + public boolean containLabel(String label) { return mLabelList.contains(label); } @@ -148,11 +157,6 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { return mLabelList; } - @NonNull - public List getAllDownloadInfoList() { - return mAllInfoList; - } - @NonNull public List getDefaultDownloadInfoList() { return mDefaultInfoList; @@ -198,8 +202,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { mDownloadListener.onStart(info); } // Notify state update - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } } } @@ -221,8 +226,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { // Update in DB EhDB.updateDownloadInfo(info); // Notify state update - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } // Make sure download is running ensureDownload(); @@ -234,33 +240,27 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { info.label = label; info.state = DownloadInfo.STATE_WAIT; info.date = System.currentTimeMillis(); - // Add to all download list and map - mAllInfoList.add(info); - mAllInfoMap.put(galleryInfo.gid, info); + // Add to label download list - LinkedList list; - if (label == null) { - list = mDefaultInfoList; - } else { - list = mMap.get(label); - if (list == null) { - Log.e(TAG, "Can't find download info list with label: " + label); - list = new LinkedList<>(); - mMap.put(label, list); - // Maybe label is missing in mLabelList - if (!mLabelList.contains(label)) { - mLabelList.add(label); - } - } + LinkedList list = getInfoListForLabel(info.label); + if (list == null) { + Log.e(TAG, "Can't find download info list with label: " + label); + return; } list.add(info); + + // Add to all download list and map + mAllInfoMap.put(galleryInfo.gid, info); + // Add to wait list mWaitList.add(info); + // Save to EhDB.addDownloadInfo(info); + // Notify if (mDownloadInfoListener != null) { - mDownloadInfoListener.onAdd(info); + mDownloadInfoListener.onAdd(info, list, list.size() - 1); } // Make sure download is running ensureDownload(); @@ -271,8 +271,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { DownloadInfo info = stopDownloadInternal(gid); if (info != null) { // Update listener - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } // Ensure download ensureDownload(); @@ -283,8 +284,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { DownloadInfo info = stopCurrentDownloadInternal(); if (info != null) { // Update listener - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } // Ensure download ensureDownload(); @@ -309,6 +311,34 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } } + void deleteDownload(int gid) { + stopDownloadInternal(gid); + DownloadInfo info = mAllInfoMap.get(gid); + if (info != null) { + // Remove from DB + EhDB.removeDownloadInfo(info.galleryInfo.gid); + + // Remove all list and map + mAllInfoMap.remove(info.galleryInfo.gid); + + // Remove label list + LinkedList list = getInfoListForLabel(info.label); + if (list != null) { + int index = list.indexOf(info); + if (index >= 0) { + list.remove(info); + // Update listener + if (mDownloadInfoListener != null) { + mDownloadInfoListener.onRemove(info, list, index); + } + } + } + + // Ensure download + ensureDownload(); + } + } + // Update in DB // Update listener // No ensureDownload @@ -504,8 +534,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { Log.e(TAG, "Current task is null, but it should not be"); } else { info.total = mPages; - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } } break; @@ -530,8 +561,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { if (mDownloadListener != null) { mDownloadListener.onGetPage(info); } - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } } break; @@ -543,8 +575,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } else { info.download = mDownloaded; info.total = mTotal; - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } } break; @@ -582,8 +615,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { if (mDownloadListener != null) { mDownloadListener.onFinish(info); } - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } // Start next download ensureDownload(); @@ -629,8 +663,9 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { if (mDownloadListener != null) { mDownloadListener.onDownload(info); } - if (mDownloadInfoListener != null) { - mDownloadInfoListener.onUpdate(info); + List list = getInfoListForLabel(info.label); + if (list != null && mDownloadInfoListener != null) { + mDownloadInfoListener.onUpdate(info, list); } } @@ -642,13 +677,13 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { public interface DownloadInfoListener { - void onAdd(DownloadInfo info); + void onAdd(DownloadInfo info, List list, int position); - void onUpdate(DownloadInfo info); + void onUpdate(DownloadInfo info, List list); void onUpdateAll(); - void onRemove(DownloadInfo info); + void onRemove(DownloadInfo info, List list, int position); } public interface DownloadListener { diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java index 8771e212a..5cabeb00d 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java @@ -40,6 +40,7 @@ public class DownloadService extends Service implements DownloadManager.Download public static final String ACTION_STOP = "stop"; public static final String ACTION_STOP_CURRENT = "stop_current"; public static final String ACTION_STOP_ALL = "stop_all"; + public static final String ACTION_DELETE = "delete"; public static final String ACTION_CLEAR = "clear"; @@ -148,6 +149,11 @@ public class DownloadService extends Service implements DownloadManager.Download if (mDownloadManager != null) { mDownloadManager.stopAllDownload(); } + } else if (ACTION_DELETE.equals(action)) { + int gid = intent.getIntExtra(KEY_GID, -1); + if (gid != -1 && mDownloadManager != null) { + mDownloadManager.deleteDownload(gid); + } } else if (ACTION_CLEAR.equals(action)) { EhApplication.clearDownloadService(getApplicationContext(), this); } diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java b/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java index 93614f767..98f08b27d 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderDen.java @@ -62,11 +62,18 @@ public final class SpiderDen { public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) { UniFile dir = Settings.getDownloadLocation(); if (dir != null) { - GalleryInfo info = EhDB.getGalleryInfo(galleryInfo.gid); - if (info == null) { - info = galleryInfo; + // Read from DB + String dirname = EhDB.getDownloadDirname(galleryInfo.gid); + if (dirname == null) { + GalleryInfo info = EhDB.getGalleryInfo(galleryInfo.gid); + if (info == null) { + info = galleryInfo; + } + dirname = FileUtils.sanitizeFilename(info.gid + "-" + info.title); + // Put into DB + EhDB.putDownloadDirname(info.gid, dirname); } - return dir.subFile(FileUtils.sanitizeFilename(info.gid + "-" + info.title)); + return dir.subFile(dirname); } else { return null; } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java index 429c60a2d..0ff7d1305 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadScene.java @@ -19,7 +19,9 @@ package com.hippo.ehviewer.ui.scene; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.support.annotation.NonNull; @@ -35,8 +37,10 @@ import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.TextView; +import com.hippo.app.CheckBoxDialogBuilder; import com.hippo.easyrecyclerview.EasyRecyclerView; import com.hippo.ehviewer.EhApplication; +import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.R; import com.hippo.ehviewer.Settings; import com.hippo.ehviewer.client.EhCacheKeyFactory; @@ -45,11 +49,13 @@ import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.download.DownloadInfo; import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.download.DownloadService; +import com.hippo.ehviewer.spider.SpiderDen; import com.hippo.ehviewer.ui.GalleryActivity; import com.hippo.ehviewer.widget.SimpleRatingView; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.TransitionHelper; +import com.hippo.unifile.UniFile; import com.hippo.util.ActivityHelper; import com.hippo.util.ApiHelper; import com.hippo.vector.VectorDrawable; @@ -57,7 +63,6 @@ import com.hippo.view.ViewTransition; import com.hippo.widget.LoadImageView; import com.hippo.widget.SimpleImageView; import com.hippo.yorozuya.FileUtils; -import com.hippo.yorozuya.ObjectUtils; import java.util.List; @@ -221,23 +226,24 @@ public class DownloadScene extends ToolbarScene } @Override - public void onAdd(DownloadInfo info) { - // TODO + public void onAdd(DownloadInfo info, List list, int position) { + if (mList != list) { + return; + } + if (mAdapter != null) { + mAdapter.notifyItemInserted(position); + } } @Override - public void onUpdate(DownloadInfo info) { - if (!ObjectUtils.equal(info.label, mLabel)) { + public void onUpdate(DownloadInfo info, List list) { + if (mList != list) { return; } RecyclerView recyclerView = mRecyclerView; if (recyclerView == null) { return; } - List list = mList; - if (list == null) { - return; - } int size = list.size(); for (int i = 0, n = recyclerView.getChildCount(); i < n; i++) { @@ -276,8 +282,17 @@ public class DownloadScene extends ToolbarScene } @Override - public void onRemove(DownloadInfo info) { + public void onRemove(DownloadInfo info, List list, int position) { + if (mList != list) { + return; + } + if (mAdapter != null) { + mAdapter.notifyItemRemoved(position); + } + if (mList != null && mList.size() == 0 && mViewTransition != null) { + mViewTransition.showView(1); + } } private void bindForState(DownloadHolder holder, DownloadInfo info) { @@ -344,6 +359,48 @@ public class DownloadScene extends ToolbarScene holder.speed.setText(FileUtils.humanReadableByteCount(speed, false) + "/S"); } + private class DeleteDialogHelper implements DialogInterface.OnClickListener { + + private final GalleryInfo mGalleryInfo; + private final CheckBoxDialogBuilder mBuilder; + + public DeleteDialogHelper(GalleryInfo galleryInfo, CheckBoxDialogBuilder builder) { + mGalleryInfo = galleryInfo; + mBuilder = builder; + } + + @Override + public void onClick(DialogInterface dialog, int which) { + if (which != DialogInterface.BUTTON_POSITIVE) { + return; + } + + // Delete + Intent intent = new Intent(getActivity(), DownloadService.class); + intent.setAction(DownloadService.ACTION_DELETE); + intent.putExtra(DownloadService.KEY_GID, mGalleryInfo.gid); + getActivity().startService(intent); + + boolean checked = mBuilder.isChecked(); + Settings.putRemoveImageFiles(checked); + if (checked) { + // Remove download path + EhDB.removeDownloadDirname(mGalleryInfo.gid); + // Delete file + UniFile file = SpiderDen.getGalleryDownloadDir(mGalleryInfo); + new AsyncTask() { + @Override + protected Void doInBackground(UniFile... params) { + for (UniFile file: params) { + file.delete(); + } + return null; + } + }.execute(file); + } + } + } + private class DownloadHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public final LoadImageView thumb; @@ -421,7 +478,15 @@ public class DownloadScene extends ToolbarScene intent.putExtra(DownloadService.KEY_GID, list.get(index).galleryInfo.gid); getActivity().startService(intent); } else if (delete == v) { - // TODO + GalleryInfo galleryInfo = list.get(index).galleryInfo; + CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(getContext(), + getString(R.string.download_remove_dialog_message, galleryInfo.title), + getString(R.string.download_remove_dialog_check_text), + Settings.getRemoveImageFiles()); + DeleteDialogHelper helper = new DeleteDialogHelper(galleryInfo, builder); + builder.setTitle(R.string.download_remove_dialog_title) + .setPositiveButton(android.R.string.ok, helper) + .show(); } } } diff --git a/app/src/main/res/layout/dialog_checkbox_builder.xml b/app/src/main/res/layout/dialog_checkbox_builder.xml new file mode 100644 index 000000000..5ec34c421 --- /dev/null +++ b/app/src/main/res/layout/dialog_checkbox_builder.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 059670358..c17ae4b27 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -227,6 +227,9 @@ %1$d succeeded, %2$d failed Succeeded: %s Failed: %s + Remove Gallery + Remove %s from download list ? + Remove image files Page %d diff --git a/build.gradle b/build.gradle index 481a2e27c..9ba597255 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0-beta5' + classpath 'com.android.tools.build:gradle:2.0.0-beta6' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java b/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java index 4aef747e2..0a7ac5c74 100644 --- a/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java +++ b/daogenerator/src/main/java/com/hippo/ehviewer/daogenerator/EhDaoGenerator.java @@ -40,6 +40,7 @@ public class EhDaoGenerator { addGalleryInfo(schema); addDownloadInfo(schema); addDownloadLabel(schema); + addDownloadDirname(schema); addHistoryInfo(schema); addQuickSearch(schema); addLocalFavorite(schema); @@ -81,6 +82,14 @@ public class EhDaoGenerator { entity.addLongProperty("time"); } + private static void addDownloadDirname(Schema schema) { + Entity entity = schema.addEntity("DownloadDirnameRaw"); + entity.setTableName("DOWNLOAD_DIRNAME"); + entity.setClassNameDao("DownloadDirnameDao"); + entity.addLongProperty("gid").primaryKey(); + entity.addStringProperty("dirname"); + } + private static void addHistoryInfo(Schema schema) { Entity entity = schema.addEntity("HistoryInfoRaw"); entity.setTableName("HISTORY_INFO");