16
16
17
17
package com .google .devtools .mobileharness .platform .android .shared .emulator ;
18
18
19
+ import com .google .common .collect .ImmutableList ;
20
+ import com .google .devtools .mobileharness .shared .util .flags .Flags ;
21
+ import java .util .ArrayList ;
22
+ import java .util .List ;
23
+ import java .util .regex .Matcher ;
24
+ import java .util .regex .Pattern ;
25
+
19
26
/** Utility class for Android JIT emulator. */
20
27
public final class AndroidJitEmulatorUtil {
21
28
22
29
private AndroidJitEmulatorUtil () {}
23
30
31
+ // Acloud creates cuttlefish emulator with port 6520 + index, where index is instance id minus
32
+ // one. Example: "acloud create --local-instance 1" will create a local emulator with port 6520.
33
+ // And instance 2 will be 6521, so on and so forth.
24
34
public static final int EMULATOR_BASE_PORT = 6520 ;
25
35
public static final int EMULATOR_INSTANCE_ID_BASE = 1 ;
36
+ public static final Pattern LOCAL_NOOP_EMULATOR_ID_PATTERN =
37
+ Pattern .compile ("^([\\ w\\ .]+):local-virtual-device-(\\ d+)$" );
38
+ public static final String TF_GLOBAL_CONFIG_PATH = "/mtt/scripts/host-config.xml" ;
26
39
27
40
public static String getAcloudInstanceId (String deviceId ) {
28
41
int instanceIndex = getInstanceIndex (deviceId );
@@ -33,14 +46,51 @@ public static String getAcloudInstanceId(String deviceId) {
33
46
}
34
47
35
48
public static String getVirtualDeviceNameInTradefed (String deviceId ) {
36
- int instanceIndex = getInstanceIndex (deviceId );
37
- if (instanceIndex < 0 ) {
49
+ if (!Flags .instance ().virtualDeviceServerIp .getNonNull ().isEmpty ()
50
+ && !Flags .instance ().virtualDeviceServerUsername .getNonNull ().isEmpty ()) {
51
+ return deviceId ;
52
+ }
53
+ Matcher matcher = LOCAL_NOOP_EMULATOR_ID_PATTERN .matcher (deviceId );
54
+ if (matcher .find ()) {
55
+ return String .format ("local-virtual-device-%s" , matcher .group (2 ));
56
+ } else {
38
57
return "" ;
39
58
}
40
- return String .format ("local-virtual-device-%s" , String .valueOf (instanceIndex ));
41
59
}
42
60
43
61
private static int getInstanceIndex (String deviceId ) {
44
62
return Integer .parseInt (deviceId .substring (deviceId .indexOf (':' ) + 1 )) - EMULATOR_BASE_PORT ;
45
63
}
64
+
65
+ public static ImmutableList <String > getAllVirtualDeviceIds () {
66
+ int emulatorNumber = Flags .instance ().androidJitEmulatorNum .getNonNull ();
67
+ List <String > emulatorIds = new ArrayList <>();
68
+
69
+ if (Flags .instance ().noopJitEmulator .getNonNull ()) {
70
+ if (!Flags .instance ().virtualDeviceServerIp .getNonNull ().isEmpty ()
71
+ && !Flags .instance ().virtualDeviceServerUsername .getNonNull ().isEmpty ()) {
72
+ for (int i = 0 ; i < emulatorNumber ; i ++) {
73
+ emulatorIds .add (
74
+ String .format (
75
+ "gce-device-%s-%d-%s" ,
76
+ Flags .instance ().virtualDeviceServerIp .getNonNull (),
77
+ i ,
78
+ Flags .instance ().virtualDeviceServerUsername .getNonNull ()));
79
+ }
80
+ } else {
81
+ for (int i = 0 ; i < emulatorNumber ; i ++) {
82
+ emulatorIds .add (String .format ("0.0.0.0:local-virtual-device-%d" , i ));
83
+ }
84
+ }
85
+ } else {
86
+ for (int i = 0 ; i < emulatorNumber ; i ++) {
87
+ emulatorIds .add (String .format ("0.0.0.0:%d" , EMULATOR_BASE_PORT + i ));
88
+ }
89
+ }
90
+ return ImmutableList .copyOf (emulatorIds );
91
+ }
92
+
93
+ public static String getHostConfigPath () {
94
+ return TF_GLOBAL_CONFIG_PATH ;
95
+ }
46
96
}
0 commit comments