Skip to content

Commit 3e2b824

Browse files
committed
支持导入数组和非数组格式的 JSON 自定义规则,并加入支持合并非全部覆盖的功能
1 parent e529e75 commit 3e2b824

3 files changed

Lines changed: 74 additions & 33 deletions

File tree

app/src/main/java/com/fankes/coloros/notify/param/IconPackParams.kt

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
*
2121
* This file is Created by fankes on 2022/1/24.
2222
*/
23+
@file:Suppress("MemberVisibilityCanBePrivate")
24+
2325
package com.fankes.coloros.notify.param
2426

2527
import android.content.Context
2628
import android.graphics.Bitmap
2729
import android.graphics.Color
2830
import com.fankes.coloros.notify.bean.IconDataBean
2931
import com.fankes.coloros.notify.hook.HookConst.NOTIFY_ICON_DATAS
30-
import com.fankes.coloros.notify.utils.bitmap
31-
import com.fankes.coloros.notify.utils.safeOf
32-
import com.fankes.coloros.notify.utils.safeOfNan
32+
import com.fankes.coloros.notify.utils.*
3333
import com.highcapable.yukihookapi.hook.factory.modulePrefs
3434
import com.highcapable.yukihookapi.hook.param.PackageParam
3535
import org.json.JSONArray
@@ -101,7 +101,7 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
101101
* 已存储的 JSON 数据
102102
* @return [String]
103103
*/
104-
private val storageDataJson get() = (context?.modulePrefs ?: param?.prefs)?.getString(NOTIFY_ICON_DATAS)
104+
internal val storageDataJson get() = (context?.modulePrefs ?: param?.prefs)?.getString(NOTIFY_ICON_DATAS)
105105

106106
/**
107107
* 获取图标数据
@@ -113,25 +113,32 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
113113
if (it.isNotBlank()) runCatching {
114114
JSONArray(it).also { array ->
115115
for (i in 0 until array.length()) runCatching {
116-
(array.get(i) as JSONObject).apply {
117-
add(
118-
IconDataBean(
119-
appName = getString("appName"),
120-
packageName = getString("packageName"),
121-
isEnabled = getBoolean("isEnabled"),
122-
isEnabledAll = getBoolean("isEnabledAll"),
123-
iconBitmap = getString("iconBitmap").bitmap,
124-
iconColor = safeOfNan { Color.parseColor(getString("iconColor")) },
125-
contributorName = getString("contributorName")
126-
)
127-
)
128-
}
129-
}
116+
add(convertToBean(array.get(i) as JSONObject)!!)
117+
}.onFailure { context?.snake(msg = "部分规则加载失败") }
130118
}
131-
}
119+
}.onFailure { context?.snake(msg = "规则加载发生错误") }
132120
}
133121
}
134122

123+
/**
124+
* 转换为 [IconDataBean]
125+
* @param jsonObject Json 实例
126+
* @return [IconDataBean] or null
127+
*/
128+
private fun convertToBean(jsonObject: JSONObject) = safeOfNull {
129+
jsonObject.let {
130+
IconDataBean(
131+
appName = it.getString("appName"),
132+
packageName = it.getString("packageName"),
133+
isEnabled = it.getBoolean("isEnabled"),
134+
isEnabledAll = it.getBoolean("isEnabledAll"),
135+
iconBitmap = it.getString("iconBitmap").bitmap,
136+
iconColor = safeOfNan { Color.parseColor(it.getString("iconColor")) },
137+
contributorName = it.getString("contributorName")
138+
)
139+
}
140+
}
141+
135142
/**
136143
* 拼接图标数组数据
137144
* @param dataJson1 图标数据 JSON
@@ -147,7 +154,21 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
147154
* @param json 数据
148155
* @return [Boolean]
149156
*/
150-
fun isNotVaildJson(json: String) = json.trim().let { !it.startsWith("[") || !it.endsWith("]") }
157+
fun isNotVaildJson(json: String) = !isJsonArray(json) && !isJsonObject(json)
158+
159+
/**
160+
* 是否为 JSON 数组
161+
* @param json 数据
162+
* @return [Boolean]
163+
*/
164+
fun isJsonArray(json: String) = json.trim().let { it.startsWith("[") && it.endsWith("]") }
165+
166+
/**
167+
* 是否为 JSON 实例
168+
* @param json 数据
169+
* @return [Boolean]
170+
*/
171+
fun isJsonObject(json: String) = json.trim().let { it.startsWith("{") && it.endsWith("}") }
151172

152173
/**
153174
* 是否为异常地址

app/src/main/java/com/fankes/coloros/notify/ui/ConfigureActivity.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,42 @@ class ConfigureActivity : BaseActivity() {
294294
invalidate()
295295
}
296296
}
297-
confirmButton {
298-
IconPackParams(context = this@ConfigureActivity).also { params ->
299-
when {
300-
editText.text.toString().isNotBlank() && params.isNotVaildJson(editText.text.toString()) ->
301-
snake(msg = "不是有效的 JSON 数据")
302-
editText.text.toString().isNotBlank() -> {
303-
params.save(editText.text.toString())
304-
filterText = ""
305-
mockLocalData()
306-
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
297+
IconPackParams(context = this@ConfigureActivity).also { params ->
298+
confirmButton(text = "合并") {
299+
editText.text.toString().also { jsonString ->
300+
when {
301+
jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
302+
jsonString.isNotBlank() -> {
303+
params.save(
304+
params.splicingJsonArray(
305+
dataJson1 = params.storageDataJson ?: "[]",
306+
dataJson2 = jsonString.takeIf { params.isJsonArray(it) } ?: "[$jsonString]"
307+
)
308+
)
309+
filterText = ""
310+
mockLocalData()
311+
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
312+
}
313+
else -> snake(msg = "请输入有效内容")
314+
}
315+
}
316+
}
317+
cancelButton(text = "覆盖") {
318+
editText.text.toString().also { jsonString ->
319+
when {
320+
jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
321+
jsonString.isNotBlank() -> {
322+
params.save(dataJson = jsonString.takeIf { params.isJsonArray(it) } ?: "[$jsonString]")
323+
filterText = ""
324+
mockLocalData()
325+
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
326+
}
327+
else -> snake(msg = "请输入有效内容")
307328
}
308-
else -> snake(msg = "规则数组内容为空")
309329
}
310330
}
311331
}
312-
cancelButton()
332+
neutralButton(text = "取消")
313333
}
314334
}
315335
}

app/src/main/res/layout/dia_source_from_string.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
android:layout_height="150dp"
2020
android:ellipsize="end"
2121
android:gravity="center|start|top"
22-
android:hint="请粘贴 JSON 规则数组到此处"
22+
android:hint="请粘贴 JSON 规则到此处"
2323
android:textSize="14sp" />
2424
</com.google.android.material.textfield.TextInputLayout>
2525
</LinearLayout>

0 commit comments

Comments
 (0)