Skip to content

Commit 0edd32d

Browse files
committed
#1066 feat: complete action to mute/unmute microphone
1 parent b06b40d commit 0edd32d

File tree

11 files changed

+34
-16
lines changed

11 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- #727 Actions to send SMS messages: "Send SMS" and "Compose SMS"
1818
- #1819 Explain how to enable the accessibility service restricted setting
1919
- #661 Action to execute shell commands.
20+
- #1066 Action to mute/unmute microphone.
2021

2122
## Removed
2223

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ sealed class ActionData : Comparable<ActionData> {
191191
}
192192

193193
@Serializable
194-
data object UnMute : Microphone() {
194+
data object Unmute : Microphone() {
195195
override val id = ActionId.UNMUTE_MICROPHONE
196196
}
197197

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionDataEntityMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ object ActionDataEntityMapper {
318318
}
319319

320320
ActionId.MUTE_MICROPHONE -> ActionData.Microphone.Mute
321-
ActionId.UNMUTE_MICROPHONE -> ActionData.Microphone.UnMute
321+
ActionId.UNMUTE_MICROPHONE -> ActionData.Microphone.Unmute
322322
ActionId.TOGGLE_MUTE_MICROPHONE -> ActionData.Microphone.Toggle
323323

324324
ActionId.TOGGLE_FLASHLIGHT,

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionUiHelper.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ class ActionUiHelper(
6060
}
6161

6262
if (action.device != null) {
63-
val name = if (action.device.name.isBlank()) {
63+
val name = action.device.name.ifBlank {
6464
getString(R.string.unknown_device_name)
65-
} else {
66-
action.device.name
6765
}
6866

6967
val nameToShow = if (showDeviceDescriptors) {
@@ -603,6 +601,10 @@ class ActionUiHelper(
603601
R.string.action_send_sms_description,
604602
arrayOf(action.message, action.number),
605603
)
604+
605+
ActionData.Microphone.Mute -> getString(R.string.action_mute_microphone)
606+
ActionData.Microphone.Toggle -> getString(R.string.action_toggle_mute_microphone)
607+
ActionData.Microphone.Unmute -> getString(R.string.action_unmute_microphone)
606608
}
607609

608610
fun getIcon(action: ActionData): ComposeIconInfo = when (action) {

base/src/main/java/io/github/sds100/keymapper/base/actions/ActionUtils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import androidx.compose.material.icons.outlined.Keyboard
4141
import androidx.compose.material.icons.outlined.KeyboardHide
4242
import androidx.compose.material.icons.outlined.Link
4343
import androidx.compose.material.icons.outlined.Lock
44+
import androidx.compose.material.icons.outlined.Mic
45+
import androidx.compose.material.icons.outlined.MicOff
4446
import androidx.compose.material.icons.outlined.MoreVert
4547
import androidx.compose.material.icons.outlined.Nfc
4648
import androidx.compose.material.icons.outlined.NotStarted

base/src/main/java/io/github/sds100/keymapper/base/actions/CreateActionDelegate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class CreateActionDelegate(
314314
}
315315

316316
ActionId.UNMUTE_MICROPHONE -> {
317-
return ActionData.Microphone.UnMute
317+
return ActionData.Microphone.Unmute
318318
}
319319

320320
ActionId.TOGGLE_MUTE_MICROPHONE -> {

base/src/main/java/io/github/sds100/keymapper/base/actions/PerformActionsUseCase.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,27 @@ class PerformActionsUseCaseImpl @AssistedInject constructor(
360360
}
361361

362362
is ActionData.Microphone.Mute -> {
363-
result = audioAdapter.muteMicrophone()
363+
result = audioAdapter.muteMicrophone().onSuccess {
364+
toastAdapter.show(resourceProvider.getString(R.string.toast_microphone_muted))
365+
}
364366
}
365367

366-
is ActionData.Microphone.UnMute -> {
367-
result = audioAdapter.unmuteMicrophone()
368+
is ActionData.Microphone.Unmute -> {
369+
result = audioAdapter.unmuteMicrophone().onSuccess {
370+
toastAdapter.show(resourceProvider.getString(R.string.toast_microphone_unmuted))
371+
}
368372
}
369373

370374
is ActionData.Microphone.Toggle -> {
371-
result = audioAdapter.toggleMuteMicrophone()
375+
result = if (audioAdapter.isMicrophoneMuted) {
376+
audioAdapter.unmuteMicrophone().onSuccess {
377+
toastAdapter.show(resourceProvider.getString(R.string.toast_microphone_unmuted))
378+
}
379+
} else {
380+
audioAdapter.muteMicrophone().onSuccess {
381+
toastAdapter.show(resourceProvider.getString(R.string.toast_microphone_muted))
382+
}
383+
}
372384
}
373385

374386
is ActionData.TapScreen -> {

base/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@
201201
<string name="toast_no_sound_files">You have saved no sound files!</string>
202202
<string name="toast_granted_itself_write_secure_settings_with_shizuku">Key Mapper has used Shizuku to grant itself WRITE_SECURE_SETTINGS permission</string>
203203
<string name="toast_granted_itself_write_secure_settings_with_root">Key Mapper has used Root to grant itself WRITE_SECURE_SETTINGS permission</string>
204+
<string name="toast_microphone_muted">Microphone muted</string>
205+
<string name="toast_microphone_unmuted">Microphone unmuted</string>
204206
<!-- Toasts -->
205207

206208
<!-- Extra labels -->

system/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<uses-permission android:name="android.permission.CALL_PHONE" />
2525
<uses-permission android:name="android.permission.SEND_SMS"
2626
tools:ignore="SmsAndCallLogPolicy" />
27+
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
2728

2829
<uses-feature
2930
android:name="android.hardware.telephony"

system/src/main/java/io/github/sds100/keymapper/system/volume/AndroidVolumeAdapter.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class AndroidVolumeAdapter @Inject constructor(
5353
}
5454
}
5555

56+
override val isMicrophoneMuted: Boolean
57+
get() = audioManager.isMicrophoneMute
58+
5659
override fun raiseVolume(stream: VolumeStream?, showVolumeUi: Boolean): KMResult<*> =
5760
stream.convert().then { streamType ->
5861
adjustVolume(AudioManager.ADJUST_RAISE, showVolumeUi, streamType)
@@ -135,11 +138,6 @@ class AndroidVolumeAdapter @Inject constructor(
135138
return Success(Unit)
136139
}
137140

138-
override fun toggleMuteMicrophone(): KMResult<*> {
139-
audioManager.isMicrophoneMute = !audioManager.isMicrophoneMute
140-
return Success(Unit)
141-
}
142-
143141
private fun DndMode.convert(): Int = when (this) {
144142
DndMode.ALARMS -> NotificationManager.INTERRUPTION_FILTER_ALARMS
145143
DndMode.PRIORITY -> NotificationManager.INTERRUPTION_FILTER_PRIORITY

0 commit comments

Comments
 (0)