68 lines
2.9 KiB
Kotlin
68 lines
2.9 KiB
Kotlin
package com.jttools.datacontrol.guard
|
|
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
|
|
/** Plage pendant laquelle l'appareil est utilisable. Minutes depuis minuit ; jours ISO (1 = lundi … 7 = dimanche). */
|
|
data class TimeWindow(val days: Set<Int>, val startMin: Int, val endMin: Int)
|
|
|
|
data class AppLimit(val pkg: String, val minutes: Int)
|
|
|
|
/**
|
|
* Règles d'usage envoyées par FamilyControl (champ `rules` de la config distante).
|
|
* Les temps s'entendent par jour ; le bonus n'est valable que le jour [bonusDate].
|
|
*/
|
|
data class Rules(
|
|
val scheduleEnabled: Boolean = false,
|
|
val scheduleCutsMobile: Boolean = true,
|
|
val windows: List<TimeWindow> = emptyList(),
|
|
val dailyLimitMin: Int = 0,
|
|
val appLimits: List<AppLimit> = emptyList(),
|
|
val blocked: Set<String> = emptySet(),
|
|
val bonusDate: String? = null,
|
|
val bonusGlobalMin: Int = 0,
|
|
val bonusApps: List<AppLimit> = emptyList()
|
|
) {
|
|
val isEmpty: Boolean
|
|
get() = !scheduleEnabled && dailyLimitMin <= 0 && appLimits.isEmpty() && blocked.isEmpty()
|
|
|
|
companion object {
|
|
val NONE = Rules()
|
|
|
|
fun parse(json: String?): Rules {
|
|
if (json.isNullOrBlank()) return NONE
|
|
return runCatching {
|
|
val o = JSONObject(json)
|
|
val sch = o.optJSONObject("schedule")
|
|
val bonus = o.optJSONObject("bonus")
|
|
Rules(
|
|
scheduleEnabled = sch?.optBoolean("enabled", false) ?: false,
|
|
scheduleCutsMobile = sch?.optBoolean("cutsMobile", true) ?: true,
|
|
windows = sch?.optJSONArray("windows").objects().map { w ->
|
|
TimeWindow(
|
|
days = w.optJSONArray("days").ints().toSet(),
|
|
startMin = w.optInt("start", 0),
|
|
endMin = w.optInt("end", 0)
|
|
)
|
|
},
|
|
dailyLimitMin = o.optInt("dailyLimitMin", 0),
|
|
appLimits = o.optJSONArray("appLimits").objects().map { AppLimit(it.optString("pkg"), it.optInt("min")) },
|
|
blocked = o.optJSONArray("blocked").strings().toSet(),
|
|
bonusDate = bonus?.optString("date")?.takeIf { it.isNotEmpty() },
|
|
bonusGlobalMin = bonus?.optInt("globalMin", 0) ?: 0,
|
|
bonusApps = bonus?.optJSONArray("apps").objects().map { AppLimit(it.optString("pkg"), it.optInt("min")) }
|
|
)
|
|
}.getOrDefault(NONE)
|
|
}
|
|
|
|
private fun JSONArray?.objects(): List<JSONObject> =
|
|
if (this == null) emptyList() else (0 until length()).mapNotNull { optJSONObject(it) }
|
|
|
|
private fun JSONArray?.ints(): List<Int> =
|
|
if (this == null) emptyList() else (0 until length()).map { optInt(it) }
|
|
|
|
private fun JSONArray?.strings(): List<String> =
|
|
if (this == null) emptyList() else (0 until length()).map { optString(it) }
|
|
}
|
|
}
|