Skip to content

Commit

Permalink
Create termux-shared library package for all termux constants and sha…
Browse files Browse the repository at this point in the history
…red utils

The termux plugins should use this library instead of hardcoding "com.termux" values in their source code.

The library can be included as a dependency by plugins and third party apps by including the following line in the build.gradle where x.xxx is the version number, once its published.

`implementation 'com.termux:termux-shared:x.xxx'`

The `TermuxConstants` class has been updated to `v0.17.0`, `TermuxPreferenceConstants` to `v0.9.0` and `TermuxPropertyConstants` to `v0.6.0`. Check their Changelog sections for info on changes.

Some typos and redundant code has also been fixed.
  • Loading branch information
agnostic-apollo committed Apr 7, 2021
1 parent c9a476c commit 682ce08
Show file tree
Hide file tree
Showing 73 changed files with 746 additions and 520 deletions.
19 changes: 7 additions & 12 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ plugins {
id "com.android.application"
}

ext.markwon_version='4.6.2'

android {
compileSdkVersion project.properties.compileSdkVersion.toInteger()
ndkVersion project.properties.ndkVersion
Expand All @@ -15,16 +13,13 @@ android {
implementation "androidx.preference:preference:1.1.1"
implementation "androidx.viewpager:viewpager:1.0.0"
implementation "com.google.guava:guava:24.1-jre"
implementation "io.noties.markwon:core:$markwon_version"
implementation "io.noties.markwon:ext-strikethrough:$markwon_version"
implementation "io.noties.markwon:linkify:$markwon_version"
implementation "io.noties.markwon:recycler:$markwon_version"
implementation project(":terminal-view")
implementation "io.noties.markwon:core:$markwonVersion"
implementation "io.noties.markwon:ext-strikethrough:$markwonVersion"
implementation "io.noties.markwon:linkify:$markwonVersion"
implementation "io.noties.markwon:recycler:$markwonVersion"

// Do not increment version higher than 2.5 or there
// will be runtime exceptions on android < 8
// due to missing classes like java.nio.file.Path.
implementation "commons-io:commons-io:2.5"
implementation project(":terminal-view")
implementation project(":termux-shared")
}

defaultConfig {
Expand Down Expand Up @@ -99,7 +94,7 @@ android {
}

dependencies {
testImplementation "junit:junit:4.13.1"
testImplementation "junit:junit:4.13.2"
testImplementation "org.robolectric:robolectric:4.4"
}

Expand Down
29 changes: 13 additions & 16 deletions app/src/main/java/com/termux/app/RunCommandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import android.os.IBinder;

import com.termux.R;
import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.app.file.FileUtils;
import com.termux.app.utils.Logger;
import com.termux.app.utils.NotificationUtils;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.shared.file.FileUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.notification.NotificationUtils;
import com.termux.app.utils.PluginUtils;
import com.termux.app.utils.DataUtils;
import com.termux.shared.data.DataUtils;
import com.termux.app.models.ExecutionCommand;

/**
Expand Down Expand Up @@ -269,10 +270,6 @@
*/
public class RunCommandService extends Service {

private static final String NOTIFICATION_CHANNEL_ID = "termux_run_command_notification_channel";
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " RunCommandService";
public static final int NOTIFICATION_ID = 1338;

private static final String LOG_TAG = "RunCommandService";

class LocalBinder extends Binder {
Expand All @@ -296,7 +293,7 @@ public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.logDebug(LOG_TAG, "onStartCommand");

if(intent == null) return Service.START_NOT_STICKY;;
if(intent == null) return Service.START_NOT_STICKY;

// Run again in case service is already started and onCreate() is not called
runStartForeground();
Expand Down Expand Up @@ -421,7 +418,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
private void runStartForeground() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setupNotificationChannel();
startForeground(NOTIFICATION_ID, buildNotification());
startForeground(TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_ID, buildNotification());
}
}

Expand All @@ -434,8 +431,8 @@ private void runStopForeground() {
private Notification buildNotification() {
// Build the notification
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_LOW,
NOTIFICATION_CHANNEL_NAME, null, null,
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_LOW,
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_NAME, null, null,
null, NotificationUtils.NOTIFICATION_MODE_SILENT);
if(builder == null) return null;

Expand All @@ -454,8 +451,8 @@ private Notification buildNotification() {
private void setupNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;

NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
NotificationUtils.setupNotificationChannel(this, TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_ID,
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
}

}
30 changes: 16 additions & 14 deletions app/src/main/java/com/termux/app/TermuxActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,23 @@
import android.widget.Toast;

import com.termux.R;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
import com.termux.app.activities.HelpActivity;
import com.termux.app.activities.SettingsActivity;
import com.termux.app.crash.CrashUtils;
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
import com.termux.app.terminal.TermuxSessionsListViewController;
import com.termux.app.terminal.io.TerminalToolbarViewPager;
import com.termux.app.terminal.TermuxSessionClient;
import com.termux.app.terminal.TermuxViewClient;
import com.termux.app.terminal.io.extrakeys.ExtraKeysView;
import com.termux.app.settings.properties.TermuxSharedProperties;
import com.termux.app.utils.DialogUtils;
import com.termux.app.utils.Logger;
import com.termux.app.utils.TermuxUtils;
import com.termux.app.settings.properties.TermuxAppSharedProperties;
import com.termux.shared.interact.DialogUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxUtils;
import com.termux.terminal.TerminalSession;
import com.termux.terminal.TerminalSessionClient;
import com.termux.app.utils.CrashUtils;
import com.termux.view.TerminalView;
import com.termux.view.TerminalViewClient;

Expand Down Expand Up @@ -101,7 +102,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
/**
* Termux app shared properties manager, loaded from termux.properties
*/
private TermuxSharedProperties mProperties;
private TermuxAppSharedProperties mProperties;

/**
* The terminal extra keys view.
Expand All @@ -116,7 +117,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
/**
* The {@link TermuxActivity} broadcast receiver for various things like terminal style configuration changes.
*/
private final BroadcastReceiver mTermuxActivityBroadcastReceiever = new TermuxActivityBroadcastReceiever();
private final BroadcastReceiver mTermuxActivityBroadcastReceiever = new TermuxActivityBroadcastReceiver();

/**
* The last toast shown, used cancel current toast before showing new in {@link #showToast(String, boolean)}.
Expand Down Expand Up @@ -161,7 +162,7 @@ public void onCreate(Bundle savedInstanceState) {

// Load termux shared preferences and properties
mPreferences = new TermuxAppSharedPreferences(this);
mProperties = new TermuxSharedProperties(this);
mProperties = new TermuxAppSharedProperties(this);

setActivityTheme();

Expand Down Expand Up @@ -327,7 +328,7 @@ public void onDestroy() {
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
saveTerminalToolbarTextInput(savedInstanceState);
}
Expand Down Expand Up @@ -383,6 +384,7 @@ public void toggleTerminalToolbar() {
if(terminalToolbarViewPager == null) return;

final boolean showNow = mPreferences.toogleShowTerminalToolbar();
Logger.showToast(this, (showNow ? getString(R.string.msg_enabling_terminal_toolbar) : getString(R.string.msg_disabling_terminal_toolbar)), true);
terminalToolbarViewPager.setVisibility(showNow ? View.VISIBLE : View.GONE);
if (showNow && terminalToolbarViewPager.getCurrentItem() == 1) {
// Focus the text input view if just revealed.
Expand Down Expand Up @@ -704,7 +706,7 @@ public TermuxAppSharedPreferences getPreferences() {
return mPreferences;
}

public TermuxSharedProperties getProperties() {
public TermuxAppSharedProperties getProperties() {
return mProperties;
}

Expand All @@ -718,7 +720,7 @@ public static void updateTermuxActivityStyling(Context context) {
context.sendBroadcast(stylingIntent);
}

class TermuxActivityBroadcastReceiever extends BroadcastReceiver {
class TermuxActivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (mIsVisible) {
Expand Down Expand Up @@ -755,7 +757,7 @@ public void onReceive(Context context, Intent intent) {
// TermuxActivity.this.recreate();
}
}
};
}



Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/termux/app/TermuxApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import android.app.Application;

import com.termux.app.crash.CrashHandler;
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
import com.termux.app.utils.Logger;
import com.termux.shared.crash.CrashHandler;
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
import com.termux.shared.logger.Logger;


public class TermuxApplication extends Application {
Expand All @@ -19,7 +19,7 @@ public void onCreate() {
}

private void setLogLevel() {
// Load the log level from shared preferences and set it to the {@link Loggger.CURRENT_LOG_LEVEL}
// Load the log level from shared preferences and set it to the {@link Logger.CURRENT_LOG_LEVEL}
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(getApplicationContext());
preferences.setLogLevel(null, preferences.getLogLevel());
Logger.logDebug("Starting Application");
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/termux/app/TermuxInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import android.view.WindowManager;

import com.termux.R;
import com.termux.app.file.FileUtils;
import com.termux.app.utils.Logger;
import com.termux.shared.file.FileUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/termux/app/TermuxOpenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;

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

import java.io.File;
import java.io.FileNotFoundException;
Expand Down
46 changes: 21 additions & 25 deletions app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@
import android.widget.ArrayAdapter;

import com.termux.R;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
import com.termux.app.terminal.TermuxSession;
import com.termux.app.terminal.TermuxSessionClient;
import com.termux.app.terminal.TermuxSessionClientBase;
import com.termux.app.utils.Logger;
import com.termux.app.utils.NotificationUtils;
import com.termux.app.utils.PermissionUtils;
import com.termux.app.shell.ShellUtils;
import com.termux.app.utils.DataUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.notification.NotificationUtils;
import com.termux.shared.packages.PermissionUtils;
import com.termux.shared.shell.ShellUtils;
import com.termux.shared.data.DataUtils;
import com.termux.app.models.ExecutionCommand;
import com.termux.app.models.ExecutionCommand.ExecutionState;
import com.termux.app.terminal.TermuxTask;
import com.termux.terminal.TerminalEmulator;
import com.termux.terminal.TerminalSession;
import com.termux.terminal.TerminalSessionClient;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -58,10 +58,6 @@
*/
public final class TermuxService extends Service {

private static final String NOTIFICATION_CHANNEL_ID = "termux_notification_channel";
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " App";
public static final int NOTIFICATION_ID = 1337;

private static int EXECUTION_ID = 1000;

/** This service is only bound from inside the same process and never uses IPC. */
Expand Down Expand Up @@ -95,7 +91,7 @@ class LocalBinder extends Binder {
/** The basic implementation of the {@link TerminalSessionClient} interface to be used by {@link TerminalSession}
* that does not hold activity references.
*/
final TermuxSessionClientBase mTermuxSessionClientBase = new TermuxSessionClientBase();;
final TermuxSessionClientBase mTermuxSessionClientBase = new TermuxSessionClientBase();

/** The wake lock and wifi lock are always acquired and released together. */
private PowerManager.WakeLock mWakeLock;
Expand Down Expand Up @@ -183,7 +179,7 @@ public boolean onUnbind(Intent intent) {
/** Make service run in foreground mode. */
private void runStartForeground() {
setupNotificationChannel();
startForeground(NOTIFICATION_ID, buildNotification());
startForeground(TermuxConstants.TERMUX_APP_NOTIFICATION_ID, buildNotification());
}

/** Make service leave foreground mode. */
Expand Down Expand Up @@ -366,7 +362,7 @@ public synchronized TermuxTask createTermuxTask(ExecutionCommand executionComman
if (newTermuxTask == null) {
Logger.logError(LOG_TAG, "Failed to execute new termux task command for:\n" + executionCommand.getCommandIdAndLabelLogString());
return null;
};
}

mTermuxTasks.add(newTermuxTask);

Expand Down Expand Up @@ -436,7 +432,7 @@ public synchronized TermuxSession createTermuxSession(ExecutionCommand execution
if (newTermuxSession == null) {
Logger.logError(LOG_TAG, "Failed to execute new termux session command for:\n" + executionCommand.getCommandIdAndLabelLogString());
return null;
};
}

mTermuxSessions.add(newTermuxSession);

Expand Down Expand Up @@ -593,13 +589,13 @@ private Notification buildNotification() {
// Set notification text
int sessionCount = getTermuxSessionsSize();
int taskCount = mTermuxTasks.size();
String notifiationText = sessionCount + " session" + (sessionCount == 1 ? "" : "s");
String notificationText = sessionCount + " session" + (sessionCount == 1 ? "" : "s");
if (taskCount > 0) {
notifiationText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s");
notificationText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s");
}

final boolean wakeLockHeld = mWakeLock != null;
if (wakeLockHeld) notifiationText += " (wake lock held)";
if (wakeLockHeld) notificationText += " (wake lock held)";


// Set notification priority
Expand All @@ -610,8 +606,8 @@ private Notification buildNotification() {

// Build the notification
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
NOTIFICATION_CHANNEL_ID, priority,
getText(R.string.application_name), notifiationText, null,
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID, priority,
getText(R.string.application_name), notificationText, null,
pendingIntent, NotificationUtils.NOTIFICATION_MODE_SILENT);
if(builder == null) return null;

Expand Down Expand Up @@ -647,8 +643,8 @@ private Notification buildNotification() {
private void setupNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;

NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
NotificationUtils.setupNotificationChannel(this, TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID,
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
}

/** Update the shown foreground service notification after making any changes that affect it. */
Expand All @@ -657,7 +653,7 @@ void updateNotification() {
// Exit if we are updating after the user disabled all locks with no sessions or tasks running.
requestStopService();
} else {
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, buildNotification());
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(TermuxConstants.TERMUX_APP_NOTIFICATION_ID, buildNotification());
}
}

Expand Down
Loading

0 comments on commit 682ce08

Please sign in to comment.