├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
└── runConfigurations.xml
├── License
├── Main.dex
├── README.md
├── adb
├── build.gradle
├── demo.gif
├── dex-package.png
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lib
├── .gitignore
├── build.gradle
└── src
│ └── main
│ └── java
│ └── com
│ └── client
│ ├── Client.java
│ ├── Install.java
│ └── ScaleIcon.java
├── settings.gradle
├── shareandcontrollib
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ ├── android
│ ├── hardware
│ │ └── display
│ │ │ └── IDisplayManager.java
│ ├── os
│ │ └── ServiceManager.java
│ └── view
│ │ ├── DisplayInfo.java
│ │ ├── IRotationWatcher.java
│ │ ├── IWindowManager.java
│ │ ├── Surface.java
│ │ └── SurfaceControl.java
│ └── com
│ └── wanjian
│ └── puppet
│ ├── Main.java
│ ├── ScreenUtils.java
│ └── TouchUtils.java
└── 效果视频.mp4
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 | /.idea/
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Code style issuesJava
47 |
48 |
49 | Groovy
50 |
51 |
52 | Java
53 |
54 |
55 | Potentially confusing code constructsGroovy
56 |
57 |
58 | Probable bugsJava
59 |
60 |
61 | Serialization issuesJava
62 |
63 |
64 | Threading issuesJava
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | 1.8
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/License:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 https://github.com/android-notes/ wanjian
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Main.dex:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/Main.dex
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | ###
3 | ### 效果
4 | 
5 | ### 使用方式
6 |
7 | 对于mac 笔记本用户:
8 | * Android手机开启开发者选项
9 | * 用数据线连接Android手机和mac
10 | * 运行lib目录下的Client,用于显示和控制
11 | * 运行lib目录下的Install,然后点击连接按钮,若不显示请安如下方式操作
12 |
13 |
14 | 对于所有用户:
15 | * Android手机开启开发者选项
16 | * 用数据线连接Android手机和PC
17 | * 使用adb命令把项目根目录下的Main.dex放到手机中 `adb push Main.dex /sdcard/Main.dex`
18 | * 执行adb shell命令 `adb shell`
19 | * 执行命令 `export CLASSPATH=/sdcard/Main.dex`
20 | * 执行命令 `exec app_process /sdcard com.wanjian.puppet.Main`
21 | * 新建命令窗口,然后执行 `adb forward tcp:8888 localabstract:puppet-ver1`
22 | * 运行lib目录下的Client,用于显示和控制,点击连接按钮即可
23 |
24 | 注意:高版本的android手机需要去开发者选项中开启 允许模拟点击
25 |
26 | 完整命令如下
27 | ```html
28 |
29 | MGJwanjian:sss wanjian$ adb push Main.dex /sdcard/Main.dex
30 | [100%] /sdcard/Main.dex
31 | MGJwanjian:sss wanjian$ adb shell
32 | shell@mx5:/ $ export CLASSPATH=/sdcard/Main.dex
33 | shell@mx5:/ $ exec app_process /sdcard com.wanjian.puppet.Main
34 | MGJwanjian:~ wanjian$ adb forward tcp:8888 localabstract:puppet-ver1
35 |
36 |
37 | ```
38 |
39 |
40 |
41 |
42 | ### 效果视频
43 |
44 | [https://github.com/android-notes/androidScreenShareAndControl/blob/master/%E6%95%88%E6%9E%9C%E8%A7%86%E9%A2%91.mp4](https://github.com/android-notes/androidScreenShareAndControl/blob/master/%E6%95%88%E6%9E%9C%E8%A7%86%E9%A2%91.mp4)
45 |
46 |
47 |
48 | ### 原理
49 |
50 | 原理和Vysor相同,Android提供了两个截屏方法Surface. screenshot和SurfaceControl. screenshot,
51 | 这两个API是隐藏的,客户端没有权限调用,即使通过反射也得不到bitmap,我们可以使用adb命令
52 | 启动一个进程,让该进程调用该API就可以得到bitmap了,然后通过socket把数据发送到PC即可。
53 |
54 | 关键代码如下:
55 | ```java
56 |
57 | public class Main{
58 | public static void main(String[]args){
59 | Point size = new Point();
60 | size.x = 1080;//最终截屏图片的大小,可以和屏幕不一样大
61 | size.y = 1920;
62 | String surfaceClassName;
63 | if (Build.VERSION.SDK_INT <= 17) {
64 | surfaceClassName = "android.view.Surface";
65 | } else {
66 | surfaceClassName = "android.view.SurfaceControl";
67 | }
68 | Bitmap b = (Bitmap) Class.forName(surfaceClassName).getDeclaredMethod("screenshot", new Class[]{Integer.TYPE, Integer.TYPE}).invoke(null, new Object[]{Integer.valueOf(size.x), Integer.valueOf(size.y)});
69 | }
70 | }
71 |
72 |
73 | ```
74 |
75 | * 然后按照如下操作:
76 | * 把这个类编译成class文件
77 | * 使用dx --dex --output=Main.dex Main.class打包成dex文件
78 | * 把dex文件发送到手机adb push Main.dex /sdcard/Main.dex
79 | * 执行adb shell进入shell
80 | * 设置类路径 export CLASSPATH=/sdcard/Main.dex
81 | * 运行代码 exec app_process /sdcard Main
82 |
83 | 这样就可以调用到Main.main方法
84 |
85 | ### 控制原理
86 |
87 | * PC端获取点击位置相对于当前显示窗口的比例
88 | * 把该比例发送给手机端
89 | * 手机端根据手机屏幕大小把比例转换成绝对位置并调用如下代码既可以实现远程控制,x和y是点击的绝对位置,action是动作,如按下,滑动,抬起等
90 |
91 | ```java
92 |
93 |
94 |
95 | InputManager im = (InputManager) InputManager.class.getDeclaredMethod("getInstance", new Class[0]).invoke(null, new Object[0]);
96 |
97 |
98 | MotionEvent.class.getDeclaredMethod("obtain", new Class[0]).setAccessible(true);
99 |
100 | Method injectInputEventMethod = InputManager.class.getMethod("injectInputEvent", new Class[]{InputEvent.class, Integer.TYPE});
101 |
102 |
103 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, 1.0f, 0, 1.0f, 1.0f, 0, 0);
104 | event.setSource(InputDeviceCompat.SOURCE_TOUCHSCREEN);
105 |
106 | injectInputEventMethod.invoke(im, new Object[]{event, Integer.valueOf(0)});
107 |
108 |
109 |
110 | ```
111 |
112 | ##### 注意:
113 | 获取屏幕大小时会用到几个特殊的API,Android sdk没有提供这几个API,但Android运行时却可以调用,
114 | 为了保证编译不报错,我们可以自己手写这几个API,保证包名,方法签名和系统中的相同即可,方法若有
115 | 返回值直接返回null即可。例如
116 |
117 |
118 | ```java
119 |
120 | package android.view;
121 | import android.graphics.Point;
122 | import android.os.IBinder;
123 | /**
124 | * Created by wanjian on 2017/4/4.
125 | */
126 | public interface IWindowManager {
127 | void getInitialDisplaySize(int i, Point displaySize);
128 | int getRotation();
129 | void getRealDisplaySize(Point displaySize);
130 | abstract class Stub {
131 | public static IWindowManager asInterface(IBinder invoke) {
132 | return null;
133 | }
134 | }
135 | }
136 |
137 |
138 | ```
139 |
140 |
141 | ```java
142 |
143 | package android.view;
144 |
145 | /**
146 | * Created by wanjian on 2017/4/4.
147 | */
148 |
149 | public interface DisplayInfo {
150 | }
151 |
152 |
153 |
154 | ```
155 |
156 |
157 |
158 | ```java
159 | package android.view;
160 |
161 | /**
162 | * Created by wanjian on 2017/4/4.
163 | */
164 |
165 | public interface IRotationWatcher {
166 | }
167 |
168 |
169 | ```
170 |
171 |
172 | 由于Android的双亲委派类加载机制,Android会从系统路径下加载这几个类,并不会使用我们编写的类,我们编
173 | 写的这几个类只是为了编译不报错,所以返回null也不会出现空指针
174 |
175 |
176 |
177 |
178 |
179 |
180 | ### 简易版
181 | [http://blog.csdn.net/qingchunweiliang/article/details/69210431](http://blog.csdn.net/qingchunweiliang/article/details/69210431)
182 |
183 |
184 |
185 | ### Github
186 | [https://github.com/android-notes/androidScreenShareAndControl](https://github.com/android-notes/androidScreenShareAndControl)
187 |
188 |
189 | ### 附 编译class方式:
190 |
191 | 最简单的方式:
192 | 在`android studio`中右击`com.wanjian.puppet.Main`这个文件,选择 `run Main.main()`,编译后的class文件就会自动保存到
193 | `androidScreenShareAndControl/shareandcontrollib/build/intermediates/classes/debug` 这个目录中
194 |
195 |
196 | 方式2:
197 | * 把 `android sdk`目录下的`android.jar`和`supportv4.jar`拷贝到
198 | `androidScreenShareAndControl/shareandcontrollib/src/main/java`
199 | 目录下
200 |
201 | * 同时在这个目录下新建`classes`文件夹,用于保存编译后的class文件,并把命令行切换到这个目录
202 |
203 | * 执行如下命令,其中`android.jar`和`support-v4-23.4.0-sources.jar` 是`android sdk`中的`jar`包,一个在`platforms/android-xx`文件夹下,一个在`extras/android/m2repository/com/android/support/support-v4`下。(命令中间用:分割,windows的话需要用;分割)
204 |
205 | * `javac -cp android.jar:support-v4-23.4.0-sources.jar:./ com/wanjian/puppet/Main.java -d classes`
206 |
207 | * 这样就会在classes文件夹中生成class文件了 (JDK版本不能太高,不然会提示 unsupported class file version 52.0)
208 |
209 | 
210 |
211 |
212 | ### 打包dex方式:
213 | 首先命令窗口切换到 `/androidScreenShareAndControl/shareandcontrollib/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes` 目录下,可以看到
214 | 所有编译生成的class文件,如果没有先执行上面步骤生成class文件。
215 |
216 | 然后使用 `dx --dex --output=Main.dex ./`命令生成dex文件。dx命令文件在 `sdk/build-tools/版本号` 下
217 |
218 | 
219 |
220 |
221 |
222 |
--------------------------------------------------------------------------------
/adb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/adb
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | maven {
7 | url 'https://maven.google.com/'
8 | name 'Google'
9 | }
10 | }
11 | dependencies {
12 | classpath 'com.android.tools.build:gradle:3.2.1'
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | jcenter()
21 | maven {
22 | url 'https://maven.google.com/'
23 | name 'Google'
24 | }
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
31 |
--------------------------------------------------------------------------------
/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/demo.gif
--------------------------------------------------------------------------------
/dex-package.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/dex-package.png
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jun 18 14:49:50 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | dependencies {
4 | compile fileTree(dir: 'libs', include: ['*.jar'])
5 | }
6 |
7 | sourceCompatibility = "1.7"
8 | targetCompatibility = "1.7"
9 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/client/Client.java:
--------------------------------------------------------------------------------
1 | package com.client;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.Image;
5 | import java.awt.event.MouseAdapter;
6 | import java.awt.event.MouseEvent;
7 | import java.io.BufferedInputStream;
8 | import java.io.BufferedWriter;
9 | import java.io.ByteArrayInputStream;
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 | import java.io.OutputStreamWriter;
13 | import java.net.Socket;
14 |
15 | import javax.imageio.ImageIO;
16 | import javax.swing.ImageIcon;
17 | import javax.swing.JButton;
18 | import javax.swing.JFrame;
19 | import javax.swing.JLabel;
20 | import javax.swing.JPanel;
21 | import javax.swing.JSlider;
22 | import javax.swing.JTextField;
23 | import javax.swing.border.EmptyBorder;
24 | import javax.swing.event.ChangeEvent;
25 | import javax.swing.event.ChangeListener;
26 |
27 | public class Client extends JFrame {
28 | JLabel label;
29 | boolean isMove = false;
30 | JLabel tips;
31 |
32 | public Client() throws IOException {
33 | setLayout(new BorderLayout(0, 0));
34 |
35 | JPanel ipPanel = new JPanel(new BorderLayout(5, 5));
36 | final JTextField ipField = new JTextField();
37 | ipField.setText("127.0.0.1");
38 | ipPanel.add(ipField, BorderLayout.CENTER);
39 | ipPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
40 |
41 | JPanel portPanel = new JPanel(new BorderLayout(5, 5));
42 | final JTextField portField = new JTextField();
43 | portPanel.add(portField, BorderLayout.CENTER);
44 | portPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
45 | portField.setText("8888");
46 |
47 | JPanel btnPanel = new JPanel(new BorderLayout(5, 5));
48 | JButton btn = new JButton("链接");
49 | btnPanel.add(btn, BorderLayout.NORTH);
50 |
51 | tips = new JLabel();
52 | tips.setBorder(new EmptyBorder(0, 8, 0, 0));
53 | tips.setText("清晰度 100%");
54 | btnPanel.add(tips, BorderLayout.CENTER);
55 |
56 |
57 | JSlider jSlider = createSlider();
58 | btnPanel.add(jSlider, BorderLayout.SOUTH);
59 |
60 | JPanel panelContainer = new JPanel(new BorderLayout());
61 | panelContainer.add(ipPanel, BorderLayout.NORTH);
62 | panelContainer.add(portPanel, BorderLayout.CENTER);
63 | panelContainer.add(btnPanel, BorderLayout.SOUTH);
64 |
65 |
66 | JPanel panelContainer2 = new JPanel(new BorderLayout());
67 | panelContainer2.add(panelContainer, BorderLayout.NORTH);
68 |
69 | label = new JLabel();
70 |
71 | label.setBorder(new EmptyBorder(5, 5, 5, 5));
72 | add(panelContainer2, BorderLayout.NORTH);
73 |
74 | add(label, BorderLayout.CENTER);
75 |
76 | add(createTableBar(), BorderLayout.SOUTH);
77 |
78 | setDefaultCloseOperation(EXIT_ON_CLOSE);
79 | setBounds(360, 20, 350, 600);
80 |
81 | setTitle("屏幕共享");
82 | btn.addMouseListener(new MouseAdapter() {
83 | @Override
84 | public void mouseClicked(MouseEvent e) {
85 | try {
86 | read(ipField.getText(), portField.getText());
87 | } catch (IOException e1) {
88 | e1.printStackTrace();
89 | }
90 | }
91 |
92 | });
93 |
94 |
95 | label.addMouseListener(new MouseAdapter() {
96 | @Override
97 | public void mouseClicked(MouseEvent mouseEvent) {
98 | super.mouseClicked(mouseEvent);
99 | int x = mouseEvent.getX();
100 | int y = mouseEvent.getY();
101 | try {
102 | writer.write("DOWN" + (x * 1.0f / label.getWidth()) + "#" + (y * 1.0f / label.getHeight()));
103 | writer.newLine();
104 | writer.write("UP" + (x * 1.0f / label.getWidth()) + "#" + (y * 1.0f / label.getHeight()));
105 | writer.newLine();
106 | writer.flush();
107 | } catch (Exception e) {
108 |
109 | }
110 | }
111 |
112 | @Override
113 | public void mouseReleased(MouseEvent mouseEvent) {
114 | super.mouseReleased(mouseEvent);
115 | try {
116 | int x = mouseEvent.getX();
117 | int y = mouseEvent.getY();
118 | writer.write("UP" + (x * 1.0f / label.getWidth()) + "#" + (y * 1.0f / label.getHeight()));
119 | writer.newLine();
120 | writer.flush();
121 | isMove = false;
122 | } catch (Exception e) {
123 |
124 | }
125 | }
126 | });
127 | label.addMouseMotionListener(new MouseAdapter() {
128 | @Override
129 | public void mouseDragged(MouseEvent mouseEvent) {
130 | super.mouseDragged(mouseEvent);
131 | try {
132 | int x = mouseEvent.getX();
133 | int y = mouseEvent.getY();
134 | if (!isMove) {
135 | isMove = true;
136 |
137 | writer.write("DOWN" + (x * 1.0f / label.getWidth()) + "#" + (y * 1.0f / label.getHeight()));
138 | } else {
139 |
140 | writer.write("MOVE" + (x * 1.0f / label.getWidth()) + "#" + (y * 1.0f / label.getHeight()));
141 | }
142 | writer.newLine();
143 | writer.flush();
144 | } catch (Exception e) {
145 |
146 | }
147 | }
148 |
149 |
150 | });
151 |
152 |
153 | }
154 |
155 | private JSlider createSlider() {
156 | int minimum = 30;
157 | int maximum = 100;
158 | JSlider slider = new JSlider(minimum, maximum, maximum);
159 |
160 | slider.addChangeListener(new ChangeListener() {
161 | @Override
162 | public void stateChanged(ChangeEvent changeEvent) {
163 | try {
164 | int v = ((JSlider) changeEvent.getSource()).getValue();
165 | tips.setText("清晰度 " + v + "%");
166 | writer.write("DEGREE" + v);
167 | writer.newLine();
168 | writer.flush();
169 | } catch (Exception e) {
170 | e.printStackTrace();
171 | }
172 |
173 | }
174 | });
175 |
176 | return slider;
177 | }
178 |
179 | private JPanel createTableBar() {
180 | JPanel bar = new JPanel(new BorderLayout());
181 | JButton menu = new JButton("menu");
182 | JButton home = new JButton("home");
183 | JButton back = new JButton("back");
184 |
185 | bar.add(menu, BorderLayout.WEST);
186 | bar.add(home, BorderLayout.CENTER);
187 | bar.add(back, BorderLayout.EAST);
188 |
189 | menu.addMouseListener(new MouseAdapter() {
190 | @Override
191 | public void mouseClicked(MouseEvent mouseEvent) {
192 | super.mouseClicked(mouseEvent);
193 | try {
194 | writer.write("MENU");
195 | writer.newLine();
196 | writer.flush();
197 | } catch (Exception e) {
198 | e.printStackTrace();
199 | }
200 | }
201 | });
202 | home.addMouseListener(new MouseAdapter() {
203 | @Override
204 | public void mouseClicked(MouseEvent mouseEvent) {
205 | super.mouseClicked(mouseEvent);
206 | try {
207 | writer.write("HOME");
208 | writer.newLine();
209 | writer.flush();
210 | } catch (Exception e) {
211 | e.printStackTrace();
212 | }
213 | }
214 | });
215 | back.addMouseListener(new MouseAdapter() {
216 | @Override
217 | public void mouseClicked(MouseEvent mouseEvent) {
218 | super.mouseClicked(mouseEvent);
219 | try {
220 | writer.write("BACK");
221 | writer.newLine();
222 | writer.flush();
223 | } catch (Exception e) {
224 | e.printStackTrace();
225 | }
226 | }
227 | });
228 | return bar;
229 |
230 | }
231 |
232 | BufferedWriter writer;
233 |
234 | private void read(final String ip, final String port) throws IOException {
235 | new Thread() {
236 | @Override
237 | public void run() {
238 | super.run();
239 | try {
240 | Socket socket = new Socket(ip, Integer.parseInt(port));
241 | BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream());
242 | writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
243 | byte[] bytes = null;
244 | while (true) {
245 | long s1 = System.currentTimeMillis();
246 | int version = inputStream.read();
247 | if (version == -1) {
248 | return;
249 | }
250 | int length = readInt(inputStream);
251 | if (bytes == null) {
252 | bytes = new byte[length];
253 | }
254 | if (bytes.length < length) {
255 | bytes = new byte[length];
256 | }
257 | int read = 0;
258 | while ((read < length)) {
259 | read += inputStream.read(bytes, read, length - read);
260 | }
261 | InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
262 | long s2 = System.currentTimeMillis();
263 | Image image = ImageIO.read(byteArrayInputStream);
264 | label.setIcon(new ScaleIcon(new ImageIcon(image)));
265 | long s3 = System.currentTimeMillis();
266 |
267 | }
268 |
269 | } catch (IOException e) {
270 | e.printStackTrace();
271 | }
272 | }
273 | }.start();
274 |
275 | }
276 |
277 | private int readInt(InputStream inputStream) throws IOException {
278 | int b1 = inputStream.read();
279 | int b2 = inputStream.read();
280 | int b3 = inputStream.read();
281 | int b4 = inputStream.read();
282 |
283 | return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
284 | }
285 |
286 | public static void main(String[] args) throws IOException {
287 | new Client().setVisible(true);
288 |
289 | }
290 | }
291 |
292 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/client/Install.java:
--------------------------------------------------------------------------------
1 | package com.client;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.BufferedWriter;
5 | import java.io.IOException;
6 | import java.io.InputStream;
7 | import java.io.InputStreamReader;
8 | import java.io.OutputStreamWriter;
9 | import java.util.Arrays;
10 |
11 | /**
12 | * Created by wanjian on 2017/4/5.
13 | */
14 |
15 | public class Install {
16 |
17 | public static void main(String[] args) {
18 | install();
19 | }
20 |
21 | public static void install() {
22 |
23 |
24 | adbCommond("push Main.dex /sdcard/Main.dex");
25 |
26 |
27 | String path = "export CLASSPATH=/sdcard/Main.dex";
28 | String app = "exec app_process /sdcard com.wanjian.puppet.Main";
29 |
30 | shellCommond(new String[]{path, app});
31 | }
32 |
33 | private static void adbCommond(String com) {
34 | System.out.println("adbCommond...."+com);
35 | commond("sh", "./adb " + com);
36 | }
37 |
38 | private static void shellCommond(String[] com) {
39 | System.out.println("shell commond..."+ Arrays.toString(com));
40 | try {
41 | Process process = Runtime
42 | .getRuntime()
43 | .exec("./adb shell "); // adb
44 | // shell
45 | final BufferedWriter outputStream = new BufferedWriter(
46 | new OutputStreamWriter(process.getOutputStream()));
47 |
48 |
49 | for (String s : com) {
50 | outputStream.write(s);
51 | outputStream.write("\n");
52 | }
53 |
54 | outputStream.flush();
55 | System.out.println("shell write finished...");
56 | readError(process.getErrorStream());
57 | adbCommond("forward tcp:8888 localabstract:puppet-ver1");
58 | readResult(process.getInputStream());
59 |
60 |
61 | while (true) {
62 | Thread.sleep(Integer.MAX_VALUE);
63 | }
64 | } catch (Exception e) {
65 | e.printStackTrace();
66 | }
67 | }
68 |
69 | private static void readError(final InputStream errorStream) {
70 | new Thread(){
71 | @Override
72 | public void run() {
73 | super.run();
74 | readResult(errorStream);
75 | }
76 | }.start();
77 | }
78 |
79 |
80 | ///////////////
81 |
82 |
83 | private static void commond(String c, String com) {
84 | System.out.println("---> " + c + com);
85 | try {
86 | Process process = Runtime
87 | .getRuntime()
88 | .exec(c); // adb
89 | final BufferedWriter outputStream = new BufferedWriter(
90 | new OutputStreamWriter(process.getOutputStream()));
91 |
92 |
93 | outputStream.write(com);
94 | outputStream.write("\n");
95 | outputStream.write("exit\n");
96 | outputStream.flush();
97 |
98 | int i = process.waitFor();
99 | readResult(process.getInputStream());
100 |
101 |
102 | System.out.println("------END-------");
103 | } catch (Exception e) {
104 | e.printStackTrace();
105 | }
106 | }
107 |
108 |
109 | private static void readResult(final InputStream stream) {
110 |
111 | System.out.println("read result.....");
112 | try {
113 | String line;
114 | final BufferedReader reader = new BufferedReader(
115 | new InputStreamReader(stream));
116 |
117 | while ((line = reader.readLine()) != null) {
118 | System.out.println(line);
119 | }
120 | System.out.println("-------END------");
121 | } catch (IOException e) {
122 | e.printStackTrace();
123 | try {
124 | stream.close();
125 | } catch (Exception e1) {
126 | e1.printStackTrace();
127 | }
128 | }
129 | }
130 |
131 |
132 | }
133 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/client/ScaleIcon.java:
--------------------------------------------------------------------------------
1 | package com.client;
2 |
3 | import java.awt.Component;
4 | import java.awt.Graphics;
5 | import java.awt.Graphics2D;
6 | import java.awt.RenderingHints;
7 | import java.awt.image.BufferedImage;
8 |
9 | import javax.swing.Icon;
10 |
11 | /**
12 | * Created by wanjian on 2016/11/20.
13 | */
14 |
15 | public class ScaleIcon implements Icon {
16 |
17 | private BufferedImage i = null;
18 | private Icon icon = null;
19 |
20 | public ScaleIcon(Icon icon) {
21 | this.icon = icon;
22 | }
23 |
24 | @Override
25 | public int getIconHeight() {
26 | return icon.getIconHeight();
27 | }
28 |
29 | @Override
30 | public int getIconWidth() {
31 | return icon.getIconWidth();
32 | }
33 |
34 | public void paintIcon(Component c, Graphics g, int x, int y) {
35 | float wid = c.getWidth();
36 | float hei = c.getHeight();
37 | int iconWid = icon.getIconWidth();
38 | int iconHei = icon.getIconHeight();
39 |
40 | Graphics2D g2d = (Graphics2D) g;
41 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
42 | g2d.scale(wid / iconWid, hei / iconHei);
43 | icon.paintIcon(c, g2d, 0, 0);
44 | }
45 |
46 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':lib', ':shareandcontrollib'
2 |
--------------------------------------------------------------------------------
/shareandcontrollib/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/shareandcontrollib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 | buildToolsVersion "28.0.3"
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 28
10 | versionCode 1
11 | versionName "1.0"
12 |
13 |
14 | }
15 |
16 | }
17 |
18 | dependencies {
19 | compile fileTree(dir: 'libs', include: ['*.jar'])
20 | compile 'com.android.support:appcompat-v7:28.0.0'
21 | }
22 |
--------------------------------------------------------------------------------
/shareandcontrollib/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/wanjian/Library/Android/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/hardware/display/IDisplayManager.java:
--------------------------------------------------------------------------------
1 | package android.hardware.display;
2 |
3 | import android.os.IBinder;
4 | import android.view.DisplayInfo;
5 |
6 | /**
7 | * Created by wanjian on 2017/4/4.
8 | */
9 |
10 | public interface IDisplayManager {
11 | DisplayInfo getDisplayInfo(int i);
12 |
13 | abstract class Stub {
14 | public static IDisplayManager asInterface(IBinder invoke) {
15 | return null;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/os/ServiceManager.java:
--------------------------------------------------------------------------------
1 | package android.os;
2 |
3 | public class ServiceManager {
4 | public static IBinder getService(String s) {
5 | return null;
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/view/DisplayInfo.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | /**
4 | * Created by wanjian on 2017/4/4.
5 | */
6 |
7 | public final class DisplayInfo {
8 |
9 | public int logicalWidth;
10 | public int logicalHeight;
11 | }
12 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/view/IRotationWatcher.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | /**
4 | * Created by wanjian on 2017/4/4.
5 | */
6 |
7 | public interface IRotationWatcher {
8 | }
9 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/view/IWindowManager.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.graphics.Point;
4 | import android.os.IBinder;
5 |
6 | /**
7 | * Created by wanjian on 2017/4/4.
8 | */
9 |
10 | public interface IWindowManager {
11 | void getInitialDisplaySize(int i, Point displaySize);
12 |
13 | // int getRotation();
14 |
15 | void getRealDisplaySize(Point displaySize);
16 |
17 | abstract class Stub {
18 |
19 | public static IWindowManager asInterface(IBinder invoke) {
20 | return null;
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/view/Surface.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by wanjian on 2017/4/5.
7 | */
8 |
9 | public class Surface {
10 | public static Bitmap screenshot(int x, int y) {
11 | return null;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/android/view/SurfaceControl.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Rect;
5 |
6 | /**
7 | * Created by wanjian on 2017/4/5.
8 | */
9 |
10 | public class SurfaceControl {
11 | public static Bitmap screenshot(int x, int y) {
12 | return null;
13 | }
14 |
15 | public static Bitmap screenshot(Rect sourceCrop, int width, int height, int rotation) {
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/com/wanjian/puppet/Main.java:
--------------------------------------------------------------------------------
1 | package com.wanjian.puppet;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Point;
5 | import android.net.LocalServerSocket;
6 | import android.net.LocalSocket;
7 |
8 | import java.io.BufferedOutputStream;
9 | import java.io.BufferedReader;
10 | import java.io.ByteArrayOutputStream;
11 | import java.io.IOException;
12 | import java.io.InputStreamReader;
13 | import java.io.OutputStream;
14 |
15 | import static com.wanjian.puppet.ScreenUtils.screenshot;
16 | import static com.wanjian.puppet.TouchUtils.back;
17 | import static com.wanjian.puppet.TouchUtils.home;
18 | import static com.wanjian.puppet.TouchUtils.menu;
19 | import static com.wanjian.puppet.TouchUtils.touchDown;
20 | import static com.wanjian.puppet.TouchUtils.touchMove;
21 | import static com.wanjian.puppet.TouchUtils.touchUp;
22 |
23 | /**
24 | * Created by wanjian on 2017/4/4.
25 | */
26 |
27 | public class Main {
28 |
29 |
30 | private static float scale = 1;
31 |
32 | public static void main(String[] args) throws Exception {
33 |
34 | System.out.println(">>>>>start>>>>>");
35 |
36 | Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
37 | @Override
38 | public void uncaughtException(Thread t, Throwable e) {
39 | e.printStackTrace(System.out);
40 | }
41 | });
42 |
43 | LocalServerSocket serverSocket = new LocalServerSocket("puppet-ver1");
44 |
45 |
46 | while (true) {
47 | System.out.println(">>>>> listen");
48 | try {
49 | LocalSocket socket = serverSocket.accept();
50 | System.out.println(">>>>> accepted");
51 | read(socket);
52 | write(socket);
53 | } catch (Exception e) {
54 | e.printStackTrace(System.out);
55 | serverSocket = new LocalServerSocket("puppet-ver1");
56 | }
57 | }
58 | }
59 |
60 |
61 | private static void write(final LocalSocket socket) {
62 | new Thread() {
63 | @Override
64 | public void run() {
65 | super.run();
66 | try {
67 | final int VERSION = 2;
68 | BufferedOutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
69 | while (true) {
70 | Bitmap bitmap = screenshot();
71 | bitmap = Bitmap.createScaledBitmap(bitmap, (int) (ScreenUtils.getDisplaySize().x * scale),
72 | (int) (ScreenUtils.getDisplaySize().y * scale), true);
73 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
74 | bitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
75 |
76 | outputStream.write(VERSION);
77 | writeInt(outputStream, byteArrayOutputStream.size());
78 | outputStream.write(byteArrayOutputStream.toByteArray());
79 | outputStream.flush();
80 | }
81 | } catch (Throwable e) {
82 | throw new RuntimeException(e);
83 | }
84 | }
85 | }.start();
86 | }
87 |
88 | private static void read(final LocalSocket socket) {
89 |
90 | new Thread() {
91 | private final String DOWN = "DOWN";
92 | private final String MOVE = "MOVE";
93 | private final String UP = "UP";
94 |
95 | private final String MENU = "MENU";
96 | private final String HOME = "HOME";
97 | private final String BACK = "BACK";
98 |
99 | private final String DEGREE = "DEGREE";
100 |
101 | @Override
102 | public void run() {
103 | super.run();
104 | try {
105 | BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
106 | while (true) {
107 | String line = reader.readLine();
108 | if (line == null) {
109 | System.out.println("socket closed....");
110 | return;
111 | }
112 | if (line.startsWith(DOWN)) {
113 | handlerDown(line.substring(DOWN.length()));
114 | } else if (line.startsWith(MOVE)) {
115 | handlerMove(line.substring(MOVE.length()));
116 | } else if (line.startsWith(UP)) {
117 | handlerUp(line.substring(UP.length()));
118 | } else if (line.startsWith(MENU)) {
119 | menu();
120 | } else if (line.startsWith(HOME)) {
121 | home();
122 | } else if (line.startsWith(BACK)) {
123 | back();
124 | } else if (line.startsWith(DEGREE)) {
125 | scale = Float.parseFloat(line.substring(DEGREE.length())) / 100;
126 | }
127 | }
128 | } catch (Exception e) {
129 | e.printStackTrace(System.out);
130 | }
131 | }
132 | }.start();
133 | }
134 |
135 | private static void handlerUp(String line) {
136 | Point point = getXY(line);
137 | if (point != null) {
138 | touchUp(point.x, point.y);
139 | }
140 | }
141 |
142 | private static void handlerMove(String line) {
143 | Point point = getXY(line);
144 | if (point != null) {
145 | touchMove(point.x, point.y);
146 | }
147 | }
148 |
149 | private static void handlerDown(String line) {
150 | Point point = getXY(line);
151 | if (point != null) {
152 | touchDown(point.x, point.y);
153 | }
154 | }
155 |
156 |
157 | private static Point getXY(String nums) {
158 | try {
159 | Point point = ScreenUtils.getDisplaySize();
160 | String[] s = nums.split("#");
161 | float scaleX = Float.parseFloat(s[0]);
162 | float scaleY = Float.parseFloat(s[1]);
163 | point.x = (int) (point.x * scaleX);
164 | point.y = (int) (point.y * scaleY);
165 | return point;
166 | } catch (Exception e) {
167 | e.printStackTrace(System.out);
168 | }
169 | return null;
170 | }
171 |
172 |
173 | private static void writeInt(OutputStream outputStream, int v) throws IOException {
174 | outputStream.write(v >> 24);
175 | outputStream.write(v >> 16);
176 | outputStream.write(v >> 8);
177 | outputStream.write(v);
178 | }
179 |
180 |
181 | }
182 |
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/com/wanjian/puppet/ScreenUtils.java:
--------------------------------------------------------------------------------
1 | package com.wanjian.puppet;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Point;
5 | import android.graphics.Rect;
6 | import android.hardware.display.IDisplayManager;
7 | import android.os.Build;
8 | import android.os.Build.VERSION;
9 | import android.os.IBinder;
10 | import android.os.ServiceManager;
11 | import android.view.DisplayInfo;
12 | import android.view.IWindowManager;
13 | import android.view.IWindowManager.Stub;
14 | import android.view.SurfaceControl;
15 |
16 | public class ScreenUtils {
17 |
18 | public static Point getDisplaySize() {
19 | return new Point(sDisplaySize.x, sDisplaySize.y);
20 | }
21 |
22 | private static final Point sDisplaySize = new Point();
23 |
24 | static {
25 | if (VERSION.SDK_INT >= 18) {
26 | IWindowManager wm = Stub.asInterface((IBinder) ServiceManager.getService("window"));
27 | wm.getInitialDisplaySize(0, sDisplaySize);
28 | } else if (VERSION.SDK_INT == 17) {
29 | DisplayInfo di =
30 | IDisplayManager.Stub.asInterface((IBinder) ServiceManager.getService("display")).getDisplayInfo(0);
31 | sDisplaySize.x = di.logicalWidth;
32 | sDisplaySize.y = di.logicalHeight;
33 | } else {
34 | IWindowManager wm = Stub.asInterface((IBinder) ServiceManager.getService("window"));
35 | wm.getRealDisplaySize(sDisplaySize);
36 | }
37 | }
38 |
39 | public static Bitmap screenshot() throws Exception {
40 | Point size = ScreenUtils.getDisplaySize();
41 | Bitmap b;
42 | if (Build.VERSION.SDK_INT <= 17) {
43 | String surfaceClassName = "android.view.Surface";
44 | b = (Bitmap) Class.forName(surfaceClassName).getDeclaredMethod("screenshot", new Class[] {int.class, int.class})
45 | .invoke(null, new Object[] {size.x, size.y});
46 |
47 | } else if (Build.VERSION.SDK_INT < 28) {
48 | b = SurfaceControl.screenshot(size.x, size.y);
49 | } else {
50 | b = SurfaceControl.screenshot(new Rect(0, 0, size.x, size.y), size.x, size.y, 0);
51 | }
52 | return b;
53 | }
54 | }
--------------------------------------------------------------------------------
/shareandcontrollib/src/main/java/com/wanjian/puppet/TouchUtils.java:
--------------------------------------------------------------------------------
1 | package com.wanjian.puppet;
2 |
3 | import android.hardware.input.InputManager;
4 | import android.os.SystemClock;
5 | import android.support.v4.view.InputDeviceCompat;
6 | import android.view.InputEvent;
7 | import android.view.KeyEvent;
8 | import android.view.MotionEvent;
9 |
10 | import java.lang.reflect.Method;
11 |
12 | class TouchUtils {
13 |
14 | static InputManager sIm;
15 | static Method sInjectInputEventMethod;
16 | static long downTime;
17 |
18 |
19 | static {
20 |
21 | try {
22 | sIm = (InputManager) InputManager.class.getDeclaredMethod("getInstance", new Class[0]).invoke(null, new Object[0]);
23 | MotionEvent.class.getDeclaredMethod("obtain", new Class[0]).setAccessible(true);
24 | sInjectInputEventMethod =
25 | InputManager.class.getMethod("injectInputEvent", new Class[] {InputEvent.class, Integer.TYPE});
26 |
27 | } catch (Exception e) {
28 | throw new RuntimeException(e);
29 | }
30 | }
31 |
32 |
33 | static void touchUp(float clientX, float clientY) {
34 | injectMotionEvent(InputDeviceCompat.SOURCE_TOUCHSCREEN, KeyEvent.ACTION_UP, downTime,
35 | SystemClock.uptimeMillis(), clientX, clientY, 1.0f);
36 | }
37 |
38 | static void touchMove(float clientX, float clientY) {
39 | injectMotionEvent(InputDeviceCompat.SOURCE_TOUCHSCREEN, 2, downTime,
40 | SystemClock.uptimeMillis(), clientX, clientY, 1.0f);
41 | }
42 |
43 | static void touchDown(float clientX, float clientY) {
44 | downTime = SystemClock.uptimeMillis();
45 | injectMotionEvent(InputDeviceCompat.SOURCE_TOUCHSCREEN, KeyEvent.ACTION_DOWN, downTime, downTime, clientX,
46 | clientY, 1.0f);
47 |
48 | }
49 |
50 | static void menu() {
51 | sendKeyEvent(InputDeviceCompat.SOURCE_KEYBOARD, KeyEvent.KEYCODE_MENU);
52 | }
53 |
54 | static void back() {
55 | sendKeyEvent(InputDeviceCompat.SOURCE_KEYBOARD, KeyEvent.KEYCODE_BACK);
56 | }
57 |
58 |
59 | static void home() {
60 | sendKeyEvent(InputDeviceCompat.SOURCE_KEYBOARD, KeyEvent.KEYCODE_HOME);
61 | }
62 |
63 |
64 | static void injectMotionEvent(int inputSource, int action,
65 | long downTime, long eventTime, float x, float y, float pressure) {
66 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, 1.0f, 0, 1.0f, 1.0f, 0, 0);
67 | event.setSource(inputSource);
68 | try {
69 | sInjectInputEventMethod.invoke(sIm, new Object[] {event, 0});
70 | } catch (Exception e) {
71 | e.printStackTrace(System.out);
72 | }
73 | }
74 |
75 |
76 | static void sendKeyEvent(int inputSource, int keyCode) {
77 | long now = SystemClock.uptimeMillis();
78 | injectKeyEvent(new KeyEvent(now, now, 0, keyCode, 0, 0, -1, 0, 0, inputSource));
79 | injectKeyEvent(new KeyEvent(now, now, 1, keyCode, 0, 0, -1, 0, 0, inputSource));
80 | }
81 |
82 | static void injectKeyEvent(KeyEvent event) {
83 | try {
84 | sInjectInputEventMethod.invoke(sIm, new Object[] {event, 0});
85 | } catch (Exception e) {
86 | e.printStackTrace(System.out);
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/效果视频.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/android-notes/androidScreenShare/5912848952f5fa0ed402b6d42965e7c1735ae201/效果视频.mp4
--------------------------------------------------------------------------------