Termux-app: Feature Request: open Termux in a certain directory. **WITH Cooperating File Manager**

Created on 16 Mar 2019  路  8Comments  路  Source: termux/termux-app

I have post this request before, but forget to attach the link to the file manager's homepage. Sorry about that.

Feature description

Open Termux in a certain working directory specified by other Apps (File Manager).

In other words, provide a intent to other Apps.

Reference implementation

Terminal App: I have only used Termux before.

The file manager waiting for the intent:
android-file-manager
On F-Droid

enhancement

Most helpful comment

Hi, author of zhanghai/MaterialFiles here. I read https://github.com/termux/termux-app/issues/1070#issuecomment-668278572 and agree that it may work, but still hope that we can solve this with a different approach because:

  1. Activity v.s. Service: Ideally since we are intending to launch a new UI screen for user to issue shell commands, the sementically correct thing to do is to start an Activity instead of a Service. Starting a Service also results in requiring the user to either manually click on a notification, or grant unnecessary permissions to Termux, which could have been fine with the semantically correct approach of starting an Activity.

    To automatically bring to foreground and start termux commands that were started with
    background mode "false" in android >= 10 without user having to click the notification manually,
    requires termux to be granted draw over apps permission due to new restrictions
    of starting activities from the background, this also applies to Termux:Tasker plugin.

  2. Permission: The ability to run any command is too powerful for the use case of simply starting a terminal with a certain working directory, which is harmless, so having to request and be granted a runtime permission seems both tedious and unnecessary just for starting a terminal with a certain working directory.

  3. Hardcoding: The approach above requires hardcoding "/data/data/com.termux/files/usr/bin/login", which should ideally be an unrelated detail to the intention of simply starting a terminal with a certain working directory.

So I'm proposing to add an intent action/extra for initial working directory on the main activity instead. This approach doesn't have any of the problems above, and is the apporach chosen by some other well known terminal applications:

  1. The good old jackpal/Android-Terminal-Emulator supports ACTION_SEND on a TermHere activity for passing in an intial working directory (source), and MaterialFiles has been using it. Notably, it also supports running custom scripts and protects that ability behind a runtime permission in the meantime, so the two are not in conflict and can instead be different features for different purposes, with different protection.

  2. On desktop, gnome-terminal supports the command line argument --working-directory=DIRNAME (source), which is also used by Nautilus for implementing the "Open terminal here" in its right-click context menu.

  3. kconsole supports --workdir dir for the initial working directory as well (source).

All 8 comments

@xeffyr , How is the implementation of this feature going? Have the core team of termux communicate with the developer of android-file-manager?

Material File can't make Open in terminal work with Termux without this feature, see zhanghai/MaterialFiles#183 . Thanks!

A sort of hacky method that I can think of is using something like below:

        Intent intent = new Intent();
        intent.setClassName("com.termux", "com.termux.app.RunCommandService");
        intent.setAction("com.termux.RUN_COMMAND");
        intent.putExtra("com.termux.RUN_COMMAND_PATH", "/data/data/com.termux/files/usr/bin/login");
        intent.putExtra("com.termux.RUN_COMMAND_WORKDIR", "/directory/to/open");
        intent.putExtra("com.termux.RUN_COMMAND_BACKGROUND", false);
        try {
            ContextCompat.startForegroundService(activity, intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

This obviously requires the user to add allow-external-apps=true in ~/.termux/termux.properties and the app has to request com.termux.permission.RUN_COMMAND.

@MuntashirAkon Right, this can be used as solution. It's not "hacky" actually. Other method would be just a shortcut for same service launch intent.

intent.putExtra("com.termux.RUN_COMMAND_PATH", "/data/data/com.termux/files/usr/bin/bash");

Use /data/data/com.termux/files/usr/bin/login instead.

It might be necessary to open Termux app itself if it's not currently running though.

It will be opened automatically if not running.

Use /data/data/com.termux/files/usr/bin/login instead.

Thanks for the info.

It will be opened automatically if not running.

In that case, the service must be started in the foreground.

Hi, author of zhanghai/MaterialFiles here. I read https://github.com/termux/termux-app/issues/1070#issuecomment-668278572 and agree that it may work, but still hope that we can solve this with a different approach because:

  1. Activity v.s. Service: Ideally since we are intending to launch a new UI screen for user to issue shell commands, the sementically correct thing to do is to start an Activity instead of a Service. Starting a Service also results in requiring the user to either manually click on a notification, or grant unnecessary permissions to Termux, which could have been fine with the semantically correct approach of starting an Activity.

    To automatically bring to foreground and start termux commands that were started with
    background mode "false" in android >= 10 without user having to click the notification manually,
    requires termux to be granted draw over apps permission due to new restrictions
    of starting activities from the background, this also applies to Termux:Tasker plugin.

  2. Permission: The ability to run any command is too powerful for the use case of simply starting a terminal with a certain working directory, which is harmless, so having to request and be granted a runtime permission seems both tedious and unnecessary just for starting a terminal with a certain working directory.

  3. Hardcoding: The approach above requires hardcoding "/data/data/com.termux/files/usr/bin/login", which should ideally be an unrelated detail to the intention of simply starting a terminal with a certain working directory.

So I'm proposing to add an intent action/extra for initial working directory on the main activity instead. This approach doesn't have any of the problems above, and is the apporach chosen by some other well known terminal applications:

  1. The good old jackpal/Android-Terminal-Emulator supports ACTION_SEND on a TermHere activity for passing in an intial working directory (source), and MaterialFiles has been using it. Notably, it also supports running custom scripts and protects that ability behind a runtime permission in the meantime, so the two are not in conflict and can instead be different features for different purposes, with different protection.

  2. On desktop, gnome-terminal supports the command line argument --working-directory=DIRNAME (source), which is also used by Nautilus for implementing the "Open terminal here" in its right-click context menu.

  3. kconsole supports --workdir dir for the initial working directory as well (source).

@xeffyr , Do you plan to add the intent suggested by @zhanghai ?

Was this page helpful?
0 / 5 - 0 ratings