Skip to content

Commit

Permalink
fix: 修复广播在Android14的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Sep 9, 2023
1 parent 78c1d1f commit 108d693
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 54 deletions.
37 changes: 0 additions & 37 deletions app/src/main/java/li/songe/gkd/composition/CompositionExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ package li.songe.gkd.composition

import android.app.Activity
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.blankj.utilcode.util.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import li.songe.gkd.util.Singleton
import kotlin.coroutines.CoroutineContext

object CompositionExt {
Expand All @@ -22,37 +16,6 @@ object CompositionExt {
return scope
}

fun Context.useMessage(name: String? = null): Pair<(f: ((InvokeMessage) -> Unit)) -> Unit, (InvokeMessage) -> Unit> {
this as CanOnDestroy

var onMessage: (InvokeMessage) -> Unit = {}
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val s = intent?.extras?.getString("__invoke") ?: return
val message = Singleton.json.decodeFromString<InvokeMessage>(s)!!

if (name != null && message.name != name) {
return
}
onMessage(message)
}
}
val filter = IntentFilter(packageName)

registerReceiver(receiver, filter)
val sendMessage: (InvokeMessage) -> Unit = { message ->
sendBroadcast(Intent(packageName).apply {
putExtra("__invoke", Singleton.json.encodeToString(message))
})
}
onDestroy {
unregisterReceiver(receiver)
}
val setter: ((InvokeMessage) -> Unit) -> Unit = { onMessage = it }
return (setter to sendMessage)
}


fun Context.useLifeCycleLog() {
val simpleName = this::class.simpleName
when (this) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/li/songe/gkd/service/AbExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import android.view.accessibility.AccessibilityNodeInfo
import li.songe.selector.Transform
import li.songe.selector.Selector

val AccessibilityService.safeActiveWindow: AccessibilityNodeInfo?
get() = try {
// java.lang.SecurityException: Call from user 0 as user -2 without permission INTERACT_ACROSS_USERS or INTERACT_ACROSS_USERS_FULL not allowed.
rootInActiveWindow
} catch (e: Exception) {
e.printStackTrace()
null
}


fun AccessibilityNodeInfo.getIndex(): Int {
parent?.forEachIndexed { index, accessibilityNodeInfo ->
if (accessibilityNodeInfo == this) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GkdAbService : CompositionAbService({
onInterrupt { serviceConnected = false }

onAccessibilityEvent { event -> // 根据事件获取 activityId, 概率不准确
val appId = rootInActiveWindow?.packageName?.toString() ?: return@onAccessibilityEvent
val appId = safeActiveWindow?.packageName?.toString() ?: return@onAccessibilityEvent
appIdFlow.value = appId
when (event?.eventType) {
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, AccessibilityEvent.TYPE_WINDOWS_CHANGED -> {
Expand Down Expand Up @@ -133,7 +133,7 @@ class GkdAbService : CompositionAbService({
val currentRules = currentRulesFlow.value
for (rule in currentRules) {
if (!isAvailableRule(rule)) continue
val target = rule.query(rootInActiveWindow)
val target = rule.query(safeActiveWindow)
val clickResult = target?.click(context)
if (clickResult != null) {
if (storeFlow.value.toastWhenClick) {
Expand Down Expand Up @@ -248,7 +248,7 @@ class GkdAbService : CompositionAbService({

val currentAbNode: AccessibilityNodeInfo?
get() {
return service?.rootInActiveWindow
return service?.safeActiveWindow
}

suspend fun currentScreenshot() = service?.run {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/li/songe/gkd/ui/SettingsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package li.songe.gkd.ui

import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -112,6 +113,9 @@ fun SettingsPage() {
toastWhenClick = it
)
)
if (!Settings.canDrawOverlays(context)) {
ToastUtils.showShort("需要悬浮窗权限")
}
})
Divider()

Expand Down Expand Up @@ -200,7 +204,8 @@ fun SettingsPage() {
})
.padding(horizontal = 16.dp)
) {
RadioButton(selected = (option.second == store.updateSubsInterval),
RadioButton(
selected = (option.second == store.updateSubsInterval),
onClick = {
updateStorage(
storeFlow,
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/li/songe/gkd/ui/component/SubsItemCard.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package li.songe.gkd.ui.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -9,7 +8,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Surface
import androidx.compose.material.Switch
import androidx.compose.material.Text
Expand All @@ -19,13 +17,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import li.songe.gkd.data.SubsItem
import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.util.SafeR
import li.songe.gkd.util.formatTimeAgo

@Composable
Expand Down Expand Up @@ -54,7 +50,7 @@ fun SubsItemCard(
}
Row {
Text(
text = formatTimeAgo(subsItem.mtime) + "更新",
text = formatTimeAgo(subsItem.mtime),
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis
Expand Down
25 changes: 19 additions & 6 deletions app/src/main/java/li/songe/gkd/util/AppInfoState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Build
import com.blankj.utilcode.util.AppUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -32,12 +33,24 @@ private val packageReceiver by lazy {
}
}
}.apply {
app.registerReceiver(this, IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REPLACED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
})
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
app.registerReceiver(this, IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REPLACED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
}, Context.RECEIVER_NOT_EXPORTED)
} else {
app.registerReceiver(
this,
IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REPLACED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addDataScheme("package")
},
)
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/li/songe/gkd/util/Multiprocess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ import com.blankj.utilcode.util.ProcessUtils


val isMainProcess by lazy { ProcessUtils.isMainProcess() }

val currentProcessName by lazy { ProcessUtils.getCurrentProcessName() }

0 comments on commit 108d693

Please sign in to comment.