170 lines
5.3 KiB
Kotlin
170 lines
5.3 KiB
Kotlin
package com.gh.common.notifier
|
|
|
|
import android.app.Activity
|
|
import androidx.core.view.ViewCompat
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import com.gh.common.util.SPUtils
|
|
import com.gh.gamecenter.*
|
|
import java.lang.ref.WeakReference
|
|
|
|
@Suppress("DEPRECATION")
|
|
class Notifier private constructor() {
|
|
|
|
companion object {
|
|
|
|
private var activityWeakReference: WeakReference<Activity>? = null
|
|
|
|
private const val SP_VIEWED_NOTIFIER = "viewed_notifier"
|
|
|
|
/**
|
|
* 根据内容决定是否显示 Notifier
|
|
*/
|
|
@JvmStatic
|
|
fun shouldShowNotifier(content: String): Boolean {
|
|
val viewedNotifierCollection = SPUtils.getString(SP_VIEWED_NOTIFIER)
|
|
return !viewedNotifierCollection.contains(content)
|
|
}
|
|
|
|
/**
|
|
* 标记相应内容的 Notifier 已经显示过了
|
|
*/
|
|
@JvmStatic
|
|
fun tagNotifierAsShowed(content: String) {
|
|
val viewedNotifierCollection = SPUtils.getString(SP_VIEWED_NOTIFIER)
|
|
if (viewedNotifierCollection.length > 3000) {
|
|
SPUtils.setString(SP_VIEWED_NOTIFIER, content)
|
|
} else {
|
|
SPUtils.setString(SP_VIEWED_NOTIFIER, viewedNotifierCollection + content)
|
|
}
|
|
}
|
|
|
|
@JvmStatic
|
|
fun create(activity: Activity?): Notifier {
|
|
if (activity == null) {
|
|
throw IllegalArgumentException("Activity cannot be null!")
|
|
}
|
|
|
|
val notifier = Notifier()
|
|
|
|
// Hide current NotifierView, if one is active
|
|
clearCurrent(activity)
|
|
|
|
notifier.setActivity(activity)
|
|
notifier.notifierView = NotifierView(activity)
|
|
|
|
return notifier
|
|
}
|
|
|
|
@JvmStatic
|
|
fun isActivityValid(activity: Activity?): Boolean {
|
|
if (activity == null) return false
|
|
|
|
return when (activity) {
|
|
is MessageActivity -> false
|
|
is DownloadManagerActivity -> false
|
|
is CleanApkActivity -> false
|
|
is SplashScreenActivity -> false
|
|
else -> isNotExistInActivity(activity)
|
|
}
|
|
}
|
|
|
|
@JvmStatic
|
|
fun isNotExistInActivity(activity: Activity?): Boolean {
|
|
if (activity == null) return false
|
|
|
|
(activity.window?.decorView as? ViewGroup)?.let {
|
|
//Find all NotifierView Views in Parent layout
|
|
for (i in 0..it.childCount) {
|
|
val childView = if (it.getChildAt(i) is NotifierView) it.getChildAt(i) as NotifierView else null
|
|
if (childView != null && childView.windowToken != null) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* Cleans up the currently showing notifierView view, if one is present
|
|
*
|
|
* @param activity The current Activity
|
|
*/
|
|
@JvmStatic
|
|
fun clearCurrent(activity: Activity?) {
|
|
(activity?.window?.decorView as? ViewGroup)?.let {
|
|
//Find all NotifierView Views in Parent layout
|
|
for (i in 0..it.childCount) {
|
|
val childView = if (it.getChildAt(i) is NotifierView) it.getChildAt(i) as NotifierView else null
|
|
if (childView != null && childView.windowToken != null) {
|
|
ViewCompat.animate(childView).alpha(0f).withEndAction(getRemoveViewRunnable(childView))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@JvmStatic
|
|
fun hide() {
|
|
activityWeakReference?.get()?.let { clearCurrent(it) }
|
|
}
|
|
|
|
private fun getRemoveViewRunnable(childView: NotifierView?): Runnable {
|
|
return Runnable {
|
|
childView?.let {
|
|
(childView.parent as? ViewGroup)?.removeView(childView)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private var notifierView: NotifierView? = null
|
|
|
|
private val activityDecorView: ViewGroup?
|
|
get() {
|
|
var decorView: ViewGroup? = null
|
|
|
|
activityWeakReference?.get()?.let {
|
|
decorView = it.window.decorView as ViewGroup
|
|
}
|
|
|
|
return decorView
|
|
}
|
|
|
|
fun show(showVerticalTranslateAnimation: Boolean, delay: Long? = 0): NotifierView? {
|
|
activityWeakReference?.get()?.let {
|
|
it.runOnUiThread {
|
|
activityDecorView?.postDelayed({
|
|
notifierView?.showVerticalTranslateAnimation = showVerticalTranslateAnimation
|
|
activityDecorView?.addView(notifierView)
|
|
}, delay!!)
|
|
}
|
|
}
|
|
|
|
return notifierView
|
|
}
|
|
|
|
fun setIcon(url: String?): Notifier {
|
|
url?.let { notifierView?.setIcon(it) }
|
|
return this
|
|
}
|
|
|
|
fun setText(text: String?): Notifier {
|
|
notifierView?.setText(text)
|
|
|
|
return this
|
|
}
|
|
|
|
fun setDuration(time: Long): Notifier {
|
|
notifierView?.duration = time
|
|
return this
|
|
}
|
|
|
|
fun setOnClickListener(onClickListener: View.OnClickListener): Notifier {
|
|
notifierView?.findViewById<View>(R.id.cardView)?.setOnClickListener(onClickListener)
|
|
return this
|
|
}
|
|
|
|
private fun setActivity(activity: Activity) {
|
|
activityWeakReference = WeakReference(activity)
|
|
}
|
|
} |