Skip to content

Commit

Permalink
Changed: Remove hardcoded com.termux values
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Mar 14, 2022
1 parent 9a1e19c commit 3bea194
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 35 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/termux/api/SocketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.LocalSocket;

import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

import java.io.BufferedWriter;
import java.io.DataInputStream;
Expand All @@ -19,7 +20,7 @@

public class SocketListener {

public static final String LISTEN_ADDRESS = "com.termux.api://listen";
public static final String LISTEN_ADDRESS = TermuxConstants.TERMUX_API_PACKAGE_NAME + "://listen";
private static final Pattern EXTRA_STRING = Pattern.compile("(-e|--es|--esa) +([^ ]+) +\"(.*?)(?<!\\\\)\"", Pattern.DOTALL);
private static final Pattern EXTRA_BOOLEAN = Pattern.compile("--ez +([^ ]+) +([^ ]+)");
private static final Pattern EXTRA_INT = Pattern.compile("--ei +([^ ]+) +(-?[0-9]+)");
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/termux/api/TermuxAPIConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.termux.api;

import com.termux.shared.termux.TermuxConstants;
import static com.termux.shared.termux.TermuxConstants.TERMUX_API_PACKAGE_NAME;
import static com.termux.shared.termux.TermuxConstants.TERMUX_PACKAGE_NAME;

public class TermuxAPIConstants {

/**
* Termux:API Receiver name.
*/
public static final String TERMUX_API_RECEIVER_NAME = TERMUX_API_PACKAGE_NAME + ".TermuxApiReceiver"; // Default to "com.termux.api.TermuxApiReceiver"

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.termux.api.util.ResultReturner;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

import java.util.ArrayList;

Expand All @@ -19,7 +20,7 @@ public class TermuxApiPermissionActivity extends Activity {
/**
* Intent extra containing the permissions to request.
*/
public static final String PERMISSIONS_EXTRA = "com.termux.api.permission_extra";
public static final String PERMISSIONS_EXTRA = TermuxConstants.TERMUX_API_PACKAGE_NAME + ".permission_extra";

/**
* Check for and request permissions if necessary.
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/termux/api/apis/DialogAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.termux.api.util.ResultReturner;
import com.termux.api.activities.TermuxApiPermissionActivity;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.theme.TermuxThemeUtils;
import com.termux.shared.theme.NightMode;
import com.termux.shared.theme.ThemeUtils;
Expand Down Expand Up @@ -702,7 +703,7 @@ protected boolean isCurrentAppTermux() {
for (final ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (final String activeProcess : processInfo.pkgList) {
if (activeProcess.equals("com.termux")) {
if (activeProcess.equals(TermuxConstants.TERMUX_PACKAGE_NAME)) {
return true;
}
}
Expand Down
27 changes: 15 additions & 12 deletions app/src/main/java/com/termux/api/apis/JobSchedulerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.termux.api.TermuxApiReceiver;
import com.termux.api.util.ResultReturner;
import com.termux.shared.logger.Logger;
import com.termux.shared.shell.command.ExecutionCommand;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -207,12 +210,7 @@ private static void cancelJob(TermuxApiReceiver apiReceiver, Intent intent, JobS

public static class JobSchedulerService extends JobService {

public static final String SCRIPT_FILE_PATH = "com.termux.api.jobscheduler_script_path";

// Constants from TermuxService.
private static final String TERMUX_SERVICE = "com.termux.app.TermuxService";
private static final String ACTION_EXECUTE = "com.termux.service_execute";
private static final String EXTRA_EXECUTE_IN_BACKGROUND = "com.termux.execute.background";
public static final String SCRIPT_FILE_PATH = TermuxConstants.TERMUX_API_PACKAGE_NAME + ".jobscheduler_script_path";

private static final String LOG_TAG = "JobSchedulerService";

Expand All @@ -223,17 +221,22 @@ public boolean onStartJob(JobParameters params) {
PersistableBundle extras = params.getExtras();
String filePath = extras.getString(SCRIPT_FILE_PATH);

Uri scriptUri = new Uri.Builder().scheme("com.termux.file").path(filePath).build();
Intent executeIntent = new Intent(ACTION_EXECUTE, scriptUri);
executeIntent.setClassName("com.termux", TERMUX_SERVICE);
executeIntent.putExtra(EXTRA_EXECUTE_IN_BACKGROUND, true);
ExecutionCommand executionCommand = new ExecutionCommand();
executionCommand.executableUri = new Uri.Builder().scheme(TERMUX_SERVICE.URI_SCHEME_SERVICE_EXECUTE).path(filePath).build();
executionCommand.runner = ExecutionCommand.Runner.APP_SHELL.getName();

// Create execution intent with the action TERMUX_SERVICE#ACTION_SERVICE_EXECUTE to be sent to the TERMUX_SERVICE
Intent executionIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, executionCommand.executableUri);
executionIntent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.TERMUX_SERVICE_NAME);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_RUNNER, executionCommand.runner);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND, true); // Also pass in case user using termux-app version < 0.119.0

Context context = getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// https://developer.android.com/about/versions/oreo/background.html
context.startForegroundService(executeIntent);
context.startForegroundService(executionIntent);
} else {
context.startService(executeIntent);
context.startService(executionIntent);
}

Logger.logInfo(LOG_TAG, "Job started for \"" + filePath + "\"");
Expand Down
36 changes: 18 additions & 18 deletions app/src/main/java/com/termux/api/apis/NotificationAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@
import androidx.core.util.Pair;

import com.termux.api.R;
import com.termux.api.TermuxAPIConstants;
import com.termux.api.TermuxApiReceiver;
import com.termux.api.util.ResultReturner;
import com.termux.shared.logger.Logger;
import com.termux.shared.shell.command.ExecutionCommand;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;

import java.io.File;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.UUID;

import static com.termux.shared.termux.TermuxConstants.TERMUX_PREFIX_DIR_PATH;

public class NotificationAPI {

private static final String LOG_TAG = "NotificationAPI";

public static final String TERMUX_SERVICE = "com.termux.app.TermuxService";
public static final String ACTION_EXECUTE = "com.termux.service_execute";
public static final String EXTRA_ARGUMENTS = "com.termux.execute.arguments";
public static final String BIN_SH = TERMUX_PREFIX_DIR_PATH+"/bin/sh";
private static final String EXTRA_EXECUTE_IN_BACKGROUND = "com.termux.execute.background";
public static final String BIN_SH = TermuxConstants.TERMUX_PREFIX_DIR_PATH + "/bin/sh";
private static final String CHANNEL_ID = "termux-notification";
private static final String CHANNEL_TITLE = "Termux API notification channel";
private static final String KEY_TEXT_REPLY = "TERMUX_TEXT_REPLY";
Expand Down Expand Up @@ -359,7 +357,7 @@ private static Intent getMessageReplyIntent(Intent oldIntent,
String buttonText, String buttonAction,
String notificationId) {
Intent intent = oldIntent.
setClassName("com.termux.api", "com.termux.api.TermuxApiReceiver").
setClassName(TermuxConstants.TERMUX_API_PACKAGE_NAME, TermuxAPIConstants.TERMUX_API_RECEIVER_NAME).
putExtra("api_method", "NotificationReply").
putExtra("id", notificationId).
putExtra("action", buttonAction).
Expand Down Expand Up @@ -410,16 +408,18 @@ public static void onReceiveReplyToNotification(TermuxApiReceiver termuxApiRecei
}

static Intent createExecuteIntent(String action){
String[] arguments = new String[]{"-c", action};
Uri executeUri = new Uri.Builder().scheme("com.termux.file")
.path(BIN_SH)
.appendQueryParameter("arguments", Arrays.toString(arguments))
.build();
Intent executeIntent = new Intent(ACTION_EXECUTE, executeUri);
executeIntent.setClassName("com.termux", TERMUX_SERVICE);
executeIntent.putExtra(EXTRA_EXECUTE_IN_BACKGROUND, true);
executeIntent.putExtra(EXTRA_ARGUMENTS, arguments);
return executeIntent;
ExecutionCommand executionCommand = new ExecutionCommand();
executionCommand.executableUri = new Uri.Builder().scheme(TERMUX_SERVICE.URI_SCHEME_SERVICE_EXECUTE).path(BIN_SH).build();
executionCommand.arguments = new String[]{"-c", action};
executionCommand.runner = ExecutionCommand.Runner.APP_SHELL.getName();

// Create execution intent with the action TERMUX_SERVICE#ACTION_SERVICE_EXECUTE to be sent to the TERMUX_SERVICE
Intent executionIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, executionCommand.executableUri);
executionIntent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.TERMUX_SERVICE_NAME);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_ARGUMENTS, executionCommand.arguments);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_RUNNER, executionCommand.runner);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND, true); // Also pass in case user using termux-app version < 0.119.0
return executionIntent;
}

static PendingIntent createAction(final Context context, String action){
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/termux/api/apis/StorageGetAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.termux.api.util.ResultReturner;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -21,7 +22,7 @@

public class StorageGetAPI {

private static final String FILE_EXTRA = "com.termux.api.storage.file";
private static final String FILE_EXTRA = TermuxConstants.TERMUX_API_PACKAGE_NAME + ".storage.file";

private static final String LOG_TAG = "StorageGetAPI";

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/termux/api/apis/UsbAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.termux.api.TermuxApiReceiver;
import com.termux.api.util.ResultReturner;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -112,7 +113,7 @@ private static boolean requestPermission(final @NonNull UsbDevice device, final
Looper looper = Looper.myLooper();
final boolean[] result = new boolean[1];

final String ACTION_USB_PERMISSION = "com.termux.api.USB_PERMISSION";
final String ACTION_USB_PERMISSION = TermuxConstants.TERMUX_API_PACKAGE_NAME + ".USB_PERMISSION";
final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context usbContext, final Intent usbIntent) {
Expand Down

0 comments on commit 3bea194

Please sign in to comment.