Improve restore download items
This commit is contained in:
1 parent
fa1348860b
commit
6879c5ea71
5 files changed
+224
-213
No files matched your search
@@ -28,7 +28,6 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.util.LruCache;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.hippo.beerbelly.SimpleDiskCache;
|
||||
import com.hippo.conaco.Conaco;
|
||||
@@ -46,13 +45,15 @@ import com.hippo.text.Html;
|
||||
import com.hippo.util.BitmapUtils;
|
||||
import com.hippo.util.ReadableTime;
|
||||
import com.hippo.yorozuya.FileUtils;
|
||||
import com.hippo.yorozuya.IntIdGenerator;
|
||||
import com.hippo.yorozuya.SimpleHandler;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
@@ -67,8 +68,8 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx
|
||||
|
||||
private Thread.UncaughtExceptionHandler mDefaultHandler;
|
||||
|
||||
private final AtomicInteger mIdGenerator = new AtomicInteger();
|
||||
private final SparseArray<Object> mGlobalStuffMap = new SparseArray<>();
|
||||
private final IntIdGenerator mIdGenerator = new IntIdGenerator();
|
||||
private final WeakHashMap<Integer, Object> mGlobalStuffMap = new WeakHashMap<>();
|
||||
private EhCookieStore mEhCookieStore;
|
||||
private EhClient mEhClient;
|
||||
private OkHttpClient mOkHttpClient;
|
||||
@@ -162,13 +163,13 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx
|
||||
}
|
||||
|
||||
public int putGlobalStuff(@NonNull Object o) {
|
||||
int id = mIdGenerator.getAndDecrement();
|
||||
int id = mIdGenerator.nextId();
|
||||
mGlobalStuffMap.put(id, o);
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean containGlobalStuff(int id) {
|
||||
return mGlobalStuffMap.indexOfKey(id) >= 0;
|
||||
return mGlobalStuffMap.containsKey(id);
|
||||
}
|
||||
|
||||
public Object getGlobalStuff(int id) {
|
||||
@@ -176,24 +177,11 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx
|
||||
}
|
||||
|
||||
public Object removeGlobalStuff(int id) {
|
||||
int index = mGlobalStuffMap.indexOfKey(id);
|
||||
if (index >= 0) {
|
||||
Object o = mGlobalStuffMap.valueAt(index);
|
||||
mGlobalStuffMap.removeAt(index);
|
||||
return o;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return mGlobalStuffMap.remove(id);
|
||||
}
|
||||
|
||||
public boolean removeGlobalStuff(Object o) {
|
||||
int index = mGlobalStuffMap.indexOfValue(o);
|
||||
if (index >= 0) {
|
||||
mGlobalStuffMap.removeAt(index);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return mGlobalStuffMap.values().removeAll(Collections.singleton(o));
|
||||
}
|
||||
|
||||
public static EhCookieStore getEhCookieStore(@NonNull Context context) {
|
||||
|
||||
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 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.ehviewer.preference;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Parcel;
|
||||
import android.preference.Preference;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Toast;
|
||||
|
||||
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.EhEngine;
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo;
|
||||
import com.hippo.ehviewer.download.DownloadManager;
|
||||
import com.hippo.ehviewer.spider.SpiderQueen;
|
||||
import com.hippo.unifile.UniFile;
|
||||
import com.hippo.yorozuya.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public class RestoreDownloadPreference extends TaskPreference {
|
||||
|
||||
public RestoreDownloadPreference(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public RestoreDownloadPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public RestoreDownloadPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Task onCreateTask() {
|
||||
return new RestoreTask(getContext());
|
||||
}
|
||||
|
||||
private static class RestoreTask extends Task {
|
||||
|
||||
private final EhApplication mApplication;
|
||||
private final DownloadManager mManager;
|
||||
private final OkHttpClient mHttpClient;
|
||||
|
||||
public RestoreTask(@NonNull Context context) {
|
||||
super(context);
|
||||
mApplication = (EhApplication) context.getApplicationContext();
|
||||
mManager = EhApplication.getDownloadManager(mApplication);
|
||||
mHttpClient = EhApplication.getOkHttpClient(mApplication);
|
||||
}
|
||||
|
||||
private RestoreItem getRestoreItem(UniFile file) {
|
||||
if (null == file || !file.isDirectory()) {
|
||||
return null;
|
||||
}
|
||||
UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
|
||||
if (null == siFile) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = siFile.openInputStream();
|
||||
// Skip start page
|
||||
IOUtils.readAsciiLine(is);
|
||||
long gid = Long.parseLong(IOUtils.readAsciiLine(is));
|
||||
if (mManager.containDownloadInfo(gid)) {
|
||||
return null;
|
||||
}
|
||||
String token = IOUtils.readAsciiLine(is);
|
||||
RestoreItem restoreItem = new RestoreItem();
|
||||
restoreItem.gid = gid;
|
||||
restoreItem.token = token;
|
||||
restoreItem.dirname = file.getName();
|
||||
return restoreItem;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doInBackground(Void... params) {
|
||||
UniFile dir = Settings.getDownloadLocation();
|
||||
if (null == dir) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<RestoreItem> restoreItemList = new ArrayList<>();
|
||||
|
||||
UniFile[] files = dir.listFiles();
|
||||
for (UniFile file: files) {
|
||||
RestoreItem restoreItem = getRestoreItem(file);
|
||||
if (null != restoreItem) {
|
||||
restoreItemList.add(restoreItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == restoreItemList.size()) {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
try {
|
||||
return EhEngine.fillGalleryListByApi(null, mHttpClient, new ArrayList<GalleryInfo>(restoreItemList));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void onPostExecute(Object o) {
|
||||
if (!(o instanceof List)) {
|
||||
Toast.makeText(mApplication, R.string.settings_download_restore_failed, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
List<RestoreItem> list = (List<RestoreItem>) o;
|
||||
if (list.isEmpty()) {
|
||||
Toast.makeText(mApplication, R.string.settings_download_restore_not_found, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
int count = 0;
|
||||
for (int i = 0, n = list.size(); i < n; i++) {
|
||||
RestoreItem item = list.get(i);
|
||||
// Avoid failed gallery info
|
||||
if (null != item.title) {
|
||||
// Put to download
|
||||
mManager.addDownload(item, null);
|
||||
// Put download dir to DB
|
||||
EhDB.putDownloadDirname(item.gid, item.dirname);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
Toast.makeText(mApplication,
|
||||
mApplication.getString(R.string.settings_download_restore_successfully, count),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
||||
Preference preference = getPreference();
|
||||
if (null != preference) {
|
||||
Context context = preference.getContext();
|
||||
if (context instanceof Activity) {
|
||||
((Activity) context).setResult(Activity.RESULT_OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
super.onPostExecute(o);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RestoreItem extends GalleryInfo {
|
||||
|
||||
public String dirname;
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeString(this.dirname);
|
||||
}
|
||||
|
||||
public RestoreItem() {
|
||||
}
|
||||
|
||||
protected RestoreItem(Parcel in) {
|
||||
super(in);
|
||||
this.dirname = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<RestoreItem> CREATOR = new Creator<RestoreItem>() {
|
||||
@Override
|
||||
public RestoreItem createFromParcel(Parcel source) {
|
||||
return new RestoreItem(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestoreItem[] newArray(int size) {
|
||||
return new RestoreItem[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -22,45 +22,25 @@ import android.content.ActivityNotFoundException;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.hippo.app.ProgressDialog;
|
||||
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.EhClient;
|
||||
import com.hippo.ehviewer.client.EhRequest;
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo;
|
||||
import com.hippo.ehviewer.download.DownloadManager;
|
||||
import com.hippo.ehviewer.spider.SpiderQueen;
|
||||
import com.hippo.ehviewer.ui.DirPickerActivity;
|
||||
import com.hippo.unifile.UniFile;
|
||||
import com.hippo.yorozuya.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DownloadFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
|
||||
|
||||
private static final String TAG = DownloadFragment.class.getSimpleName();
|
||||
|
||||
public static final int REQUEST_CODE_PICK_IMAGE_DIR = 0;
|
||||
public static final int REQUEST_CODE_PICK_IMAGE_DIR_L = 1;
|
||||
|
||||
public static final String KEY_DOWNLOAD_LOCATION = "download_location";
|
||||
public static final String KEY_RESTORE_DOWNLOAD_ITEMS = "restore_download_items";
|
||||
|
||||
@Nullable
|
||||
private Preference mDownloadLocation;
|
||||
@@ -71,14 +51,12 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O
|
||||
addPreferencesFromResource(R.xml.download_settings);
|
||||
|
||||
mDownloadLocation = findPreference(KEY_DOWNLOAD_LOCATION);
|
||||
Preference restoreDownloadItems = findPreference(KEY_RESTORE_DOWNLOAD_ITEMS);
|
||||
|
||||
onUpdateDownloadLocation();
|
||||
|
||||
if (mDownloadLocation != null) {
|
||||
mDownloadLocation.setOnPreferenceClickListener(this);
|
||||
}
|
||||
restoreDownloadItems.setOnPreferenceClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -111,11 +89,6 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O
|
||||
showDirPickerDialogL();
|
||||
}
|
||||
return true;
|
||||
} else if (KEY_RESTORE_DOWNLOAD_ITEMS.equals(key)) {
|
||||
ProgressDialog dialog = ProgressDialog.show(getActivity(), null,
|
||||
getString(R.string.settings_download_restoring), true, false);
|
||||
new RestoreTask(dialog).execute();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -208,167 +181,4 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class RestoreTask extends AsyncTask<Void, Void, List<RestoreItem>> {
|
||||
|
||||
private final ProgressDialog mDialog;
|
||||
private final DownloadManager mManager;
|
||||
|
||||
public RestoreTask(ProgressDialog dialog) {
|
||||
mDialog = dialog;
|
||||
mManager = EhApplication.getDownloadManager(getActivity());
|
||||
}
|
||||
|
||||
private RestoreItem getRestoreItem(UniFile file) {
|
||||
if (null == file || !file.isDirectory()) {
|
||||
return null;
|
||||
}
|
||||
UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
|
||||
if (null == siFile) {
|
||||
return null;
|
||||
}
|
||||
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = siFile.openInputStream();
|
||||
// Skip start page
|
||||
IOUtils.readAsciiLine(is);
|
||||
long gid = Long.parseLong(IOUtils.readAsciiLine(is));
|
||||
if (mManager.containDownloadInfo(gid)) {
|
||||
return null;
|
||||
}
|
||||
String token = IOUtils.readAsciiLine(is);
|
||||
RestoreItem restoreItem = new RestoreItem();
|
||||
restoreItem.gid = gid;
|
||||
restoreItem.token = token;
|
||||
restoreItem.dirname = file.getName();
|
||||
return restoreItem;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<RestoreItem> doInBackground(Void... params) {
|
||||
UniFile dir = Settings.getDownloadLocation();
|
||||
if (null == dir) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<RestoreItem> restoreItemList = new ArrayList<>();
|
||||
|
||||
UniFile[] files = dir.listFiles();
|
||||
for (UniFile file: files) {
|
||||
RestoreItem restoreItem = getRestoreItem(file);
|
||||
if (null != restoreItem) {
|
||||
restoreItemList.add(restoreItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (0 == restoreItemList.size()) {
|
||||
return null;
|
||||
} else {
|
||||
return restoreItemList;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<RestoreItem> restoreItemList) {
|
||||
if (null == restoreItemList) {
|
||||
Toast.makeText(getActivity(), R.string.settings_download_restore_not_found,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
mDialog.dismiss();
|
||||
return;
|
||||
}
|
||||
|
||||
EhRequest request = new EhRequest();
|
||||
request.setMethod(EhClient.METHOD_FILL_GALLERY_LIST_BY_API);
|
||||
request.setArgs(new ArrayList<GalleryInfo>(restoreItemList));
|
||||
request.setCallback(new EhClient.Callback<List<GalleryInfo>>() {
|
||||
@Override
|
||||
public void onSuccess(List<GalleryInfo> galleryInfoList) {
|
||||
DownloadManager downloadManager = EhApplication.getDownloadManager(getActivity());
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0, n = galleryInfoList.size(); i < n; i++) {
|
||||
GalleryInfo galleryInfo = galleryInfoList.get(i);
|
||||
// Avoid failed gallery info
|
||||
if (null != galleryInfo.title) {
|
||||
// Put to download
|
||||
downloadManager.addDownload(galleryInfo, null);
|
||||
// Put download dir to DB
|
||||
if (galleryInfo instanceof RestoreItem) {
|
||||
EhDB.putDownloadDirname(galleryInfo.gid, ((RestoreItem) galleryInfo).dirname);
|
||||
} else {
|
||||
Log.w(TAG, "The GalleryInfo is not RestoreItem");
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(getActivity(),
|
||||
getString(R.string.settings_download_restore_successfully, count),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
|
||||
mDialog.dismiss();
|
||||
|
||||
getActivity().setResult(Activity.RESULT_OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
Toast.makeText(getActivity(), R.string.settings_download_restore_failed,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
mDialog.dismiss();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel() {
|
||||
mDialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
EhClient client = EhApplication.getEhClient(getActivity());
|
||||
client.execute(request);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RestoreItem extends GalleryInfo {
|
||||
|
||||
public String dirname;
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeString(this.dirname);
|
||||
}
|
||||
|
||||
public RestoreItem() {
|
||||
}
|
||||
|
||||
protected RestoreItem(Parcel in) {
|
||||
super(in);
|
||||
this.dirname = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<RestoreItem> CREATOR = new Creator<RestoreItem>() {
|
||||
@Override
|
||||
public RestoreItem createFromParcel(Parcel source) {
|
||||
return new RestoreItem(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestoreItem[] newArray(int size) {
|
||||
return new RestoreItem[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -144,7 +144,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct
|
||||
mSkipSigningIn.setOnClickListener(this);
|
||||
|
||||
Context context = getContext2();
|
||||
AssertUtils.assertNotNull(container);
|
||||
AssertUtils.assertNotNull(context);
|
||||
EhApplication application = (EhApplication) context.getApplicationContext();
|
||||
if (application.containGlobalStuff(mRequestId)) {
|
||||
mSigningIn = true;
|
||||
|
||||
Reference in new issue
Block a user