From 9a892e1c8ceef834f043a8312f5778d33a4ae883 Mon Sep 17 00:00:00 2001 From: DeepMind Date: Fri, 10 Nov 2023 11:53:52 -0800 Subject: [PATCH] Send explicit broadcasts to SET_GRPC and ENABLE_A11Y_TREE_LOGS. changes: -add component field to SendBroadcast in AdbRequest -change the send broadcast function in the call parser to pass the component name if present in the request -in the a11y grpc wrapper, set the component name to match FlagsBroadcastReceiver PiperOrigin-RevId: 581326914 --- android_env/components/adb_call_parser.py | 16 ++++++++++++---- android_env/components/adb_call_parser_test.py | 12 ++++++++++++ android_env/proto/adb.proto | 5 +++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/android_env/components/adb_call_parser.py b/android_env/components/adb_call_parser.py index da94298..9a853dd 100644 --- a/android_env/components/adb_call_parser.py +++ b/android_env/components/adb_call_parser.py @@ -230,16 +230,24 @@ def _send_broadcast( An AdbResponse. """ - send_brodcast = request.send_broadcast + send_broadcast = request.send_broadcast response = adb_pb2.AdbResponse(status=adb_pb2.AdbResponse.Status.OK) - if not send_brodcast.action: + if not send_broadcast.action: response.status = adb_pb2.AdbResponse.Status.FAILED_PRECONDITION response.error_message = ('`send_broadcast.{action}` cannot be empty.') return response + if send_broadcast.component: + component_args = ['-n', send_broadcast.component] + else: + component_args = [] + response, _ = self._execute_command( - ['shell', 'am', 'broadcast', '-a', send_brodcast.action], - timeout=timeout) + ['shell', 'am', 'broadcast', '-a', send_broadcast.action] + + component_args, + timeout=timeout, + ) + return response def _install_apk( diff --git a/android_env/components/adb_call_parser_test.py b/android_env/components/adb_call_parser_test.py index 0325f11..0866c66 100644 --- a/android_env/components/adb_call_parser_test.py +++ b/android_env/components/adb_call_parser_test.py @@ -362,6 +362,18 @@ def test_send_broadcast_successful(self): self.assertEqual(response.status, adb_pb2.AdbResponse.Status.OK) self.assertEmpty(response.error_message) + def test_send_broadcast_with_component_successful(self): + adb = mock.create_autospec(adb_controller.AdbController) + parser = adb_call_parser.AdbCallParser( + adb, tmp_dir=absltest.get_default_test_tmpdir() + ) + request = adb_pb2.AdbRequest() + request.send_broadcast.action = 'SOME-ACTION' + request.send_broadcast.component = 'SOME-COMPONENT' + response = parser.parse(request) + self.assertEqual(response.status, adb_pb2.AdbResponse.Status.OK) + self.assertEmpty(response.error_message) + def test_uninstall_package_empty_package_name(self): adb = mock.create_autospec(adb_controller.AdbController) parser = adb_call_parser.AdbCallParser( diff --git a/android_env/proto/adb.proto b/android_env/proto/adb.proto index 4f298ef..840efa1 100644 --- a/android_env/proto/adb.proto +++ b/android_env/proto/adb.proto @@ -52,6 +52,11 @@ message AdbRequest { message SendBroadcast { // Action to send during the broadcast event. string action = 1; + + // Specify the component name with package name prefix to create an explicit + // intent, such as com.example.app/.ExampleActivity (see -n specification at + // https://developer.android.com/tools/adb#IntentSpec). + string component = 2; } message UninstallPackage {