diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index 385e29295..39ef25ecf 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -43,7 +43,7 @@ public class EhClient { public static final int METHOD_SIGN_IN = 0; public static final int METHOD_GET_GALLERY_LIST = 1; - public static final int METHOD_GET_FILL_GALLERY_LIST_BY_API = 2; + public static final int METHOD_FILL_GALLERY_LIST_BY_API = 2; public static final int METHOD_GET_GALLERY_DETAIL = 3; public static final int METHOD_GET_LARGE_PREVIEW_SET = 4; public static final int METHOD_GET_RATE_GALLERY = 5; @@ -151,7 +151,7 @@ public class EhClient { return EhEngine.signIn(this, mOkHttpClient, (String) params[0], (String) params[1]); case METHOD_GET_GALLERY_LIST: return EhEngine.getGalleryList(this, mOkHttpClient, (String) params[0], (Boolean) params[1]); - case METHOD_GET_FILL_GALLERY_LIST_BY_API: + case METHOD_FILL_GALLERY_LIST_BY_API: return EhEngine.fillGalleryListByApi(this, mOkHttpClient, (List) params[0]); case METHOD_GET_GALLERY_DETAIL: return EhEngine.getGalleryDetail(this, mOkHttpClient, (String) params[0]); diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index 578b7fe48..e53f35fc7 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -162,7 +162,7 @@ public class EhEngine { } // At least, GalleryInfo contain valid gid and token - public static Void fillGalleryListByApi(EhClient.Task task, OkHttpClient okHttpClient, + public static List fillGalleryListByApi(EhClient.Task task, OkHttpClient okHttpClient, List galleryInfoList) throws Exception { // We can only request 25 items one time at most final int MAX_REQUEST_SIZE = 25; @@ -174,7 +174,7 @@ public class EhEngine { requestItems.clear(); } } - return null; + return galleryInfoList; } private static void doFillGalleryListByApi(EhClient.Task task, OkHttpClient okHttpClient, 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 1df69f756..f489eb6dd 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -345,6 +345,42 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { } } + public void addDownload(GalleryInfo galleryInfo, @Nullable String label) { + if (containDownloadInfo(galleryInfo.gid)) { + // Contain + return; + } + + // It is new download info + DownloadInfo info = new DownloadInfo(galleryInfo); + info.label = label; + info.state = DownloadInfo.STATE_NONE; + info.time = System.currentTimeMillis(); + + // Add to label download list + LinkedList list = getInfoListForLabel(info.label); + if (list == null) { + Log.e(TAG, "Can't find download info list with label: " + label); + return; + } + list.add(info); + + // Add to all download list and map + mAllInfoMap.put(galleryInfo.gid, info); + + // Add to wait list + mWaitList.add(info); + + // Save to + EhDB.putDownloadInfo(info); + + // Notify + for (DownloadInfoListener l: mDownloadInfoListeners) { + l.onAdd(info, list, list.size() - 1); + } + } + + void stopDownload(long gid) { DownloadInfo info = stopDownloadInternal(gid); if (info != null) { diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java index 54f7b313c..ca792223f 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java @@ -103,7 +103,7 @@ public class SpiderQueen implements Runnable { private static final int NUMBER_SPIDER_WORKER = 3; private static final int NUMBER_PRELOAD = 5; - private static final String SPIDER_INFO_FILENAME = ".ehviewer"; + public static final String SPIDER_INFO_FILENAME = ".ehviewer"; private static final String[] URL_509_SUFFIX_ARRAY = { "/509.gif", diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.java b/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.java index 2bf71f699..e1a4c1fd6 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/DownloadFragment.java @@ -22,6 +22,7 @@ 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.preference.Preference; @@ -30,10 +31,23 @@ import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.widget.Toast; +import com.hippo.app.ProgressDialog; +import com.hippo.ehviewer.EhApplication; 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 { @@ -41,8 +55,10 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O 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"; - private @Nullable Preference mDownloadLocation; + @Nullable + private Preference mDownloadLocation; @Override public void onCreate(Bundle savedInstanceState) { @@ -50,12 +66,20 @@ 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 + public void onDestroy() { + super.onDestroy(); + mDownloadLocation = null; } public void onUpdateDownloadLocation() { @@ -73,7 +97,6 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O public boolean onPreferenceClick(Preference preference) { String key = preference.getKey(); if (KEY_DOWNLOAD_LOCATION.equals(key)) { - int sdk = Build.VERSION.SDK_INT; if (sdk < Build.VERSION_CODES.KITKAT) { openDirPicker(); @@ -83,6 +106,11 @@ 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; } @@ -175,4 +203,121 @@ public class DownloadFragment extends PreferenceFragment implements Preference.O } } } + + + private class RestoreTask extends AsyncTask> { + + private final ProgressDialog mDialog; + private final DownloadManager mManager; + + public RestoreTask(ProgressDialog dialog) { + mDialog = dialog; + mManager = EhApplication.getDownloadManager(getActivity()); + } + + private GalleryInfo getGalleryInfo(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); + GalleryInfo galleryInfo = new GalleryInfo(); + galleryInfo.gid = gid; + galleryInfo.token = token; + return galleryInfo; + } catch (IOException e) { + return null; + } finally { + IOUtils.closeQuietly(is); + } + } + + @Override + protected List doInBackground(Void... params) { + UniFile dir = Settings.getDownloadLocation(); + if (null == dir) { + return null; + } + + List galleryInfoList = new ArrayList<>(); + + UniFile[] files = dir.listFiles(); + for (UniFile file: files) { + GalleryInfo galleryInfo = getGalleryInfo(file); + if (null != galleryInfo) { + galleryInfoList.add(galleryInfo); + } + } + + if (0 == galleryInfoList.size()) { + return null; + } else { + return galleryInfoList; + } + } + + @Override + protected void onPostExecute(List galleryInfoList) { + if (null == galleryInfoList) { + 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(galleryInfoList); + request.setCallback(new EhClient.Callback>() { + @Override + public void onSuccess(List 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) { + downloadManager.addDownload(galleryInfo, null); + count++; + } + } + + Toast.makeText(getActivity(), + getString(R.string.settings_download_restore_successfully, count), + Toast.LENGTH_SHORT).show(); + + mDialog.dismiss(); + } + + @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); + } + } } diff --git a/app/src/main/res/layout/dialog_gallery_menu.xml b/app/src/main/res/layout/dialog_gallery_menu.xml index 05e01281e..5f941b1e6 100644 --- a/app/src/main/res/layout/dialog_gallery_menu.xml +++ b/app/src/main/res/layout/dialog_gallery_menu.xml @@ -63,7 +63,7 @@ android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" - android:text="@string/settings_read_page_scaling_title" + android:text="@string/settings_read_page_scaling" android:textColor="@color/primary_text_dark"/> diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index d33bf7cad..d131d658e 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -280,48 +280,54 @@ 图片已保存至 %s - EH - 显示日文标题 + EH + 显示日文标题 尝试显示日文标题。这会使获取画廊列表花费更多时间 显示默认标题。一般是英文标题 - 阅读 + 阅读 阅读方向 从左至右 从右至左 从上至下 - 页面缩放 + 页面缩放 原始尺寸 匹配宽度 匹配长度 匹配屏幕 固定缩放 - 开页位置 + 开页位置 左上角 右上角 左下角 右下角 中心 - 屏幕常亮 - 显示时钟 - 显示电量 - 使用音量键翻页 + 屏幕常亮 + 显示时钟 + 显示电量 + 使用音量键翻页 - 下载 - 下载路径 + 下载 + 下载路径 无效的下载路径 无法获取下载路径 在 KitKat 中您无法把数据写入外置 SD 卡,除非您做了奇怪的事情 Lollipop 提供了新的写入外置 SD 卡的 API。如果您想把下载路径设置到外置 SD 卡上,点击 \"文档\" 按钮 文档 + 恢复下载项 + 恢复下载目录里的所有下载项 + 恢复中 + 未找到可恢复下载项 + 恢复失败 + 成功恢复 %s 项 - 关于 - 作者 - Google+ 社群 - 网站 - 源码 - 更新日志 - 开源许可 + 关于 + 作者 + Google+ 社群 + 网站 + 源码 + 更新日志 + 开源许可 捐赠 你的捐赠能帮助我开发更多更好的应用。\n\n支付宝:seven332$163.com 启用数据统计 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index d23e0f904..6768f78be 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -280,48 +280,54 @@ 圖片已保存至 %s - EH - 顯示日文標題 + EH + 顯示日文標題 嘗試顯示日文標題。這會使獲取畫廊列表花費更多時間 顯示默認標題。一般是英文標題 - 閲讀 + 閲讀 閲讀方向 從左至右 從右至左 從上至下 - 頁面縮放 + 頁面縮放 原始尺寸 匹配寬度 匹配長度 匹配屏幕 固定縮放 - 開頁位置 + 開頁位置 左上角 右上角 左下角 右下角 中心 - 屏幕常亮 - 顯示時鐘 - 顯示電量 - 使用音量鍵翻頁 + 屏幕常亮 + 顯示時鐘 + 顯示電量 + 使用音量鍵翻頁 - 下載 - 下載路徑 + 下載 + 下載路徑 無效的下載路徑 無法獲取下載路徑 在 KitKat 中您無法把數據寫入外置 SD 卡,除非您做了奇怪的事情 Lollipop 提供了新的寫入外置 SD 卡的 API。如果您想把下載路徑設置到外置 SD 卡上,點擊 \"文檔\" 按鈕 文檔 + 恢復下載項 + 恢復下載目錄裏的所有下載項 + 恢復中 + 未找到可恢復下載項 + 恢復失敗 + 成功恢復 %s 項 - 關於 - 作者 - Google+ 社羣 - 網站 - 源碼 - 更新日誌 - 開源許可 + 關於 + 作者 + Google+ 社羣 + 網站 + 源碼 + 更新日誌 + 開源許可 捐贈 你的捐贈能幫助我開發更多更好的應用。\n\n支付寶:seven332$163.com 啟用數據統計 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index b188dacb2..8bab7ecb0 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -280,48 +280,54 @@ 圖片已儲存至 %s - EH - 顯示日文標題 + EH + 顯示日文標題 嘗試顯示日文標題。這會使獲取畫廊列表花費更多時間 顯示預設標題。一般是英文標題 - 閱讀 + 閱讀 閱讀方向 從左至右 從右至左 從上至下 - 頁面縮放 + 頁面縮放 原始尺寸 匹配寬度 匹配長度 匹配螢幕 固定縮放 - 開頁位置 + 開頁位置 左上角 右上角 左下角 右下角 中心 - 螢幕常亮 - 顯示時鐘 - 顯示電量 - 使用音量鍵翻頁 + 螢幕常亮 + 顯示時鐘 + 顯示電量 + 使用音量鍵翻頁 - 下載 - 下載路徑 + 下載 + 下載路徑 無效的下載路徑 無法獲取下載路徑 在 KitKat 中您無法把資料寫入外接 SD 卡,除非您做了奇怪的事情 Lollipop 提供了新的寫入外接 SD 卡的 API。如果您想把下載路徑設定到外接 SD 卡上,點選 \"文件\" 按鈕 文件 + 恢復下載項 + 恢復下載目錄裡的所有下載項 + 恢復中 + 未找到可恢復下載項 + 恢復失敗 + 成功恢復 %s 項 - 關於 - 作者 - Google+ 社群 - 網站 - 源碼 - 更新日誌 - 開源許可 + 關於 + 作者 + Google+ 社群 + 網站 + 源碼 + 更新日誌 + 開源許可 捐贈 你的捐贈能幫助我開發更多更好的應用。\n\n支付寶:seven332$163.com 啟用資料統計 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1afea6e8b..29b1b6855 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -319,49 +319,55 @@ Share image Image saved to %s - EH - Show Japanese title + EH + Show Japanese title Try to show Japanese title. It takes more time to get gallery list Show default title. It is usually English title - Read + Read Reading direction Left to right Right to left Top to bottom - Page scaling + Page scaling Actual size Fit to width Fit to height Fit to screen Fixed scale - Start position + Start position Top left Top right Bottom left Bottom right Center - Keep screen on - Show clock - Show battery - Use volume key to turn pages + Keep screen on + Show clock + Show battery + Use volume key to turn pages - Download - Download location + Download + Download location Invalid download location Can\'t get download location You can\'t write any kind of data on your external SD card in Android 4.4+ unless you have hacked it Android 5.0+ provides new API to write on external SD card. Click the \"DOCUMENT\" button if you want to set download location to SD card Document + Restore download items + Restore all download items in download location + Restoring + Not found download items to restore + Restore failed + Restore %d items successfully - About - Author + About + Author Hippo <ehviewersu$gmail.com> - Google+ Community - Website - Source - Changelog - License + Google+ Community + Website + Source + Changelog + License Donate Your donation can help me develop more and better applications.\n\nAlipay: seven332$163.com Enable analytics diff --git a/app/src/main/res/xml/about_settings.xml b/app/src/main/res/xml/about_settings.xml index 36d06d8e4..b91df5773 100644 --- a/app/src/main/res/xml/about_settings.xml +++ b/app/src/main/res/xml/about_settings.xml @@ -20,27 +20,27 @@ + android:title="@string/settings_about_author"/> + android:title="@string/settings_about_google_plus"/> + android:title="@string/settings_about_website"/> + android:title="@string/settings_about_source"/> + android:title="@string/settings_about_changelog"/> + android:title="@string/settings_about_license"/> + android:title="@string/settings_download_download_location"/> + + diff --git a/app/src/main/res/xml/eh_settings.xml b/app/src/main/res/xml/eh_settings.xml index 9c515f967..075c8af53 100644 --- a/app/src/main/res/xml/eh_settings.xml +++ b/app/src/main/res/xml/eh_settings.xml @@ -23,7 +23,7 @@ style="?android:attr/preferenceStyle" android:widgetLayout="@layout/preference_widget_fixed_switch" android:key="show_jpn_title" - android:title="@string/settings_eh_show_jpn_title_title" + android:title="@string/settings_eh_show_jpn_title" app:summaryOn="@string/settings_eh_show_jpn_title_summary_on" app:summaryOff="@string/settings_eh_show_jpn_title_summary_off" android:defaultValue="false"/> diff --git a/app/src/main/res/xml/read_settings.xml b/app/src/main/res/xml/read_settings.xml index 88bafbdee..dc118f15b 100644 --- a/app/src/main/res/xml/read_settings.xml +++ b/app/src/main/res/xml/read_settings.xml @@ -28,7 +28,7 @@ diff --git a/app/src/main/res/xml/settings_headers.xml b/app/src/main/res/xml/settings_headers.xml index 1d13cb4d7..5f20f6551 100644 --- a/app/src/main/res/xml/settings_headers.xml +++ b/app/src/main/res/xml/settings_headers.xml @@ -20,22 +20,22 @@