Skip to content

Commit

Permalink
#810 fix: Intent actions don't work because an empty category was bei…
Browse files Browse the repository at this point in the history
…ng saved with the intent
  • Loading branch information
sds100 committed Nov 4, 2021
1 parent 056f619 commit 48c5174
Showing 1 changed file with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.sds100.keymapper.system.intents

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.InputType
import androidx.core.net.toUri
import androidx.lifecycle.*
Expand Down Expand Up @@ -127,21 +128,19 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),
val description = MutableStateFlow("")
val action = MutableStateFlow("")
val categoriesString = MutableStateFlow("")
val categoriesList = categoriesString.map {
it.split(',')
}

val showChooseActivityButton: StateFlow<Boolean> = target.map {
it == IntentTarget.ACTIVITY
}.stateIn(viewModelScope, SharingStarted.Lazily, false)

val data = MutableStateFlow("")
val targetPackage = MutableStateFlow("")
val targetClass = MutableStateFlow("")
val data: MutableStateFlow<String> = MutableStateFlow("")
val targetPackage: MutableStateFlow<String> = MutableStateFlow("")
val targetClass: MutableStateFlow<String> = MutableStateFlow("")

val flagsString = MutableStateFlow("")
val flagsString: MutableStateFlow<String> = MutableStateFlow("")

private val extras = MutableStateFlow(emptyList<IntentExtraModel>())
private val extras: MutableStateFlow<List<IntentExtraModel>> =
MutableStateFlow(emptyList<IntentExtraModel>())

val extraListItems: StateFlow<List<IntentExtraListItem>> = extras.map { extras ->
extras.map { it.toListItem() }
Expand Down Expand Up @@ -198,39 +197,44 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),

fun onDoneClick() {
viewModelScope.launch {
val intent = Intent().apply {
if (this@ConfigIntentViewModel.action.value.isNotEmpty()) {
action = this@ConfigIntentViewModel.action.value
}
val intent = Intent()

categoriesList.first().forEach {
addCategory(it)
}
if (this@ConfigIntentViewModel.action.value.isNotEmpty()) {
intent.action = this@ConfigIntentViewModel.action.value
}

if (this@ConfigIntentViewModel.data.value.isNotEmpty()) {
data = this@ConfigIntentViewModel.data.value.toUri()
}
val categoriesStringValue = categoriesString.value

if (this@ConfigIntentViewModel.flagsString.value.isNotBlank()) {
flags = flagsString.value.toIntOrNull() ?: 0
}
if (categoriesStringValue.isNotBlank()) {
categoriesStringValue
.split(',')
.map { it.trim() }
.forEach { intent.addCategory(it) }
}

if (this@ConfigIntentViewModel.targetPackage.value.isNotEmpty()) {
`package` = this@ConfigIntentViewModel.targetPackage.value
if (this@ConfigIntentViewModel.data.value.isNotEmpty()) {
intent.data = this@ConfigIntentViewModel.data.value.toUri()
}

if (targetClass.value.isNotEmpty()) {
setClassName(targetPackage.value, targetClass.value)
}
}
if (this@ConfigIntentViewModel.flagsString.value.isNotBlank()) {
intent.flags = flagsString.value.toIntOrNull() ?: 0
}

this@ConfigIntentViewModel.extras.value.forEach { model ->
if (model.name.isEmpty()) return@forEach
if (model.parsedValue == null) return@forEach
if (this@ConfigIntentViewModel.targetPackage.value.isNotEmpty()) {
intent.`package` = this@ConfigIntentViewModel.targetPackage.value

model.type.putInIntent(this, model.name, model.value)
if (targetClass.value.isNotEmpty()) {
intent.setClassName(targetPackage.value, targetClass.value)
}
}

this@ConfigIntentViewModel.extras.value.forEach { extraModel ->
if (extraModel.name.isEmpty()) return@forEach
if (extraModel.parsedValue == null) return@forEach

extraModel.type.putInIntent(intent, extraModel.name, extraModel.value)
}

val uri = intent.toUri(0)

_returnResult.emit(
Expand Down Expand Up @@ -354,43 +358,43 @@ class ConfigIntentViewModel(resourceProvider: ResourceProvider) : ViewModel(),
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED

is IntArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT

is LongExtraType ->
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED

is LongArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT

is ByteExtraType ->
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED

is ByteArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT

is DoubleExtraType ->
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_NUMBER_FLAG_DECIMAL
InputType.TYPE_NUMBER_FLAG_DECIMAL

is DoubleArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_CLASS_TEXT

is FloatExtraType ->
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_NUMBER_FLAG_DECIMAL
InputType.TYPE_NUMBER_FLAG_DECIMAL

is FloatArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_DECIMAL or
InputType.TYPE_NUMBER_FLAG_SIGNED or
InputType.TYPE_CLASS_TEXT

is ShortExtraType ->
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED

is ShortArrayExtraType -> InputType.TYPE_CLASS_NUMBER or
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT
InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_CLASS_TEXT

else -> InputType.TYPE_CLASS_TEXT
}
Expand Down

0 comments on commit 48c5174

Please sign in to comment.