From 37caa07fce1129f1cc01d9d078b916b2eef954f4 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Tue, 13 Aug 2024 14:41:15 +0800 Subject: [PATCH 1/3] update --- .../com/android/skip/KeepAliveActivity.kt | 7 ++++ app/src/main/java/com/android/skip/SKIPApp.kt | 12 ++++++ .../java/com/android/skip/SettingsActivity.kt | 16 +++----- .../skip/service/MyAccessibilityService.kt | 39 +++++++------------ .../android/skip/utils/NotificationUtils.kt | 24 ++++++++++++ 5 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/com/android/skip/utils/NotificationUtils.kt diff --git a/app/src/main/java/com/android/skip/KeepAliveActivity.kt b/app/src/main/java/com/android/skip/KeepAliveActivity.kt index 45401a94..d5b1d04a 100644 --- a/app/src/main/java/com/android/skip/KeepAliveActivity.kt +++ b/app/src/main/java/com/android/skip/KeepAliveActivity.kt @@ -16,9 +16,12 @@ import com.android.skip.compose.PictureDialog import com.android.skip.compose.ResourceIcon import com.android.skip.compose.RowContent import com.android.skip.compose.ScaffoldPage +import com.android.skip.enums.AccessibilityState import com.android.skip.service.MyAccessibilityService +import com.android.skip.utils.AccessibilityUtils import com.android.skip.utils.Constants import com.android.skip.utils.DataStoreUtils +import com.android.skip.utils.NotificationUtils import com.blankj.utilcode.util.ServiceUtils.startService import java.net.URLEncoder @@ -96,6 +99,10 @@ class KeepAliveActivity : BaseActivity() { checkForegroundAccessibility.value = it DataStoreUtils.putSyncData(Constants.SKIP_FOREGROUND_ACCESSIBILITY, it) + if (it && !NotificationUtils.hasNotificationPermission()) { + NotificationUtils.startNotificationSettings(context) + } + val intent = Intent(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ACTION) intent.putExtra(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ENABLED, it) intent.setPackage(packageName) diff --git a/app/src/main/java/com/android/skip/SKIPApp.kt b/app/src/main/java/com/android/skip/SKIPApp.kt index 50b3d4b5..16ebaf02 100644 --- a/app/src/main/java/com/android/skip/SKIPApp.kt +++ b/app/src/main/java/com/android/skip/SKIPApp.kt @@ -1,10 +1,19 @@ package com.android.skip +import android.annotation.SuppressLint import android.app.Application +import android.content.Context import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.Utils class SKIPApp : Application() { + + companion object { + @SuppressLint("StaticFieldLeak") + lateinit var context: Context + lateinit var myPackageName: String + } + override fun onCreate() { super.onCreate() Utils.init(this) @@ -20,5 +29,8 @@ class SKIPApp : Application() { .setConsoleFilter(LogUtils.V) // 控制台过滤器 .setFileFilter(LogUtils.V) // 文件过滤器 .setStackDeep(1) // 栈深度 + + context = applicationContext + myPackageName = this.packageName } } \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/SettingsActivity.kt b/app/src/main/java/com/android/skip/SettingsActivity.kt index 5e1a5518..e786d590 100644 --- a/app/src/main/java/com/android/skip/SettingsActivity.kt +++ b/app/src/main/java/com/android/skip/SettingsActivity.kt @@ -26,7 +26,7 @@ import com.android.skip.compose.ResourceIcon import com.android.skip.compose.RowContent import com.android.skip.compose.ScaffoldPage import com.android.skip.utils.DataStoreUtils - +import com.android.skip.utils.NotificationUtils class SettingsActivity : BaseActivity() { @@ -37,7 +37,7 @@ class SettingsActivity : BaseActivity() { override fun onResume() { super.onResume() - if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) { + if (!NotificationUtils.hasNotificationPermission()) { permitNoticeState.value = false DataStoreUtils.putSyncData(SKIP_PERMIT_NOTICE, false) } @@ -134,16 +134,12 @@ fun SettingsActivityInterface(onBackClick: () -> Unit) { { permitNoticeState.value = it DataStoreUtils.putSyncData(SKIP_PERMIT_NOTICE, it) - if (it && !NotificationManagerCompat.from(context) - .areNotificationsEnabled() - ) { - val intent = Intent().apply { - action = Settings.ACTION_APP_NOTIFICATION_SETTINGS - putExtra(Settings.EXTRA_APP_PACKAGE, "com.android.skip") - } - context.startActivity(intent) + if (it && !NotificationUtils.hasNotificationPermission()) { + NotificationUtils.startNotificationSettings(context) } }) + }, { + NotificationUtils.startNotificationSettings(context) }) Menu(expandedState, options, selectedState) diff --git a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt index f5e4507b..4afeb71b 100644 --- a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt +++ b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt @@ -34,7 +34,18 @@ class MyAccessibilityService : AccessibilityService() { private val boundsHandler = BoundsHandler() private var isLayoutInspect = false private var layoutInspectClassName: String? = null - private lateinit var foregroundAccessibilityReceiver: ForegroundAccessibilityChangeReceiver + private val foregroundAccessibilityReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + if (intent?.action == Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ACTION) { + val enabled = intent.getBooleanExtra(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ENABLED, false) + if (enabled) { + ServiceUtils.startService(MyForegroundService::class.java) + } else { + ServiceUtils.stopService(MyForegroundService::class.java) + } + } + } + } init { textNodeHandler.setNextHandler(idNodeHandler).setNextHandler(boundsHandler) @@ -169,16 +180,10 @@ class MyAccessibilityService : AccessibilityService() { super.onServiceConnected() val intentFilter = IntentFilter(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ACTION) - foregroundAccessibilityReceiver = ForegroundAccessibilityChangeReceiver() registerReceiver(foregroundAccessibilityReceiver, intentFilter, RECEIVER_NOT_EXPORTED) if (DataStoreUtils.getSyncData(Constants.SKIP_FOREGROUND_ACCESSIBILITY, false)) { - val intent = Intent(this, MyForegroundService::class.java) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startForegroundService(intent); - } else { - startService(intent); - } + ServiceUtils.startService(MyForegroundService::class.java) } } @@ -186,22 +191,4 @@ class MyAccessibilityService : AccessibilityService() { super.onDestroy() unregisterReceiver(foregroundAccessibilityReceiver) } - - inner class ForegroundAccessibilityChangeReceiver: BroadcastReceiver() { - override fun onReceive(p0: Context?, p1: Intent?) { - if (p1 != null && p1.action.equals(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ACTION)) { - val enabled = p1.getBooleanExtra(Constants.FOREGROUND_ACCESSIBILITY_RECEIVER_ENABLED, false) - val intent = Intent(this@MyAccessibilityService, MyForegroundService::class.java) - if (enabled) { // start foreground service - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - startForegroundService(intent); - } else { - startService(intent); - } - } else { // stop foreground service - stopService(intent) - } - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/android/skip/utils/NotificationUtils.kt b/app/src/main/java/com/android/skip/utils/NotificationUtils.kt new file mode 100644 index 00000000..986a010c --- /dev/null +++ b/app/src/main/java/com/android/skip/utils/NotificationUtils.kt @@ -0,0 +1,24 @@ +package com.android.skip.utils + +import android.content.Context +import android.content.Intent +import android.os.Build +import android.provider.Settings +import androidx.core.app.NotificationManagerCompat +import com.android.skip.SKIPApp + +object NotificationUtils { + fun startNotificationSettings(context: Context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val intent = Intent().apply { + action = Settings.ACTION_APP_NOTIFICATION_SETTINGS + putExtra(Settings.EXTRA_APP_PACKAGE, SKIPApp.myPackageName) + } + context.startActivity(intent) + } + } + + fun hasNotificationPermission(): Boolean { + return NotificationManagerCompat.from(SKIPApp.context).areNotificationsEnabled() + } +} \ No newline at end of file From 2282fab6e1067b97e768dae54d5bfe7fd378d2e9 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Tue, 13 Aug 2024 18:04:46 +0800 Subject: [PATCH 2/3] update --- app/src/main/java/com/android/skip/NewMainActivity.kt | 2 +- .../main/java/com/android/skip/utils/AccessibilityUtils.kt | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/android/skip/NewMainActivity.kt b/app/src/main/java/com/android/skip/NewMainActivity.kt index 732933f6..43360240 100644 --- a/app/src/main/java/com/android/skip/NewMainActivity.kt +++ b/app/src/main/java/com/android/skip/NewMainActivity.kt @@ -144,7 +144,7 @@ class NewMainActivity : BaseActivity() { override fun onResume() { super.onResume() - startButtonViewModel.changeButtonState(AccessibilityUtils.getAccessibilityState(this)) + startButtonViewModel.changeButtonState(AccessibilityUtils.getAccessibilityState()) WhitelistManager.setWhitelist(lifecycleScope, applicationContext) diff --git a/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt b/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt index a31bca75..afdf932c 100644 --- a/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt +++ b/app/src/main/java/com/android/skip/utils/AccessibilityUtils.kt @@ -5,6 +5,7 @@ import android.app.ActivityManager import android.content.Context import android.provider.Settings import android.text.TextUtils +import com.android.skip.SKIPApp import com.android.skip.enums.AccessibilityState import com.android.skip.service.MyAccessibilityService @@ -13,9 +14,9 @@ object AccessibilityUtils { /** * 取得无障碍服务的状态 */ - fun getAccessibilityState(mContext: Context): AccessibilityState { - val isSettingsOn = isAccessibilitySettingsOn(mContext) - val isRunning = isAccessibilityServiceRunning(mContext, MyAccessibilityService::class.java) + fun getAccessibilityState(): AccessibilityState { + val isSettingsOn = isAccessibilitySettingsOn(SKIPApp.context) + val isRunning = isAccessibilityServiceRunning(SKIPApp.context, MyAccessibilityService::class.java) return when { isSettingsOn && isRunning -> AccessibilityState.STARTED From fb940bdeb6ed847f2d0427cf27c5473141d15dd4 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Tue, 13 Aug 2024 20:09:31 +0800 Subject: [PATCH 3/3] update LayoutInspectService --- .../skip/service/LayoutInspectService.kt | 29 ++++++++++++++----- .../skip/service/MyAccessibilityService.kt | 6 ++-- .../java/com/android/skip/utils/Constants.kt | 2 ++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/android/skip/service/LayoutInspectService.kt b/app/src/main/java/com/android/skip/service/LayoutInspectService.kt index c793074e..b6a9cd26 100644 --- a/app/src/main/java/com/android/skip/service/LayoutInspectService.kt +++ b/app/src/main/java/com/android/skip/service/LayoutInspectService.kt @@ -6,8 +6,10 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.app.Service +import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.PixelFormat @@ -21,14 +23,15 @@ import android.os.Environment import android.os.Handler import android.os.IBinder import android.os.Looper -import android.view.KeyEvent import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import com.android.skip.NewMainActivity import com.android.skip.R import com.android.skip.SKIP_LAYOUT_INSPECT import com.android.skip.manager.ToastManager +import com.android.skip.utils.Constants import com.android.skip.utils.DataStoreUtils +import com.blankj.utilcode.util.LogUtils import com.blankj.utilcode.util.ScreenUtils import java.io.File import java.io.FileOutputStream @@ -40,11 +43,19 @@ class LayoutInspectService: Service() { private var mProjectionManager:MediaProjectionManager? = null private var virtualDisplay: VirtualDisplay? = null private var isProcessingImage = false + private val keyEventVolumeDownReceiver = object: BroadcastReceiver() { + override fun onReceive(p0: Context?, p1: Intent?) { + if (p1?.action == Constants.SKIP_KEY_EVENT_VOLUME_DOWN) { + isProcessingImage = true + } + } + } override fun onBind(p0: Intent?): IBinder? { TODO("Not yet implemented") } + @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun onCreate() { super.onCreate() @@ -64,22 +75,21 @@ class LayoutInspectService: Service() { .setContentIntent(pi) .build() startForeground(1, notification) + + val intentFilter = IntentFilter(Constants.SKIP_KEY_EVENT_VOLUME_DOWN) + registerReceiver(keyEventVolumeDownReceiver, intentFilter, RECEIVER_NOT_EXPORTED) } @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val keyCode = intent?.getIntExtra("keyCode", -1) - if (mMediaProjection == null) { val resultCode = intent?.getIntExtra("resultCode", Activity.RESULT_CANCELED) val data = intent?.getParcelableExtra("data", Intent::class.java) if (resultCode == Activity.RESULT_OK && data != null) { mProjectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager mMediaProjection = mProjectionManager?.getMediaProjection(resultCode, data) + setupVirtualDisplay() } - } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { - isProcessingImage = true - setupVirtualDisplay() } return START_NOT_STICKY @@ -106,7 +116,11 @@ class LayoutInspectService: Service() { ) imageReader.setOnImageAvailableListener({reader -> - if (!isProcessingImage) return@setOnImageAvailableListener + if (!isProcessingImage) { + // 清理缓冲区 + reader.acquireLatestImage()?.close() + return@setOnImageAvailableListener + } val image = reader.acquireLatestImage() if (image != null) { @@ -135,7 +149,6 @@ class LayoutInspectService: Service() { } isProcessingImage = false - stopSelf() } }, Handler(Looper.getMainLooper())) } diff --git a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt index 4afeb71b..e42a1997 100644 --- a/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt +++ b/app/src/main/java/com/android/skip/service/MyAccessibilityService.kt @@ -124,9 +124,9 @@ class MyAccessibilityService : AccessibilityService() { && event.keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && DataStoreUtils.getSyncData(SKIP_LAYOUT_INSPECT, false) ) { - val intent = Intent(this, LayoutInspectService::class.java) - intent.putExtra("keyCode", event.keyCode) - startService(intent) + val intent = Intent(Constants.SKIP_KEY_EVENT_VOLUME_DOWN) + intent.setPackage(packageName) + sendBroadcast(intent) isLayoutInspect = true return true diff --git a/app/src/main/java/com/android/skip/utils/Constants.kt b/app/src/main/java/com/android/skip/utils/Constants.kt index 9cdcfa08..d4a3b3ad 100644 --- a/app/src/main/java/com/android/skip/utils/Constants.kt +++ b/app/src/main/java/com/android/skip/utils/Constants.kt @@ -6,4 +6,6 @@ object Constants { const val FOREGROUND_ACCESSIBILITY_RECEIVER_ENABLED = "SKIP_FOREGROUND_ACCESSIBILITY_RECEIVER_ENABLED" const val SKIP_FOREGROUND_ACCESSIBILITY = "SKIP_FOREGROUND_ACCESSIBILITY" + + const val SKIP_KEY_EVENT_VOLUME_DOWN = "SKIP_KEY_EVENT_VOLUME_DOWN" } \ No newline at end of file