FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing-android/src/processing/mode/android/Devices.java at master · jwill/processing-android · GitHub
jwill
/
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
/
Devices.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
354 lines (307 loc) · 10.8 KB
Breadcrumbs
processing-android
/
src
/
processing
/
mode
/
android
/
Devices.java
Copy path
File metadata and controls
354 lines (307 loc) · 10.8 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-16 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
processing
.
mode
.
android
;
import
processing
.
app
.
exec
.
ProcessResult
;
import
processing
.
mode
.
android
.
EmulatorController
.
State
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.*;
//import processing.app.EditorConsole;
/**
* <pre> AndroidEnvironment env = AndroidEnvironment.getInstance();
* AndroidDevice n1 = env.getHardware();
* AndroidDevice emu = env.getEmulator();</pre>
* @author Jonathan Feinberg <jdf@pobox.com>
*
*/
class
Devices
{
private
static
final
String
ADB_DEVICES_ERROR
=
"Received unfamiliar output from “adb devices”.
\n
"
+
"The device list may have errors."
;
private
static
final
Devices
INSTANCE
=
new
Devices
();
private
static
final
String
BT_DEBUG_PORT
=
"4444"
;
private
Device
selectedDevice
;
public
static
Devices
getInstance
() {
return
INSTANCE
;
}
private
final
Map
<
String
,
Device
>
devices
=
new
ConcurrentHashMap
<
String
,
Device
>();
private
final
ExecutorService
deviceLaunchThread
=
Executors
.
newSingleThreadExecutor
();
public
Device
getSelectedDevice
() {
return
selectedDevice
;
}
public
void
setSelectedDevice
(
Device
selectedDevice
) {
this
.
selectedDevice
=
selectedDevice
;
}
public
static
void
killAdbServer
() {
System
.
out
.
println
(
"Shutting down any existing adb server..."
);
System
.
out
.
flush
();
try
{
AndroidSDK
.
runADB
(
"kill-server"
);
}
catch
(
final
Exception
e
) {
System
.
err
.
println
(
"Devices.killAdbServer() failed."
);
e
.
printStackTrace
();
}
}
public
static
void
enableBlueToothDebugging
() {
try
{
// Enable debugging over bluetooth
// http://developer.android.com/training/wearables/apps/bt-debugging.html
AndroidSDK
.
runADB
(
"forward"
,
"tcp:"
+
BT_DEBUG_PORT
,
"localabstract:/adb-hub"
);
AndroidSDK
.
runADB
(
"connect"
,
"127.0.0.1:"
+
BT_DEBUG_PORT
);
}
catch
(
final
Exception
e
) {
e
.
printStackTrace
();
}
}
private
Devices
() {
if
(
processing
.
app
.
Base
.
DEBUG
) {
System
.
out
.
println
(
"Starting up Devices"
);
}
// killAdbServer();
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(
"processing.mode.android.Devices Shutdown"
) {
public
void
run
() {
//System.out.println("Shutting down Devices");
//System.out.flush();
for
(
Device
device
:
new
ArrayList
<
Device
>(
devices
.
values
())) {
device
.
shutdown
();
}
// Don't do this, it'll just make Eclipse and others freak out.
//killAdbServer();
}
});
}
public
Future
<
Device
>
getEmulator
(
final
boolean
wear
,
final
boolean
gpu
) {
final
Callable
<
Device
>
androidFinder
=
new
Callable
<
Device
>() {
public
Device
call
()
throws
Exception
{
return
blockingGetEmulator
(
wear
,
gpu
);
}
};
final
FutureTask
<
Device
>
task
=
new
FutureTask
<
Device
>(
androidFinder
);
deviceLaunchThread
.
execute
(
task
);
return
task
;
}
private
final
Device
blockingGetEmulator
(
final
boolean
wear
,
final
boolean
gpu
) {
// System.out.println("going looking for emulator");
String
port
=
AVD
.
getPort
(
wear
);
Device
emu
=
find
(
true
,
port
);
if
(
emu
!=
null
) {
// System.out.println("found emu " + emu);
return
emu
;
}
// System.out.println("no emu found");
EmulatorController
emuController
=
EmulatorController
.
getInstance
(
wear
);
// System.out.println("checking emulator state");
if
(
emuController
.
getState
() ==
State
.
NOT_RUNNING
) {
try
{
// System.out.println("not running, gonna launch");
emuController
.
launch
(
wear
,
gpu
);
// this blocks until emulator boots
// System.out.println("not just gonna, we've done the launch");
}
catch
(
final
IOException
e
) {
System
.
err
.
println
(
"Problem while launching emulator."
);
e
.
printStackTrace
(
System
.
err
);
return
null
;
}
}
else
{
System
.
out
.
println
(
"Emulator is "
+
emuController
.
getState
() +
", which is not expected."
);
}
// System.out.println("and now we're out");
// System.out.println("Devices.blockingGet thread is " + Thread.currentThread());
while
(!
Thread
.
currentThread
().
isInterrupted
()) {
// System.err.println("AndroidEnvironment: looking for emulator in loop.");
// System.err.println("AndroidEnvironment: emulatorcontroller state is "
// + emuController.getState());
if
(
emuController
.
getState
() ==
State
.
NOT_RUNNING
) {
System
.
err
.
println
(
"Error while starting the emulator. ("
+
emuController
.
getState
() +
")"
);
return
null
;
}
emu
=
find
(
true
,
port
);
if
(
emu
!=
null
) {
// System.err.println("AndroidEnvironment: returning " + emu.getId()
// + " from loop.");
return
emu
;
}
try
{
Thread
.
sleep
(
2000
);
}
catch
(
final
InterruptedException
e
) {
System
.
err
.
println
(
"Devices: interrupted in loop."
);
return
null
;
}
}
return
null
;
}
private
Device
find
(
final
boolean
wantEmulator
,
final
String
port
) {
refresh
();
synchronized
(
devices
) {
for
(
final
Device
device
:
devices
.
values
()) {
if
(
port
!=
null
&&
device
.
getName
().
indexOf
(
port
) == -
1
)
continue
;
final
boolean
isEmulator
=
device
.
getId
().
contains
(
"emulator"
);
if
((
isEmulator
&&
wantEmulator
) || (!
isEmulator
&& !
wantEmulator
)) {
return
device
;
}
}
}
return
null
;
}
public
List
<
Device
>
findMultiple
(
final
boolean
wantEmulator
) {
List
<
Device
>
deviceList
=
new
ArrayList
<
Device
>();
refresh
();
synchronized
(
devices
) {
for
(
final
Device
device
:
devices
.
values
()) {
final
boolean
isEmulator
=
device
.
getId
().
contains
(
"emulator"
);
if
((
isEmulator
&&
wantEmulator
) || (!
isEmulator
&& !
wantEmulator
)) {
deviceList
.
add
(
device
);
}
}
}
return
deviceList
;
}
/**
* @return the first Android hardware device known to be running, or null if there are none.
*/
public
Future
<
Device
>
getHardware
() {
Device
device
=
getSelectedDevice
();
if
(
device
==
null
|| !
device
.
isAlive
())
device
=
blockingGetHardware
();
return
getHardware
(
device
);
}
public
Future
<
Device
>
getHardware
(
final
Device
device
) {
final
Callable
<
Device
>
androidFinder
=
new
Callable
<
Device
>() {
public
Device
call
()
throws
Exception
{
return
device
;
}
};
final
FutureTask
<
Device
>
task
=
new
FutureTask
<
Device
>(
androidFinder
);
deviceLaunchThread
.
execute
(
task
);
return
task
;
}
private
final
Device
blockingGetHardware
() {
Device
hardware
=
find
(
false
,
null
);
if
(
hardware
!=
null
) {
return
hardware
;
}
while
(!
Thread
.
currentThread
().
isInterrupted
()) {
try
{
Thread
.
sleep
(
2000
);
}
catch
(
final
InterruptedException
e
) {
return
null
;
}
hardware
=
find
(
false
,
null
);
if
(
hardware
!=
null
) {
return
hardware
;
}
}
return
null
;
}
private
void
refresh
() {
final
List
<
String
>
activeDevices
=
list
();
for
(
final
String
deviceId
:
activeDevices
) {
if
(!
devices
.
containsKey
(
deviceId
)) {
addDevice
(
new
Device
(
this
,
deviceId
));
}
}
}
private
void
addDevice
(
final
Device
device
) {
// System.err.println("AndroidEnvironment: adding " + device.getId());
try
{
device
.
initialize
();
if
(
devices
.
put
(
device
.
getId
(),
device
) !=
null
) {
throw
new
IllegalStateException
(
"Adding "
+
device
+
", which already exists!"
);
}
}
catch
(
final
Exception
e
) {
System
.
err
.
println
(
"While initializing "
+
device
.
getId
() +
": "
+
e
);
}
}
void
deviceRemoved
(
final
Device
device
) {
// System.err.println("AndroidEnvironment: removing " + device.getId());
if
(
devices
.
remove
(
device
.
getId
()) ==
null
) {
throw
new
IllegalStateException
(
"I didn't know about device "
+
device
.
getId
() +
"!"
);
}
}
/**
* <p>First line starts "List of devices"
*
* <p>When an emulator is started with a debug port, then it shows up
* in the list of devices.
*
* <p>List of devices attached
* <br>HT91MLC00031 device
* <br>emulator-5554 offline
*
* <p>List of devices attached
* <br>HT91MLC00031 device
* <br>emulator-5554 device
*
* @return list of device identifiers
* @throws IOException
*/
public
static
List
<
String
>
list
() {
if
(
AndroidSDK
.
adbDisabled
) {
return
Collections
.
emptyList
();
}
ProcessResult
result
;
try
{
// System.out.println("listing devices 00");
result
=
AndroidSDK
.
runADB
(
"devices"
);
// System.out.println("listing devices 05");
}
catch
(
InterruptedException
e
) {
return
Collections
.
emptyList
();
}
catch
(
IOException
e
) {
System
.
err
.
println
(
"Problem inside Devices.list()"
);
e
.
printStackTrace
();
// System.err.println(e);
// System.err.println("checking devices");
// e.printStackTrace(EditorConsole.systemErr);
return
Collections
.
emptyList
();
}
// System.out.println("listing devices 10");
if
(!
result
.
succeeded
()) {
if
(
result
.
getStderr
().
contains
(
"protocol fault (no status)"
)) {
System
.
err
.
println
(
"bleh: "
+
result
);
// THIS IS WORKING
}
else
{
System
.
err
.
println
(
"nope: "
+
result
);
}
return
Collections
.
emptyList
();
}
// System.out.println("listing devices 20");
// might read "List of devices attached"
final
String
stdout
=
result
.
getStdout
();
if
(!(
stdout
.
contains
(
"List of devices"
) ||
stdout
.
trim
().
length
() ==
0
)) {
System
.
err
.
println
(
ADB_DEVICES_ERROR
);
System
.
err
.
println
(
"Output was “"
+
stdout
+
"”"
);
return
Collections
.
emptyList
();
}
// System.out.println("listing devices 30");
final
List
<
String
>
devices
=
new
ArrayList
<
String
>();
for
(
final
String
line
:
result
) {
if
(
line
.
contains
(
"
\t
"
)) {
final
String
[]
fields
=
line
.
split
(
"
\t
"
);
if
(
fields
[
1
].
equals
(
"device"
)) {
devices
.
add
(
fields
[
0
]);
}
}
}
return
devices
;
}
}
Back
|
FazBrowse Home
|
New Git URL