Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/io/github/sds100/keymapper/UseCases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ object UseCases {
fun createAction(ctx: Context) = CreateActionUseCaseImpl(
ServiceLocator.inputMethodAdapter(ctx),
ServiceLocator.systemFeatureAdapter(ctx),
ServiceLocator.cameraAdapter(ctx),
)

private fun keyMapperImeMessenger(
Expand Down
31 changes: 29 additions & 2 deletions app/src/main/java/io/github/sds100/keymapper/actions/ActionData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,46 @@ sealed class ActionData : Comparable<ActionData> {
}

@Serializable
data class Toggle(override val lens: CameraLens) : Flashlight() {
data class Toggle(
override val lens: CameraLens,
/**
* Strength is null if the default strength should be used. This is a percentage
* of the flash strength so key maps can be exported to other devices with potentially
* different strength levels.
*/
val strengthPercent: Float?,
) : Flashlight() {
override val id = ActionId.TOGGLE_FLASHLIGHT
}

@Serializable
data class Enable(override val lens: CameraLens) : Flashlight() {
data class Enable(
override val lens: CameraLens,
/**
* Strength is null if the default strength should be used. This is a percentage
* of the flash strength so key maps can be exported to other devices with potentially
* different strength levels.
*/
val strengthPercent: Float?,
) : Flashlight() {
override val id = ActionId.ENABLE_FLASHLIGHT
}

@Serializable
data class Disable(override val lens: CameraLens) : Flashlight() {
override val id = ActionId.DISABLE_FLASHLIGHT
}

@Serializable
data class ChangeStrength(
override val lens: CameraLens,
/**
* This can be positive or negative to increase/decrease respectively.
*/
val percent: Float,
) : Flashlight() {
override val id = ActionId.CHANGE_FLASHLIGHT_STRENGTH
}
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,26 +286,40 @@ object ActionDataEntityMapper {

ActionId.TOGGLE_FLASHLIGHT,
ActionId.ENABLE_FLASHLIGHT,
ActionId.DISABLE_FLASHLIGHT,
ActionId.CHANGE_FLASHLIGHT_STRENGTH,
-> {
val lens = entity.extras.getData(ActionEntity.EXTRA_LENS).then {
LENS_MAP.getKey(it)!!.success()
}.valueOrNull() ?: return null

when (actionId) {
ActionId.TOGGLE_FLASHLIGHT ->
ActionData.Flashlight.Toggle(lens)

ActionId.ENABLE_FLASHLIGHT ->
ActionData.Flashlight.Enable(lens)
val flashStrength = entity.extras.getData(ActionEntity.EXTRA_FLASH_STRENGTH).then {
it.toFloatOrNull().success()
}.valueOrNull()

ActionId.DISABLE_FLASHLIGHT ->
ActionData.Flashlight.Disable(lens)
when (actionId) {
ActionId.TOGGLE_FLASHLIGHT -> ActionData.Flashlight.Toggle(lens, flashStrength)
ActionId.ENABLE_FLASHLIGHT -> ActionData.Flashlight.Enable(lens, flashStrength)

ActionId.CHANGE_FLASHLIGHT_STRENGTH -> {
flashStrength ?: return null
ActionData.Flashlight.ChangeStrength(
lens,
flashStrength,
)
}

else -> throw Exception("don't know how to create system action for $actionId")
}
}

ActionId.DISABLE_FLASHLIGHT,
-> {
val lens = entity.extras.getData(ActionEntity.EXTRA_LENS).then {
LENS_MAP.getKey(it)!!.success()
}.valueOrNull() ?: return null
ActionData.Flashlight.Disable(lens)
}

ActionId.TOGGLE_DND_MODE,
ActionId.ENABLE_DND_MODE,
-> {
Expand Down Expand Up @@ -614,9 +628,48 @@ object ActionDataEntityMapper {
),
)

is ActionData.Flashlight -> listOf(
EntityExtra(ActionEntity.EXTRA_LENS, LENS_MAP[data.lens]!!),
)
is ActionData.Flashlight -> {
val lensExtra = EntityExtra(ActionEntity.EXTRA_LENS, LENS_MAP[data.lens]!!)

when (data) {
is ActionData.Flashlight.Toggle -> buildList {
add(lensExtra)

if (data.strengthPercent != null) {
add(
EntityExtra(
ActionEntity.EXTRA_FLASH_STRENGTH,
data.strengthPercent.toString(),
),
)
}
}

is ActionData.Flashlight.Enable -> buildList {
add(lensExtra)

if (data.strengthPercent != null) {
add(
EntityExtra(
ActionEntity.EXTRA_FLASH_STRENGTH,
data.strengthPercent.toString(),
),
)
}
}

is ActionData.Flashlight.Disable -> listOf(lensExtra)
is ActionData.Flashlight.ChangeStrength -> buildList {
add(lensExtra)
add(
EntityExtra(
ActionEntity.EXTRA_FLASH_STRENGTH,
data.percent.toString(),
),
)
}
}
}

is ActionData.SwitchKeyboard -> listOf(
EntityExtra(ActionEntity.EXTRA_IME_ID, data.imeId),
Expand Down Expand Up @@ -773,6 +826,7 @@ object ActionDataEntityMapper {
ActionId.TOGGLE_FLASHLIGHT to "toggle_flashlight",
ActionId.ENABLE_FLASHLIGHT to "enable_flashlight",
ActionId.DISABLE_FLASHLIGHT to "disable_flashlight",
ActionId.CHANGE_FLASHLIGHT_STRENGTH to "change_flashlight_strength",

ActionId.ENABLE_NFC to "nfc_enable",
ActionId.DISABLE_NFC to "nfc_disable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LazyActionErrorSnapshot(
private val soundsManager: SoundsManager,
shizukuAdapter: ShizukuAdapter,
) : ActionErrorSnapshot,
IsActionSupportedUseCase by IsActionSupportedUseCaseImpl(systemFeatureAdapter) {
IsActionSupportedUseCase by IsActionSupportedUseCaseImpl(systemFeatureAdapter, cameraAdapter) {
private val keyMapperImeHelper = KeyMapperImeHelper(inputMethodAdapter)

private val isCompatibleImeEnabled by lazy { keyMapperImeHelper.isCompatibleImeEnabled() }
Expand All @@ -34,11 +34,11 @@ class LazyActionErrorSnapshot(
private val grantedPermissions: MutableMap<Permission, Boolean> = mutableMapOf()
private val flashLenses by lazy {
buildSet {
if (cameraAdapter.hasFlashFacing(CameraLens.FRONT)) {
if (cameraAdapter.getFlashInfo(CameraLens.FRONT) != null) {
add(CameraLens.FRONT)
}

if (cameraAdapter.hasFlashFacing(CameraLens.BACK)) {
if (cameraAdapter.getFlashInfo(CameraLens.BACK) != null) {
add(CameraLens.BACK)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ enum class ActionId {
TOGGLE_FLASHLIGHT,
ENABLE_FLASHLIGHT,
DISABLE_FLASHLIGHT,
CHANGE_FLASHLIGHT_STRENGTH,

ENABLE_NFC,
DISABLE_NFC,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sds100.keymapper.actions

import android.os.Build
import android.view.KeyEvent
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Android
Expand Down Expand Up @@ -240,15 +241,56 @@ class ActionUiHelper(
)

is ActionData.Flashlight -> {
val resId = when (action) {
is ActionData.Flashlight.Toggle -> R.string.action_toggle_flashlight_formatted
is ActionData.Flashlight.Enable -> R.string.action_enable_flashlight_formatted
is ActionData.Flashlight.Disable -> R.string.action_disable_flashlight_formatted
}

val lensString = getString(CameraLensUtils.getLabel(action.lens))

getString(resId, lensString)
when (action) {
is ActionData.Flashlight.Toggle -> {
if (action.strengthPercent == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
getString(R.string.action_toggle_flashlight_formatted, lensString)
} else {
getString(
R.string.action_toggle_flashlight_with_strength,
arrayOf(
lensString,
(action.strengthPercent * 100).toInt(),
),
)
}
}

is ActionData.Flashlight.Enable -> {
if (action.strengthPercent == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
getString(R.string.action_enable_flashlight_formatted, lensString)
} else {
getString(
R.string.action_enable_flashlight_with_strength,
arrayOf(
lensString,
(action.strengthPercent * 100).toInt(),
),
)
}
}

is ActionData.Flashlight.Disable -> getString(
R.string.action_disable_flashlight_formatted,
lensString,
)

is ActionData.Flashlight.ChangeStrength -> {
if (action.percent > 0) {
getString(
R.string.action_flashlight_increase_strength_formatted,
arrayOf(lensString, (action.percent * 100).toInt()),
)
} else {
getString(
R.string.action_flashlight_decrease_strength_formatted,
arrayOf(lensString, (action.percent * 100).toInt()),
)
}
}
}
}

is ActionData.SwitchKeyboard -> getInputMethodLabel(action.imeId).handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT -> ActionCategory.CAMERA_SOUND
ActionId.ENABLE_FLASHLIGHT -> ActionCategory.CAMERA_SOUND
ActionId.DISABLE_FLASHLIGHT -> ActionCategory.CAMERA_SOUND
ActionId.CHANGE_FLASHLIGHT_STRENGTH -> ActionCategory.CAMERA_SOUND
ActionId.SOUND -> ActionCategory.CAMERA_SOUND

ActionId.ENABLE_NFC -> ActionCategory.CONNECTIVITY
Expand Down Expand Up @@ -294,6 +295,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT -> R.string.action_toggle_flashlight
ActionId.ENABLE_FLASHLIGHT -> R.string.action_enable_flashlight
ActionId.DISABLE_FLASHLIGHT -> R.string.action_disable_flashlight
ActionId.CHANGE_FLASHLIGHT_STRENGTH -> R.string.action_flashlight_change_strength
ActionId.ENABLE_NFC -> R.string.action_nfc_enable
ActionId.DISABLE_NFC -> R.string.action_nfc_disable
ActionId.TOGGLE_NFC -> R.string.action_nfc_toggle
Expand Down Expand Up @@ -404,6 +406,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT -> R.drawable.ic_flashlight
ActionId.ENABLE_FLASHLIGHT -> R.drawable.ic_flashlight
ActionId.DISABLE_FLASHLIGHT -> R.drawable.ic_flashlight_off
ActionId.CHANGE_FLASHLIGHT_STRENGTH -> R.drawable.ic_flashlight
ActionId.ENABLE_NFC -> R.drawable.ic_outline_nfc_24
ActionId.DISABLE_NFC -> R.drawable.ic_nfc_off
ActionId.TOGGLE_NFC -> R.drawable.ic_outline_nfc_24
Expand Down Expand Up @@ -471,6 +474,9 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT,
-> Build.VERSION_CODES.M

ActionId.CHANGE_FLASHLIGHT_STRENGTH,
-> Build.VERSION_CODES.TIRAMISU

ActionId.TOGGLE_KEYBOARD,
ActionId.SHOW_KEYBOARD,
ActionId.HIDE_KEYBOARD,
Expand Down Expand Up @@ -533,6 +539,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT,
ActionId.ENABLE_FLASHLIGHT,
ActionId.DISABLE_FLASHLIGHT,
ActionId.CHANGE_FLASHLIGHT_STRENGTH,
-> listOf(PackageManager.FEATURE_CAMERA_FLASH)

else -> emptyList()
Expand Down Expand Up @@ -596,6 +603,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT,
ActionId.ENABLE_FLASHLIGHT,
ActionId.DISABLE_FLASHLIGHT,
ActionId.CHANGE_FLASHLIGHT_STRENGTH,
-> return listOf(Permission.CAMERA)

ActionId.ENABLE_NFC,
Expand Down Expand Up @@ -714,6 +722,7 @@ object ActionUtils {
ActionId.TOGGLE_FLASHLIGHT -> Icons.Outlined.FlashlightOn
ActionId.ENABLE_FLASHLIGHT -> Icons.Outlined.FlashlightOn
ActionId.DISABLE_FLASHLIGHT -> Icons.Outlined.FlashlightOff
ActionId.CHANGE_FLASHLIGHT_STRENGTH -> Icons.Outlined.FlashlightOn
ActionId.ENABLE_NFC -> Icons.Outlined.Nfc
ActionId.DISABLE_NFC -> KeyMapperIcons.NfcOff
ActionId.TOGGLE_NFC -> Icons.Outlined.Nfc
Expand Down Expand Up @@ -800,7 +809,7 @@ fun ActionData.isEditable(): Boolean = when (this) {
is ActionData.Rotation.CycleRotations,
is ActionData.Flashlight.Toggle,
is ActionData.Flashlight.Enable,
is ActionData.Flashlight.Disable,
is ActionData.Flashlight.ChangeStrength,
is ActionData.TapScreen,
is ActionData.SwipeScreen,
is ActionData.PinchScreen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ fun ActionsScreen(modifier: Modifier = Modifier, viewModel: ConfigActionsViewMod
)
}

EnableFlashlightActionBottomSheet(viewModel.createActionDelegate)
ChangeFlashlightStrengthActionBottomSheet(viewModel.createActionDelegate)

ActionsScreen(
modifier = modifier,
state = state,
Expand Down Expand Up @@ -300,7 +303,10 @@ private fun EmptyPreview() {
ShortcutModel(
icon = ComposeIconInfo.Vector(Icons.Rounded.FlashlightOn),
text = "Toggle Back flashlight",
data = ActionData.Flashlight.Toggle(lens = CameraLens.BACK),
data = ActionData.Flashlight.Toggle(
lens = CameraLens.BACK,
strengthPercent = null,
),
),
),
),
Expand Down Expand Up @@ -339,7 +345,10 @@ private fun LoadedPreview() {
ShortcutModel(
icon = ComposeIconInfo.Vector(Icons.Rounded.FlashlightOn),
text = "Toggle Back flashlight",
data = ActionData.Flashlight.Toggle(lens = CameraLens.BACK),
data = ActionData.Flashlight.Toggle(
lens = CameraLens.BACK,
strengthPercent = null,
),
),
),
isReorderingEnabled = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ fun ChooseActionScreen(
val state by viewModel.groups.collectAsStateWithLifecycle()
val query by viewModel.searchQuery.collectAsStateWithLifecycle()

EnableFlashlightActionBottomSheet(viewModel.createActionDelegate)
ChangeFlashlightStrengthActionBottomSheet(viewModel.createActionDelegate)

ChooseActionScreen(
modifier = modifier,
state = state,
Expand Down
Loading