-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add support to switch to existing session instead of creating duplicate session for RUN_COMMAND intent
#2668
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
Conversation
…ame` in addition to `TerminalSession.mSessionName`
…reating duplicate session for `RUN_COMMAND` intent This is done via addition of the `com.termux.RUN_COMMAND_SESSION_CREATE_MODE` extra, which currently supports two values. - `always` to always create a new session every time. - `no-session-with-name` to create a new session only if no existing session exits with the same terminal session name. The terminal session name will equal executable basename by default and dashes `-` in the basename will no longer be replaced with spaces when session name as done previously. The `com.termux.RUN_COMMAND_SESSION_NAME` extra can be used to set custom session name. Usage: You can use this with `Termux:Tasker` or `Termux:Widget`. For example for `Termux:Widget` - Create a wrapper script at `~/.shortcuts/tasks/my-script.sh` with following contents under `tasks` directory so that it runs in background app shell instead of a terminal session. Do not use terminal session runner for wrapper script, since it will open two sessions everytime otherwise, first for wrapper script, then for actual target executable. There would also be conflicts if both wrapper script and target executable have the same basename and it would be incorrectly assumed that session is already running. - Replace the `bash` executable with actual target executable that you want to run in the terminal session if its not already running. - Optionally set custom session name. By default it will set to executable basename and not the wrapper script name. To set it to wrapper script name, you can pass `$(basename "$0")`. - Launch the wrapper script with widget. On first launch, a new terminal session should open but on subsequent launches, same terminal session should open. Note that you can also pass `com.termux.RUN_COMMAND_SESSION_ACTION` to modify session action behaviour. Check https://github.com/termux/termux-app/wiki/RUN_COMMAND-Intent#run_command-intent-command-extras. ``` am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \ -a com.termux.RUN_COMMAND \ --es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/bash' \ --es com.termux.RUN_COMMAND_SESSION_CREATE_MODE 'no-session-with-name' \ --es com.termux.RUN_COMMAND_SESSION_NAME "custom-name" ```
|
Hey @trygveaa, can you test if this suits your needs. Thanks Github actions APK link: https://github.com/termux/termux-app/actions/runs/2001675564 |
|
@agnostic-apollo: Sorry! I missed your original comment. Thanks for this, I'll definitely test it asap. |
|
I have a potential problem with being able to test it though. I recently upgraded to Android 12 and the single shortcut widget is completely gone, the one I had disappeared, and it's missing from add widgets. I haven't had time to check out what could cause this yet. But the widget with a list of shortcuts still exists and works, so I'll test with that. |
|
You are welcome. Thanks for testing.
It is showing in android 12 avd with Pixel Launcher, possible issue could be not explicitly defined https://github.com/termux/termux-widget/blob/v0.13.0/app/src/main/AndroidManifest.xml#L40 https://developer.android.com/about/versions/12/behavior-changes-12#exported |
|
And is the internal and external |
|
Hm, there is a 3 second delay from pressing the widget until Termux is launched. Is it like that on your device? |
|
It's instantaneous on my android 11 lineageOS device. You can check with termux notification, |
|
Make sure both apps have any battery optimizations disabled. |
Ah, thanks for the pointer and suggestion. I tried nova launcher and it shows up there (along with others that were missing). Strangely, when I try to add it there it's added to the oneplus launcher instead. But if I change default launcher to nova it's added there. So okay, that issue is not related to Termux.
A notification with
Right, I tried that now, but same result unfortunately. I'm using a OnePlus 9 Pro (LE2123) with Android 12 (LE2123_11_C.47).
I don't know enough about the widget design to say anything about the way a widget is normally launched vs. using RUN_COMMAND, but at least when using RUN_COMMAND I think this looks good. A bit cumbersome to create though, not sure if it can be simplified? (but not that important for me, I'm fine with creating it like you described.) |
Pinned shortcuts can only be launched by default launcher, so
Can you try with
A library/script is planned to be provided once designs are finalized.
|
Hm, it seems to work fine for other pinned shortcuts, like settings, translate and connectbot. They are added to Nova, and launch fine from Nova even though Nova is not the default launcher.
Ah, nice, that got rid of the delay. However, now my script is only launched if I run the widget when Termux is already running (just any session running, not necessarily with that session). If I try to launch the widget when Termux is not running, it just opens bash instead of my script.
Perfect! |
They may be static shortcuts. https://github.com/agnostic-apollo/TaskerLauncherShortcut#Shortcut-Types
Issue is that if the initial task completes before the As for the delay, its because |
Thanks for the pointers, I'll have to look into this in more detail when I have some time.
Ah, makes sense. Works great with the sleep. Thanks so much for implementing this, and helping me getting it working nicely.
Seems to work with
Does it need that am integration? Can't Termux:Widget just send the RUN_COMMAND intent directly? |
|
You are very welcome. :)
Yup. It's not gonna be that simple. Will need to implement synchronization logic to handle that and will be time consuming. I have added it to my todo list, will do it at some point. It's not a bug with the current feature add anyways and is an old bug from before my time and initial commit. https://stackoverflow.com/questions/21081100/android-service-stopselfint I handled a similar case before as well.
No it does not. Termux:Widget directly sends intents to termux-app/termux-shared/src/main/java/com/termux/shared/termux/TermuxConstants.java Line 956 in 353ae93
Since feature is working fine, I will merge this unless there are any objections. |
Right, not a big problem anyways (at least for my use case).
Great!
Yes, please do :) |
Great. Done! :) |
|
@trygveaa you may wanna test #2458 (comment) too if you want, for your am delay issues. |
UPDATE Extras changed, check d287734
This is done via addition of the
com.termux.RUN_COMMAND_SESSION_CREATE_MODEextra, which currently supports two values.alwaysto always create a new session every time.no-session-with-nameto create a new session only if no existing session exits with the same terminal session name.The terminal session name will equal executable basename by default and dashes
-in the basename will no longer be replaced with spaces when session name as done previously. Thecom.termux.RUN_COMMAND_SESSION_NAMEextra can be used to set custom session name.Usage:
You can use this with
Termux:TaskerorTermux:Widget.For example for
Termux:Widget~/.shortcuts/tasks/my-script.shwith following contents undertasksdirectory so that it runs in background app shell instead of a terminal session. Do not use terminal session runner for wrapper script, since it will open two sessions everytime otherwise, first for wrapper script, then for actual target executable. There would also be conflicts if both wrapper script and target executable have the same basename and it would be incorrectly assumed that session is already running.bashexecutable with actual target executable that you want to run in the terminal session if its not already running.$(basename "$0").Note that you can also pass
com.termux.RUN_COMMAND_SESSION_ACTIONto modify session action behaviour. Check https://github.com/termux/termux-app/wiki/RUN_COMMAND-Intent#run_command-intent-command-extras.