1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.systemui.plugins.clocks 15 16 import com.android.internal.annotations.Keep 17 import org.json.JSONArray 18 import org.json.JSONObject 19 20 @Keep 21 /** Structure for keeping clock-specific settings */ 22 data class ClockSettings( 23 val clockId: ClockId? = null, 24 val seedColor: Int? = null, 25 val axes: List<ClockFontAxisSetting> = listOf(), 26 ) { 27 // Exclude metadata from equality checks 28 var metadata: JSONObject = JSONObject() 29 30 companion object { 31 private val KEY_CLOCK_ID = "clockId" 32 private val KEY_SEED_COLOR = "seedColor" 33 private val KEY_METADATA = "metadata" 34 private val KEY_AXIS_LIST = "axes" 35 toJsonnull36 fun toJson(setting: ClockSettings): JSONObject { 37 return JSONObject().apply { 38 put(KEY_CLOCK_ID, setting.clockId) 39 put(KEY_SEED_COLOR, setting.seedColor) 40 put(KEY_METADATA, setting.metadata) 41 put(KEY_AXIS_LIST, ClockFontAxisSetting.toJson(setting.axes)) 42 } 43 } 44 fromJsonnull45 fun fromJson(json: JSONObject): ClockSettings { 46 val clockId = if (!json.isNull(KEY_CLOCK_ID)) json.getString(KEY_CLOCK_ID) else null 47 val seedColor = if (!json.isNull(KEY_SEED_COLOR)) json.getInt(KEY_SEED_COLOR) else null 48 val axisList = json.optJSONArray(KEY_AXIS_LIST)?.let(ClockFontAxisSetting::fromJson) 49 return ClockSettings(clockId, seedColor, axisList ?: listOf()).apply { 50 metadata = json.optJSONObject(KEY_METADATA) ?: JSONObject() 51 } 52 } 53 } 54 } 55 56 @Keep 57 /** Axis setting value for a clock */ 58 data class ClockFontAxisSetting( 59 /** Axis key; matches ClockFontAxis.key */ 60 val key: String, 61 62 /** Value to set this axis to */ 63 val value: Float, 64 ) { 65 companion object { 66 private val KEY_AXIS_KEY = "key" 67 private val KEY_AXIS_VALUE = "value" 68 toJsonnull69 fun toJson(setting: ClockFontAxisSetting): JSONObject { 70 return JSONObject().apply { 71 put(KEY_AXIS_KEY, setting.key) 72 put(KEY_AXIS_VALUE, setting.value) 73 } 74 } 75 toJsonnull76 fun toJson(settings: List<ClockFontAxisSetting>): JSONArray { 77 return JSONArray().apply { 78 for (axis in settings) { 79 put(toJson(axis)) 80 } 81 } 82 } 83 fromJsonnull84 fun fromJson(jsonObj: JSONObject): ClockFontAxisSetting { 85 return ClockFontAxisSetting( 86 key = jsonObj.getString(KEY_AXIS_KEY), 87 value = jsonObj.getDouble(KEY_AXIS_VALUE).toFloat(), 88 ) 89 } 90 fromJsonnull91 fun fromJson(jsonArray: JSONArray): List<ClockFontAxisSetting> { 92 val result = mutableListOf<ClockFontAxisSetting>() 93 for (i in 0..jsonArray.length() - 1) { 94 val obj = jsonArray.getJSONObject(i) 95 if (obj == null) continue 96 result.add(fromJson(obj)) 97 } 98 return result 99 } 100 toFVarnull101 fun toFVar(settings: List<ClockFontAxisSetting>): String { 102 val sb = StringBuilder() 103 for (axis in settings) { 104 if (sb.length > 0) sb.append(", ") 105 sb.append("'${axis.key}' ${axis.value.toInt()}") 106 } 107 return sb.toString() 108 } 109 } 110 } 111