|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + if sys.platform != "android": |
| 8 | + assert False, "This backend is only available on Android" |
| 9 | + |
| 10 | +import logging |
| 11 | +from typing import Callable |
| 12 | + |
| 13 | +from android.content import BroadcastReceiver as _BroadcastReceiver |
| 14 | +from android.content import Context, Intent, IntentFilter |
| 15 | +from android.os import Handler, HandlerThread |
| 16 | +from java import Override, jvoid, static_proxy |
| 17 | + |
| 18 | +from bleak.backends._utils import external_thread_callback |
| 19 | +from bleak.backends.android.utils import context |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +# Copied BroadcastReceiver logic from python-for-android and adapted it to chaquopy. |
| 25 | +# See https://github.com/kivy/python-for-android/blob/6f3ab805972e0d9531e3a207a6bc51c0effd8eb9/pythonforandroid/recipes/android/src/android/broadcast.py |
| 26 | +class BroadcastReceiver(static_proxy(_BroadcastReceiver)): # type: ignore[misc] |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + callback: Callable[[Context, Intent], None], |
| 30 | + actions: list[str] | None = None, |
| 31 | + categories: list[str] | None = None, |
| 32 | + ): |
| 33 | + super(BroadcastReceiver, self).__init__() |
| 34 | + self.context = context |
| 35 | + self.callback = callback |
| 36 | + |
| 37 | + if not actions and not categories: |
| 38 | + raise Exception("You need to define at least actions or categories") |
| 39 | + |
| 40 | + def _expand_partial_name(partial_name: str): |
| 41 | + if "." in partial_name: |
| 42 | + return partial_name # Its actually a full dotted name |
| 43 | + else: |
| 44 | + name = "ACTION_{}".format(partial_name.upper()) |
| 45 | + if not hasattr(Intent, name): |
| 46 | + raise Exception("The intent {} does not exist".format(name)) |
| 47 | + return getattr(Intent, name) |
| 48 | + |
| 49 | + # resolve actions/categories first |
| 50 | + resolved_actions = [_expand_partial_name(x) for x in actions or []] |
| 51 | + resolved_categories = [_expand_partial_name(x) for x in categories or []] |
| 52 | + |
| 53 | + # create a thread for handling events from the receiver |
| 54 | + self.handlerthread = HandlerThread("handlerthread") |
| 55 | + |
| 56 | + # create a listener |
| 57 | + self.receiver_filter = IntentFilter() |
| 58 | + for x in resolved_actions: |
| 59 | + self.receiver_filter.addAction(x) |
| 60 | + for x in resolved_categories: |
| 61 | + self.receiver_filter.addCategory(x) |
| 62 | + |
| 63 | + def start(self): |
| 64 | + self.handlerthread.start() |
| 65 | + self.handler = Handler(self.handlerthread.getLooper()) |
| 66 | + self.context.registerReceiver( |
| 67 | + self, |
| 68 | + self.receiver_filter, |
| 69 | + None, # type: ignore |
| 70 | + self.handler, |
| 71 | + ) |
| 72 | + |
| 73 | + def stop(self): |
| 74 | + self.context.unregisterReceiver(self) |
| 75 | + self.handlerthread.quit() |
| 76 | + |
| 77 | + @Override(jvoid, [Context, Intent]) |
| 78 | + @external_thread_callback |
| 79 | + def onReceive(self, context: Context, intent: Intent): |
| 80 | + logger.debug(f"BroadcastReceiver received intent: {intent}") |
| 81 | + self.callback(context, intent) |
0 commit comments