mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-24 16:00:02 +00:00
committed by
Moxie Marlinspike
parent
9ba19df2af
commit
f42d100f15
@@ -22,7 +22,6 @@ import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
@@ -51,11 +50,11 @@ public abstract class BubbleContainer extends RelativeLayout {
|
||||
@IntDef({MEDIA_STATE_NO_MEDIA, MEDIA_STATE_CAPTIONLESS, MEDIA_STATE_CAPTIONED})
|
||||
public @interface MediaState {}
|
||||
|
||||
private View bodyBubble;
|
||||
private View triangleTick;
|
||||
private ForegroundImageView media;
|
||||
private int shadowColor;
|
||||
private int mmsPendingOverlayColor;
|
||||
private View bodyBubble;
|
||||
private View triangleTick;
|
||||
private ThumbnailView media;
|
||||
private int shadowColor;
|
||||
private int mmsPendingOverlayColor;
|
||||
|
||||
public BubbleContainer(Context context) {
|
||||
super(context);
|
||||
@@ -88,7 +87,7 @@ public abstract class BubbleContainer extends RelativeLayout {
|
||||
onCreateView();
|
||||
this.bodyBubble = findViewById(R.id.body_bubble );
|
||||
this.triangleTick = findViewById(R.id.triangle_tick);
|
||||
this.media = (ForegroundImageView) findViewById(R.id.image_view);
|
||||
this.media = (ThumbnailView) findViewById(R.id.image_view);
|
||||
|
||||
this.shadowColor = ResUtil.getColor(getContext(), R.attr.conversation_item_shadow);
|
||||
this.mmsPendingOverlayColor = ResUtil.getColor(getContext(), R.attr.conversation_item_mms_pending_mask);
|
||||
|
||||
@@ -23,12 +23,11 @@ import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Build.VERSION_CODES;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.animation.AlphaAnimation;
|
||||
|
||||
import com.makeramen.RoundedImageView;
|
||||
|
||||
@@ -46,9 +45,9 @@ public class ForegroundImageView extends RoundedImageView {
|
||||
|
||||
private int mForegroundGravity = Gravity.FILL;
|
||||
|
||||
protected boolean mForegroundInPadding = true;
|
||||
private boolean mForegroundInPadding = true;
|
||||
|
||||
boolean mForegroundBoundsChanged = false;
|
||||
private boolean mForegroundBoundsChanged = false;
|
||||
|
||||
public ForegroundImageView(Context context) {
|
||||
super(context);
|
||||
@@ -123,54 +122,15 @@ public class ForegroundImageView extends RoundedImageView {
|
||||
return ActivityOptions.makeScaleUpAnimation(this, 0, 0, getWidth(), getHeight());
|
||||
}
|
||||
|
||||
public void show(Drawable drawable, boolean instantaneous) {
|
||||
setImageDrawable(drawable);
|
||||
if (drawable.getIntrinsicHeight() < (getHeight() * 0.75f) &&
|
||||
drawable.getIntrinsicWidth() < (getHeight() * 0.75f))
|
||||
{
|
||||
setScaleType(ScaleType.CENTER_INSIDE);
|
||||
} else {
|
||||
setScaleType(ScaleType.CENTER_CROP);
|
||||
}
|
||||
fadeIn(instantaneous ? 0 : 200);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
cancelAnimations();
|
||||
setImageDrawable(null);
|
||||
setVisibility(View.INVISIBLE);
|
||||
setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
public void hide() {
|
||||
setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private void fadeIn(final long millis) {
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) fadeInModern(millis);
|
||||
else fadeInLegacy(millis);
|
||||
setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void fadeInLegacy(final long millis) {
|
||||
final AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
|
||||
alpha.setDuration(millis);
|
||||
alpha.setFillAfter(true);
|
||||
startAnimation(alpha);
|
||||
}
|
||||
|
||||
@TargetApi(VERSION_CODES.JELLY_BEAN)
|
||||
private void fadeInModern(final long millis) {
|
||||
setAlpha(0f);
|
||||
animate().alpha(1f).setDuration(millis);
|
||||
}
|
||||
|
||||
private void cancelAnimations() {
|
||||
if (getAnimation() != null) {
|
||||
getAnimation().cancel();
|
||||
clearAnimation();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean verifyDrawable(Drawable who) {
|
||||
return super.verifyDrawable(who) || (who == mForeground);
|
||||
@@ -249,7 +209,7 @@ public class ForegroundImageView extends RoundedImageView {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
public void draw(@NonNull Canvas canvas) {
|
||||
super.draw(canvas);
|
||||
|
||||
if (mForeground != null) {
|
||||
@@ -278,5 +238,4 @@ public class ForegroundImageView extends RoundedImageView {
|
||||
foreground.draw(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
202
src/org/thoughtcrime/securesms/components/ThumbnailView.java
Normal file
202
src/org/thoughtcrime/securesms/components/ThumbnailView.java
Normal file
@@ -0,0 +1,202 @@
|
||||
package org.thoughtcrime.securesms.components;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.bumptech.glide.GenericRequestBuilder;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.RequestListener;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
||||
import org.thoughtcrime.securesms.mms.Slide;
|
||||
import org.thoughtcrime.securesms.mms.SlideDeck;
|
||||
import org.thoughtcrime.securesms.mms.ThumbnailTransform;
|
||||
import org.thoughtcrime.securesms.util.FutureTaskListener;
|
||||
import org.thoughtcrime.securesms.util.ListenableFutureTask;
|
||||
|
||||
import ws.com.google.android.mms.pdu.PduPart;
|
||||
|
||||
public class ThumbnailView extends ForegroundImageView {
|
||||
private ListenableFutureTask<SlideDeck> slideDeckFuture = null;
|
||||
private SlideDeckListener slideDeckListener = null;
|
||||
private ThumbnailClickListener thumbnailClickListener = null;
|
||||
private Handler handler = new Handler();
|
||||
|
||||
public ThumbnailView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public ThumbnailView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public ThumbnailView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
public void setImageResource(@NonNull ListenableFutureTask<SlideDeck> slideDeckFuture,
|
||||
@Nullable MasterSecret masterSecret)
|
||||
{
|
||||
if (this.slideDeckFuture != null && this.slideDeckListener != null) {
|
||||
this.slideDeckFuture.removeListener(this.slideDeckListener);
|
||||
}
|
||||
|
||||
this.slideDeckListener = new SlideDeckListener(masterSecret);
|
||||
this.slideDeckFuture = slideDeckFuture;
|
||||
this.slideDeckFuture.addListener(this.slideDeckListener);
|
||||
}
|
||||
|
||||
public void setImageResource(@NonNull Slide slide, @Nullable MasterSecret masterSecret) {
|
||||
buildGlideRequest(slide, masterSecret).into(ThumbnailView.this);
|
||||
setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide));
|
||||
}
|
||||
|
||||
public void setImageResource(@NonNull Slide slide)
|
||||
{
|
||||
setImageResource(slide, null);
|
||||
}
|
||||
|
||||
public void setThumbnailClickListener(ThumbnailClickListener listener) {
|
||||
this.thumbnailClickListener = listener;
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildGlideRequest(@NonNull Slide slide,
|
||||
@Nullable MasterSecret masterSecret)
|
||||
{
|
||||
final GenericRequestBuilder builder;
|
||||
if (slide.getPart().isPendingPush()) {
|
||||
builder = buildPendingGlideRequest(slide);
|
||||
} else if (slide.getThumbnailUri() != null) {
|
||||
builder = buildThumbnailGlideRequest(slide, masterSecret);
|
||||
} else {
|
||||
builder = buildPlaceholderGlideRequest(slide);
|
||||
}
|
||||
|
||||
return builder.error(R.drawable.ic_missing_thumbnail_picture);
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildPendingGlideRequest(Slide slide) {
|
||||
return Glide.with(getContext()).load(R.drawable.stat_sys_download_anim0)
|
||||
.dontTransform()
|
||||
.skipMemoryCache(true)
|
||||
.crossFade();
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildThumbnailGlideRequest(Slide slide, MasterSecret masterSecret) {
|
||||
|
||||
final GenericRequestBuilder builder;
|
||||
if (slide.isDraft()) builder = buildDraftGlideRequest(slide);
|
||||
else builder = buildEncryptedPartGlideRequest(slide, masterSecret);
|
||||
return builder;
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildDraftGlideRequest(Slide slide) {
|
||||
return Glide.with(getContext()).load(slide.getThumbnailUri()).asBitmap()
|
||||
.fitCenter()
|
||||
.listener(new PduThumbnailSetListener(slide.getPart()));
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildEncryptedPartGlideRequest(Slide slide, MasterSecret masterSecret) {
|
||||
if (masterSecret == null) {
|
||||
throw new IllegalStateException("null MasterSecret when loading non-draft thumbnail");
|
||||
}
|
||||
|
||||
return Glide.with(getContext()).load(new DecryptableUri(masterSecret, slide.getThumbnailUri()))
|
||||
.crossFade().transform(new ThumbnailTransform(getContext()));
|
||||
}
|
||||
|
||||
private GenericRequestBuilder buildPlaceholderGlideRequest(Slide slide) {
|
||||
return Glide.with(getContext()).load(slide.getPlaceholderRes(getContext().getTheme()))
|
||||
.fitCenter()
|
||||
.crossFade();
|
||||
}
|
||||
|
||||
private class SlideDeckListener implements FutureTaskListener<SlideDeck> {
|
||||
private final MasterSecret masterSecret;
|
||||
|
||||
public SlideDeckListener(MasterSecret masterSecret) {
|
||||
this.masterSecret = masterSecret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(final SlideDeck slideDeck) {
|
||||
if (slideDeck == null) return;
|
||||
|
||||
final Slide slide = slideDeck.getThumbnailSlide(getContext());
|
||||
if (slide != null) {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
setImageResource(slide, masterSecret);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable error) {
|
||||
Log.w(TAG, error);
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public interface ThumbnailClickListener {
|
||||
void onClick(View v, Slide slide);
|
||||
}
|
||||
|
||||
private class ThumbnailClickDispatcher implements View.OnClickListener {
|
||||
private ThumbnailClickListener listener;
|
||||
private Slide slide;
|
||||
|
||||
public ThumbnailClickDispatcher(ThumbnailClickListener listener, Slide slide) {
|
||||
this.listener = listener;
|
||||
this.slide = slide;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
listener.onClick(view, slide);
|
||||
}
|
||||
}
|
||||
|
||||
private static class PduThumbnailSetListener implements RequestListener<Uri, Bitmap> {
|
||||
private PduPart part;
|
||||
|
||||
public PduThumbnailSetListener(@NonNull PduPart part) {
|
||||
this.part = part;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onException(Exception e, Uri model, Target<Bitmap> target, boolean isFirstResource) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onResourceReady(Bitmap resource, Uri model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
|
||||
part.setThumbnail(resource);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user