@@ -2,16 +2,21 @@ package com.tool.tree
22
33import android.app.Activity
44import android.app.WallpaperManager
5- import android.graphics.BitmapFactory
5+ import android.graphics.Color
6+ import android.graphics.drawable.BitmapDrawable
7+ import android.graphics.drawable.Drawable
68import android.os.Build
79import android.view.View
810import android.view.WindowManager
911import androidx.appcompat.app.AppCompatDelegate
12+ import androidx.core.view.ViewCompat
13+ import androidx.core.view.WindowCompat
14+ import androidx.core.view.WindowInsetsCompat
15+ import androidx.core.view.WindowInsetsControllerCompat
1016import com.omarea.common.ui.ThemeMode
11- import java.io.File
1217import com.omarea.common.ui.BlurEngine
18+ import java.io.File
1319
14- @Suppress(" DEPRECATION" )
1520object ThemeModeState {
1621
1722 private var themeMode: ThemeMode = ThemeMode ()
@@ -21,19 +26,15 @@ object ThemeModeState {
2126
2227 fun switchTheme (activity : Activity , themeLevel : Int? = null): ThemeMode {
2328 val level = themeLevel ? : ThemeConfig (activity).getThemeMode()
24- val wallpaper = WallpaperManager .getInstance(activity)
25- val wallpaperInfo = wallpaper.wallpaperInfo
26-
27- val customWallpaperFile = File (activity.filesDir, " home/etc/wallpaper.jpg" )
28- val useCustomWallpaper = customWallpaperFile.exists()
29+
30+ // Cập nhật logic xác định Night Mode hệ thống nhanh hơn
31+ val isSystemNight = (activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK ) == android.content.res.Configuration .UI_MODE_NIGHT_YES
2932
3033 when (level.coerceIn(0 , 5 )) {
3134 0 -> {
32- val nightModeFlags = activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
33- val isNight = nightModeFlags == android.content.res.Configuration .UI_MODE_NIGHT_YES
34- themeMode.isDarkMode = isNight
35+ themeMode.isDarkMode = isSystemNight
3536 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM )
36- activity.setTheme(if (isNight ) R .style.AppThemeDark else R .style.AppTheme )
37+ activity.setTheme(if (isSystemNight ) R .style.AppThemeDark else R .style.AppTheme )
3738 }
3839 1 -> {
3940 themeMode.isDarkMode = true
@@ -46,29 +47,29 @@ object ThemeModeState {
4647 activity.setTheme(R .style.AppTheme )
4748 }
4849 3 -> {
49- val nightModeFlags = activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK
50- val isNight = nightModeFlags == android.content.res.Configuration .UI_MODE_NIGHT_YES
51- themeMode.isDarkMode = isNight
50+ themeMode.isDarkMode = isSystemNight
5251 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM )
53- applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper )
52+ applyWallpaperMode(activity, isSystemNight )
5453 }
5554 4 -> {
5655 themeMode.isDarkMode = true
5756 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_YES )
58- applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper, true )
57+ applyWallpaperMode(activity, true )
5958 }
6059 5 -> {
6160 themeMode.isDarkMode = false
6261 AppCompatDelegate .setDefaultNightMode(AppCompatDelegate .MODE_NIGHT_NO )
63- applyWallpaperMode(activity, wallpaper, wallpaperInfo, useCustomWallpaper, false )
62+ applyWallpaperMode(activity, false )
6463 }
6564 }
6665
67- // CẬP NHẬT: Luôn gọi captureAndBlur khi ở chế độ Wallpaper (level >= 3)
68- // Không sử dụng check null ở đây vì BlurController đã có logic so sánh File Size bên trong
66+ // BlurEngine logic
6967 if (level >= 3 ) {
7068 BlurEngine .isPaused = false
71- BlurEngine .controller.captureAndBlur(activity)
69+ // Đảm bảo View đã layout xong trước khi capture
70+ activity.window.decorView.post {
71+ BlurEngine .controller.captureAndBlur(activity)
72+ }
7273 } else {
7374 BlurEngine .isPaused = true
7475 }
@@ -77,29 +78,23 @@ object ThemeModeState {
7778 return themeMode
7879 }
7980
80- private fun applyWallpaperMode (
81- activity : Activity ,
82- wallpaper : WallpaperManager ,
83- wallpaperInfo : android.app.WallpaperInfo ? ,
84- useCustomWallpaper : Boolean ,
85- forceDark : Boolean? = null
86- ) {
87- val night = forceDark ? : (activity.resources.configuration.uiMode and android.content.res.Configuration .UI_MODE_NIGHT_MASK == android.content.res.Configuration .UI_MODE_NIGHT_YES )
88- themeMode.isDarkMode = night
89- activity.setTheme(if (night) R .style.AppThemeWallpaper else R .style.AppThemeWallpaperLight )
81+ private fun applyWallpaperMode (activity : Activity , isNight : Boolean ) {
82+ activity.setTheme(if (isNight) R .style.AppThemeWallpaper else R .style.AppThemeWallpaperLight )
9083
84+ val wallpaper = WallpaperManager .getInstance(activity)
85+ val customWallpaperFile = File (activity.filesDir, " home/etc/wallpaper.jpg" )
86+
9187 try {
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- }
98- } else if (wallpaperInfo != null && wallpaperInfo.packageName != null ) {
88+ if (customWallpaperFile.exists()) {
89+ // Sử dụng createFromPath để tiết kiệm bộ nhớ hơn decode trực tiếp
90+ val drawable = Drawable .createFromPath(customWallpaperFile.absolutePath)
91+ activity.window.setBackgroundDrawable(drawable)
92+ } else if (wallpaper.wallpaperInfo != null ) {
93+ // Nếu là Live Wallpaper, dùng flag SHOW_WALLPAPER
9994 activity.window.addFlags(WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER )
95+ activity.window.setBackgroundDrawable(null )
10096 } else {
101- // CẬP NHẬT: Xóa cache của WallpaperManager trước khi lấy Drawable hệ thống
102- wallpaper.forgetLoadedWallpaper()
97+ // Hình nền tĩnh hệ thống
10398 activity.window.setBackgroundDrawable(wallpaper.drawable)
10499 }
105100 } catch (e: Exception ) {
@@ -109,30 +104,29 @@ object ThemeModeState {
109104
110105 private fun applyWindowFlags (activity : Activity ) {
111106 val window = activity.window
112- androidx.core.view.WindowCompat .setDecorFitsSystemWindows(window, false )
113- window.statusBarColor = android.graphics.Color .TRANSPARENT
114- window.navigationBarColor = android.graphics.Color .TRANSPARENT
107+ // Thiết lập Edge-to-Edge chuẩn SDK 23+
108+ WindowCompat .setDecorFitsSystemWindows(window, false )
109+ window.statusBarColor = Color .TRANSPARENT
110+ window.navigationBarColor = Color .TRANSPARENT
115111
116- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
117- val rootView = window.decorView.rootView
118- androidx.core.view.ViewCompat .setOnApplyWindowInsetsListener(rootView) { _, insets ->
119- val systemBars = insets.getInsets(androidx.core.view.WindowInsetsCompat .Type .systemBars())
120- activity.findViewById<View >(R .id.blur_top_container)?.setPadding(0 , systemBars.top, 0 , 0 )
121- activity.findViewById<View >(R .id.file_selector_list)?.setPadding(0 , systemBars.top, 0 , 0 )
122- activity.findViewById<View >(R .id.main_list)?.setPadding(0 , systemBars.top, 0 , 0 )
123- activity.findViewById<View >(R .id.blur_bottom_container)?.setPadding(0 , 0 , 0 , systemBars.bottom)
124- insets
125- }
112+ val rootView = window.decorView
113+ ViewCompat .setOnApplyWindowInsetsListener(rootView) { view, insets ->
114+ val systemBars = insets.getInsets(WindowInsetsCompat .Type .systemBars())
115+
116+ // Cập nhật padding cho các container nếu tồn tại
117+ activity.findViewById<View >(R .id.blur_top_container)?.setPadding(0 , systemBars.top, 0 , 0 )
118+ activity.findViewById<View >(R .id.file_selector_list)?.setPadding(0 , systemBars.top, 0 , 0 )
119+ activity.findViewById<View >(R .id.main_list)?.setPadding(0 , systemBars.top, 0 , 0 )
120+ activity.findViewById<View >(R .id.blur_bottom_container)?.setPadding(0 , 0 , 0 , systemBars.bottom)
121+
122+ insets // Trả về insets để các view khác vẫn nhận được
126123 }
127124
128- val controller = androidx.core.view.WindowInsetsControllerCompat (window, window.decorView)
129- if (! themeMode.isDarkMode) {
130- controller.isAppearanceLightStatusBars = true
131- controller.isAppearanceLightNavigationBars = true
132- } else {
133- controller.isAppearanceLightStatusBars = false
134- controller.isAppearanceLightNavigationBars = false
135- }
125+ // Điều khiển màu icon trên thanh trạng thái
126+ val controller = WindowInsetsControllerCompat (window, rootView)
127+ val useLightIcons = ! themeMode.isDarkMode
128+ controller.isAppearanceLightStatusBars = useLightIcons
129+ controller.isAppearanceLightNavigationBars = useLightIcons
136130 }
137131
138132 fun getThemeMode (): ThemeMode = themeMode
0 commit comments