├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── themes.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── xml │ │ │ │ ├── backup_rules.xml │ │ │ │ └── data_extraction_rules.xml │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── nico │ │ │ │ └── dump_hierarchy │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── nico │ │ │ └── dump_hierarchy │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── nico │ │ └── dump_hierarchy │ │ ├── TouchController.java │ │ └── HierarchyTest.java ├── proguard-rules.pro └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── settings.gradle ├── gradle.properties ├── gradlew.bat ├── gradlew └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | dump hierarchy 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/letmeNo1/AndroidHierarchyDumper/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://mirrors.huaweicloud.com/gradle/gradle-8.12-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | .idea 17 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/nico/dump_hierarchy/MainActivity.java: -------------------------------------------------------------------------------- 1 | package nico.dump_hierarchy; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | import android.os.Bundle; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | } 13 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | } 14 | } 15 | rootProject.name = "dump_hierarchy_for" 16 | include ':app' 17 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /app/src/test/java/nico/dump_hierarchy/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package nico.dump_hierarchy; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Enables namespacing of each library's R class so that its R class includes only the 19 | # resources declared in the library itself and none from the library's dependencies, 20 | # thereby reducing the size of the R class for that library 21 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | } 4 | 5 | android { 6 | namespace 'nico.dump_hierarchy' // 添加命名空间声明 7 | compileSdk 33 8 | 9 | defaultConfig { 10 | applicationId "nico.dump_hierarchy" 11 | minSdk 23 12 | targetSdk 32 13 | versionCode 1 14 | versionName "1.4" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | testOptions { 19 | unitTests.includeAndroidResources false 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | } 33 | 34 | dependencies { 35 | androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' 36 | implementation 'androidx.appcompat:appcompat:1.6.1' 37 | implementation 'com.google.android.material:material:1.9.0' 38 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 39 | testImplementation 'junit:junit:4.13.2' 40 | androidTestImplementation 'androidx.test.ext:junit:1.1.5' 41 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 42 | implementation 'com.google.code.gson:gson:2.8.9' 43 | 44 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/androidTest/java/nico/dump_hierarchy/TouchController.java: -------------------------------------------------------------------------------- 1 | package nico.dump_hierarchy; 2 | 3 | import android.app.Instrumentation; 4 | import android.app.UiAutomation; 5 | import android.content.Context; 6 | import android.os.PowerManager; 7 | import android.os.SystemClock; 8 | import android.view.InputEvent; 9 | import android.view.KeyCharacterMap; 10 | import android.view.MotionEvent; 11 | import androidx.core.view.InputDeviceCompat; 12 | import androidx.test.uiautomator.Configurator; 13 | 14 | /* loaded from: classes9.dex */ 15 | public class TouchController { 16 | private static final boolean DEBUG; 17 | private static final String LOG_TAG; 18 | private static final int MOTION_EVENT_INJECTION_DELAY_MILLIS = 5; 19 | private long mDownTime; 20 | private final Instrumentation mInstrumentation; 21 | private final KeyCharacterMap mKeyCharacterMap = KeyCharacterMap.load(-1); 22 | 23 | static { 24 | String simpleName = TouchController.class.getSimpleName(); 25 | LOG_TAG = simpleName; 26 | DEBUG = android.util.Log.isLoggable(simpleName, 3); 27 | } 28 | 29 | public TouchController(Instrumentation instrumentation) { 30 | this.mInstrumentation = instrumentation; 31 | } 32 | 33 | public boolean isScreenOn() { 34 | PowerManager pm = (PowerManager) getContext().getSystemService("power"); 35 | return pm.isScreenOn(); 36 | } 37 | 38 | private boolean injectEventSync(InputEvent event) { 39 | return getUiAutomation().injectInputEvent(event, true); 40 | } 41 | 42 | public boolean touchDown(float x, float y) { 43 | if (DEBUG) { 44 | android.util.Log.d(LOG_TAG, "touchDown (" + x + ", " + y + ")"); 45 | } 46 | long jUptimeMillis = SystemClock.uptimeMillis(); 47 | this.mDownTime = jUptimeMillis; 48 | MotionEvent event = getMotionEvent(jUptimeMillis, jUptimeMillis, 0, x, y); 49 | return injectEventSync(event); 50 | } 51 | 52 | public boolean touchUp(float x, float y) { 53 | if (DEBUG) { 54 | android.util.Log.d(LOG_TAG, "touchUp (" + x + ", " + y + ")"); 55 | } 56 | long eventTime = SystemClock.uptimeMillis(); 57 | MotionEvent event = getMotionEvent(this.mDownTime, eventTime, 1, x, y); 58 | this.mDownTime = 0L; 59 | return injectEventSync(event); 60 | } 61 | 62 | public boolean touchMove(float x, float y) { 63 | if (DEBUG) { 64 | android.util.Log.d(LOG_TAG, "touchMove (" + x + ", " + y + ")"); 65 | } 66 | long eventTime = SystemClock.uptimeMillis(); 67 | MotionEvent event = getMotionEvent(this.mDownTime, eventTime, 2, x, y); 68 | return injectEventSync(event); 69 | } 70 | 71 | private static MotionEvent getMotionEvent(long downTime, long eventTime, int action, float x, float y) { 72 | MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties(); 73 | properties.id = 0; 74 | properties.toolType = Configurator.getInstance().getToolType(); 75 | MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords(); 76 | coords.pressure = 1.0f; 77 | coords.size = 1.0f; 78 | coords.x = x; 79 | coords.y = y; 80 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, 1, new MotionEvent.PointerProperties[]{properties}, new MotionEvent.PointerCoords[]{coords}, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 81 | return event; 82 | } 83 | 84 | public boolean performMultiPointerGesture(MotionEvent.PointerCoords[]... touches) { 85 | if (touches.length < 2) { 86 | throw new IllegalArgumentException("Must provide coordinates for at least 2 pointers"); 87 | } 88 | int maxSteps = 0; 89 | for (int x = 0; x < touches.length; x++) { 90 | maxSteps = maxSteps < touches[x].length ? touches[x].length : maxSteps; 91 | } 92 | int x2 = touches.length; 93 | MotionEvent.PointerProperties[] properties = new MotionEvent.PointerProperties[x2]; 94 | MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[touches.length]; 95 | for (int x3 = 0; x3 < touches.length; x3++) { 96 | MotionEvent.PointerProperties prop = new MotionEvent.PointerProperties(); 97 | prop.id = x3; 98 | prop.toolType = Configurator.getInstance().getToolType(); 99 | properties[x3] = prop; 100 | pointerCoords[x3] = touches[x3][0]; 101 | } 102 | long downTime = SystemClock.uptimeMillis(); 103 | MotionEvent event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 0, 1, properties, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 104 | boolean ret = true & injectEventSync(event); 105 | for (int x4 = 1; x4 < touches.length; x4++) { 106 | MotionEvent event2 = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(5, x4), x4 + 1, properties, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 107 | ret &= injectEventSync(event2); 108 | } 109 | for (int i = 1; i < maxSteps - 1; i++) { 110 | for (int x5 = 0; x5 < touches.length; x5++) { 111 | if (touches[x5].length > i) { 112 | pointerCoords[x5] = touches[x5][i]; 113 | } else { 114 | pointerCoords[x5] = touches[x5][touches[x5].length - 1]; 115 | } 116 | } 117 | MotionEvent event3 = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 2, touches.length, properties, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 118 | ret &= injectEventSync(event3); 119 | SystemClock.sleep(5L); 120 | } 121 | for (int x6 = 0; x6 < touches.length; x6++) { 122 | pointerCoords[x6] = touches[x6][touches[x6].length - 1]; 123 | } 124 | for (int x7 = 1; x7 < touches.length; x7++) { 125 | MotionEvent event4 = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), getPointerAction(6, x7), x7 + 1, properties, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 126 | ret &= injectEventSync(event4); 127 | } 128 | android.util.Log.i(LOG_TAG, "x " + pointerCoords[0].x); 129 | MotionEvent event5 = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), 1, 1, properties, pointerCoords, 0, 0, 1.0f, 1.0f, 0, 0, InputDeviceCompat.SOURCE_TOUCHSCREEN, 0); 130 | return ret & injectEventSync(event5); 131 | } 132 | 133 | private int getPointerAction(int motionEnvent, int index) { 134 | return (index << 8) + motionEnvent; 135 | } 136 | 137 | UiAutomation getUiAutomation() { 138 | return getInstrumentation().getUiAutomation(); 139 | } 140 | 141 | Context getContext() { 142 | return getInstrumentation().getContext(); 143 | } 144 | 145 | Instrumentation getInstrumentation() { 146 | return this.mInstrumentation; 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | if ! command -v java >/dev/null 2>&1 137 | then 138 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 139 | 140 | Please set the JAVA_HOME variable in your environment to match the 141 | location of your Java installation." 142 | fi 143 | fi 144 | 145 | # Increase the maximum file descriptors if we can. 146 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 147 | case $MAX_FD in #( 148 | max*) 149 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 150 | # shellcheck disable=SC2039,SC3045 151 | MAX_FD=$( ulimit -H -n ) || 152 | warn "Could not query maximum file descriptor limit" 153 | esac 154 | case $MAX_FD in #( 155 | '' | soft) :;; #( 156 | *) 157 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 158 | # shellcheck disable=SC2039,SC3045 159 | ulimit -n "$MAX_FD" || 160 | warn "Could not set maximum file descriptor limit to $MAX_FD" 161 | esac 162 | fi 163 | 164 | # Collect all arguments for the java command, stacking in reverse order: 165 | # * args from the command line 166 | # * the main class name 167 | # * -classpath 168 | # * -D...appname settings 169 | # * --module-path (only if needed) 170 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 171 | 172 | # For Cygwin or MSYS, switch paths to Windows format before running java 173 | if "$cygwin" || "$msys" ; then 174 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 175 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 176 | 177 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 178 | 179 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 180 | for arg do 181 | if 182 | case $arg in #( 183 | -*) false ;; # don't mess with options #( 184 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 185 | [ -e "$t" ] ;; #( 186 | *) false ;; 187 | esac 188 | then 189 | arg=$( cygpath --path --ignore --mixed "$arg" ) 190 | fi 191 | # Roll the args list around exactly as many times as the number of 192 | # args, so each arg winds up back in the position where it started, but 193 | # possibly modified. 194 | # 195 | # NB: a `for` loop captures its iteration list before it begins, so 196 | # changing the positional parameters here affects neither the number of 197 | # iterations, nor the values presented in `arg`. 198 | shift # remove old arg 199 | set -- "$@" "$arg" # push replacement arg 200 | done 201 | fi 202 | 203 | 204 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 205 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 206 | 207 | # Collect all arguments for the java command: 208 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 209 | # and any embedded shellness will be escaped. 210 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 211 | # treated as '${Hostname}' itself on the command line. 212 | 213 | set -- \ 214 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 215 | -classpath "$CLASSPATH" \ 216 | org.gradle.wrapper.GradleWrapperMain \ 217 | "$@" 218 | 219 | # Stop when "xargs" is not available. 220 | if ! command -v xargs >/dev/null 2>&1 221 | then 222 | die "xargs is not available" 223 | fi 224 | 225 | # Use "xargs" to parse quoted args. 226 | # 227 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 228 | # 229 | # In Bash we could simply go: 230 | # 231 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 232 | # set -- "${ARGS[@]}" "$@" 233 | # 234 | # but POSIX shell has neither arrays nor command substitution, so instead we 235 | # post-process each arg (as a line of input to sed) to backslash-escape any 236 | # character that might be a shell metacharacter, then use eval to reverse 237 | # that process (while maintaining the separation between arguments), and wrap 238 | # the whole thing up as a single "set" statement. 239 | # 240 | # This will of course break if any of these variables contains a newline or 241 | # an unmatched quote. 242 | # 243 | 244 | eval "set -- $( 245 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 246 | xargs -n1 | 247 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 248 | tr '\n' ' ' 249 | )" '"$@"' 250 | 251 | exec "$JAVACMD" "$@" 252 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidHierarchyDumper 2 | 3 | ## 项目介绍 4 | AndroidHierarchyDumper 是一个基于 Android 平台的 UI 自动化工具,主要用于通过网络接口与设备交互,支持 UI 层级获取、元素查找、触控操作、文本输入等功能。该工具依赖 UiAutomator 框架,可通过 HTTP 接口执行单步操作或批量 JSON 脚本,适用于 UI 测试、自动化操作、屏幕分析等场景,是 Nico 系列工程的依赖组件。 5 | 6 | 7 | ## 功能说明 8 | - **UI 信息获取**:获取当前屏幕 UI 层级结构(XML 格式)、检查 UI 变化状态、截取屏幕截图。 9 | - **元素操作**:按文本、资源 ID、类名等属性查找单个或多个 UI 元素,并返回元素详细信息(位置、尺寸等)。 10 | - **触控交互**:支持坐标点击、触摸按下/抬起/滑动,以及基于元素的点击操作。 11 | - **文本输入**:查找输入框元素并执行文本输入(支持清空原有内容)。 12 | - **批量脚本**:通过 JSON 脚本批量执行多步动作(如点击、输入、滑动序列),减少网络请求次数。 13 | 14 | 15 | ## 环境要求 16 | - JDK 1.8 及以上 17 | - Android SDK(API 23 及以上,对应 Android 6.0+) 18 | - Gradle 8.12(项目已包含 wrapper,无需手动安装) 19 | - Android 设备或模拟器(开启 USB 调试模式) 20 | 21 | 22 | ## 构建与安装 23 | 24 | ### 构建 APK 25 | 在项目根目录执行以下命令构建 APK: 26 | ```shell 27 | ./gradlew clean assemble assembleAndroidTest 28 | ``` 29 | 30 | ### 输出文件 31 | 构建完成后,APK 文件位于: 32 | ``` 33 | ./app/build/outputs/apk/ 34 | ├── app-debug.apk # 主应用 APK 35 | └── app-debug-androidTest.apk # 测试组件 APK(核心功能实现) 36 | ``` 37 | - 应用包名:`nico.dump_hierarchy` 38 | 39 | 40 | ### 安装步骤 41 | 1. 连接 Android 设备(确保已开启 USB 调试) 42 | 2. 通过 ADB 安装 APK: 43 | ```shell 44 | adb install app/build/outputs/apk/app-debug.apk 45 | adb install app/build/outputs/apk/app-debug-androidTest.apk 46 | ``` 47 | 48 | 49 | ## 使用方法 50 | 51 | ### 启动服务 52 | 1. 设备上启动应用(默认启动后自动开启后台服务) 53 | 2. 通过 ADB 转发网络端口(默认使用 9008 端口): 54 | ```shell 55 | adb forward tcp:9008 tcp:9008 56 | ``` 57 | 3. 验证端口转发状态: 58 | ```shell 59 | adb forward --list 60 | ``` 61 | 62 | 63 | ### 核心接口说明 64 | 所有接口通过 HTTP 协议交互,支持 GET/POST 方法,请求参数为键值对或 JSON,响应格式为文本、JSON 或二进制数据。 65 | 66 | | 接口路径 | 请求方法 | 功能描述 | 参数说明 | 返回结果示例 | 67 | |--------------------|----------|--------------------------|--------------------------------------------------------------------------|------------------------------------------------------------------------------| 68 | | `/status` | GET | 检查服务状态 | 无 | `server is running` | 69 | | `/dump` | GET | 获取 UI 层级结构 | 无 | UI 层级 XML 数据(包含元素位置、属性等) | 70 | | `/screenshot` | GET | 获取屏幕截图 | `quality`(可选,0-100,默认 80,图片质量) | 二进制 PNG 图片数据 | 71 | | `/is_ui_change` | GET | 检查 UI 是否变化 | 无 | `{"changed": true}`(`true` 表示有变化) | 72 | | `/click` | GET | 坐标点击 | `x`(横坐标)、`y`(纵坐标) | `{"success": true, "message": "坐标点击成功", "x": 500, "y": 1000}` | 73 | | `/touch_down` | GET | 触摸按下 | `x`(横坐标)、`y`(纵坐标) | `{"success": true, "message": "触摸按下成功"}` | 74 | | `/touch_up` | GET | 触摸抬起 | `x`(横坐标)、`y`(纵坐标) | `{"success": true, "message": "触摸抬起成功"}` | 75 | | `/touch_move` | GET | 触摸滑动 | `x`(目标横坐标)、`y`(目标纵坐标) | `{"success": true, "message": "触摸滑动成功"}` | 76 | | `/find_element` | GET | 查找单个元素 | `type`(查找类型:text/resourceId/className 等)、`value`(查找值)、`timeout`(超时时间,默认 5000ms) | 元素信息 JSON(如 `{"className": "android.widget.Button", "bounds": "[100,200][300,400]"}`) | 77 | | `/find_elements` | GET | 查找多个元素 | 同 `/find_element` | 元素列表 JSON(如 `[{"className": "android.widget.TextView"}, ...]`) | 78 | | `/input` | POST | 文本输入 | 请求体:`{"type": "查找类型", "value": "查找值", "text": "输入内容", "clear": true}`(`clear` 可选,默认 true) | `{"success": true, "message": "输入成功", "actual_text": "输入内容"}` | 79 | | `/execute_json_script` | POST | 批量执行动作 | 请求体:JSON 数组(包含多个动作,见下方示例) | 执行结果 JSON(包含总状态和每个动作的详细结果) | 80 | 81 | 82 | 以下是除 `/execute_json_script` 外其他核心接口的详细使用示例,包含请求方式、参数说明、请求示例(基于 `curl`)及响应示例: 83 | 84 | 85 | ### 1. 服务状态检查 `/status` 86 | - **功能**:检查服务是否正常运行 87 | - **请求方法**:`GET` 88 | - **参数**:无 89 | 90 | **请求示例**: 91 | ```bash 92 | curl http://localhost:9008/status 93 | ``` 94 | 95 | **响应示例**(文本): 96 | ```text 97 | server is running 98 | ``` 99 | 100 | 101 | ### 2. 获取UI层级结构 `/dump` 102 | - **功能**:获取当前屏幕的UI层级结构(XML格式) 103 | - **请求方法**:`GET` 104 | - **参数**:无 105 | 106 | **请求示例**: 107 | ```bash 108 | curl http://localhost:9008/dump -o hierarchy.xml # 保存为本地文件 109 | ``` 110 | 111 | **响应示例**(XML片段): 112 | ```xml 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | ``` 122 | 123 | 124 | ### 3. 获取屏幕截图 `/screenshot` 125 | - **功能**:获取当前屏幕的PNG截图 126 | - **请求方法**:`GET` 127 | - **参数**: 128 | - `quality`(可选):图片质量(0-100,默认80) 129 | 130 | **请求示例**(保存为图片): 131 | ```bash 132 | curl http://localhost:9008/screenshot?quality=90 -o screenshot.png 133 | ``` 134 | 135 | **响应**:二进制PNG图片数据(可直接保存为图片文件) 136 | 137 | 138 | ### 4. 检查UI是否变化 `/is_ui_change` 139 | - **功能**:判断当前UI与上一次检查时是否变化 140 | - **请求方法**:`GET` 141 | - **参数**:无 142 | 143 | **请求示例**: 144 | ```bash 145 | curl http://localhost:9008/is_ui_change 146 | ``` 147 | 148 | **响应示例**(JSON): 149 | ```json 150 | {"changed": true} # true表示有变化,false表示无变化 151 | ``` 152 | 153 | 154 | ### 5. 坐标点击 `/click` 155 | - **功能**:模拟在指定坐标的单次点击(按下→延迟→抬起) 156 | - **请求方法**:`GET` 157 | - **参数**: 158 | - `x`:横坐标(整数/浮点数) 159 | - `y`:纵坐标(整数/浮点数) 160 | 161 | **请求示例**: 162 | ```bash 163 | curl http://localhost:9008/click?x=500&y=1000 164 | ``` 165 | 166 | **响应示例**(JSON): 167 | ```json 168 | {"success": true, "message": "坐标点击成功", "x": 500.0, "y": 1000.0} 169 | ``` 170 | 171 | 172 | ### 6. 触摸按下 `/touch_down` 173 | - **功能**:模拟在指定坐标的触摸按下(不抬起,需配合`/touch_up`使用) 174 | - **请求方法**:`GET` 175 | - **参数**: 176 | - `x`:横坐标 177 | - `y`:纵坐标 178 | 179 | **请求示例**: 180 | ```bash 181 | curl http://localhost:9008/touch_down?x=300&y=800 182 | ``` 183 | 184 | **响应示例**(JSON): 185 | ```json 186 | {"success": true, "message": "触摸按下成功"} 187 | ``` 188 | 189 | 190 | ### 7. 触摸抬起 `/touch_up` 191 | - **功能**:模拟在指定坐标的触摸抬起(与`/touch_down`配对使用) 192 | - **请求方法**:`GET` 193 | - **参数**: 194 | - `x`:横坐标 195 | - `y`:纵坐标 196 | 197 | **请求示例**: 198 | ```bash 199 | curl http://localhost:9008/touch_up?x=300&y=800 200 | ``` 201 | 202 | **响应示例**(JSON): 203 | ```json 204 | {"success": true, "message": "触摸抬起成功"} 205 | ``` 206 | 207 | 208 | ### 8. 触摸滑动 `/touch_move` 209 | - **功能**:模拟触摸滑动(需在`/touch_down`之后、`/touch_up`之前调用) 210 | - **请求方法**:`GET` 211 | - **参数**: 212 | - `x`:目标横坐标 213 | - `y`:目标纵坐标 214 | 215 | **请求示例**(配合按下/抬起完成滑动): 216 | ```bash 217 | # 按下→滑动→抬起(模拟从(300,800)滑动到(600,800)) 218 | curl http://localhost:9008/touch_down?x=300&y=800 219 | curl http://localhost:9008/touch_move?x=450&y=800 # 中间点 220 | curl http://localhost:9008/touch_move?x=600&y=800 # 终点 221 | curl http://localhost:9008/touch_up?x=600&y=800 222 | ``` 223 | 224 | **响应示例**(JSON): 225 | ```json 226 | {"success": true, "message": "触摸滑动成功"} 227 | ``` 228 | 229 | 230 | ### 9. 查找单个UI元素 `/find_element` 231 | - **功能**:按属性查找单个UI元素(如文本、资源ID等) 232 | - **请求方法**:`GET` 233 | - **参数**: 234 | - `type`:查找类型(如`text`、`resourceId`、`className`) 235 | - `value`:查找值(与`type`对应,如文本内容、资源ID) 236 | - `timeout`(可选):超时时间(毫秒,默认5000) 237 | 238 | **请求示例**(查找文本为“登录”的元素): 239 | ```bash 240 | curl http://localhost:9008/find_element?type=text&value=登录&timeout=3000 241 | ``` 242 | 243 | **响应示例**(JSON): 244 | ```json 245 | { 246 | "className": "android.widget.Button", 247 | "resourceId": "com.example:id/btn_login", 248 | "text": "登录", 249 | "bounds": "[400,800][680,920]", # 元素位置(左、上、右、下) 250 | "enabled": true, 251 | "visible": true 252 | } 253 | ``` 254 | 255 | 256 | ### 10. 查找多个UI元素 `/find_elements` 257 | - **功能**:按属性查找多个符合条件的UI元素 258 | - **请求方法**:`GET` 259 | - **参数**:同 `/find_element` 260 | 261 | **请求示例**(查找所有文本框): 262 | ```bash 263 | curl http://localhost:9008/find_elements?type=className&value=android.widget.EditText 264 | ``` 265 | 266 | **响应示例**(JSON数组): 267 | ```json 268 | [ 269 | { 270 | "className": "android.widget.EditText", 271 | "resourceId": "com.example:id/et_username", 272 | "bounds": "[300,500][780,600]", 273 | "text": "" 274 | }, 275 | { 276 | "className": "android.widget.EditText", 277 | "resourceId": "com.example:id/et_password", 278 | "bounds": "[300,650][780,750]", 279 | "text": "" 280 | } 281 | ] 282 | ``` 283 | 284 | 285 | ### 11. 文本输入 `/input` 286 | - **功能**:查找输入框并执行文本输入(支持清空原有内容) 287 | - **请求方法**:`POST` 288 | - **请求体**(JSON): 289 | - `type`:查找输入框的类型(如`resourceId`) 290 | - `value`:查找输入框的值(如资源ID) 291 | - `text`:要输入的文本 292 | - `clear`(可选):是否清空原有内容(默认`true`) 293 | 294 | **请求示例**(向用户名输入框输入文本): 295 | ```bash 296 | curl -X POST http://localhost:9008/input \ 297 | -H "Content-Type: application/json" \ 298 | -d '{"type":"resourceId", "value":"com.example:id/et_username", "text":"test_user", "clear":true}' 299 | ``` 300 | 301 | **响应示例**(JSON): 302 | ```json 303 | { 304 | "success": true, 305 | "message": "输入成功", 306 | "input_text": "test_user", 307 | "actual_text": "test_user" 308 | } 309 | ``` 310 | 311 | 以下是结合你提供的 `HierarchyTest.java` 代码,在保留原有内容基础上补充完整的 `/execute_json_script` 接口文档(含 `click` 动作详细说明): 312 | 313 | 314 | ### 12. 批量脚本示例(`/execute_json_script`) 315 | `execute_json_script` 是一个支持批量执行多步骤操作的核心接口,允许通过 JSON 格式的脚本定义一系列连续动作(如点击、输入、滑动等),适用于复杂场景的自动化操作。以下是详细介绍: 316 | 317 | 318 | ### **核心功能** 319 | - 接收一个包含多个动作的 JSON 脚本,按顺序执行所有动作。 320 | - 支持动作间共享元素缓存(例如,前一个动作查找的元素可被后续动作复用)。 321 | - 返回每个动作的执行结果及整体执行状态,便于调试和流程控制。 322 | 323 | 324 | ### **请求方式与参数** 325 | - **请求方法**:`POST` 326 | - **请求体**:JSON 数组,每个元素为一个动作对象,包含以下字段: 327 | - `type`:动作类型(必填,支持 `click`/`find_and_click`/`find_and_input`/`swipe_sequence`)。 328 | - `params`:动作参数(必填,根据 `type` 不同而变化,详见下方动作说明)。 329 | 330 | 331 | ### **支持的动作类型及参数** 332 | #### 1. 坐标点击(`type: "click"`) 333 | - **功能**:直接按指定坐标执行点击(模拟真实点击流程:按下→50ms延迟→抬起)。 334 | - **`params` 字段**: 335 | - `x`:横坐标(数字,必填,单位:像素)。 336 | - `y`:纵坐标(数字,必填,单位:像素)。 337 | - **代码逻辑说明**: 338 | 从 `HierarchyTest.java` 可知,`click` 动作通过 `touchController` 执行底层触摸事件: 339 | ```java 340 | // 执行点击(按下→延迟→抬起) 341 | boolean clickSuccess = touchController.touchDown(x, y); 342 | SystemClock.sleep(50); // 模拟按下时长 343 | clickSuccess &= touchController.touchUp(x, y); 344 | ``` 345 | 346 | 347 | #### 2. 查找并点击(`type: "find_and_click"`) 348 | - **功能**:先按属性查找元素,再点击元素中心。 349 | - **`params` 字段**: 350 | - `type`:查找类型(字符串,必填,如 `text`/`resourceId`/`className`)。 351 | - `value`:查找值(字符串,必填,与 `type` 对应,如文本内容、资源 ID)。 352 | - `timeout`:超时时间(数字,可选,默认 5000 毫秒,超过时间未找到元素则失败)。 353 | 354 | 355 | #### 3. 查找并输入(`type: "find_and_input"`) 356 | - **功能**:先按属性查找输入框,再输入文本(默认清空原有内容)。 357 | - **`params` 字段**: 358 | - `type`:查找类型(字符串,必填,如 `resourceId`)。 359 | - `value`:查找值(字符串,必填,如输入框的资源 ID)。 360 | - `text`:要输入的文本(字符串,必填)。 361 | - `clear`:是否清空原有内容(布尔值,可选,默认 `true`)。 362 | - `timeout`:超时时间(数字,可选,默认 5000 毫秒)。 363 | 364 | 365 | #### 4. 滑动序列(`type: "swipe_sequence"`) 366 | - **功能**:按指定起点和多步滑动路径执行滑动(按下→分步移动→抬起)。 367 | - **`params` 字段**: 368 | - `startX`:滑动起点横坐标(数字,必填)。 369 | - `startY`:滑动起点纵坐标(数字,必填)。 370 | - `steps`:滑动步骤数组(数组,必填,至少 1 步,每步为 `{x: 数字, y: 数字}`)。 371 | - `duration`:总滑动时长(数字,可选,默认 500 毫秒,分配到每步的间隔时间)。 372 | 373 | 374 | ### **请求示例(curl)** 375 | 包含 `click` 动作与其他动作的组合示例: 376 | ```bash 377 | curl -X POST http://localhost:9008/execute_json_script \ 378 | -H "Content-Type: application/json" \ 379 | -d '[ 380 | { 381 | "type": "click", 382 | "params": { 383 | "x": 300, # 左上角菜单按钮坐标 384 | "y": 150 385 | } 386 | }, 387 | { 388 | "type": "find_and_click", 389 | "params": { 390 | "type": "text", 391 | "value": "登录", 392 | "timeout": 3000 393 | } 394 | }, 395 | { 396 | "type": "find_and_input", 397 | "params": { 398 | "type": "resourceId", 399 | "value": "com.example:id/et_username", 400 | "text": "test_user", 401 | "clear": true 402 | } 403 | }, 404 | { 405 | "type": "swipe_sequence", 406 | "params": { 407 | "startX": 500, 408 | "startY": 1500, 409 | "steps": [{"x":500,"y":1000}, {"x":500,"y":500}], 410 | "duration": 800 411 | } 412 | }, 413 | { 414 | "type": "click", 415 | "params": { 416 | "x": 900, # 右下角确认按钮坐标 417 | "y": 1800 418 | } 419 | } 420 | ]' 421 | ``` 422 | 423 | 424 | ### **响应格式** 425 | 返回 JSON 对象,包含整体执行状态和每个动作的详细结果: 426 | ```json 427 | { 428 | "success": true, // 整体是否成功(所有动作均成功为true) 429 | "totalActions": 5, // 总动作数 430 | "results": [ 431 | { 432 | "actionIndex": 0, 433 | "actionType": "click", 434 | "success": true, 435 | "message": "坐标点击成功", 436 | "x": 300.0, 437 | "y": 150.0 438 | }, 439 | { 440 | "actionIndex": 1, 441 | "actionType": "find_and_click", 442 | "success": true, 443 | "message": "查找并点击成功", 444 | "element_bounds": "[600,480][800,580]", 445 | "click_x": 700, 446 | "click_y": 530 447 | }, 448 | { 449 | "actionIndex": 2, 450 | "actionType": "find_and_input", 451 | "success": true, 452 | "message": "输入成功", 453 | "input_text": "test_user", 454 | "actual_text": "test_user" 455 | }, 456 | { 457 | "actionIndex": 3, 458 | "actionType": "swipe_sequence", 459 | "success": true, 460 | "message": "滑动序列执行成功", 461 | "start": {"x": 500, "y": 1500}, 462 | "end": {"x": 500, "y": 500}, 463 | "step_count": 2 464 | }, 465 | { 466 | "actionIndex": 4, 467 | "actionType": "click", 468 | "success": true, 469 | "message": "坐标点击成功", 470 | "x": 900.0, 471 | "y": 1800.0 472 | } 473 | ] 474 | } 475 | ``` 476 | 477 | 478 | ### **错误场景说明** 479 | - 若 `click` 动作缺少 `x` 或 `y` 参数,响应将提示: 480 | `{"success":false, "message":"click动作缺少参数x或y"}` 481 | - 若动作类型不存在,响应将提示: 482 | `{"success":false, "message":"未知动作类型:xxx"}` 483 | 484 | 485 | ### **错误处理** 486 | - **JSON 格式错误**:返回 `400` 状态码,提示“解析JSON脚本失败”。 487 | - **动作缺少字段**:如缺少 `type` 或 `params`,对应动作标记为失败,整体 `success` 为 `false`。 488 | - **单个动作失败**:不中断后续动作(可通过代码修改为“失败即停止”),整体 `success` 为 `false`。 489 | - **执行异常**:如参数类型错误、元素未找到等,在对应动作结果中返回错误消息。 490 | 491 | 492 | ### **关键特性** 493 | - **原子性**:按顺序执行动作,前一个动作的结果不强制影响后一个(可自定义中断逻辑)。 494 | - **元素缓存**:`find_and_click` 等查找动作会将元素存入缓存(`elementCache`),供后续动作复用(需代码中显式调用)。 495 | - **灵活性**:支持通过 `duration` 控制滑动速度、`timeout` 控制元素查找等待时间,适配不同场景。 496 | 497 | 498 | ## 权限说明 499 | 应用需要以下权限(已在 `AndroidManifest.xml` 中声明): 500 | - `android.permission.INTERNET`:用于网络接口通信 501 | - `android.permission.READ_SECURE_SETTINGS`:用于获取部分受保护的 UI 信息(可能需要手动授予) 502 | 503 | 504 | ## 依赖说明 505 | - **UI 自动化**:`androidx.test.uiautomator:uiautomator:2.2.0`(核心 UI 元素操作) 506 | - **AndroidX 组件**:`appcompat:1.6.1`、`constraintlayout:2.1.4` 等 507 | - **JSON 解析**:`com.google.code.gson:gson:2.8.9`(处理 JSON 序列化/反序列化) 508 | - **Material Design**:`com.google.android.material:material:1.9.0`(基础 UI 组件) 509 | 510 | 511 | ## 注意事项 512 | 1. **兼容性**:最低支持 Android 6.0(API 23),推荐在 Android 10+ 设备上使用。 513 | 2. **元素查找**:`type` 参数支持 UiAutomator 所有查找类型(如 `text`、`resourceId`、`desc`、`className` 等)。 514 | 3. **批量脚本执行**:动作按数组顺序执行,前序动作失败不会中断后续执行(可修改代码中的 `continue` 为 `break` 停止)。 515 | 4. **权限授予**:`READ_SECURE_SETTINGS` 权限可能需要通过 `adb shell pm grant nico.dump_hierarchy android.permission.READ_SECURE_SETTINGS` 手动授予。 516 | 517 | 518 | ## 开发相关 519 | - **核心功能实现**:`app/src/androidTest/java/nico/dump_hierarchy/HierarchyTest.java`(处理网络请求和动作执行) 520 | - **触控控制**:`app/src/androidTest/java/nico/dump_hierarchy/TouchController.java`(封装触摸事件注入逻辑) 521 | - **布局资源**:`app/src/main/res/`(包含应用图标、主题配置等基础资源) 522 | - **构建配置**:`app/build.gradle`(依赖管理和编译配置) 523 | -------------------------------------------------------------------------------- /app/src/androidTest/java/nico/dump_hierarchy/HierarchyTest.java: -------------------------------------------------------------------------------- 1 | package nico.dump_hierarchy; 2 | 3 | import static androidx.test.InstrumentationRegistry.getContext; 4 | import com.google.gson.Gson; 5 | import com.google.gson.reflect.TypeToken; 6 | 7 | import android.accessibilityservice.AccessibilityService; 8 | import android.accessibilityservice.GestureDescription; 9 | import android.app.UiAutomation; 10 | import android.content.Context; 11 | import android.graphics.Path; 12 | import android.graphics.Rect; 13 | import android.net.Uri; 14 | import android.os.Bundle; 15 | import android.os.SystemClock; 16 | import android.util.Log; 17 | import android.view.accessibility.AccessibilityEvent; 18 | import android.view.accessibility.AccessibilityNodeInfo; 19 | 20 | import androidx.test.platform.app.InstrumentationRegistry; 21 | import androidx.test.uiautomator.By; 22 | import androidx.test.uiautomator.BySelector; 23 | import androidx.test.uiautomator.Configurator; 24 | import androidx.test.uiautomator.StaleObjectException; 25 | import androidx.test.uiautomator.UiDevice; 26 | import androidx.test.uiautomator.UiObject2; 27 | import androidx.test.uiautomator.UiObjectNotFoundException; 28 | import androidx.test.uiautomator.Until; 29 | 30 | import org.junit.Test; 31 | 32 | import java.io.BufferedReader; 33 | import java.io.ByteArrayOutputStream; 34 | import java.io.File; 35 | import java.io.FileInputStream; 36 | import java.io.IOException; 37 | import java.io.InputStream; 38 | import java.io.InputStreamReader; 39 | import java.io.OutputStream; 40 | import java.lang.reflect.InvocationTargetException; 41 | import java.lang.reflect.Method; 42 | import java.lang.reflect.Type; 43 | import java.net.InetAddress; 44 | import java.net.ServerSocket; 45 | import java.net.Socket; 46 | import java.net.URLDecoder; 47 | import java.util.ArrayList; 48 | import java.util.Arrays; 49 | import java.util.HashMap; 50 | import java.util.List; 51 | import java.util.Map; 52 | import java.util.Objects; 53 | import java.util.concurrent.TimeoutException; 54 | import java.util.concurrent.atomic.AtomicBoolean; 55 | 56 | public class HierarchyTest extends AccessibilityService { 57 | private static final String TAG = "hank_auto"; 58 | private String path; 59 | private AccessibilityEvent lastWindowChangeEvent = null; 60 | private ServerSocket serverSocket; 61 | private TouchController touchController; // 添加TouchController实例 62 | private final AtomicBoolean uiChanged = new AtomicBoolean(false); 63 | 64 | private void init() { 65 | Configurator.getInstance().setWaitForIdleTimeout(1); 66 | Configurator.getInstance().setWaitForSelectorTimeout(1); 67 | Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 68 | File filesDir = context.getFilesDir(); 69 | touchController = new TouchController(InstrumentationRegistry.getInstrumentation()); 70 | 71 | path = filesDir.getPath(); 72 | } 73 | 74 | private final UiAutomation.AccessibilityEventFilter checkWindowUpdate = event -> { 75 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) { 76 | lastWindowChangeEvent = event; 77 | return true; 78 | } 79 | return false; 80 | }; 81 | 82 | @Test 83 | public void TestCase1() { 84 | init(); 85 | startWatchingUiChanges(); 86 | startHttpServer(); 87 | while (true) { 88 | try { 89 | Thread.sleep(1000); 90 | } catch (InterruptedException e) { 91 | e.printStackTrace(); 92 | } 93 | } 94 | } 95 | 96 | private void startHttpServer() { 97 | Thread serverThread = new Thread(() -> { 98 | try { 99 | Bundle arguments = InstrumentationRegistry.getArguments(); 100 | int port = arguments.getInt("port", 8000); 101 | InetAddress serverAddress = InetAddress.getByName("localhost"); 102 | serverSocket = new ServerSocket(port, 0, serverAddress); 103 | Log.i(TAG, "HTTP Server running on " + serverAddress.getHostAddress() + ":" + port); 104 | 105 | while (!serverSocket.isClosed()) { 106 | Socket clientSocket = serverSocket.accept(); 107 | new Thread(() -> handleClient(clientSocket)).start(); 108 | } 109 | } catch (IOException e) { 110 | e.printStackTrace(); 111 | } 112 | }); 113 | serverThread.start(); 114 | } 115 | 116 | private void handleClient(Socket socket) { 117 | try (InputStream is = socket.getInputStream(); 118 | OutputStream os = socket.getOutputStream()) { // os在此块内有效 119 | 120 | Map request = parseHttpRequest(is); 121 | String method = request.get("method"); 122 | String path = request.get("path"); 123 | String requestBody = request.get("body"); // 新增:获取POST请求体 124 | Map params = parseQueryParams(request.get("query")); 125 | 126 | try { // 新增内层try块 127 | switch (path) { 128 | case "/execute_json_script": 129 | if ("POST".equals(method)) { 130 | // 调用handleExecuteJsonScript处理POST请求体中的JSON 131 | handleExecuteJsonScript(os, requestBody); 132 | } else { 133 | sendResponse(os, 405, "application/json", 134 | "{\"success\":false, \"message\":\"仅支持POST方法\"}"); 135 | } 136 | break; 137 | case "/status": // 状态检查路由 138 | handleHealthRequest(os); 139 | break; 140 | case "/dump": 141 | handleDumpRequest(os, params); 142 | break; 143 | case "/screenshot": 144 | handlePicRequest(os, params); 145 | break; 146 | case "/is_ui_change": 147 | handleIsUiChangeRequest(os); 148 | break; 149 | case "/find_element": 150 | handleFindElementRequest(os, params); 151 | break; 152 | case "/find_elements": 153 | handleFindElementsRequest(os, params); 154 | break; 155 | case "/get_root": 156 | handleGetRootRequest(os); 157 | break; 158 | case "/click": 159 | handleClickRequest(os,params); 160 | break; 161 | case "/touch_down": // 新增:单点按下 162 | handleTouchDownRequest(os, params); 163 | break; 164 | case "/touch_up": // 新增:单点抬起 165 | handleTouchUpRequest(os, params); 166 | break; 167 | case "/touch_move": // 新增:滑动 168 | handleTouchMoveRequest(os, params); 169 | break; 170 | case "/input": 171 | // 支持GET和POST(参数可放在URL或请求体) 172 | if ("GET".equals(method) || "POST".equals(method)) { 173 | handleInputRequest(os, params); 174 | } else { 175 | sendResponse(os, 405, "application/json", 176 | "{\"success\":false, \"message\":\"仅支持GET/POST方法\"}"); 177 | } 178 | break; 179 | default: 180 | sendResponse(os, 404, "text/plain", "Not Found"); 181 | } 182 | } catch (Exception e) { // 捕获所有异常 183 | sendResponse(os, 500, "text/plain", "Internal Server Error"); 184 | e.printStackTrace(); 185 | } 186 | 187 | } catch (IOException e) { 188 | // 处理socket创建异常 189 | e.printStackTrace(); 190 | } 191 | } 192 | 193 | private void handleHealthRequest(OutputStream os) throws IOException { 194 | String responseText = "server is running"; 195 | sendResponse(os, 200, "text/plain", responseText); 196 | } 197 | 198 | /** 199 | * 处理点击请求:通过坐标直接调用TouchController执行点击 200 | */ 201 | private void handleClickRequest(OutputStream os, Map params) throws IOException { 202 | // 获取坐标参数(从请求参数中解析x和y) 203 | String xStr = params.get("x"); 204 | String yStr = params.get("y"); 205 | 206 | // 参数校验 207 | if (xStr == null || yStr == null) { 208 | sendResponse(os, 400, "text/plain", "Missing parameters: x and y are required"); 209 | return; 210 | } 211 | 212 | try { 213 | // 转换坐标为浮点数 214 | float x = Float.parseFloat(xStr); 215 | float y = Float.parseFloat(yStr); 216 | 217 | // 调用TouchController执行点击(按下->抬起) 218 | boolean downSuccess = touchController.touchDown(x, y); 219 | // 增加微小延迟,模拟真实点击的按下-抬起间隔 220 | SystemClock.sleep(50); 221 | boolean upSuccess = touchController.touchUp(x, y); 222 | 223 | // 根据执行结果返回响应 224 | if (downSuccess && upSuccess) { 225 | sendResponse(os, 200, "text/plain", "Click at (" + x + ", " + y + ") success"); 226 | } else { 227 | sendResponse(os, 500, "text/plain", "Click failed (down: " + downSuccess + ", up: " + upSuccess + ")"); 228 | } 229 | } catch (NumberFormatException e) { 230 | // 处理坐标格式错误 231 | sendResponse(os, 400, "text/plain", "Invalid coordinates: x and y must be numbers"); 232 | } catch (Exception e) { 233 | // 处理其他异常 234 | sendResponse(os, 500, "text/plain", "Click error: " + e.getMessage()); 235 | } 236 | } 237 | 238 | 239 | private Map parseHttpRequest(InputStream is) throws IOException { 240 | Map request = new HashMap<>(); 241 | BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 242 | 243 | // 读取请求行 244 | String requestLine = reader.readLine(); 245 | if (requestLine == null) return request; 246 | String[] parts = requestLine.split(" "); 247 | if (parts.length >= 2) { 248 | request.put("method", parts[0]); 249 | request.put("path", parts[1]); 250 | if (parts.length >= 3) request.put("protocol", parts[2]); 251 | } 252 | 253 | // 解析路径和查询参数 254 | String path = request.get("path"); 255 | if (path != null) { 256 | int qIndex = path.indexOf('?'); 257 | if (qIndex != -1) { 258 | request.put("query", path.substring(qIndex + 1)); 259 | request.put("path", path.substring(0, qIndex)); 260 | } else { 261 | request.put("query", ""); 262 | } 263 | } else { 264 | request.put("query", ""); 265 | } 266 | 267 | // 读取头部信息(重点:获取Content-Length) 268 | int contentLength = 0; 269 | while (true) { 270 | String line = reader.readLine(); 271 | if (line == null || line.isEmpty()) break; 272 | // 解析Content-Length头部,用于读取Body 273 | if (line.startsWith("Content-Length:")) { 274 | contentLength = Integer.parseInt(line.split(":")[1].trim()); 275 | } 276 | } 277 | 278 | // 读取POST Body(根据Content-Length读取) 279 | if (contentLength > 0) { 280 | char[] bodyChars = new char[contentLength]; 281 | reader.read(bodyChars, 0, contentLength); 282 | request.put("body", new String(bodyChars)); 283 | } else { 284 | request.put("body", ""); // 空Body 285 | } 286 | 287 | return request; 288 | } 289 | 290 | private Map parseQueryParams(String query) { 291 | Map params = new HashMap<>(); 292 | if (query == null) return params; 293 | 294 | String[] pairs = query.split("&"); 295 | for (String pair : pairs) { 296 | int idx = pair.indexOf('='); 297 | if (idx != -1) { 298 | try { 299 | String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8"); 300 | String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8"); 301 | params.put(key, value); 302 | } catch (IOException e) { 303 | e.printStackTrace(); 304 | } 305 | } 306 | } 307 | return params; 308 | } 309 | 310 | /** 311 | * 处理单点按下请求(仅按下不抬起) 312 | */ 313 | private void handleTouchDownRequest(OutputStream os, Map params) throws IOException { 314 | // 解析坐标参数 315 | String xStr = params.get("x"); 316 | String yStr = params.get("y"); 317 | if (xStr == null || yStr == null) { 318 | sendResponse(os, 400, "text/plain", "Missing parameters: x and y are required"); 319 | return; 320 | } 321 | 322 | try { 323 | float x = Float.parseFloat(xStr); 324 | float y = Float.parseFloat(yStr); 325 | // 调用TouchController执行按下 326 | boolean success = touchController.touchDown(x, y); 327 | if (success) { 328 | sendResponse(os, 200, "text/plain", "TouchDown at (" + x + ", " + y + ") success"); 329 | } else { 330 | sendResponse(os, 500, "text/plain", "TouchDown failed"); 331 | } 332 | } catch (NumberFormatException e) { 333 | sendResponse(os, 400, "text/plain", "Invalid coordinates: x and y must be numbers"); 334 | } catch (Exception e) { 335 | sendResponse(os, 500, "text/plain", "TouchDown error: " + e.getMessage()); 336 | } 337 | } 338 | 339 | /** 340 | * 处理单点抬起请求(需与之前的touchDown对应) 341 | */ 342 | private void handleTouchUpRequest(OutputStream os, Map params) throws IOException { 343 | // 解析坐标参数(需与按下坐标一致,确保抬起对应点) 344 | String xStr = params.get("x"); 345 | String yStr = params.get("y"); 346 | if (xStr == null || yStr == null) { 347 | sendResponse(os, 400, "text/plain", "Missing parameters: x and y are required"); 348 | return; 349 | } 350 | 351 | try { 352 | float x = Float.parseFloat(xStr); 353 | float y = Float.parseFloat(yStr); 354 | // 调用TouchController执行抬起 355 | boolean success = touchController.touchUp(x, y); 356 | if (success) { 357 | sendResponse(os, 200, "text/plain", "TouchUp at (" + x + ", " + y + ") success"); 358 | } else { 359 | sendResponse(os, 500, "text/plain", "TouchUp failed"); 360 | } 361 | } catch (NumberFormatException e) { 362 | sendResponse(os, 400, "text/plain", "Invalid coordinates: x and y must be numbers"); 363 | } catch (Exception e) { 364 | sendResponse(os, 500, "text/plain", "TouchUp error: " + e.getMessage()); 365 | } 366 | } 367 | 368 | /** 369 | * 处理滑动请求(从起点(x1,y1)滑动到终点(x2,y2),支持自定义滑动时长) 370 | */ 371 | /** 372 | * 处理触摸移动请求(基于MotionEvent的连续移动,需配合touchDown和touchUp使用) 373 | */ 374 | private void handleTouchMoveRequest(OutputStream os, Map params) throws IOException { 375 | // 解析当前移动坐标参数 376 | String xStr = params.get("x"); 377 | String yStr = params.get("y"); 378 | 379 | // 参数校验 380 | if (xStr == null || yStr == null) { 381 | sendResponse(os, 400, "text/plain", "Missing parameters: x and y are required"); 382 | return; 383 | } 384 | 385 | try { 386 | // 转换坐标为浮点数 387 | float x = Float.parseFloat(xStr); 388 | float y = Float.parseFloat(yStr); 389 | 390 | // 调用TouchController执行移动(基于MotionEvent的单点连续移动) 391 | boolean success = touchController.touchMove(x, y); 392 | 393 | // 根据执行结果返回响应 394 | if (success) { 395 | sendResponse(os, 200, "text/plain", "TouchMove to (" + x + ", " + y + ") success"); 396 | } else { 397 | sendResponse(os, 500, "text/plain", "TouchMove failed"); 398 | } 399 | } catch (NumberFormatException e) { 400 | // 处理坐标格式错误 401 | sendResponse(os, 400, "text/plain", "Invalid coordinates: x and y must be numbers"); 402 | } catch (Exception e) { 403 | // 处理其他异常 404 | sendResponse(os, 500, "text/plain", "TouchMove error: " + e.getMessage()); 405 | } 406 | } 407 | 408 | 409 | private void handleDumpRequest(OutputStream os, Map params) throws IOException { 410 | boolean compressed = Boolean.parseBoolean(params.getOrDefault("compressed", "false")); 411 | File dumpFile = dumpWindowHierarchy(compressed, "dump.xml"); 412 | sendFileResponse(os, "application/xml", dumpFile); 413 | } 414 | 415 | private void handlePicRequest(OutputStream os, Map params) throws IOException { 416 | int quality = Integer.parseInt(Objects.requireNonNull(params.getOrDefault("quality", "80"))); 417 | File screenshot = takeScreenshot(quality); 418 | sendFileResponse(os, "image/png", screenshot); 419 | } 420 | 421 | private void handleIsUiChangeRequest(OutputStream os) throws IOException { 422 | String result = is_ui_change(); 423 | sendResponse(os, 200, "text/plain", result); 424 | } 425 | 426 | /** 427 | * 执行单个动作 428 | * @param actionType 动作类型:click/find_and_click/find_and_input/swipe_sequence 429 | * @param params 动作参数 430 | * @param elementCache 元素缓存(供动作间共享) 431 | * @return 执行结果(JSON字符串) 432 | */ 433 | private String executeAction(String actionType, Map params, Map elementCache) { 434 | Gson gson = new Gson(); 435 | Map result = new HashMap<>(); 436 | 437 | try { 438 | switch (actionType) { 439 | // 1. 坐标点击(直接按x,y点击) 440 | case "click": 441 | // 参数校验:必须包含x和y 442 | if (!params.containsKey("x") || !params.containsKey("y")) { 443 | result.put("success", false); 444 | result.put("message", "click动作缺少参数x或y"); 445 | return gson.toJson(result); 446 | } 447 | 448 | float x = ((Number) params.get("x")).floatValue(); 449 | float y = ((Number) params.get("y")).floatValue(); 450 | 451 | // 执行点击(按下→延迟→抬起) 452 | boolean clickSuccess = touchController.touchDown(x, y); 453 | SystemClock.sleep(50); // 模拟按下时长 454 | clickSuccess &= touchController.touchUp(x, y); 455 | 456 | result.put("success", clickSuccess); 457 | result.put("message", clickSuccess ? "坐标点击成功" : "坐标点击失败"); 458 | result.put("x", x); 459 | result.put("y", y); 460 | return gson.toJson(result); 461 | 462 | // 2. 查找并点击(先找元素,再点击中心) 463 | case "find_and_click": 464 | // 参数校验:必须包含type和value 465 | if (!params.containsKey("type") || !params.containsKey("value")) { 466 | result.put("success", false); 467 | result.put("message", "find_and_click缺少参数type或value"); 468 | return gson.toJson(result); 469 | } 470 | 471 | String findType = (String) params.get("type"); 472 | String findValue = (String) params.get("value"); 473 | int findTimeout = params.containsKey("timeout") ? 474 | ((Number) params.get("timeout")).intValue() : 5000; 475 | 476 | // 查找元素 477 | UiObject2 element = findElementWithTimeout(findType, findValue, findTimeout); 478 | if (element == null) { 479 | result.put("success", false); 480 | result.put("message", "未找到元素:" + findType + "=" + findValue); 481 | return gson.toJson(result); 482 | } 483 | 484 | // 点击元素中心 485 | Rect bounds = element.getVisibleBounds(); 486 | float centerX = bounds.centerX(); 487 | float centerY = bounds.centerY(); 488 | boolean findClickSuccess = touchController.touchDown(centerX, centerY); 489 | SystemClock.sleep(50); 490 | findClickSuccess &= touchController.touchUp(centerX, centerY); 491 | 492 | // 缓存元素供后续动作使用 493 | elementCache.put("last_found", element); 494 | 495 | result.put("success", findClickSuccess); 496 | result.put("message", findClickSuccess ? "查找并点击成功" : "查找成功但点击失败"); 497 | result.put("element_bounds", bounds.toShortString()); 498 | result.put("click_x", centerX); 499 | result.put("click_y", centerY); 500 | return gson.toJson(result); 501 | 502 | // 3. 查找并输入(先找元素,再输入文本) 503 | case "find_and_input": 504 | // 参数校验:必须包含type、value、text 505 | if (!params.containsKey("type") || !params.containsKey("value") || !params.containsKey("text")) { 506 | result.put("success", false); 507 | result.put("message", "find_and_input缺少参数type/value/text"); 508 | return gson.toJson(result); 509 | } 510 | 511 | String inputType = (String) params.get("type"); 512 | String inputValue = (String) params.get("value"); 513 | String inputText = (String) params.get("text"); 514 | boolean clear = params.containsKey("clear") ? 515 | (boolean) params.get("clear") : true; // 默认清空 516 | int inputTimeout = params.containsKey("timeout") ? 517 | ((Number) params.get("timeout")).intValue() : 5000; 518 | 519 | // 查找输入框元素 520 | UiObject2 inputElement = findElementWithTimeout(inputType, inputValue, inputTimeout); 521 | if (inputElement == null) { 522 | result.put("success", false); 523 | result.put("message", "未找到输入元素:" + inputType + "=" + inputValue); 524 | return gson.toJson(result); 525 | } 526 | 527 | // 执行输入 528 | if (clear) inputElement.clear(); 529 | SystemClock.sleep(200); // 等待清空 530 | inputElement.setText(inputText); 531 | SystemClock.sleep(300); // 等待输入完成 532 | 533 | // 验证输入结果 534 | String actualText = inputElement.getText(); 535 | boolean inputSuccess = inputText.equals(actualText); 536 | 537 | result.put("success", inputSuccess); 538 | result.put("message", inputSuccess ? "输入成功" : "输入失败(实际值:" + actualText + ")"); 539 | result.put("input_text", inputText); 540 | result.put("actual_text", actualText); 541 | return gson.toJson(result); 542 | 543 | // 4. 滑动序列(按下→多步滑动→抬起) 544 | case "swipe_sequence": 545 | // 参数校验:必须包含startX、startY和steps(滑动步骤数组) 546 | if (!params.containsKey("startX") || !params.containsKey("startY") || !params.containsKey("steps")) { 547 | result.put("success", false); 548 | result.put("message", "swipe_sequence缺少参数startX/startY/steps"); 549 | return gson.toJson(result); 550 | } 551 | 552 | float startX = ((Number) params.get("startX")).floatValue(); 553 | float startY = ((Number) params.get("startY")).floatValue(); 554 | List> steps = (List>) params.get("steps"); 555 | 556 | if (steps.isEmpty()) { 557 | result.put("success", false); 558 | result.put("message", "steps数组不能为空(至少需要1步滑动)"); 559 | return gson.toJson(result); 560 | } 561 | 562 | // 执行滑动序列:按下起点→分步滑动→抬起终点 563 | boolean swipeSuccess = touchController.touchDown(startX, startY); 564 | SystemClock.sleep(50); // 按下延迟 565 | 566 | // 总滑动时长(默认500ms,可通过参数自定义) 567 | int totalDuration = params.containsKey("duration") ? 568 | ((Number) params.get("duration")).intValue() : 500; 569 | int stepDelay = totalDuration / steps.size(); 570 | 571 | // 执行每步滑动 572 | for (Map step : steps) { 573 | float stepX = step.get("x").floatValue(); 574 | float stepY = step.get("y").floatValue(); 575 | swipeSuccess &= touchController.touchMove(stepX, stepY); 576 | SystemClock.sleep(stepDelay); // 每步间隔 577 | } 578 | 579 | // 抬起(终点为最后一步的坐标) 580 | Map lastStep = steps.get(steps.size() - 1); 581 | float endX = lastStep.get("x").floatValue(); 582 | float endY = lastStep.get("y").floatValue(); 583 | swipeSuccess &= touchController.touchUp(endX, endY); 584 | 585 | result.put("success", swipeSuccess); 586 | result.put("message", swipeSuccess ? "滑动序列执行成功" : "滑动序列执行失败"); 587 | result.put("start", Map.of("x", startX, "y", startY)); 588 | result.put("end", Map.of("x", endX, "y", endY)); 589 | result.put("step_count", steps.size()); 590 | return gson.toJson(result); 591 | 592 | default: 593 | result.put("success", false); 594 | result.put("message", "未知动作类型:" + actionType); 595 | return gson.toJson(result); 596 | } 597 | } catch (ClassCastException e) { 598 | result.put("success", false); 599 | result.put("message", "参数类型错误:" + e.getMessage()); 600 | return gson.toJson(result); 601 | } catch (Exception e) { 602 | result.put("success", false); 603 | result.put("message", "动作执行失败:" + e.getMessage()); 604 | return gson.toJson(result); 605 | } 606 | } 607 | 608 | private void handleExecuteJsonScript(OutputStream os, String jsonContent) throws IOException { 609 | Gson gson = new Gson(); 610 | try { 611 | // 解析JSON脚本为动作列表:[{type: "...", params: {...}}, ...] 612 | Type actionListType = new TypeToken>>(){}.getType(); 613 | List> actions = gson.fromJson(jsonContent, actionListType); 614 | 615 | if (actions == null || actions.isEmpty()) { 616 | sendResponse(os, 400, "application/json", 617 | "{\"success\":false, \"message\":\"JSON脚本为空或格式错误\"}"); 618 | return; 619 | } 620 | 621 | // 元素缓存:存储查找的元素供后续动作复用 622 | Map elementCache = new HashMap<>(); 623 | // 执行结果列表:记录每个动作的详细执行情况 624 | List> executionResults = new ArrayList<>(); 625 | boolean allSuccess = true; 626 | 627 | // 按顺序执行每个动作 628 | for (int i = 0; i < actions.size(); i++) { 629 | Map action = actions.get(i); 630 | Map result = new HashMap<>(); 631 | result.put("actionIndex", i); 632 | result.put("actionType", action.getOrDefault("type", "unknown")); 633 | 634 | try { 635 | // 基础校验:动作必须包含type和params 636 | if (!action.containsKey("type") || !action.containsKey("params")) { 637 | result.put("success", false); 638 | result.put("message", "动作缺少type或params字段"); 639 | executionResults.add(result); 640 | allSuccess = false; 641 | continue; 642 | } 643 | 644 | // 执行动作并获取结果 645 | String actionType = (String) action.get("type"); 646 | Map params = (Map) action.get("params"); 647 | String actionResultJson = executeAction(actionType, params, elementCache); 648 | 649 | // 解析动作结果并记录 650 | Map actionResult = gson.fromJson(actionResultJson, new TypeToken>(){}.getType()); 651 | result.putAll(actionResult); 652 | executionResults.add(result); 653 | 654 | // 若当前动作失败,标记整体失败 655 | if (!(boolean) actionResult.getOrDefault("success", false)) { 656 | allSuccess = false; 657 | // 可根据需求改为break(停止后续动作)或continue(继续执行) 658 | // break; 659 | } 660 | 661 | } catch (Exception e) { 662 | result.put("success", false); 663 | result.put("message", "动作执行异常:" + e.getMessage()); 664 | executionResults.add(result); 665 | allSuccess = false; 666 | } 667 | } 668 | 669 | // 构建总响应 670 | Map totalResponse = new HashMap<>(); 671 | totalResponse.put("success", allSuccess); 672 | totalResponse.put("totalActions", actions.size()); 673 | totalResponse.put("results", executionResults); 674 | sendResponse(os, 200, "application/json", gson.toJson(totalResponse)); 675 | 676 | } catch (Exception e) { 677 | sendResponse(os, 400, "application/json", 678 | "{\"success\":false, \"message\":\"解析JSON脚本失败:" + e.getMessage() + "\"}"); 679 | } 680 | } 681 | 682 | private void handleInputRequest(OutputStream os, Map params) throws IOException { 683 | try { 684 | // 解析元素查找参数 685 | String type = params.get("type"); 686 | String value = params.get("value"); 687 | int timeout = parseTimeout(params.get("timeout"), 5000); 688 | 689 | // 解析输入参数 690 | String inputText = params.get("text"); 691 | boolean clearBeforeInput = Boolean.parseBoolean(params.getOrDefault("clear", "true")); // 默认清空 692 | 693 | if (type == null || value == null || inputText == null) { 694 | sendResponse(os, 400, "application/json", 695 | "{\"success\":false, \"message\":\"缺少参数(type/value/text)\"}"); 696 | return; 697 | } 698 | 699 | // 查找输入元素 700 | UiObject2 inputElement = findElementWithTimeout(type, value, timeout); 701 | if (inputElement == null) { 702 | sendResponse(os, 404, "application/json", 703 | "{\"success\":false, \"message\":\"未找到输入元素\"}"); 704 | return; 705 | } 706 | 707 | // 执行输入操作 708 | if (clearBeforeInput) { 709 | inputElement.clear(); // 清空 710 | SystemClock.sleep(200); // 等待清空完成 711 | } 712 | inputElement.setText(inputText); // 输入文本 713 | SystemClock.sleep(300); // 等待输入完成 714 | 715 | // 验证结果 716 | String actualText = inputElement.getText(); 717 | boolean success = inputText.equals(actualText); 718 | 719 | // 返回结果 720 | sendResponse(os, 200, "application/json", 721 | String.format("{\"success\":%b, \"message\":\"%s\", \"actual_text\":\"%s\"}", 722 | success, 723 | success ? "输入成功" : "输入失败", 724 | actualText) 725 | ); 726 | 727 | } catch (Exception e) { 728 | sendResponse(os, 500, "application/json", 729 | String.format("{\"success\":false, \"message\":\"输入操作异常:%s\"}", e.getMessage())); 730 | } 731 | } 732 | 733 | private UiObject2 findElementWithTimeout(String type, String value, int timeout) throws IOException { 734 | UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 735 | BySelector selector = buildBySelector(type, value); 736 | // 使用 UiAutomator 的超时等待机制(等待元素出现,最多等待 timeout 毫秒) 737 | return device.wait(Until.findObject(selector), timeout); 738 | } 739 | 740 | // 带超时的多个元素查找 741 | private List findElementsWithTimeout(String type, String value, int timeout) throws IOException { 742 | UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 743 | BySelector selector = buildBySelector(type, value); 744 | // 等待元素出现后再查找(最多等待 timeout 毫秒) 745 | if (device.wait(Until.hasObject(selector), timeout)) { 746 | return device.findObjects(selector); 747 | } 748 | return Arrays.asList(); // 超时返回空列表 749 | } 750 | 751 | private void handleFindElementRequest(OutputStream os, Map params) throws IOException { 752 | String type = params.get("type"); 753 | String value = params.get("value"); 754 | // 获取超时参数(默认5000毫秒) 755 | int timeout = parseTimeout(params.get("timeout"), 5000); 756 | 757 | try { 758 | UiObject2 element = findElementWithTimeout(type, value, timeout); 759 | if (element != null) { 760 | String json = String.format("{%s}", getElementAttributes(element)); 761 | sendResponse(os, 200, "application/json", json); 762 | } else { 763 | sendResponse(os, 404, "text/plain", "Element not found within timeout"); 764 | } 765 | } catch (Exception e) { 766 | sendResponse(os, 400, "text/plain", e.getMessage()); 767 | } 768 | } 769 | 770 | private void handleFindElementsRequest(OutputStream os, Map params) throws IOException { 771 | String type = params.get("type"); 772 | String value = params.get("value"); 773 | // 获取超时参数(默认5000毫秒) 774 | int timeout = parseTimeout(params.get("timeout"), 5000); 775 | 776 | try { 777 | List elements = findElementsWithTimeout(type, value, timeout); 778 | if (!elements.isEmpty()) { 779 | StringBuilder json = new StringBuilder("["); 780 | for (int i = 0; i < elements.size(); i++) { 781 | json.append("{").append(getElementAttributes(elements.get(i))).append("}"); 782 | if (i < elements.size() - 1) json.append(","); 783 | } 784 | json.append("]"); 785 | sendResponse(os, 200, "application/json", json.toString()); 786 | } else { 787 | sendResponse(os, 404, "text/plain", "Elements not found within timeout"); 788 | } 789 | } catch (Exception e) { 790 | sendResponse(os, 400, "text/plain", e.getMessage()); 791 | } 792 | } 793 | 794 | private int parseTimeout(String timeoutStr, int defaultValue) { 795 | if (timeoutStr == null || timeoutStr.isEmpty()) { 796 | return defaultValue; 797 | } 798 | try { 799 | int timeout = Integer.parseInt(timeoutStr); 800 | return Math.max(timeout, 0); // 确保超时时间非负 801 | } catch (NumberFormatException e) { 802 | return defaultValue; 803 | } 804 | } 805 | 806 | private void handleGetRootRequest(OutputStream os) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { 807 | String rst = getWindowRoots(); 808 | sendResponse(os, 200, "text/plain", rst); 809 | } 810 | 811 | private void sendResponse(OutputStream os, int statusCode, String contentType, String content) throws IOException { 812 | byte[] contentBytes = content.getBytes("UTF-8"); // 明确指定编码(如UTF-8) 813 | String header = String.format("HTTP/1.1 %d %s\r\n" + 814 | "Content-Type: %s; charset=UTF-8\r\n" + // 补充字符集说明 815 | "Content-Length: %d\r\n" + 816 | "\r\n", 817 | statusCode, getStatusMessage(statusCode), contentType, contentBytes.length); // 使用字节数组长度 818 | 819 | os.write(header.getBytes("UTF-8")); 820 | os.write(contentBytes); 821 | os.flush(); 822 | } 823 | 824 | private void sendFileResponse(OutputStream os, String contentType, File file) throws IOException { 825 | long contentLength = file.length(); 826 | String header = String.format("HTTP/1.1 200 OK\r\n" + 827 | "Content-Type: %s\r\n" + 828 | "Content-Length: %d\r\n" + 829 | "\r\n", contentType, contentLength); 830 | 831 | os.write(header.getBytes()); 832 | 833 | try (FileInputStream fis = new FileInputStream(file)) { 834 | byte[] buffer = new byte[1024]; 835 | int length; 836 | while ((length = fis.read(buffer)) != -1) { 837 | os.write(buffer, 0, length); 838 | } 839 | } 840 | os.flush(); 841 | } 842 | 843 | private String getStatusMessage(int statusCode) { 844 | switch (statusCode) { 845 | case 200: return "OK"; 846 | case 400: return "Bad Request"; 847 | case 404: return "Not Found"; 848 | case 500: return "Internal Server Error"; 849 | default: return "Unknown"; 850 | } 851 | } 852 | 853 | private File dumpWindowHierarchy(boolean compressed, String fileName) { 854 | UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 855 | mDevice.setCompressedLayoutHeirarchy(compressed); 856 | 857 | File file = new File(path, fileName); 858 | try { 859 | if (!file.exists()) file.createNewFile(); 860 | mDevice.dumpWindowHierarchy(file); 861 | } catch (IOException e) { 862 | e.printStackTrace(); 863 | } 864 | return file; 865 | } 866 | 867 | private File takeScreenshot(int quality) throws IOException { 868 | File screenshotFile = new File(path, "screenshot.png"); 869 | UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 870 | mDevice.takeScreenshot(screenshotFile, 0.1f, quality); 871 | return screenshotFile; 872 | } 873 | 874 | private BySelector buildBySelector(String type, String value) throws IOException { 875 | BySelector bySelector = null; 876 | 877 | switch (type) { 878 | case "text": 879 | bySelector = By.text(value); 880 | break; 881 | case "textContains": 882 | bySelector = By.textContains(value); 883 | break; 884 | case "textStartsWith": 885 | bySelector = By.textStartsWith(value); 886 | break; 887 | case "id": 888 | bySelector = By.res(value); 889 | break; 890 | case "class": 891 | bySelector = By.clazz(value); 892 | break; 893 | case "content_desc": 894 | bySelector = By.desc(value); 895 | break; 896 | case "content_descContains": 897 | bySelector = By.descContains(value); 898 | break; 899 | case "pkg": 900 | bySelector = By.pkg(value); 901 | break; 902 | case "checkable": 903 | bySelector = By.checkable(Boolean.parseBoolean(value)); 904 | break; 905 | case "checked": 906 | bySelector = By.checked(Boolean.parseBoolean(value)); 907 | break; 908 | case "clickable": 909 | bySelector = By.clickable(Boolean.parseBoolean(value)); 910 | break; 911 | case "enabled": 912 | bySelector = By.enabled(Boolean.parseBoolean(value)); 913 | break; 914 | case "focusable": 915 | bySelector = By.focusable(Boolean.parseBoolean(value)); 916 | break; 917 | case "focused": 918 | bySelector = By.focused(Boolean.parseBoolean(value)); 919 | break; 920 | case "scrollable": 921 | bySelector = By.scrollable(Boolean.parseBoolean(value)); 922 | break; 923 | case "selected": 924 | bySelector = By.selected(Boolean.parseBoolean(value)); 925 | break; 926 | default: 927 | throw new IOException("Unknown selector type"); 928 | } 929 | return bySelector; 930 | } 931 | 932 | private String getElementAttributes(UiObject2 uiObject) { 933 | return "\"text\": \"" + (uiObject.getText() != null ? uiObject.getText() : "") + "\", " + 934 | "\"id\": \"" + (uiObject.getResourceName() != null ? uiObject.getResourceName() : "") + "\", " + 935 | "\"class_name\": \"" + (uiObject.getClassName() != null ? uiObject.getClassName() : "") + "\", " + 936 | "\"package\": \"" + (uiObject.getApplicationPackage() != null ? uiObject.getApplicationPackage() : "") + "\", " + 937 | "\"content_desc\": \"" + (uiObject.getContentDescription() != null ? uiObject.getContentDescription() : "") + "\", " + 938 | "\"checkable\": \"" + uiObject.isCheckable() + "\", " + 939 | "\"checked\": \"" + uiObject.isChecked() + "\", " + 940 | "\"clickable\": \"" + uiObject.isClickable() + "\", " + 941 | "\"enabled\": \"" + uiObject.isEnabled() + "\", " + 942 | "\"focusable\": \"" + uiObject.isFocusable() + "\", " + 943 | "\"focused\": \"" + uiObject.isFocused() + "\", " + 944 | "\"scrollable\": \"" + uiObject.isScrollable() + "\", " + 945 | "\"long_clickable\": \"" + uiObject.isLongClickable() + "\", " + 946 | "\"selected\": \"" + uiObject.isSelected() + "\", " + 947 | "\"bounds\": \"" + uiObject.getVisibleBounds().toShortString() + "\""; 948 | } 949 | 950 | private UiObject2 findElement(String type, String value) throws IOException { 951 | BySelector selector = buildBySelector(type, value); 952 | UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 953 | int retryCount = 3; 954 | while (retryCount > 0) { 955 | UiObject2 element = mDevice.findObject(selector); 956 | if (element != null) return element; 957 | retryCount--; 958 | } 959 | return null; 960 | } 961 | 962 | private List findElements(String type, String value) throws IOException { 963 | BySelector selector = buildBySelector(type, value); 964 | return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).findObjects(selector); 965 | } 966 | 967 | 968 | public String getWindowRoots() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { 969 | UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 970 | Class clazz = Class.forName("androidx.test.uiautomator.UiDevice"); 971 | Method method = clazz.getDeclaredMethod("getWindowRoots"); 972 | method.setAccessible(true); 973 | AccessibilityNodeInfo[] roots = (AccessibilityNodeInfo[]) method.invoke(mDevice); 974 | return Arrays.toString(roots); 975 | } 976 | 977 | private void startWatchingUiChanges() { 978 | // 添加循环控制标志,避免无限循环无法终止 979 | AtomicBoolean isRunning = new AtomicBoolean(true); 980 | 981 | Thread watcherThread = new Thread(() -> { 982 | while (isRunning.get()) { // 用标志位控制循环 983 | try { 984 | // 延长超时时间到15秒,减少频繁超时 985 | InstrumentationRegistry.getInstrumentation().getUiAutomation().executeAndWaitForEvent( 986 | () -> {}, // 前置操作(无实际逻辑可留空) 987 | checkWindowUpdate, // 事件条件 988 | 15000 // 超时时间延长至15000ms 989 | ); 990 | // 只有收到符合条件的事件时,才标记UI变化 991 | uiChanged.set(true); 992 | Log.d("WatcherThread", "检测到UI变化"); 993 | } catch (TimeoutException e) { 994 | // 仅打印超时日志,不抛出异常,让线程继续循环 995 | Log.w("WatcherThread", "超时未检测到UI事件,继续等待..."); 996 | // 可选:超时后也可以标记UI变化(根据业务需求) 997 | // uiChanged.set(false); 998 | } catch (Exception e) { 999 | // 捕获其他异常(如UiAutomation被销毁),终止循环 1000 | Log.e("WatcherThread", "监听线程发生错误,停止监听", e); 1001 | isRunning.set(false); // 终止循环 1002 | } 1003 | } 1004 | }); 1005 | watcherThread.start(); 1006 | } 1007 | 1008 | 1009 | private String is_ui_change() { 1010 | if (lastWindowChangeEvent != null && uiChanged.getAndSet(false)) { 1011 | lastWindowChangeEvent = null; 1012 | return "true"; 1013 | } else { 1014 | return "false"; 1015 | } 1016 | } 1017 | 1018 | @Override 1019 | public void onAccessibilityEvent(AccessibilityEvent event) { 1020 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) { 1021 | lastWindowChangeEvent = event; 1022 | uiChanged.set(true); 1023 | } 1024 | } 1025 | 1026 | @Override 1027 | public void onInterrupt() { 1028 | // Handle interrupt 1029 | } 1030 | } --------------------------------------------------------------------------------