Add transfer indicators for attachments

Closes #3498
// FREEBIE
This commit is contained in:
Jake McGinty
2015-06-26 20:14:51 -07:00
committed by Moxie Marlinspike
parent daa98107c3
commit c2e5f4e80a
51 changed files with 301 additions and 440 deletions

View File

@@ -1,232 +0,0 @@
/*
* Copyright (C) 2006 The Android Open Source Project
*
* 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 org.thoughtcrime.securesms.components;
import android.annotation.TargetApi;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION_CODES;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import com.makeramen.roundedimageview.RoundedImageView;
import org.thoughtcrime.securesms.R;
/**
* https://gist.github.com/chrisbanes/9091754
*/
public class ForegroundImageView extends RoundedImageView {
private Drawable mForeground;
private final Rect mSelfBounds = new Rect();
private final Rect mOverlayBounds = new Rect();
private int mForegroundGravity = Gravity.FILL;
private boolean mForegroundInPadding = true;
private boolean mForegroundBoundsChanged = false;
public ForegroundImageView(Context context) {
super(context);
}
public ForegroundImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ForegroundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundImageView,
defStyle, 0);
mForegroundGravity = a.getInt(
R.styleable.ForegroundImageView_android_foregroundGravity, mForegroundGravity);
final Drawable d = a.getDrawable(R.styleable.ForegroundImageView_android_foreground);
if (d != null) {
setForeground(d);
}
mForegroundInPadding = a.getBoolean(
R.styleable.ForegroundImageView_android_foregroundInsidePadding, true);
a.recycle();
}
/**
* Describes how the foreground is positioned.
*
* @return foreground gravity.
*
* @see #setForegroundGravity(int)
*/
public int getForegroundGravity() {
return mForegroundGravity;
}
/**
* Describes how the foreground is positioned. Defaults to START and TOP.
*
* @param foregroundGravity See {@link android.view.Gravity}
*
* @see #getForegroundGravity()
*/
public void setForegroundGravity(int foregroundGravity) {
if (mForegroundGravity != foregroundGravity) {
if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.START;
}
if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
foregroundGravity |= Gravity.TOP;
}
mForegroundGravity = foregroundGravity;
if (mForegroundGravity == Gravity.FILL && mForeground != null) {
Rect padding = new Rect();
mForeground.getPadding(padding);
}
requestLayout();
}
}
@TargetApi(VERSION_CODES.JELLY_BEAN)
public ActivityOptions getThumbnailTransition() {
return ActivityOptions.makeScaleUpAnimation(this, 0, 0, getWidth(), getHeight());
}
@Override
protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || (who == mForeground);
}
@Override
@TargetApi(VERSION_CODES.HONEYCOMB)
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (mForeground != null) mForeground.jumpToCurrentState();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mForeground != null && mForeground.isStateful()) {
mForeground.setState(getDrawableState());
}
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views in the frame layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
public void setForeground(Drawable drawable) {
if (mForeground != drawable) {
if (mForeground != null) {
mForeground.setCallback(null);
unscheduleDrawable(mForeground);
}
mForeground = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
if (mForegroundGravity == Gravity.FILL) {
Rect padding = new Rect();
drawable.getPadding(padding);
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
/**
* Returns the drawable used as the foreground of this FrameLayout. The
* foreground drawable, if non-null, is always drawn on top of the children.
*
* @return A Drawable or null if no foreground was set.
*/
public Drawable getForeground() {
return mForeground;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mForegroundBoundsChanged = changed;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mForegroundBoundsChanged = true;
}
@Override
public void draw(@NonNull Canvas canvas) {
super.draw(canvas);
if (mForeground != null) {
final Drawable foreground = mForeground;
if (mForegroundBoundsChanged) {
mForegroundBoundsChanged = false;
final Rect selfBounds = mSelfBounds;
final Rect overlayBounds = mOverlayBounds;
final int w = getRight() - getLeft();
final int h = getBottom() - getTop();
if (mForegroundInPadding) {
selfBounds.set(0, 0, w, h);
} else {
selfBounds.set(getPaddingLeft(), getPaddingTop(),
w - getPaddingRight(), h - getPaddingBottom());
}
Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),
foreground.getIntrinsicHeight(), selfBounds, overlayBounds);
foreground.setBounds(overlayBounds);
}
foreground.draw(canvas);
}
}
}

View File

@@ -0,0 +1,35 @@
package org.thoughtcrime.securesms.components;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.widget.FrameLayout;
public class SquareFrameLayout extends FrameLayout {
@SuppressWarnings("unused")
public SquareFrameLayout(Context context) {
super(context);
}
@SuppressWarnings("unused")
public SquareFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@TargetApi(VERSION_CODES.HONEYCOMB) @SuppressWarnings("unused")
public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(VERSION_CODES.LOLLIPOP) @SuppressWarnings("unused")
public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//noinspection SuspiciousNameCombination
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}

View File

