Skip to content

Commit

Permalink
Merge pull request #190 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update LayoutInspectService & log with LogUtil
  • Loading branch information
GuoXiCheng authored Aug 7, 2024
2 parents 2e4b04e + 37dd659 commit 922c8b7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ android {
}

dependencies {
implementation "com.blankj:utilcodex:1.31.1"
implementation "com.google.accompanist:accompanist-drawablepainter:0.28.0"
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
Expand All @@ -73,5 +74,4 @@ dependencies {
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:name=".SKIPApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/android/skip/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.android.skip.compose.FlatButton
import com.android.skip.compose.OpenBrowserDialog
import com.android.skip.compose.RowContent
import com.android.skip.compose.ScaffoldPage
import com.android.skip.manager.RectManager
import com.blankj.utilcode.util.ScreenUtils

class AboutActivity : BaseActivity() {
@Composable
Expand Down Expand Up @@ -54,7 +54,7 @@ fun AboutActivityInterface(onBackClick: () -> Unit) {
})

FlatButton(content = {
RowContent(stringResource(id = R.string.about_current_resolution) + RectManager.getMaxRect())
RowContent(stringResource(id = R.string.about_current_resolution) + ScreenUtils.getScreenWidth() + "x" + ScreenUtils.getScreenHeight())
})

})
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/android/skip/SKIPApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.android.skip

import android.app.Application
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils

class SKIPApp : Application() {
override fun onCreate() {
super.onCreate()
Utils.init(this)

LogUtils.getConfig()
.setLogSwitch(true) // 是否输出日志开关
.setConsoleSwitch(true) // 是否在控制台输出日志开关
.setGlobalTag("SKIP_APP") // 全局标签
.setLog2FileSwitch(true) // 是否写入日志文件开关
.setDir("") // 日志文件目录,默认为空,放在 /storage/emulated/0/Android/data/com.android.skip/cache/logs/
.setFilePrefix("log") // 日志文件前缀
.setBorderSwitch(true) // 日志边框开关
.setConsoleFilter(LogUtils.V) // 控制台过滤器
.setFileFilter(LogUtils.V) // 文件过滤器
.setStackDeep(1) // 栈深度
}
}
20 changes: 8 additions & 12 deletions app/src/main/java/com/android/skip/service/LayoutInspectService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.os.Environment
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.DisplayMetrics
import android.view.KeyEvent
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
Expand All @@ -30,6 +29,7 @@ import com.android.skip.R
import com.android.skip.SKIP_LAYOUT_INSPECT
import com.android.skip.manager.ToastManager
import com.android.skip.utils.DataStoreUtils
import com.blankj.utilcode.util.ScreenUtils
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand Down Expand Up @@ -93,13 +93,9 @@ class LayoutInspectService: Service() {

@SuppressLint("WrongConstant")
private fun setupVirtualDisplay() {
val metrics = DisplayMetrics()
val windowManager = applicationContext.getSystemService(Activity.WINDOW_SERVICE) as android.view.WindowManager
windowManager.defaultDisplay.getMetrics(metrics)
val density = metrics.densityDpi

val displayWidth = metrics.widthPixels
val displayHeight = metrics.heightPixels
val displayWidth = ScreenUtils.getScreenWidth()
val displayHeight = ScreenUtils.getScreenHeight()
val density = ScreenUtils.getScreenDensityDpi()

val imageReader = ImageReader.newInstance(displayWidth, displayHeight, PixelFormat.RGBA_8888, 2)
virtualDisplay = mMediaProjection?.createVirtualDisplay(
Expand All @@ -118,15 +114,15 @@ class LayoutInspectService: Service() {
val buffer = planes[0].buffer
val pixelStride = planes[0].pixelStride
val rowStride = planes[0].rowStride
val rowPadding = rowStride - pixelStride * displayWidth

// Create Bitmap
val bitmap = Bitmap.createBitmap(
displayWidth + rowPadding / pixelStride,
val bitmapWithStride = Bitmap.createBitmap(
rowStride / pixelStride,
displayHeight,
Bitmap.Config.ARGB_8888
)
bitmap.copyPixelsFromBuffer(buffer)
bitmapWithStride.copyPixelsFromBuffer(buffer)
val bitmap = Bitmap.createBitmap(bitmapWithStride, 0, 0, displayWidth, displayHeight)
image.close()

// 保存或处理bitmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import com.android.skip.manager.AnalyticsManager
import com.android.skip.manager.ToastManager
import com.android.skip.manager.WhitelistManager
import com.android.skip.utils.DataStoreUtils
import com.blankj.utilcode.util.LogUtils


class MyAccessibilityService : AccessibilityService() {
private val textNodeHandler = TextNodeHandler()
private val idNodeHandler = IdNodeHandler()
private val boundsHandler = BoundsHandler()
private var isLayoutInspect = false

init {
textNodeHandler.setNextHandler(idNodeHandler).setNextHandler(boundsHandler)
Expand All @@ -34,6 +36,11 @@ class MyAccessibilityService : AccessibilityService() {

val rootNode = getCurrentRootNode()

if (isLayoutInspect) {
isLayoutInspect = false
bfsTraverse(rootNode)
}

if (!AnalyticsManager.isPerformScan(rootNode.packageName.toString())) return

if (WhitelistManager.isInWhitelist(rootNode.packageName.toString())) return
Expand Down Expand Up @@ -91,8 +98,32 @@ class MyAccessibilityService : AccessibilityService() {
val intent = Intent(this, LayoutInspectService::class.java)
intent.putExtra("keyCode", event.keyCode)
startService(intent)

isLayoutInspect = true
return true
}
return super.onKeyEvent(event)
}

private fun bfsTraverse(root: AccessibilityNodeInfo) {
val queue: MutableList<AccessibilityNodeInfo> = mutableListOf(root)
val temp: MutableList<String> = mutableListOf()
while (queue.isNotEmpty()) {
val node = queue.removeAt(0)
processNode(node, temp)

for (i in 0 until node.childCount) {
node.getChild(i)?.let { queue.add(it) }
}
}
LogUtils.d(temp.toString())
}

private fun processNode(node: AccessibilityNodeInfo, temp: MutableList<String>) {
// 处理节点信息,可以进行读取文本属性、点击、长按等操作
node.text?.let {
temp.add(it.toString())
}
// 根据需要处理其它节点属性
}
}

0 comments on commit 922c8b7

Please sign in to comment.