这是indexloc提供的服务,不要输入任何密码
Skip to content

Add protocol method to set a Termux RUN_COMMAND pending intent on a remote view #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mogenson
Copy link

@mogenson mogenson commented Apr 11, 2025

This change allows a remote view in a widget to launch a command in Termux when clicked. This is useful to start a Termux program on-demand to update a widget, but let Termux and the program remain closed when the user is not interacting with the widget.

Add a new protocol method "setRemoteViewRunCommand", that accepts a command path parameter (which should be a full path to the executable), a command arguments parameter (which is a comma separated string), and a run in background parameter (which is a boolean). Store these in an optional RunCommand data class.

If this optional data class exists for a remote view ID when updating the widget layout, create a pending intent to invoke the Termux RUN_COMMAND service and set it on the widget root view.

Below is an example Python script that uses this protocol method to update a count every time the user taps on the widget.

import sys
import termuxgui as tg

with tg.Connection() as c:
    wid = sys.argv[1]
    count = sys.argv[2]
    count += 1

    rv = tg.RemoteViews(c)
    c.send_msg({"method": "setRemoteViewRunCommand", "params": {
        "rid": rv.rid,
        "path": "/data/data/com.termux/files/usr/bin/python",
        "args": f"widget.py, {wid}, {count}",
        "background": True
    }})

    tv = rv.addTextView()
    rv.setText(tv, f"count: {count}")
    rv.setBackgroundColor(tv, 0xffffffff)
    rv.updateWidget(wid)

This change allows a remote view in a widget to launch a command in
Termux when clicked. This is useful to start a Termux program on-demand
to update a widget, but let Termux and the program remain closed when
the user is not interacting with the widget.

Add a new protocol method "setRemoteViewRunCommand", that accepts a
command path parameter (which should be a full path to the executable),
a command arguments parameter (which is a comma separated string), and
a run in background parameter (which is a boolean). Store these in an
optional RunCommand data class.

If this optional data class exists for a remote view ID when updating
the widget layout, create a pending intent to invoke the Termux RUN_COMMAND
service and set it on the widget root view.

Below is an example Python script that uses this protocol method to
update a count every time the user taps on the widget.

```
import sys
import termuxgui as tg

with tg.Connection() as c:
    wid = sys.argv[1]
    count = sys.argv[2]
    count += 1

    rv = tg.RemoteViews(c)
    c.send_msg({"method": "setRemoteViewRunCommand", "params:" {
        "rid": rv.rid,
        "path": "/data/data/com.termux/files/usr/bin/python",
        "args": f"widget.py, {wid}, {count}",
        "background", True
    }})

    tv = rv.addTextView()
    rv.setText(tv, f"count: {count}")
    rv.setBackgroundColor(tv, 0xffffffff)
    rv.updateWidget(wid)
```
@mogenson
Copy link
Author

Here is a build with the above changes in case anyone wants to try without building themselves app-debug.apk.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant