Fix #705 and improve handling of unknown attachment dimensions

This commit is contained in:
Grishka 2023-10-03 02:52:07 +03:00
parent 70386ea1b2
commit 1f4152b588
7 changed files with 51 additions and 18 deletions

View File

@ -9,8 +9,8 @@ android {
applicationId "org.joinmastodon.android" applicationId "org.joinmastodon.android"
minSdk 23 minSdk 23
targetSdk 33 targetSdk 33
versionCode 71 versionCode 72
versionName "2.1.5" versionName "2.1.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "ar-rSA", "be-rBY", "bn-rBD", "bs-rBA", "ca-rES", "cs-rCZ", "da-rDK", "de-rDE", "el-rGR", "es-rES", "eu-rES", "fa-rIR", "fi-rFI", "fil-rPH", "fr-rFR", "ga-rIE", "gd-rGB", "gl-rES", "hi-rIN", "hr-rHR", "hu-rHU", "hy-rAM", "ig-rNG", "in-rID", "is-rIS", "it-rIT", "iw-rIL", "ja-rJP", "kab", "ko-rKR", "my-rMM", "nl-rNL", "no-rNO", "oc-rFR", "pl-rPL", "pt-rBR", "pt-rPT", "ro-rRO", "ru-rRU", "si-rLK", "sl-rSI", "sv-rSE", "th-rTH", "tr-rTR", "uk-rUA", "ur-rIN", "vi-rVN", "zh-rCN", "zh-rTW" resConfigs "ar-rSA", "be-rBY", "bn-rBD", "bs-rBA", "ca-rES", "cs-rCZ", "da-rDK", "de-rDE", "el-rGR", "es-rES", "eu-rES", "fa-rIR", "fi-rFI", "fil-rPH", "fr-rFR", "ga-rIE", "gd-rGB", "gl-rES", "hi-rIN", "hr-rHR", "hu-rHU", "hy-rAM", "ig-rNG", "in-rID", "is-rIS", "it-rIT", "iw-rIL", "ja-rJP", "kab", "ko-rKR", "my-rMM", "nl-rNL", "no-rNO", "oc-rFR", "pl-rPL", "pt-rBR", "pt-rPT", "ro-rRO", "ru-rRU", "si-rLK", "sl-rSI", "sv-rSE", "th-rTH", "tr-rTR", "uk-rUA", "ur-rIN", "vi-rVN", "zh-rCN", "zh-rTW"
} }

View File

