forked from sameerasw/airsync-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonUtil.kt
More file actions
255 lines (237 loc) · 8.51 KB
/
JsonUtil.kt
File metadata and controls
255 lines (237 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.sameerasw.airsync.utils
object JsonUtil {
/**
* Ensures JSON string is a single line by removing all newlines and extra whitespace
*/
fun toSingleLine(json: String): String {
return json.replace(Regex("\\s*\\n\\s*"), " ")
.replace(Regex("\\s+"), " ")
.trim()
}
/**
* Creates a single-line JSON string for device info
*/
fun createDeviceInfoJson(
name: String,
ipAddress: String,
port: Int,
version: String,
targetIpAddress: String? = null
): String {
val targetIpJson =
if (targetIpAddress != null) """, "targetIpAddress": "$targetIpAddress" """ else ""
return """{"type":"device","data":{"name":"$name","ipAddress":"$ipAddress","port":$port,"version":"$version","adbPorts":[]$targetIpJson}}"""
}
/**
* Creates a single-line JSON string for device info with ADB ports
*/
fun createDeviceInfoJson(
id: String,
name: String,
ipAddress: String,
port: Int,
version: String,
adbPorts: List<String>,
targetIpAddress: String? = null
): String {
val portsJson = adbPorts.joinToString(",") { "\"$it\"" }
val targetIpJson =
if (targetIpAddress != null) """, "targetIpAddress": "$targetIpAddress" """ else ""
return """{"type":"device","data":{"id":"$id","name":"$name","ipAddress":"$ipAddress","port":$port,"version":"$version","adbPorts":[$portsJson]$targetIpJson}}"""
}
/**
* Creates a single-line JSON string for device info with wallpaper
*/
fun createDeviceInfoJson(
id: String,
name: String,
ipAddress: String,
port: Int,
version: String,
wallpaperBase64: String?,
adbPorts: List<String>,
targetIpAddress: String? = null
): String {
val wallpaperJson = if (wallpaperBase64 != null) {
""","wallpaper":"$wallpaperBase64""""
} else {
""
}
val portsJson = adbPorts.joinToString(",") { "\"$it\"" }
val targetIpJson =
if (targetIpAddress != null) """, "targetIpAddress": "$targetIpAddress" """ else ""
return """{"type":"device","data":{"id":"$id","name":"$name","ipAddress":"$ipAddress","port":$port,"version":"$version","adbPorts":[$portsJson]$wallpaperJson$targetIpJson}}"""
}
/**
* Creates a single-line JSON string for notifications with unique ID
*/
fun createNotificationJson(
id: String,
title: String,
body: String,
app: String,
packageName: String
): String {
return """{"type":"notification","data":{"id":"$id","title":"$title","body":"$body","app":"$app","package":"$packageName"}}"""
}
/**
* Creates a single-line JSON string for notifications with actions included.
* actions: list of objects { name: String, type: "button" | "reply" }
*/
fun createNotificationJson(
id: String,
title: String,
body: String,
app: String,
packageName: String,
priority: String = "alerting",
actions: List<Pair<String, String>>
): String {
val actionsJson = if (actions.isNotEmpty()) {
val items = actions.joinToString(",") { (name, type) ->
"""{"name":"${escape(name)}","type":"${escape(type)}"}"""
}
",\"actions\":[${items}]"
} else {
""
}
return """{"type":"notification","data":{"id":"$id","title":"${escape(title)}","body":"${
escape(
body
)
}","app":"${escape(app)}","package":"${escape(packageName)}","priority":"$priority"$actionsJson}}"""
}
/**
* Creates a one-line JSON for Android->Mac notification state updates (e.g., dismissals).
* If action is not provided, defaults to "dismiss" when dismissed=true.
*/
fun createNotificationUpdateJson(
id: String,
dismissed: Boolean = true,
action: String? = null
): String {
val safeAction = action ?: if (dismissed) "dismiss" else null
val actionPart = safeAction?.let { ",\"action\":\"${escape(it)}\"" } ?: ""
return """{"type":"notificationUpdate","data":{"id":"$id","dismissed":$dismissed$actionPart}}"""
}
private fun escape(value: String): String {
return value
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r")
}
/**
* Creates a single-line JSON string for device status with media playing state
*/
fun createDeviceStatusJson(
batteryLevel: Int,
isCharging: Boolean,
isPaired: Boolean,
isPlaying: Boolean,
title: String,
artist: String,
volume: Int,
isMuted: Boolean,
albumArt: String?,
likeStatus: String
): String {
val albumArtJson = if (albumArt != null) ",\"albumArt\":\"$albumArt\"" else ""
return """{"type":"status","data":{"battery":{"level":$batteryLevel,"isCharging":$isCharging},"isPaired":$isPaired,"music":{"isPlaying":$isPlaying,"title":"$title","artist":"$artist","volume":$volume,"isMuted":$isMuted$albumArtJson,"likeStatus":"$likeStatus"}}}"""
}
/**
* Creates a response JSON for notification dismissal result
*/
fun createNotificationDismissalResponse(
id: String,
success: Boolean,
message: String = ""
): String {
return """{"type":"dismissalResponse","data":{"id":"$id","success":$success,"message":"$message"}}"""
}
/**
* Creates a response JSON for notification action result
*/
fun createNotificationActionResponse(
id: String,
actionName: String,
success: Boolean,
message: String = ""
): String {
return """{"type":"notificationActionResponse","data":{"id":"$id","action":"${
escape(
actionName
)
}","success":$success,"message":"${escape(message)}"}}"""
}
/**
* Creates a response JSON for media control result
*/
fun createMediaControlResponse(action: String, success: Boolean, message: String = ""): String {
return """{"type":"mediaControlResponse","data":{"action":"$action","success":$success,"message":"$message"}}"""
}
/**
* Creates a response JSON for volume control result
*/
fun createVolumeControlResponse(
action: String,
success: Boolean,
message: String = ""
): String {
return """{"type":"volumeControlResponse","data":{"action":"$action","success":$success,"message":"$message"}}"""
}
/**
* Creates a JSON string for app icons
*/
fun createAppIconsJson(
apps: List<com.sameerasw.airsync.domain.model.NotificationApp>,
iconMap: Map<String, String>
): String {
val appEntries = apps.joinToString(",") { app ->
val iconData = iconMap[app.packageName] ?: ""
"""
"${app.packageName}": {
"name": "${app.appName}",
"icon": "$iconData",
"listening": ${app.isEnabled},
"systemApp": ${app.isSystemApp}
}
""".trimIndent()
}
return """{"type":"appIcons","data":{ $appEntries }}"""
}
/**
* Creates a JSON string for clipboard updates
*/
fun createClipboardUpdateJson(text: String): String {
// Escape quotes and newlines in the text
val escapedText = text.replace("\"", "\\\"").replace("\n", "\\n").replace("\r", "\\r")
return """{"type":"clipboardUpdate","data":{"text":"$escapedText"}}"""
}
/**
* Creates a response JSON for app notification toggle result
*/
fun createToggleAppNotificationResponse(
packageName: String,
success: Boolean,
newState: Boolean,
message: String = ""
): String {
return """{"type":"toggleAppNotifResponse","data":{"package":"$packageName","success":$success,"newState":$newState,"message":"$message"}}"""
}
/**
* Creates a response JSON for toggleNowPlaying command
*/
fun createToggleNowPlayingResponse(
success: Boolean,
newState: Boolean?,
message: String = ""
): String {
val statePart = newState?.let { ",\"state\":$it" } ?: ""
return """{"type":"toggleNowPlayingResponse","data":{"success":$success$statePart,"message":"${
escape(
message
)
}"}}"""
}
}