支持导入数组和非数组格式的 JSON 自定义规则,并加入支持合并非全部覆盖的功能

This commit is contained in:
2022-03-06 02:59:32 +08:00
parent 4109a25b5d
commit 099886b0b2
3 changed files with 74 additions and 33 deletions

View File

@@ -20,15 +20,15 @@
* *
* This file is Created by fankes on 2022/1/24. * This file is Created by fankes on 2022/1/24.
*/ */
@file:Suppress("MemberVisibilityCanBePrivate")
package com.fankes.miui.notify.params package com.fankes.miui.notify.params
import android.content.Context import android.content.Context
import android.graphics.Color import android.graphics.Color
import com.fankes.miui.notify.bean.IconDataBean import com.fankes.miui.notify.bean.IconDataBean
import com.fankes.miui.notify.hook.HookConst.NOTIFY_ICON_DATAS import com.fankes.miui.notify.hook.HookConst.NOTIFY_ICON_DATAS
import com.fankes.miui.notify.utils.bitmap import com.fankes.miui.notify.utils.*
import com.fankes.miui.notify.utils.safeOf
import com.fankes.miui.notify.utils.safeOfNan
import com.highcapable.yukihookapi.hook.factory.modulePrefs import com.highcapable.yukihookapi.hook.factory.modulePrefs
import com.highcapable.yukihookapi.hook.param.PackageParam import com.highcapable.yukihookapi.hook.param.PackageParam
import org.json.JSONArray import org.json.JSONArray
@@ -47,7 +47,7 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
* 已存储的 JSON 数据 * 已存储的 JSON 数据
* @return [String] * @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 { if (it.isNotBlank()) runCatching {
JSONArray(it).also { array -> JSONArray(it).also { array ->
for (i in 0 until array.length()) runCatching { for (i in 0 until array.length()) runCatching {
(array.get(i) as JSONObject).apply { add(convertToBean(array.get(i) as JSONObject)!!)
add( }.onFailure { context?.snake(msg = "部分规则加载失败") }
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")
)
)
}
}
} }
} }.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 * @param dataJson1 图标数据 JSON
@@ -93,7 +100,21 @@ class IconPackParams(private val context: Context? = null, private val param: Pa
* @param json 数据 * @param json 数据
* @return [Boolean] * @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("}") }
/** /**
* 是否为异常地址 * 是否为异常地址

View File

@@ -294,22 +294,42 @@ class ConfigureActivity : BaseActivity() {
invalidate() invalidate()
} }
} }
confirmButton { IconPackParams(context = this@ConfigureActivity).also { params ->
IconPackParams(context = this@ConfigureActivity).also { params -> confirmButton(text = "合并") {
when { editText.text.toString().also { jsonString ->
editText.text.toString().isNotBlank() && params.isNotVaildJson(editText.text.toString()) -> when {
snake(msg = "不是有效的 JSON 数据") jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
editText.text.toString().isNotBlank() -> { jsonString.isNotBlank() -> {
params.save(editText.text.toString()) params.save(
filterText = "" params.splicingJsonArray(
mockLocalData() dataJson1 = params.storageDataJson ?: "[]",
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity) 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 = "取消")
} }
} }
} }

View File

@@ -19,7 +19,7 @@
android:layout_height="150dp" android:layout_height="150dp"
android:ellipsize="end" android:ellipsize="end"
android:gravity="center|start|top" android:gravity="center|start|top"
android:hint="请粘贴 JSON 规则数组到此处" android:hint="请粘贴 JSON 规则到此处"
android:textSize="14sp" /> android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
</LinearLayout> </LinearLayout>