Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void showImage() {
if (isAdded() && !TextUtils.isEmpty(mImageUrl)) {
// use max of width/height so image is cached the same regardless of orientation
Rect pt = DisplayUtils.getWindowSize(requireActivity());
int hiResWidth = Math.max(pt.height(), pt.width());
int hiResWidth = (int) (Math.max(pt.height(), pt.width()) * 0.8);
// don't use AT media proxy here
mPhotoView.setImageUrl(mImageUrl, hiResWidth, mIsPrivate, false, mPhotoViewListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@
* but adds pinch/zoom and the ability to first load a lo-res version of the image
*/
public class ReaderPhotoView extends RelativeLayout {
public static final double FALLBACK_RESOLUTION_MULTIPLIER = 0.5;

public interface PhotoViewListener {
void onTapPhotoView();
}

private String mImageUrl;

Check notice

Code scanning / Android Lint

Nullable/NonNull annotation missing on field Note

Missing null annotation
private int mHiResWidth;
private boolean mIsPrivate;
private boolean mIsPrivateAtSite;

private PhotoViewListener mPhotoViewListener;
private String mLoResImageUrl;
private String mHiResImageUrl;
Expand Down Expand Up @@ -75,6 +82,11 @@
boolean isPrivate,
boolean isPrivateAtSite,
PhotoViewListener listener) {
mImageUrl = imageUrl;
mHiResWidth = hiResWidth;
mIsPrivate = isPrivate;
mIsPrivateAtSite = isPrivateAtSite;

int loResWidth = (int) (hiResWidth * 0.10f);
mLoResImageUrl = ReaderUtils
.getResizedImageUrl(imageUrl, loResWidth, 0, isPrivate, isPrivateAtSite, PhotonUtils.Quality.LOW);
Expand Down Expand Up @@ -110,16 +122,24 @@

mImageManager
.loadWithResultListener(mImageView, ImageType.IMAGE, mHiResImageUrl, ScaleType.CENTER, mLoResImageUrl,
new RequestListener<Drawable>() {
new RequestListener<>() {
@Override
public void onLoadFailed(@Nullable Exception e, @Nullable Object model) {
if (e != null) {
AppLog.e(AppLog.T.READER, e);
}
boolean lowResNotLoadedYet = isLoading();
if (lowResNotLoadedYet) {
hideProgress();
showError();
// Check if the stack trace contains TimeoutError before trying fallback
if (containsTimeoutInStackTrace(e)) {
AppLog.d(AppLog.T.READER, "Timeout detected in stack trace, loading fallback image");
mImageView.post(
() -> loadFallbackImage()
);
} else {
boolean lowResNotLoadedYet = isLoading();
if (lowResNotLoadedYet) {
hideProgress();
showError();
}
}
}
}

Expand All @@ -130,6 +150,50 @@
});
}

private boolean containsTimeoutInStackTrace(@Nullable Exception e) {
if (e == null) {
return false;
}

// Convert stack trace to string and check for TimeoutError
java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
e.printStackTrace(pw);
String stackTrace = sw.toString();

return stackTrace.contains("TimeoutError");
}

private void loadFallbackImage() {
if (!hasLayout()) {
return;
}
String resImageUrl = ReaderUtils.getResizedImageUrl(mImageUrl,
(int) (mHiResWidth * FALLBACK_RESOLUTION_MULTIPLIER), 0, mIsPrivate,
mIsPrivateAtSite, PhotonUtils.Quality.MEDIUM);

mImageManager
.loadWithResultListener(mImageView, ImageType.IMAGE, resImageUrl, ScaleType.CENTER, mLoResImageUrl,
new RequestListener<>() {
@Override
public void onLoadFailed(@Nullable Exception e, @Nullable Object model) {
if (e != null) {
AppLog.e(AppLog.T.READER, e);
}
boolean lowResNotLoadedYet = isLoading();
if (lowResNotLoadedYet) {
hideProgress();
showError();
}
}

@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Object model) {
handleResponse();
}
});
}

private void handleResponse() {
hideProgress();
hideError();
Expand Down