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 000000000..f43066ada Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_label_dark_x24.png differ 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 000000000..cfc868c0f Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_label_dark_x24.png differ 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 000000000..57f863b45 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_label_dark_x24.png differ 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 000000000..7a020b40c Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_label_dark_x24.png differ 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