blob: 4c444a212eba3e339da584c5341c118fc072c48a [file] [log] [blame]
Alan Viverette7d87cea2017-09-26 13:39:21 -04001#!/usr/bin/python3
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the 'License');
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an 'AS IS' BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import functools
19import math
20import socket
21import subprocess
22import sys
23import tempfile
24
25from android_device import *
26
27
28def find_free_port():
29 s = socket.socket()
30 s.bind(('', 0))
31 return int(s.getsockname()[1])
32
33
34class AVD(object):
35 def __init__(self, name, emu_path):
36 self._name = name
37 self._emu_path = emu_path
38 self._opts = ''
39 self._adb_name = None
40 self._emu_proc = None
41
42 def start(self):
43 if self._emu_proc:
44 raise Exception('Emulator already running')
45
46 port_adb = find_free_port()
47 port_tty = find_free_port()
48 # -no-window might be useful here
49 emu_cmd = "%s -avd %s %s-ports %d,%d" \
50 % (self._emu_path, self._name, self._opts, port_adb, port_tty)
51 print(emu_cmd)
52
53 emu_proc = subprocess.Popen(emu_cmd.split(" "), bufsize=-1, stdout=subprocess.PIPE,
54 stderr=subprocess.PIPE)
55
56 # The emulator ought to be starting now.
57 self._adb_name = "emulator-%d" % (port_tty - 1)
58 self._emu_proc = emu_proc
59
60 def stop(self):
61 if not self._emu_proc:
62 raise Exception('Emulator not currently running')
63 self._emu_proc.kill()
64 (out, err) = self._emu_proc.communicate()
65 self._emu_proc = None
66 return out, err
67
68 def get_serial(self):
69 if not self._emu_proc:
70 raise Exception('Emulator not currently running')
71 return self._adb_name
72
73 def get_device(self):
74 if not self._emu_proc:
75 raise Exception('Emulator not currently running')
76 return AndroidDevice(self._adb_name)
77
78 def configure_screen(self, density, width_dp, height_dp):
79 width_px = int(math.ceil(width_dp * density / 1600) * 10)
80 height_px = int(math.ceil(height_dp * density / 1600) * 10)
81 self._opts = "-prop qemu.sf.lcd_density=%d -skin %dx%d " % (density, width_px, height_px)