Skip to content

Commit

Permalink
Merge pull request #1172 from keymapperorg/1171-the-string-array-extr…
Browse files Browse the repository at this point in the history
…a-in-the-send-intent-action-dialog-cant-be-saved

#1171 fix: saving arrays into intent actions didn't work
  • Loading branch information
sds100 authored Oct 7, 2023
2 parents 04c798f + 719867d commit 1c21da1
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.sds100.keymapper.actions
import io.github.sds100.keymapper.actions.pinchscreen.PinchScreenType
import io.github.sds100.keymapper.system.camera.CameraLens
import io.github.sds100.keymapper.system.display.Orientation
import io.github.sds100.keymapper.system.intents.IntentExtraModel
import io.github.sds100.keymapper.system.intents.IntentTarget
import io.github.sds100.keymapper.system.volume.DndMode
import io.github.sds100.keymapper.system.volume.RingerMode
Expand Down Expand Up @@ -294,7 +295,8 @@ sealed class ActionData {
data class Intent(
val description: String,
val target: IntentTarget,
val uri: String
val uri: String,
val extras: List<IntentExtraModel>
) : ActionData() {
override val id = ActionId.INTENT
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.sds100.keymapper.data.entities.Extra
import io.github.sds100.keymapper.data.entities.getData
import io.github.sds100.keymapper.system.camera.CameraLens
import io.github.sds100.keymapper.system.display.Orientation
import io.github.sds100.keymapper.system.intents.IntentExtraModel
import io.github.sds100.keymapper.system.intents.IntentTarget
import io.github.sds100.keymapper.system.volume.DndMode
import io.github.sds100.keymapper.system.volume.RingerMode
Expand All @@ -14,6 +15,9 @@ import io.github.sds100.keymapper.util.getKey
import io.github.sds100.keymapper.util.success
import io.github.sds100.keymapper.util.then
import io.github.sds100.keymapper.util.valueOrNull
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import splitties.bitflags.hasFlag

/**
Expand Down Expand Up @@ -214,10 +218,15 @@ object ActionDataEntityMapper {
entity.extras.getData(ActionEntity.EXTRA_INTENT_DESCRIPTION).valueOrNull()
?: return null

val intentExtras = entity.extras.getData(ActionEntity.EXTRA_INTENT_EXTRAS).then {
Json.decodeFromString<List<IntentExtraModel>>(it).success()
}.valueOrNull()

return ActionData.Intent(
target = target,
description = description,
uri = entity.data
uri = entity.data,
extras = intentExtras ?: emptyList()
)
}

Expand Down Expand Up @@ -532,7 +541,8 @@ object ActionDataEntityMapper {
is ActionData.Intent ->
listOf(
Extra(ActionEntity.EXTRA_INTENT_DESCRIPTION, data.description),
Extra(ActionEntity.EXTRA_INTENT_TARGET, INTENT_TARGET_MAP[data.target]!!)
Extra(ActionEntity.EXTRA_INTENT_TARGET, INTENT_TARGET_MAP[data.target]!!),
Extra(ActionEntity.EXTRA_INTENT_EXTRAS, Json.encodeToString(data.extras))
)

is ActionData.InputKeyEvent -> sequence {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ class CreateActionViewModelImpl(
ConfigIntentResult(
oldData.uri,
oldData.target,
oldData.description
oldData.description,
oldData.extras
)
} else {
null
Expand All @@ -465,7 +466,8 @@ class CreateActionViewModelImpl(
return ActionData.Intent(
description = result.description,
target = result.target,
uri = result.uri
uri = result.uri,
extras = result.extras
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class PerformActionsUseCaseImpl(
}

is ActionData.Intent -> {
result = intentAdapter.send(action.target, action.uri)
result = intentAdapter.send(action.target, action.uri, action.extras)
}

is ActionData.InputKeyEvent -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ data class ActionEntity(
const val EXTRA_INTENT_TARGET = "extra_intent_target"
const val EXTRA_INTENT_DESCRIPTION = "extra_intent_description"
const val EXTRA_SOUND_FILE_DESCRIPTION = "extra_sound_file_description"
const val EXTRA_INTENT_EXTRAS = "extra_intent_extras"

//DON'T CHANGE THESE. Used for JSON serialization and parsing.
const val NAME_ACTION_TYPE = "type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import kotlinx.serialization.Serializable
data class ConfigIntentResult(
val uri: String,
val target: IntentTarget,
val description: String
val description: String,
val extras: List<IntentExtraModel>
)
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),
ConfigIntentResult(
uri = uri,
target = target.value,
description = description.value
description = description.value,
extras = extras.value
)
)
}
Expand Down Expand Up @@ -356,13 +357,22 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),

val extrasBundle = intent.extras ?: Bundle.EMPTY

extras.value = extrasBundle.keySet().mapNotNull { key ->
val value = extrasBundle.get(key)

if (value == null) {
return@mapNotNull null
val intentExtras: MutableList<IntentExtraModel> = result.extras.toMutableList()

/**
* See issue #1171. Until version 2.6.1, the extras were assumed to be stored in
* the URI representation of the intent. But the array extras were never saved.
* So to maintain backwards compatibility with old intent actions that stored the arrays
* in the URI, also add the extras from the URI.
*/
for (key in extrasBundle.keySet()) {
// skip the extra if the list already contains it.
if (intentExtras.any { it.name == key }) {
continue
}

val value = extrasBundle.get(key) ?: continue

val extraType = when (value) {
is Boolean -> BoolExtraType()
is BooleanArray -> BoolArrayExtraType()
Expand All @@ -379,15 +389,20 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),
is Short -> ShortExtraType()
is ShortArray -> ShortArrayExtraType()
is String -> StringExtraType()
else -> throw IllegalArgumentException("Don't know how to conver this extra (${value.javaClass.name}) to an IntentExtraType")
is Array<*> -> StringArrayExtraType()
else -> throw IllegalArgumentException("Don't know how to convert this extra (${value.javaClass.name}) to an IntentExtraType")
}

IntentExtraModel(
val extra = IntentExtraModel(
type = extraType,
name = key,
value = value.toString()
)

intentExtras.add(extra)
}

this.extras.value = intentExtras
}

fun setActivity(activityInfo: ActivityInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import io.github.sds100.keymapper.util.Success
class IntentAdapterImpl(context: Context) : IntentAdapter {
private val ctx = context.applicationContext

override fun send(target: IntentTarget, uri: String): Result<*> {
override fun send(target: IntentTarget, uri: String, extras: List<IntentExtraModel>): Result<*> {
val intent = Intent.parseUri(uri, 0)

extras.forEach { e ->
e.type.putInIntent(intent, e.name, e.value)
}

try {
when (target) {
IntentTarget.ACTIVITY -> {
Expand All @@ -37,5 +41,5 @@ class IntentAdapterImpl(context: Context) : IntentAdapter {
}

interface IntentAdapter {
fun send(target: IntentTarget, uri: String): Result<*>
fun send(target: IntentTarget, uri: String, extras: List<IntentExtraModel>): Result<*>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.sds100.keymapper.system.intents

import kotlinx.serialization.Serializable
import java.util.UUID

/**
* Created by sds100 on 01/01/21.
*/
@Serializable
data class IntentExtraModel(
val type: IntentExtraType,
val name: String = "",
Expand Down
Loading

0 comments on commit 1c21da1

Please sign in to comment.