Skip to content

Commit

Permalink
feat: writeSecureSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Sep 7, 2024
1 parent 03334ed commit aad9cb2
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 63 deletions.
17 changes: 15 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />

Expand All @@ -19,15 +20,16 @@
<!-- 国产ROM-获取应用列表权限 -->
<uses-permission android:name="com.android.permission.GET_INSTALLED_APPS" />

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

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

<!-- save image to album, save file to Downloads -->
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />

<uses-permission
android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions" />

<application
android:name="li.songe.gkd.App"
Expand Down Expand Up @@ -154,6 +156,17 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
<service
android:name=".service.GkdTileService"
android:exported="true"
android:icon="@drawable/ic_status"
android:label="@string/app_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">

<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<service
android:name=".debug.SnapshotActionService"
Expand Down
44 changes: 40 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ package li.songe.gkd

import android.app.Activity
import android.app.Application
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import android.database.ContentObserver
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.text.TextUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.hjq.toast.Toaster
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.MutableStateFlow
import li.songe.gkd.debug.clearHttpSubs
import li.songe.gkd.notif.initChannel
import li.songe.gkd.service.GkdAbService
import li.songe.gkd.util.GIT_COMMIT_URL
import li.songe.gkd.util.initAppState
import li.songe.gkd.util.initFolder
Expand Down Expand Up @@ -73,7 +79,7 @@ class App : Application() {
"VERSION_NAME: ${BuildConfig.VERSION_NAME}",
"CHANNEL: $channel"
)

initFolder()
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
LogUtils.d("onActivityCreated", activity, savedInstanceState)
Expand Down Expand Up @@ -103,8 +109,16 @@ class App : Application() {
LogUtils.d("onActivityDestroyed", activity)
}
})

initFolder()
app.contentResolver.registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES),
false,
object : ContentObserver(null) {
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
a11yServiceEnabledFlow.value = getA11yServiceEnabled()
}
}
)
appScope.launchTry(Dispatchers.IO) {
initStore()
initAppState()
Expand All @@ -113,4 +127,26 @@ class App : Application() {
clearHttpSubs()
}
}
}
}

val a11yServiceEnabledFlow by lazy { MutableStateFlow(getA11yServiceEnabled()) }
private fun getA11yServiceEnabled(): Boolean {
val value = try {
Settings.Secure.getString(
app.contentResolver,
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
)
} catch (_: Exception) {
null
}
if (value.isNullOrEmpty()) return false
val colonSplitter = TextUtils.SimpleStringSplitter(':')
colonSplitter.setString(value)
val name = ComponentName(app, GkdAbService::class.java)
while (colonSplitter.hasNext()) {
if (ComponentName.unflattenFromString(colonSplitter.next()) == name) {
return true
}
}
return false
}
2 changes: 2 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import li.songe.gkd.permission.AuthDialog
import li.songe.gkd.permission.updatePermissionState
import li.songe.gkd.service.GkdAbService
import li.songe.gkd.service.ManageService
import li.songe.gkd.service.fixRestartService
import li.songe.gkd.service.updateLauncherAppId
import li.songe.gkd.ui.NavGraphs
import li.songe.gkd.ui.component.BuildDialog
Expand Down Expand Up @@ -159,6 +160,7 @@ private fun updateServiceRunning() {
FloatingService.isRunning.value = ServiceUtils.isServiceRunning(FloatingService::class.java)
ScreenshotService.isRunning.value = ServiceUtils.isServiceRunning(ScreenshotService::class.java)
HttpService.isRunning.value = ServiceUtils.isServiceRunning(HttpService::class.java)
fixRestartService()
}

