|
| 1 | +package com.omarea.common.ui |
| 2 | + |
| 3 | +import android.animation.Animator |
| 4 | +import android.animation.AnimatorListenerAdapter |
| 5 | +import android.app.Activity |
| 6 | +import android.graphics.drawable.GradientDrawable |
| 7 | +import android.os.Handler |
| 8 | +import android.os.Looper |
| 9 | +import android.view.LayoutInflater |
| 10 | +import android.view.View |
| 11 | +import android.view.ViewGroup |
| 12 | +import android.widget.ImageView |
| 13 | +import android.widget.TextView |
| 14 | +import androidx.core.view.ViewCompat |
| 15 | +import androidx.core.view.WindowInsetsCompat |
| 16 | +import com.tool.tree.R |
| 17 | + |
| 18 | +enum class BannerType { INFO, SUCCESS, WARNING, ERROR } |
| 19 | + |
| 20 | +/** |
| 21 | + * Hiện 1 banner thông báo đè lên trên cùng của Activity đang foreground. |
| 22 | + * Khác với Toast: hiển thị trong nội dung ứng dụng, có thể tùy biến màu/icon/tiêu đề, |
| 23 | + * và chỉ hoạt động khi app đang mở (cần 1 Activity foreground để addView vào). |
| 24 | + * |
| 25 | + * Gọi từ shell: am broadcast -a <applicationId>.broadcast.BANNER --es text "..." --es type "success" |
| 26 | + */ |
| 27 | +object BannerNotificationManager { |
| 28 | + private val mainHandler = Handler(Looper.getMainLooper()) |
| 29 | + |
| 30 | + private data class BannerRequest( |
| 31 | + val title: String?, |
| 32 | + val message: String, |
| 33 | + val type: BannerType, |
| 34 | + val durationMs: Long |
| 35 | + ) |
| 36 | + |
| 37 | + private val queue = ArrayDeque<BannerRequest>() |
| 38 | + private var isShowing = false |
| 39 | + |
| 40 | + /** |
| 41 | + * @param onNoActivity gọi khi không có Activity nào đang foreground (app ở background), |
| 42 | + * dùng để nơi gọi có thể fallback sang Toast hoặc bỏ qua. |
| 43 | + */ |
| 44 | + fun show( |
| 45 | + title: String? = null, |
| 46 | + message: String, |
| 47 | + type: BannerType = BannerType.INFO, |
| 48 | + durationMs: Long = 3000L, |
| 49 | + onNoActivity: (() -> Unit)? = null |
| 50 | + ) { |
| 51 | + if (CurrentActivityHolder.get() == null) { |
| 52 | + onNoActivity?.invoke() |
| 53 | + return |
| 54 | + } |
| 55 | + mainHandler.post { |
| 56 | + queue.addLast(BannerRequest(title, message, type, durationMs)) |
| 57 | + if (!isShowing) showNext() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private fun showNext() { |
| 62 | + val req = queue.removeFirstOrNull() |
| 63 | + if (req == null) { |
| 64 | + isShowing = false |
| 65 | + return |
| 66 | + } |
| 67 | + val activity = CurrentActivityHolder.get() |
| 68 | + if (activity == null) { |
| 69 | + // Không còn Activity nào để hiện -> bỏ qua item này, thử item tiếp theo |
| 70 | + showNext() |
| 71 | + return |
| 72 | + } |
| 73 | + isShowing = true |
| 74 | + showOn(activity, req) |
| 75 | + } |
| 76 | + |
| 77 | + private fun showOn(activity: Activity, req: BannerRequest) { |
| 78 | + val root = activity.findViewById<ViewGroup>(android.R.id.content) |
| 79 | + val view = LayoutInflater.from(activity).inflate(R.layout.banner_notification, root, false) |
| 80 | + |
| 81 | + val bannerRoot = view.findViewById<View>(R.id.banner_root) |
| 82 | + val icon = view.findViewById<ImageView>(R.id.banner_icon) |
| 83 | + val titleView = view.findViewById<TextView>(R.id.banner_title) |
| 84 | + val messageView = view.findViewById<TextView>(R.id.banner_message) |
| 85 | + val closeButton = view.findViewById<View>(R.id.banner_close) |
| 86 | + |
| 87 | + val (colorRes, iconRes) = when (req.type) { |
| 88 | + BannerType.INFO -> R.color.banner_info to R.drawable.ic_banner_info |
| 89 | + BannerType.SUCCESS -> R.color.banner_success to R.drawable.ic_banner_success |
| 90 | + BannerType.WARNING -> R.color.banner_warning to R.drawable.ic_banner_warning |
| 91 | + BannerType.ERROR -> R.color.banner_error to R.drawable.ic_banner_error |
| 92 | + } |
| 93 | + (bannerRoot.background?.mutate() as? GradientDrawable)?.setColor( |
| 94 | + activity.resources.getColor(colorRes, activity.theme) |
| 95 | + ) |
| 96 | + icon.setImageResource(iconRes) |
| 97 | + |
| 98 | + if (!req.title.isNullOrEmpty()) { |
| 99 | + titleView.text = req.title |
| 100 | + titleView.visibility = View.VISIBLE |
| 101 | + } else { |
| 102 | + titleView.visibility = View.GONE |
| 103 | + } |
| 104 | + messageView.text = req.message |
| 105 | + |
| 106 | + // Tránh banner bị che bởi status bar / notch |
| 107 | + ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets -> |
| 108 | + val top = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top |
| 109 | + v.setPadding(v.paddingLeft, top, v.paddingRight, v.paddingBottom) |
| 110 | + insets |
| 111 | + } |
| 112 | + |
| 113 | + root.addView(view) |
| 114 | + ViewCompat.requestApplyInsets(view) |
| 115 | + |
| 116 | + var dismissed = false |
| 117 | + val dismiss = { |
| 118 | + if (!dismissed) { |
| 119 | + dismissed = true |
| 120 | + mainHandler.removeCallbacksAndMessages(view) |
| 121 | + view.animate() |
| 122 | + .translationY(-(view.height.takeIf { it > 0 } ?: 300).toFloat()) |
| 123 | + .alpha(0f) |
| 124 | + .setDuration(200) |
| 125 | + .setListener(object : AnimatorListenerAdapter() { |
| 126 | + override fun onAnimationEnd(animation: Animator) { |
| 127 | + root.removeView(view) |
| 128 | + showNext() |
| 129 | + } |
| 130 | + }) |
| 131 | + .start() |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + closeButton.setOnClickListener { dismiss() } |
| 136 | + view.setOnClickListener { dismiss() } |
| 137 | + |
| 138 | + view.alpha = 0f |
| 139 | + view.translationY = -200f |
| 140 | + view.post { |
| 141 | + view.translationY = -(view.height.takeIf { it > 0 } ?: 300).toFloat() |
| 142 | + view.animate() |
| 143 | + .translationY(0f) |
| 144 | + .alpha(1f) |
| 145 | + .setDuration(250) |
| 146 | + .start() |
| 147 | + } |
| 148 | + |
| 149 | + val runnable = Runnable { dismiss() } |
| 150 | + mainHandler.postAtTime(runnable, view, android.os.SystemClock.uptimeMillis() + req.durationMs) |
| 151 | + } |
| 152 | +} |
0 commit comments