1# Copyright 2018 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Module for the Sl4aService.""" 15 16from mobly.controllers.android_device_lib import sl4a_client 17from mobly.controllers.android_device_lib.services import base_service 18 19 20class Sl4aService(base_service.BaseService): 21 """Service for managing sl4a's client. 22 23 Direct calls on the service object will forwarded to the client object as 24 syntactic sugar. So `Sl4aService.doFoo()` is equivalent to 25 `Sl4aClient.doFoo()`. 26 """ 27 28 def __init__(self, device, configs=None): 29 del configs # Never used. 30 self._ad = device 31 self._sl4a_client = None 32 33 @property 34 def is_alive(self): 35 return self._sl4a_client is not None 36 37 def start(self): 38 self._sl4a_client = sl4a_client.Sl4aClient(ad=self._ad) 39 self._sl4a_client.start_app_and_connect() 40 41 def stop(self): 42 if self.is_alive: 43 self._sl4a_client.stop_app() 44 self._sl4a_client = None 45 46 def pause(self): 47 # Need to stop dispatcher because it continuously polls the device. 48 # It's not necessary to stop the sl4a client. 49 self._sl4a_client.stop_event_dispatcher() 50 self._sl4a_client.clear_host_port() 51 52 def resume(self): 53 # Restore sl4a if needed. 54 self._sl4a_client.restore_app_connection() 55 56 def __getattr__(self, name): 57 """Forwards the getattr calls to the client itself.""" 58 if self._sl4a_client: 59 return getattr(self._sl4a_client, name) 60 return self.__getattribute__(name) 61