From fa3d55d3414ae4741d84f9b398d99f88de7c63d9 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 20 Dec 2014 03:47:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E4=B8=8B=E8=BD=BD=E5=8E=9F?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/values-zh-rCN/strings.xml | 6 +- res/xml/download_settings.xml | 6 + src/com/hippo/ehviewer/StartActivity.java | 17 +++ .../hippo/ehviewer/ehclient/ExDownloader.java | 103 +++++++++++----- .../ehviewer/ehclient/ImagePageParser.java | 10 +- .../hippo/ehviewer/network/HttpHelper.java | 115 ++++++++++++++++-- src/com/hippo/ehviewer/util/Config.java | 7 ++ src/com/hippo/ehviewer/util/EhUtils.java | 14 ++- src/com/hippo/ehviewer/util/Utils.java | 61 +++------- 9 files changed, 251 insertions(+), 88 deletions(-) diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml index fa13eed93..ad79ccd2c 100644 --- a/res/values-zh-rCN/strings.xml +++ b/res/values-zh-rCN/strings.xml @@ -108,8 +108,8 @@ 下载成功 %1$d 项, 下载失败 %2$d 项 该下载项将被删除 包括图片 - 下载流量不足 - 错误码 509,请稍后完成下载 + 下载配额不足 + 错误码 509,请稍后阅读或完成下载 网络错误 @@ -255,6 +255,8 @@ 请避免他人翻看你的图库应用 大多数图库应用将不会显示下载目录中的图片 下载线程数 + 下载原图 + 这很危险!勾选此项会导致下载配额迅速流失 保持下载服务 下载时,若应用被系统或用户强制关闭,下载服务会重新启动 下载时,若应用被系统或用户强制关闭,下载服务不会重新启动 diff --git a/res/xml/download_settings.xml b/res/xml/download_settings.xml index 7bbe5149b..92f8508cd 100644 --- a/res/xml/download_settings.xml +++ b/res/xml/download_settings.xml @@ -38,6 +38,12 @@ auto:etpMin="1" auto:etpMax="10" /> + +
"); + m = p.matcher(body); + if (m.find()) { + originalImageUrl = Utils.unescapeXml( + new StringBuilder(m.group(1)).append("fullimg.php").append(m.group(2)).toString()); + } return true; } else { return false; diff --git a/src/com/hippo/ehviewer/network/HttpHelper.java b/src/com/hippo/ehviewer/network/HttpHelper.java index 27ee3708b..fc12bb64f 100644 --- a/src/com/hippo/ehviewer/network/HttpHelper.java +++ b/src/com/hippo/ehviewer/network/HttpHelper.java @@ -45,6 +45,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Message; +import android.webkit.MimeTypeMap; import com.hippo.ehviewer.AppHandler; import com.hippo.ehviewer.R; @@ -54,6 +55,7 @@ import com.hippo.ehviewer.exception.StopRequestException; import com.hippo.ehviewer.util.BgThread; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.util.Constants; +import com.hippo.ehviewer.util.EhUtils; import com.hippo.ehviewer.util.FastByteArrayOutputStream; import com.hippo.ehviewer.util.Log; import com.hippo.ehviewer.util.MathUtils; @@ -173,6 +175,10 @@ public class HttpHelper { return getEMsg(mContext, mException); } + public Exception getException() { + return mException; + } + public static String getEMsg(Context c, Exception e) { if (e == null) return c.getString(R.string.em_unknown_error); @@ -234,7 +240,7 @@ public class HttpHelper { int redirectionCount = 0; boolean firstTime = true; try { - url = rh.getUrl(); + url = (mLastUrl == null ? rh.getUrl() : new URL(mLastUrl)); isCookiable = isUrlCookiable(url); @@ -251,7 +257,6 @@ public class HttpHelper { } } - Log.d(TAG, "Requst " + url.toString()); while (redirectionCount++ < Constants.MAX_REDIRECTS) { conn = (HttpURLConnection) url.openConnection(); @@ -275,8 +280,9 @@ public class HttpHelper { case HttpURLConnection.HTTP_PARTIAL: // Test sad panda if (url.getHost().equals(EhInfo.EX_HOST)) { - String contentType = conn.getHeaderField("Content-Type"); - if (contentType != null && contentType.equals("image/gif")) + String contentType = conn.getContentType(); + if (contentType != null && contentType.equals("image/gif") + && conn.getContentLength() == 9615) throw new SadPandaException(); } // Store cookie if necessary @@ -747,7 +753,7 @@ public class HttpHelper { private final String mUrl; private final File mDir; - private final String mFileName; + private String mFileName; private File mFile; private File mTempFile; private final boolean mIsProxy; @@ -821,16 +827,43 @@ public class HttpHelper { return contentLength; } + protected String fixNewExtension(String ext) { + return ext; + } + @Override public Object onAfterConnect(HttpURLConnection conn) throws Exception { // Check stop if (mControlor != null && mControlor.isStop()) throw new StopRequestException(); + String contentType = conn.getContentType(); + int index = contentType.indexOf(';'); + if (index != -1) + contentType = contentType.substring(0, index); + mContentLength = getContentLength(conn); if (mListener != null) mListener.onDownloadStartDownload(mContentLength); + // Fix extension + MimeTypeMap mime = MimeTypeMap.getSingleton(); + String newExtension = mime.getExtensionFromMimeType(contentType); + if (newExtension == null) { + index = contentType.lastIndexOf('/'); + if (index != -1) + newExtension = contentType.substring(index + 1); + } + newExtension = fixNewExtension(newExtension); + if (newExtension != null) { + index = mFileName.lastIndexOf('.'); + if (index == -1) { + mFileName = mFileName + '.' + newExtension; + } else { + mFileName = mFileName.substring(0, index) + '.' + newExtension; + } + } + // Make sure parent exist mDir.mkdirs(); mFile = new File(mDir, mFileName); @@ -838,7 +871,6 @@ public class HttpHelper { // Transfer transferData(conn.getInputStream(), new FileOutputStream(mTempFile)); // Get ok, rename - // TODO what if rename fail mTempFile.renameTo(mFile); // Callback if (mListener != null) @@ -882,11 +914,53 @@ public class HttpHelper { } if (mContentLength != -1 && mReceivedSize != mContentLength) - throw new UncompletedException(); + throw new UncompletedException("Received size is " + mReceivedSize + + ", but ContentLength is " + mContentLength); } } + + private class DownloadEhImageHelper extends DownloadHelper { + + public DownloadEhImageHelper(String url, File dir, String fileName, + boolean isProxy, DownloadControlor controlor, + OnDownloadListener listener) { + super(url, dir, fileName, isProxy, controlor, listener); + } + + @Override + protected String fixNewExtension(String ext) { + int length = EhUtils.POSSIBLE_IMAGE_EXTENSION_ARRAY.length; + int i; + for (i = 0; i < length; i++) { + if (EhUtils.POSSIBLE_IMAGE_EXTENSION_ARRAY[i].equals(ext)) + break; + } + if (i == length) + ext = EhUtils.DEFAULT_IMAGE_EXTENSION; + return ext; + } + } + + private class DownloadOriginEhImageHelper extends DownloadEhImageHelper { + + public DownloadOriginEhImageHelper(String url, File dir, String fileName, + boolean isProxy, DownloadControlor controlor, + OnDownloadListener listener) { + super(url, dir, fileName, isProxy, controlor, listener); + } + + @Override + public Object onAfterConnect(HttpURLConnection conn) throws Exception { + if (mLastUrl == null) + throw new BandwidthExceededException(); + + return super.onAfterConnect(conn); + } + } + + /** * Check Sad Panda. * If get Sad Panda, return null, @@ -942,12 +1016,12 @@ public class HttpHelper { * @return */ public Bitmap getImage(String url) { - return (Bitmap)requst(new GetImageHelper(url)); + return (Bitmap) requst(new GetImageHelper(url)); } public String download(String url, File dir, String file, boolean isProxy, DownloadControlor controlor, OnDownloadListener listener) { - return (String)requst(new DownloadHelper(url, dir, file, isProxy, + return (String) requst(new DownloadHelper(url, dir, file, isProxy, controlor, listener)); } @@ -962,6 +1036,18 @@ public class HttpHelper { }.start(); } + public String downloadEhImage(String url, File dir, String file, boolean isProxy, + DownloadControlor controlor, OnDownloadListener listener) { + return (String) requst(new DownloadEhImageHelper(url, dir, file, isProxy, + controlor, listener)); + } + + public String downloadOriginEhImage(String url, File dir, String file, boolean isProxy, + DownloadControlor controlor, OnDownloadListener listener) { + return (String) requst(new DownloadOriginEhImageHelper(url, dir, file, isProxy, + controlor, listener)); + } + /** Exceptions **/ public class SadPandaException extends Exception { @@ -977,6 +1063,11 @@ public class HttpHelper { } public class UncompletedException extends Exception { + + public UncompletedException(String string) { + super(string); + } + private static final long serialVersionUID = 1L; } @@ -999,4 +1090,10 @@ public class HttpHelper { return mResponseCode; } } + + public class BandwidthExceededException extends Exception { + + private static final long serialVersionUID = 1L; + + } } diff --git a/src/com/hippo/ehviewer/util/Config.java b/src/com/hippo/ehviewer/util/Config.java index d062a75f9..4a26fb1d8 100644 --- a/src/com/hippo/ehviewer/util/Config.java +++ b/src/com/hippo/ehviewer/util/Config.java @@ -459,6 +459,9 @@ public final class Config { private static final String KEY_DOWNLOAD_THREAD = "download_thread"; private static final int DEFAULT_DOWNLOAD_THREAD = 3; + private static final String KEY_DOWNLOAD_ORIGIN_IMAGE = "download_origin_image"; + private static final boolean DEFAULT_DOWNLOAD_ORIGIN_IMAGE = false; + private static final String KEY_KEEP_DOWNLOAD_SERVICE = "keep_download_service"; private static final boolean DEFAULT_KEEP_DOWNLOAD_SERVICE = true; @@ -478,6 +481,10 @@ public final class Config { return getIntFromStr(KEY_DOWNLOAD_THREAD, DEFAULT_DOWNLOAD_THREAD); } + public static boolean getDownloadOriginImage() { + return getBoolean(KEY_DOWNLOAD_ORIGIN_IMAGE, DEFAULT_DOWNLOAD_ORIGIN_IMAGE); + } + public static boolean getKeepDownloadService() { return getBoolean(KEY_KEEP_DOWNLOAD_SERVICE, DEFAULT_KEEP_DOWNLOAD_SERVICE); } diff --git a/src/com/hippo/ehviewer/util/EhUtils.java b/src/com/hippo/ehviewer/util/EhUtils.java index b880cdc16..1544845e0 100644 --- a/src/com/hippo/ehviewer/util/EhUtils.java +++ b/src/com/hippo/ehviewer/util/EhUtils.java @@ -104,9 +104,21 @@ public final class EhUtils { return String.format("%08d.%s", index + 1, extension); } + public static String[] POSSIBLE_IMAGE_EXTENSION_ARRAY = new String[] { + "jpg", "jpeg", "png", "gif" + }; + + public static String DEFAULT_IMAGE_EXTENSION = POSSIBLE_IMAGE_EXTENSION_ARRAY[0]; + public static String[] getPossibleImageFilenames(int index) { String prefix = String.format("%08d.", index + 1); - return new String[] { prefix + "jpg", prefix + "jpeg", prefix + "png", prefix + "gif" }; + + int length = POSSIBLE_IMAGE_EXTENSION_ARRAY.length; + String[] strs = new String[length]; + for (int i = 0; i < length; i++) + strs[i] = prefix + POSSIBLE_IMAGE_EXTENSION_ARRAY[i]; + + return strs; } } diff --git a/src/com/hippo/ehviewer/util/Utils.java b/src/com/hippo/ehviewer/util/Utils.java index c3bc7abab..6787204a0 100644 --- a/src/com/hippo/ehviewer/util/Utils.java +++ b/src/com/hippo/ehviewer/util/Utils.java @@ -39,6 +39,7 @@ import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Build; import android.os.Vibrator; +import android.text.TextUtils; import android.webkit.MimeTypeMap; import com.hippo.ehviewer.AppHandler; @@ -611,21 +612,6 @@ public final class Utils { } } - /** - * Get extension from target string, int lower case - * - * @param name - * @param defautl - * @return - */ - public static String getExtension(String name, String defautl) { - int index = name.lastIndexOf('.'); - if (index == -1 || index == name.length() - 1) - return defautl; - else - return name.substring(index + 1).toLowerCase(); - } - /** * Return true if point is in the rect * @@ -699,6 +685,20 @@ public final class Utils { return object; } + /** + * Get extension from target string, int lower case + * + * @param name + * @param defautl + * @return + */ + public static String getExtension(String name, String defautl) { + String ext = MimeTypeMap.getFileExtensionFromUrl(name); + if (TextUtils.isEmpty(ext)) + ext = defautl; + return ext; + } + /** * It will return null if can't find mime * @@ -732,35 +732,4 @@ public final class Utils { return ""; } } - - /* - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - try { - - - sNavBarOverride = - } catch (Throwable e) { - sNavBarOverride = null; - } - } - - - public static boolean hasNavBar(Context context) { - Resources res = context.getResources(); - int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android"); - if (resourceId != 0) { - boolean hasNav = res.getBoolean(resourceId); - // check override flag (see static block) - if ("1".equals(sNavBarOverride)) { - hasNav = false; - } else if ("0".equals(sNavBarOverride)) { - hasNav = true; - } - return hasNav; - } else { // fallback - return !ViewConfiguration.get(context).hasPermanentMenuKey(); - } - } - - */ }