Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge feature/action shortcut into develop #464

Merged
merged 11 commits into from
Sep 8, 2020
Merged
20 changes: 20 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,32 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ui.activity.HomeActivity"
android:label="@string/app_name" />

<activity android:name=".ui.activity.AppIntroActivity" />

<activity
android:name=".ui.activity.LaunchActionShortcutActivity"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>

<activity
android:name=".ui.activity.CreateActionShortcutActivity"
android:label="@string/shortcut_label_action">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>

<receiver android:name=".broadcastreceiver.KeyMapperBroadcastReceiver">
<intent-filter>
<action android:name="android.hardware.input.action.QUERY_KEYBOARD_LAYOUTS" />
Expand Down
26 changes: 23 additions & 3 deletions app/src/main/java/io/github/sds100/keymapper/data/model/Action.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.github.sds100.keymapper.data.model

import android.content.Context
import com.github.salomonbrys.kotson.byArray
import com.github.salomonbrys.kotson.byInt
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.util.ActionType
Expand Down Expand Up @@ -116,12 +120,15 @@ data class Action(
return Action(ActionType.APP, packageName)
}

fun appShortcutAction(name: String, packageName: String, uri: String): Action {
fun appShortcutAction(name: String, packageName: String?, uri: String): Action {
val extras = mutableListOf(
Extra(EXTRA_SHORTCUT_TITLE, name),
Extra(EXTRA_PACKAGE_NAME, packageName)
)

packageName?.let {
extras.add(Extra(EXTRA_PACKAGE_NAME, packageName))
}

return Action(ActionType.APP_SHORTCUT, data = uri, extras = extras)
}

Expand All @@ -137,7 +144,6 @@ data class Action(
return Action(
ActionType.KEY_EVENT,
keyCode.toString(),
flags = ACTION_FLAG_REPEAT,
extras = listOf(Extra(EXTRA_KEY_EVENT_META_STATE, metaState.toString()))
)
}
Expand Down Expand Up @@ -186,6 +192,20 @@ data class Action(
extras = extras
)
}

val DESERIALIZER = jsonDeserializer {
val typeString by it.json.byString(NAME_ACTION_TYPE)
val type = ActionType.valueOf(typeString)

val data by it.json.byString(NAME_DATA)

val extrasJsonArray by it.json.byArray(NAME_EXTRAS)
val extraList = it.context.deserialize<List<Extra>>(extrasJsonArray) ?: listOf()

val flags by it.json.byInt(NAME_FLAGS)

Action(type, data, extraList.toMutableList(), flags)
}
}

constructor(type: ActionType, data: String, extra: Extra) : this(type, data, mutableListOf(extra))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ class TriggerBehavior(keys: List<Trigger.Key>, @Trigger.Mode mode: Int, flags: I
}
}

