mirror of
https://github.com/fankes/MIUINativeNotifyIcon.git
synced 2025-09-05 10:15:31 +08:00
支持导入数组和非数组格式的 JSON 自定义规则,并加入支持合并非全部覆盖的功能
This commit is contained in:
@@ -20,15 +20,15 @@
|
||||
*
|
||||
* This file is Created by fankes on 2022/1/24.
|
||||
*/
|
||||
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||
|
||||
package com.fankes.miui.notify.params
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import com.fankes.miui.notify.bean.IconDataBean
|
||||
import com.fankes.miui.notify.hook.HookConst.NOTIFY_ICON_DATAS
|
||||
import com.fankes.miui.notify.utils.bitmap
|
||||
import com.fankes.miui.notify.utils.safeOf
|
||||
import com.fankes.miui.notify.utils.safeOfNan
|
||||
import com.fankes.miui.notify.utils.*
|
||||
import com.highcapable.yukihookapi.hook.factory.modulePrefs
|
||||
import com.highcapable.yukihookapi.hook.param.PackageParam
|
||||
import org.json.JSONArray
|
||||
@@ -47,7 +47,7 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
|
||||
* 已存储的 JSON 数据
|
||||
* @return [String]
|
||||
*/
|
||||
private val storageDataJson get() = (context?.modulePrefs ?: param?.prefs)?.getString(NOTIFY_ICON_DATAS)
|
||||
internal val storageDataJson get() = (context?.modulePrefs ?: param?.prefs)?.getString(NOTIFY_ICON_DATAS)
|
||||
|
||||
/**
|
||||
* 获取图标数据
|
||||
@@ -59,25 +59,32 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
|
||||
if (it.isNotBlank()) runCatching {
|
||||
JSONArray(it).also { array ->
|
||||
for (i in 0 until array.length()) runCatching {
|
||||
(array.get(i) as JSONObject).apply {
|
||||
add(
|
||||
IconDataBean(
|
||||
appName = getString("appName"),
|
||||
packageName = getString("packageName"),
|
||||
isEnabled = getBoolean("isEnabled"),
|
||||
isEnabledAll = getBoolean("isEnabledAll"),
|
||||
iconBitmap = getString("iconBitmap").bitmap,
|
||||
iconColor = safeOfNan { Color.parseColor(getString("iconColor")) },
|
||||
contributorName = getString("contributorName")
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
add(convertToBean(array.get(i) as JSONObject)!!)
|
||||
}.onFailure { context?.snake(msg = "部分规则加载失败") }
|
||||
}
|
||||
}
|
||||
}.onFailure { context?.snake(msg = "规则加载发生错误") }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为 [IconDataBean]
|
||||
* @param jsonObject Json 实例
|
||||
* @return [IconDataBean] or null
|
||||
*/
|
||||
private fun convertToBean(jsonObject: JSONObject) = safeOfNull {
|
||||
jsonObject.let {
|
||||
IconDataBean(
|
||||
appName = it.getString("appName"),
|
||||
packageName = it.getString("packageName"),
|
||||
isEnabled = it.getBoolean("isEnabled"),
|
||||
isEnabledAll = it.getBoolean("isEnabledAll"),
|
||||
iconBitmap = it.getString("iconBitmap").bitmap,
|
||||
iconColor = safeOfNan { Color.parseColor(it.getString("iconColor")) },
|
||||
contributorName = it.getString("contributorName")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接图标数组数据
|
||||
* @param dataJson1 图标数据 JSON
|
||||
@@ -93,7 +100,21 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
|
||||
* @param json 数据
|
||||
* @return [Boolean]
|
||||
*/
|
||||
fun isNotVaildJson(json: String) = json.trim().let { !it.startsWith("[") || !it.endsWith("]") }
|
||||
fun isNotVaildJson(json: String) = !isJsonArray(json) && !isJsonObject(json)
|
||||
|
||||
/**
|
||||
* 是否为 JSON 数组
|
||||
* @param json 数据
|
||||
* @return [Boolean]
|
||||
*/
|
||||
fun isJsonArray(json: String) = json.trim().let { it.startsWith("[") && it.endsWith("]") }
|
||||
|
||||
/**
|
||||
* 是否为 JSON 实例
|
||||
* @param json 数据
|
||||
* @return [Boolean]
|
||||
*/
|
||||
fun isJsonObject(json: String) = json.trim().let { it.startsWith("{") && it.endsWith("}") }
|
||||
|
||||
/**
|
||||
* 是否为异常地址
|
||||
|
@@ -294,22 +294,42 @@ class ConfigureActivity : BaseActivity() {
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
confirmButton {
|
||||
IconPackParams(context = this@ConfigureActivity).also { params ->
|
||||
when {
|
||||
editText.text.toString().isNotBlank() && params.isNotVaildJson(editText.text.toString()) ->
|
||||
snake(msg = "不是有效的 JSON 数据")
|
||||
editText.text.toString().isNotBlank() -> {
|
||||
params.save(editText.text.toString())
|
||||
filterText = ""
|
||||
mockLocalData()
|
||||
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
|
||||
IconPackParams(context = this@ConfigureActivity).also { params ->
|
||||
confirmButton(text = "合并") {
|
||||
editText.text.toString().also { jsonString ->
|
||||
when {
|
||||
jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
|
||||
jsonString.isNotBlank() -> {
|
||||
params.save(
|
||||
params.splicingJsonArray(
|
||||
dataJson1 = params.storageDataJson ?: "[]",
|
||||
dataJson2 = jsonString.takeIf { params.isJsonArray(it) } ?: "[$jsonString]"
|
||||
)
|
||||
)
|
||||
filterText = ""
|
||||
mockLocalData()
|
||||
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
|
||||
}
|
||||
else -> snake(msg = "请输入有效内容")
|
||||
}
|
||||
}
|
||||
}
|
||||
cancelButton(text = "覆盖") {
|
||||
editText.text.toString().also { jsonString ->
|
||||
when {
|
||||
jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
|
||||
jsonString.isNotBlank() -> {
|
||||
params.save(dataJson = jsonString.takeIf { params.isJsonArray(it) } ?: "[$jsonString]")
|
||||
filterText = ""
|
||||
mockLocalData()
|
||||
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
|
||||
}
|
||||
else -> snake(msg = "请输入有效内容")
|
||||
}
|
||||
else -> snake(msg = "规则数组内容为空")
|
||||
}
|
||||
}
|
||||
}
|
||||
cancelButton()
|
||||
neutralButton(text = "取消")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,7 @@
|
||||
android:layout_height="150dp"
|
||||
android:ellipsize="end"
|
||||
android:gravity="center|start|top"
|
||||
android:hint="请粘贴 JSON 规则数组到此处"
|
||||
android:hint="请粘贴 JSON 规则到此处"
|
||||
android:textSize="14sp" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
</LinearLayout>
|
Reference in New Issue
Block a user