Skip to content

Commit b074286

Browse files
author
Fastace
committed
feat(core): 新增桌面图标样式切换,设置页横排预览、启动遮罩同步换肤
用户提供三套桌面图标(默认/设备桥/安全同步)可供选择。实现方式是 activity-alias + 组件启停, 与 Telegram 官方结构同构:SplashActivity 只保留 MAIN、不带 LAUNCHER,桌面入口 100% 由别名提供, 切换时先启用目标再禁用其余,保证任意时刻恰好一个入口。首版把默认入口挂在真实 Activity 上, 一旦切走就会用 setComponentEnabledSetting 把唯一入口自己禁用掉、桌面彻底没图标(该状态还会被 PackageManager 持久化,覆盖安装也无法自愈),故最终改为「全别名」结构。 一、样式枚举与偏好(core/model/AppIconType、core/datastore/Int.kt) - 新增 AppIconType(DEFAULT=0/DEVICE_BRIDGE=1/SHIELD_SYNC=2),of(id) 非法值回退 DEFAULT - 新增 KeyAppIcon("app_icon") 及 readAppIcon()/saveAppIcon(),defValue=0,老用户读不到键即默认图标 二、图标资产与清单入口(core/ui/drawable、app/mipmap-anydpi-v26、app/AndroidManifest.xml) - 新增设备桥、安全同步两套 foreground(108dp 视口、安全区缩放),背景层色值落在 core/ui/values/colors.xml - 两个 mipmap-anydpi-v26 adaptive-icon 与 SplashActivityBridge/Shield 别名一一对应;默认别名不声明 icon/label,直接继承 <application> 上的 ic_launcher - 默认别名 enabled=true,另两个 enabled=false;「任一时刻恰好一个」的约束写进注释避免后续误改 三、切换与自愈(core/util/AppIconUtil) - apply() 先 setComponentEnabledSetting(ENABLED) 目标、再 DISABLED 其余,避免中间出现无入口空窗 - 三个样式共用一次读到的 AppIconType,SplashActivity 每次冷启动按偏好 apply 一次,用于升级或异常 场景下把别名状态矫正回与偏好一致 - exitApp() 结束后再 killProcess:launcher 只在进程重开后重新加载组件信息,否则新图标不会生效 四、设置页交互(feature/main/settings/Index.kt、core/ui/component/Settings.kt) - 图标选择改为横向三等分,去掉名称与 RadioButton,选中态用主色描边,名称只作无障碍描述 - 设置行右侧胶囊由文本改为展示当前样式真实图标:Selectable 增加可选参数 currentIcon,非空时画图标, current 退化为 contentDescription,其它调用点行为不变 - 中英双语各新增 6 条字符串;一次性栅格化三个图标并缓存,避免选中态变化重组时反复解码 五、启动遮罩跟随所选样式(app/SplashActivity、app/res/values/themes.xml、values-v31/colors.xml) - activity-alias 不能声明 android:theme,故在 super.onCreate 之前按偏好 setTheme —— 遮罩图标与背景色 都取自 activity 主题,时机必须早于 installSplashScreen() - 新增 SplashScreen.DeviceBridge / ShieldSync 两套主题,分别指定对应 foreground 与背景色 - 背景色沿用项目既有 M3E 取色:v31 下设备桥=system_accent3_100、安全同步=system_accent2_100, 低版本回退到各自的品牌底色 六、预览取图修复(core/util/AppIconUtil.getIcon) - 原用 packageManager.getActivityIcon(component):PM 默认不解析已禁用组件,会抛 NameNotFoundException, 三个方块里只有当前启用那个能拿到图标、其余 runCatching 后变 null 显示为空白——这正是「只有选中项 渲染」的真因,与重组时机无关 - 改为按资源名 getIdentifier(mipmap) + resources.getDrawable 直接读取(ic_launcher / ic_launcher_bridge / ic_launcher_shield,已核对与实际 mipmap 名一致),与组件启用状态解耦;取不到时再退回 PackageManager 验证 - 真机实测通过:三套图标在设置页均能正常预览、选择后桌面图标即时切换;冷启动遮罩图标与背景色跟随 当前样式;覆盖安装保留用户上次选择 - :core:util :core:ui :feature:main:settings compileDebugKotlin 与 :app:compileArm64-v8aAlphaDebugKotlin --offline 均 BUILD SUCCESSFUL - :app:processArm64-v8aAlphaDebugResources BUILD SUCCESSFUL,合并后 manifest 核对:SplashActivity 仅 MAIN, SplashActivityDefault 启用、Bridge/Shield 禁用 - :core:ui:lintDebug、:feature:main:settings:lintDebug、:core:util:lintDebug BUILD SUCCESSFUL - 已知遗留:① 部分 ROM 桌面不把 activity-alias 当入口,此类机型只能使用默认图标;② 入口组件由 SplashActivity 变为 SplashActivityDefault,用户拖到桌面的旧快捷方式会失效
1 parent 1a6917f commit b074286

