From 4baab36e71836d5cc016d0b2e06716487334407d Mon Sep 17 00:00:00 2001 From: anubhavaanand Date: Wed, 11 Nov 2026 22:57:39 +0530 Subject: [PATCH] feat: add commute and bedtime recipes --- app/build.gradle.kts | 1 + .../java/com/endroid/latch/data/RuleJson.kt | 19 +++++ .../java/com/endroid/latch/data/RuleStore.kt | 5 +- .../java/com/endroid/latch/ui/LatchApp.kt | 2 +- .../java/com/endroid/latch/RuleJsonTest.kt | 76 +++++++++++++++++++ docs/RECIPES.md | 2 + recipes/README.md | 4 +- recipes/bedtime_winddown.json | 23 ++++++ recipes/commute_bluetooth_music.json | 23 ++++++ 9 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/endroid/latch/data/RuleJson.kt create mode 100644 app/src/test/java/com/endroid/latch/RuleJsonTest.kt create mode 100644 recipes/bedtime_winddown.json create mode 100644 recipes/commute_bluetooth_music.json diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 30b6915..4fcc780 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -111,5 +111,6 @@ dependencies { implementation("dev.rikka.shizuku:provider:13.1.5") debugImplementation(libs.androidx.ui.tooling) testImplementation("junit:junit:4.13.2") + testImplementation("org.json:json:20240303") } diff --git a/app/src/main/java/com/endroid/latch/data/RuleJson.kt b/app/src/main/java/com/endroid/latch/data/RuleJson.kt new file mode 100644 index 0000000..03316bc --- /dev/null +++ b/app/src/main/java/com/endroid/latch/data/RuleJson.kt @@ -0,0 +1,19 @@ +package com.endroid.latch.data + +import org.json.JSONArray +import org.json.JSONObject + +/** Parses either a bare rules array (export format) or a recipe wrapper ({"rules":[...]}). */ +object RuleJson { + fun parseList(text: String): List { + val trimmed = text.trim() + val arr = if (trimmed.startsWith("[")) { + JSONArray(trimmed) + } else { + JSONObject(trimmed).getJSONArray("rules") + } + return buildList { + for (i in 0 until arr.length()) add(Rule.fromJson(arr.getJSONObject(i))) + } + } +} diff --git a/app/src/main/java/com/endroid/latch/data/RuleStore.kt b/app/src/main/java/com/endroid/latch/data/RuleStore.kt index 37d87ec..9253255 100644 --- a/app/src/main/java/com/endroid/latch/data/RuleStore.kt +++ b/app/src/main/java/com/endroid/latch/data/RuleStore.kt @@ -63,10 +63,7 @@ class RuleStore(private val context: Context) { } fun importJson(text: String, replace: Boolean = false): Int { - val arr = JSONArray(text) - val incoming = buildList { - for (i in 0 until arr.length()) add(Rule.fromJson(arr.getJSONObject(i))) - } + val incoming = RuleJson.parseList(text) if (replace) { save(incoming) return incoming.size diff --git a/app/src/main/java/com/endroid/latch/ui/LatchApp.kt b/app/src/main/java/com/endroid/latch/ui/LatchApp.kt index 04ef6ec..3f768ae 100644 --- a/app/src/main/java/com/endroid/latch/ui/LatchApp.kt +++ b/app/src/main/java/com/endroid/latch/ui/LatchApp.kt @@ -929,7 +929,7 @@ private fun SettingsScreen( value = importText, onValueChange = { importText = it }, modifier = Modifier.fillMaxWidth().height(200.dp), - placeholder = { Text("Paste rules array…") }, + placeholder = { Text("Paste rules array or recipe JSON…") }, ) }, confirmButton = { diff --git a/app/src/test/java/com/endroid/latch/RuleJsonTest.kt b/app/src/test/java/com/endroid/latch/RuleJsonTest.kt new file mode 100644 index 0000000..3987391 --- /dev/null +++ b/app/src/test/java/com/endroid/latch/RuleJsonTest.kt @@ -0,0 +1,76 @@ +package com.endroid.latch + +import com.endroid.latch.data.ActionType +import com.endroid.latch.data.RingerModeKind +import com.endroid.latch.data.RuleJson +import com.endroid.latch.data.TriggerType +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.File + +class RuleJsonTest { + private val rule = """ + { + "id": "rule-1", + "name": "Test rule", + "trigger": "BATTERY_BELOW", + "actions": [{"type": "TOAST", "text": "hello"}] + } + """.trimIndent() + + @Test fun parsesBareArray() { + val rules = RuleJson.parseList("[$rule]") + + assertEquals(1, rules.size) + assertEquals("Test rule", rules[0].name) + assertEquals(TriggerType.BATTERY_BELOW, rules[0].trigger) + assertEquals(ActionType.TOAST, rules[0].actions.single().type) + } + + @Test fun parsesRecipeWrapper() { + val rules = RuleJson.parseList("""{"rules": [$rule]}""") + + assertEquals(1, rules.size) + assertEquals("rule-1", rules[0].id) + } + + @Test fun parsesCommuteRecipeFile() { + val file = recipeFile("commute_bluetooth_music.json") + assertTrue("missing ${file.path}", file.exists()) + + val rule = RuleJson.parseList(file.readText()).single() + + assertTrue(rule.enabled) + assertEquals("default", rule.profileId) + assertEquals(TriggerType.BLUETOOTH_CONNECTED, rule.trigger) + assertEquals(listOf(ActionType.MEDIA_VOLUME, ActionType.OPEN_APP), rule.actions.map { it.type }) + assertEquals("80", rule.actions[0].param) + assertEquals("com.google.android.apps.youtube.music", rule.actions[1].param) + } + + @Test fun parsesBedtimeRecipeFile() { + val file = recipeFile("bedtime_winddown.json") + assertTrue("missing ${file.path}", file.exists()) + + val rule = RuleJson.parseList(file.readText()).single() + + assertTrue(rule.enabled) + assertEquals("default", rule.profileId) + assertEquals(TriggerType.TIME_DAILY, rule.trigger) + assertEquals(22, rule.triggerValue) + assertEquals(30, rule.triggerValue2) + assertEquals(listOf(ActionType.SET_BRIGHTNESS, ActionType.RINGER_MODE), rule.actions.map { it.type }) + assertEquals("25", rule.actions[0].param) + assertEquals(RingerModeKind.VIBRATE, rule.actions[1].ringerMode) + } + + private fun recipeFile(name: String): File { + var dir = File(System.getProperty("user.dir") ?: ".") + while (true) { + val file = File(dir, "recipes/$name") + if (file.exists()) return file + dir = dir.parentFile ?: return file + } + } +} diff --git a/docs/RECIPES.md b/docs/RECIPES.md index cae8a3f..10a8204 100644 --- a/docs/RECIPES.md +++ b/docs/RECIPES.md @@ -22,6 +22,7 @@ Import JSON from the `recipes/` folder in **Settings → Import**, or copy a fil | --- | --- | --- | | `night_dnd.json` | Time window | Ringer silent | | `sleep_focus.json` | Time window | Vibrate + DND | +| `bedtime_winddown.json` | Daily at 22:30 | Brightness 25% + ringer vibrate | ## Tips - Triggers and actions must match names in `TriggerType` / `ActionType` (`Models.kt`). @@ -32,3 +33,4 @@ Import JSON from the `recipes/` folder in **Settings → Import**, or copy a fil | Recipe | Trigger | Actions | | --- | --- | --- | | `bluetooth_connected_toast.json` | `BLUETOOTH_CONNECTED` | Toast | +| `commute_bluetooth_music.json` | `BLUETOOTH_CONNECTED` | Media volume 80% + open music app | diff --git a/recipes/README.md b/recipes/README.md index 10aad01..3602e66 100644 --- a/recipes/README.md +++ b/recipes/README.md @@ -1,6 +1,6 @@ # Latch recipe packs -Import any JSON file from the app (**Import rules**) or merge the `rules` array into an export. +Import any JSON file from the app (**Import rules**). Both the recipe wrapper and a bare rules array are accepted. | File | Idea | |------|------| @@ -8,8 +8,10 @@ Import any JSON file from the app (**Import rules**) or merge the `rules` array | `sleep_focus.json` | Night / focus style rule | | `work_wifi_silent.json` | Silent ringer on a work SSID | | `headphones_music.json` | Open music app on headphone connect | +| `commute_bluetooth_music.json` | Raise media volume and open music on Bluetooth connect | | `charging_notify.json` | Notify when charging starts | | `night_dnd.json` | Silent between 22:00 and 07:00 | +| `bedtime_winddown.json` | Dim screen and switch ringer to vibrate at bedtime | | `boot_welcome.json` | Notify after boot | | `car_bluetooth.json` | Vibrate ringer when a Bluetooth device connects (fires for any device: refine before use) | diff --git a/recipes/bedtime_winddown.json b/recipes/bedtime_winddown.json new file mode 100644 index 0000000..3d0da65 --- /dev/null +++ b/recipes/bedtime_winddown.json @@ -0,0 +1,23 @@ +{ + "rules": [ + { + "id": "recipe-bedtime-winddown", + "name": "Bedtime wind-down", + "enabled": true, + "trigger": "TIME_DAILY", + "triggerValue": 22, + "triggerValue2": 30, + "triggerParam": "", + "condition": "NONE", + "conditionValue": 0, + "cooldownSec": 3600, + "notes": "Recipe: dim the screen and switch the ringer to vibrate at 22:30. Edit the time as needed. Brightness needs Modify system settings.", + "tag": "recipe", + "profileId": "default", + "actions": [ + {"type": "SET_BRIGHTNESS", "text": "", "title": "", "ringerMode": "NORMAL", "param": "25"}, + {"type": "RINGER_MODE", "text": "", "title": "", "ringerMode": "VIBRATE", "param": ""} + ] + } + ] +} diff --git a/recipes/commute_bluetooth_music.json b/recipes/commute_bluetooth_music.json new file mode 100644 index 0000000..768628f --- /dev/null +++ b/recipes/commute_bluetooth_music.json @@ -0,0 +1,23 @@ +{ + "rules": [ + { + "id": "recipe-commute-bluetooth", + "name": "Commute: Bluetooth music", + "enabled": true, + "trigger": "BLUETOOTH_CONNECTED", + "triggerValue": 0, + "triggerValue2": 0, + "triggerParam": "", + "condition": "NONE", + "conditionValue": 0, + "cooldownSec": 30, + "notes": "Recipe: set media volume to 80% and open a music app when Bluetooth connects. Fires for any Bluetooth device (no per-device filter yet); edit the package. Needs the Bluetooth permission.", + "tag": "recipe", + "profileId": "default", + "actions": [ + {"type": "MEDIA_VOLUME", "text": "", "title": "", "ringerMode": "NORMAL", "param": "80"}, + {"type": "OPEN_APP", "text": "", "title": "", "ringerMode": "NORMAL", "param": "com.google.android.apps.youtube.music"} + ] + } + ] +}