Skip to content

Commit 00194eb

Browse files
Add logcat errors in RunCommandService
This commit adds `logcat` errors if an invalid intent action is passed or if `allow-external-apps` is not set to `true` while sending an intent to `RunCommandService`, so that users can detect issues.
1 parent f50d15d commit 00194eb

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

app/src/main/java/com/termux/app/RunCommandService.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,30 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9292
// Run again in case service is already started and onCreate() is not called
9393
runStartForeground();
9494

95-
if (allowExternalApps() && RUN_COMMAND_ACTION.equals(intent.getAction())) {
96-
Uri programUri = new Uri.Builder().scheme("com.termux.file").path(parsePath(intent.getStringExtra(RUN_COMMAND_PATH))).build();
97-
98-
Intent execIntent = new Intent(TermuxService.ACTION_EXECUTE, programUri);
99-
execIntent.setClass(this, TermuxService.class);
100-
execIntent.putExtra(TermuxService.EXTRA_ARGUMENTS, intent.getStringArrayExtra(RUN_COMMAND_ARGUMENTS));
101-
execIntent.putExtra(TermuxService.EXTRA_CURRENT_WORKING_DIRECTORY, parsePath(intent.getStringExtra(RUN_COMMAND_WORKDIR)));
102-
execIntent.putExtra(TermuxService.EXTRA_EXECUTE_IN_BACKGROUND, intent.getBooleanExtra(RUN_COMMAND_BACKGROUND, false));
103-
104-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
105-
this.startForegroundService(execIntent);
106-
} else {
107-
this.startService(execIntent);
108-
}
95+
// If wrong action passed, then just return
96+
if (!RUN_COMMAND_ACTION.equals(intent.getAction())) {
97+
Log.e("termux", "Unexpected intent action to RunCommandService: " + intent.getAction());
98+
return Service.START_NOT_STICKY;
99+
}
100+
101+
// If allow-external-apps property to not set to "true"
102+
if (!allowExternalApps()) {
103+
Log.e("termux", "RunCommandService requires allow-external-apps property to be set to \"true\" in ~/.termux/termux.properties file.");
104+
return Service.START_NOT_STICKY;
105+
}
106+
107+
Uri programUri = new Uri.Builder().scheme("com.termux.file").path(parsePath(intent.getStringExtra(RUN_COMMAND_PATH))).build();
108+
109+
Intent execIntent = new Intent(TermuxService.ACTION_EXECUTE, programUri);
110+
execIntent.setClass(this, TermuxService.class);
111+
execIntent.putExtra(TermuxService.EXTRA_ARGUMENTS, intent.getStringArrayExtra(RUN_COMMAND_ARGUMENTS));
112+
execIntent.putExtra(TermuxService.EXTRA_CURRENT_WORKING_DIRECTORY, parsePath(intent.getStringExtra(RUN_COMMAND_WORKDIR)));
113+
execIntent.putExtra(TermuxService.EXTRA_EXECUTE_IN_BACKGROUND, intent.getBooleanExtra(RUN_COMMAND_BACKGROUND, false));
114+
115+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
116+
this.startForegroundService(execIntent);
117+
} else {
118+
this.startService(execIntent);
109119
}
110120

111121
runStopForeground();

0 commit comments

Comments
 (0)