private fun Activity.fixTopPadding() {
Expand Down
27 changes: 20 additions & 7 deletions app/src/main/kotlin/li/songe/gkd/permission/PermissionState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.app
import li.songe.gkd.appScope
import li.songe.gkd.service.fixRestartService
import li.songe.gkd.shizuku.newActivityTaskManager
import li.songe.gkd.shizuku.safeGetTasks
import li.songe.gkd.shizuku.shizukuIsSafeOK
Expand All @@ -24,15 +25,15 @@ import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class PermissionState(
val check: suspend () -> Boolean,
val check: () -> Boolean,
val request: (suspend (context: Activity) -> PermissionResult)? = null,
/**
* show it when user doNotAskAgain
*/
val reason: AuthReason? = null,
) {
val stateFlow = MutableStateFlow(false)
suspend fun updateAndGet(): Boolean {
fun updateAndGet(): Boolean {
return stateFlow.updateAndGet { check() }
}
}
Expand Down Expand Up @@ -74,7 +75,7 @@ private suspend fun asyncRequestPermission(
val notificationState by lazy {
PermissionState(
check = {
checkSelfPermission(Permission.POST_NOTIFICATIONS)
XXPermissions.isGranted(app, Permission.NOTIFICATION_SERVICE)
},
request = {
asyncRequestPermission(it, Permission.POST_NOTIFICATIONS)
Expand Down Expand Up @@ -121,7 +122,7 @@ val canDrawOverlaysState by lazy {
reason = AuthReason(
text = "当前操作需要[悬浮窗权限]\n\n您需要前往应用权限设置打开此权限",
confirm = {
XXPermissions.startPermissionActivity(app, Permission.SYSTEM_ALERT_WINDOW)
XXPermissions.startPermissionActivity(app, Manifest.permission.SYSTEM_ALERT_WINDOW)
}
),
)
Expand All @@ -138,20 +139,29 @@ val canWriteExternalStorage by lazy {
},
request = {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
asyncRequestPermission(it, Permission.WRITE_EXTERNAL_STORAGE)
asyncRequestPermission(it, Manifest.permission.WRITE_EXTERNAL_STORAGE)
} else {
PermissionResult.Granted
}
},
reason = AuthReason(
text = "当前操作需要[写入外部存储权限]\n\n您需要前往应用权限设置打开此权限",
confirm = {
XXPermissions.startPermissionActivity(app, Permission.WRITE_EXTERNAL_STORAGE)
XXPermissions.startPermissionActivity(
app,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
}
),
)
}

val writeSecureSettingsState by lazy {
PermissionState(
check = { checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS) },
)
}

val shizukuOkState by lazy {
PermissionState(
check = {
Expand All @@ -173,12 +183,15 @@ suspend fun updatePermissionState() {
notificationState,
canDrawOverlaysState,
canWriteExternalStorage,
shizukuOkState
).forEach { it.updateAndGet() }
if (canQueryPkgState.stateFlow.value != canQueryPkgState.updateAndGet()) {
appScope.launchTry {
initOrResetAppInfoCache()
}
}
if (writeSecureSettingsState.stateFlow.value != writeSecureSettingsState.updateAndGet()) {
fixRestartService()
}
shizukuOkState.updateAndGet()
}
}
10 changes: 5 additions & 5 deletions app/src/main/kotlin/li/songe/gkd/service/GkdAbService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class GkdAbService : CompositionAbService({
shizukuAliveFlow,
storeFlow.map(scope) { s -> s.enableShizukuActivity }
)
val safeGetTasksFc = useSafeGetTasksFc(scope, shizukuCanUsedFlow)
val safeGetTasksFc by lazy { useSafeGetTasksFc(scope, shizukuCanUsedFlow) }

val shizukuClickCanUsedFlow = getShizukuCanUsedFlow(
scope,
Expand Down Expand Up @@ -146,7 +146,7 @@ class GkdAbService : CompositionAbService({
val events = mutableListOf<AccessibilityNodeInfo>()
var queryTaskJob: Job? = null
fun newQueryTask(byEvent: Boolean = false, byForced: Boolean = false) {
if (!storeFlow.value.enableService) return
if (!storeFlow.value.enableMatch) return
queryTaskJob = scope.launchTry(queryThread) {
var latestEvent = synchronized(events) {
val size = events.size
Expand Down Expand Up @@ -348,7 +348,7 @@ class GkdAbService : CompositionAbService({
if (evAppId != rightAppId) {
return@launch
}
if (!storeFlow.value.enableService) return@launch
if (!storeFlow.value.enableMatch) return@launch
val eventNode = event.safeSource
synchronized(events) {
val eventLog = events.lastOrNull()
Expand All @@ -363,7 +363,7 @@ class GkdAbService : CompositionAbService({
}
}

var lastUpdateSubsTime = 0L
var lastUpdateSubsTime = System.currentTimeMillis() - 25000
onAccessibilityEvent {// 借助 无障碍事件 触发自动检测更新
if (it.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {// 筛选降低判断频率
val i = storeFlow.value.updateSubsInterval
Expand All @@ -378,7 +378,7 @@ class GkdAbService : CompositionAbService({

scope.launch(Dispatchers.IO) {
activityRuleFlow.debounce(300).collect {
if (storeFlow.value.enableService && it.currentRules.isNotEmpty()) {
if (storeFlow.value.enableMatch && it.currentRules.isNotEmpty()) {
LogUtils.d(it.topActivity, *it.currentRules.map { r ->
r.statusText()
}.toTypedArray())
Expand Down
Loading

0 comments on commit aad9cb2

Please sign in to comment.