Skip to content

Commit ab31295

Browse files
committed
Update
1 parent 0904e1a commit ab31295

File tree

2 files changed

+35
-53
lines changed

2 files changed

+35
-53
lines changed

app/src/main/java/com/omarea/common/ui/BlurController.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212

1313
public class BlurController {
1414

15-
// Lưu trữ trạng thái file để so sánh ở lần chạy kế tiếp
15+
// Lưu trữ thông tin file để so sánh
1616
private static long lastFileLength = -1;
1717
private static long lastFileModified = -1;
1818

19-
/**
20-
* Chụp và làm mờ ảnh nền.
21-
*/
2219
public void captureAndBlur(Activity activity) {
2320
final WeakReference<Activity> activityRef = new WeakReference<>(activity);
2421

@@ -29,33 +26,35 @@ public void captureAndBlur(Activity activity) {
2926
Bitmap source = null;
3027
Context context = act.getApplicationContext();
3128

32-
// 1. Kiểm tra Wallpaper tùy chỉnh trong internal storage
29+
// 1. Kiểm tra Wallpaper tùy chỉnh
3330
File customWallpaperFile = new File(act.getFilesDir(), "home/etc/wallpaper.jpg");
3431

3532
if (customWallpaperFile.exists()) {
3633
long currentLength = customWallpaperFile.length();
3734
long currentModified = customWallpaperFile.lastModified();
3835

39-
// KIỂM TRA THAY ĐỔI: Nếu size và thời gian giống hệt thì bỏ qua không xử lý lại
36+
// Nếu file giống hệt lần trước thì thoát, tránh xử lý thừa
4037
if (currentLength == lastFileLength && currentModified == lastFileModified) {
41-
return;
38+
// Nếu đã có ảnh blur rồi thì không cần làm lại
39+
if (BlurEngine.blurBitmap != null && !BlurEngine.blurBitmap.isRecycled()) {
40+
return;
41+
}
4242
}
4343

44-
// Cập nhật dấu vết file mới
44+
// Cập nhật thông tin file mới
4545
lastFileLength = currentLength;
4646
lastFileModified = currentModified;
47-
4847
source = BitmapFactory.decodeFile(customWallpaperFile.getAbsolutePath());
4948
} else {
50-
// Nếu file custom không tồn tại, reset dấu vết để có thể nhận diện lại nếu file xuất hiện sau này
49+
// Reset nếu không dùng file custom
5150
lastFileLength = -1;
5251
lastFileModified = -1;
5352
}
5453

55-
// 2. Kế tiếp: Lấy trực tiếp Wallpaper hệ thống nếu không có file custom
54+
// 2. Lấy Wallpaper hệ thống nếu không có file hoặc file lỗi
5655
if (source == null) {
5756
WallpaperManager wm = WallpaperManager.getInstance(context);
58-
// Xóa cache cũ của WallpaperManager để đảm bảo lấy ảnh mới nhất nếu hệ thống vừa đổi
57+
// Ép hệ thống xóa cache để lấy ảnh mới nhất (vừa đổi ngoài launcher)
5958
wm.forgetLoadedWallpaper();
6059

6160
if (wm.getWallpaperInfo() == null) {
@@ -66,20 +65,19 @@ public void captureAndBlur(Activity activity) {
6665
}
6766
}
6867

69-
// Xử lý làm mờ
68+
// Xử lý Blur
7069
Bitmap blurredResult;
7170
if (source != null) {
7271
blurredResult = FastBlurUtility.startBlurBackground(source);
7372
} else {
74-
// Fallback: Chụp màn hình (Lưu ý: Fallback này có thể không cần check file size)
7573
blurredResult = FastBlurUtility.getBlurBackgroundDrawer(act);
7674
}
7775

7876
if (blurredResult != null) {
7977
BlurEngine.blurBitmap = blurredResult;
8078
BlurEngine.isPaused = false;
8179

82-
// Cập nhật UI
80+
// Cập nhật UI ngay lập tức
8381
act.runOnUiThread(() -> {
8482
if (act != null && !act.isFinishing() && act.getWindow() != null) {
8583
act.getWindow().getDecorView().invalidate();

app/src/main/java/com/tool/tree/ThemeModeState.kt

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)