Skip to content

Commit

Permalink
perf:黑名单支持传递类名
Browse files Browse the repository at this point in the history
  • Loading branch information
Petterpx committed Aug 27, 2024
1 parent cd907e5 commit a4313e8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ object FxSystemSimple {
setEnableAllInstall(true)
// 2.禁止插入Activity的页面, setEnableAllBlackClass(true)时,此方法生效
addInstallBlackClass(BlackActivity::class.java)
addInstallBlackClass(ScopeActivity::class.java.name)
// 3.允许插入Activity的页面, setEnableAllBlackClass(false)时,此方法生效
addInstallWhiteClass(
MainActivity::class.java,
ImmersedActivity::class.java,
ScopeActivity::class.java,
)
// 设置点击事件
setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.petterp.floatingx.assist.helper
import android.app.Activity
import android.app.Application
import android.content.Context
import android.view.View
import androidx.annotation.IdRes
import com.petterp.floatingx.assist.FxScopeType
import com.petterp.floatingx.listener.IFxPermissionInterceptor
Expand All @@ -23,10 +22,10 @@ class FxAppHelper(
internal val context: Application,
/** 黑名单list */
@JvmSynthetic
internal val blackFilterList: MutableList<Class<*>>,
internal val blackFilterList: MutableList<String>,
/** 白名单list */
@JvmSynthetic
internal val whiteInsertList: MutableList<Class<*>>,
internal val whiteInsertList: MutableList<String>,
/** 是否允许插入全部Activity */
@JvmSynthetic
internal val isAllInstall: Boolean,
Expand All @@ -44,6 +43,7 @@ class FxAppHelper(
@JvmSynthetic
internal val fxAskPermissionInterceptor: IFxPermissionInterceptor?,
) : FxBasisHelper() {
private val insertCls = mutableMapOf<Class<*>, Boolean>()

@JvmSynthetic
internal fun updateNavigationBar(activity: Activity?) {
Expand All @@ -59,13 +59,14 @@ class FxAppHelper(

@JvmSynthetic
internal fun isCanInstall(act: Activity): Boolean {
return isCanInstall(act.javaClass)
}

@JvmSynthetic
internal fun isCanInstall(cls: Class<*>): Boolean {
return (isAllInstall && !blackFilterList.contains(cls)) ||
(!isAllInstall && whiteInsertList.contains(cls))
val cls = act.javaClass
return insertCls[cls] ?: let {
val name = cls.name
val canInstall = (isAllInstall && !blackFilterList.contains(name)) ||
(!isAllInstall && whiteInsertList.contains(name))
insertCls[cls] = canInstall
canInstall
}
}

class Builder : FxBasisHelper.Builder<Builder, FxAppHelper>() {
Expand All @@ -78,8 +79,8 @@ class FxAppHelper(
private var scopeEnum: FxScopeType = FxScopeType.APP
private var fxLifecycleExpand: IFxProxyTagActivityLifecycle? = null
private var askPermissionInterceptor: IFxPermissionInterceptor? = null
private var whiteInsertList: MutableList<Class<*>> = mutableListOf()
private var blackFilterList: MutableList<Class<*>> = mutableListOf()
private var whiteInsertList: MutableList<String> = mutableListOf()
private var blackFilterList: MutableList<String> = mutableListOf()

/**
* 设置context
Expand Down Expand Up @@ -110,17 +111,25 @@ class FxAppHelper(
/**
* 添加禁止显示悬浮窗的activity
*
* @param c 禁止显示的activity
* @param actNames 禁止显示的activity
* @sample [xxxActivity::class.java.name]
*
* [setEnableAllBlackClass(true)] 时,此方法生效
*/
fun addInstallBlackClass(vararg c: Class<out Activity>): Builder {
blackFilterList.addAll(c)
fun addInstallBlackClass(vararg actNames: String): Builder {
blackFilterList.addAll(actNames)
return this
}

fun addInstallBlackClass(vararg cls: Class<out Activity>): Builder {
val names = cls.map { it.name }
blackFilterList.addAll(names)
return this
}

fun addInstallBlackClass(cls: List<Class<out Activity>>): Builder {
blackFilterList.addAll(cls)
val names = cls.map { it.name }
blackFilterList.addAll(names)
return this
}

Expand Down Expand Up @@ -163,17 +172,25 @@ class FxAppHelper(
/**
* 允许显示浮窗的activity
*
* @param c 允许显示的activity
* @param actNames 允许显示的activity路径
* @sample [xxxActivity::class.java.name]
*
* [setEnableAllBlackClass(false)] 时,此方法生效
*/
fun addInstallWhiteClass(vararg c: Class<out Activity>): Builder {
whiteInsertList.addAll(c)
fun addInstallWhiteClass(vararg actNames: String): Builder {
whiteInsertList.addAll(actNames)
return this
}

fun addInstallWhiteClass(vararg cls: Class<out Activity>): Builder {
val names = cls.map { it.name }
whiteInsertList.addAll(names)
return this
}

fun addInstallWhiteClass(cls: List<Class<out Activity>>): Builder {
whiteInsertList.addAll(cls)
val names = cls.map { it.name }
whiteInsertList.addAll(names)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class FxAppLifecycleImp(
get() = appControl.getManagerView()?.parent === decorView

private val Activity.isActivityInValid: Boolean
get() {
val cls = this.javaClass
return insertCls[cls] ?: isInsertActivity(cls)
}
get() = helper.isCanInstall(this)

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
if (!enableFx) return
Expand Down Expand Up @@ -105,12 +102,4 @@ class FxAppLifecycleImp(
if (activity.isActivityInValid) it.onSaveInstanceState(activity, outState)
}
}

private fun isInsertActivity(cls: Class<*>): Boolean =
helper.let {
// 条件 允许全局安装&&不在黑名单 || 禁止全局安装&&在白名单
val isInsert = it.isCanInstall(cls)
insertCls[cls] = isInsert
isInsert
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ class FxSystemLifecycleImp(
get() = javaClass.name.split(".").last()

private val Activity.isActivityInValid: Boolean
get() {
val cls = this.javaClass
return insertCls[cls] ?: isInsertActivity(cls)
}
get() = helper.isCanInstall(this)

init {
isNeedAskPermission = helper.enableFx
Expand Down Expand Up @@ -95,11 +92,4 @@ class FxSystemLifecycleImp(

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

private fun isInsertActivity(cls: Class<*>): Boolean =
helper.let {
// 条件 允许全局安装&&不在黑名单 || 禁止全局安装&&在白名单
val isInsert = it.isCanInstall(cls)
insertCls[cls] = isInsert
isInsert
}
}

0 comments on commit a4313e8

Please sign in to comment.