TransferControlView

Closes #4044
// FREEBIE
pull/1/head
Jake McGinty 9 years ago committed by Moxie Marlinspike
parent f95435b0f1
commit 415a61a09b

@ -9,23 +9,15 @@
android:contentDescription="@string/conversation_item__mms_image_description"
android:layout_margin="@dimen/media_bubble_border_width" />
<ViewStub android:id="@+id/progress_wheel_stub"
<ViewStub android:id="@+id/transfer_controls_stub"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout="@layout/thumbnail_view_progress_wheel" />
android:layout="@layout/transfer_controls_stub" />
<ViewStub android:id="@+id/remove_button_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout="@layout/thumbnail_view_remove_button" />
<ImageButton android:id="@+id/download_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:background="@drawable/progress_background"
android:src="@drawable/ic_file_download_white_36dp"
android:visibility="gone" />
</merge>

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<com.pnikosis.materialishprogress.ProgressWheel
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/progress_wheel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/progress_background"
android:visibility="gone"
app:matProg_barColor="@color/white"
app:matProg_linearProgress="true"
app:matProg_spinSpeed="0.333" />

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.components.TransferControlView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/transfer_controls"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.pnikosis.materialishprogress.ProgressWheel
android:id="@+id/progress_wheel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/progress_background"
android:visibility="gone"
app:matProg_barColor="@color/white"
app:matProg_linearProgress="true"
app:matProg_spinSpeed="0.333" />
<ImageButton android:id="@+id/download_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/progress_background"
android:src="@drawable/ic_file_download_white_36dp"
android:visibility="gone" />
</merge>

