@@ -402,39 +402,51 @@ class DialogHelper {
402402 return ThemeModeState .isDarkMode()
403403 }
404404
405+ // Trong setWindowBlurBg
405406 fun setWindowBlurBg (window : Window , activity : Activity ) {
406407 val wallpaperMode = activity.window.attributes.flags and WindowManager .LayoutParams .FLAG_SHOW_WALLPAPER != 0
407- window.run {
408- val blurBitmap = if (disableBlurBg) {
409- null
410- } else {
411- FastBlurUtility .getBlurBackgroundDrawer(activity)
408+
409+ if (disableBlurBg) {
410+ window.applyDefaultBackground(activity, wallpaperMode)
411+ return
412+ }
413+
414+ FastBlurUtility .getBlurBackgroundAsync(activity) { blurBitmap ->
415+ // An toàn: Không làm việc với activity đã đóng
416+ if (activity.isFinishing || activity.isDestroyed) {
417+ blurBitmap?.recycle()
418+ return @getBlurBackgroundAsync
412419 }
420+
413421 if (blurBitmap != null ) {
414- setBackgroundDrawable(blurBitmap.toDrawable (activity.resources))
422+ window. setBackgroundDrawable(BitmapDrawable (activity.resources, blurBitmap ))
415423 } else {
416- try {
417- val bg = getWindowBackground(activity)
418- if (bg == Color .TRANSPARENT ) {
419- if (isFloating) {
420- setBackgroundDrawable(bg.toDrawable())
421- setDimAmount(0.5f )
422- return
423- } else {
424- val d = if (wallpaperMode || isNightMode(context)) {
425- Color .argb(255 , 18 , 18 , 18 ).toDrawable()
426- } else {
427- Color .argb(255 , 245 , 245 , 245 ).toDrawable()
428- }
429- setBackgroundDrawable(d)
430- }
424+ window.applyDefaultBackground(activity, wallpaperMode)
425+ }
426+ }
427+ }
428+
429+ // Tách logic nền mặc định ra để tái sử dụng
430+ private fun Window.applyDefaultBackground (activity : Activity , wallpaperMode : Boolean ) {
431+ try {
432+ val bg = getWindowBackground(activity)
433+ if (bg == Color .TRANSPARENT ) {
434+ if (isFloating) {
435+ setBackgroundDrawable(bg.toDrawable())
436+ setDimAmount(0.5f )
437+ } else {
438+ val d = if (wallpaperMode || isNightMode(context)) {
439+ Color .argb(255 , 18 , 18 , 18 ).toDrawable()
431440 } else {
432- setBackgroundDrawable(bg .toDrawable() )
441+ Color .argb( 255 , 245 , 245 , 245 ) .toDrawable()
433442 }
434- } catch (_: Exception ) {
435- setBackgroundDrawable(Color .TRANSPARENT .toDrawable())
443+ setBackgroundDrawable(d)
436444 }
445+ } else {
446+ setBackgroundDrawable(bg.toDrawable())
437447 }
448+ } catch (_: Exception ) {
449+ setBackgroundDrawable(Color .TRANSPARENT .toDrawable())
438450 }
439451 }
440452 }
0 commit comments