def _start_event_client(self): # Start an EventDispatcher for the current sl4a session event_client = Sl4aClient(self._adb, self.log) event_client.host_port = self.host_port event_client.device_port = self.device_port event_client.connect( uid=self.uid, cmd=jsonrpc_client_base.JsonRpcCommand.CONTINUE) ed = event_dispatcher.EventDispatcher(event_client) ed.start() return ed
def start_app_and_connect(self): """Overrides superclass.""" # Check that sl4a is installed out = self._adb.shell('pm list package') if not utils.grep('com.googlecode.android_scripting', out): raise AppStartError('%s is not installed on %s' % (_APP_NAME, self._adb.serial)) # sl4a has problems connecting after disconnection, so kill the apk and # try connecting again. try: self.stop_app() except Exception as e: self.log.warning(e) # Launch the app self.host_port = utils.get_available_host_port() self.device_port = _DEVICE_SIDE_PORT self._adb.forward( ['tcp:%d' % self.host_port, 'tcp:%d' % self.device_port]) self._adb.shell(_LAUNCH_CMD % self.device_port) # Connect with retry start_time = time.time() expiration_time = start_time + _APP_START_WAIT_TIME started = False while time.time() < expiration_time: self.log.debug('Attempting to start %s.', self.app_name) try: self.connect() started = True break except: self.log.debug('%s is not yet running, retrying', self.app_name, exc_info=True) time.sleep(1) if not started: raise jsonrpc_client_base.AppStartError( '%s failed to start on %s.' % (self.app_name, self._adb.serial)) # Start an EventDispatcher for the current sl4a session event_client = Sl4aClient(self._adb, self.log) event_client.host_port = self.host_port event_client.connect(uid=self.uid, cmd=jsonrpc_client_base.JsonRpcCommand.CONTINUE) self.ed = event_dispatcher.EventDispatcher(event_client) self.ed.start()
def get_dispatcher(self, droid): """Return an EventDispatcher for an sl4a session Args: droid: Session to create EventDispatcher for. Returns: ed: An EventDispatcher for specified session. """ ed_key = self.serial + str(droid.uid) if ed_key in self._event_dispatchers: if self._event_dispatchers[ed_key] is None: raise Error("EventDispatcher Key Empty") self.log.debug("Returning existing key %s for event dispatcher!", ed_key) return self._event_dispatchers[ed_key] event_droid = self.add_new_connection_to_session(droid.uid) ed = event_dispatcher.EventDispatcher(event_droid) self._event_dispatchers[ed_key] = ed return ed
def load_sl4a(self): """Start sl4a service on the Android device. Launch sl4a server if not already running, spin up a session on the server, and two connections to this session. Creates an sl4a client (self.sl4a) with one connection, and one EventDispatcher obj (self.ed) with the other connection. """ host_port = utils.get_available_host_port() self.sl4a = sl4a_client.Sl4aClient(host_port=host_port, adb_proxy=self.adb) self._start_jsonrpc_client(self.sl4a) # Start an EventDispatcher for the current sl4a session event_client = sl4a_client.Sl4aClient(host_port=host_port, adb_proxy=self.adb) event_client.connect(uid=self.sl4a.uid, cmd=jsonrpc_client_base.JsonRpcCommand.CONTINUE) self.ed = event_dispatcher.EventDispatcher(event_client) self.ed.start()