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/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

19 changes: 19 additions & 0 deletions app/src/main/java/com/endroid/latch/data/RuleJson.kt
Original file line number Diff line number Diff line change
@@ -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<Rule> {
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)))
}
}
}
5 changes: 1 addition & 4 deletions app/src/main/java/com/endroid/latch/data/RuleStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/endroid/latch/ui/LatchApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
76 changes: 76 additions & 0 deletions app/src/test/java/com/endroid/latch/RuleJsonTest.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 2 additions & 0 deletions docs/RECIPES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand All @@ -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 |
4 changes: 3 additions & 1 deletion recipes/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# 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 |
|------|------|
| `battery_saver.json` | Dim brightness when battery is low |
| `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) |

Expand Down
23 changes: 23 additions & 0 deletions recipes/bedtime_winddown.json
Original file line number Diff line number Diff line change
@@ -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": ""}
]
}
]
}
23 changes: 23 additions & 0 deletions recipes/commute_bluetooth_music.json
Original file line number Diff line number Diff line change
@@ -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"}
]
}
]
}