Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:修复全局浮窗对activity的依赖 #193

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@

```groovy
dependencies {
implementation 'io.github.petterpx:floatingx:2.3.2'
implementation 'io.github.petterpx:floatingx:2.3.3'

// system浮窗&&compose时需要导入
// 记得AppHelper里调用 enableComposeSupport()
implementation 'io.github.petterpx:floatingx-compose:2.3.2'
implementation 'io.github.petterpx:floatingx-compose:2.3.3'
}
```

Expand Down
6 changes: 3 additions & 3 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@

```groovy
dependencies {
implementation 'io.github.petterpx:floatingx:2.3.2'
implementation 'io.github.petterpx:floatingx:2.3.3'

// System floating window && need to be imported when compose
// System floating window && 使用 Compose 时需要导入
// AppHelper invoke enableComposeSupport()
implementation 'io.github.petterpx:floatingx-compose:2.3.2'
implementation 'io.github.petterpx:floatingx-compose:2.3.3'
}
```

Expand Down
25 changes: 24 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application
android:name=".kotlin.CustomKtApplication"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -42,9 +45,29 @@
<activity
android:name=".test.SimpleRvActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true" >
android:exported="true">

</activity>

<receiver
android:name=".service.LauncherReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>

<service
android:name=".service.LauncherService"
android:enabled="true"
android:exported="true"
android:process="com.test.service">
<intent-filter>
<action android:name="com.test.Service" />
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.petterp.floatingx.app.service

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

/**
*
* @author petterp
*/
class LauncherReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == ACTION_BOOT) {
val intent = Intent(context, LauncherService::class.java)
context.startService(intent)
}
}

companion object {
private const val ACTION_BOOT: String = "android.intent.action.BOOT_COMPLETED"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.petterp.floatingx.app.service

import android.app.Service
import android.content.Intent
import android.os.IBinder

/**
*
* @author petterp
*/
import com.petterp.floatingx.app.kotlin.FxSystemSimple

class LauncherService : Service() {

override fun onCreate() {
super.onCreate()
FxSystemSimple.install(application)
}

override fun onBind(intent: Intent?): IBinder? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ abstract class FxBasisControlImp<F : FxBasisHelper, P : IFxPlatformProvider<F>>(

override fun show() {
if (isShow()) return
if (!updateEnableStatusAndCheckCanShow()) return
helper.enableFx = true
if (!platformProvider.checkOrInit()) return
// FIXME: 这里有可能会触发多次show
val fxView = getManagerView() ?: return
platformProvider.show()
helper.fxLog.d("fxView -> showFx")
Expand All @@ -52,16 +54,6 @@ abstract class FxBasisControlImp<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
}
}

private fun updateEnableStatusAndCheckCanShow(): Boolean {
val oldStatus = helper.enableFx
// pre update status
helper.enableFx = true
val canShow = platformProvider.checkOrInit()
// If it was not open before, it can be safely updated to the latest status
if (!oldStatus) helper.enableFx = canShow
return canShow
}

override fun hide() {
if (!isShow()) return
helper.enableFx = false
Expand All @@ -88,12 +80,7 @@ abstract class FxBasisControlImp<F : FxBasisHelper, P : IFxPlatformProvider<F>>(
}
}

override fun isShow(): Boolean {
val interView = getManagerView() ?: return false
val isShow = platformProvider.isShow()
if (isShow != null) return isShow
return interView.isAttachedToWindow && interView.visibility == View.VISIBLE
}
override fun isShow() = platformProvider.isShow() == true

override fun updateView(@LayoutRes resource: Int) {
check(resource != INVALID_LAYOUT_ID) { "resource cannot be INVALID_LAYOUT_ID!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class FxSystemPlatformProvider(
override val internalView: FxSystemContainerView?
get() = _internalView

init {
checkRegisterAppLifecycle()
}

override fun show() {
val internalView = _internalView ?: return
internalView.registerWM(wm ?: return)
Expand All @@ -57,24 +53,32 @@ class FxSystemPlatformProvider(
}

override fun checkOrInit(): Boolean {
checkRegisterAppLifecycle()
// topActivity==null,依然返回true,因为在某些情况下,可能会在Activity未创建时,就调用show
val act = topActivity ?: return true
// 禁止安装浮窗时,直接返回false
if (!helper.isCanInstall(act)) {
if (_internalView != null) return true
checkOrRegisterActivityLifecycle()

// topAct不为null,进行黑名单判断
val act = topActivity
if (act != null && !helper.isCanInstall(act)) {
helper.fxLog.d("fx not show,This [${act.javaClass.simpleName}] is not in the list of allowed inserts!")
return false
}
if (_internalView == null) {
if (!checkAgreePermission(act)) {
internalAskAutoPermission(act)
return false
}

val hasPermission = checkAgreePermission(helper.context)
if (hasPermission) {
wm = helper.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
_internalView = FxSystemContainerView(helper, wm!!, context)
_internalView!!.initView()
} else {
internalAskAutoPermission(act ?: return false)
}
return hasPermission
}

private fun checkOrRegisterActivityLifecycle() {
if (_lifecycleImp == null) {
_lifecycleImp = FxSystemLifecycleImp(helper, this)
helper.context.registerActivityLifecycleCallbacks(_lifecycleImp)
}
return true
}

override fun requestPermission(
Expand Down Expand Up @@ -133,12 +137,6 @@ class FxSystemPlatformProvider(
}
}

private fun checkRegisterAppLifecycle() {
if (!helper.enableFx || _lifecycleImp != null) return
_lifecycleImp = FxSystemLifecycleImp(helper, this)
helper.context.registerActivityLifecycleCallbacks(_lifecycleImp)
}

internal fun internalAskAutoPermission(activity: Activity) {
// 有权限,则跳过
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || checkAgreePermission(activity)) {
Expand All @@ -153,9 +151,9 @@ class FxSystemPlatformProvider(
requestPermission(activity, true, helper.scope == FxScopeType.SYSTEM_AUTO)
}

private fun checkAgreePermission(activity: Activity): Boolean {
private fun checkAgreePermission(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Settings.canDrawOverlays(activity)
return Settings.canDrawOverlays(context)
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class FxViewLocationHelper : FxViewBasicHelper(), View.OnLayoutChangeListener {
val (pW, pH) = view.parentSize() ?: return
val viewH = view.height.toFloat()
val viewW = view.width.toFloat()
// 如果大小没有变化,则不更新
if (this.parentW == pW.toFloat() && this.parentH == pH.toFloat()
&& this.viewW == viewW && this.viewH == viewH
) return
this.parentW = pW.toFloat()
this.parentH = pH.toFloat()
this.viewW = viewW
Expand Down
Loading