16 files changed

Lines changed: 495 additions & 5 deletions

File tree

‎source/app/src/main/AndroidManifest.xml‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,63 @@
3030
tools:replace="android:enableOnBackInvokedCallback"
3131
tools:targetApi="31">
3232

33+
<!--
34+
图标切换与 Telegram 同构:
35+
SplashActivity 只保留 MAIN、不带 LAUNCHER,桌面入口全部由 activity-alias 提供,
36+
任一时刻恰好启用一个别名。好处是永远不需要禁用真实 Activity,
37+
不会出现"入口被自己禁用掉、桌面彻底没图标"的情况。
38+
-->
3339
<activity
3440
android:name="com.xayah.databackup.SplashActivity"
3541
android:exported="true"
3642
android:theme="@style/Theme.DataBackup.SplashScreen">
3743
<intent-filter>
3844
<action android:name="android.intent.action.MAIN" />
39-
<category android:name="android.intent.category.LAUNCHER" />
4045
</intent-filter>
4146
</activity>
4247

48+
<!-- 默认图标:不声明 icon/label,直接继承 <application> 上的 ic_launcher 与 app_name。 -->
49+
<activity-alias
50+
android:name="com.xayah.databackup.SplashActivityDefault"
51+
android:enabled="true"
52+
android:exported="true"
53+
android:targetActivity="com.xayah.databackup.SplashActivity">
54+
<intent-filter>
55+
<action android:name="android.intent.action.MAIN" />
56+
<category android:name="android.intent.category.LAUNCHER" />
57+
</intent-filter>
58+
</activity-alias>
59+
60+
<!-- 可选图标:默认禁用,切换时启用其中一个并禁用其余别名。 -->
61+
62+
<activity-alias
63+
android:name="com.xayah.databackup.SplashActivityBridge"
64+
android:enabled="false"
65+
android:exported="true"
66+
android:icon="@mipmap/ic_launcher_bridge"
67+
android:label="@string/app_name"
68+
android:roundIcon="@mipmap/ic_launcher_bridge"
69+
android:targetActivity="com.xayah.databackup.SplashActivity">
70+
<intent-filter>
71+
<action android:name="android.intent.action.MAIN" />
72+
<category android:name="android.intent.category.LAUNCHER" />
73+
</intent-filter>
74+
</activity-alias>
75+
76+
<activity-alias
77+
android:name="com.xayah.databackup.SplashActivityShield"
78+
android:enabled="false"
79+
android:exported="true"
80+
android:icon="@mipmap/ic_launcher_shield"
81+
android:label="@string/app_name"
82+
android:roundIcon="@mipmap/ic_launcher_shield"
83+
android:targetActivity="com.xayah.databackup.SplashActivity">
84+
<intent-filter>
85+
<action android:name="android.intent.action.MAIN" />
86+
<category android:name="android.intent.category.LAUNCHER" />
87+
</intent-filter>
88+
</activity-alias>
89+
4390
<activity
4491
android:name="com.xayah.databackup.MainActivity"
4592
android:exported="false"

‎source/app/src/main/kotlin/com/xayah/databackup/SplashActivity.kt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,42 @@ import androidx.compose.animation.ExperimentalAnimationApi
88
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
99
import androidx.core.view.WindowCompat
1010
import com.xayah.core.datastore.getCurrentAppVersionName
11+
import com.xayah.core.datastore.readAppIcon
1112
import com.xayah.core.datastore.readAppVersionName
13+
import com.xayah.core.model.AppIconType
14+
import com.xayah.core.util.AppIconUtil
1215
import com.xayah.core.work.WorkManagerInitializer
1316
import dagger.hilt.android.AndroidEntryPoint
1417
import kotlinx.coroutines.flow.first
1518
import kotlinx.coroutines.runBlocking
1619
import com.xayah.feature.setup.MainActivity as SetupActivity
1720

