Skip to content

Commit 682ce08

Browse files
Create termux-shared library package for all termux constants and shared 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.
1 parent c9a476c commit 682ce08

File tree

73 files changed

+746
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+746
-520
lines changed

app/build.gradle

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ plugins {
22
id "com.android.application"
33
}
44

5-
ext.markwon_version='4.6.2'
6-
75
android {
86
compileSdkVersion project.properties.compileSdkVersion.toInteger()
97
ndkVersion project.properties.ndkVersion
@@ -15,16 +13,13 @@ android {
1513
implementation "androidx.preference:preference:1.1.1"
1614
implementation "androidx.viewpager:viewpager:1.0.0"
1715
implementation "com.google.guava:guava:24.1-jre"
18-
implementation "io.noties.markwon:core:$markwon_version"
19-
implementation "io.noties.markwon:ext-strikethrough:$markwon_version"
20-
implementation "io.noties.markwon:linkify:$markwon_version"
21-
implementation "io.noties.markwon:recycler:$markwon_version"
22-
implementation project(":terminal-view")
16+
implementation "io.noties.markwon:core:$markwonVersion"
17+
implementation "io.noties.markwon:ext-strikethrough:$markwonVersion"
18+
implementation "io.noties.markwon:linkify:$markwonVersion"
19+
implementation "io.noties.markwon:recycler:$markwonVersion"
2320

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

3025
defaultConfig {
@@ -99,7 +94,7 @@ android {
9994
}
10095

10196
dependencies {
102-
testImplementation "junit:junit:4.13.1"
97+
testImplementation "junit:junit:4.13.2"
10398
testImplementation "org.robolectric:robolectric:4.4"
10499
}
105100

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

+13-16
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import android.os.IBinder;
1313

1414
import com.termux.R;
15-
import com.termux.app.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
16-
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
17-
import com.termux.app.file.FileUtils;
18-
import com.termux.app.utils.Logger;
19-
import com.termux.app.utils.NotificationUtils;
15+
import com.termux.shared.termux.TermuxConstants;
16+
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
17+
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
18+
import com.termux.shared.file.FileUtils;
19+
import com.termux.shared.logger.Logger;
20+
import com.termux.shared.notification.NotificationUtils;
2021
import com.termux.app.utils.PluginUtils;
21-
import com.termux.app.utils.DataUtils;
22+
import com.termux.shared.data.DataUtils;
2223
import com.termux.app.models.ExecutionCommand;
2324

2425
/**
@@ -269,10 +270,6 @@
269270
*/
270271
public class RunCommandService extends Service {
271272

272-
private static final String NOTIFICATION_CHANNEL_ID = "termux_run_command_notification_channel";
273-
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " RunCommandService";
274-
public static final int NOTIFICATION_ID = 1338;
275-
276273
private static final String LOG_TAG = "RunCommandService";
277274

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

299-
if(intent == null) return Service.START_NOT_STICKY;;
296+
if(intent == null) return Service.START_NOT_STICKY;
300297

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

@@ -434,8 +431,8 @@ private void runStopForeground() {
434431
private Notification buildNotification() {
435432
// Build the notification
436433
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
437-
NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_LOW,
438-
NOTIFICATION_CHANNEL_NAME, null, null,
434+
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_LOW,
435+
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_NAME, null, null,
439436
null, NotificationUtils.NOTIFICATION_MODE_SILENT);
440437
if(builder == null) return null;
441438

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

457-
NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
458-
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
454+
NotificationUtils.setupNotificationChannel(this, TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_ID,
455+
TermuxConstants.TERMUX_RUN_COMMAND_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
459456
}
460457

461458
}

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@
3131
import android.widget.Toast;
3232

3333
import com.termux.R;
34-
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
34+
import com.termux.shared.termux.TermuxConstants;
35+
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
3536
import com.termux.app.activities.HelpActivity;
3637
import com.termux.app.activities.SettingsActivity;
37-
import com.termux.app.crash.CrashUtils;
38-
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
38+
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
3939
import com.termux.app.terminal.TermuxSessionsListViewController;
4040
import com.termux.app.terminal.io.TerminalToolbarViewPager;
4141
import com.termux.app.terminal.TermuxSessionClient;
4242
import com.termux.app.terminal.TermuxViewClient;
4343
import com.termux.app.terminal.io.extrakeys.ExtraKeysView;
44-
import com.termux.app.settings.properties.TermuxSharedProperties;
45-
import com.termux.app.utils.DialogUtils;
46-
import com.termux.app.utils.Logger;
47-
import com.termux.app.utils.TermuxUtils;
44+
import com.termux.app.settings.properties.TermuxAppSharedProperties;
45+
import com.termux.shared.interact.DialogUtils;
46+
import com.termux.shared.logger.Logger;
47+
import com.termux.shared.termux.TermuxUtils;
4848
import com.termux.terminal.TerminalSession;
4949
import com.termux.terminal.TerminalSessionClient;
50+
import com.termux.app.utils.CrashUtils;
5051
import com.termux.view.TerminalView;
5152
import com.termux.view.TerminalViewClient;
5253

@@ -101,7 +102,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
101102
/**
102103
* Termux app shared properties manager, loaded from termux.properties
103104
*/
104-
private TermuxSharedProperties mProperties;
105+
private TermuxAppSharedProperties mProperties;
105106

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

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

162163
// Load termux shared preferences and properties
163164
mPreferences = new TermuxAppSharedPreferences(this);
164-
mProperties = new TermuxSharedProperties(this);
165+
mProperties = new TermuxAppSharedProperties(this);
165166

166167
setActivityTheme();
167168

@@ -327,7 +328,7 @@ public void onDestroy() {
327328
}
328329

329330
@Override
330-
public void onSaveInstanceState(Bundle savedInstanceState) {
331+
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
331332
super.onSaveInstanceState(savedInstanceState);
332333
saveTerminalToolbarTextInput(savedInstanceState);
333334
}
@@ -383,6 +384,7 @@ public void toggleTerminalToolbar() {
383384
if(terminalToolbarViewPager == null) return;
384385

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

707-
public TermuxSharedProperties getProperties() {
709+
public TermuxAppSharedProperties getProperties() {
708710
return mProperties;
709711
}
710712

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

721-
class TermuxActivityBroadcastReceiever extends BroadcastReceiver {
723+
class TermuxActivityBroadcastReceiver extends BroadcastReceiver {
722724
@Override
723725
public void onReceive(Context context, Intent intent) {
724726
if (mIsVisible) {
@@ -755,7 +757,7 @@ public void onReceive(Context context, Intent intent) {
755757
// TermuxActivity.this.recreate();
756758
}
757759
}
758-
};
760+
}
759761

760762

761763

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import android.app.Application;
44

5-
import com.termux.app.crash.CrashHandler;
6-
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
7-
import com.termux.app.utils.Logger;
5+
import com.termux.shared.crash.CrashHandler;
6+
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
7+
import com.termux.shared.logger.Logger;
88

99

1010
public class TermuxApplication extends Application {
@@ -19,7 +19,7 @@ public void onCreate() {
1919
}
2020

2121
private void setLogLevel() {
22-
// Load the log level from shared preferences and set it to the {@link Loggger.CURRENT_LOG_LEVEL}
22+
// Load the log level from shared preferences and set it to the {@link Logger.CURRENT_LOG_LEVEL}
2323
TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(getApplicationContext());
2424
preferences.setLogLevel(null, preferences.getLogLevel());
2525
Logger.logDebug("Starting Application");

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
import android.view.WindowManager;
1212

1313
import com.termux.R;
14-
import com.termux.app.file.FileUtils;
15-
import com.termux.app.utils.Logger;
14+
import com.termux.shared.file.FileUtils;
15+
import com.termux.shared.logger.Logger;
16+
import com.termux.shared.termux.TermuxConstants;
1617

1718
import java.io.BufferedReader;
1819
import java.io.ByteArrayInputStream;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import android.provider.MediaStore;
1414
import android.webkit.MimeTypeMap;
1515

16-
import com.termux.app.utils.Logger;
16+
import com.termux.shared.logger.Logger;
17+
import com.termux.shared.termux.TermuxConstants;
1718

1819
import java.io.File;
1920
import java.io.FileNotFoundException;

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

+21-25
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@
2020
import android.widget.ArrayAdapter;
2121

2222
import com.termux.R;
23-
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
24-
import com.termux.app.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
25-
import com.termux.app.settings.preferences.TermuxAppSharedPreferences;
23+
import com.termux.shared.termux.TermuxConstants;
24+
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY;
25+
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
26+
import com.termux.shared.settings.preferences.TermuxAppSharedPreferences;
2627
import com.termux.app.terminal.TermuxSession;
2728
import com.termux.app.terminal.TermuxSessionClient;
2829
import com.termux.app.terminal.TermuxSessionClientBase;
29-
import com.termux.app.utils.Logger;
30-
import com.termux.app.utils.NotificationUtils;
31-
import com.termux.app.utils.PermissionUtils;
32-
import com.termux.app.shell.ShellUtils;
33-
import com.termux.app.utils.DataUtils;
30+
import com.termux.shared.logger.Logger;
31+
import com.termux.shared.notification.NotificationUtils;
32+
import com.termux.shared.packages.PermissionUtils;
33+
import com.termux.shared.shell.ShellUtils;
34+
import com.termux.shared.data.DataUtils;
3435
import com.termux.app.models.ExecutionCommand;
3536
import com.termux.app.models.ExecutionCommand.ExecutionState;
3637
import com.termux.app.terminal.TermuxTask;
3738
import com.termux.terminal.TerminalEmulator;
3839
import com.termux.terminal.TerminalSession;
3940
import com.termux.terminal.TerminalSessionClient;
4041

41-
import java.io.File;
4242
import java.util.ArrayList;
4343
import java.util.List;
4444

@@ -58,10 +58,6 @@
5858
*/
5959
public final class TermuxService extends Service {
6060

61-
private static final String NOTIFICATION_CHANNEL_ID = "termux_notification_channel";
62-
private static final String NOTIFICATION_CHANNEL_NAME = TermuxConstants.TERMUX_APP_NAME + " App";
63-
public static final int NOTIFICATION_ID = 1337;
64-
6561
private static int EXECUTION_ID = 1000;
6662

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

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

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

371367
mTermuxTasks.add(newTermuxTask);
372368

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

441437
mTermuxSessions.add(newTermuxSession);
442438

@@ -593,13 +589,13 @@ private Notification buildNotification() {
593589
// Set notification text
594590
int sessionCount = getTermuxSessionsSize();
595591
int taskCount = mTermuxTasks.size();
596-
String notifiationText = sessionCount + " session" + (sessionCount == 1 ? "" : "s");
592+
String notificationText = sessionCount + " session" + (sessionCount == 1 ? "" : "s");
597593
if (taskCount > 0) {
598-
notifiationText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s");
594+
notificationText += ", " + taskCount + " task" + (taskCount == 1 ? "" : "s");
599595
}
600596

601597
final boolean wakeLockHeld = mWakeLock != null;
602-
if (wakeLockHeld) notifiationText += " (wake lock held)";
598+
if (wakeLockHeld) notificationText += " (wake lock held)";
603599

604600

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

611607
// Build the notification
612608
Notification.Builder builder = NotificationUtils.geNotificationBuilder(this,
613-
NOTIFICATION_CHANNEL_ID, priority,
614-
getText(R.string.application_name), notifiationText, null,
609+
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID, priority,
610+
getText(R.string.application_name), notificationText, null,
615611
pendingIntent, NotificationUtils.NOTIFICATION_MODE_SILENT);
616612
if(builder == null) return null;
617613

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

650-
NotificationUtils.setupNotificationChannel(this, NOTIFICATION_CHANNEL_ID,
651-
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
646+
NotificationUtils.setupNotificationChannel(this, TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_ID,
647+
TermuxConstants.TERMUX_APP_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
652648
}
653649

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

0 commit comments

Comments
 (0)