From ab9edf1cf7bff0d328d174cd6e7b1f075bb04966 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 28 Feb 2016 01:05:15 +0800 Subject: [PATCH] Add change download label --- .../ehviewer/download/DownloadManager.java | 59 ++++++++- .../com/hippo/ehviewer/ui/MainActivity.java | 6 + .../hippo/ehviewer/ui/scene/BaseScene.java | 7 ++ .../ehviewer/ui/scene/DownloadScene.java | 112 ++++++++++++++---- .../res/drawable-hdpi/ic_label_dark_x24.png | Bin 0 -> 170 bytes .../res/drawable-mdpi/ic_label_dark_x24.png | Bin 0 -> 123 bytes .../res/drawable-xhdpi/ic_label_dark_x24.png | Bin 0 -> 191 bytes .../res/drawable-xxhdpi/ic_label_dark_x24.png | Bin 0 -> 245 bytes app/src/main/res/menu/drawer_download.xml | 2 +- app/src/main/res/menu/scene_download.xml | 8 +- app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values/strings.xml | 2 + 12 files changed, 169 insertions(+), 28 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_label_dark_x24.png create mode 100644 app/src/main/res/drawable-mdpi/ic_label_dark_x24.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_label_dark_x24.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_label_dark_x24.png 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 31040f475..895408724 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -30,8 +30,11 @@ import com.hippo.ehviewer.spider.SpiderQueen; import com.hippo.image.Image; import com.hippo.yorozuya.ConcurrentPool; import com.hippo.yorozuya.IntList; +import com.hippo.yorozuya.ObjectUtils; import com.hippo.yorozuya.SimpleHandler; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -519,7 +522,7 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { // Two way if (gidList.size() < mWaitList.size()) { for (int i = 0, n = gidList.size(); i < n; i++) { - DownloadInfo info = stopDownloadInternal(gidList.get(i)); + stopDownloadInternal(gidList.get(i)); } } else { // Check current task @@ -543,6 +546,51 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } } + /** + * @param label Not allow new label + */ + public void changeLabel(List list, String label) { + if (null != label && !containLabel(label)) { + Log.e(TAG, "Not exits label: " + label); + return; + } + + List dstList = getInfoListForLabel(label); + if (dstList == null) { + Log.e(TAG, "Can't find label with label: " + label); + return; + } + + boolean changed = false; + + for (DownloadInfo info: list) { + if (ObjectUtils.equal(info.label, label)) { + continue; + } + + List srcList = getInfoListForLabel(info.label); + if (srcList == null) { + Log.e(TAG, "Can't find label with label: " + info.label); + continue; + } + + srcList.remove(info); + dstList.add(info); + info.label = label; + // TODO Other comparator + Collections.sort(dstList, sDateAscComparator); + + // Save to DB + EhDB.updateDownloadInfo(info); + + changed = true; + } + + if (changed && mDownloadInfoListener != null) { + mDownloadInfoListener.onReload(); + } + } + boolean isIdle() { return mCurrentTask == null && mWaitList.isEmpty(); } @@ -825,6 +873,13 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } } + private static Comparator sDateAscComparator = new Comparator() { + @Override + public int compare(DownloadInfo lhs, DownloadInfo rhs) { + return lhs.date - rhs.date > 0 ? 1 : -1; + } + }; + public interface DownloadInfoListener { void onAdd(DownloadInfo info, List list, int position); @@ -836,6 +891,8 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { void onReload(); void onRemove(DownloadInfo info, List list, int position); + + void onUpdateLabels(); } public interface DownloadListener { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java index a2dcd2974..fcf519f0e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java @@ -211,6 +211,12 @@ public final class MainActivity extends StageActivity } } + public void openDrawer(int drawerGravity) { + if (mDrawerLayout != null) { + mDrawerLayout.openDrawer(drawerGravity); + } + } + public void closeDrawer(int drawerGravity) { if (mDrawerLayout != null) { mDrawerLayout.closeDrawer(drawerGravity); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java index 2928e5136..70b1a06c9 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java @@ -38,6 +38,13 @@ public class BaseScene extends SceneFragment { } } + public void openDrawer(int drawerGravity) { + FragmentActivity activity = getActivity(); + if (activity instanceof MainActivity) { + ((MainActivity) activity).openDrawer(drawerGravity); + } + } + public void closeDrawer(int drawerGravity) { FragmentActivity activity = getActivity(); if (activity instanceof MainActivity) { 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 403325afc..00f7fe673 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 @@ -29,6 +29,7 @@ import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; @@ -103,9 +104,6 @@ public class DownloadScene extends ToolbarScene @Nullable private List mList; - private final IntList mTempIntList = new IntList(); - private final List mTempGalleryInfoList = new LinkedList<>(); - // TODO Only single instance @Override public int getLaunchMode() { @@ -137,6 +135,11 @@ public class DownloadScene extends ToolbarScene mList = manager.getDefaultDownloadInfoList(); } } + + if (mAdapter != null) { + mAdapter.notifyDataSetChanged(); + } + updateTitle(); Settings.putRecentDownloadLabel(mLabel); } @@ -251,6 +254,9 @@ public class DownloadScene extends ToolbarScene getActivity().startService(intent); return true; } + case R.id.action_label: { + openDrawer(Gravity.RIGHT); + } } return false; } @@ -405,16 +411,36 @@ public class DownloadScene extends ToolbarScene return; } + IntList gidList = null; + List galleryInfoList = null; + List downloadInfoList = null; + boolean collectGid = position == 1 || position == 2 || position == 3; // Start, Stop, Delete boolean collectGalleryInfo = position == 3; // Delete + boolean collectDownloadInfo = position == 4; // Move + if (collectGid) { + gidList = new IntList(); + } + if (collectGalleryInfo) { + galleryInfoList = new LinkedList<>(); + } + if (collectDownloadInfo) { + downloadInfoList = new LinkedList<>(); + } SparseBooleanArray stateArray = recyclerView.getCheckedItemPositions(); for (int i = 0, n = stateArray.size(); i < n; i++) { if (stateArray.valueAt(i)) { - GalleryInfo gi = list.get(stateArray.keyAt(i)).galleryInfo; + DownloadInfo info = list.get(stateArray.keyAt(i)); + if (collectDownloadInfo) { + downloadInfoList.add(info); + } + GalleryInfo gi = info.galleryInfo; if (collectGalleryInfo) { - mTempGalleryInfoList.add(gi); + galleryInfoList.add(gi); + } + if (collectGid) { + gidList.add(gi.gid); } - mTempIntList.add(gi.gid); } } @@ -422,10 +448,8 @@ public class DownloadScene extends ToolbarScene case 1: { // Start Intent intent = new Intent(getActivity(), DownloadService.class); intent.setAction(DownloadService.ACTION_START_RANGE); - intent.putExtra(DownloadService.KEY_GID_LIST, mTempIntList); + intent.putExtra(DownloadService.KEY_GID_LIST, gidList); getActivity().startService(intent); - // Clear - mTempIntList.clear(); // Cancel check mode recyclerView.outOfCustomChoiceMode(); break; @@ -433,29 +457,41 @@ public class DownloadScene extends ToolbarScene case 2: { // Stop Intent intent = new Intent(getActivity(), DownloadService.class); intent.setAction(DownloadService.ACTION_STOP_RANGE); - intent.putExtra(DownloadService.KEY_GID_LIST, mTempIntList); + intent.putExtra(DownloadService.KEY_GID_LIST, gidList); getActivity().startService(intent); - // Clear - mTempIntList.clear(); // Cancel check mode recyclerView.outOfCustomChoiceMode(); break; } - case 3: // Delete + case 3: { // Delete CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(getContext(), - getString(R.string.download_remove_dialog_message_2, mTempIntList.size()), + getString(R.string.download_remove_dialog_message_2, gidList.size()), getString(R.string.download_remove_dialog_check_text), Settings.getRemoveImageFiles()); DeleteRangeDialogHelper helper = new DeleteRangeDialogHelper( - mTempGalleryInfoList, mTempIntList, builder); + galleryInfoList, gidList, builder); builder.setTitle(R.string.download_remove_dialog_title) .setPositiveButton(android.R.string.ok, helper) - .setOnDismissListener(helper) .show(); break; - case 4: // Move - // TODO + } + case 4: {// Move + List labelRawList = EhApplication.getDownloadManager(getContext()).getLabelList(); + List labelList = new ArrayList<>(labelRawList.size() + 1); + labelList.add(getString(R.string.default_download_label_name)); + for (int i = 0, n = labelRawList.size(); i < n; i++) { + labelList.add(labelRawList.get(i).getLabel()); + } + String[] labels = labelList.toArray(new String[labelList.size()]); + + MoveDialogHelper helper = new MoveDialogHelper(labels, downloadInfoList); + + new AlertDialog.Builder(getContext()) + .setTitle(R.string.download_move_dialog_title) + .setItems(labels, helper) + .show(); break; + } } } } @@ -545,6 +581,11 @@ public class DownloadScene extends ToolbarScene } } + @Override + public void onUpdateLabels() { + // TODO + } + private void bindForState(DownloadHolder holder, DownloadInfo info) { switch (info.state) { case DownloadInfo.STATE_NONE: @@ -656,8 +697,7 @@ public class DownloadScene extends ToolbarScene } } - private class DeleteRangeDialogHelper implements DialogInterface.OnClickListener, - DialogInterface.OnDismissListener { + private class DeleteRangeDialogHelper implements DialogInterface.OnClickListener { private final List mGalleryInfoList; private final IntList mGidList; @@ -676,6 +716,11 @@ public class DownloadScene extends ToolbarScene return; } + // Cancel check mode + if (mRecyclerView != null) { + mRecyclerView.outOfCustomChoiceMode(); + } + // Delete Intent intent = new Intent(getActivity(), DownloadService.class); intent.setAction(DownloadService.ACTION_DELETE_RANGE); @@ -698,17 +743,33 @@ public class DownloadScene extends ToolbarScene // Delete file deleteFileAsync(files); } + } + } + private class MoveDialogHelper implements DialogInterface.OnClickListener { + + private final String[] mLabels; + private final List mDownloadInfoList; + + public MoveDialogHelper(String[] labels, List downloadInfoList) { + mLabels = labels; + mDownloadInfoList = downloadInfoList; + } + + @Override + public void onClick(DialogInterface dialog, int which) { // Cancel check mode if (mRecyclerView != null) { mRecyclerView.outOfCustomChoiceMode(); } - } - @Override - public void onDismiss(DialogInterface dialog) { - mGalleryInfoList.clear(); - mGidList.clear(); + String label; + if (which == 0) { + label = null; + } else { + label = mLabels[which]; + } + EhApplication.getDownloadManager(getContext()).changeLabel(mDownloadInfoList, label); } } @@ -743,6 +804,7 @@ public class DownloadScene extends ToolbarScene percent = (TextView) itemView.findViewById(R.id.percent); speed = (TextView) itemView.findViewById(R.id.speed); + // TODO cancel on click listener when select items thumb.setOnClickListener(this); start.setOnClickListener(this); stop.setOnClickListener(this); diff --git a/app/src/main/res/drawable-hdpi/ic_label_dark_x24.png b/app/src/main/res/drawable-hdpi/ic_label_dark_x24.png new file mode 100644 index 0000000000000000000000000000000000000000..f43066ada66f6f31f263bc777bc94ea78eeba3ec GIT binary patch literal 170 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8K8p{I*uNCjguLvx@3*FWw*`akYF zu#_mIoca0xNE1s9o9G{PR*45@D;iar-~AU~a7KaU#qui(EF24_IllkT9grr#^ajM& zSa3tZ#R(|*oPDk%TeH@N@BEusBn?&=z5V&0X^Y+s4gD-l{XgypH5eEY9c(@A@*Yh9 PTF&6<>gTe~DWM4fMOivV literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-mdpi/ic_label_dark_x24.png b/app/src/main/res/drawable-mdpi/ic_label_dark_x24.png new file mode 100644 index 0000000000000000000000000000000000000000..cfc868c0f5878c9437954081299e20be130b30b9 GIT binary patch literal 123 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM0wlfaz7_*1Cr=m0kP60RiGvU757*yWz!<#q zK>Z(uh7jKO|G5`rE3$lBoUOpab-`4CjcrMmg?}+Go5>fGv<*SlkC;4sYm{Xf7*=)N V4zpvImIs=~;OXk;vd$@?2>=?jBys=% literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xhdpi/ic_label_dark_x24.png b/app/src/main/res/drawable-xhdpi/ic_label_dark_x24.png new file mode 100644 index 0000000000000000000000000000000000000000..57f863b45eb5b1352fc3ba86970ba8b513f3c2c3 GIT binary patch literal 191 zcmeAS@N?(olHy`uVBq!ia0vp^1|ZDA0wn)(8}b0DHcuDFkP61P*Vq}I6?q&SRa~4@ z6;y*J1h7vP;q>TWQQ7l;!R)h~32Cj#w#OSE)ttKcefs}v)zJ@XRu^!uSNL-%E1%)} z_2@rZ*X|vdxJ&xL;%{sVwwh~r+~qw0lv(+WO&~je!t(nHet%DzRW5pY<*1%=u=$*j p?Ws|JH!l76Ahf>3rY|{A{8E+Y9mbEx?*LuI;OXk;vd$@?2>>~sOlAN8 literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_label_dark_x24.png b/app/src/main/res/drawable-xxhdpi/ic_label_dark_x24.png new file mode 100644 index 0000000000000000000000000000000000000000..7a020b40c98adba3c0fba723c05948b069ed3cdd GIT binary patch literal 245 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY0wn)GsXhaw_ISEDhEy=Vy^+1J*?__Mq4q){ zt%X8rjGB`hnza`O0ofA-IMbynp1a$zwjXbJs;{J^bcktc-;TBySBfop3w1A^nj3|&7D%SK=#kZ=gRR{{sh|m3AajiHkMwq rM{RGSdDX77hyER|ba8QMX_OEB;BoWWmy%$hKNvh+{an^LB{Ts5;!j}N literal 0 HcmV?d00001 diff --git a/app/src/main/res/menu/drawer_download.xml b/app/src/main/res/menu/drawer_download.xml index 54cbeb7cf..d397a7737 100644 --- a/app/src/main/res/menu/drawer_download.xml +++ b/app/src/main/res/menu/drawer_download.xml @@ -23,6 +23,6 @@ android:id="@+id/action_settings" android:icon="@drawable/ic_settings_dark_x24" android:title="@string/settings" - app:showAsAction="ifRoom"/> + app:showAsAction="always"/> diff --git a/app/src/main/res/menu/scene_download.xml b/app/src/main/res/menu/scene_download.xml index 3578bff20..737452165 100644 --- a/app/src/main/res/menu/scene_download.xml +++ b/app/src/main/res/menu/scene_download.xml @@ -23,12 +23,18 @@ android:id="@+id/action_start_all" android:icon="@drawable/ic_play_dark_x24" android:title="@string/refresh" - app:showAsAction="ifRoom"/> + app:showAsAction="always"/> + + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 238d66515..e03895338 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -197,6 +197,7 @@ 全部停止 标签 默认 + 标签 第 %d 页 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b8e7edd3..0cc105e73 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -237,6 +237,8 @@ Stop All Label Default + Move + Label Page %d