@@ -24,52 +24,51 @@ object ThemeModeState {
2424 val wallpaper = WallpaperManager .getInstance(activity)
2525 val wallpaperInfo = wallpaper.wallpaperInfo
2626
27- // File wallpaper tùy chỉnh
2827 val customWallpaperFile = File (activity.filesDir, " home/etc/wallpaper.jpg" )
2928 val useCustomWallpaper = customWallpaperFile.exists()
3029
3130 when (level.coerceIn(0 , 5 )) {
32- 0 -> { // System default
31+ 0 -> {
3332 val nightModeFlags = activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
3433 val isNight = nightModeFlags == android.content.res.Configuration .UI_MODE_NIGHT_YES
3534 themeMode.isDarkMode = isNight
3635 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM )
3736 activity.setTheme(if (isNight) R .style.AppThemeDark else R .style.AppTheme )
3837 }
39- 1 -> { // Dark
38+ 1 -> {
4039 themeMode.isDarkMode = true
4140 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
4241 activity.setTheme(R .style.AppThemeDark )
4342 }
44- 2 -> { // Light
43+ 2 -> {
4544 themeMode.isDarkMode = false
4645 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
4746 activity.setTheme(R .style.AppTheme )
4847 }
49- 3 -> { // Wallpaper system
48+ 3 -> {
5049 val nightModeFlags = activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
5150 val isNight = nightModeFlags == android.content.res.Configuration .UI_MODE_NIGHT_YES
5251 themeMode.isDarkMode = isNight
5352 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM )
5453 applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper)
5554 }
56- 4 -> { // Wallpaper Dark
55+ 4 -> {
5756 themeMode.isDarkMode = true
5857 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
5958 applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper, true )
6059 }
61- 5 -> { // Wallpaper Light
60+ 5 -> {
6261 themeMode.isDarkMode = false
6362 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
6463 applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper, false )
6564 }
6665 }
6766
67+ // CẬP NHẬT TẠI ĐÂY: Luôn gọi captureAndBlur khi ở chế độ Wallpaper
68+ // BlurController sẽ tự chịu trách nhiệm so sánh file cũ/mới
6869 if (level >= 3 ) {
6970 BlurEngine .isPaused = false
70- if (BlurEngine .blurBitmap == null || BlurEngine .blurBitmap.isRecycled) {
71- BlurEngine .controller.captureAndBlur(activity)
72- }
71+ BlurEngine .controller.captureAndBlur(activity)
7372 } else {
7473 BlurEngine .isPaused = true
7574 }
@@ -90,62 +89,47 @@ object ThemeModeState {
9089 activity.setTheme(if (night) R .style.AppThemeWallpaper else R .style.AppThemeWallpaperLight )
9190
9291 try {
93- val customWallpaperFile = File (activity.filesDir, " home/etc/wallpaper.jpg" )
94- if (useCustomWallpaper && customWallpaperFile.exists()) {
95- val bitmap = BitmapFactory .decodeFile(customWallpaperFile.absolutePath)
96- activity.window.setBackgroundDrawable(android.graphics.drawable.BitmapDrawable (activity.resources, bitmap))
92+ if (useCustomWallpaper) {
93+ val customWallpaperFile = File (activity.filesDir, " home/etc/wallpaper.jpg" )
94+ if (customWallpaperFile.exists()) {
95+ val bitmap = BitmapFactory .decodeFile(customWallpaperFile.absolutePath)
96+ activity.window.setBackgroundDrawable(android.graphics.drawable.BitmapDrawable (activity.resources, bitmap))
97+ }
9798 } else if (wallpaperInfo != null && wallpaperInfo.packageName != null ) {
9899 activity.window.addFlags(WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER )
99100 } else {
101+ // Xóa cache WallpaperManager trước khi lấy Drawable
102+ wallpaper.forgetLoadedWallpaper()
100103 activity.window.setBackgroundDrawable(wallpaper.drawable)
101104 }
102105 } catch (e: Exception ) {
103- if (wallpaperInfo != null && wallpaperInfo.packageName != null ) {
104- activity.window.addFlags(WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER )
105- } else {
106- activity.window.setBackgroundDrawable(wallpaper.drawable)
107- }
106+ activity.window.addFlags(WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER )
108107 }
109108 }
110109
111110 private fun applyWindowFlags (activity : Activity ) {
112111 val window = activity.window
113-
114- // 1. Kích hoạt Edge-to-Edge (Hỗ trợ từ API 21+ thông qua WindowCompat)
115112 androidx.core.view.WindowCompat .setDecorFitsSystemWindows(window, false )
116-
117- // Đặt màu trong suốt cho thanh hệ thống (API 21+)
118113 window.statusBarColor = android.graphics.Color .TRANSPARENT
119114 window.navigationBarColor = android.graphics.Color .TRANSPARENT
120-
121- // 2. Xử lý WindowInsets (Cần API 21+ để hoạt động ổn định)
115+
122116 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
123117 val rootView = window.decorView.rootView
124118 androidx.core.view.ViewCompat .setOnApplyWindowInsetsListener(rootView) { _, insets ->
125119 val systemBars = insets.getInsets(androidx.core.view.WindowInsetsCompat .Type .systemBars())
126-
127120 activity.findViewById<View >(R .id.blur_top_container)?.setPadding(0 , systemBars.top, 0 , 0 )
128121 activity.findViewById<View >(R .id.file_selector_list)?.setPadding(0 , systemBars.top, 0 , 0 )
129122 activity.findViewById<View >(R .id.main_list)?.setPadding(0 , systemBars.top, 0 , 0 )
130123 activity.findViewById<View >(R .id.blur_bottom_container)?.setPadding(0 , 0 , 0 , systemBars.bottom)
131-
132124 insets
133125 }
134126 }
135-
136- // 3. Logic xử lý Icon (Light/Dark mode)
127+
137128 val controller = androidx.core.view.WindowInsetsControllerCompat (window, window.decorView)
138-
139129 if (! themeMode.isDarkMode) {
140- // Mode Sáng: Cần icon tối màu
141- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
142- controller.isAppearanceLightStatusBars = true
143- }
144- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
145- controller.isAppearanceLightNavigationBars = true
146- }
130+ controller.isAppearanceLightStatusBars = true
131+ controller.isAppearanceLightNavigationBars = true
147132 } else {
148- // Mode Tối: Cần icon sáng màu
149133 controller.isAppearanceLightStatusBars = false
150134 controller.isAppearanceLightNavigationBars = false
151135 }
0 commit comments