Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 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. |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 16 | """Tool to interactively call sl4a methods. |
| 17 | |
| 18 | SL4A (Scripting Layer for Android) is an RPC service exposing API calls on |
| 19 | Android. |
| 20 | |
| 21 | Original version: https://github.com/damonkohler/sl4a |
| 22 | |
| 23 | Fork in AOSP (can make direct system privileged calls): |
| 24 | https://android.googlesource.com/platform/external/sl4a/ |
| 25 | |
| 26 | Also allows access to Event Dispatcher, which allows waiting for asynchronous |
| 27 | actions. For more information see the Mobly codelab: |
| 28 | https://github.com/google/mobly#event-dispatcher |
| 29 | |
| 30 | Usage: |
Alexander Dorokhine | 6e7a636 | 2016-12-13 15:10:27 -0800 | [diff] [blame] | 31 | $ sl4a_shell |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 32 | >>> s.getBuildID() |
| 33 | u'N2F52' |
| 34 | """ |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 35 | |
| 36 | import argparse |
Alexander Dorokhine | efa488e | 2017-05-25 18:14:47 -0700 | [diff] [blame] | 37 | import logging |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 38 | |
Alexander Dorokhine | 401771b | 2016-12-14 18:26:35 -0800 | [diff] [blame] | 39 | from mobly.controllers.android_device_lib import jsonrpc_shell_base |
Ang Li | 9ffe882 | 2018-10-03 21:35:14 -0700 | [diff] [blame] | 40 | from mobly.controllers.android_device_lib.services import sl4a_service |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 41 | |
| 42 | |
Alexander Dorokhine | 401771b | 2016-12-14 18:26:35 -0800 | [diff] [blame] | 43 | class Sl4aShell(jsonrpc_shell_base.JsonRpcShellBase): |
Ang Li | b7c2324 | 2021-01-20 10:47:27 +0800 | [diff] [blame] | 44 | |
Eric Lin (Tzu Hsiang Lin) | 11d0f08 | 2021-01-12 21:21:37 +0800 | [diff] [blame] | 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 |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 51 | |
Eric Lin (Tzu Hsiang Lin) | 11d0f08 | 2021-01-12 21:21:37 +0800 | [diff] [blame] | 52 | def _get_banner(self, serial): |
| 53 | lines = [ |
nkprasad12 | ebdec3a | 2023-10-26 17:29:55 -0700 | [diff] [blame] | 54 | 'Connected to %s.' % serial, |
| 55 | 'Call methods against:', |
| 56 | ' ad (android_device.AndroidDevice)', |
| 57 | ' sl4a or s (SL4A)', |
| 58 | ' ed (EventDispatcher)', |
Eric Lin (Tzu Hsiang Lin) | 11d0f08 | 2021-01-12 21:21:37 +0800 | [diff] [blame] | 59 | ] |
| 60 | return '\n'.join(lines) |
Alexander Dorokhine | c48276a | 2016-12-05 19:04:25 -0800 | [diff] [blame] | 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
Ang Li | b7c2324 | 2021-01-20 10:47:27 +0800 | [diff] [blame] | 64 | parser = argparse.ArgumentParser(description='Interactive client for sl4a.') |
Eric Lin (Tzu Hsiang Lin) | 11d0f08 | 2021-01-12 21:21:37 +0800 | [diff] [blame] | 65 | parser.add_argument( |
Ang Li | b7c2324 | 2021-01-20 10:47:27 +0800 | [diff] [blame] | 66 | '-s', |
| 67 | '--serial', |
nkprasad12 | ebdec3a | 2023-10-26 17:29:55 -0700 | [diff] [blame] | 68 | help='Device serial to connect to (if more than one device is connected)', |
| 69 | ) |
Eric Lin (Tzu Hsiang Lin) | 11d0f08 | 2021-01-12 21:21:37 +0800 | [diff] [blame] | 70 | args = parser.parse_args() |
| 71 | logging.basicConfig(level=logging.INFO) |
| 72 | Sl4aShell().main(args.serial) |