diff --git a/app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.java b/app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.java new file mode 100644 index 000000000..e3ed1d3a6 --- /dev/null +++ b/app/src/main/java/com/hippo/drawable/PreciselyClipDrawable.java @@ -0,0 +1,102 @@ +/* + * 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.drawable; + +import android.graphics.Canvas; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; +import android.support.v7.graphics.drawable.DrawableWrapper; + +import com.hippo.yorozuya.MathUtils; + +public class PreciselyClipDrawable extends DrawableWrapper { + + private final boolean mClip; + private RectF mScale; + private Rect mTemp; + + public PreciselyClipDrawable(Drawable drawable, int offsetX, int offsetY, int width, int height) { + super(drawable); + float originWidth = drawable.getIntrinsicWidth(); + float originHeight = drawable.getIntrinsicHeight(); + + if (originWidth <= 0 || originHeight <= 0) { + // Can not clip + mClip = false; + } else { + mClip = true; + mScale = new RectF(); + mScale.set(MathUtils.clamp(offsetX / originWidth, 0.0f, 1.0f), + MathUtils.clamp(offsetY / originHeight, 0.0f, 1.0f), + MathUtils.clamp((offsetX + width) / originWidth, 0.0f, 1.0f), + MathUtils.clamp((offsetY + height) / originHeight, 0.0f, 1.0f)); + mTemp = new Rect(); + } + } + + @Override + protected void onBoundsChange(Rect bounds) { + if (mClip) { + if (!mScale.isEmpty()) { + mTemp.left = (int) ((mScale.left * bounds.right - mScale.right * bounds.left) / + (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))); + mTemp.right = (int) (((1 - mScale.right) * bounds.left - (1 - mScale.left) * bounds.right) / + (mScale.left * (1 - mScale.right) - mScale.right * (1 - mScale.left))); + mTemp.top = (int) ((mScale.top * bounds.bottom - mScale.bottom * bounds.top) / + (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))); + mTemp.bottom = (int) (((1 - mScale.bottom) * bounds.top - (1 - mScale.top) * bounds.bottom) / + (mScale.top * (1 - mScale.bottom) - mScale.bottom * (1 - mScale.top))); + super.onBoundsChange(mTemp); + } + } else { + super.onBoundsChange(bounds); + } + } + + @Override + public int getIntrinsicWidth() { + if (mClip) { + return (int) (super.getIntrinsicWidth() * mScale.width()); + } else { + return super.getIntrinsicWidth(); + } + } + + @Override + public int getIntrinsicHeight() { + if (mClip) { + return (int) (super.getIntrinsicHeight() * mScale.height()); + } else { + return super.getIntrinsicHeight(); + } + } + + @Override + public void draw(Canvas canvas) { + if (mClip) { + if (!mScale.isEmpty()) { + int saveCount = canvas.save(); + canvas.clipRect(getBounds()); + super.draw(canvas); + canvas.restoreToCount(saveCount); + } + } else { + super.draw(canvas); + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.java b/app/src/main/java/com/hippo/ehviewer/EhApplication.java index 1519920de..b8e4e9eaa 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhApplication.java +++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.java @@ -36,7 +36,6 @@ import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhCookieStore; import com.hippo.ehviewer.client.EhEngine; import com.hippo.ehviewer.client.data.GalleryDetail; -import com.hippo.ehviewer.client.data.LargePreviewSet; import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.spider.SpiderDen; import com.hippo.ehviewer.ui.CommonOperations; @@ -75,8 +74,6 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx private BitmapHelper mBitmapHelper; private Conaco mConaco; private LruCache mGalleryDetailCache; - private LruCache mLargePreviewSetCache; - private LruCache mPreviewPagesCache; private SimpleDiskCache mSpiderInfoCache; private DownloadManager mDownloadManager; @@ -131,12 +128,6 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx if (null != mGalleryDetailCache) { mGalleryDetailCache.evictAll(); } - if (null != mLargePreviewSetCache) { - mLargePreviewSetCache.evictAll(); - } - if (null != mPreviewPagesCache) { - mPreviewPagesCache.evictAll(); - } } @Override @@ -269,26 +260,6 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx return application.mGalleryDetailCache; } - @NonNull - public static LruCache getLargePreviewSetCache(@NonNull Context context) { - EhApplication application = ((EhApplication) context.getApplicationContext()); - if (application.mLargePreviewSetCache == null) { - // Max size 50, 3 min timeout - application.mLargePreviewSetCache = new LruCache<>(50); - } - return application.mLargePreviewSetCache; - } - - @NonNull - public static LruCache getPreviewPagesCache(@NonNull Context context) { - EhApplication application = ((EhApplication) context.getApplicationContext()); - if (application.mPreviewPagesCache == null) { - // Max size 50, 3 min timeout - application.mPreviewPagesCache = new LruCache<>(50); - } - return application.mPreviewPagesCache; - } - @NonNull public static SimpleDiskCache getSpiderInfoCache(@NonNull Context context) { EhApplication application = ((EhApplication) context.getApplicationContext()); 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 fad013808..5957b2cd7 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -45,7 +45,7 @@ public class EhClient { public static final int METHOD_GET_GALLERY_LIST = 1; 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_PREVIEW_SET = 4; public static final int METHOD_GET_RATE_GALLERY = 5; public static final int METHOD_GET_COMMENT_GALLERY = 6; public static final int METHOD_GET_GALLERY_TOKEN = 7; @@ -155,8 +155,8 @@ public class EhClient { return EhEngine.fillGalleryListByApi(this, mOkHttpClient, (List) params[0]); case METHOD_GET_GALLERY_DETAIL: return EhEngine.getGalleryDetail(this, mOkHttpClient, (String) params[0]); - case METHOD_GET_LARGE_PREVIEW_SET: - return EhEngine.getLargePreviewSet(this, mOkHttpClient, (String) params[0]); + case METHOD_GET_PREVIEW_SET: + return EhEngine.getPreviewSet(this, mOkHttpClient, (String) params[0]); case METHOD_GET_RATE_GALLERY: return EhEngine.rateGallery(this, mOkHttpClient, (Long) params[0], (String) params[1], (Float) params[2]); case METHOD_GET_COMMENT_GALLERY: 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 65b269020..fcc4924e5 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -27,7 +27,7 @@ import com.hippo.ehviewer.Settings; import com.hippo.ehviewer.client.data.GalleryComment; import com.hippo.ehviewer.client.data.GalleryDetail; import com.hippo.ehviewer.client.data.GalleryInfo; -import com.hippo.ehviewer.client.data.LargePreviewSet; +import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.exception.CancelledException; import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.client.exception.ParseException; @@ -309,7 +309,7 @@ public class EhEngine { } - public static Pair getLargePreviewSet( + public static Pair getPreviewSet( EhClient.Task task, OkHttpClient okHttpClient, String url) throws Exception { Log.d(TAG, url); Request request = new EhRequestBuilder(url, task.getEhConfig()).build(); @@ -326,9 +326,8 @@ public class EhEngine { code = response.code(); headers = response.headers(); body = response.body().string(); - Document document = Jsoup.parse(body); - return Pair.create(GalleryDetailParser.parseLargePreviewSet(document, body), - GalleryDetailParser.parsePreviewPages(document, body)); + return Pair.create(GalleryDetailParser.parsePreviewSet(body), + GalleryDetailParser.parsePreviewPages(body)); } catch (Exception e) { throwException(call, code, headers, body, e); throw e; diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java index b399066ed..7a93ba09f 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryDetail.java @@ -37,8 +37,7 @@ public class GalleryDetail extends GalleryInfo { public GalleryTagGroup[] tags; public GalleryComment[] comments; public int previewPages; - public LargePreviewSet previewSet; - + public PreviewSet previewSet; @Override public int describeContents() { @@ -94,7 +93,7 @@ public class GalleryDetail extends GalleryInfo { this.comments = null; } this.previewPages = in.readInt(); - this.previewSet = in.readParcelable(LargePreviewSet.class.getClassLoader()); + this.previewSet = in.readParcelable(PreviewSet.class.getClassLoader()); } public static final Creator CREATOR = new Creator() { diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.java b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.java index 906fcf06a..6529481e2 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/GalleryPreview.java @@ -19,11 +19,27 @@ package com.hippo.ehviewer.client.data; import android.os.Parcel; import android.os.Parcelable; +import com.hippo.widget.LoadImageView; + public class GalleryPreview implements Parcelable { - public String imageUrl; - public String pageUrl; - public int index; + String imageKey; + String imageUrl; + String pageUrl; + int position; + int offsetX = Integer.MIN_VALUE; + int offsetY = Integer.MIN_VALUE; + int clipWidth = Integer.MIN_VALUE; + int clipHeight = Integer.MIN_VALUE; + + public int getPosition() { + return position; + } + + public void load(LoadImageView view) { + view.setClip(offsetX, offsetY, clipWidth, clipHeight); + view.load(imageKey, imageUrl); + } @Override public int describeContents() { @@ -34,7 +50,11 @@ public class GalleryPreview implements Parcelable { public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.imageUrl); dest.writeString(this.pageUrl); - dest.writeInt(this.index); + dest.writeInt(this.position); + dest.writeInt(this.offsetX); + dest.writeInt(this.offsetY); + dest.writeInt(this.clipWidth); + dest.writeInt(this.clipHeight); } public GalleryPreview() { @@ -43,14 +63,20 @@ public class GalleryPreview implements Parcelable { protected GalleryPreview(Parcel in) { this.imageUrl = in.readString(); this.pageUrl = in.readString(); - this.index = in.readInt(); + this.position = in.readInt(); + this.offsetX = in.readInt(); + this.offsetY = in.readInt(); + this.clipWidth = in.readInt(); + this.clipHeight = in.readInt(); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override public GalleryPreview createFromParcel(Parcel source) { return new GalleryPreview(source); } + @Override public GalleryPreview[] newArray(int size) { return new GalleryPreview[size]; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/LargePreviewSet.java b/app/src/main/java/com/hippo/ehviewer/client/data/LargePreviewSet.java index 2531c42bd..032a62c54 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/LargePreviewSet.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/LargePreviewSet.java @@ -19,38 +19,56 @@ package com.hippo.ehviewer.client.data; import android.os.Parcel; import android.os.Parcelable; +import com.hippo.ehviewer.client.EhCacheKeyFactory; +import com.hippo.widget.LoadImageView; import com.hippo.yorozuya.IntList; import java.util.ArrayList; -public class LargePreviewSet implements Parcelable { +public class LargePreviewSet extends PreviewSet { - private final IntList mIndexList; + private final IntList mPositionList; private final ArrayList mImageUrlList; private final ArrayList mPageUrlList; - public int size() { - return mImageUrlList.size(); - } - public void addItem(int index, String imageUrl, String pageUrl) { - mIndexList.add(index); + mPositionList.add(index); mImageUrlList.add(imageUrl); mPageUrlList.add(pageUrl); } - public int getIndexAt(int index) { - return mIndexList.get(index); + @Override + public int size() { + return mImageUrlList.size(); } - public String getImageUrlAt(int index) { - return mImageUrlList.get(index); + @Override + public int getPosition(int index) { + return mPositionList.get(index); } + @Override public String getPageUrlAt(int index) { return mPageUrlList.get(index); } + @Override + public GalleryPreview getGalleryPreview(long gid, int index) { + GalleryPreview galleryPreview = new GalleryPreview(); + galleryPreview.position = mPositionList.get(index); + galleryPreview.imageKey = EhCacheKeyFactory.getLargePreviewKey(gid, galleryPreview.position); + galleryPreview.imageUrl = mImageUrlList.get(index); + galleryPreview.pageUrl = mPageUrlList.get(index); + return galleryPreview; + } + + @Override + public void load(LoadImageView view, long gid, int index) { + view.resetClip(); + view.load(EhCacheKeyFactory.getLargePreviewKey(gid, mPositionList.get(index)), + mImageUrlList.get(index)); + } + @Override public int describeContents() { return 0; @@ -58,19 +76,19 @@ public class LargePreviewSet implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeParcelable(this.mIndexList, flags); + dest.writeParcelable(this.mPositionList, flags); dest.writeStringList(this.mImageUrlList); dest.writeStringList(this.mPageUrlList); } public LargePreviewSet() { - mIndexList = new IntList(); + mPositionList = new IntList(); mImageUrlList = new ArrayList<>(); mPageUrlList = new ArrayList<>(); } protected LargePreviewSet(Parcel in) { - this.mIndexList = in.readParcelable(IntList.class.getClassLoader()); + this.mPositionList = in.readParcelable(IntList.class.getClassLoader()); this.mImageUrlList = in.createStringArrayList(); this.mPageUrlList = in.createStringArrayList(); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/NormalPreviewSet.java b/app/src/main/java/com/hippo/ehviewer/client/data/NormalPreviewSet.java index 725453596..cb4a380a2 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/data/NormalPreviewSet.java +++ b/app/src/main/java/com/hippo/ehviewer/client/data/NormalPreviewSet.java @@ -16,40 +16,121 @@ package com.hippo.ehviewer.client.data; +import android.os.Parcel; + +import com.hippo.widget.LoadImageView; import com.hippo.yorozuya.IntList; import java.util.ArrayList; -public class NormalPreviewSet { +public class NormalPreviewSet extends PreviewSet { - private final IntList mIndexList = new IntList(); - private final ArrayList mImageUrlList = new ArrayList<>(); - private final IntList mXOffsetList = new IntList(); - private final IntList mYOffsetList = new IntList(); - private final IntList mWidthList = new IntList(); - private final IntList mHeightList = new IntList(); - private final ArrayList mPageUrlList = new ArrayList<>(); + private IntList mPositionList = new IntList(); + private ArrayList mImageKeyList = new ArrayList<>(); + private ArrayList mImageUrlList = new ArrayList<>(); + private IntList mOffsetXList = new IntList(); + private IntList mOffsetYList = new IntList(); + private IntList mClipWidthList = new IntList(); + private IntList mClipHeightList = new IntList(); + private ArrayList mPageUrlList = new ArrayList<>(); - public void addItem(int index, String imageUrl, int xOffset, int yOffset, int width, + private String getImageKey(String imageUrl) { + int index = imageUrl.indexOf('/'); + if (index >= 0) { + return imageUrl.substring(index + 1); + } else { + return imageUrl; + } + } + + public void addItem(int position, String imageUrl, int xOffset, int yOffset, int width, int height, String pageUrl) { - mIndexList.add(index); + mPositionList.add(position); + mImageKeyList.add(getImageKey(imageUrl)); mImageUrlList.add(imageUrl); - mXOffsetList.add(xOffset); - mYOffsetList.add(yOffset); - mWidthList.add(width); - mHeightList.add(height); + mOffsetXList.add(xOffset); + mOffsetYList.add(yOffset); + mClipWidthList.add(width); + mClipHeightList.add(height); mPageUrlList.add(pageUrl); } + @Override public int size() { - return mIndexList.size(); + return mPositionList.size(); } - public int getIndexAt(int index) { - return mIndexList.get(index); + @Override + public int getPosition(int index) { + return mPositionList.get(index); } + @Override public String getPageUrlAt(int index) { return mPageUrlList.get(index); } + + @Override + public GalleryPreview getGalleryPreview(long gid, int index) { + GalleryPreview galleryPreview = new GalleryPreview(); + galleryPreview.position = mPositionList.get(index); + galleryPreview.imageKey = mImageKeyList.get(index); + galleryPreview.imageUrl = mImageUrlList.get(index); + galleryPreview.pageUrl = mPageUrlList.get(index); + galleryPreview.offsetX = mOffsetXList.get(index); + galleryPreview.offsetY = mOffsetYList.get(index); + galleryPreview.clipWidth = mClipWidthList.get(index); + galleryPreview.clipHeight = mClipHeightList.get(index); + return galleryPreview; + } + + @Override + public void load(LoadImageView view, long gid, int index) { + view.setClip(mOffsetXList.get(index), mOffsetYList.get(index), + mClipWidthList.get(index), mClipHeightList.get(index)); + view.load(mImageKeyList.get(index), mImageUrlList.get(index)); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeParcelable(this.mPositionList, flags); + dest.writeStringList(this.mImageKeyList); + dest.writeStringList(this.mImageUrlList); + dest.writeParcelable(this.mOffsetXList, flags); + dest.writeParcelable(this.mOffsetYList, flags); + dest.writeParcelable(this.mClipWidthList, flags); + dest.writeParcelable(this.mClipHeightList, flags); + dest.writeStringList(this.mPageUrlList); + } + + public NormalPreviewSet() { + } + + protected NormalPreviewSet(Parcel in) { + this.mPositionList = in.readParcelable(IntList.class.getClassLoader()); + this.mImageKeyList = in.createStringArrayList(); + this.mImageUrlList = in.createStringArrayList(); + this.mOffsetXList = in.readParcelable(IntList.class.getClassLoader()); + this.mOffsetYList = in.readParcelable(IntList.class.getClassLoader()); + this.mClipWidthList = in.readParcelable(IntList.class.getClassLoader()); + this.mClipHeightList = in.readParcelable(IntList.class.getClassLoader()); + this.mPageUrlList = in.createStringArrayList(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public NormalPreviewSet createFromParcel(Parcel source) { + return new NormalPreviewSet(source); + } + + @Override + public NormalPreviewSet[] newArray(int size) { + return new NormalPreviewSet[size]; + } + }; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/PreviewSet.java b/app/src/main/java/com/hippo/ehviewer/client/data/PreviewSet.java new file mode 100644 index 000000000..c33c90413 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/data/PreviewSet.java @@ -0,0 +1,34 @@ +/* + * 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.client.data; + +import android.os.Parcelable; + +import com.hippo.widget.LoadImageView; + +public abstract class PreviewSet implements Parcelable { + + public abstract int size(); + + public abstract int getPosition(int index); + + public abstract String getPageUrlAt(int index); + + public abstract GalleryPreview getGalleryPreview(long gid, int index); + + public abstract void load(LoadImageView view, long gid, int index); +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java index 6d1690d44..0b8bb5fa1 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java @@ -25,6 +25,7 @@ import com.hippo.ehviewer.client.data.GalleryDetail; import com.hippo.ehviewer.client.data.GalleryTagGroup; import com.hippo.ehviewer.client.data.LargePreviewSet; import com.hippo.ehviewer.client.data.NormalPreviewSet; +import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.client.exception.OffensiveException; import com.hippo.ehviewer.client.exception.ParseException; @@ -129,7 +130,7 @@ public class GalleryDetailParser { galleryDetail.tags = parseTagGroups(document); galleryDetail.comments = parseComments(document); galleryDetail.previewPages = parsePreviewPages(document, body); - galleryDetail.previewSet = parseLargePreviewSet(document, body); + galleryDetail.previewSet = parsePreviewSet(body); return galleryDetail; } @@ -377,10 +378,18 @@ public class GalleryDetailParser { } } + public static PreviewSet parsePreviewSet(String body) throws ParseException { + try { + return parseLargePreviewSet(body); + } catch (ParseException e) { + return parseNormalPreviewSet(body); + } + } + /** * Parse large previews with regular expressions */ - public static LargePreviewSet parseLargePreviewSet(Document document, String body) throws ParseException { + private static LargePreviewSet parseLargePreviewSet(Document document, String body) throws ParseException { try { LargePreviewSet largePreviewSet = new LargePreviewSet(); Element gdt = document.getElementById("gdt"); @@ -407,7 +416,7 @@ public class GalleryDetailParser { /** * Parse large previews with regular expressions */ - public static LargePreviewSet parseLargePreviewSet(String body) throws ParseException { + private static LargePreviewSet parseLargePreviewSet(String body) throws ParseException { Matcher m = LARGE_PREVIEW_PATTERN.matcher(body); LargePreviewSet largePreviewSet = new LargePreviewSet(); @@ -426,7 +435,7 @@ public class GalleryDetailParser { /** * Parse normal previews with regular expressions */ - public static NormalPreviewSet parseNormalPreviewSet(String body) throws ParseException { + private static NormalPreviewSet parseNormalPreviewSet(String body) throws ParseException { Matcher m = NORMAL_PREVIEW_PATTERN.matcher(body); NormalPreviewSet normalPreviewSet = new NormalPreviewSet(); while (m.find()) { 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 964288c0a..7e4c7c6ed 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java @@ -37,8 +37,7 @@ import com.hippo.ehviewer.client.EhConfig; import com.hippo.ehviewer.client.EhRequestBuilder; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.data.GalleryInfo; -import com.hippo.ehviewer.client.data.LargePreviewSet; -import com.hippo.ehviewer.client.data.NormalPreviewSet; +import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.exception.Image509Exception; import com.hippo.ehviewer.client.exception.ParseException; import com.hippo.ehviewer.client.parser.GalleryDetailParser; @@ -680,23 +679,12 @@ public class SpiderQueen implements Runnable { } private void readPreviews(String body, SpiderInfo spiderInfo) throws ParseException { - try { - NormalPreviewSet previewSet = GalleryDetailParser.parseNormalPreviewSet(body); - spiderInfo.previewPerPage = previewSet.size(); - for (int i = 0, n = previewSet.size(); i < n; i++) { - GalleryPageUrlParser.Result result = GalleryPageUrlParser.parse(previewSet.getPageUrlAt(i)); - if (result != null) { - spiderInfo.pTokenMap.put(result.page, result.pToken); - } - } - } catch (ParseException e) { - LargePreviewSet previewSet = GalleryDetailParser.parseLargePreviewSet(body); - spiderInfo.previewPerPage = previewSet.size(); - for (int i = 0, n = previewSet.size(); i < n; i++) { - GalleryPageUrlParser.Result result = GalleryPageUrlParser.parse(previewSet.getPageUrlAt(i)); - if (result != null) { - spiderInfo.pTokenMap.put(result.page, result.pToken); - } + PreviewSet previewSet = GalleryDetailParser.parsePreviewSet(body); + spiderInfo.previewPerPage = previewSet.size(); + for (int i = 0, n = previewSet.size(); i < n; i++) { + GalleryPageUrlParser.Result result = GalleryPageUrlParser.parse(previewSet.getPageUrlAt(i)); + if (result != null) { + spiderInfo.pTokenMap.put(result.page, result.pToken); } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java index bbf805572..47ffc7982 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java @@ -1098,7 +1098,7 @@ public class DownloadsScene extends ToolbarScene return; } DownloadInfo info = mList.get(position); - holder.thumb.load(EhCacheKeyFactory.getThumbKey(info.gid), info.thumb, true); + holder.thumb.load(EhCacheKeyFactory.getThumbKey(info.gid), info.thumb); holder.title.setText(EhUtils.getSuitableTitle(info)); holder.uploader.setText(info.uploader); holder.rating.setRating(info.rating); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java index acd075e86..c8d8a7774 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.app.Dialog; @@ -69,8 +70,8 @@ import com.hippo.ehviewer.client.data.GalleryComment; import com.hippo.ehviewer.client.data.GalleryDetail; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.GalleryTagGroup; -import com.hippo.ehviewer.client.data.LargePreviewSet; import com.hippo.ehviewer.client.data.ListUrlBuilder; +import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.parser.RateGalleryParser; import com.hippo.ehviewer.dao.DownloadInfo; import com.hippo.ehviewer.dao.Filter; @@ -102,7 +103,6 @@ import com.hippo.yorozuya.ViewUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.List; -import java.util.Locale; public class GalleryDetailScene extends BaseScene implements View.OnClickListener, com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener, @@ -715,7 +715,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene if (ACTION_GALLERY_INFO.equals(mAction) && mGalleryInfo != null) { GalleryInfo gi = mGalleryInfo; - mThumb.load(EhCacheKeyFactory.getThumbKey(gi.gid), gi.thumb, true); + mThumb.load(EhCacheKeyFactory.getThumbKey(gi.gid), gi.thumb); mTitle.setText(EhUtils.getSuitableTitle(gi)); mUploader.setText(gi.uploader); mCategory.setText(EhUtils.getCategory(gi.category)); @@ -756,7 +756,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene Resources resources = getResources2(); AssertUtils.assertNotNull(resources); - mThumb.load(EhCacheKeyFactory.getThumbKey(gd.gid), gd.thumb, true); + mThumb.load(EhCacheKeyFactory.getThumbKey(gd.gid), gd.thumb); mTitle.setText(EhUtils.getSuitableTitle(gd)); mUploader.setText(gd.uploader); mCategory.setText(EhUtils.getCategory(gd.category)); @@ -861,6 +861,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } } + @SuppressLint("SetTextI18n") private void bindPreviews(GalleryDetail gd) { LayoutInflater inflater = getLayoutInflater2(); Resources resources = getResources2(); @@ -869,7 +870,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } mGridLayout.removeAllViews(); - LargePreviewSet previewSet = gd.previewSet; + PreviewSet previewSet = gd.previewSet; if (gd.previewPages <= 0 || previewSet == null || previewSet.size() == 0) { mPreviewText.setText(R.string.no_previews); return; @@ -886,14 +887,12 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene View view = inflater.inflate(R.layout.item_gallery_preview, mGridLayout, false); mGridLayout.addView(view); - int index = previewSet.getIndexAt(i); - String imageUrl = previewSet.getImageUrlAt(i); LoadImageView image = (LoadImageView) view.findViewById(R.id.image); - image.load(EhCacheKeyFactory.getLargePreviewKey(gd.gid, index), imageUrl, true); + previewSet.load(image, gd.gid, i); image.setTag(R.id.index, i); image.setOnClickListener(this); TextView text = (TextView) view.findViewById(R.id.text); - text.setText(String.format(Locale.US, "%d", index + 1)); + text.setText(Integer.toString(previewSet.getPosition(i) + 1)); } } @@ -1602,9 +1601,6 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene // Put gallery detail to cache EhApplication.getGalleryDetailCache(getApplication()).put(result.gid, result); - EhApplication.getLargePreviewSetCache(getApplication()).put( - EhCacheKeyFactory.getLargePreviewSetKey(result.gid, 0), result.previewSet); - EhApplication.getPreviewPagesCache(getApplication()).put(result.gid, result.previewPages); // Add history EhDB.putHistoryInfo(result); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java index afc32176a..679c48797 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; @@ -37,13 +38,12 @@ import com.hippo.easyrecyclerview.EasyRecyclerView; import com.hippo.easyrecyclerview.MarginItemDecoration; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.R; -import com.hippo.ehviewer.client.EhCacheKeyFactory; import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.data.GalleryPreview; -import com.hippo.ehviewer.client.data.LargePreviewSet; +import com.hippo.ehviewer.client.data.PreviewSet; import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.ui.GalleryActivity; import com.hippo.ehviewer.ui.MainActivity; @@ -54,7 +54,6 @@ import com.hippo.widget.Slider; import com.hippo.widget.recyclerview.AutoGridLayoutManager; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; -import com.hippo.yorozuya.SimpleHandler; import com.hippo.yorozuya.ViewUtils; import java.util.ArrayList; @@ -229,7 +228,7 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi Intent intent = new Intent(context, GalleryActivity.class); intent.setAction(GalleryActivity.ACTION_EH); intent.putExtra(GalleryActivity.KEY_GALLERY_INFO, mGalleryInfo); - intent.putExtra(GalleryActivity.KEY_PAGE, p.index); + intent.putExtra(GalleryActivity.KEY_PAGE, p.getPosition()); startActivity(intent); } return true; @@ -263,11 +262,12 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi } @Override + @SuppressLint("SetTextI18n") public void onBindViewHolder(GalleryPreviewHolder holder, int position) { - if (null != mHelper && null != mGalleryInfo) { + if (null != mHelper) { GalleryPreview preview = mHelper.getDataAt(position); - holder.image.load(EhCacheKeyFactory.getLargePreviewKey(mGalleryInfo.gid, preview.index), preview.imageUrl); - holder.text.setText(String.format(Locale.US, "%d", preview.index + 1)); + preview.load(holder.image); + holder.text.setText(Integer.toString(preview.getPosition() + 1)); } } @@ -287,24 +287,11 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi return; } - final LargePreviewSet previewSet = EhApplication.getLargePreviewSetCache( - getContext()).get(EhCacheKeyFactory.getLargePreviewSetKey(mGalleryInfo.gid, page)); - final Integer pages = EhApplication.getPreviewPagesCache(getContext()).get(mGalleryInfo.gid); - if (previewSet != null && pages != null) { - SimpleHandler.getInstance().post(new Runnable() { - @Override - public void run() { - onGetLargePreviewSetSuccess(Pair.create(previewSet, pages), taskId); - } - }); - return; - } - String url = EhUrl.getGalleryDetailUrl(mGalleryInfo.gid, mGalleryInfo.token, page, false); EhRequest request = new EhRequest(); - request.setMethod(EhClient.METHOD_GET_LARGE_PREVIEW_SET); - request.setCallback(new GetLargePreviewSetListener(getContext(), - activity.getStageId(), getTag(), taskId, mGalleryInfo.gid, page)); + request.setMethod(EhClient.METHOD_GET_PREVIEW_SET); + request.setCallback(new GetPreviewSetListener(getContext(), + activity.getStageId(), getTag(), taskId)); request.setArgs(url); mClient.execute(request); } @@ -336,17 +323,13 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi } } - private void onGetLargePreviewSetSuccess(Pair result, int taskId) { - if (mHelper != null && mHelper.isCurrentTask(taskId) && isViewCreated()) { - LargePreviewSet previewSet = result.first; + private void onGetPreviewSetSuccess(Pair result, int taskId) { + if (null != mHelper && mHelper.isCurrentTask(taskId) && null != mGalleryInfo) { + PreviewSet previewSet = result.first; int size = previewSet.size(); ArrayList list = new ArrayList<>(size); for (int i = 0; i < size; i++) { - GalleryPreview preview = new GalleryPreview(); - preview.imageUrl = previewSet.getImageUrlAt(i); - preview.pageUrl = previewSet.getPageUrlAt(i); - preview.index = previewSet.getIndexAt(i); - list.add(preview); + list.add(previewSet.getGalleryPreview(mGalleryInfo.gid, i)); } mHelper.setPages(taskId, result.second); @@ -354,34 +337,26 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi } } - private void onGetLargePreviewSetFailure(Exception e, int taskId) { - if (mHelper != null && mHelper.isCurrentTask(taskId) && isViewCreated()) { + private void onGetPreviewSetFailure(Exception e, int taskId) { + if (mHelper != null && mHelper.isCurrentTask(taskId)) { mHelper.onGetException(taskId, e); } } - private static class GetLargePreviewSetListener extends EhCallback> { + private static class GetPreviewSetListener extends EhCallback> { private final int mTaskId; - private final long mGid; - private final int mPage; - public GetLargePreviewSetListener(Context context, int stageId, String sceneTag, int taskId, long gid, int page) { + public GetPreviewSetListener(Context context, int stageId, String sceneTag, int taskId) { super(context, stageId, sceneTag); mTaskId = taskId; - mGid = gid; - mPage = page; } @Override - public void onSuccess(Pair result) { - EhApplication.getLargePreviewSetCache(getApplication()).put( - EhCacheKeyFactory.getLargePreviewSetKey(mGid, mPage), result.first); - EhApplication.getPreviewPagesCache(getApplication()).put(mGid, result.second); - + public void onSuccess(Pair result) { GalleryPreviewsScene scene = getScene(); if (scene != null) { - scene.onGetLargePreviewSetSuccess(result, mTaskId); + scene.onGetPreviewSetSuccess(result, mTaskId); } } @@ -389,7 +364,7 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi public void onFailure(Exception e) { GalleryPreviewsScene scene = getScene(); if (scene != null) { - scene.onGetLargePreviewSetFailure(e, mTaskId); + scene.onGetPreviewSetFailure(e, mTaskId); } } diff --git a/app/src/main/java/com/hippo/widget/LoadImageView.java b/app/src/main/java/com/hippo/widget/LoadImageView.java index fa78e3861..8069637f8 100644 --- a/app/src/main/java/com/hippo/widget/LoadImageView.java +++ b/app/src/main/java/com/hippo/widget/LoadImageView.java @@ -34,6 +34,7 @@ import com.hippo.conaco.ConacoTask; import com.hippo.conaco.DataContainer; import com.hippo.conaco.Unikery; import com.hippo.conaco.ValueHolder; +import com.hippo.drawable.PreciselyClipDrawable; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.R; @@ -49,6 +50,11 @@ public class LoadImageView extends FixedAspectImageView implements Unikery mHolder; private boolean mFailed; @@ -152,6 +158,20 @@ public class LoadImageView extends FixedAspectImageView implements Unikery