add lock recording feature

This commit is contained in:
adbenitez
2021-03-10 07:54:55 -05:00
committed by Hocuri
parent 252bed8ac8
commit 7b6f1bd507
12 changed files with 386 additions and 168 deletions
+9
View File
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M22.1,15l-9.5,-9.5c-0.3,-0.3 -0.8,-0.3 -1.1,0L1.9,15L3,16.1l9,-9l9,9L22.1,15z"/>
</vector>
+9
View File
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M18,9h-1V6c0,-2.8 -2.2,-5 -5,-5S7,3.2 7,6v3H6c-1.1,0 -2,0.9 -2,2v9c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2v-9C20,9.9 19.1,9 18,9zM9,6c0,-1.7 1.3,-3 3,-3s3,1.3 3,3v3H9V6zM13,16.2v2.3h-2v-2.3c-0.6,-0.4 -1,-1 -1,-1.7c0,-1.1 0.9,-2 2,-2s2,0.9 2,2C14,15.2 13.6,15.9 13,16.2z"/>
</vector>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="?attr/input_panel_bg_color" />
<corners
android:radius="20dp" />
</shape>
+5 -77
View File
@@ -26,57 +26,6 @@
android:clipChildren="false"
android:clipToPadding="false">
<LinearLayout
android:id="@+id/recording_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:visibility="gone">
<TextView
android:id="@+id/record_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
style="@style/Signal.Text.Body"
android:text="00:00.0"
android:textColor="?conversation_item_outgoing_text_primary_color"
android:singleLine="true"
android:visibility="gone"
tools:visibility="visible"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="true">
<TextView
android:id="@+id/slide_to_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|start|center_vertical"
android:drawableLeft="@drawable/slidearrow"
android:drawableStart="@drawable/slidearrow"
style="@style/Signal.Text.Preview"
android:singleLine="true"
android:text="@string/chat_record_slide_to_cancel"
android:textColor="@color/slidearrow_color"
android:ellipsize="none"
android:visibility="gone"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="50dp"
android:paddingEnd="50dp"
tools:visibility="visible"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@@ -135,37 +84,14 @@
<org.thoughtcrime.securesms.components.MicrophoneRecorderView
android:id="@+id/recorder_view"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_marginEnd="8dp"
android:layout_gravity="center_vertical"
android:clipChildren="false"
android:clipToPadding="false">
<ImageButton
android:id="@+id/quick_audio_toggle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"
android:paddingLeft="11dp"
android:paddingRight="11dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:src="?quick_mic_icon"
android:background="@null"
android:contentDescription="@string/audio" />
<ImageView
android:id="@+id/quick_audio_fab"
android:layout_width="74dp"
android:layout_height="74dp"
android:src="@drawable/ic_mic_white_48dp"
android:background="@drawable/circle_tintable"
android:backgroundTint="@color/audio_icon"
android:elevation="4dp"
android:visibility="gone"
android:scaleType="center"/>
<include layout="@layout/microphone_recorder_view" />
</org.thoughtcrime.securesms.components.MicrophoneRecorderView>
@@ -173,6 +99,8 @@
</LinearLayout>
<include layout="@layout/recording_layout" />
</FrameLayout>
<org.thoughtcrime.securesms.components.AnimatingToggle
+64
View File
@@ -0,0 +1,64 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/conversation_input_panel">
<ImageButton
android:id="@+id/quick_audio_toggle"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginEnd="2dp"
android:background="@null"
android:contentDescription="@string/audio"
android:scaleType="fitCenter"
android:src="?quick_mic_icon" />
<LinearLayout
android:id="@+id/lock_drop_target"
android:layout_width="40dp"
android:layout_height="64dp"
android:layout_gravity="center"
android:background="@drawable/recording_lock_background"
android:clipChildren="false"
android:orientation="vertical"
android:visibility="gone"
tools:showIn="@layout/conversation_input_panel"
tools:translationY="@dimen/recording_voice_lock_target"
tools:visibility="visible">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:contentDescription="@string/conversation_activity__quick_attachment_drawer_lock_record_description"
android:tint="?conversation_item_outgoing_text_primary_color"
app:srcCompat="@drawable/ic_lock" />
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="6dp"
android:contentDescription="@string/conversation_activity__quick_attachment_drawer_lock_record_description"
android:tint="?conversation_item_outgoing_text_primary_color"
app:srcCompat="@drawable/ic_chevron_up" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/quick_audio_fab"
android:layout_width="74dp"
android:layout_height="74dp"
android:layout_marginStart="-18dp"
android:layout_marginTop="-18dp"
android:padding="18dp"
android:background="@drawable/circle_tintable"
android:clickable="false"
android:visibility="gone"
android:tint="@color/core_white"
android:src="@drawable/ic_mic_white_48dp"
tools:ignore="ContentDescription"
tools:visibility="visible" />
</merge>
+69
View File
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recording_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:visibility="visible"
tools:showIn="@layout/conversation_input_panel">
<TextView
android:id="@+id/record_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
style="@style/Signal.Text.Body"
android:text="00:00.0"
android:textColor="?conversation_item_outgoing_text_primary_color"
android:singleLine="true"
android:visibility="gone"
tools:visibility="visible"/>
<TextView
android:id="@+id/record_cancel"
style="@style/Signal.Text.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:ellipsize="none"
android:gravity="center_vertical"
android:padding="4dp"
android:text="@string/cancel"
android:textColor="@color/audio_icon"
android:visibility="gone"
tools:visibility="visible" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="true">
<TextView
android:id="@+id/slide_to_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|start|center_vertical"
android:drawableLeft="@drawable/slidearrow"
android:drawableStart="@drawable/slidearrow"
style="@style/Signal.Text.Preview"
android:singleLine="true"
android:text="@string/chat_record_slide_to_cancel"
android:textColor="@color/slidearrow_color"
android:ellipsize="none"
android:visibility="gone"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="50dp"
android:paddingEnd="50dp"
tools:visibility="visible"/>
</FrameLayout>
</LinearLayout>
+2
View File
@@ -38,6 +38,8 @@
<dimen name="media_keyboard_provider_icon_padding">5dp</dimen>
<dimen name="media_keyboard_provider_icon_margin">4dp</dimen>
<dimen name="recording_voice_lock_target">-150dp</dimen>
<dimen name="conversation_individual_right_gutter">12dp</dimen>
<dimen name="conversation_individual_left_gutter">12dp</dimen>
<dimen name="conversation_group_left_gutter">52dp</dimen>
+1
View File
@@ -335,6 +335,7 @@
<string name="cannot_display_unsuported_file_type">Cannot display this file type: %s</string>
<string name="attachment_failed_to_load">Failed to load attachment</string>
<string name="conversation_activity__quick_attachment_drawer_lock_record_description">Lock recording of audio attachment</string>
<!-- mailing lists -->
<string name="mailing_list">Mailing list</string>
@@ -21,6 +21,7 @@ import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Color;
@@ -1204,6 +1205,12 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
// handle attachment drawer, camera, recorder
private void updateToggleButtonState() {
if (inputPanel.isRecordingInLockedMode()) {
buttonToggle.display(sendButton);
quickAttachmentToggle.hide();
return;
}
if (composeText.getText().length() == 0 && !attachmentManager.isAttachmentPresent()) {
buttonToggle.display(attachButton);
quickAttachmentToggle.show();
@@ -1269,8 +1276,15 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
audioRecorder.startRecording();
}
@Override
public void onRecorderLocked() {
updateToggleButtonState();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
@Override
public void onRecorderFinished() {
updateToggleButtonState();
Vibrator vibrator = ServiceUtil.getVibrator(this);
vibrator.vibrate(20);
@@ -1307,6 +1321,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
@Override
public void onRecorderCanceled() {
updateToggleButtonState();
Vibrator vibrator = ServiceUtil.getVibrator(this);
vibrator.vibrate(50);
@@ -1404,6 +1419,11 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private class SendButtonListener implements OnClickListener, TextView.OnEditorActionListener {
@Override
public void onClick(View v) {
if (inputPanel.isRecordingInLockedMode()) {
inputPanel.releaseRecordingLock();
return;
}
String rawText = composeText.getTextTrimmed();
if (rawText.length() < 1 && !attachmentManager.isAttachmentPresent()) {
Toast.makeText(ConversationActivity.this, R.string.chat_please_enter_message,
@@ -63,6 +63,7 @@ public class InputPanel extends ConstraintLayout
private View quickAudioToggle;
private View buttonToggle;
private View recordingContainer;
private View recordLockCancel;
private MicrophoneRecorderView microphoneRecorderView;
private SlideToCancel slideToCancel;
@@ -98,11 +99,13 @@ public class InputPanel extends ConstraintLayout
this.quickAudioToggle = findViewById(R.id.quick_audio_toggle);
this.buttonToggle = findViewById(R.id.button_toggle);
this.recordingContainer = findViewById(R.id.recording_container);
this.recordLockCancel = findViewById(R.id.record_cancel);
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);
this.microphoneRecorderView.setListener(this);
this.recordLockCancel.setOnClickListener(v -> microphoneRecorderView.cancelAction());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
this.microphoneRecorderView.setVisibility(View.GONE);
@@ -215,24 +218,24 @@ public class InputPanel extends ConstraintLayout
}
@Override
public void onRecordPressed(float startPositionX) {
public void onRecordPressed() {
if (listener != null) listener.onRecorderStarted();
recordTime.display();
slideToCancel.display(startPositionX);
slideToCancel.display();
if (emojiVisible) ViewUtil.fadeOut(mediaKeyboard, FADE_TIME, View.INVISIBLE);
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);
buttonToggle.animate().alpha(0).setDuration(FADE_TIME).start();
}
@Override
public void onRecordReleased(float x) {
long elapsedTime = onRecordHideEvent(x);
public void onRecordReleased() {
long elapsedTime = onRecordHideEvent();
if (listener != null) {
Log.w(TAG, "Elapsed time: " + elapsedTime);
Log.d(TAG, "Elapsed time: " + elapsedTime);
if (elapsedTime > 1000) {
listener.onRecorderFinished();
} else {
@@ -243,25 +246,32 @@ public class InputPanel extends ConstraintLayout
}
@Override
public void onRecordMoved(float x, float absoluteX) {
slideToCancel.moveTo(x);
public void onRecordMoved(float offsetX, float absoluteX) {
slideToCancel.moveTo(offsetX);
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)
if (ViewUtil.isLtr(this) && position <= 0.5 ||
ViewUtil.isRtl(this) && position >= 0.6)
{
this.microphoneRecorderView.cancelAction();
}
}
@Override
public void onRecordCanceled(float x) {
onRecordHideEvent(x);
public void onRecordCanceled() {
onRecordHideEvent();
if (listener != null) listener.onRecorderCanceled();
}
@Override
public void onRecordLocked() {
slideToCancel.hide();
recordLockCancel.setVisibility(View.VISIBLE);
buttonToggle.animate().alpha(1).setDuration(FADE_TIME).start();
if (listener != null) listener.onRecorderLocked();
}
public void onPause() {
this.microphoneRecorderView.cancelAction();
}
@@ -273,8 +283,10 @@ public class InputPanel extends ConstraintLayout
quickCameraToggle.setEnabled(enabled);
}
private long onRecordHideEvent(float x) {
ListenableFuture<Void> future = slideToCancel.hide(x);
private long onRecordHideEvent() {
recordLockCancel.setVisibility(View.GONE);
ListenableFuture<Void> future = slideToCancel.hide();
long elapsedTime = recordTime.hide();
future.addListener(new AssertedSuccessListener<Void>() {
@@ -284,7 +296,7 @@ public class InputPanel extends ConstraintLayout
ViewUtil.fadeIn(composeText, FADE_TIME);
ViewUtil.fadeIn(quickCameraToggle, FADE_TIME);
ViewUtil.fadeIn(quickAudioToggle, FADE_TIME);
ViewUtil.fadeIn(buttonToggle, FADE_TIME);
buttonToggle.animate().alpha(1).setDuration(FADE_TIME).start();
}
});
@@ -306,8 +318,17 @@ public class InputPanel extends ConstraintLayout
composeText.insertEmoji(emoji);
}
public boolean isRecordingInLockedMode() {
return microphoneRecorderView.isRecordingLocked();
}
public void releaseRecordingLock() {
microphoneRecorderView.unlockAction();
}
public interface Listener {
void onRecorderStarted();
void onRecorderLocked();
void onRecorderFinished();
void onRecorderCanceled();
void onRecorderPermissionRequired();
@@ -318,23 +339,19 @@ public class InputPanel extends ConstraintLayout
private final View slideToCancelView;
private float startPositionX;
public SlideToCancel(View slideToCancelView) {
SlideToCancel(View slideToCancelView) {
this.slideToCancelView = slideToCancelView;
}
public void display(float startPositionX) {
this.startPositionX = startPositionX;
public void display() {
ViewUtil.fadeIn(this.slideToCancelView, FADE_TIME);
}
public ListenableFuture<Void> hide(float x) {
public ListenableFuture<Void> hide() {
final SettableFuture<Void> future = new SettableFuture<>();
float offset = getOffset(x);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, offset,
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, slideToCancelView.getTranslationX(),
Animation.ABSOLUTE, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0));
@@ -351,8 +368,7 @@ public class InputPanel extends ConstraintLayout
return future;
}
public void moveTo(float x) {
float offset = getOffset(x);
void moveTo(float offset) {
Animation animation = new TranslateAnimation(Animation.ABSOLUTE, offset,
Animation.ABSOLUTE, offset,
Animation.RELATIVE_TO_SELF, 0,
@@ -364,11 +380,6 @@ public class InputPanel extends ConstraintLayout
slideToCancelView.startAnimation(animation);
}
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);
}
}
private static class RecordTime implements Runnable {
@@ -3,8 +3,6 @@ package org.thoughtcrime.securesms.components;
import android.Manifest;
import android.content.Context;
import android.graphics.PorterDuff;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
@@ -12,23 +10,35 @@ import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.util.ViewUtil;
public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchListener {
public final class MicrophoneRecorderView extends FrameLayout implements View.OnTouchListener {
enum State {
NOT_RUNNING,
RUNNING_HELD,
RUNNING_LOCKED
}
public static final int ANIMATION_DURATION = 200;
private FloatingRecordButton floatingRecordButton;
private LockDropTarget lockDropTarget;
private @Nullable Listener listener;
private boolean actionInProgress;
private @NonNull State state = State.NOT_RUNNING;
public MicrophoneRecorderView(Context context) {
super(context);
@@ -42,22 +52,49 @@ public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchL
public void onFinishInflate() {
super.onFinishInflate();
ImageView recordButtonFab = ViewUtil.findById(this, R.id.quick_audio_fab);
this.floatingRecordButton = new FloatingRecordButton(getContext(), recordButtonFab);
floatingRecordButton = new FloatingRecordButton(getContext(), findViewById(R.id.quick_audio_fab));
lockDropTarget = new LockDropTarget (getContext(), findViewById(R.id.lock_drop_target));
View recordButton = ViewUtil.findById(this, R.id.quick_audio_toggle);
View recordButton = findViewById(R.id.quick_audio_toggle);
recordButton.setOnTouchListener(this);
}
public void cancelAction() {
if (this.actionInProgress) {
this.actionInProgress = false;
this.floatingRecordButton.hide(this.floatingRecordButton.lastPositionX);
if (state != State.NOT_RUNNING) {
state = State.NOT_RUNNING;
hideUi();
if (listener != null) listener.onRecordCanceled(this.floatingRecordButton.lastPositionX);
if (listener != null) listener.onRecordCanceled();
}
}
public boolean isRecordingLocked() {
return state == State.RUNNING_LOCKED;
}
private void lockAction() {
if (state == State.RUNNING_HELD) {
state = State.RUNNING_LOCKED;
hideUi();
if (listener != null) listener.onRecordLocked();
}
}
public void unlockAction() {
if (state == State.RUNNING_LOCKED) {
state = State.NOT_RUNNING;
hideUi();
if (listener != null) listener.onRecordReleased();
}
}
private void hideUi() {
floatingRecordButton.hide();
lockDropTarget.hide();
}
@Override
public boolean onTouch(View v, final MotionEvent event) {
switch (event.getAction()) {
@@ -65,23 +102,29 @@ public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchL
if (!Permissions.hasAll(getContext(), Manifest.permission.RECORD_AUDIO)) {
if (listener != null) listener.onRecordPermissionRequired();
} else {
this.actionInProgress = true;
this.floatingRecordButton.display(event.getX());
if (listener != null) listener.onRecordPressed(event.getX());
state = State.RUNNING_HELD;
floatingRecordButton.display(event.getX(), event.getY());
lockDropTarget.display();
if (listener != null) listener.onRecordPressed();
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (this.actionInProgress) {
this.actionInProgress = false;
this.floatingRecordButton.hide(event.getX());
if (listener != null) listener.onRecordReleased(event.getX());
if (this.state == State.RUNNING_HELD) {
state = State.NOT_RUNNING;
hideUi();
if (listener != null) listener.onRecordReleased();
}
break;
case MotionEvent.ACTION_MOVE:
if (this.actionInProgress) {
this.floatingRecordButton.moveTo(event.getX());
if (listener != null) listener.onRecordMoved(event.getX(), event.getRawX());
if (this.state == State.RUNNING_HELD) {
this.floatingRecordButton.moveTo(event.getX(), event.getY());
if (listener != null) listener.onRecordMoved(floatingRecordButton.lastOffsetX, event.getRawX());
int dimensionPixelSize = getResources().getDimensionPixelSize(R.dimen.recording_voice_lock_target);
if (floatingRecordButton.lastOffsetY <= dimensionPixelSize) {
lockAction();
}
}
break;
}
@@ -94,10 +137,11 @@ public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchL
}
public interface Listener {
void onRecordPressed(float x);
void onRecordReleased(float x);
void onRecordCanceled(float x);
void onRecordMoved(float x, float absoluteX);
void onRecordPressed();
void onRecordReleased();
void onRecordCanceled();
void onRecordLocked();
void onRecordMoved(float offsetX, float absoluteX);
void onRecordPermissionRequired();
}
@@ -106,78 +150,73 @@ public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchL
private final ImageView recordButtonFab;
private float startPositionX;
private float lastPositionX;
private float startPositionY;
private float lastOffsetX;
private float lastOffsetY;
public FloatingRecordButton(Context context, ImageView recordButtonFab) {
FloatingRecordButton(Context context, ImageView recordButtonFab) {
this.recordButtonFab = recordButtonFab;
this.recordButtonFab.getBackground().setColorFilter(context.getResources()
.getColor(R.color.audio_icon),
PorterDuff.Mode.SRC_IN);
}
public void display(float x) {
void display(float x, float y) {
this.startPositionX = x;
this.lastPositionX = x;
this.startPositionY = y;
recordButtonFab.setVisibility(View.VISIBLE);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -.25f,
Animation.RELATIVE_TO_SELF, -.25f));
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0));
animation.addAnimation(new ScaleAnimation(.5f, 1f, .5f, 1f,
Animation.RELATIVE_TO_SELF, .5f,
Animation.RELATIVE_TO_SELF, .5f));
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setDuration(ANIMATION_DURATION);
animation.setInterpolator(new OvershootInterpolator());
recordButtonFab.startAnimation(animation);
}
public void moveTo(float x) {
this.lastPositionX = x;
void moveTo(float x, float y) {
lastOffsetX = getXOffset(x);
lastOffsetY = getYOffset(y);
float offset = getOffset(x);
if (Math.abs(lastOffsetX) > Math.abs(lastOffsetY)) {
lastOffsetY = 0;
} else {
lastOffsetX = 0;
}
Animation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, offset,
Animation.ABSOLUTE, offset,
Animation.RELATIVE_TO_SELF, -.25f,
Animation.RELATIVE_TO_SELF, -.25f);
translateAnimation.setDuration(0);
translateAnimation.setFillAfter(true);
translateAnimation.setFillBefore(true);
recordButtonFab.startAnimation(translateAnimation);
recordButtonFab.setTranslationX(lastOffsetX);
recordButtonFab.setTranslationY(lastOffsetY);
}
public void hide(float x) {
this.lastPositionX = x;
float offset = getOffset(x);
void hide() {
recordButtonFab.setTranslationX(0);
recordButtonFab.setTranslationY(0);
if (recordButtonFab.getVisibility() != VISIBLE) return;
AnimationSet animation = new AnimationSet(false);
Animation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Animation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, offset,
Animation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, lastOffsetX,
Animation.ABSOLUTE, 0,
Animation.RELATIVE_TO_SELF, -.25f,
Animation.RELATIVE_TO_SELF, -.25f);
Animation.ABSOLUTE, lastOffsetY,
Animation.ABSOLUTE, 0);
scaleAnimation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
translateAnimation.setInterpolator(new DecelerateInterpolator());
animation.addAnimation(scaleAnimation);
animation.addAnimation(translateAnimation);
animation.setDuration(ANIMATION_DURATION);
animation.setFillBefore(true);
animation.setFillAfter(false);
animation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
recordButtonFab.setVisibility(View.GONE);
@@ -185,10 +224,48 @@ public class MicrophoneRecorderView extends FrameLayout implements View.OnTouchL
recordButtonFab.startAnimation(animation);
}
private float getOffset(float x) {
return ViewCompat.getLayoutDirection(recordButtonFab) == ViewCompat.LAYOUT_DIRECTION_LTR ?
-Math.max(0, this.startPositionX - x) : Math.max(0, x - this.startPositionX);
private float getXOffset(float x) {
return ViewUtil.isLtr(recordButtonFab) ? -Math.max(0, this.startPositionX - x)
: Math.max(0, x - this.startPositionX);
}
private float getYOffset(float y) {
return Math.min(0, y - this.startPositionY);
}
}
private static class LockDropTarget {
private final View lockDropTarget;
private final int dropTargetPosition;
LockDropTarget(Context context, View lockDropTarget) {
this.lockDropTarget = lockDropTarget;
this.dropTargetPosition = context.getResources().getDimensionPixelSize(R.dimen.recording_voice_lock_target);
}
void display() {
lockDropTarget.setScaleX(1);
lockDropTarget.setScaleY(1);
lockDropTarget.setAlpha(0);
lockDropTarget.setTranslationY(0);
lockDropTarget.setVisibility(VISIBLE);
lockDropTarget.animate()
.setStartDelay(ANIMATION_DURATION * 2)
.setDuration(ANIMATION_DURATION)
.setInterpolator(new DecelerateInterpolator())
.translationY(dropTargetPosition)
.alpha(1)
.start();
}
void hide() {
lockDropTarget.animate()
.setStartDelay(0)
.setDuration(ANIMATION_DURATION)
.setInterpolator(new LinearInterpolator())
.scaleX(0).scaleY(0)
.start();
}
}
}
@@ -195,6 +195,22 @@ public class ViewUtil {
}
}
public static boolean isLtr(@NonNull View view) {
return isLtr(view.getContext());
}
public static boolean isLtr(@NonNull Context context) {
return context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
}
public static boolean isRtl(@NonNull View view) {
return isRtl(view.getContext());
}
public static boolean isRtl(@NonNull Context context) {
return context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
public static int dpToPx(Context context, int dp) {
return (int)((dp * context.getResources().getDisplayMetrics().density) + 0.5);
}