2020 *
2121 * This file is Created by fankes on 2022/1/24.
2222 */
23+ @file:Suppress(" MemberVisibilityCanBePrivate" )
24+
2325package com.fankes.coloros.notify.param
2426
2527import android.content.Context
2628import android.graphics.Bitmap
2729import android.graphics.Color
2830import com.fankes.coloros.notify.bean.IconDataBean
2931import 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.*
3333import com.highcapable.yukihookapi.hook.factory.modulePrefs
3434import com.highcapable.yukihookapi.hook.param.PackageParam
3535import 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 * 是否为异常地址
0 commit comments