From 7b6f1bd50791afb45bbd378d4ca30450892d58b4 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Mar 2021 07:54:55 -0500 Subject: [PATCH] add lock recording feature --- res/drawable/ic_chevron_up.xml | 9 + res/drawable/ic_lock.xml | 9 + res/drawable/recording_lock_background.xml | 12 ++ res/layout/conversation_input_panel.xml | 82 +------- res/layout/microphone_recorder_view.xml | 64 ++++++ res/layout/recording_layout.xml | 69 ++++++ res/values/dimens.xml | 2 + res/values/strings.xml | 1 + .../securesms/ConversationActivity.java | 20 ++ .../securesms/components/InputPanel.java | 73 ++++--- .../components/MicrophoneRecorderView.java | 197 ++++++++++++------ .../thoughtcrime/securesms/util/ViewUtil.java | 16 ++ 12 files changed, 386 insertions(+), 168 deletions(-) create mode 100644 res/drawable/ic_chevron_up.xml create mode 100644 res/drawable/ic_lock.xml create mode 100644 res/drawable/recording_lock_background.xml create mode 100644 res/layout/microphone_recorder_view.xml create mode 100644 res/layout/recording_layout.xml diff --git a/res/drawable/ic_chevron_up.xml b/res/drawable/ic_chevron_up.xml new file mode 100644 index 000000000..c54db3bcc --- /dev/null +++ b/res/drawable/ic_chevron_up.xml @@ -0,0 +1,9 @@ + + + diff --git a/res/drawable/ic_lock.xml b/res/drawable/ic_lock.xml new file mode 100644 index 000000000..b642ae75e --- /dev/null +++ b/res/drawable/ic_lock.xml @@ -0,0 +1,9 @@ + + + diff --git a/res/drawable/recording_lock_background.xml b/res/drawable/recording_lock_background.xml new file mode 100644 index 000000000..315310b15 --- /dev/null +++ b/res/drawable/recording_lock_background.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/res/layout/conversation_input_panel.xml b/res/layout/conversation_input_panel.xml index 4b915e098..8fecf7246 100644 --- a/res/layout/conversation_input_panel.xml +++ b/res/layout/conversation_input_panel.xml @@ -26,57 +26,6 @@ android:clipChildren="false" android:clipToPadding="false"> - - - - - - - - - - - - - - - + @@ -173,6 +99,8 @@ + + + + + + + + + + + + + + + diff --git a/res/layout/recording_layout.xml b/res/layout/recording_layout.xml new file mode 100644 index 000000000..451ca0c27 --- /dev/null +++ b/res/layout/recording_layout.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 18208aa90..b4c85434f 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -38,6 +38,8 @@ 5dp 4dp + -150dp + 12dp 12dp 52dp diff --git a/res/values/strings.xml b/res/values/strings.xml index cae3ab1a3..2d9ba1c95 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -335,6 +335,7 @@ Cannot display this file type: %s Failed to load attachment + Lock recording of audio attachment Mailing list diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index a179c7211..d1e02b77f 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -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, diff --git a/src/org/thoughtcrime/securesms/components/InputPanel.java b/src/org/thoughtcrime/securesms/components/InputPanel.java index 3699790ae..16ce41202 100644 --- a/src/org/thoughtcrime/securesms/components/InputPanel.java +++ b/src/org/thoughtcrime/securesms/components/InputPanel.java @@ -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 future = slideToCancel.hide(x); + private long onRecordHideEvent() { + recordLockCancel.setVisibility(View.GONE); + + ListenableFuture future = slideToCancel.hide(); long elapsedTime = recordTime.hide(); future.addListener(new AssertedSuccessListener() { @@ -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 hide(float x) { + public ListenableFuture hide() { final SettableFuture 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 { diff --git a/src/org/thoughtcrime/securesms/components/MicrophoneRecorderView.java b/src/org/thoughtcrime/securesms/components/MicrophoneRecorderView.java index df575a300..263ab3cc9 100644 --- a/src/org/thoughtcrime/securesms/components/MicrophoneRecorderView.java +++ b/src/org/thoughtcrime/securesms/components/MicrophoneRecorderView.java @@ -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(); + } + } } diff --git a/src/org/thoughtcrime/securesms/util/ViewUtil.java b/src/org/thoughtcrime/securesms/util/ViewUtil.java index 762a33072..f4ef2edd6 100644 --- a/src/org/thoughtcrime/securesms/util/ViewUtil.java +++ b/src/org/thoughtcrime/securesms/util/ViewUtil.java @@ -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); }