Commit Graph

265 Commits

Author SHA1 Message Date
agnostic-apollo
18a1a33e83 Added: Enable TERMUX_PACKAGE_VARIANT apt-android-5 builds 2022-05-24 01:19:45 +05:00
agnostic-apollo
7677633e8f Fixed: Catch UnsatisfiedLinkError for local-socket library 2022-05-24 01:19:45 +05:00
agnostic-apollo
5290ce1f77 Added|Fixed: Add TermuxNotificationUtils.getTermuxOrPluginAppNotificationBuilder() helper function and fix notification icon drawable resource id issue on Android 5 2022-05-24 01:19:45 +05:00
agnostic-apollo
ab9b620c88 Added: Add ResourceUtils to get resource ids from names
This will mainly be used later when MediaViewer gets added.
2022-05-24 01:19:45 +05:00
agnostic-apollo
4e08f76fd2 Changed: Export correct PATH and also export LD_LIBRARY_PATH for Android 5/6 since packages won't use DT_RUNPATH 2022-05-24 01:19:45 +05:00
agnostic-apollo
c549988434 Fixed: Fix broken javadocs links 2022-05-24 01:19:45 +05:00
agnostic-apollo
55dcd09a09 Fixed: Fixed extra keys not showing properly on Android 5
Related issue #739
2022-05-24 01:19:45 +05:00
agnostic-apollo
677a580042 Changed: Add general compatibility fixes for minSdkVerion 21 2022-05-24 01:19:45 +05:00
agnostic-apollo
fa829623a8 Added: Add ViewUtils.pxToDp() 2022-05-24 01:19:45 +05:00
agnostic-apollo
14e9a8b6fc Changed: Use float dp parameter instead of int for ViewUtils.dpToPx() to not lose precision 2022-05-24 01:19:45 +05:00
agnostic-apollo
a1719d91b3 Changed: Bump termux-am-library to 2.0.0 that uses minSdkVersion 21 2022-05-24 01:19:45 +05:00
agnostic-apollo
9143ebdc22 Changed: Enable desugaring support to enable support for new language APIs like Java 8 on old android versions
https://developer.android.com/studio/write/java8-support
2022-05-24 01:19:45 +05:00
agnostic-apollo
6213b7f782 Changed: Use double quotes instead of single quotes for all gradle dependencies 2022-05-24 01:19:45 +05:00
agnostic-apollo
b950efec27 Added: Add support for TERMUX_APP_PACKAGE_MANAGER and TERMUX_APP_PACKAGE_VARIANT to build APKs with different package manager configurations
The `TermuxBootstrap` class has been added that defines the `PackageManager` and `PackageVariant` classes for the supported package manager configurations for the app. The variant is defined by the `project.ext.packageVariant` value in the `app/build.gradle` and its value is used by the `build.gradle` to pack its respective bootstrap zips in the app APK at build time and the value is used to set `TermuxBootstrap.TERMUX_APP_PACKAGE_MANAGER` and `TermuxBootstrap.TERMUX_APP_PACKAGE_VARIANT` static values that are used at runtime by the app to run variant specific code. The manager is automatically extracted from the variant as the substring before first dash `-`.

The default variant is `apt-android-7` and it can either be replaced in `app/build.gradle` manually or the `TERMUX_PACKAGE_VARIANT` env variable can be exported in which the build command is run.

The `TERMUX_APP_PACKAGE_MANAGER` and `TERMUX_APP_PACKAGE_VARIANT` environmental variables will be exported by the app and they will also be added in Termux app info in about page and reports, allowing users and devs to know which variant is currently installed.

Bootstrap of a different variant must not be manually installed by the user after app installation by replacing `$PREFIX` since app code is dependant on the variant used to build the APK.