@ -45,26 +45,34 @@ public class Attachment extends BaseModel{
public int getWidth(){ public int getWidth(){
if(meta==null) if(meta==null)
return 0; return 1920;
if(meta.width>0) if(meta.width>0)
return meta.width; return meta.width;
if(meta.original!=null && meta.original.width>0) if(meta.original!=null && meta.original.width>0)
return meta.original.width; return meta.original.width;
if(meta.small!=null && meta.small.width>0) if(meta.small!=null && meta.small.width>0)
return meta.small.width; return meta.small.width;
return 0; return 1920;
} }
public int getHeight(){ public int getHeight(){
if(meta==null) if(meta==null)
return 0; return 1080;
if(meta.height>0) if(meta.height>0)
return meta.height; return meta.height;
if(meta.original!=null && meta.original.height>0) if(meta.original!=null && meta.original.height>0)
return meta.original.height; return meta.original.height;
if(meta.small!=null && meta.small.height>0) if(meta.small!=null && meta.small.height>0)
return meta.small.height; return meta.small.height;
return 0; return 1080;
}
public boolean hasKnownDimensions(){
return meta!=null && (
(meta.height>0 && meta.width>0)
|| (meta.original!=null && meta.original.height>0 && meta.original.width>0)
|| (meta.small!=null && meta.small.height>0 && meta.small.width>0)
);
} }
public double getDuration(){ public double getDuration(){

View File

@ -66,6 +66,10 @@ public class BlurhashCrossfadeDrawable extends Drawable{
public void setImageDrawable(Drawable imageDrawable){ public void setImageDrawable(Drawable imageDrawable){
this.imageDrawable=imageDrawable; this.imageDrawable=imageDrawable;
if(imageDrawable!=null){
width=imageDrawable.getIntrinsicWidth();
height=imageDrawable.getIntrinsicHeight();
}
invalidateSelf(); invalidateSelf();
} }

View File

@ -716,9 +716,18 @@ public class PhotoViewer implements ZoomPanView.Listener{
public void onBind(Attachment item){ public void onBind(Attachment item){
super.onBind(item); super.onBind(item);
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams) imageView.getLayoutParams(); FrameLayout.LayoutParams params=(FrameLayout.LayoutParams) imageView.getLayoutParams();
params.width=item.getWidth(); Drawable currentDrawable=listener.getPhotoViewCurrentDrawable(getAbsoluteAdapterPosition());
params.height=item.getHeight(); if(item.hasKnownDimensions()){
ViewImageLoader.load(this, listener.getPhotoViewCurrentDrawable(getAbsoluteAdapterPosition()), new UrlImageLoaderRequest(item.url), false); params.width=item.getWidth();
params.height=item.getHeight();
}else if(currentDrawable!=null){
params.width=currentDrawable.getIntrinsicWidth();
params.height=currentDrawable.getIntrinsicHeight();
}else{
params.width=1920;
params.height=1080;
}
ViewImageLoader.load(this, currentDrawable, new UrlImageLoaderRequest(item.url), false);
} }
@Override @Override
@ -760,9 +769,18 @@ public class PhotoViewer implements ZoomPanView.Listener{
super.onBind(item); super.onBind(item);
playerReady=false; playerReady=false;
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams) wrap.getLayoutParams(); FrameLayout.LayoutParams params=(FrameLayout.LayoutParams) wrap.getLayoutParams();
params.width=item.getWidth(); Drawable currentDrawable=listener.getPhotoViewCurrentDrawable(getAbsoluteAdapterPosition());
params.height=item.getHeight(); if(item.hasKnownDimensions()){
wrap.setBackground(listener.getPhotoViewCurrentDrawable(getAbsoluteAdapterPosition())); params.width=item.getWidth();
params.height=item.getHeight();
}else if(currentDrawable!=null){
params.width=currentDrawable.getIntrinsicWidth();
params.height=currentDrawable.getIntrinsicHeight();
}else{
params.width=1920;
params.height=1080;
}
wrap.setBackground(currentDrawable);
progressBar.setVisibility(item.type==Attachment.Type.VIDEO ? View.VISIBLE : View.GONE); progressBar.setVisibility(item.type==Attachment.Type.VIDEO ? View.VISIBLE : View.GONE);
if(itemView.isAttachedToWindow()){ if(itemView.isAttachedToWindow()){
reset(); reset();
@ -845,6 +863,8 @@ public class PhotoViewer implements ZoomPanView.Listener{
player.prepareAsync(); player.prepareAsync();
}catch(IOException x){ }catch(IOException x){
Log.w(TAG, "Error initializing gif player", x); Log.w(TAG, "Error initializing gif player", x);
Toast.makeText(activity, R.string.error_playing_video, Toast.LENGTH_SHORT).show();
onStartSwipeToDismissTransition(0f);
} }
} }

View File

@ -193,9 +193,6 @@ public class ZoomPanView extends FrameLayout implements ScaleGestureDetector.OnS
private float prepareTransitionCropRect(Rect rect){ private float prepareTransitionCropRect(Rect rect){
float initialScale; float initialScale;
if(rect.isEmpty()){
rect.set(rect.centerX()-child.getWidth()/2, rect.centerY()-child.getHeight()/2, rect.centerX()+child.getWidth()/2, rect.centerY()+child.getWidth()/2);
}
float scaleW=rect.width()/(float)child.getWidth(); float scaleW=rect.width()/(float)child.getWidth();
float scaleH=rect.height()/(float)child.getHeight(); float scaleH=rect.height()/(float)child.getHeight();
if(scaleW>scaleH){ if(scaleW>scaleH){

View File

@ -27,6 +27,7 @@ public class MediaAttachmentViewController{
private final Context context; private final Context context;
private boolean didClear; private boolean didClear;
private Status status; private Status status;
private Attachment attachment;
public MediaAttachmentViewController(Context context, MediaGridStatusDisplayItem.GridItemType type){ public MediaAttachmentViewController(Context context, MediaGridStatusDisplayItem.GridItemType type){
view=context.getSystemService(LayoutInflater.class).inflate(switch(type){ view=context.getSystemService(LayoutInflater.class).inflate(switch(type){
@ -50,6 +51,7 @@ public class MediaAttachmentViewController{
public void bind(Attachment attachment, Status status){ public void bind(Attachment attachment, Status status){
this.status=status; this.status=status;
this.attachment=attachment;
crossfadeDrawable.setSize(attachment.getWidth(), attachment.getHeight()); crossfadeDrawable.setSize(attachment.getWidth(), attachment.getHeight());
crossfadeDrawable.setBlurhashDrawable(attachment.blurhashPlaceholder); crossfadeDrawable.setBlurhashDrawable(attachment.blurhashPlaceholder);
crossfadeDrawable.setCrossfadeAlpha(0f); crossfadeDrawable.setCrossfadeAlpha(0f);
@ -69,6 +71,11 @@ public class MediaAttachmentViewController{
crossfadeDrawable.setImageDrawable(drawable); crossfadeDrawable.setImageDrawable(drawable);
if(didClear) if(didClear)
crossfadeDrawable.animateAlpha(0f); crossfadeDrawable.animateAlpha(0f);
// Make sure the image is not stretched if the server returned wrong dimensions
if(drawable!=null && (drawable.getIntrinsicWidth()!=attachment.getWidth() || drawable.getIntrinsicHeight()!=attachment.getHeight())){
photo.setImageDrawable(null);
photo.setImageDrawable(crossfadeDrawable);
}
} }
public void clearImage(){ public void clearImage(){

View File

@ -2,14 +2,11 @@ package org.joinmastodon.android.ui.views;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.joinmastodon.android.ui.PhotoLayoutHelper; import org.joinmastodon.android.ui.PhotoLayoutHelper;
import java.util.Arrays;
import me.grishka.appkit.utils.V; import me.grishka.appkit.utils.V;
public class MediaGridLayout extends ViewGroup{ public class MediaGridLayout extends ViewGroup{