Module: Ruboto::Util::Emulator
- Includes:
- Verify
- Defined in:
- lib/ruboto/util/emulator.rb
Constant Summary
collapse
- ON_WINDOWS =
(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/i)
- ON_MAC_OS_X =
RbConfig::CONFIG['host_os'] =~ /^darwin/
- ON_LINUX =
RbConfig::CONFIG['host_os'] =~ /linux/
Constants included
from Verify
Verify::ON_TRAVIS
Instance Method Summary
collapse
-
#add_property(config, property_name, value) ⇒ Object
-
#create_avd(avd_home, avd_name, android_device, heap_size, sdk_level) ⇒ Object
-
#device_ready? ⇒ Boolean
-
#kill_all_emulators(emulator_cmd) ⇒ Object
-
#kill_emulator(telnet) ⇒ Object
-
#patch_config_ini(avd_home, heap_size, no_snapshot = nil) ⇒ Object
-
#read_property(config, property_name) ⇒ Object
-
#sdk_level_name(sdk_level) ⇒ Object
-
#start_emulator(sdk_level, no_snapshot) ⇒ Object
-
#update_heap_size(new_hw_config, heap_size) ⇒ Object
Methods included from Verify
#project_api_level, #project_properties, #save_manifest, #save_ruboto_config, #save_test_manifest, #verify_activity, #verify_api, #verify_manifest, #verify_min_sdk, #verify_package, #verify_project_properties, #verify_ruboto_config, #verify_sdk_versions, #verify_strings, #verify_target_sdk, #verify_test_manifest
Instance Method Details
#add_property(config, property_name, value) ⇒ Object
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/ruboto/util/emulator.rb', line 340
def add_property(config, property_name, value)
pattern = /^#{property_name}=(.*)$/
new_property = "#{property_name}=#{value}"
if (old_property = read_property(config, property_name))
if old_property != value
config.gsub! pattern, new_property
puts "Changed property: #{new_property} (was #{old_property.inspect})"
end
else
config << "#{new_property}\n"
puts "Added property: #{new_property}"
end
end
|
#create_avd(avd_home, avd_name, android_device, heap_size, sdk_level) ⇒ Object
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
|
# File 'lib/ruboto/util/emulator.rb', line 235
def create_avd(avd_home, avd_name, android_device, heap_size, sdk_level)
puts "Creating AVD #{avd_name}"
target = `android list target`.split(/----------\n/).
find { |l| l =~ /android-#{sdk_level}/ }
if target.nil?
puts "Target android-#{sdk_level} not found. You should run"
puts "\n ruboto setup -y -t #{sdk_level}\n\nto install it."
exit 3
end
abis = target.slice(/(?<=ABIs : ).*/).split(', ')
puts "Available abis: #{abis.inspect}"
has_x86 = abis.find { |a| a =~ /x86/ }
has_x86_64 = has_x86 && abis.find { |a| a =~ /x86_64/ }
avoid_x86 = ON_TRAVIS && sdk_level.to_i >= 22
if avoid_x86
abis.reject!{|a| a =~ /x86/}
has_x86_64 = nil
has_x86 = nil
end
if has_x86
if has_x86_64
abi_opt = "--abi #{has_x86_64}"
else
abi_opt = "--abi #{has_x86}"
end
else
abi_opt = "--abi #{abis.first || 'armeabi-v7a'}"
end
ruboto_config_filename = 'ruboto.yml'
if File.exists?(ruboto_config_filename)
ruboto_config = YAML.load_file(ruboto_config_filename)
skin = ruboto_config && ruboto_config['emulator'] && ruboto_config['emulator']['skin']
end
skin ||= '768x1280'
puts `echo no | android create avd #{'-a' unless avoid_x86} -n #{avd_name} -t android-#{sdk_level} #{abi_opt} -c 64M #{"-s #{skin}" if skin} -d "#{android_device}"`
if $? != 0
puts 'Failed to create AVD.'
exit 3
end
patch_config_ini(avd_home, heap_size)
hw_config_file_name = "#{avd_home}/hardware-qemu.ini"
if File.exists?(hw_config_file_name)
old_hw_config = File.read(hw_config_file_name)
new_hw_config = old_hw_config.dup
update_heap_size(new_hw_config, heap_size)
File.write(hw_config_file_name, new_hw_config) if new_hw_config != old_hw_config
end
end
|
#device_ready? ⇒ Boolean
330
331
332
|
# File 'lib/ruboto/util/emulator.rb', line 330
def device_ready?
`adb get-state 2>&1`.gsub(/^WARNING:.*$/, '').chomp == 'device'
end
|
#kill_all_emulators(emulator_cmd) ⇒ Object
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
|
# File 'lib/ruboto/util/emulator.rb', line 209
def kill_all_emulators(emulator_cmd)
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? == 0
`killall #{emulator_cmd}`
10.times do |i|
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? != 0
break
end
if i == 3
print 'Waiting for emulator to die: ...'
elsif i > 3
print '.'
end
sleep 1
end
puts
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? == 0
puts 'Emulator still running.'
`killall -9 #{emulator_cmd}`
sleep 1
end
end
end
|
#kill_emulator(telnet) ⇒ Object
201
202
203
204
205
206
207
|
# File 'lib/ruboto/util/emulator.rb', line 201
def kill_emulator(telnet)
begin
telnet.cmd('kill')
rescue Errno::ECONNRESET
puts "Emulator reset connection."
end
end
|
#patch_config_ini(avd_home, heap_size, no_snapshot = nil) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/ruboto/util/emulator.rb', line 313
def patch_config_ini(avd_home, heap_size, no_snapshot = nil)
avd_config_file_name = "#{avd_home}/config.ini"
old_avd_config = File.read(avd_config_file_name)
new_avd_config = old_avd_config.dup
update_heap_size(new_avd_config, heap_size)
add_property(new_avd_config, 'hw.mainKeys', 'no')
unless no_snapshot.nil?
add_property(new_avd_config, 'snapshot.present', (!no_snapshot).to_s)
end
File.write(avd_config_file_name, new_avd_config) if new_avd_config != old_avd_config
end
|
#read_property(config, property_name) ⇒ Object
334
335
336
337
338
|
# File 'lib/ruboto/util/emulator.rb', line 334
def read_property(config, property_name)
pattern = /^#{property_name}=(.*)$/
config =~ pattern
$1
end
|
#sdk_level_name(sdk_level) ⇒ Object
#start_emulator(sdk_level, no_snapshot) ⇒ Object
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
|
# File 'lib/ruboto/util/emulator.rb', line 18
def start_emulator(sdk_level, no_snapshot)
sdk_level = sdk_level.gsub(/^android-/, '')
STDOUT.sync = true
if RbConfig::CONFIG['host_cpu'] == 'x86_64'
if ON_MAC_OS_X
emulator_cmd = '-m "emulator64-(crash-service|arm|x86)|qemu-system-x86_64"'
elsif ON_LINUX
emulator_cmd = '-r "emulator64-(arm|x86)"'
else
emulator_cmd = 'emulator64-arm'
end
else
emulator_cmd = 'emulator-arm'
end
emulator_config = verify_ruboto_config['emulator']
avd_name = (emulator_config && emulator_config['name']) || "Android_#{sdk_level_name(sdk_level)}"
android_device = (emulator_config && emulator_config['device']) || "Nexus One"
new_snapshot = false
running_emulators = `adb devices`.scan(/emulator-(\d+)/)
if running_emulators.any?
desired_emulator_is_running = false
running_emulators.each do |port, _|
t = Net::Telnet.new('Host' => 'localhost', 'Port' => port, 'Prompt' => /^OK\n|^KO:/)
begin
intro = t.waitfor(/^OK\n/)
rescue Net::ReadTimeout
puts "Timeout waiting for emulator intro."
kill_emulator(t)
next
end
if %r{Android Console: you can find your <auth_token> in\s+'(?<auth_file>.*)'} =~ intro
auth_token = File.read auth_file
t.cmd("auth #{auth_token}")
end
output = t.cmd('avd name')
if output =~ /(.*)\nOK\n/
running_avd_name = $1
if running_avd_name == avd_name
if desired_emulator_is_running
puts "Stopping duplicate #{running_avd_name} emulator."
kill_emulator(t)
else
puts "Emulator #{avd_name} is already running."
desired_emulator_is_running = true
end
else
puts "Stopping #{running_avd_name} emulator."
kill_emulator(t)
end
else
puts "Unexpected response from emulator: #{output.inspect}"
puts 'Assuming wrong emulator is running.'
kill_emulator(t)
end
t.close
end
return if desired_emulator_is_running
else
puts 'No emulator is running.'
end
loop do
emulator_opts = '-partition-size 256'
emulator_opts << ' -noskin' unless android_device
emulator_opts << ' -no-snapshot' if no_snapshot
if !ON_MAC_OS_X && !ON_WINDOWS && ENV['DISPLAY'].nil?
emulator_opts << ' -no-window'
ENV['QEMU_AUDIO_DRV'] = 'none'
end
kill_all_emulators(emulator_cmd)
avd_home = "#{ENV['HOME'].gsub('\\', '/')}/.android/avd/#{avd_name}.avd"
manifest_file = 'AndroidManifest.xml'
large_heap = (!File.exists?(manifest_file)) || (File.read(manifest_file) =~ /largeHeap/)
heap_size = large_heap ? 256 : 64
if File.exists? avd_home
patch_config_ini(avd_home, heap_size, no_snapshot)
else
create_avd(avd_home, avd_name, android_device, heap_size, sdk_level)
new_snapshot = true
end
puts "Start emulator #{avd_name}#{' without snapshot' if no_snapshot}"
start_cmd = "emulator -avd #{avd_name} #{emulator_opts} #{'&' unless ON_WINDOWS}"
puts start_cmd
system start_cmd
return if ON_WINDOWS
3.times do |i|
sleep 1
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? == 0
break
end
if i == 3
print 'Waiting for emulator: ...'
elsif i > 3
print '.'
end
end
puts
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? != 0
puts 'Unable to start the emulator. Retrying without loading snapshot.'
system "emulator -no-snapshot -avd #{avd_name} #{emulator_opts} #{'&' unless ON_WINDOWS}"
10.times do |i|
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? == 0
new_snapshot = true
break
end
if i == 3
print 'Waiting for emulator: ...'
elsif i > 3
print '.'
end
sleep 1
end
end
`killall -0 #{emulator_cmd} 2> /dev/null`
if $? == 0
print 'Emulator started: '
60.times do
break if device_ready?
print '.'
sleep 1
end
puts
break if device_ready?
puts 'Emulator started, but failed to respond.'
unless no_snapshot
puts 'Retrying without loading snapshot.'
no_snapshot = true
end
else
puts 'Unable to start the emulator.'
exit 1 if ON_TRAVIS
end
end
if new_snapshot
puts 'Allow the emulator to calm down a bit.'
60.times do
break if `adb shell ps` =~ /android.process.acore/
print '.'
sleep 1
end
puts
end
system <<EOF
(
set +e
for i in {1..10} ; do
sleep 6
adb shell input keyevent 82 >/dev/null 2>&1
if [ "$?" = "0" ] ; then
set -e
adb shell input keyevent 82 >/dev/null 2>&1
adb shell input keyevent 4 >/dev/null 2>&1
exit 0
fi
done
echo "Failed to unlock screen"
set -e
exit 1
) &
EOF
system 'adb logcat > adb_logcat.log &'
puts "Emulator #{avd_name} started OK."
end
|
#update_heap_size(new_hw_config, heap_size) ⇒ Object
306
307
308
309
310
311
|
# File 'lib/ruboto/util/emulator.rb', line 306
def update_heap_size(new_hw_config, heap_size)
old_heap = read_property(new_hw_config, 'vm.heapSize')
if old_heap.nil? || old_heap.to_i < heap_size
add_property(new_hw_config, 'vm.heapSize', heap_size)
end
end
|