Currently, `apt-android-7` and `apt-android-5` variants will be built for by the workflows but they will fail for `apt-android-5` since `build.gradle` support is currently not enabled and will be enabled by a pull request that adds support for Android 5. The workflow needs to try to build the `apt-android-5` variant so that pull request builds are generated.
2022-04-28 09:33:20 +05:00
agnostic-apollo
7f7d889dd0 Fixed: Fix proguard removing JNI used methods for release builds
```
Exception in createServerSocketNative():
java.lang.NoSuchMethodError: no non-static method "Lcom/termux/shared/jni/models/JniResult;.<init>(IILjava/lang/String;I)V"
	at com.termux.shared.net.socket.local.LocalSocketManager.createServerSocketNative(Native Method)
	at com.termux.shared.net.socket.local.LocalSocketManager.createServerSocket(LocalSocketManager.java:125)
	at com.termux.shared.net.socket.local.LocalServerSocket.start(LocalServerSocket.java:100)
	at com.termux.shared.net.socket.local.LocalSocketManager.start(LocalSocketManager.java:84)
	at com.termux.shared.shell.am.AmSocketServer.start(AmSocketServer.java:68)
	at com.termux.shared.termux.shell.am.TermuxAmSocketServer.start(TermuxAmSocketServer.java:101)
	at com.termux.shared.termux.shell.am.TermuxAmSocketServer.setupTermuxAmSocketServer(TermuxAmSocketServer.java:77)
	at com.termux.app.TermuxApplication.onCreate(TermuxApplication.java:53)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6719)
	at android.app.ActivityThread.access$1300(ActivityThread.java:237)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:223)
	at android.app.ActivityThread.main(ActivityThread.java:7664)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
```
2022-04-23 00:39:00 +05:00
agnostic-apollo
53f26c9659 Changed: Refactor am socket server
The `AmSocketServer` now handles the entire logic for processing of am commands sent by clients and its results. This can be used by other apps as well to run their own am servers. The server started by `termux-app` will be managed by `TermuxAmSocketServer`. Read their javadocs for details.

The extended implementation `TermuxAmSocketServerClient` of `AmSocketServer.AmSocketServerClient`/`ILocalSocketManager` will also send a plugin error notification for all errors to the user instead of just logging to logcat since users are not very good at checking those, this should save dev time debugging problems. We may need to ignore notifications for some errors like broken pipe, based on their `Error` objects if they are normally expected, this requires further investigation.

The `TERMUX_APP_AM_SOCKET_SERVER_ENABLED` env variable will also be exported for all shell sessions and tasks for whether the server was successfully started on app startup. The user can disable the server by adding "run-termux-am-socket-server=false" to the "~/.termux/termux.properties" as implemented in 5f8a9222. The env variable will be checked by `$PREFIX/bin/termux-am` before attempting to connect.

The new path for the server socket is `/data/data/com.termux/files/apps/termux-app/termux-am/am.sock` as per `TERMUX_APP.APPS_DIR_PATH` added in bcd8f4c4.
2022-04-23 00:39:00 +05:00
agnostic-apollo
2aa7f43d1c Added|Changed|Fixed: Refactor local socket server implementation and make client handling abstract
- Added `LocalSocketManager` to manage the server, `LocalServerSocket` to represent server socket, `LocalClientSocket` to represent client socket, `LocalSocketRunConfig` to store server run config and `ILocalSocketManager` as interface for the `LocalSocketManager` to handle callbacks from the server to handle clients.
- Added support to get full `PeerCred` for client socket, including `pid`, `pname`, `uid`, `uname`, `gid`, `gname` and `cmdline` instead of just `uid`. This should provide more info for error logs about which client failed or tried to connect in case of disallowed clients. Some data is filled in native code and some in java. Native support for added to get process name and `cmdline` of a process with a specific pid.
- Added `JniResult` to get results for JNI calls. Previously only an int was returned and incomplete errors logged. With `JniResult`, both `retval` and `errno` will be returned and full error messages in `errmsg`, including all `strerror()` output for `errno`s. This would provide more helpful info on errors.
- Added `Error` support via `LocalSocketErrno` which contains full error messages and stacktraces for all native and java calls, allowing much better error reporting to users and devs. The errors will be logged by `LocalSocketManagerClientBase` if log level is debug or higher since `PeerCred` `cmdline` may contain private info of users.
- Added support in java to check if socket path was an absolute path and not greater than `108` bytes, after canonicalizing it since otherwise it would result in creation of useless parent directories on failure.
- Added `readDataOnInputStream()` and `sendDataToOutputStream()` functions to `LocalClientSocket` so that server manager client can easily read and send data.

- Renamed the variables and functions as per convention, specially one letter variables. https://source.android.com/setup/contribute/code-style#follow-field-naming-conventions
- Rename `local-filesystem-socket` to `local-filesystem` since abstract namespace sockets can also be created.
- Previously, it was assumed that all local server would expect a shell command string that should be converted to command args with `ArgumentTokenizer` and then should be passed to `LocalSocketHandler.handle()` and then result sent back to client with exit code, stdout and stderr, but there could be any kind of servers in which behaviour is different. Such client handling should not be hard coded and the server manager client should handle the client themselves however they like, including closing the client socket. This will now be done with `ILocalSocketManager. onClientAccepted(LocalSocketManager, LocalClientSocket)`.