class ActionBehavior(action: Action, @Trigger.Mode triggerMode: Int, triggerKeys: List<Trigger.Key>) : Serializable {
class ActionBehavior(
action: Action,
@Trigger.Mode triggerMode: Int? = null,
triggerKeys: List<Trigger.Key>? = null
) : Serializable {
companion object {
const val ID_REPEAT_RATE = "repeat_rate"
const val ID_REPEAT = "repeat"
Expand All @@ -223,7 +227,11 @@ class ActionBehavior(action: Action, @Trigger.Mode triggerMode: Int, triggerKeys
val repeat = BehaviorOption(
id = ID_REPEAT,
value = action.flags.hasFlag(Action.ACTION_FLAG_REPEAT),
isAllowed = KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
isAllowed = if (triggerKeys != null && triggerMode != null) {
KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
} else {
false
}
)

val showVolumeUi = BehaviorOption(
Expand All @@ -241,8 +249,12 @@ class ActionBehavior(action: Action, @Trigger.Mode triggerMode: Int, triggerKeys
val holdDown = BehaviorOption(
id = ID_HOLD_DOWN,
value = action.flags.hasFlag(Action.ACTION_FLAG_HOLD_DOWN),
isAllowed = action.type == ActionType.KEY_EVENT
&& KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
isAllowed = if (triggerKeys != null && triggerMode != null) {
action.type == ActionType.KEY_EVENT
&& KeymapDetectionDelegate.performActionOnDown(triggerKeys, triggerMode)
} else {
false
}
)

val stopRepeatingWhenTriggerReleased: BehaviorOption<Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package io.github.sds100.keymapper.data.model

import androidx.annotation.IntDef
import androidx.annotation.StringDef
import com.github.salomonbrys.kotson.byArray
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.R
import io.github.sds100.keymapper.util.result.ExtraNotFound
Expand Down Expand Up @@ -90,6 +93,15 @@ data class Constraint(@ConstraintType
fun screenOffConstraint(): Constraint {
return Constraint(SCREEN_OFF)
}

val DESERIALIZER = jsonDeserializer {
val type by it.json.byString(NAME_TYPE)

val extrasJsonArray by it.json.byArray(NAME_EXTRAS)
val extraList = it.context.deserialize<List<Extra>>(extrasJsonArray) ?: listOf()

Constraint(type, extraList)
}
}

fun getExtraData(extraId: String): Result<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.github.sds100.keymapper.data.model
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.data.db.dao.DeviceInfoDao
import io.github.sds100.keymapper.data.db.dao.DeviceInfoDao.Companion.KEY_DESCRIPTOR
Expand All @@ -26,5 +28,12 @@ data class DeviceInfo(
//DON'T CHANGE THESE. Used for JSON serialization and parsing.
const val NAME_DESCRIPTOR = "descriptor"
const val NAME_NAME = "name"

val DESERIALIZER = jsonDeserializer {
val descriptor by it.json.byString(NAME_DESCRIPTOR)
val name by it.json.byString(NAME_NAME)

DeviceInfo(descriptor, name)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.sds100.keymapper.data.model

import androidx.annotation.StringDef
import com.github.salomonbrys.kotson.byString
import com.github.salomonbrys.kotson.jsonDeserializer
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.data.model.Action.Companion.EXTRA_LENS
import io.github.sds100.keymapper.data.model.Action.Companion.EXTRA_PACKAGE_NAME
Expand Down Expand Up @@ -52,6 +54,13 @@ data class Extra(@ExtraId
//DON'T CHANGE THESE. Used for JSON serialization and parsing.
const val NAME_ID = "id"
const val NAME_DATA = "data"

val DESERIALIZER = jsonDeserializer {
val id by it.json.byString(NAME_ID)
val data by it.json.byString(NAME_DATA)

Extra(id, data)
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/data/model/KeyMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.sds100.keymapper.data.model
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.github.salomonbrys.kotson.*
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.data.db.dao.KeyMapDao

Expand Down Expand Up @@ -59,6 +60,33 @@ class KeyMap(
const val NAME_FLAGS = "flags"
const val NAME_FOLDER_NAME = "folderName"
const val NAME_IS_ENABLED = "isEnabled"

val DESERIALIZER = jsonDeserializer {
val actionListJsonArray by it.json.byArray(NAME_ACTION_LIST)
val actionList = it.context.deserialize<List<Action>>(actionListJsonArray)

val triggerJsonObject by it.json.byObject(NAME_TRIGGER)
val trigger = it.context.deserialize<Trigger>(triggerJsonObject)

val constraintListJsonArray by it.json.byArray(NAME_CONSTRAINT_LIST)
val constraintList = it.context.deserialize<List<Constraint>>(constraintListJsonArray)

val constraintMode by it.json.byInt(NAME_CONSTRAINT_MODE)
val flags by it.json.byInt(NAME_FLAGS)
val folderName by it.json.byNullableString(NAME_FOLDER_NAME)
val isEnabled by it.json.byBool(NAME_IS_ENABLED)

KeyMap(
0,
trigger,
actionList,
constraintList,
constraintMode,
flags,
folderName,
isEnabled
)
}
}

override fun hashCode() = id.toInt()
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/io/github/sds100/keymapper/data/model/Trigger.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sds100.keymapper.data.model

import androidx.annotation.IntDef
import com.github.salomonbrys.kotson.*
import com.google.gson.annotations.SerializedName
import io.github.sds100.keymapper.R

Expand Down Expand Up @@ -69,6 +70,20 @@ class Trigger(
EXTRA_DOUBLE_PRESS_DELAY,
EXTRA_VIBRATION_DURATION
)

val DESERIALIZER = jsonDeserializer {
val triggerKeysJsonArray by it.json.byArray(NAME_KEYS)
val keys = it.context.deserialize<List<Key>>(triggerKeysJsonArray)

val extrasJsonArray by it.json.byArray(NAME_EXTRAS)
val extraList = it.context.deserialize<List<Extra>>(extrasJsonArray) ?: listOf()

val mode by it.json.byInt(NAME_MODE)

val flags by it.json.byNullableInt(NAME_FLAGS)

Trigger(keys, extraList, mode, flags ?: 0)
}
}

class Key(
Expand All @@ -91,6 +106,14 @@ class Trigger(
//IDS! DON'T CHANGE
const val DEVICE_ID_THIS_DEVICE = "io.github.sds100.keymapper.THIS_DEVICE"
const val DEVICE_ID_ANY_DEVICE = "io.github.sds100.keymapper.ANY_DEVICE"

val DESERIALIZER = jsonDeserializer {
val keycode by it.json.byInt(NAME_KEYCODE)
val deviceId by it.json.byString(NAME_DEVICE_ID)
val clickType by it.json.byInt(NAME_CLICK_TYPE)

Key(keycode, deviceId, clickType)
}
}

val uniqueId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.github.sds100.keymapper.data.IOnboardingState
import io.github.sds100.keymapper.data.KeymapRepository
import io.github.sds100.keymapper.data.model.*
import io.github.sds100.keymapper.data.model.BehaviorOption.Companion.nullIfDefault
import io.github.sds100.keymapper.util.ActionType
import io.github.sds100.keymapper.util.Event
import io.github.sds100.keymapper.util.dataExtraString
import io.github.sds100.keymapper.util.result.Failure
Expand Down Expand Up @@ -569,6 +570,14 @@ class ConfigKeymapViewModel internal constructor(
add(action)
}

if (action.type == ActionType.KEY_EVENT) {
ActionBehavior(action, triggerMode.value!!, triggerKeys.value!!).apply {
setValue(ActionBehavior.ID_REPEAT, true)

setActionBehavior(this)
}
}

invalidateOptions()
}

Expand Down
Loading