Skip to content

Commit

Permalink
feature: pause and resume buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
DevEmperor committed Jul 4, 2024
1 parent 5ba9cda commit b9b2b64
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public class DictateInputMethodService extends InputMethodService {
private Runnable deleteRunnable;
private Runnable recordTimeRunnable;

private long startTime;
private long elapsedTime;
private boolean isDeleting = false;
private boolean isRecording = false;
private boolean isPaused = false;
private boolean vibrationEnabled = true;
private String translationPrompt = "";

Expand All @@ -77,9 +78,11 @@ public class DictateInputMethodService extends InputMethodService {
private ConstraintLayout dictateKeyboardView;
private MaterialButton settingsButton;
private MaterialButton recordButton;
private MaterialButton pauseButton;
private MaterialButton backspaceButton;
private MaterialButton switchButton;
private MaterialButton spaceButton;
private MaterialButton stopButton;
private MaterialButton enterButton;

private ConstraintLayout infoCl;
Expand All @@ -105,9 +108,11 @@ public View onCreateInputView() {

settingsButton = dictateKeyboardView.findViewById(R.id.settings_btn);
recordButton = dictateKeyboardView.findViewById(R.id.record_btn);
pauseButton = dictateKeyboardView.findViewById(R.id.pause_btn);
backspaceButton = dictateKeyboardView.findViewById(R.id.backspace_btn);
switchButton = dictateKeyboardView.findViewById(R.id.switch_btn);
spaceButton = dictateKeyboardView.findViewById(R.id.space_btn);
stopButton = dictateKeyboardView.findViewById(R.id.stop_btn);
enterButton = dictateKeyboardView.findViewById(R.id.enter_btn);

infoCl = dictateKeyboardView.findViewById(R.id.info_cl);
Expand All @@ -123,6 +128,16 @@ public View onCreateInputView() {
e.printStackTrace(); //TODO firebase crashlytics
}

recordTimeRunnable = new Runnable() {
@Override
public void run() {
elapsedTime += 100;
recordButton.setText(getString(R.string.dictate_send,
String.format(Locale.getDefault(), "%02d:%02d", (int) (elapsedTime / 60000), (int) (elapsedTime / 1000) % 60)));
recordTimeHandler.postDelayed(this, 100);
}
};

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) switchButton.setEnabled(false);

settingsButton.setOnClickListener(v -> {
Expand All @@ -143,6 +158,23 @@ public View onCreateInputView() {
}
});

pauseButton.setOnClickListener(v -> {
vibrate();
if (recorder != null) {
if (isPaused) {
recorder.resume();
recordTimeHandler.post(recordTimeRunnable);
pauseButton.setForeground(AppCompatResources.getDrawable(context, R.drawable.baseline_pause_24));
isPaused = false;
} else {
recorder.pause();
recordTimeHandler.removeCallbacks(recordTimeRunnable);
pauseButton.setForeground(AppCompatResources.getDrawable(context, R.drawable.ic_baseline_mic_24));
isPaused = true;
}
}
});

backspaceButton.setOnClickListener(v -> {
vibrate();
deleteOneCharacter();
Expand Down Expand Up @@ -188,6 +220,27 @@ public void run() {
}
});

stopButton.setOnClickListener(v -> {
vibrate();
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;

if (recordTimeRunnable != null) {
recordTimeHandler.removeCallbacks(recordTimeRunnable);
}
}
isRecording = false;
recordButton.setText(R.string.dictate_record);
recordButton.setIcon(AppCompatResources.getDrawable(this, R.drawable.ic_baseline_mic_24));
settingsButton.setEnabled(true);
switchButton.setEnabled(true);
recordButton.setEnabled(true);
pauseButton.setVisibility(View.GONE);
stopButton.setVisibility(View.GONE);
});

enterButton.setOnClickListener(v -> {
vibrate();

Expand Down Expand Up @@ -215,7 +268,6 @@ public void onUnbindInput() {

if (recordTimeRunnable != null) {
recordTimeHandler.removeCallbacks(recordTimeRunnable);
recordTimeRunnable = null;
}
}
}
Expand Down Expand Up @@ -254,18 +306,11 @@ private void startRecording() {
recordButton.setIcon(AppCompatResources.getDrawable(this, R.drawable.ic_baseline_send_24));
settingsButton.setEnabled(false);
switchButton.setEnabled(false);
pauseButton.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.VISIBLE);
isRecording = true;

startTime = System.currentTimeMillis();
recordTimeRunnable = new Runnable() {
@Override
public void run() {
long elapsedTime = System.currentTimeMillis() - startTime;
recordButton.setText(getString(R.string.dictate_send,
String.format(Locale.getDefault(), "%02d:%02d", (int) (elapsedTime / 60000), (int) (elapsedTime / 1000) % 60)));
recordTimeHandler.postDelayed(this, 1000);
}
};
elapsedTime = 0;
recordTimeHandler.post(recordTimeRunnable);
}

Expand All @@ -277,11 +322,12 @@ private void stopRecording() {

if (recordTimeRunnable != null) {
recordTimeHandler.removeCallbacks(recordTimeRunnable);
recordTimeRunnable = null;
}

recordButton.setText(R.string.dictate_sending);
recordButton.setEnabled(false);
pauseButton.setVisibility(View.GONE);
stopButton.setVisibility(View.GONE);
isRecording = false;

String customApiHost = sp.getString("net.devemperor.dictate.custom_api_host", getString(R.string.dictate_custom_host_hint));
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_pause_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M6,19h4L10,5L6,5v14zM14,5v14h4L18,5h-4z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_stop_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M6,6h12v12H6z"/>

</vector>
91 changes: 66 additions & 25 deletions app/src/main/res/layout/activity_dictate_keyboard_view.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/dictate_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:id="@+id/settings_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginStart="16dp"
android:foreground="@drawable/ic_baseline_settings_24"
android:foregroundGravity="center"
android:minWidth="64dp"
app:layout_constraintBottom_toTopOf="@+id/switch_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info_cl" />

<com.google.android.material.button.MaterialButton
android:id="@+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:foreground="@drawable/ic_baseline_stop_24"
android:foregroundGravity="center"
android:minWidth="0dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/switch_btn"
tools:visibility="visible" />

<com.google.android.material.button.MaterialButton
android:id="@+id/record_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_margin="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/dictate_record"
android:textSize="16sp"
app:icon="@drawable/ic_baseline_mic_24"
app:layout_constraintBottom_toTopOf="@+id/space_btn"
app:layout_constraintEnd_toStartOf="@+id/backspace_btn"
app:layout_constraintStart_toEndOf="@+id/switch_btn"
app:layout_constraintStart_toEndOf="@+id/settings_btn"
app:layout_constraintTop_toBottomOf="@+id/info_cl" />

<com.google.android.material.button.MaterialButton
android:id="@+id/settings_btn"
android:id="@+id/pause_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:foreground="@drawable/ic_baseline_settings_24"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:foreground="@drawable/baseline_pause_24"
android:foregroundGravity="center"
app:layout_constraintBottom_toTopOf="@+id/switch_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info_cl" />
android:minWidth="0dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/enter_btn"
tools:visibility="visible" />

<com.google.android.material.button.MaterialButton
android:id="@+id/backspace_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginEnd="16dp"
android:foreground="@drawable/ic_baseline_keyboard_backspace_24"
android:foregroundGravity="center"
android:minWidth="64dp"
app:layout_constraintBottom_toTopOf="@+id/enter_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info_cl" />
Expand All @@ -49,51 +85,54 @@
android:layout_marginBottom="16dp"
android:foreground="@drawable/ic_baseline_keyboard_hide_24"
android:foregroundGravity="center"
android:minWidth="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/enter_btn"
android:layout_width="wrap_content"
android:id="@+id/space_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:foreground="@drawable/ic_baseline_subdirectory_arrow_left_24"
android:foregroundGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toStartOf="@+id/pause_btn"
app:layout_constraintStart_toEndOf="@+id/stop_btn" />

<com.google.android.material.button.MaterialButton
android:id="@+id/space_btn"
android:layout_width="0dp"
android:id="@+id/enter_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:foreground="@drawable/ic_baseline_subdirectory_arrow_left_24"
android:foregroundGravity="center"
android:minWidth="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/enter_btn"
app:layout_constraintStart_toEndOf="@+id/settings_btn" />
app:layout_constraintEnd_toEndOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/info_cl"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible">

<TextView
android:id="@+id/info_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="@string/dictate_internet_error_msg"
android:textColor="@color/dictate_red"
android:textSize="16sp"
android:textStyle="italic"
android:textColor="@color/dictate_red"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/info_yes_btn"
app:layout_constraintHorizontal_bias="0.5"
Expand All @@ -103,31 +142,33 @@
<Button
android:id="@+id/info_yes_btn"
style="@style/Widget.AppCompat.Button.Small"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:foreground="@drawable/ic_baseline_check_circle_outline_24"
android:foregroundGravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/info_no_btn"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/info_tv"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />

<Button
android:id="@+id/info_no_btn"
style="@style/Widget.AppCompat.Button.Small"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:foreground="@drawable/ic_baseline_not_interested_24"
android:foregroundGravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/info_yes_btn"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit b9b2b64

Please sign in to comment.