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

This commit is contained in:
2022-03-06 03:02:37 +08:00
parent e529e75bc2
commit 3e2b8247c8
3 changed files with 74 additions and 33 deletions

View File

@@ -20,6 +20,8 @@
* *
* 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.coloros.notify.param package com.fankes.coloros.notify.param
import android.content.Context import android.content.Context
@@ -27,9 +29,7 @@ import android.graphics.Bitmap
import android.graphics.Color import android.graphics.Color
import com.fankes.coloros.notify.bean.IconDataBean import com.fankes.coloros.notify.bean.IconDataBean
import com.fankes.coloros.notify.hook.HookConst.NOTIFY_ICON_DATAS import com.fankes.coloros.notify.hook.HookConst.NOTIFY_ICON_DATAS
import com.fankes.coloros.notify.utils.bitmap import com.fankes.coloros.notify.utils.*
import com.fankes.coloros.notify.utils.safeOf
import com.fankes.coloros.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
@@ -101,7 +101,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)
/** /**
* 获取图标数据 * 获取图标数据
@@ -113,22 +113,29 @@ 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 = "部分规则加载失败") }
}
}.onFailure { context?.snake(msg = "规则加载发生错误") }
}
}
/**
* 转换为 [IconDataBean]
* @param jsonObject Json 实例
* @return [IconDataBean] or null
*/
private fun convertToBean(jsonObject: JSONObject) = safeOfNull {
jsonObject.let {
IconDataBean( IconDataBean(
appName = getString("appName"), appName = it.getString("appName"),
packageName = getString("packageName"), packageName = it.getString("packageName"),
isEnabled = getBoolean("isEnabled"), isEnabled = it.getBoolean("isEnabled"),
isEnabledAll = getBoolean("isEnabledAll"), isEnabledAll = it.getBoolean("isEnabledAll"),
iconBitmap = getString("iconBitmap").bitmap, iconBitmap = it.getString("iconBitmap").bitmap,
iconColor = safeOfNan { Color.parseColor(getString("iconColor")) }, iconColor = safeOfNan { Color.parseColor(it.getString("iconColor")) },
contributorName = getString("contributorName") contributorName = it.getString("contributorName")
) )
)
}
}
}
}
} }
} }
@@ -147,7 +154,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 = "合并") {
editText.text.toString().also { jsonString ->
when { when {
editText.text.toString().isNotBlank() && params.isNotVaildJson(editText.text.toString()) -> jsonString.isNotBlank() && params.isNotVaildJson(jsonString) -> snake(msg = "不是有效的 JSON 数据")
snake(msg = "不是有效的 JSON 数据") jsonString.isNotBlank() -> {
editText.text.toString().isNotBlank() -> { params.save(
params.save(editText.text.toString()) params.splicingJsonArray(
dataJson1 = params.storageDataJson ?: "[]",
dataJson2 = jsonString.takeIf { params.isJsonArray(it) } ?: "[$jsonString]"
)
)
filterText = "" filterText = ""
mockLocalData() mockLocalData()
SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity) SystemUITool.showNeedUpdateApplySnake(context = this@ConfigureActivity)
} }
else -> snake(msg = "规则数组内容为空") else -> snake(msg = "请输入有效内容")
} }
} }
} }
cancelButton() 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 = "请输入有效内容")
}
}
}
}
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>