- Ensure app does not crash if `local-socket` library is not found or for any other exceptions in the server since anything running in the `Application` class is critical that it does not fail since user would not be able to recover from it, specially non rooted users without SAF support to disable the server with a prop.
- Make sure all reasonable JNI exceptions are caught instead of crashing the app.
- Fixed issue where client logic (`LocalSocketHandler.handle()` was being run in the same thread as the new client acceptable thread, basically blocking new clients until previous client's am command was fully processed. Now all client interface callbacks are started in new threads by `LocalSocketManager`.
- Fix bug where timeout would not be greater than `1000ms` due to only using `tv_usec` which caps at `999,999`.
2022-04-23 00:36:12 +05:00
agnostic-apollo
5f8a922201 Added: Allow users to disable termux-am server
The user can add `run-termux-am-socket-server=false` entry to `termux.properties` file to disable the `termux-am` server to run at app startup which is connected to by `$PREFIX/bin/termux-am` from the `termux-am-socket` package. The default value is `true`. Changes require `termux-app` to be force stopped and restarted to provide consistent state for all termux sessions and tasks.

The prop will be used in a later commit.
2022-04-23 00:36:12 +05:00
agnostic-apollo
9a71074c7d Added: Add function to ProcessUtils to get app process name for a pid from ActivityManager 2022-04-23 00:36:12 +05:00
agnostic-apollo
69cc65c3ac Added: Add functions to UserUtils to get user name for user id from PackageManager and Libcore 2022-04-23 00:36:12 +05:00
agnostic-apollo
bcd8f4c419 Added: Add TERMUX_APPS_DIR_PATH and TERMUX_APP.APPS_DIR_PATH and create them at application startup.
The termux files directory will also be checked and created if required at startup and code related to it will only be run if it is accessible. This can later also be used for init execution commands.

The `TERMUX_APP.APPS_DIR_PATH` will act as app specific directory for `termux-app` app related files. Other plugin apps will have their own directories under `TERMUX_APPS_DIR_PATH` if required.
2022-04-23 00:36:12 +05:00
agnostic-apollo
6bda7c4fc4 Added|Changed: Add Logger.logErrorPrivate*() functions which do not log errors that may contain potentially private info unless log level is debug or higher
Execution commands and other errors that may contain potentially private info should not be logged unless user has explicitly allowed it since apps with `READ_LOGS` permission would be able to read the data. A notification for failed executions commands would still be shown if enabled and required.
2022-04-23 00:36:12 +05:00
agnostic-apollo
89a08ff01a Fixed: Allow Object class object to be passed to ReflectionUtils.invokeField() 2022-04-23 00:36:12 +05:00
agnostic-apollo
c81d9c3346 Added: Add FileType.SOCKET support and add FileUtils.deleteSocketFile() function 2022-04-23 00:36:12 +05:00
agnostic-apollo
58c3d427e8 Fixed: Log and add to Error the current file type in FileUtils.deleteFile() in addition to allowed file types 2022-04-23 00:36:12 +05:00
agnostic-apollo
1b9ca91da5 Added: Add functions to DataUtils to get generic, space and tab indented strings 2022-04-23 00:36:12 +05:00
agnostic-apollo
9c7ec0cebd Added: Add functions that can be used by non main threads to set CrashHandler as the UncaughtExceptionHandler 2022-04-23 00:36:12 +05:00
agnostic-apollo
6b60adc079 Fixed: Do not stop the app if UncaughtExceptionHandler implemented by CrashHandler receives an exception on a non main thread
Rename function that should be used by main thread of apps to `setDefaultCrashHandler()`.

Functions for other threads will be added in a later commit.
2022-04-23 00:36:12 +05:00
agnostic-apollo
5116d886c3 Changed: Add label parameter to ExecutionCommand getArgumentsLogString() and getArgumentsMarkdownString() functions for external usage 2022-04-23 00:36:12 +05:00
agnostic-apollo
02ab8324e9 Changed|Fixed: Do not add empty stacktraces entry to Error log and markdown String 2022-04-23 00:36:12 +05:00
agnostic-apollo
c095a6184b Changed: Rename TermuxCrashUtils sendPluginCrashReportNotification() to sendCrashReportNotification()` 2022-04-23 00:36:12 +05:00
agnostic-apollo
cc981d8a03 Changed: Move com.termux.app.utils.PluginUtils to com.termux.shared.termux.plugins.TermuxPluginUtils
This will allow plugins and `termux-shared` library to trigger plugin error notifications too and process plugin command results.
2022-04-23 00:36:12 +05:00
tareksander
3dee2eb486 Changed: Allow connections from root o sockets. 2022-04-23 00:36:12 +05:00
tareksander
f366db0cb3 Added: LocalFilesystemSocket as an Interface to UNIX sockets in the filesystem. The UID of connecting programs is automatically checked against the processes UID and connections where the UID doesn't match are automatically rejected and logged.
Changed: LocalSocketListener now uses sockets in the filesystem.
2022-04-23 00:36:12 +05:00
tareksander
4aca16326c Added: termux-am-library to integrate am with Termux. 2022-04-23 00:36:12 +05:00
tareksander
e597ece75f Added: ArgumentTokenizer to com.termux.shared.shell 2022-04-23 00:36:12 +05:00
agnostic-apollo
5794ab9a56 Added|Changed: Add support to switch to existing session instead of creating 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"
```
2022-04-08 02:42:54 +05:00
agnostic-apollo
ee32ef0c7e Changed: Maintain terminal session name in ExecutionCommand.sessionName in addition to TerminalSession.mSessionName 2022-04-08 02:42:54 +05:00
agnostic-apollo
ce12b8ad2d Fixed: Fix Settings.ACTION_* permission requests result callback
Adding `FLAG_ACTIVITY_NEW_TASK` will start permission activity in separate task and `onActivityResult()` will be called early in the calling activity without grant/not-grant result being actually set.
2022-03-30 19:45:55 +05:00
agnostic-apollo
5e820ad249 Added: Allow users to adjust $TMPDIR clear mechanism on termux exit
The `delete-tmpdir-files-older-than-x-days-on-exit` key can be used to adjust how many days old the access time should be of files that should be deleted from `$TMPDIR` on termux exit. The user can set an integer value between `-1` and `100000`. Set `-1` to delete no files, `0` to delete all files and `> 0` for `x` days. The default value is `3` days. So adding an entry like `delete-tmpdir-files-older-than-x-days-on-exit=10` to `termux.properties` file will make termux delete files older than `10` when termux is exited. After updating the value, either restart termux or run `termux-reload-settings` for changes to take effect.

Note that currently `> 0` will revert back to `0` since deletion is currently broken for empty sub directories and deletion needs to be done based on access time instead of modified time. It will need to be fixed in a later commit. Check `FileUtils.deleteFilesOlderThanXDays()`.

Related issue #2350
2022-03-17 22:35:31 +05:00
agnostic-apollo
d25f7afd97 Changed: Share terminal transcript with ShareUtils 2022-03-17 05:30:25 +05:00
agnostic-apollo
e0074f280f Fixed: Fix typos 2022-03-17 05:30:25 +05:00
agnostic-apollo
477b36acd1 Added: Add support for ACTION_NOTIFY_APP_CRASH in receiver registered by TermuxActivity to notify users of plugin app crashes
Once plugins integrate changes for `TermuxCrashUtils.onPostLogCrash()`, they will send the `ACTION_NOTIFY_APP_CRASH` broadcast when an uncaught exception is caught by `CrashHandler`. If `TermuxActivity` is in foreground, then it will receive the broadcast and notify user of the crash by reading it from the crash log file without the user having to restart termux app to be notified.
2022-03-17 02:10:51 +05:00
agnostic-apollo
5f00531381 Changed: Move com.termux.app.utils.CrashUtils to com.termux.shared.termux.crash.TermuxCrashUtils so that plugins trigger plugin notifications too
Calls to `notifyAppCrashFromCrashLogFile()` will now be synchronized as well.
2022-03-17 02:10:51 +05:00
agnostic-apollo
4dbfc1fac8 Added: Add support for onPreLogCrash() and onPostLogCrash() in CrashHandler so that CrashHandlerClient can decide which exceptions to log and add custom logic 2022-03-17 02:10:51 +05:00
agnostic-apollo
4b07e4f4c0 Added: Add multi process support in TermuxAppSharedPreferences since plugin apps may need to read values modified by termux app process 2022-03-17 02:10:51 +05:00
agnostic-apollo
621545dd0a Added: Add support for getting termux app and plugin app info only in TermuxUtils.getAppInfoMarkdownString() 2022-03-17 02:10:51 +05:00
agnostic-apollo
9a65aa4589 Fixed: Do not add double heading if callingPackageName passed to TermuxUtils.getAppInfoMarkdownString() is a plugin app 2022-03-17 02:10:51 +05:00
agnostic-apollo
021cb60e23 Added: Add TERMUX_API_APT_* constants 2022-03-17 02:10:51 +05:00
agnostic-apollo
14c5fc7b1e Fixed: Suppress warnings for requiring android 11 to request MANAGE_EXTERNAL_STORAGE permission and call Environment.isExternalStorageManager() 2022-03-17 02:10:51 +05:00