@ -2,22 +2,19 @@ package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import org.thoughtcrime.securesms.R;
public class AnimatingToggle extends FrameLayout {
private static final int SPEED_MILLIS = 200;
private View current;
public AnimatingToggle(Context context) {
@ -45,11 +42,20 @@ public class AnimatingToggle extends FrameLayout {
child.setClickable(false);
}
public void display(View view) {
public void display(@Nullable View view) {
display(view, true);
}
protected void display(@Nullable View view, boolean animated) {
if (view == current) return;
animateOut(current, AnimationUtils.loadAnimation(getContext(), R.anim.animation_toggle_out));
animateIn(view, AnimationUtils.loadAnimation(getContext(), R.anim.animation_toggle_in));
if (animated) {
if (current != null) animateOut(current, AnimationUtils.loadAnimation(getContext(), R.anim.animation_toggle_out));
if (view != null) animateIn(view, AnimationUtils.loadAnimation(getContext(), R.anim.animation_toggle_in));
} else {
if (current != null) current.setVisibility(GONE);
if (view != null) view.setVisibility(VISIBLE);
}
current = view;
}
@ -78,12 +84,4 @@ public class AnimatingToggle extends FrameLayout {
view.setVisibility(View.VISIBLE);
view.startAnimation(animation);
}
private int getViewIndex(View view) {
for (int i=0;i<getChildCount();i++) {
if (getChildAt(i) == view) return i;
}
throw new IllegalArgumentException("Not a parent of this view.");
}
}

@ -12,11 +12,7 @@ import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.bumptech.glide.DrawableTypeRequest;
@ -26,12 +22,10 @@ import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.pnikosis.materialishprogress.ProgressWheel;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.PartDatabase;
import org.thoughtcrime.securesms.jobs.PartProgressEvent;
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
import org.thoughtcrime.securesms.mms.RoundedCorners;
import org.thoughtcrime.securesms.mms.Slide;
@ -41,19 +35,17 @@ import org.thoughtcrime.securesms.util.ListenableFutureTask;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import de.greenrobot.event.EventBus;
import ws.com.google.android.mms.pdu.PduPart;
public class ThumbnailView extends FrameLayout {
private static final String TAG = ThumbnailView.class.getSimpleName();
private boolean hideControls;
private ImageView image;
private ProgressWheel progress;
private ImageView removeButton;
private ImageButton downloadButton;
private int backgroundColorHint;
private int radius;
private boolean hideControls;
private ImageView image;
private ImageView removeButton;
private TransferControlView transferControls;
private int backgroundColorHint;
private int radius;
private ListenableFutureTask<SlideDeck> slideDeckFuture = null;
private SlideDeckListener slideDeckListener = null;
@ -73,13 +65,8 @@ public class ThumbnailView extends FrameLayout {
public ThumbnailView(final Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inflate(context, R.layout.thumbnail_view, this);
radius = getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius);
image = (ImageView) findViewById(R.id.thumbnail_image);
progress = (ProgressWheel) findViewById(R.id.progress_wheel);
downloadButton = (ImageButton) findViewById(R.id.download_button);
setOnClickListener(new ThumbnailClickDispatcher());
downloadButton.setOnClickListener(new DownloadClickDispatcher());
radius = getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius);
image = (ImageView) findViewById(R.id.thumbnail_image);
if (attrs != null) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ThumbnailView, 0, 0);
@ -97,40 +84,15 @@ public class ThumbnailView extends FrameLayout {
}
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().registerSticky(this);
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
EventBus.getDefault().unregister(this);
}
private ProgressWheel getProgressWheel() {
if (progress == null) progress = ViewUtil.inflateStub(this, R.id.progress_wheel_stub);
return progress;
}
private void hideProgressWheel() {
if (progress != null) progress.setVisibility(GONE);
}
private ImageView getRemoveButton() {
if (removeButton == null) removeButton = ViewUtil.inflateStub(this, R.id.remove_button_stub);
return removeButton;
}
@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() {
getProgressWheel().setInstantProgress(((float)event.progress) / event.total);
if (event.progress >= event.total) animateOutProgress();
}
});
}
private TransferControlView getTransferControls() {
if (transferControls == null) transferControls = ViewUtil.inflateStub(this, R.id.transfer_controls_stub);
return transferControls;
}
public void setBackgroundColorHint(int color) {
@ -148,7 +110,7 @@ public class ThumbnailView extends FrameLayout {
String slideId = id + "::" + timestamp;
if (!slideId.equals(this.slideId)) {
hideProgressWheel();
if (transferControls != null) transferControls.clear();
image.setImageDrawable(null);
this.slide = null;
this.slideId = slideId;
@ -170,22 +132,21 @@ public class ThumbnailView extends FrameLayout {
}
Log.w(TAG, "loading part with id " + slide.getPart().getPartId() + ", progress " + slide.getTransferProgress());
if (!hideControls && slide.getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_STARTED) {
getProgressWheel().spin();
getProgressWheel().setVisibility(VISIBLE);
downloadButton.setVisibility(GONE);
} else if (!hideControls && slide.getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_AUTO_PENDING ||
slide.getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_FAILED)
{
hideProgressWheel();
downloadButton.setVisibility(VISIBLE);
} else {
hideProgressWheel();
downloadButton.setVisibility(GONE);
}
this.slide = slide;
buildGlideRequest(slide, masterSecret).into(image);
if (this.slide.getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_DONE) {
setOnClickListener(new ThumbnailClickDispatcher());
} else {
setOnClickListener(null);
}
if (!hideControls) {
getTransferControls().setSlide(slide);
getTransferControls().setDownloadClickListener(new DownloadClickDispatcher());
getTransferControls().setVisibility(View.VISIBLE);
}
}
public void setThumbnailClickListener(ThumbnailClickListener listener) {
@ -201,8 +162,9 @@ public class ThumbnailView extends FrameLayout {
}
public void clear() {
if (isContextValid()) Glide.clear(image);
if (slideDeckFuture != null) slideDeckFuture.removeListener(slideDeckListener);
if (isContextValid()) Glide.clear(image);
if (slideDeckFuture != null) slideDeckFuture.removeListener(slideDeckListener);
if (transferControls != null) transferControls.clear();
slide = null;
slideId = null;
slideDeckFuture = null;
@ -211,12 +173,11 @@ public class ThumbnailView extends FrameLayout {
public void hideControls(boolean hideControls) {
this.hideControls = hideControls;
if (hideControls) hideProgressWheel();
if (hideControls && transferControls != null) transferControls.setVisibility(View.GONE);
}
public void showProgressSpinner() {
getProgressWheel().spin();
getProgressWheel().setVisibility(VISIBLE);
getTransferControls().showProgressSpinner();
}
@TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
@ -276,20 +237,6 @@ public class ThumbnailView extends FrameLayout {
.fitCenter();
}
private void animateOutProgress() {
if (progress == null) return;
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) {
getProgressWheel().setVisibility(View.GONE);
}
});
getProgressWheel().startAnimation(animation);
}
private class SlideDeckListener implements FutureTaskListener<SlideDeck> {
private final MasterSecret masterSecret;

@ -0,0 +1,87 @@
package org.thoughtcrime.securesms.components;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageButton;
import com.pnikosis.materialishprogress.ProgressWheel;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.PartDatabase;
import org.thoughtcrime.securesms.jobs.PartProgressEvent;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil;
import de.greenrobot.event.EventBus;
public class TransferControlView extends AnimatingToggle {
private Slide slide;
private ProgressWheel progressWheel;
private ImageButton downloadButton;
public TransferControlView(Context context) {
this(context, null);
}
public TransferControlView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TransferControlView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflate(context, R.layout.transfer_controls_view, this);
this.progressWheel = ViewUtil.findById(this, R.id.progress_wheel);
this.downloadButton = ViewUtil.findById(this, R.id.download_button);
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().registerSticky(this);
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
EventBus.getDefault().unregister(this);
}
public void setSlide(final @NonNull Slide slide) {
this.slide = slide;
if (slide.getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_STARTED) {
showProgressSpinner();
} else if (slide.isPendingDownload()) {
display(downloadButton);
} else {
display(null, false);
}
}
public void showProgressSpinner() {
progressWheel.spin();
display(progressWheel);
}
public void setDownloadClickListener(final @Nullable OnClickListener listener) {
downloadButton.setOnClickListener(listener);
}
public void clear() {
display(null, false);
slide = null;
}
@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() {
progressWheel.setInstantProgress(((float)event.progress) / event.total);
if (event.progress >= event.total) display(null);
}
});
}
}
}

@ -25,6 +25,7 @@ import android.support.annotation.Nullable;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.database.PartDatabase;
import org.thoughtcrime.securesms.util.Util;
import java.io.IOException;
@ -74,6 +75,11 @@ public abstract class Slide {
return part.isInProgress();
}
public boolean isPendingDownload() {
return getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_FAILED ||
getTransferProgress() == PartDatabase.TRANSFER_PROGRESS_AUTO_PENDING;
}
public long getTransferProgress() {
return part.getTransferProgress();
}

@ -68,4 +68,9 @@ public class ViewUtil {
public static <T extends View> T inflateStub(@NonNull View parent, @IdRes int stubId) {
return (T)((ViewStub)parent.findViewById(stubId)).inflate();
}
@SuppressWarnings("unchecked")
public static <T extends View> T findById(@NonNull View parent, @IdRes int resId) {
return (T) parent.findViewById(resId);
}
}

Loading…
Cancel
Save