2015-11-18 14:52:26 -08:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.content.Context;
|
2016-12-26 15:14:23 -08:00
|
|
|
import android.net.Uri;
|
2015-11-18 14:52:26 -08:00
|
|
|
import android.os.Build;
|
2015-11-22 10:44:53 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2015-11-18 14:52:26 -08:00
|
|
|
import android.support.annotation.Nullable;
|
2017-01-04 22:43:02 +02:00
|
|
|
import android.support.v4.view.ViewCompat;
|
2017-01-12 21:18:03 +02:00
|
|
|
import android.text.format.DateUtils;
|
2015-11-18 14:52:26 -08:00
|
|
|
import android.util.AttributeSet;
|
2015-11-22 10:44:53 -08:00
|
|
|
import android.view.KeyEvent;
|
2015-11-18 14:52:26 -08:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.animation.AlphaAnimation;
|
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.AnimationSet;
|
|
|
|
import android.view.animation.TranslateAnimation;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-11-22 10:44:53 -08:00
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiDrawer;
|
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiToggle;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-02-07 14:01:37 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
|
|
|
import org.thoughtcrime.securesms.mms.QuoteModel;
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-11-22 10:44:53 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2017-09-09 23:46:48 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2015-11-18 14:52:26 -08:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
|
|
|
|
import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
|
2018-02-07 14:01:37 -08:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
2015-11-22 10:44:53 -08:00
|
|
|
public class InputPanel extends LinearLayout
|
2016-12-26 15:14:23 -08:00
|
|
|
implements MicrophoneRecorderView.Listener,
|
|
|
|
KeyboardAwareLinearLayout.OnKeyboardShownListener,
|
|
|
|
EmojiDrawer.EmojiEventListener
|
|
|
|
{
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
private static final String TAG = InputPanel.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int FADE_TIME = 150;
|
|
|
|
|
2018-02-07 14:01:37 -08:00
|
|
|
private QuoteView quoteView;
|
|
|
|
private EmojiToggle emojiToggle;
|
|
|
|
private ComposeText composeText;
|
|
|
|
private View quickCameraToggle;
|
|
|
|
private View quickAudioToggle;
|
|
|
|
private View buttonToggle;
|
|
|
|
private View recordingContainer;
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
private MicrophoneRecorderView microphoneRecorderView;
|
|
|
|
private SlideToCancel slideToCancel;
|
|
|
|
private RecordTime recordTime;
|
|
|
|
|
|
|
|
private @Nullable Listener listener;
|
2015-11-22 10:44:53 -08:00
|
|
|
private boolean emojiVisible;
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
public InputPanel(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputPanel(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
|
|
|
public InputPanel(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFinishInflate() {
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
2018-02-07 14:01:37 -08:00
|
|
|
View quoteDismiss = findViewById(R.id.quote_dismiss);
|
|
|
|
|
|
|
|
this.quoteView = findViewById(R.id.quote_view);
|
|
|
|
this.emojiToggle = findViewById(R.id.emoji_toggle);
|
|
|
|
this.composeText = findViewById(R.id.embedded_text_editor);
|
|
|
|
this.quickCameraToggle = findViewById(R.id.quick_camera_toggle);
|
|
|
|
this.quickAudioToggle = findViewById(R.id.quick_audio_toggle);
|
|
|
|
this.buttonToggle = findViewById(R.id.button_toggle);
|
|
|
|
this.recordingContainer = findViewById(R.id.recording_container);
|
|
|
|
this.recordTime = new RecordTime(findViewById(R.id.record_time));
|
|
|
|
this.slideToCancel = new SlideToCancel(findViewById(R.id.slide_to_cancel));
|
|
|
|
this.microphoneRecorderView = findViewById(R.id.recorder_view);
|
2015-11-18 14:52:26 -08:00
|
|
|
this.microphoneRecorderView.setListener(this);
|
|
|
|
|
2016-11-21 18:58:10 -08:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
|
2015-11-18 14:52:26 -08:00
|
|
|
this.microphoneRecorderView.setVisibility(View.GONE);
|
|
|
|
this.microphoneRecorderView.setClickable(false);
|
2016-11-21 18:58:10 -08:00
|
|
|
}
|
2015-11-22 10:44:53 -08:00
|
|
|
|
|
|
|
if (TextSecurePreferences.isSystemEmojiPreferred(getContext())) {
|
|
|
|
emojiToggle.setVisibility(View.GONE);
|
|
|
|
emojiVisible = false;
|
|
|
|
} else {
|
|
|
|
emojiToggle.setVisibility(View.VISIBLE);
|
|
|
|
emojiVisible = true;
|
|
|
|
}
|
2018-02-07 14:01:37 -08:00
|
|
|
|
|
|
|
quoteDismiss.setOnClickListener(v -> clearQuote());
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
2017-01-19 11:31:41 -08:00
|
|
|
public void setListener(final @NonNull Listener listener) {
|
2015-11-18 14:52:26 -08:00
|
|
|
this.listener = listener;
|
2015-11-22 10:44:53 -08:00
|
|
|
|
2017-11-24 22:00:30 -08:00
|
|
|
emojiToggle.setOnClickListener(v -> listener.onEmojiToggle());
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:14:23 -08:00
|
|
|
public void setMediaListener(@NonNull MediaListener listener) {
|
|
|
|
composeText.setMediaListener(listener);
|
|
|
|
}
|
|
|
|
|
2018-09-21 15:16:52 -07:00
|
|
|
public void setQuote(@NonNull GlideRequests glideRequests, long id, @NonNull Recipient author, @NonNull Recipient conversation, @NonNull String body, @NonNull SlideDeck attachments) {
|
|
|
|
this.quoteView.setQuote(glideRequests, id, author, conversation, body, false, attachments);
|
2018-02-07 14:01:37 -08:00
|
|
|
this.quoteView.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearQuote() {
|
|
|
|
this.quoteView.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<QuoteModel> getQuote() {
|
|
|
|
if (quoteView.getQuoteId() > 0 && quoteView.getVisibility() == View.VISIBLE) {
|
2018-08-11 09:55:52 -04:00
|
|
|
return Optional.of(new QuoteModel(quoteView.getQuoteId(), quoteView.getAuthor().getAddress(), quoteView.getBody(), false, quoteView.getAttachments()));
|
2018-02-07 14:01:37 -08:00
|
|
|
} else {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 11:31:41 -08:00
|
|
|
public void setEmojiDrawer(@NonNull EmojiDrawer emojiDrawer) {
|
|
|
|
emojiToggle.attach(emojiDrawer);
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:00:30 -08:00
|
|
|
@Override
|
|
|
|
public void onRecordPermissionRequired() {
|
|
|
|
if (listener != null) listener.onRecorderPermissionRequired();
|
|
|
|
}
|
|
|
|
|
2015-11-18 14:52:26 -08:00
|
|
|
@Override
|
|
|
|
public void onRecordPressed(float startPositionX) {
|
|
|
|
if (listener != null) listener.onRecorderStarted();
|
|
|
|
recordTime.display();
|
|
|
|
slideToCancel.display(startPositionX);
|
|
|
|
|
2015-11-22 10:44:53 -08:00
|
|
|
if (emojiVisible) ViewUtil.fadeOut(emojiToggle, FADE_TIME, View.INVISIBLE);
|
2015-11-18 14:52:26 -08:00
|
|
|
ViewUtil.fadeOut(composeText, FADE_TIME, View.INVISIBLE);
|
|
|
|
ViewUtil.fadeOut(quickCameraToggle, FADE_TIME, View.INVISIBLE);
|
|
|
|
ViewUtil.fadeOut(quickAudioToggle, FADE_TIME, View.INVISIBLE);
|
|
|
|
ViewUtil.fadeOut(buttonToggle, FADE_TIME, View.INVISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRecordReleased(float x) {
|
2015-11-20 10:22:36 -08:00
|
|
|
long elapsedTime = onRecordHideEvent(x);
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
if (listener != null) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.d(TAG, "Elapsed time: " + elapsedTime);
|
2015-11-20 10:22:36 -08:00
|
|
|
if (elapsedTime > 1000) {
|
2015-11-18 14:52:26 -08:00
|
|
|
listener.onRecorderFinished();
|
|
|
|
} else {
|
2015-11-20 10:25:08 -08:00
|
|
|
Toast.makeText(getContext(), R.string.InputPanel_tap_and_hold_to_record_a_voice_message_release_to_send, Toast.LENGTH_LONG).show();
|
2015-11-18 14:52:26 -08:00
|
|
|
listener.onRecorderCanceled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRecordMoved(float x, float absoluteX) {
|
|
|
|
slideToCancel.moveTo(x);
|
|
|
|
|
2017-01-04 22:43:02 +02:00
|
|
|
int direction = ViewCompat.getLayoutDirection(this);
|
|
|
|
float position = absoluteX / recordingContainer.getWidth();
|
|
|
|
|
|
|
|
if (direction == ViewCompat.LAYOUT_DIRECTION_LTR && position <= 0.5 ||
|
|
|
|
direction == ViewCompat.LAYOUT_DIRECTION_RTL && position >= 0.6)
|
|
|
|
{
|
2015-11-18 14:52:26 -08:00
|
|
|
this.microphoneRecorderView.cancelAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRecordCanceled(float x) {
|
|
|
|
onRecordHideEvent(x);
|
|
|
|
if (listener != null) listener.onRecorderCanceled();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onPause() {
|
|
|
|
this.microphoneRecorderView.cancelAction();
|
|
|
|
}
|
|
|
|
|
2015-11-22 10:44:57 -08:00
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
composeText.setEnabled(enabled);
|
|
|
|
emojiToggle.setEnabled(enabled);
|
|
|
|
quickAudioToggle.setEnabled(enabled);
|
|
|
|
quickCameraToggle.setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:22:36 -08:00
|
|
|
private long onRecordHideEvent(float x) {
|
|
|
|
ListenableFuture<Void> future = slideToCancel.hide(x);
|
|
|
|
long elapsedTime = recordTime.hide();
|
|
|
|
|
2015-11-18 14:52:26 -08:00
|
|
|
future.addListener(new AssertedSuccessListener<Void>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(Void result) {
|
2015-11-22 10:44:53 -08:00
|
|
|
if (emojiVisible) ViewUtil.fadeIn(emojiToggle, FADE_TIME);
|
2015-11-18 14:52:26 -08:00
|
|
|
ViewUtil.fadeIn(composeText, FADE_TIME);
|
|
|
|
ViewUtil.fadeIn(quickCameraToggle, FADE_TIME);
|
|
|
|
ViewUtil.fadeIn(quickAudioToggle, FADE_TIME);
|
|
|
|
ViewUtil.fadeIn(buttonToggle, FADE_TIME);
|
|
|
|
}
|
|
|
|
});
|
2015-11-20 10:22:36 -08:00
|
|
|
|
|
|
|
return elapsedTime;
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
2015-11-22 10:44:53 -08:00
|
|
|
@Override
|
|
|
|
public void onKeyboardShown() {
|
|
|
|
emojiToggle.setToEmoji();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onKeyEvent(KeyEvent keyEvent) {
|
|
|
|
composeText.dispatchKeyEvent(keyEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEmojiSelected(String emoji) {
|
|
|
|
composeText.insertEmoji(emoji);
|
|
|
|
}
|
|
|
|
|
2018-02-07 14:01:37 -08:00
|
|
|
|
2015-11-18 14:52:26 -08:00
|
|
|
public interface Listener {
|
2017-11-24 22:00:30 -08:00
|
|
|
void onRecorderStarted();
|
|
|
|
void onRecorderFinished();
|
|
|
|
void onRecorderCanceled();
|
|
|
|
void onRecorderPermissionRequired();
|
|
|
|
void onEmojiToggle();
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class SlideToCancel {
|
|
|
|
|
|
|
|
private final View slideToCancelView;
|
|
|
|
|
|
|
|
private float startPositionX;
|
|
|
|
|
|
|
|
public SlideToCancel(View slideToCancelView) {
|
|
|
|
this.slideToCancelView = slideToCancelView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display(float startPositionX) {
|
|
|
|
this.startPositionX = startPositionX;
|
|
|
|
ViewUtil.fadeIn(this.slideToCancelView, FADE_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListenableFuture<Void> hide(float x) {
|
|
|
|
final SettableFuture<Void> future = new SettableFuture<>();
|
2017-01-04 22:43:02 +02:00
|
|
|
float offset = getOffset(x);
|
2015-11-18 14:52:26 -08:00
|
|
|
|
|
|
|
AnimationSet animation = new AnimationSet(true);
|
|
|
|
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, offset,
|
|
|
|
Animation.ABSOLUTE, 0,
|
|
|
|
Animation.RELATIVE_TO_SELF, 0,
|
|
|
|
Animation.RELATIVE_TO_SELF, 0));
|
|
|
|
animation.addAnimation(new AlphaAnimation(1, 0));
|
|
|
|
|
|
|
|
animation.setDuration(MicrophoneRecorderView.ANIMATION_DURATION);
|
|
|
|
animation.setFillBefore(true);
|
|
|
|
animation.setFillAfter(false);
|
|
|
|
|
2018-07-20 19:02:19 -07:00
|
|
|
slideToCancelView.postDelayed(() -> future.set(null), MicrophoneRecorderView.ANIMATION_DURATION);
|
2015-11-18 14:52:26 -08:00
|
|
|
slideToCancelView.setVisibility(View.GONE);
|
|
|
|
slideToCancelView.startAnimation(animation);
|
|
|
|
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void moveTo(float x) {
|
2017-01-04 22:43:02 +02:00
|
|
|
float offset = getOffset(x);
|
2015-11-18 14:52:26 -08:00
|
|
|
Animation animation = new TranslateAnimation(Animation.ABSOLUTE, offset,
|
|
|
|
Animation.ABSOLUTE, offset,
|
|
|
|
Animation.RELATIVE_TO_SELF, 0,
|
|
|
|
Animation.RELATIVE_TO_SELF, 0);
|
|
|
|
|
|
|
|
animation.setDuration(0);
|
|
|
|
animation.setFillAfter(true);
|
|
|
|
animation.setFillBefore(true);
|
|
|
|
|
|
|
|
slideToCancelView.startAnimation(animation);
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:43:02 +02:00
|
|
|
private float getOffset(float x) {
|
|
|
|
return ViewCompat.getLayoutDirection(slideToCancelView) == ViewCompat.LAYOUT_DIRECTION_LTR ?
|
|
|
|
-Math.max(0, this.startPositionX - x) : Math.max(0, x - this.startPositionX);
|
|
|
|
}
|
|
|
|
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class RecordTime implements Runnable {
|
|
|
|
|
|
|
|
private final TextView recordTimeView;
|
|
|
|
private final AtomicLong startTime = new AtomicLong(0);
|
|
|
|
|
|
|
|
private RecordTime(TextView recordTimeView) {
|
|
|
|
this.recordTimeView = recordTimeView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display() {
|
|
|
|
this.startTime.set(System.currentTimeMillis());
|
2017-01-12 21:18:03 +02:00
|
|
|
this.recordTimeView.setText(DateUtils.formatElapsedTime(0));
|
2015-11-18 14:52:26 -08:00
|
|
|
ViewUtil.fadeIn(this.recordTimeView, FADE_TIME);
|
2017-09-09 23:46:48 -07:00
|
|
|
Util.runOnMainDelayed(this, TimeUnit.SECONDS.toMillis(1));
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
|
2015-11-20 10:22:36 -08:00
|
|
|
public long hide() {
|
|
|
|
long elapsedtime = System.currentTimeMillis() - startTime.get();
|
2015-11-18 14:52:26 -08:00
|
|
|
this.startTime.set(0);
|
|
|
|
ViewUtil.fadeOut(this.recordTimeView, FADE_TIME, View.INVISIBLE);
|
2015-11-20 10:22:36 -08:00
|
|
|
return elapsedtime;
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
2015-11-20 10:25:08 -08:00
|
|
|
|
2015-11-18 14:52:26 -08:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
long localStartTime = startTime.get();
|
|
|
|
if (localStartTime > 0) {
|
|
|
|
long elapsedTime = System.currentTimeMillis() - localStartTime;
|
2017-01-12 12:26:50 -08:00
|
|
|
recordTimeView.setText(DateUtils.formatElapsedTime(TimeUnit.MILLISECONDS.toSeconds(elapsedTime)));
|
2017-09-09 23:46:48 -07:00
|
|
|
Util.runOnMainDelayed(this, TimeUnit.SECONDS.toMillis(1));
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 15:14:23 -08:00
|
|
|
|
|
|
|
public interface MediaListener {
|
|
|
|
public void onMediaSelected(@NonNull Uri uri, String contentType);
|
|
|
|
}
|
2015-11-18 14:52:26 -08:00
|
|
|
}
|