1
1
package com .genymobile .scrcpy ;
2
2
3
- import com .genymobile .scrcpy .wrappers .ContentProvider ;
4
3
import com .genymobile .scrcpy .wrappers .ServiceManager ;
5
4
5
+ import android .os .Parcel ;
6
+ import android .os .Parcelable ;
7
+ import android .util .Base64 ;
8
+
6
9
import java .io .File ;
7
10
import java .io .IOException ;
8
11
@@ -15,22 +18,123 @@ public final class CleanUp {
15
18
16
19
public static final String SERVER_PATH = "/data/local/tmp/scrcpy-server.jar" ;
17
20
21
+ // A simple struct to be passed from the main process to the cleanup process
22
+ public static class Config implements Parcelable {
23
+
24
+ public static final Creator <Config > CREATOR = new Creator <Config >() {
25
+ @ Override
26
+ public Config createFromParcel (Parcel in ) {
27
+ return new Config (in );
28
+ }
29
+
30
+ @ Override
31
+ public Config [] newArray (int size ) {
32
+ return new Config [size ];
33
+ }
34
+ };
35
+
36
+ private static final int FLAG_DISABLE_SHOW_TOUCHES = 1 ;
37
+ private static final int FLAG_RESTORE_NORMAL_POWER_MODE = 2 ;
38
+ private static final int FLAG_POWER_OFF_SCREEN = 4 ;
39
+
40
+ private int displayId ;
41
+
42
+ // Restore the value (between 0 and 7), -1 to not restore
43
+ // <https://developer.android.com/reference/android/provider/Settings.Global#STAY_ON_WHILE_PLUGGED_IN>
44
+ private int restoreStayOn = -1 ;
45
+
46
+ private boolean disableShowTouches ;
47
+ private boolean restoreNormalPowerMode ;
48
+ private boolean powerOffScreen ;
49
+
50
+ public Config () {
51
+ // Default constructor, the fields are initialized by CleanUp.configure()
52
+ }
53
+
54
+ protected Config (Parcel in ) {
55
+ displayId = in .readInt ();
56
+ restoreStayOn = in .readInt ();
57
+ byte options = in .readByte ();
58
+ disableShowTouches = (options & FLAG_DISABLE_SHOW_TOUCHES ) != 0 ;
59
+ restoreNormalPowerMode = (options & FLAG_RESTORE_NORMAL_POWER_MODE ) != 0 ;
60
+ powerOffScreen = (options & FLAG_POWER_OFF_SCREEN ) != 0 ;
61
+ }
62
+
63
+ @ Override
64
+ public void writeToParcel (Parcel dest , int flags ) {
65
+ dest .writeInt (displayId );
66
+ dest .writeInt (restoreStayOn );
67
+ byte options = 0 ;
68
+ if (disableShowTouches ) {
69
+ options |= FLAG_DISABLE_SHOW_TOUCHES ;
70
+ }
71
+ if (restoreNormalPowerMode ) {
72
+ options |= FLAG_RESTORE_NORMAL_POWER_MODE ;
73
+ }
74
+ if (powerOffScreen ) {
75
+ options |= FLAG_POWER_OFF_SCREEN ;
76
+ }
77
+ dest .writeByte (options );
78
+ }
79
+
80
+ private boolean hasWork () {
81
+ return disableShowTouches || restoreStayOn != -1 || restoreNormalPowerMode || powerOffScreen ;
82
+ }
83
+
84
+ @ Override
85
+ public int describeContents () {
86
+ return 0 ;
87
+ }
88
+
89
+ byte [] serialize () {
90
+ Parcel parcel = Parcel .obtain ();
91
+ writeToParcel (parcel , 0 );
92
+ byte [] bytes = parcel .marshall ();
93
+ parcel .recycle ();
94
+ return bytes ;
95
+ }
96
+
97
+ static Config deserialize (byte [] bytes ) {
98
+ Parcel parcel = Parcel .obtain ();
99
+ parcel .unmarshall (bytes , 0 , bytes .length );
100
+ parcel .setDataPosition (0 );
101
+ return CREATOR .createFromParcel (parcel );
102
+ }
103
+
104
+ static Config fromBase64 (String base64 ) {
105
+ byte [] bytes = Base64 .decode (base64 , Base64 .NO_WRAP );
106
+ return deserialize (bytes );
107
+ }
108
+
109
+ String toBase64 () {
110
+ byte [] bytes = serialize ();
111
+ return Base64 .encodeToString (bytes , Base64 .NO_WRAP );
112
+ }
113
+ }
114
+
18
115
private CleanUp () {
19
116
// not instantiable
20
117
}
21
118
22
- public static void configure (boolean disableShowTouches , int restoreStayOn ) throws IOException {
23
- boolean needProcess = disableShowTouches || restoreStayOn != -1 ;
24
- if (needProcess ) {
25
- startProcess (disableShowTouches , restoreStayOn );
119
+ public static void configure (int displayId , int restoreStayOn , boolean disableShowTouches , boolean restoreNormalPowerMode , boolean powerOffScreen )
120
+ throws IOException {
121
+ Config config = new Config ();
122
+ config .displayId = displayId ;
123
+ config .disableShowTouches = disableShowTouches ;
124
+ config .restoreStayOn = restoreStayOn ;
125
+ config .restoreNormalPowerMode = restoreNormalPowerMode ;
126
+ config .powerOffScreen = powerOffScreen ;
127
+
128
+ if (config .hasWork ()) {
129
+ startProcess (config );
26
130
} else {
27
131
// There is no additional clean up to do when scrcpy dies
28
132
unlinkSelf ();
29
133
}
30
134
}
31
135
32
- private static void startProcess (boolean disableShowTouches , int restoreStayOn ) throws IOException {
33
- String [] cmd = {"app_process" , "/" , CleanUp .class .getName (), String . valueOf ( disableShowTouches ), String . valueOf ( restoreStayOn )};
136
+ private static void startProcess (Config config ) throws IOException {
137
+ String [] cmd = {"app_process" , "/" , CleanUp .class .getName (), config . toBase64 ( )};
34
138
35
139
ProcessBuilder builder = new ProcessBuilder (cmd );
36
140
builder .environment ().put ("CLASSPATH" , SERVER_PATH );
@@ -57,21 +161,37 @@ public static void main(String... args) {
57
161
58
162
Ln .i ("Cleaning up" );
59
163
60
- boolean disableShowTouches = Boolean .parseBoolean (args [0 ]);
61
- int restoreStayOn = Integer .parseInt (args [1 ]);
164
+ Config config = Config .fromBase64 (args [0 ]);
62
165
63
- if (disableShowTouches || restoreStayOn != -1 ) {
166
+ if (config . disableShowTouches || config . restoreStayOn != -1 ) {
64
167
ServiceManager serviceManager = new ServiceManager ();
65
- try (ContentProvider settings = serviceManager .getActivityManager ().createSettingsProvider ()) {
66
- if (disableShowTouches ) {
67
- Ln .i ("Disabling \" show touches\" " );
68
- settings .putValue (ContentProvider .TABLE_SYSTEM , "show_touches" , "0" );
168
+ Settings settings = new Settings (serviceManager );
169
+ if (config .disableShowTouches ) {
170
+ Ln .i ("Disabling \" show touches\" " );
171
+ try {
172
+ settings .putValue (Settings .TABLE_SYSTEM , "show_touches" , "0" );
173
+ } catch (SettingsException e ) {
174
+ Ln .e ("Could not restore \" show_touches\" " , e );
69
175
}
70
- if (restoreStayOn != -1 ) {
71
- Ln .i ("Restoring \" stay awake\" " );
72
- settings .putValue (ContentProvider .TABLE_GLOBAL , "stay_on_while_plugged_in" , String .valueOf (restoreStayOn ));
176
+ }
177
+ if (config .restoreStayOn != -1 ) {
178
+ Ln .i ("Restoring \" stay awake\" " );
179
+ try {
180
+ settings .putValue (Settings .TABLE_GLOBAL , "stay_on_while_plugged_in" , String .valueOf (config .restoreStayOn ));
181
+ } catch (SettingsException e ) {
182
+ Ln .e ("Could not restore \" stay_on_while_plugged_in\" " , e );
73
183
}
74
184
}
75
185
}
186
+
187
+ if (Device .isScreenOn ()) {
188
+ if (config .powerOffScreen ) {
189
+ Ln .i ("Power off screen" );
190
+ Device .powerOffScreen (config .displayId );
191
+ } else if (config .restoreNormalPowerMode ) {
192
+ Ln .i ("Restoring normal power mode" );
193
+ Device .setScreenPowerMode (Device .POWER_MODE_NORMAL );
194
+ }
195
+ }
76
196
}
77
197
}
0 commit comments