允许下载原图

This commit is contained in:
seven332 committed 2014-12-20 03:47:09 +08:00
1 parent a9363487be
commit fa3d55d341
9 files changed
+251 -88

No files matched your search

+4 -2
View File
@@ -108,8 +108,8 @@
<string name="download_6">下载成功 %1$d 项, 下载失败 %2$d 项</string>
<string name="delete_item">该下载项将被删除</string>
<string name="pic_included">包括图片</string>
<string name="notification_be_title">下载流量不足</string>
<string name="notification_be_mesg">错误码 509,请稍后完成下载</string>
<string name="notification_be_title">下载配额不足</string>
<string name="notification_be_mesg">错误码 509,请稍后阅读或完成下载</string>
<!-- ErrorMessage -->
<string name="em_network_error">网络错误</string>
@@ -255,6 +255,8 @@
<string name="media_scan_summary_on">请避免他人翻看你的图库应用</string>
<string name="media_scan_summary_off">大多数图库应用将不会显示下载目录中的图片</string>
<string name="download_thread_title">下载线程数</string>
<string name="download_origin_image_title">下载原图</string>
<string name="download_origin_image_summary">这很危险!勾选此项会导致下载配额迅速流失</string>
<string name="keep_download_service_title">保持下载服务</string>
<string name="keep_download_service_summary_on">下载时,若应用被系统或用户强制关闭,下载服务会重新启动</string>
<string name="keep_download_service_summary_off">下载时,若应用被系统或用户强制关闭,下载服务不会重新启动</string>
+6
View File
@@ -38,6 +38,12 @@
auto:etpMin="1"
auto:etpMax="10" />
<CheckBoxPreference
android:key="download_origin_image"
android:title="@string/download_origin_image_title"
android:summary="@string/download_origin_image_summary"
android:defaultValue="false" />
<CheckBoxPreference
android:key="keep_download_service"
android:title="@string/keep_download_service_title"
+17
View File
@@ -202,6 +202,23 @@ public class StartActivity extends AbsActivity {
}
}, 2000);
check(CHECK_WARING);
/*
HttpHelper hh = new HttpHelper(StartActivity.this);
hh.downloadInThread("http://exhentai.org/fullimg.php?gid=767110&page=15&key=84c2b5f9aa", new File("/sdcard"), "aaa.txt", false, null, null);
new Thread() {
@Override
public void run() {
//
}
}.start();
*/
}
/**
@@ -526,7 +526,7 @@ public final class ExDownloader implements Runnable {
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_stat_eh).setContentTitle(context.getString(R.string.notification_be_title))
.setContentText("")
.setContentText(context.getString(R.string.notification_be_mesg))
.setOngoing(false).setAutoCancel(true);
notifyManager.notify(BE_NOTIFY_ID, mBuilder.build());
}
@@ -732,51 +732,96 @@ public final class ExDownloader implements Runnable {
}
// Parser image page
boolean refreshImageUrl = false;
for (int i = 0; i < 2 && !mStopWork && !mPauseWork; i++) {
hh.reset();
ipp.reset();
String body = hh.get(EhClient.getPageUrl(mGid, pageToken, targetIndex + 1, mMode)
+ (i == 1 ? "?nl=48" : ""));
+ (refreshImageUrl ? "?nl=48" : ""));
if (ipp.parser(body, mMode)) {
// Download image
HttpHelper.DownloadControlor c = mControlorArray[mIndex];
String imageUrl = ipp.imageUrl;
//Check 509 gif
boolean get509 = false;
int length = URL_ARRAY_509.length;
for (int j = 0; j < length; j++) {
if (URL_ARRAY_509[j].equals(imageUrl)) {
// Get 509 gif here
get509 = true;
if (Config.getDownloadOriginImage() && ipp.originalImageUrl != null) {
Log.d(TAG, "Download origin image");
// Download orgin image
String imageUrl = ipp.originalImageUrl;
// Guess filename
String filename = EhUtils.getImageFilename(targetIndex, "jpg");
// Download
hh.reset();
if (hh.downloadOriginEhImage(imageUrl, mDir, filename, false, c, listener) == null) {
if (hh.getException() instanceof HttpHelper.BandwidthExceededException) {
// Get 509
show509Notification(mContext);
break;
} else {
String newImageUrl = hh.getLastUrl();
if (newImageUrl != null) {
// Try download from proxy
hh.reset();
if (hh.downloadEhImage(newImageUrl, mDir, filename, true, c, listener) == null) {
// Download failed
Log.d(TAG, "Download failed");
} else {
// Download successful
Log.d(TAG, "Download successful");
}
} else {
// Download failed
Log.d(TAG, "Download failed");
}
}
} else {
// Download successful
Log.d(TAG, "Download successful");
}
// No need to refresh image url
break;
} else {
// Download scale image
String imageUrl = ipp.imageUrl;
//Check 509 gif
boolean get509 = false;
int length = URL_ARRAY_509.length;
for (int j = 0; j < length; j++) {
if (URL_ARRAY_509[j].equals(imageUrl)) {
// Get 509 gif here
get509 = true;
break;
}
}
if (get509) {
show509Notification(mContext);
break;
}
}
if (get509) {
show509Notification(mContext);
break;
}
String filename = EhUtils.getImageFilename(targetIndex, Utils.getExtension(imageUrl, "jpg"));
// Just put filename to mImageFilenameArray
mImageFilenameArray.set(targetIndex, filename);
hh.reset();
if (hh.download(imageUrl, mDir, filename, false, c, listener) == null) {
// Guess filename
String filename = EhUtils.getImageFilename(targetIndex, Utils.getExtension(imageUrl, "jpg"));
// Don't put into mImageFilenameArray, because extension maybe be wrong
// mImageFilenameArray.set(targetIndex, filename);
hh.reset();
if (hh.download(imageUrl, mDir, filename, true, c, listener) == null) {
if (i == 1) {
// TODO download error
Log.d(TAG, "download error");
if (hh.downloadEhImage(imageUrl, mDir, filename, false, c, listener) == null) {
hh.reset();
if (hh.downloadEhImage(imageUrl, mDir, filename, true, c, listener) == null) {
if (i == 1) {
// TODO download error
Log.d(TAG, "download error");
}
} else {
// download ok
Log.d(TAG, "download ok proxy");
break;
}
} else {
// download ok
Log.d(TAG, "download ok proxy");
Log.d(TAG, "download ok");
break;
}
} else {
// download ok
Log.d(TAG, "download ok");
break;
}
} else {
// TODO parser error, Do somthing
@@ -24,11 +24,13 @@ import com.hippo.ehviewer.util.Utils;
public class ImagePageParser {
public String imageUrl;
public String originalImageUrl;
public int gid;
public String token;
public void reset() {
imageUrl = null;
originalImageUrl = null;
gid = 0;
token = null;
}
@@ -63,11 +65,17 @@ public class ImagePageParser {
+ ".+?"
+ "<div id=\"i5\"><div class=\"sb\"><a href=\"(?:http|https)://.+?/g/(\\d+)/(\\w+)");
m = p.matcher(body);
if (m.find()) {
imageUrl = Utils.unescapeXml(m.group(1));
gid = Integer.parseInt(m.group(2));
token = m.group(3);
p = Pattern.compile("<a href=\"([^\"]+)fullimg.php([^\"]+)\">");
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;
+106 -9
View File
@@ -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;
}
}
+7
View File
@@ -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);
}
+13 -1
View File
@@ -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;
}
}
+15 -46
View File
@@ -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();
}
}
*/
}