Support downloading galleries from FavoritesScene

This commit is contained in:
seven332 committed 2019-01-17 18:41:49 +08:00
1 parent 7228736c95
commit 879b8fa50a
4 files changed
+96 -15

No files matched your search

@@ -45,6 +45,9 @@ import com.hippo.util.ExceptionUtils;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.collect.LongList;
import java.util.ArrayList;
import java.util.Collections;
import org.json.JSONException;
import org.json.JSONObject;
@@ -281,11 +284,37 @@ public final class CommonOperations {
}
}
// TODO Add context if activity and context are different style
public static void startDownload(final MainActivity activity, final GalleryInfo galleryInfo, boolean forceDefault) {
startDownload(activity, Collections.singletonList(galleryInfo), forceDefault);
}
// TODO Add context if activity and context are different style
public static void startDownload(final MainActivity activity, final List<GalleryInfo> galleryInfos, boolean forceDefault) {
final DownloadManager dm = EhApplication.getDownloadManager(activity);
boolean justStart = forceDefault || dm.containDownloadInfo(galleryInfo.gid);
LongList toStart = new LongList();
List<GalleryInfo> toAdd = new ArrayList<>();
for (GalleryInfo gi : galleryInfos) {
if (dm.containDownloadInfo(gi.gid)) {
toStart.add(gi.gid);
} else {
toAdd.add(gi);
}
}
if (!toStart.isEmpty()) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START_RANGE);
intent.putExtra(DownloadService.KEY_GID_LIST, toStart);
activity.startService(intent);
}
if (toAdd.isEmpty()) {
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
return;
}
boolean justStart = forceDefault;
String label = null;
// Get default download label
if (!justStart && Settings.getHasDefaultDownloadLabel()) {
@@ -299,12 +328,14 @@ public final class CommonOperations {
}
if (justStart) {
// Already in download list or get default label
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo);
activity.startService(intent);
// Got default label
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Notify
activity.showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT);
} else {
@@ -330,11 +361,13 @@ public final class CommonOperations {
}
}
// Start download
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo);
activity.startService(intent);
for (GalleryInfo gi : toAdd) {
Intent intent = new Intent(activity, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_LABEL, label);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
activity.startService(intent);
}
// Save settings
if (builder.isChecked()) {
Settings.putHasDefaultDownloadLabel(true);
@@ -63,6 +63,7 @@ import com.hippo.ehviewer.client.EhUrl;
import com.hippo.ehviewer.client.data.FavListUrlBuilder;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.parser.FavoritesParser;
import com.hippo.ehviewer.ui.CommonOperations;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.ehviewer.ui.annotation.DrawerLifeCircle;
import com.hippo.ehviewer.ui.annotation.ViewLifeCircle;
@@ -762,7 +763,18 @@ public class FavoritesScene extends BaseScene implements
}
switch (position) {
case 0: { // Delete
case 0: { // Download
Activity activity = getActivity2();
if (activity != null) {
CommonOperations.startDownload(getActivity2(), mModifyGiList, false);
}
mModifyGiList.clear();
if (mRecyclerView != null && mRecyclerView.isInCustomChoice()) {
mRecyclerView.outOfCustomChoiceMode();
}
break;
}
case 1: { // Delete
DeleteDialogHelper helper = new DeleteDialogHelper();
new AlertDialog.Builder(context)
.setTitle(R.string.delete_favorites_dialog_title)
@@ -772,7 +784,7 @@ public class FavoritesScene extends BaseScene implements
.show();
break;
}
case 1: { // Move
case 2: { // Move
MoveDialogHelper helper = new MoveDialogHelper();
// First is local favorite, the other 10 is cloud favorite
String[] array = new String[11];