Android Utilities
This module provides utility functions and classes for handling Android UI interactions, running code on the UI thread, and logging.
This module offers several helper classes and functions to simplify common Android development tasks within your Python plugins, such as UI updates, event handling, and logging.
Wrappers for Java Interfaces
These classes act as convenient Python proxies for common Java functional interfaces, especially useful for setting listeners.
R (Runnable Proxy)
A static_proxy
class implementing Java's java.lang.Runnable
interface. It's primarily used with run_on_ui_thread
and can also be passed to many internal Telegram methods or other Android APIs that expect a Runnable
.
Using R
is generally preferred over creating a dynamic_proxy
for Runnable
due to its optimized nature as a static_proxy
.
OnClickListener
A dynamic_proxy
wrapper for Android's android.view.View.OnClickListener
. Simplifies setting click listeners on UI views from Python.
The lambda or function passed to OnClickListener
will be executed when the view is clicked. It takes no arguments.
OnLongClickListener
A dynamic_proxy
wrapper for Android's android.view.View.OnLongClickListener
. Used for handling long-press events on UI views.
The function passed to OnLongClickListener
should return True
if the long click event was consumed (preventing further processing, like a normal click), or False
otherwise. It takes no arguments.
Utility Functions
run_on_ui_thread
Schedules and runs the provided Python callable on the main Android UI thread. This is crucial for any operations that modify the user interface, as UI updates must happen on this thread.
func
: The Python callable to execute.delay
(optional): Delay in milliseconds before the callable is executed. Defaults to0
(execute as soon as possible).
log
A versatile logging function that sends output to Android's logcat, viewable with adb logcat
or Android Studio's Logcat panel. It intelligently handles different data types.
- If
data
is a simple type (str
,int
,float
,bool
, orNone
), it's converted to a string and logged. - If
data
is any other object (e.g., a complex class instance, a list, a dictionary), its detailed structure or relevant information in JSON format (viaAppUtils.printObjectDetails
) is logged. This is very useful for inspecting the state of Java or Python objects.