Skip to content

Commit e0de0f6

Browse files
committed
feat: Context.getIdentifier support third part app
Signed-off-by: Hu Shenghao <dede.hu@qq.com>
1 parent 84dd1ce commit e0de0f6

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

basic/src/main/java/com/dede/basic/DrawableKt.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.dede.basic
55

66
import android.annotation.SuppressLint
77
import android.content.Context
8+
import android.content.pm.PackageManager
89
import android.content.res.Resources
910
import android.graphics.drawable.Drawable
1011
import android.os.Build
@@ -59,8 +60,22 @@ fun Context.getIdentifier(name: String, defType: DefType, defPackage: String = p
5960
val key = makeKey(name, defType, defPackage)
6061
var id = identifierCache.get(key)
6162
if (id == null) {
63+
val appResources: Resources? = when (defPackage) {
64+
packageName -> resources
65+
"android" -> Resources.getSystem()
66+
else -> {
67+
val pm = packageManager
68+
try {
69+
val applicationInfo =
70+
pm.getApplicationInfo(defPackage, PackageManager.GET_META_DATA)
71+
pm.getResourcesForApplication(applicationInfo)
72+
} catch (ignore: PackageManager.NameNotFoundException) {
73+
null
74+
}
75+
}
76+
}
6277
val type = defType.toString()
63-
id = resources.getIdentifier(name, type, defPackage)
78+
id = appResources?.getIdentifier(name, type, defPackage) ?: Resources.ID_NULL
6479
identifierCache.put(key, id)
6580
}
6681
return id

core/settings/src/main/java/com/dede/android_eggs/views/settings/compose/prefs/IconShapePrefUtil.kt

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
package com.dede.android_eggs.views.settings.compose.prefs
44

5-
import android.content.Context
6-
import android.content.res.Resources
7-
import android.os.Build
8-
import android.text.TextUtils
95
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
106
import androidx.compose.material3.MaterialShapes
117
import androidx.compose.material3.toShape
@@ -17,37 +13,13 @@ import androidx.graphics.shapes.RoundedPolygon
1713
import androidx.graphics.shapes.rectangle
1814
import androidx.graphics.shapes.star
1915
import com.dede.android_eggs.views.settings.compose.basic.SettingPrefUtil
20-
import com.dede.android_eggs.views.settings.compose.utils.PathShape
21-
import com.dede.basic.DefType
22-
import com.dede.basic.getIdentifier
16+
import com.dede.android_eggs.views.settings.compose.utils.SystemIconMaskUtil
2317
import sv.lib.squircleshape.SquircleShape
2418

2519
object IconShapePrefUtil {
2620

2721
const val KEY_ICON_SHAPE = "pref_key_override_icon_shape"
2822

29-
private var sCachedSystemIconShape: Shape? = null
30-
31-
private fun getSystemIconMaskShape(context: Context): Shape? {
32-
if (sCachedSystemIconShape != null) {
33-
return sCachedSystemIconShape
34-
}
35-
var pathStr: String? = null
36-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
37-
val resId = context.getIdentifier("config_icon_mask", DefType.STRING, "android")
38-
if (resId != Resources.ID_NULL) {
39-
try {
40-
pathStr = context.resources.getString(resId)
41-
} catch (ignore: Resources.NotFoundException) {
42-
}
43-
}
44-
}
45-
if (pathStr == null || TextUtils.isEmpty(pathStr)) {
46-
return null
47-
}
48-
return PathShape(pathStr).also { sCachedSystemIconShape = it }
49-
}
50-
5123
fun getIconShapeRoundedPolygon(index: Int): RoundedPolygon? {
5224
return polygonItems.getOrNull(index)
5325
}
@@ -63,10 +35,9 @@ object IconShapePrefUtil {
6335
if (shape != null) {
6436
return shape
6537
}
66-
return getSystemIconMaskShape(LocalContext.current) ?: defaultSquare.toShape()
38+
return SystemIconMaskUtil.getIconMaskShape(LocalContext.current) ?: defaultSquare.toShape()
6739
}
6840

69-
7041
@Composable
7142
fun RoundedPolygon?.toShapePlusNullable(): Shape? {
7243
if (this == null) return null
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.dede.android_eggs.views.settings.compose.utils
2+
3+
import android.content.Context
4+
import android.content.res.Resources
5+
import android.os.Build
6+
import android.text.TextUtils
7+
import androidx.compose.ui.graphics.Shape
8+
import com.dede.basic.DefType
9+
import com.dede.basic.getIdentifier
10+
11+
object SystemIconMaskUtil {
12+
13+
private const val CONFIG_ICON_MASK = "config_icon_mask"
14+
15+
private var isCalledGetSystemIconMaskShape = false
16+
private var sCachedSystemIconShape: Shape? = null
17+
18+
fun getIconMaskShape(context: Context): Shape? {
19+
if (sCachedSystemIconShape != null || isCalledGetSystemIconMaskShape) {
20+
return sCachedSystemIconShape
21+
}
22+
23+
isCalledGetSystemIconMaskShape = true
24+
val iconMask = getIconMaskPath(context)
25+
if (iconMask == null || TextUtils.isEmpty(iconMask)) {
26+
return null
27+
}
28+
return PathShape(iconMask).also { sCachedSystemIconShape = it }
29+
}
30+
31+
private fun getIconMaskPath(context: Context): String? {
32+
var pathStr: String? = null
33+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
34+
val resId = context.getIdentifier(CONFIG_ICON_MASK, DefType.STRING, "android")
35+
if (resId != Resources.ID_NULL) {
36+
try {
37+
pathStr = context.resources.getString(resId)
38+
} catch (ignore: Resources.NotFoundException) {
39+
}
40+
}
41+
}
42+
return pathStr
43+
}
44+
}

0 commit comments

Comments
 (0)