diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml index 025169fbe..5fefb2709 100644 --- a/res/values-zh-rCN/strings.xml +++ b/res/values-zh-rCN/strings.xml @@ -231,6 +231,9 @@ 读取超时 (ms) EH EH 站点最小连接间隔 (ms) + 其他 + 修正下载文件夹名 + 将下载文件夹名由 gid 改为 gid-title,不进行修正不会影响使用 关于 作者 diff --git a/res/xml/advanced_settings.xml b/res/xml/advanced_settings.xml index b685fba03..63f6fcf6a 100644 --- a/res/xml/advanced_settings.xml +++ b/res/xml/advanced_settings.xml @@ -59,5 +59,14 @@ android:defaultValue="0" /> + + + + + \ No newline at end of file diff --git a/src/com/hippo/ehviewer/AppContext.java b/src/com/hippo/ehviewer/AppContext.java index 36b3150d3..b39db1887 100644 --- a/src/com/hippo/ehviewer/AppContext.java +++ b/src/com/hippo/ehviewer/AppContext.java @@ -107,23 +107,14 @@ public class AppContext extends Application implements UncaughtExceptionHandler String[] list = downloadDir.list(); if (list != null && list.length != 0) { for (String str : list) { - // Change download dir name from gid-title to gid - File oldDir = new File(downloadDir, str); - if (!new File(oldDir, EhUtils.EH_DOWNLOAD_FILENAME).exists()) - continue; int index = str.indexOf('-'); if (index == -1) continue; String gid = str.substring(0, index); - File newDir = new File(downloadDir, gid); - if (!oldDir.renameTo(newDir)) { - // if delete error, might same gid and different title - Utils.deleteDirInThread(oldDir); - continue; - } + File gDir = new File(downloadDir, str); // Move download info file File oldInfoFile = new File(oldEdInfoDir, gid); - File newInfoFile = new File(newDir, EhUtils.EH_DOWNLOAD_FILENAME); + File newInfoFile = new File(gDir, EhUtils.EH_DOWNLOAD_FILENAME); try { FileOutputStream fos = new FileOutputStream(newInfoFile); // Add last page @@ -139,7 +130,6 @@ public class AppContext extends Application implements UncaughtExceptionHandler Utils.deleteDirInThread(oldEdInfoDir); } - // Update version code try { PackageInfo pi= getPackageManager().getPackageInfo(getPackageName(), 0); diff --git a/src/com/hippo/ehviewer/ehclient/DetailParser.java b/src/com/hippo/ehviewer/ehclient/DetailParser.java index 4b6fdf90b..a68193049 100644 --- a/src/com/hippo/ehviewer/ehclient/DetailParser.java +++ b/src/com/hippo/ehviewer/ehclient/DetailParser.java @@ -27,6 +27,7 @@ import com.hippo.ehviewer.data.Comment; import com.hippo.ehviewer.data.LargePreviewList; import com.hippo.ehviewer.data.NormalPreviewList; import com.hippo.ehviewer.data.PreviewList; +import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.Utils; public class DetailParser { @@ -153,7 +154,7 @@ public class DetailParser { thumb = Utils.unescapeXml(m.group(1)); title = Utils.unescapeXml(m.group(2)); title_jpn = Utils.unescapeXml(m.group(3)); - category = EhClient.getType(m.group(4)); + category = EhUtils.getCategory(m.group(4)); uploader = m.group(5); posted = m.group(6); pages = Integer.parseInt(m.group(7)); diff --git a/src/com/hippo/ehviewer/ehclient/EhClient.java b/src/com/hippo/ehviewer/ehclient/EhClient.java index 92936c950..d32b91905 100644 --- a/src/com/hippo/ehviewer/ehclient/EhClient.java +++ b/src/com/hippo/ehviewer/ehclient/EhClient.java @@ -36,15 +36,17 @@ import android.os.Process; import com.hippo.ehviewer.Analytics; import com.hippo.ehviewer.AppHandler; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.data.ApiGalleryInfo; import com.hippo.ehviewer.data.Comment; import com.hippo.ehviewer.data.GalleryDetail; import com.hippo.ehviewer.data.GalleryInfo; import com.hippo.ehviewer.data.GalleryPopular; -import com.hippo.ehviewer.data.ListUrls; import com.hippo.ehviewer.data.LofiGalleryDetail; import com.hippo.ehviewer.data.PreviewList; import com.hippo.ehviewer.network.HttpHelper; +import com.hippo.ehviewer.util.BgThread; import com.hippo.ehviewer.util.Config; +import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.Utils; public class EhClient { @@ -885,7 +887,7 @@ public class EhClient { public void comment(final String detailUrl, final String comment, final OnCommentListener listener) { - new Thread(new Runnable() { + new BgThread(new Runnable() { @Override public void run() { HttpHelper hp = new HttpHelper(mContext); @@ -1090,56 +1092,6 @@ public class EhClient { }).start(); } - private static final int[] CATEGORY_VALUES = { - ListUrls.MISC, - ListUrls.DOUJINSHI, - ListUrls.MANGA, - ListUrls.ARTIST_CG, - ListUrls.GAME_CG, - ListUrls.IMAGE_SET, - ListUrls.COSPLAY, - ListUrls.ASIAN_PORN, - ListUrls.NON_H, - ListUrls.WESTERN, - ListUrls.UNKNOWN - }; - - // TODO How about "Private" - private static final String[][] CATEGORY_STRINGS = { - new String[]{"misc"}, - new String[]{"doujinshi"}, - new String[]{"manga"}, - new String[]{"artistcg", "Artist CG Sets"}, - new String[]{"gamecg", "Game CG Sets"}, - new String[]{"imageset", "Image Sets"}, - new String[]{"cosplay"}, - new String[]{"asianporn", "Asian Porn"}, - new String[]{"non-h"}, - new String[]{"western"}, - new String[]{"unknown"} - }; - - public static int getType(String type) { - int i; - for (i = 0; i < CATEGORY_STRINGS.length - 1; i++) { - for (String str : CATEGORY_STRINGS[i]) - if (str.equalsIgnoreCase(type)) - return CATEGORY_VALUES[i]; - } - - return CATEGORY_VALUES[i]; - } - - public static String getType(int type) { - int i; - for (i = 0; i < CATEGORY_VALUES.length - 1; i++) { - if (CATEGORY_VALUES[i] == type) - break; - } - return CATEGORY_STRINGS[i][0]; - } - - // modifyFavorite public interface OnModifyFavoriteListener { void onSuccess(List gis, int pageNum); @@ -1173,7 +1125,7 @@ public class EhClient { */ public void modifyFavorite(final int[] gids, final int dstCat, final int srcCat, final OnModifyFavoriteListener listener) { - new Thread(new Runnable() { + new BgThread(new Runnable() { @Override public void run() { int i; @@ -1231,7 +1183,7 @@ public class EhClient { listener.onFailure(e.getMessage()); return; } - new Thread(new Runnable() { + new BgThread(new Runnable() { @Override public void run() { HttpHelper hp = new HttpHelper(mContext); @@ -1262,7 +1214,7 @@ public class EhClient { gi.title = Utils.unescapeXml(j.getString("title")); gi.posted = Utils.sDate.format(Long.parseLong(j.getString("posted")) * 1000); gi.thumb = j.getString("thumb"); - gi.category = getType(j.getString("category")); + gi.category = EhUtils.getCategory(j.getString("category")); gi.uploader = j.getString("uploader"); gi.rating = Float.parseFloat(j.getString("rating")); gi.generateSLang(); @@ -1290,183 +1242,163 @@ public class EhClient { }).start(); } - - /* - private static class GetGalleryMetadataPackage { - public Map lmds; - public OnGetGalleryMetadataListener listener; - - public GetGalleryMetadataPackage(Map lmds, - OnGetGalleryMetadataListener listener) { - this.lmds = lmds; - this.listener = listener; - } + public interface OnGetApiGalleryInfoListener { + public void onSuccess(Object checkFlag, ApiGalleryInfo[] agiArray); + public void onFailure(Object checkFlag, String eMsg); } - private static Handler getGalleryMetadataHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - OnGetGalleryMetadataListener listener = - ((GetGalleryMetadataPackage) msg.obj).listener; - Map lmds = - ((GetGalleryMetadataPackage) msg.obj).lmds; - if (lmds == null || lmds.size() == 0) - listener.onFailure(msg.what); - else - listener.onSuccess(lmds); - }; - }; + private class GetApiGalleryInfoResponder implements Runnable { + private final boolean isOk; + private final OnGetApiGalleryInfoListener listener; + private final Object checkFlag; + private final ApiGalleryInfo[] agiArray; + private final String eMesg; - private static class GetGalleryMetadataRunnable implements Runnable { - private String[][] gidTokens; - private OnGetGalleryMetadataListener listener; - - public GetGalleryMetadataRunnable(String[][] gidTokens, - OnGetGalleryMetadataListener listener) { - this.gidTokens = gidTokens; + public GetApiGalleryInfoResponder(OnGetApiGalleryInfoListener listener, + Object checkFlag, ApiGalleryInfo[] agiArray) { + this.isOk = true; this.listener = listener; + this.checkFlag = checkFlag; + this.agiArray = agiArray; + this.eMesg = null; + } + + public GetApiGalleryInfoResponder(OnGetApiGalleryInfoListener listener, + Object checkFlag, String eMsg) { + this.isOk = false; + this.listener = listener; + this.checkFlag = checkFlag; + this.agiArray = null; + this.eMesg = eMsg; } @Override public void run() { - int errorMessageId = R.string.em_unknown_error; - Map lmds = new HashMap(); + if (isOk) + listener.onSuccess(checkFlag, agiArray); + else + listener.onFailure(checkFlag, eMesg); + } + } - // Create post string - StringBuffer strBuf = new StringBuffer(); - strBuf.append("{\"method\": \"gdata\",\"gidlist\": ["); - for (String[] item: gidTokens) - strBuf.append("[" + item[0] + ",\"" + item[1] + "\"],"); - strBuf.delete(strBuf.length()-1, strBuf.length()); - strBuf.append("]}"); - StringBuffer sb = new StringBuffer(); - errorMessageId = postJson(API_URL, strBuf.toString(),sb); - if (sb.length() != 0) { - String pageContent = sb.toString(); + private ApiGalleryInfo Json2Agi(JSONObject js) throws JSONException { + ApiGalleryInfo agi = new ApiGalleryInfo(); + agi.gid = js.getInt("gid"); + agi.token = js.getString("token"); + agi.archiver_key = js.getString("archiver_key"); + agi.title = js.getString("title"); + agi.title_jpn = js.getString("title_jpn"); + agi.category = EhUtils.getCategory(js.getString("category")); + agi.thumb = js.getString("thumb"); + agi.uploader = js.getString("uploader"); + agi.posted = Utils.sDate.format(Long.parseLong(js.getString("posted")) * 1000); + agi.filecount = js.getString("filecount"); + agi.filesize = js.getLong("filesize"); + agi.expunged = js.getBoolean("expunged"); + agi.rating = Float.parseFloat(js.getString("rating")); + agi.torrentcount = js.getString("torrentcount"); + JSONArray tags = js.getJSONArray("tags"); + agi.apiTags = new String[tags.length()]; + for (int j = 0; j < tags.length(); j++) + agi.apiTags[j] = tags.getString(j); + return agi; + } + + public void getApiGalleryInfo(final Object checkFlag, final int[] gids, final String[] tokens, final OnGetApiGalleryInfoListener l) { + new BgThread() { + @Override + public void run() { + GetApiGalleryInfoResponder responder; + ApiGalleryInfo[] agiArray; + int requstLen; + // Create json + JSONObject json = new JSONObject(); try { - SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd hh:mm"); - JSONObject jsonObject = new JSONObject(pageContent); - JSONArray jsonArray = jsonObject.getJSONArray("gmetadata"); - for (int i = 0; i < jsonArray.length(); i++) { - JSONObject jo = jsonArray.getJSONObject(i); - if (jo.getString("error") != null) - continue; - ListMangaDetail lmd = new ListMangaDetail(); - lmd.gid = Long.toString(jo.getLong("gid")); - lmd.token = jo.getString("token"); - lmd.archiver_key = jo.getString("archiver_key"); - lmd.title = jo.getString("title"); - lmd.title_jpn = jo.getString("title_jpn"); - lmd.category = getType(jo.getString("category")); - lmd.thumb = jo.getString("thumb"); - lmd.uploader = jo.getString("uploader"); - lmd.posted = fm.format(Long.parseLong(jo.getString("posted")) * 1000); - lmd.filecount = jo.getString("filecount"); - lmd.filesize = jo.getLong("filesize"); - lmd.expunged = jo.getBoolean("expunged"); - lmd.rating = jo.getString("rating"); - lmd.torrentcount = jo.getString("torrentcount"); - JSONArray ja = jo.getJSONArray("tags"); - int length = ja.length(); - lmd.tags = new String[1][length+1]; - lmd.tags[0][0] = "all"; - for (int j = 1; j < length+1; j++) - lmd.tags[0][j] = ja.getString(j); - - lmds.put(lmd.gid, lmd); + json.put("method", "gdata"); + JSONArray jsonA = new JSONArray(); + requstLen = Math.min(gids.length, tokens.length); + agiArray = new ApiGalleryInfo[requstLen]; + for (int i = 0; i < requstLen; i++) { + JSONArray ja = new JSONArray(); + ja.put(gids[i]); + ja.put(tokens[i]); + jsonA.put(ja); } - } catch (JSONException e) { - e.printStackTrace(); - errorMessageId = R.string.em_parser_error; + json.put("gidlist", jsonA); + } catch (Throwable e) { + responder = new GetApiGalleryInfoResponder(l, null, "Create json error"); + mHandler.post(responder); + return; } + // Post + HttpHelper hh = new HttpHelper(mContext); + String body = hh.postJson(getApiUrl(), json); + if (body != null) { + try { + json = new JSONObject(body); + JSONArray jsonA = (JSONArray) json.get("gmetadata"); + for (int i = 0; i < jsonA.length(); i++) { + ApiGalleryInfo agi = Json2Agi(jsonA.getJSONObject(i)); + // Add to agiArray + for (int j = 0; j < requstLen; j++) { + if (agi.gid == gids[j]) + agiArray[j] = agi; + } + } + responder = new GetApiGalleryInfoResponder(l, checkFlag, agiArray); + } catch (Throwable e) { + responder = new GetApiGalleryInfoResponder(l, checkFlag, "Json error"); + } + } else { + responder = new GetApiGalleryInfoResponder(l, checkFlag, hh.getEMsg()); + } + mHandler.post(responder); } - Message msg = new Message(); - msg.what = errorMessageId; - msg.obj = new GetGalleryMetadataPackage(lmds, listener); - getGalleryMetadataHandler.sendMessage(msg); - } + }.start(); } - public static void getGalleryMetadata(String[][] gidTokens, - OnGetGalleryMetadataListener listener) { - new Thread(new GetGalleryMetadataRunnable(gidTokens, - listener)).start(); - } - - - - private static class GetGalleryTokensPackage { - public Map tokens; - public OnGetGalleryTokensListener listener; - - public GetGalleryTokensPackage(Map tokens, - OnGetGalleryTokensListener listener) { - this.tokens = tokens; - this.listener = listener; + public ApiGalleryInfo[] getApiGalleryInfo(int[] gids, String[] tokens) { + ApiGalleryInfo[] agiArray; + int requstLen; + // Create json + JSONObject json = new JSONObject(); + try { + json.put("method", "gdata"); + JSONArray jsonA = new JSONArray(); + requstLen = Math.min(gids.length, tokens.length); + agiArray = new ApiGalleryInfo[requstLen]; + for (int i = 0; i < requstLen; i++) { + JSONArray ja = new JSONArray(); + ja.put(gids[i]); + ja.put(tokens[i]); + jsonA.put(ja); + } + json.put("gidlist", jsonA); + } catch (Throwable e) { + return null; } - } - - private static Handler getGalleryTokensHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - OnGetGalleryTokensListener listener = - ((GetGalleryTokensPackage) msg.obj).listener; - Map tokens = - ((GetGalleryTokensPackage) msg.obj).tokens; - if (tokens == null || tokens.size() == 0) - listener.onFailure(msg.what); - else - listener.onSuccess(tokens); - }; - }; - - - private static class GetGalleryTokensRunnable implements Runnable { - private String gid; - private String token; - private String[] pages; - private OnGetGalleryMetadataListener listener; - - public GetGalleryTokensRunnable(String gid, String token, String[] pages, - OnGetGalleryMetadataListener listener) { - this.gid = gid; - this.token = token; - this.pages = pages; - this.listener = listener; - } - - @Override - public void run() { - int errorMessageId = R.string.em_unknown_error; - Map tokens = new HashMap(); - - // Create post string - StringBuffer strBuf = new StringBuffer(); - strBuf.append("{\"method\": \"gtoken\",\"pagelist\": ["); - for (String item: pages) - strBuf.append("[" + gid + ",\"" + token + "\"," + item + "],"); - strBuf.delete(strBuf.length()-1, strBuf.length()); - strBuf.append("]}"); - - Log.i(TAG, strBuf.toString()); - - StringBuffer sb = new StringBuffer(); - errorMessageId = postJson(API_URL, strBuf.toString(),sb); - if (sb.length() != 0) { - String pageContent = sb.toString(); - Log.d(TAG, pageContent); + // Post api + HttpHelper hh = new HttpHelper(mContext); + String body = hh.postJson(getApiUrl(), json); + if (body == null) { + return null; + } else { + try { + json = new JSONObject(body); + JSONArray jsonA = (JSONArray) json.get("gmetadata"); + for (int i = 0; i < jsonA.length(); i++) { + ApiGalleryInfo agi = Json2Agi(jsonA.getJSONObject(i)); + // Add to agiArray + for (int j = 0; j < requstLen; j++) { + if (agi.gid == gids[j]) + agiArray[j] = agi; + } + } + return agiArray; + } catch (Throwable e) { + return null; } } } - - */ - - /* - private static void getGalleryTokens(String gid, String token, String[] pages, - OnGetGalleryMetadataListener listener) { - new Thread(new GetGalleryTokensRunnable(gid, token, pages, - listener)).start(); - }*/ - } diff --git a/src/com/hippo/ehviewer/ehclient/ExDownloader.java b/src/com/hippo/ehviewer/ehclient/ExDownloader.java index d28c229d5..8d1127cd2 100644 --- a/src/com/hippo/ehviewer/ehclient/ExDownloader.java +++ b/src/com/hippo/ehviewer/ehclient/ExDownloader.java @@ -135,7 +135,7 @@ public class ExDownloader implements Runnable { mControlorArray = new HttpHelper.DownloadControlor[mWorkerNum]; // Make sure dir - mDir = EhUtils.getGalleryDir(mGid); + mDir = EhUtils.getGalleryDir(mGid, mTitle); Utils.ensureDir(mDir, true); } @@ -427,9 +427,9 @@ public class ExDownloader implements Runnable { } catch (Throwable e) {} } - public static int readCurReadIndex(int gid) { + public static int readCurReadIndex(int gid, String title) { try { - RandomAccessFile raf = new RandomAccessFile(new File(EhUtils.getGalleryDir(gid), + RandomAccessFile raf = new RandomAccessFile(new File(EhUtils.getGalleryDir(gid, title), EhUtils.EH_DOWNLOAD_FILENAME), "r"); int index = Integer.parseInt(raf.readLine(), 16); raf.close(); diff --git a/src/com/hippo/ehviewer/ehclient/ListParser.java b/src/com/hippo/ehviewer/ehclient/ListParser.java index cddd266d5..f18599336 100644 --- a/src/com/hippo/ehviewer/ehclient/ListParser.java +++ b/src/com/hippo/ehviewer/ehclient/ListParser.java @@ -22,6 +22,7 @@ import java.util.regex.Pattern; import com.hippo.ehviewer.data.GalleryInfo; import com.hippo.ehviewer.data.LofiGalleryInfo; +import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.Utils; public class ListParser { @@ -84,7 +85,7 @@ public class ListParser { } else { continue; } - lgi.category = EhClient.getType(m.group(5)); + lgi.category = EhUtils.getCategory(m.group(5)); String tags = m.group(6); if (tags.equals("-")) lgi.lofiTags = null; @@ -147,7 +148,7 @@ public class ListParser { while (m.find()) { GalleryInfo gi = new GalleryInfo(); - gi.category = EhClient.getType(m.group(1)); + gi.category = EhUtils.getCategory(m.group(1)); gi.posted = m.group(2); if (m.group(3) == null) { gi.thumb = Utils.unescapeXml("http://" diff --git a/src/com/hippo/ehviewer/gallery/GalleryView.java b/src/com/hippo/ehviewer/gallery/GalleryView.java index a36f0330f..f341b7d1a 100644 --- a/src/com/hippo/ehviewer/gallery/GalleryView.java +++ b/src/com/hippo/ehviewer/gallery/GalleryView.java @@ -1158,38 +1158,40 @@ public class GalleryView extends GLView case SCROLL_ANIME_RIGHT: return false; case SCROLL_NONE: - boolean changePage = false; + // If it is true, page will move + boolean movePage = false; Rect rect = curShowItem.mRect; - // Check change page or not - if (curShowItem == null || !(curShowItem instanceof BasicItem)) - changePage = true; - else{ - if ( Math.abs(totalX/totalY) > 1 && ((totalX > SLIDE_SENSITIVITY && dx < 0 && rect.left >= 0) - || (totalX < -SLIDE_SENSITIVITY && dx > 0 && rect.right <= mScreenWidth))) - changePage = true; + // If show item is null or TextItem, just move + // Check finger move left or right + // Check min sensitivity + if (curShowItem == null || !(curShowItem instanceof BasicItem) || + (Math.abs(totalX/totalY) > 1 && ((totalX > SLIDE_SENSITIVITY && dx < 0 && rect.left >= 0) + || (totalX < -SLIDE_SENSITIVITY && dx > 0 && rect.right <= mScreenWidth)))) { + movePage = true; } - if (changePage) { // If change page + if (movePage) { // If change page if (dx < 0) { // Go to left if (!isFirstPage()) { // Not first page scrollXOffset = 0; mScrollState = SCROLL_LEFT; + // Prepare previous show item resetSizePosition(PRE_TARGET_INDEX); } else { - changePage = false; + movePage = false; } - } - else { // Go to righ + } else { // Go to righ if (!isLastPage()) { // Not last page scrollXOffset = 0; mScrollState = SCROLL_RIGHT; + // Prepare next show item resetSizePosition(NEXT_TARGET_INDEX); } else { - changePage = false; + movePage = false; } } } - if (!changePage){ // Move cur image + if (!movePage) { // Move cur image int actDx = -(int)dx; int actDy = -(int)dy; if (rect.width() <= mScreenWidth) diff --git a/src/com/hippo/ehviewer/gallery/data/ImageSet.java b/src/com/hippo/ehviewer/gallery/data/ImageSet.java index 5933544c4..95dd10323 100644 --- a/src/com/hippo/ehviewer/gallery/data/ImageSet.java +++ b/src/com/hippo/ehviewer/gallery/data/ImageSet.java @@ -88,7 +88,7 @@ public class ImageSet implements ExDownloader.ListenerForImageSet { public ImageSet(int gid, String token, String title, int startIndex) { - mDir = EhUtils.getGalleryDir(gid); + mDir = EhUtils.getGalleryDir(gid, title); Utils.ensureDir(mDir, true); // Init ExDownloader diff --git a/src/com/hippo/ehviewer/ui/DownloadActivity.java b/src/com/hippo/ehviewer/ui/DownloadActivity.java index 226d3cfff..b645ee402 100644 --- a/src/com/hippo/ehviewer/ui/DownloadActivity.java +++ b/src/com/hippo/ehviewer/ui/DownloadActivity.java @@ -360,7 +360,7 @@ public class DownloadActivity extends AbsActivity { mServiceConn.getService().delete(mDownloadInfo); // Delete dir GalleryInfo gi = mDownloadInfo.galleryInfo; - Utils.deleteDirInThread(EhUtils.getGalleryDir(gi.gid)); + Utils.deleteDirInThread(EhUtils.getGalleryDir(gi.gid, gi.title)); } return true; } diff --git a/src/com/hippo/ehviewer/ui/GalleryActivity.java b/src/com/hippo/ehviewer/ui/GalleryActivity.java index 899b9f91a..c3f7c4012 100644 --- a/src/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/src/com/hippo/ehviewer/ui/GalleryActivity.java @@ -201,7 +201,7 @@ public class GalleryActivity extends AbsActivity mTitle = intent.getStringExtra(KEY_TITLE); int startIndex = intent.getIntExtra(KEY_START_INDEX, 0); if (startIndex == -1) - startIndex = ExDownloader.readCurReadIndex(mGid); + startIndex = ExDownloader.readCurReadIndex(mGid, mTitle); if (mGid == -1 || token == null || mTitle == null) { new MaterialAlertDialog.Builder(this).setTitle(R.string.error) diff --git a/src/com/hippo/ehviewer/ui/SettingsActivity.java b/src/com/hippo/ehviewer/ui/SettingsActivity.java index 547e14af0..ceb6a7761 100644 --- a/src/com/hippo/ehviewer/ui/SettingsActivity.java +++ b/src/com/hippo/ehviewer/ui/SettingsActivity.java @@ -16,7 +16,9 @@ package com.hippo.ehviewer.ui; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -56,8 +58,10 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.SimpleSuggestionProvider; import com.hippo.ehviewer.UpdateHelper; import com.hippo.ehviewer.app.MaterialAlertDialog; +import com.hippo.ehviewer.data.ApiGalleryInfo; import com.hippo.ehviewer.data.Data; import com.hippo.ehviewer.data.DownloadInfo; +import com.hippo.ehviewer.ehclient.EhClient; import com.hippo.ehviewer.ehclient.EhInfo; import com.hippo.ehviewer.network.HttpHelper; import com.hippo.ehviewer.preference.ListPreference; @@ -65,6 +69,7 @@ import com.hippo.ehviewer.util.BgThread; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.Favorite; +import com.hippo.ehviewer.util.Log; import com.hippo.ehviewer.util.Theme; import com.hippo.ehviewer.util.Ui; import com.hippo.ehviewer.util.Utils; @@ -772,17 +777,20 @@ public class SettingsActivity extends AbsPreferenceActivity { } public static class AdvancedFragment extends TranslucentPreferenceFragment - implements Preference.OnPreferenceChangeListener { + implements Preference.OnPreferenceChangeListener, + Preference.OnPreferenceClickListener { private static final String KEY_HTTP_RETRY = "http_retry"; private static final String KEY_HTTP_CONNECT_TIMEOUT = "http_connect_timeout"; private static final String KEY_HTTP_READ_TIMEOUT = "http_read_timeout"; private static final String KEY_EH_MIN_INTERVAL = "eh_min_interval"; + private static final String KEY_FIX_DIRNAME = "fix_dirname"; private Preference mHttpRetry; private Preference mHttpConnectTimeout; private Preference mHttpReadTimeout; private Preference mEhMinInterval; + private Preference mFixDirname; @Override public void onCreate(Bundle savedInstanceState) { @@ -797,6 +805,8 @@ public class SettingsActivity extends AbsPreferenceActivity { mHttpReadTimeout.setOnPreferenceChangeListener(this); mEhMinInterval = findPreference(KEY_EH_MIN_INTERVAL); mEhMinInterval.setOnPreferenceChangeListener(this); + mFixDirname = findPreference(KEY_FIX_DIRNAME); + mFixDirname.setOnPreferenceClickListener(this); } @Override @@ -846,6 +856,110 @@ public class SettingsActivity extends AbsPreferenceActivity { return true; } + + @Override + public boolean onPreferenceClick(Preference preference) { + final String key = preference.getKey(); + if (KEY_FIX_DIRNAME.equals(key)) { + mFixDirname.setEnabled(false); + new BgThread() { + @Override + public void run() { + File downloadDir = new File(Config.getDownloadPath()); + List targetDirList = new ArrayList(); + + String[] list = downloadDir.list(); + if (list != null) { + for (String str: list) { + if (str.contains("-")) continue; + File dir = new File(downloadDir, str); + if (new File(dir, EhUtils.EH_DOWNLOAD_FILENAME).exists()) + targetDirList.add(dir); + } + } + + if (targetDirList.size() == 0) { + AppHandler.getInstance().post(new Runnable() { + @Override + public void run() { + mFixDirname.setEnabled(true); + MaterialToast.showToast("未发现可修正项"); // TODO + } + }); + return; + } else { + AppHandler.getInstance().post(new Runnable() { + @Override + public void run() { + MaterialToast.showToast("开始修正"); // TODO + } + }); + } + + int handleNum = 0; + List dirs = new ArrayList(); + List gids = new ArrayList(); + List tokens = new ArrayList(); + // Do get title from api + for (int i = 0; i < targetDirList.size(); i++) { + File dir = targetDirList.get(i); + File info = new File(dir, EhUtils.EH_DOWNLOAD_FILENAME); + InputStream is = null; + int gid; + String token; + try { + is = new BufferedInputStream(new FileInputStream(info), + 128); + // skip read index + Utils.readAsciiLine(is); + gid = Integer.parseInt(Utils.readAsciiLine(is)); + token = Utils.readAsciiLine(is); + Utils.closeQuietly(is); + dirs.add(dir); + gids.add(gid); + tokens.add(token); + // Post api when to 25 or the end + if (gids.size() == 25 || i == targetDirList.size() - 1) { + ApiGalleryInfo[] agiArray = EhClient.getInstance().getApiGalleryInfo( + Utils.toIntArray(gids), tokens.toArray(new String[tokens.size()])); + // Change dirname + if (agiArray != null) { + for (int j = 0; j < agiArray.length; j++) { + ApiGalleryInfo agi = agiArray[j]; + if (agi != null) { + handleNum++; + + Log.d(TAG, dirs.get(j).getName()); + Log.d(TAG, EhUtils.generateGalleryDir(agi.gid, agi.title).getName()); + + dirs.get(j).renameTo(EhUtils.generateGalleryDir(agi.gid, agi.title)); + } + } + } + dirs.clear(); + gids.clear(); + tokens.clear(); + // Wait for CD, if not last + if (i != targetDirList.size() - 1) + Thread.sleep(3000); + } + } catch (Throwable e) { + continue; + } + } + final int _handleNum = handleNum; + AppHandler.getInstance().post(new Runnable() { + @Override + public void run() { + mFixDirname.setEnabled(true); + MaterialToast.showToast("共修正 " + _handleNum + " 项"); + } + }); + } + }.start(); + } + return true; + } } public static class AboutFragment extends TranslucentPreferenceFragment diff --git a/src/com/hippo/ehviewer/util/Config.java b/src/com/hippo/ehviewer/util/Config.java index ca0a67612..5db2e7702 100644 --- a/src/com/hippo/ehviewer/util/Config.java +++ b/src/com/hippo/ehviewer/util/Config.java @@ -111,6 +111,14 @@ public class Config { mConfigPre.edit().putInt(key, value).apply(); } + public static boolean getBoolean(String key, boolean defValue) { + return mConfigPre.getBoolean(key, defValue); + } + + public static void setBoolean(String key, boolean value) { + mConfigPre.edit().putBoolean(key, value).apply(); + } + /****** For Normal Config ******/ private static final String KEY_ALLOWED = "allowed"; diff --git a/src/com/hippo/ehviewer/util/EhUtils.java b/src/com/hippo/ehviewer/util/EhUtils.java index 708c68aff..792bc835a 100644 --- a/src/com/hippo/ehviewer/util/EhUtils.java +++ b/src/com/hippo/ehviewer/util/EhUtils.java @@ -18,19 +18,88 @@ package com.hippo.ehviewer.util; import java.io.File; +import com.hippo.ehviewer.data.ListUrls; public final class EhUtils { public static final String EH_DOWNLOAD_FILENAME = ".ehviewer"; + private static final int[] CATEGORY_VALUES = { + ListUrls.MISC, + ListUrls.DOUJINSHI, + ListUrls.MANGA, + ListUrls.ARTIST_CG, + ListUrls.GAME_CG, + ListUrls.IMAGE_SET, + ListUrls.COSPLAY, + ListUrls.ASIAN_PORN, + ListUrls.NON_H, + ListUrls.WESTERN, + ListUrls.UNKNOWN + }; + + // TODO How about "Private" + private static final String[][] CATEGORY_STRINGS = { + new String[]{"misc"}, + new String[]{"doujinshi"}, + new String[]{"manga"}, + new String[]{"artistcg", "Artist CG Sets"}, + new String[]{"gamecg", "Game CG Sets"}, + new String[]{"imageset", "Image Sets"}, + new String[]{"cosplay"}, + new String[]{"asianporn", "Asian Porn"}, + new String[]{"non-h"}, + new String[]{"western"}, + new String[]{"unknown"} + }; + + public static int getCategory(String type) { + int i; + for (i = 0; i < CATEGORY_STRINGS.length - 1; i++) { + for (String str : CATEGORY_STRINGS[i]) + if (str.equalsIgnoreCase(type)) + return CATEGORY_VALUES[i]; + } + + return CATEGORY_VALUES[i]; + } + + public static String getCategory(int type) { + int i; + for (i = 0; i < CATEGORY_VALUES.length - 1; i++) { + if (CATEGORY_VALUES[i] == type) + break; + } + return CATEGORY_STRINGS[i][0]; + } + /** * Get gallery for read and download + * Gid is constant, but title may be changed + * 非要标题,记一下 gid 不行么 + * * @param gid + * @param title * @return */ - public static File getGalleryDir(int gid) { - return new File(Config.getDownloadPath(), + public static File getGalleryDir(int gid, String title) { + + File downloadDir = new File(Config.getDownloadPath()); + File gDir = new File(downloadDir, Utils.standardizeFilename(Integer.toString(gid))); + + if (gDir.exists()) + return gDir; + else + return generateGalleryDir(gid, title); + + // TODO check a dir start with the gid, but it may take a long time + // Create a map to record all ? + } + + public static File generateGalleryDir(int gid, String title) { + return new File(Config.getDownloadPath(), + Utils.standardizeFilename(gid + "-" + title)); } /** diff --git a/src/com/hippo/ehviewer/util/Utils.java b/src/com/hippo/ehviewer/util/Utils.java index c9604b39d..a4a5f0272 100644 --- a/src/com/hippo/ehviewer/util/Utils.java +++ b/src/com/hippo/ehviewer/util/Utils.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Locale; @@ -634,4 +635,15 @@ public final class Utils { return defaultValue; } } + + public static int[] toIntArray(Collection collection) { + Object[] boxedArray = collection.toArray(); + int len = boxedArray.length; + int[] array = new int[len]; + for (int i = 0; i < len; i++) { + // checkNotNull for GWT (do not optimize) + array[i] = ((Number) (boxedArray[i])).intValue(); + } + return array; + } } diff --git a/src/com/hippo/ehviewer/widget/LinkifyTextView.java b/src/com/hippo/ehviewer/widget/LinkifyTextView.java index 9b86e3eb5..b635eda10 100644 --- a/src/com/hippo/ehviewer/widget/LinkifyTextView.java +++ b/src/com/hippo/ehviewer/widget/LinkifyTextView.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.widget; +import android.annotation.SuppressLint; import android.content.Context; import android.text.Layout; import android.text.Spannable; @@ -27,43 +28,44 @@ import android.view.MotionEvent; import android.widget.TextView; public class LinkifyTextView extends TextView { - + private String mUrl; - + public LinkifyTextView(Context context) { super(context); init(); } - + public LinkifyTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } - + public LinkifyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } - + public void init() { // Just make urls striking // LinkifyTextView itself should not handle touch url event setAutoLinkMask(Linkify.WEB_URLS); setLinksClickable(false); } - + public String getTouchedUrl() { return mUrl; } - + /** * Call it when you do not need the url any more */ public void clearTouchedUrl() { mUrl = null; } - + @Override + @SuppressLint("ClickableViewAccessibility") public boolean onTouchEvent(MotionEvent event) { if (getText() instanceof Spannable) { // Get this code from android.text.method.LinkMovementMethod. @@ -71,19 +73,19 @@ public class LinkifyTextView extends TextView { int x = (int) event.getX(); int y = (int) event.getY(); boolean isGetUrl = false; - + x -= getTotalPaddingLeft(); y -= getTotalPaddingTop(); - + x += getScrollX(); y += getScrollY(); - + Layout layout = getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); - + ClickableSpan[] links = ((Spannable)getText()).getSpans(off, off, ClickableSpan.class); - + for (ClickableSpan link : links) { if (link instanceof URLSpan) { mUrl = ((URLSpan)link).getURL(); @@ -96,7 +98,7 @@ public class LinkifyTextView extends TextView { } else { mUrl = null; } - + return super.onTouchEvent(event); } }