21+
/** 启动遮罩主题:图标与背景色跟随当前桌面图标样式。 */
22+
private val AppIconType.splashTheme: Int
23+
get() = when (this) {
24+
AppIconType.DEFAULT -> R.style.Theme_DataBackup_SplashScreen
25+
AppIconType.DEVICE_BRIDGE -> R.style.Theme_DataBackup_SplashScreen_DeviceBridge
26+
AppIconType.SHIELD_SYNC -> R.style.Theme_DataBackup_SplashScreen_ShieldSync
27+
}
28+
1829
@SuppressLint("CustomSplashScreen")
1930
@AndroidEntryPoint
2031
class SplashActivity : AppCompatActivity() {
2132

2233
// Workaround for HarmonyOS
2334
@ExperimentalAnimationApi
2435
override fun onCreate(savedInstanceState: Bundle?) {
36+
// 启动遮罩的图标与配色取自 activity 主题,
37+
// 而 activity-alias 无法声明 theme,必须在 super.onCreate 之前按当前图标样式替换。
38+
val appIcon = AppIconType.of(runBlocking { readAppIcon().first() })
39+
setTheme(appIcon.splashTheme)
2540
super.onCreate(savedInstanceState)
2641
WindowCompat.setDecorFitsSystemWindows(window, false)
2742
installSplashScreen()
2843

44+
// 升级或异常场景下矫正别名状态,确保启用的别名与偏好一致(任一时刻恰好一个)。
45+
AppIconUtil.apply(this, appIcon)
46+
2947
if (getCurrentAppVersionName() > runBlocking { readAppVersionName().first() }) {
3048
// There is an update
3149
startActivity(Intent(this, SetupActivity::class.java))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@color/ic_launcher_bridge_background" />
4+
<foreground android:drawable="@drawable/ic_launcher_bridge_foreground" />
5+
<monochrome android:drawable="@drawable/ic_launcher_bridge_foreground" />
6+
</adaptive-icon>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3+
<background android:drawable="@color/ic_launcher_shield_background" />
4+
<foreground android:drawable="@drawable/ic_launcher_shield_foreground" />
5+
<monochrome android:drawable="@drawable/ic_launcher_shield_foreground" />
6+
</adaptive-icon>

‎source/app/src/main/res/values-v31/colors.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<color name="ic_launcher_background">@android:color/system_accent1_100</color>
4+
<color name="ic_launcher_bridge_background">@android:color/system_accent3_100</color>
5+
<color name="ic_launcher_shield_background">@android:color/system_accent2_100</color>
46
<color name="ic_launcher_foreground_main">@android:color/system_accent1_400</color>
57
<color name="ic_launcher_foreground_sub">@color/colorPrimary</color>
68
<color name="colorPrimary">@android:color/system_accent1_600</color>

‎source/app/src/main/res/values/themes.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@
1010
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
1111
<item name="postSplashScreenTheme">@style/Theme.DataBackup</item>
1212
</style>
13+
14+
<!-- 与所选桌面图标配套:活动别名无法指定 theme,因此在 SplashActivity 启动时按偏好 setTheme。 -->
15+
<style name="Theme.DataBackup.SplashScreen.DeviceBridge" parent="Theme.DataBackup.SplashScreen">
16+
<item name="windowSplashScreenBackground">@color/ic_launcher_bridge_background</item>
17+
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_bridge_foreground</item>
18+
</style>
19+
20+
<style name="Theme.DataBackup.SplashScreen.ShieldSync" parent="Theme.DataBackup.SplashScreen">
21+
<item name="windowSplashScreenBackground">@color/ic_launcher_shield_background</item>
22+
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_shield_foreground</item>
23+
</style>
1324
</resources>

‎source/core/datastore/src/main/kotlin/com/xayah/core/datastore/Int.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ val KeyScreenOffTimeout = intPreferencesKey("screen_off_timeout")
1010
val KeyRestoreUser = intPreferencesKey("restore_user")
1111
val KeyCompressionLevel = intPreferencesKey("compression_level")
1212
val KeyUpdateChannel = intPreferencesKey("update_channel")
13+
val KeyAppIcon = intPreferencesKey("app_icon")
1314

1415

1516
// -----------------------------------------Read-----------------------------------------
@@ -18,11 +19,13 @@ fun Context.readScreenOffTimeout() = readStoreInt(key = KeyScreenOffTimeout, def
1819
fun Context.readRestoreUser() = readStoreInt(key = KeyRestoreUser, defValue = -1)
1920
fun Context.readCompressionLevel() = readStoreInt(key = KeyCompressionLevel, defValue = 1)
2021
fun Context.readUpdateChannel() = readStoreInt(key = KeyUpdateChannel, defValue = 0)
22+
fun Context.readAppIcon() = readStoreInt(key = KeyAppIcon, defValue = 0)
2123

2224

2325
// -----------------------------------------Write-----------------------------------------
2426
suspend fun Context.saveScreenOffCountDown(value: Int) = saveStoreInt(key = KeyScreenOffCountDown, value = value)
2527
suspend fun Context.saveScreenOffTimeout(value: Int) = saveStoreInt(key = KeyScreenOffTimeout, value = value)
2628
suspend fun Context.saveRestoreUser(value: Int) = saveStoreInt(key = KeyRestoreUser, value = value)
2729
suspend fun Context.saveCompressionLevel(value: Int) = saveStoreInt(key = KeyCompressionLevel, value = value)
28-
suspend fun Context.saveUpdateChannel(value: Int) = saveStoreInt(key = KeyUpdateChannel, value = value)
30+
suspend fun Context.saveUpdateChannel(value: Int) = saveStoreInt(key = KeyUpdateChannel, value = value)
31+
suspend fun Context.saveAppIcon(value: Int) = saveStoreInt(key = KeyAppIcon, value = value)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.xayah.core.model
2+
3+
/**
4+
* 可选的桌面图标样式,与 AndroidManifest 中的 activity-alias 一一对应。
5+
*
6+
* 切换原理:三个别名共享同一个 SplashActivity,任一时刻只启用其中一个组件,
7+
* 其余保持禁用,以此改变桌面显示的图标。
8+
*/
9+
enum class AppIconType(val id: Int) {
10+
/** 原始图标 */
11+
DEFAULT(0),
12+
13+
/** 设备桥 Device Bridge:两台设备之间的数据迁移 */
14+
DEVICE_BRIDGE(1),
15+
16+
/** 安全同步 Shield Sync:盾牌与双向同步环 */
17+
SHIELD_SYNC(2),
18+
;
19+
20+
companion object {
21+
fun of(id: Int): AppIconType = values().firstOrNull { it.id == id } ?: DEFAULT
22+
}
23+
}

‎source/core/ui/src/main/kotlin/com/xayah/core/ui/component/Settings.kt‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.xayah.core.ui.component
22

33
import androidx.compose.animation.ExperimentalAnimationApi
4+
import androidx.compose.foundation.Image
45
import androidx.compose.foundation.Indication
56
import androidx.compose.foundation.interaction.MutableInteractionSource
67
import androidx.compose.foundation.layout.Arrangement
@@ -11,6 +12,7 @@ import androidx.compose.foundation.layout.ColumnScope
1112
import androidx.compose.foundation.layout.IntrinsicSize
1213
import androidx.compose.foundation.layout.Row
1314
import androidx.compose.foundation.layout.RowScope
15+
import androidx.compose.foundation.layout.size
1416
import androidx.compose.foundation.layout.fillMaxHeight
1517
import androidx.compose.foundation.layout.fillMaxWidth
1618
import androidx.compose.foundation.layout.height
@@ -29,9 +31,11 @@ import androidx.compose.runtime.remember
2931
import androidx.compose.runtime.rememberCoroutineScope
3032
import androidx.compose.ui.Alignment
3133
import androidx.compose.ui.Modifier
34+
import androidx.compose.ui.graphics.ImageBitmap
3235
import androidx.compose.ui.graphics.vector.ImageVector
3336
import androidx.compose.ui.platform.LocalContext
3437
import androidx.compose.ui.text.font.FontWeight
38+
import androidx.compose.ui.unit.dp
3539
import androidx.datastore.preferences.core.Preferences
3640
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3741
import com.xayah.core.datastore.readStoreBoolean
@@ -215,7 +219,9 @@ fun Selectable(
215219
title: String,
216220
value: String? = null,
217221
desc: String? = null,
218-
current: String,
222+
current: String = "",
223+
/** 非空时按钮内展示该图标而不是 [current] 文本。 */
224+
currentIcon: ImageBitmap? = null,
219225
onClick: suspend () -> Unit = suspend {}
220226
) {
221227
val scope = rememberCoroutineScope()
@@ -229,8 +235,15 @@ fun Selectable(
229235
},
230236
trailingContent = {
231237
FilledTonalButton(enabled = enabled, onClick = { scope.launch { onClick() } }) {
232-
233-
Text(text = current)
238+
if (currentIcon != null) {
239+
Image(
240+
bitmap = currentIcon,
241+
contentDescription = current,
242+
modifier = Modifier.size(28.dp),
243+
)
244+
} else {
245+
Text(text = current)
246+
}
234247
}
235248
},
236249
onClick = { scope.launch { onClick() } }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
设备桥 Device Bridge:两台设备之间的数据迁移。
4+
设计稿视口 128x128,此处按 adaptive icon 规范缩至 108dp 画布的安全区(18dp ~ 90dp)内。
5+
-->
6+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
7+
android:width="108dp"
8+
android:height="108dp"
9+
android:viewportWidth="128"
10+
android:viewportHeight="128">
11+
<group
12+
android:pivotX="64.0"
13+
android:pivotY="64.0"
14+
android:scaleX="0.82"
15+
android:scaleY="0.82">
16+
<!-- 左侧设备 -->
17+
<path
18+
android:fillColor="#A63C35"
19+
android:pathData="M20,38a13,13 0 0 1 13,-13h13a13,13 0 0 1 13,13v52a13,13 0 0 1 -13,13h-13a13,13 0 0 1 -13,-13z" />
20+
<!-- 右侧设备 -->
21+
<path
22+
android:fillColor="#5D1916"
23+
android:pathData="M69,38a13,13 0 0 1 13,-13h13a13,13 0 0 1 13,13v52a13,13 0 0 1 -13,13h-13a13,13 0 0 1 -13,-13z" />
24+
<!-- 左侧设备顶部细节 -->
25+
<path
26+
android:pathData="m31,43 -4,-6m21,6 4,-6M29,51c0,-6 4.8,-11 11,-11s11,5 11,11"
27+
android:strokeColor="#FFEDEA"
28+
android:strokeWidth="4"
29+
android:strokeLineCap="round"
30+
android:strokeLineJoin="round"
31+
android:fillColor="#00000000" />
32+
<path
33+
android:fillColor="#FFEDEA"
34+
android:pathData="M32.5,50a2.5,2.5 0 1 1 5,0a2.5,2.5 0 1 1 -5,0" />
35+
<path
36+
android:fillColor="#FFEDEA"
37+
android:pathData="M42.5,50a2.5,2.5 0 1 1 5,0a2.5,2.5 0 1 1 -5,0" />
38+
<!-- 右侧设备底部按键 -->
39+
<path
40+
android:pathData="M84,87h9"
41+
android:strokeColor="#FFEDEA"
42+
android:strokeWidth="5"
43+
android:strokeLineCap="round"
44+
android:strokeLineJoin="round"
45+
android:fillColor="#00000000" />
46+
<!-- 桥接箭头 -->
47+
<path
48+
android:pathData="M48,63h29"
49+
android:strokeColor="#FFF8F6"
50+
android:strokeWidth="8"
51+
android:strokeLineCap="round"
52+
android:strokeLineJoin="round"
53+
android:fillColor="#00000000" />
54+
<path
55+
android:pathData="m68,54 9,9 -9,9"
56+
android:strokeColor="#FFF8F6"
57+
android:strokeWidth="8"
58+
android:strokeLineCap="round"
59+
android:strokeLineJoin="round"
60+
android:fillColor="#00000000" />
61+
</group>
62+
</vector>

0 commit comments

Comments
 (0)