Skip to content

Commit d40956a

Browse files
committed
Update
1 parent a57a4f7 commit d40956a

2 files changed

Lines changed: 33 additions & 39 deletions

File tree

app/src/main/java/com/omarea/common/ui/DialogHelper.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,21 @@ class DialogHelper {
380380
if (context is Activity) {
381381
dialog.show()
382382
dialog.window?.run {
383-
setWindowBlurBg(this, context)
383+
val wasPausedOriginal = BlurEngine.isPaused
384+
if (!wasPausedOriginal) {
385+
BlurEngine.isPaused = true
386+
}
387+
388+
try {
389+
setWindowBlurBg(this, context)
390+
} finally {
391+
if (!wasPausedOriginal) {
392+
BlurEngine.isPaused = false
393+
}
394+
}
395+
384396
decorView.run {
385-
systemUiVisibility = context.window.decorView.systemUiVisibility // View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
397+
systemUiVisibility = context.window.decorView.systemUiVisibility
386398
}
387399
}
388400
} else {
@@ -395,22 +407,28 @@ class DialogHelper {
395407
}
396408
}
397409

398-
return setOutsideTouchDismiss(view, DialogWrap(dialog).setCancelable(cancelable))
410+
val dialogWrap = DialogWrap(dialog).setCancelable(cancelable)
411+
dialog.setOnDismissListener {
412+
BlurEngine.isPaused = false
413+
}
414+
415+
return setOutsideTouchDismiss(view, dialogWrap)
399416
}
400417

401418
private fun isNightMode(context: Context): Boolean {
402419
return ThemeModeState.isDarkMode()
403420
}
404421

405-
// Trong setWindowBlurBg
406422
fun setWindowBlurBg(window: Window, activity: Activity) {
407423
val wallpaperMode = activity.window.attributes.flags and WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER != 0
408424
window.run {
409425
val blurBitmap = if (disableBlurBg) {
410426
null
411427
} else {
428+
// Gọi hàm này, Java Utility sẽ tự xử lý ảnh mờ
412429
FastBlurUtility.getBlurBackgroundDrawer(activity)
413430
}
431+
414432
if (blurBitmap != null) {
415433
setBackgroundDrawable(blurBitmap.toDrawable(activity.resources))
416434
} else {

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

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,16 @@ public class FastBlurUtility {
1616
private static final int BLUR_RADIUS = 8;
1717

1818
/**
19-
* Chụp màn hình và làm mờ (Đã tối ưu hóa quản lý Engine)
20-
* Quy trình: Lưu trạng thái -> Tạm dừng Engine -> Xử lý ảnh -> Khôi phục trạng thái cũ
19+
* Chụp màn hình và làm mờ (Dùng làm phương án dự phòng khi không lấy được Wallpaper)
2120
*/
2221
public static Bitmap getBlurBackgroundDrawer(Activity activity) {
23-
// 1. Kiểm tra và lưu lại trạng thái gốc của Engine
24-
// Chỉ can thiệp nếu Engine hiện tại đang KHÔNG ở trạng thái tạm dừng
25-
boolean wasPausedOriginal = BlurEngine.isPaused;
26-
27-
if (!wasPausedOriginal) {
28-
BlurEngine.isPaused = true;
29-
}
30-
31-
try {
32-
// 2. Thực hiện quy trình chụp màn hình
33-
Bitmap bmp = takeScreenShot(activity);
34-
if (bmp == null) {
35-
return null;
36-
}
37-
38-
// 3. Thực hiện quy trình làm mờ và nhuộm tối (Dim)
39-
return startBlurBackground(bmp);
40-
41-
} catch (Exception e) {
42-
// Tránh crash ứng dụng nếu có lỗi xử lý đồ họa hoặc bộ nhớ
43-
e.printStackTrace();
44-
return null;
45-
} finally {
46-
// 4. KHÔI PHỤC TRẠNG THÁI:
47-
// Nếu ban đầu Engine đang chạy (!wasPausedOriginal), thì giờ ta bật lại.
48-
// Nếu ban đầu nó đã tắt sẵn (do Theme Level < 3), thì ta giữ nguyên trạng thái tắt đó.
49-
if (!wasPausedOriginal) {
50-
BlurEngine.isPaused = false;
51-
}
52-
}
22+
Bitmap bmp = takeScreenShot(activity);
23+
return startBlurBackground(bmp);
5324
}
5425

5526
/**
5627
* Quy trình xử lý: Thu nhỏ -> Làm mờ -> Phóng to & Nhuộm tối (Dim)
28+
* Đảm bảo mượt mà từ SDK 23 trở lên.
5729
*/
5830
public static Bitmap startBlurBackground(Bitmap bkg) {
5931
if (bkg == null || bkg.isRecycled()) return null;
@@ -75,7 +47,7 @@ public static Bitmap startBlurBackground(Bitmap bkg) {
7547
}
7648

7749
/**
78-
* Chụp ảnh màn hình an toàn
50+
* Chụp ảnh màn hình an toàn trên SDK 23+
7951
*/
8052
private static Bitmap takeScreenShot(Activity activity) {
8153
try {
@@ -98,9 +70,10 @@ private static Bitmap scaleAndDim(Bitmap bitmap, int targetW, int targetH) {
9870
Bitmap output = Bitmap.createBitmap(targetW, targetH, Bitmap.Config.ARGB_8888);
9971
Canvas canvas = new Canvas(output);
10072

73+
// Paint với bộ lọc chống răng cưa và lọc bitmap khi scale
10174
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
10275

103-
// Tạo bộ lọc màu để giảm độ sáng (contrast 0.80f ~ giảm 20% độ sáng)
76+
// Tạo bộ lọc màu để giảm độ sáng (contrast 0.85f ~ giảm 15% độ sáng)
10477
ColorMatrix cm = new ColorMatrix();
10578
float contrast = 0.80f;
10679
cm.set(new float[]{
@@ -110,10 +83,12 @@ private static Bitmap scaleAndDim(Bitmap bitmap, int targetW, int targetH) {
11083
0, 0, 0, 1, 0});
11184
paint.setColorFilter(new ColorMatrixColorFilter(cm));
11285

86+
// Vẽ ảnh từ vùng nguồn (nhỏ) ra vùng đích (toàn màn hình)
11387
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
11488
Rect dst = new Rect(0, 0, targetW, targetH);
11589
canvas.drawBitmap(bitmap, src, dst, paint);
11690

91+
// Giải phóng bitmap tạm sau khi đã vẽ xong
11792
if (bitmap != null && !bitmap.isRecycled()) {
11893
bitmap.recycle();
11994
}
@@ -122,7 +97,8 @@ private static Bitmap scaleAndDim(Bitmap bitmap, int targetW, int targetH) {
12297
}
12398

12499
/**
125-
* Thuật toán StackBlur - Tối ưu cho hiệu năng CPU
100+
* Thuật toán StackBlur (Multi-pass box blur) - Tối ưu cho hiệu năng CPU
101+
* Hỗ trợ hoàn hảo cho các thiết bị từ cũ đến mới.
126102
*/
127103
private static Bitmap fastBlur(Bitmap sentBitmap, int radius) {
128104
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

0 commit comments

Comments
 (0)