Skip to content

Commit

Permalink
Merge pull request #194 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update LayoutInspectService
  • Loading branch information
GuoXiCheng authored Aug 13, 2024
2 parents c14f157 + fb940bd commit ba9fa08
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 51 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/android/skip/KeepAliveActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/android/skip/NewMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/android/skip/SKIPApp.kt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -20,5 +29,8 @@ class SKIPApp : Application() {
.setConsoleFilter(LogUtils.V) // 控制台过滤器
.setFileFilter(LogUtils.V) // 文件过滤器
.setStackDeep(1) // 栈深度

context = applicationContext
myPackageName = this.packageName
}
}
16 changes: 6 additions & 10 deletions app/src/main/java/com/android/skip/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 21 additions & 8 deletions app/src/main/java/com/android/skip/service/LayoutInspectService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -135,7 +149,6 @@ class LayoutInspectService: Service() {
}

isProcessingImage = false
stopSelf()
}
}, Handler(Looper.getMainLooper()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -113,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
Expand Down Expand Up @@ -169,39 +180,15 @@ 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)
}
}

override fun onDestroy() {
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)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/android/skip/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/android/skip/utils/NotificationUtils.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit ba9fa08

Please sign in to comment.