Add select download label

This commit is contained in:
seven332 committed 2016-03-05 00:47:25 +08:00
1 parent a0b273bb72
commit d2553b39a6
11 files changed
+183 -17

No files matched your search

@@ -273,6 +273,28 @@ public class Settings {
putString(KEY_RECENT_DOWNLOAD_LABEL, value);
}
private static final String KEY_HAS_DEFAULT_DOWNLOAD_LABEL = "has_default_download_label";
private static final boolean DEFAULT_HAS_DOWNLOAD_LABEL = false;
public static boolean getHasDefaultDownloadLabel() {
return getBoolean(KEY_HAS_DEFAULT_DOWNLOAD_LABEL, DEFAULT_HAS_DOWNLOAD_LABEL);
}
public static void putHasDefaultDownloadLabel(boolean hasDefaultDownloadLabel) {
putBoolean(KEY_HAS_DEFAULT_DOWNLOAD_LABEL, hasDefaultDownloadLabel);
}
private static final String KEY_DEFAULT_DOWNLOAD_LABEL = "default_download_label";
private static final String DEFAULT_DOWNLOAD_LABEL = null;
public static String getDefaultDownloadLabel() {
return getString(KEY_DEFAULT_DOWNLOAD_LABEL, DEFAULT_DOWNLOAD_LABEL);
}
public static void putDefaultDownloadLabel(String value) {
putString(KEY_DEFAULT_DOWNLOAD_LABEL, value);
}
/********************
****** Favorites
********************/
@@ -174,6 +174,10 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener {
return false;
}
public boolean containDownloadInfo(int gid) {
return mAllInfoMap.indexOfKey(gid) >= 0;
}
@NonNull
public List<DownloadLabelRaw> getLabelList() {
return mLabelList;
@@ -17,6 +17,7 @@
package com.hippo.ehviewer.ui;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import com.hippo.app.ListCheckBoxDialogBuilder;
@@ -27,6 +28,11 @@ import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.dao.DownloadLabelRaw;
import com.hippo.ehviewer.download.DownloadManager;
import com.hippo.ehviewer.download.DownloadService;
import java.util.List;
public final class CommonOperations {
@@ -63,10 +69,10 @@ public final class CommonOperations {
public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) {
int slot = position - 1;
doAddToFavorites(activity, galleryInfo, slot, listener);
if (!builder.isChecked()) {
Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT);
} else {
if (builder.isChecked()) {
Settings.putDefaultFavSlot(slot);
} else {
Settings.putDefaultFavSlot(Settings.INVALID_DEFAULT_FAV_SLOT);
}
}
}, activity.getString(R.string.remember_favorite_slot), false)
@@ -84,4 +90,63 @@ public final class CommonOperations {
request.setCallback(listener);
client.execute(request);
}
public static void startDownload(final Activity activity, final GalleryInfo galleryInfo) {
final DownloadManager dm = EhApplication.getDownloadManager(activity);
boolean justStart = dm.containDownloadInfo(galleryInfo.gid);
String label = null;
if (!justStart && Settings.getHasDefaultDownloadLabel()) {
label = Settings.getDefaultDownloadLabel();
justStart = label == null || dm.containLabel(label);
}
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);
} else {
// Let use chose label
List<DownloadLabelRaw> list = dm.getLabelList();
final String[] items = new String[list.size() + 1];
items[0] = activity.getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 1] = list.get(i).getLabel();
}
new ListCheckBoxDialogBuilder(activity, items,
new ListCheckBoxDialogBuilder.OnItemClickListener() {
@Override
public void onItemClick(ListCheckBoxDialogBuilder builder, AlertDialog dialog, int position) {
String label;
if (position == 0) {
label = null;
} else {
label = items[position];
if (!dm.containLabel(label)) {
label = null;
}
}
// 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);
// Save settings
if (builder.isChecked()) {
Settings.putHasDefaultDownloadLabel(true);
Settings.putDefaultDownloadLabel(label);
} else {
Settings.putHasDefaultDownloadLabel(false);
}
}
}, activity.getString(R.string.remember_download_label), false)
.setTitle(R.string.download)
.show();
}
}
}
@@ -308,6 +308,35 @@ public class DownloadScene extends ToolbarScene
case R.id.action_settings:
startScene(new Announcer(DownloadLabelScene.class));
return true;
case R.id.action_default_download_label:
DownloadManager dm = EhApplication.getDownloadManager(getContext());
List<DownloadLabelRaw> list = dm.getLabelList();
final String[] items = new String[list.size() + 2];
items[0] = getString(R.string.let_me_select);
items[1] = getString(R.string.default_download_label_name);
for (int i = 0, n = list.size(); i < n; i++) {
items[i + 2] = list.get(i).getLabel();
}
new AlertDialog.Builder(getContext())
.setTitle(R.string.default_download_label)
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Settings.putHasDefaultDownloadLabel(false);
} else {
Settings.putHasDefaultDownloadLabel(true);
String label;
if (which == 1) {
label = null;
} else {
label = items[which];
}
Settings.putDefaultDownloadLabel(label);
}
}
}).show();
return true;
}
return false;
}
@@ -347,7 +347,7 @@ public class FavoritesScene extends BaseScene implements
switch (id) {
case R.id.action_default_favorites_slot:
String[] items = new String[12];
items[0] = getString(R.string.let_me_select_favorites_slot);
items[0] = getString(R.string.let_me_select);
items[1] = getString(R.string.local_favorites);
String[] favCat = Settings.getFavCat();
System.arraycopy(favCat, 0, items, 2, 10);
@@ -71,7 +71,6 @@ import com.hippo.ehviewer.client.data.GalleryTagGroup;
import com.hippo.ehviewer.client.data.LargePreviewSet;
import com.hippo.ehviewer.client.data.ListUrlBuilder;
import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.download.DownloadService;
import com.hippo.ehviewer.ui.CommonOperations;
import com.hippo.ehviewer.ui.GalleryActivity;
import com.hippo.ehviewer.ui.annotation.ViewLifeCircle;
@@ -992,10 +991,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
galleryInfo = mGalleryDetail;
}
if (galleryInfo != null) {
Intent intent = new Intent(getActivity(), DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, galleryInfo);
getActivity().startService(intent);
CommonOperations.startDownload(getActivity(), galleryInfo);
}
} else if (mRead == v) {
GalleryInfo galleryInfo = null;
@@ -20,7 +20,6 @@ import android.animation.Animator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
@@ -60,7 +59,7 @@ import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.data.ListUrlBuilder;
import com.hippo.ehviewer.client.parser.GalleryListParser;
import com.hippo.ehviewer.download.DownloadService;
import com.hippo.ehviewer.ui.CommonOperations;
import com.hippo.ehviewer.widget.FitPaddingLayout;
import com.hippo.ehviewer.widget.SearchBar;
import com.hippo.ehviewer.widget.SearchLayout;
@@ -460,13 +459,10 @@ public final class GalleryListScene extends BaseScene
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // Download
Intent intent = new Intent(getActivity(), DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra(DownloadService.KEY_GALLERY_INFO, gi);
getActivity().startService(intent);
CommonOperations.startDownload(getActivity(), gi);
break;
case 1: // Favorites
// TODO
CommonOperations.addToFavorites(getActivity(), gi, new addToFavoriteListener());
break;
}
}
@@ -892,4 +888,20 @@ public final class GalleryListScene extends BaseScene
return scene instanceof GalleryListScene;
}
}
private class addToFavoriteListener implements EhClient.Callback<Void> {
@Override
public void onSuccess(Void result) {
Toast.makeText(getContext(), R.string.add_to_favorite_success, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Exception e) {
Toast.makeText(getContext(), R.string.add_to_favorite_failure, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {}
}
}