FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing-android/src/processing/mode/android/Device.java at master · pkdevboxy/processing-android · GitHub
pkdevboxy
/
processing-android
Public
forked from
processing/processing-android
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing-android
/
src
/
processing
/
mode
/
android
/
Device.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
340 lines (297 loc) · 10.4 KB
Breadcrumbs
processing-android
/
src
/
processing
/
mode
/
android
/
Device.java
Copy path
File metadata and controls
340 lines (297 loc) · 10.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package
processing
.
mode
.
android
;
import
processing
.
app
.
Base
;
import
processing
.
app
.
RunnerListener
;
import
processing
.
app
.
exec
.
LineProcessor
;
import
processing
.
app
.
exec
.
ProcessRegistry
;
import
processing
.
app
.
exec
.
ProcessResult
;
import
processing
.
app
.
exec
.
StreamPump
;
import
processing
.
core
.
PApplet
;
import
processing
.
mode
.
android
.
LogEntry
.
Severity
;
import
java
.
io
.
IOException
;
import
java
.
util
.*;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
class
Device
{
private
final
Devices
env
;
private
final
String
id
;
private
final
Set
<
Integer
>
activeProcesses
=
new
HashSet
<
Integer
>();
private
final
Set
<
DeviceListener
>
listeners
=
Collections
.
synchronizedSet
(
new
HashSet
<
DeviceListener
>());
// public static final String APP_STARTED = "android.device.app.started";
// public static final String APP_ENDED = "android.device.app.ended";
// mutable state
private
Process
logcat
;
public
Device
(
final
Devices
env
,
final
String
id
) {
this
.
env
=
env
;
this
.
id
=
id
;
}
public
void
bringLauncherToFront
() {
try
{
adb
(
"shell"
,
"am"
,
"start"
,
"-a"
,
"android.intent.action.MAIN"
,
"-c"
,
"android.intent.category.HOME"
);
}
catch
(
final
Exception
e
) {
e
.
printStackTrace
(
System
.
err
);
}
}
public
String
getName
() {
String
name
=
""
;
try
{
ProcessResult
result
=
AndroidSDK
.
runADB
(
"-s"
,
id
,
"shell"
,
"getprop"
,
"ro.product.brand"
);
if
(
result
.
succeeded
()) {
name
+=
result
.
getStdout
() +
" "
;
}
result
=
AndroidSDK
.
runADB
(
"-s"
,
id
,
"shell"
,
"getprop"
,
"ro.product.model"
);
if
(
result
.
succeeded
()) {
name
+=
result
.
getStdout
();
}
}
catch
(
InterruptedException
e
) {
e
.
printStackTrace
();
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
return
name
+
" ["
+
id
+
"]"
;
}
// adb -s emulator-5556 install helloWorld.apk
// : adb -s HT91MLC00031 install bin/Brightness-debug.apk
// 532 KB/s (190588 bytes in 0.349s)
// pkg: /data/local/tmp/Brightness-debug.apk
// Failure [INSTALL_FAILED_ALREADY_EXISTS]
// : adb -s HT91MLC00031 install -r bin/Brightness-debug.apk
// 1151 KB/s (190588 bytes in 0.161s)
// pkg: /data/local/tmp/Brightness-debug.apk
// Success
// safe to just always include the -r (reinstall) flag
public
boolean
installApp
(
final
AndroidBuild
build
,
final
RunnerListener
status
) {
if
(!
isAlive
()) {
return
false
;
}
bringLauncherToFront
();
try
{
final
ProcessResult
installResult
=
adb
(
"install"
,
"-r"
,
build
.
getPathForAPK
());
if
(!
installResult
.
succeeded
()) {
status
.
statusError
(
"Could not install the sketch."
);
System
.
err
.
println
(
installResult
);
return
false
;
}
String
errorMsg
=
null
;
for
(
final
String
line
:
installResult
) {
if
(
line
.
startsWith
(
"Failure"
)) {
errorMsg
=
line
.
substring
(
8
);
if
(
line
.
contains
(
"INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES"
)) {
boolean
removeResult
=
removeApp
(
build
.
getPackageName
());
if
(
removeResult
)
return
installApp
(
build
,
status
);
}
System
.
err
.
println
(
line
);
}
}
if
(
errorMsg
==
null
) {
status
.
statusNotice
(
"Done installing."
);
return
true
;
}
status
.
statusError
(
"Error while installing "
+
errorMsg
);
}
catch
(
final
IOException
e
) {
status
.
statusError
(
e
);
}
catch
(
final
InterruptedException
e
) {
}
return
false
;
}
public
boolean
removeApp
(
String
packageName
)
throws
IOException
,
InterruptedException
{
final
ProcessResult
removeResult
=
adb
(
"uninstall"
,
packageName
);
if
(!
removeResult
.
succeeded
()) {
return
false
;
}
return
true
;
}
// different version that actually runs through JDI:
// http://asantoso.wordpress.com/2009/09/26/using-jdb-with-adb-to-debugging-of-android-app-on-a-real-device/
public
boolean
launchApp
(
final
String
packageName
)
throws
IOException
,
InterruptedException
{
if
(!
isAlive
()) {
return
false
;
}
String
[]
cmd
= {
"shell"
,
"am"
,
"start"
,
"-e"
,
"debug"
,
"true"
,
"-a"
,
"android.intent.action.MAIN"
,
"-c"
,
"android.intent.category.LAUNCHER"
,
"-n"
,
packageName
+
"/.MainActivity"
};
// PApplet.println(cmd);
ProcessResult
pr
=
adb
(
cmd
);
if
(
Base
.
DEBUG
) {
System
.
out
.
println
(
pr
.
toString
());
}
// Sometimes this shows up on stdout, even though it returns 'success'
// Error type 2
// android.util.AndroidException: Can't connect to activity manager; is the system running?
if
(
pr
.
getStdout
().
contains
(
"android.util.AndroidException"
)) {
System
.
err
.
println
(
pr
.
getStdout
());
return
false
;
}
return
pr
.
succeeded
();
}
public
boolean
isEmulator
() {
return
id
.
startsWith
(
"emulator"
);
}
// I/Process ( 9213): Sending signal. PID: 9213 SIG: 9
private
static
final
Pattern
SIG
=
Pattern
.
compile
(
"PID:
\\
s+(
\\
d+)
\\
s+SIG:
\\
s+(
\\
d+)"
);
private
final
List
<
String
>
stackTrace
=
new
ArrayList
<
String
>();
private
class
LogLineProcessor
implements
LineProcessor
{
public
void
processLine
(
final
String
line
) {
final
LogEntry
entry
=
new
LogEntry
(
line
);
if
(
entry
.
message
.
startsWith
(
"PROCESSING"
)) {
if
(
entry
.
message
.
contains
(
"onStart"
)) {
startProc
(
entry
.
source
,
entry
.
pid
);
}
else
if
(
entry
.
message
.
contains
(
"onStop"
)) {
endProc
(
entry
.
pid
);
}
}
else
if
(
entry
.
source
.
equals
(
"Process"
)) {
handleCrash
(
entry
);
}
else
if
(
activeProcesses
.
contains
(
entry
.
pid
)) {
handleConsole
(
entry
);
}
}
private
void
handleCrash
(
final
LogEntry
entry
) {
final
Matcher
m
=
SIG
.
matcher
(
entry
.
message
);
if
(
m
.
find
()) {
final
int
pid
=
Integer
.
parseInt
(
m
.
group
(
1
));
final
int
signal
=
Integer
.
parseInt
(
m
.
group
(
2
));
if
(
activeProcesses
.
contains
(
pid
)) {
// only report crashes of *our* sketches, por favor
/*
* A crashed sketch first gets a signal 3, which causes the
* "you've crashed" dialog to appear on the device. After
* the user dismisses the dialog, a sig 9 is sent.
* TODO: is it possible to forcibly dismiss the crash dialog?
*/
if
(
signal
==
3
) {
endProc
(
pid
);
reportStackTrace
(
entry
);
}
}
}
}
private
void
handleConsole
(
final
LogEntry
entry
) {
final
boolean
isStackTrace
=
entry
.
source
.
equals
(
"AndroidRuntime"
)
&&
entry
.
severity
==
Severity
.
Error
;
if
(
isStackTrace
) {
if
(!
entry
.
message
.
startsWith
(
"Uncaught handler"
)) {
stackTrace
.
add
(
entry
.
message
);
System
.
err
.
println
(
entry
.
message
);
}
}
else
if
(
entry
.
source
.
equals
(
"System.out"
)
||
entry
.
source
.
equals
(
"System.err"
)) {
if
(
entry
.
severity
.
useErrorStream
) {
System
.
err
.
println
(
entry
.
message
);
}
else
{
System
.
out
.
println
(
entry
.
message
);
}
}
}
}
private
void
reportStackTrace
(
final
LogEntry
entry
) {
if
(
stackTrace
.
isEmpty
()) {
System
.
err
.
println
(
"That's weird. Proc "
+
entry
.
pid
+
" got signal 3, but there's no stack trace."
);
}
final
List
<
String
>
stackCopy
=
Collections
.
unmodifiableList
(
new
ArrayList
<
String
>(
stackTrace
));
for
(
final
DeviceListener
listener
:
listeners
) {
listener
.
stackTrace
(
stackCopy
);
}
stackTrace
.
clear
();
}
void
initialize
()
throws
IOException
,
InterruptedException
{
adb
(
"logcat"
,
"-c"
);
final
String
[]
cmd
=
generateAdbCommand
(
"logcat"
);
final
String
title
=
PApplet
.
join
(
cmd
,
' '
);
logcat
=
Runtime
.
getRuntime
().
exec
(
cmd
);
ProcessRegistry
.
watch
(
logcat
);
new
StreamPump
(
logcat
.
getInputStream
(),
"log: "
+
title
).
addTarget
(
new
LogLineProcessor
()).
start
();
new
StreamPump
(
logcat
.
getErrorStream
(),
"err: "
+
title
).
addTarget
(
System
.
err
).
start
();
new
Thread
(
new
Runnable
() {
public
void
run
() {
try
{
logcat
.
waitFor
();
// final int result = logcat.waitFor();
// System.err.println("AndroidDevice: " + getId() + " logcat exited "
// + (result == 0 ? "normally" : "with status " + result));
}
catch
(
final
InterruptedException
e
) {
System
.
err
.
println
(
"AndroidDevice: logcat process monitor interrupted"
);
}
finally
{
shutdown
();
}
}
},
"AndroidDevice: logcat process monitor"
).
start
();
// System.err.println("Receiving log entries from " + id);
}
synchronized
void
shutdown
() {
if
(!
isAlive
()) {
return
;
}
// System.err.println(id + " is shutting down.");
if
(
logcat
!=
null
) {
logcat
.
destroy
();
logcat
=
null
;
ProcessRegistry
.
unwatch
(
logcat
);
}
env
.
deviceRemoved
(
this
);
if
(
activeProcesses
.
size
() >
0
) {
for
(
final
DeviceListener
listener
:
listeners
) {
listener
.
sketchStopped
();
}
}
listeners
.
clear
();
}
synchronized
boolean
isAlive
() {
return
logcat
!=
null
;
}
public
String
getId
() {
return
id
;
}
public
Devices
getEnv
() {
return
env
;
}
private
void
startProc
(
final
String
name
,
final
int
pid
) {
// System.err.println("Process " + name + " started at pid " + pid);
activeProcesses
.
add
(
pid
);
}
private
void
endProc
(
final
int
pid
) {
// System.err.println("Process " + pid + " stopped.");
activeProcesses
.
remove
(
pid
);
for
(
final
DeviceListener
listener
:
listeners
) {
listener
.
sketchStopped
();
}
}
public
void
addListener
(
final
DeviceListener
listener
) {
listeners
.
add
(
listener
);
}
public
void
removeListener
(
final
DeviceListener
listener
) {
listeners
.
remove
(
listener
);
}
private
ProcessResult
adb
(
final
String
...
cmd
)
throws
InterruptedException
,
IOException
{
final
String
[]
adbCmd
=
generateAdbCommand
(
cmd
);
return
AndroidSDK
.
runADB
(
adbCmd
);
}
private
String
[]
generateAdbCommand
(
final
String
...
cmd
) {
// final String[] adbCmd = new String[3 + cmd.length];
// adbCmd[0] = "adb";
// adbCmd[1] = "-s";
// adbCmd[2] = getId();
// System.arraycopy(cmd, 0, adbCmd, 3, cmd.length);
// return adbCmd;
return
PApplet
.
concat
(
new
String
[] {
"adb"
,
"-s"
,
getId
() },
cmd
);
}
@
Override
public
String
toString
() {
return
"[AndroidDevice "
+
getId
() +
"]"
;
}
@
Override
public
boolean
equals
(
Object
obj
) {
return
((
Device
)
obj
).
getId
().
equals
(
getId
());
}
}
Back
|
FazBrowse Home
|
New Git URL