@@ -4,56 +4,87 @@ import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
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 android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import com.bumptech.glide.GenericRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.makeramen.roundedimageview.RoundedImageView;
import com.pnikosis.materialishprogress.ProgressWheel;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.jobs.PartProgressEvent;
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 org.thoughtcrime.securesms.util.Util;
import de.greenrobot.event.EventBus;
import ws.com.google.android.mms.pdu.PduPart;
public class ThumbnailView extends RoundedImageView {
public class ThumbnailView extends FrameLayout {
private static final String TAG = ThumbnailView.class.getSimpleName();
private boolean showProgress = true;
private RoundedImageView image;
private ProgressWheel progress;
private ListenableFutureTask<SlideDeck> slideDeckFuture = null;
private SlideDeckListener slideDeckListener = null;
private ThumbnailClickListener thumbnailClickListener = null;
private String slideId = null;
private Slide slide = null;
private Handler handler = new Handler();
public ThumbnailView(Context context) {
super(context);
this(context, null);
}
public ThumbnailView(Context context, AttributeSet attrs) {
super(context, attrs);
this(context, attrs, 0);
}
public ThumbnailView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflate(context, R.layout.thumbnail_view, this);
image = (RoundedImageView) findViewById(R.id.thumbnail_image);
progress = (ProgressWheel) findViewById(R.id.progress_wheel);
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
EventBus.getDefault().registerSticky(this);
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
EventBus.getDefault().unregister(this);
}
@SuppressWarnings("unused")
public void onEventAsync(final PartProgressEvent event) {
if (this.slide != null && event.partId.equals(this.slide.getPart().getPartId())) {
Util.runOnMain(new Runnable() {
@Override public void run() {
progress.setInstantProgress(((float) event.progress) / event.total);
if (event.progress >= event.total) animateOutProgress();
}
});
}
}
public void setImageResource(@Nullable MasterSecret masterSecret,
@@ -67,7 +98,7 @@ public class ThumbnailView extends RoundedImageView {
String slideId = id + "::" + timestamp;
if (!slideId.equals(this.slideId)) {
setImageDrawable(null);
image.setImageDrawable(null);
this.slide = null;
this.slideId = slideId;
}
@@ -82,13 +113,24 @@ public class ThumbnailView extends RoundedImageView {
}
public void setImageResource(@NonNull Slide slide, @Nullable MasterSecret masterSecret) {
if (isContextValid()) {
if (!Util.equals(slide, this.slide)) buildGlideRequest(slide, masterSecret).into(this);
this.slide = slide;
setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide));
} else {
Log.w(TAG, "Not going to load resource, context is invalid");
if (Util.equals(slide, this.slide)) {
Log.w(TAG, "Not loading resource, slide was identical");
return;
}
if (!isContextValid()) {
Log.w(TAG, "Not loading resource, context is invalid");
return;
}
this.slide = slide;
if (slide.isInProgress() && showProgress) {
progress.spin();
progress.setVisibility(VISIBLE);
} else {
progress.setVisibility(GONE);
}
buildGlideRequest(slide, masterSecret).into(image);
setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide));
}
public void setThumbnailClickListener(ThumbnailClickListener listener) {
@@ -99,6 +141,13 @@ public class ThumbnailView extends RoundedImageView {
if (isContextValid()) Glide.clear(this);
}
public void setShowProgress(boolean showProgress) {
this.showProgress = showProgress;
if (progress.getVisibility() == View.VISIBLE && !showProgress) {
animateOutProgress();
}
}
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
private boolean isContextValid() {
return !(getContext() instanceof Activity) ||
@@ -110,22 +159,17 @@ public class ThumbnailView extends RoundedImageView {
@Nullable MasterSecret masterSecret)
{
final GenericRequestBuilder builder;
if (slide.getPart().isPendingPush()) {
builder = buildPendingGlideRequest(slide);
} else if (slide.getThumbnailUri() != null) {
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();
if (slide.isInProgress() && showProgress) {
return builder;
} else {
return builder.error(R.drawable.ic_missing_thumbnail_picture);
}
}
private GenericRequestBuilder buildThumbnailGlideRequest(Slide slide, MasterSecret masterSecret) {
@@ -148,7 +192,7 @@ public class ThumbnailView extends RoundedImageView {
}
return Glide.with(getContext()).load(new DecryptableUri(masterSecret, slide.getThumbnailUri()))
.transform(new ThumbnailTransform(getContext()));
.centerCrop();
}
private GenericRequestBuilder buildPlaceholderGlideRequest(Slide slide) {
@@ -157,6 +201,19 @@ public class ThumbnailView extends RoundedImageView {
.crossFade();
}
private void animateOutProgress() {
AlphaAnimation animation = new AlphaAnimation(1f, 0f);
animation.setDuration(200);
animation.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) { }
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) {
progress.setVisibility(View.GONE);
}
});
progress.startAnimation(animation);
}
private class SlideDeckListener implements FutureTaskListener<SlideDeck> {
private final MasterSecret masterSecret;
@@ -170,14 +227,14 @@ public class ThumbnailView extends RoundedImageView {
final Slide slide = slideDeck.getThumbnailSlide(getContext());
if (slide != null) {
handler.post(new Runnable() {
Util.runOnMain(new Runnable() {
@Override
public void run() {
setImageResource(slide, masterSecret);
}
});
} else {
handler.post(new Runnable() {
Util.runOnMain(new Runnable() {
@Override
public void run() {
Log.w(TAG, "Resolved slide was null!");
@@ -190,7 +247,7 @@ public class ThumbnailView extends RoundedImageView {
@Override
public void onFailure(Throwable error) {
Log.w(TAG, error);
handler.post(new Runnable() {
Util.runOnMain(new Runnable() {
@Override
public void run() {
Log.w(TAG, "onFailure!");