1#!/usr/bin/env python3.4
2#
3# Copyright 2016 Google Inc.
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"""Tool to interactively call sl4a methods.
17
18SL4A (Scripting Layer for Android) is an RPC service exposing API calls on
19Android.
20
21Original version: https://github.com/damonkohler/sl4a
22
23Fork in AOSP (can make direct system privileged calls):
24https://android.googlesource.com/platform/external/sl4a/
25
26Also allows access to Event Dispatcher, which allows waiting for asynchronous
27actions. For more information see the Mobly codelab:
28https://github.com/google/mobly#event-dispatcher
29
30Usage:
31$ sl4a_shell
32>>> s.getBuildID()
33u'N2F52'
34"""
35
36import argparse
37import logging
38
39from mobly.controllers.android_device_lib import jsonrpc_shell_base
40from mobly.controllers.android_device_lib.services import sl4a_service
41
42
43class Sl4aShell(jsonrpc_shell_base.JsonRpcShellBase):
44
45  def _start_services(self, console_env):
46    """Overrides superclass."""
47    self._ad.services.register('sl4a', sl4a_service.Sl4aService)
48    console_env['s'] = self._ad.services.sl4a
49    console_env['sl4a'] = self._ad.sl4a
50    console_env['ed'] = self._ad.ed
51
52  def _get_banner(self, serial):
53    lines = [
54        'Connected to %s.' % serial,
55        'Call methods against:',
56        '    ad (android_device.AndroidDevice)',
57        '    sl4a or s (SL4A)',
58        '    ed (EventDispatcher)',
59    ]
60    return '\n'.join(lines)
61
62
63if __name__ == '__main__':
64  parser = argparse.ArgumentParser(description='Interactive client for sl4a.')
65  parser.add_argument(
66      '-s',
67      '--serial',
68      help='Device serial to connect to (if more than one device is connected)',
69  )
70  args = parser.parse_args()
71  logging.basicConfig(level=logging.INFO)
72  Sl4aShell().main(args.serial)
73