Skip to content

Commit

Permalink
Added: Add support for custom log level for background commands
Browse files Browse the repository at this point in the history
Values must be between `Logger.LOG_LEVEL_OFF (0)` and `Logger.MAX_LOG_LEVEL` (currently `Logger.LOG_LEVEL_VERBOSE (3)` as per termux/termux-app@60f37bde
  • Loading branch information
agnostic-apollo committed Sep 11, 2021
1 parent 6f6ddd0 commit 5bf1518
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
37 changes: 34 additions & 3 deletions app/src/main/java/com/termux/tasker/EditConfigurationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public final class EditConfigurationActivity extends AbstractPluginActivity {
private AutoCompleteTextView mWorkingDirectoryPathText;
private TextView mStdinView;
private EditText mSessionAction;
private EditText mBackgroundCustomLogLevel;
private CheckBox mInTerminalCheckbox;
private CheckBox mWaitForResult;
private TextView mExecutableAbsolutePathText;
Expand Down Expand Up @@ -113,6 +114,7 @@ protected void onCreate(final Bundle savedInstanceState) {
mWorkingDirectoryPathText = findViewById(R.id.working_directory_path);
mStdinView = findViewById(R.id.view_stdin);
mSessionAction = findViewById(R.id.session_action);
mBackgroundCustomLogLevel = findViewById(R.id.background_custom_log_level);
mInTerminalCheckbox = findViewById(R.id.in_terminal);
mWaitForResult = findViewById(R.id.wait_for_result);
mExecutableAbsolutePathText = findViewById(R.id.executable_absolute_path);
Expand All @@ -126,6 +128,7 @@ protected void onCreate(final Bundle savedInstanceState) {
setWorkingDirectoryPathViews();
setStdinView();
setSessionActionViews();
setBackgroundCustomLogLevelViews();
setInTerminalView();

// Currently savedInstanceState bundle is not supported
Expand Down Expand Up @@ -164,6 +167,11 @@ protected void onCreate(final Bundle savedInstanceState) {
processSessionAction(sessionAction);
updateSessionActionViewVisibility(inTerminal);

final String backgroundCustomLogLevel = localeBundle.getString(PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL);
mBackgroundCustomLogLevel.setText(backgroundCustomLogLevel);
processBackgroundCustomLogLevel(backgroundCustomLogLevel);
updateBackgroundCustomLogLevelViewVisibility(inTerminal);

final boolean waitForResult = localeBundle.getBoolean(PluginBundleManager.EXTRA_WAIT_FOR_RESULT);
mWaitForResult.setChecked(waitForResult);
}
Expand Down Expand Up @@ -280,12 +288,28 @@ private void updateSessionActionViewVisibility(boolean inTerminal) {
}


private void setBackgroundCustomLogLevelViews() {
mBackgroundCustomLogLevel.addTextChangedListener(new AfterTextChangedWatcher() {
@Override
public void afterTextChanged(Editable editable) {
processBackgroundCustomLogLevel(editable == null ? null : editable.toString());
}
});
}

private void updateBackgroundCustomLogLevelViewVisibility(boolean inTerminal) {
if (mBackgroundCustomLogLevel == null) return;
mBackgroundCustomLogLevel.setVisibility(inTerminal ? View.GONE : View.VISIBLE);
}


private void setInTerminalView() {
mInTerminalCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateStdinViewVisibility(isChecked);
updateSessionActionViewVisibility(isChecked);
updateBackgroundCustomLogLevelViewVisibility(isChecked);
}
});
}
Expand Down Expand Up @@ -582,6 +606,11 @@ private void processSessionAction(String sessionActionString) {
TERMUX_SERVICE.MIN_VALUE_EXTRA_SESSION_ACTION, TERMUX_SERVICE.MAX_VALUE_EXTRA_SESSION_ACTION);
}

private void processBackgroundCustomLogLevel(String backgroundCustomLogLevelString) {
processIntFieldValue(mBackgroundCustomLogLevel, backgroundCustomLogLevelString,
Logger.LOG_LEVEL_OFF, Logger.MAX_LOG_LEVEL);
}

private void processIntFieldValue(EditText editText, String stringValue, int min, int max) {
if (editText == null) return;
editText.setError(null);
Expand Down Expand Up @@ -618,6 +647,7 @@ public void finish() {
final String arguments = DataUtils.getDefaultIfUnset(mArgumentsText.getText() == null ? null : mArgumentsText.getText().toString(), null);
final String workingDirectory = DataUtils.getDefaultIfUnset(mWorkingDirectoryPathText.getText() == null ? null : mWorkingDirectoryPathText.getText().toString(), null);
final String sessionAction = DataUtils.getDefaultIfUnset(mSessionAction.getText() == null ? null : mSessionAction.getText().toString(), null);
final String backgroundCustomLogLevel = DataUtils.getDefaultIfUnset(mBackgroundCustomLogLevel.getText() == null ? null : mBackgroundCustomLogLevel.getText().toString(), null);
final boolean inTerminal = mInTerminalCheckbox.isChecked();
final boolean waitForResult = mWaitForResult.isChecked();

Expand All @@ -637,7 +667,7 @@ public void finish() {
* stored in the Bundle, as Locale's classloader will not recognize it).
*/
final Bundle resultBundle = PluginBundleManager.generateBundle(getApplicationContext(),
executable, arguments, workingDirectory, mStdin, sessionAction, inTerminal, waitForResult);
executable, arguments, workingDirectory, mStdin, sessionAction, backgroundCustomLogLevel, inTerminal, waitForResult);
if (resultBundle == null) {
Logger.showToast(this, getString(R.string.error_generate_plugin_bundle_failed), true);
setResult(RESULT_CODE_FAILED, resultIntent);
Expand All @@ -649,7 +679,7 @@ public void finish() {

// The blurb is a concise status text to be displayed in the host's UI.
final String blurb = PluginBundleManager.generateBlurb(this, executable, arguments,
workingDirectory, mStdin, sessionAction, inTerminal, waitForResult);
workingDirectory, mStdin, sessionAction, backgroundCustomLogLevel, inTerminal, waitForResult);

// If host supports variable replacement when running plugin action, then
// request it to replace variables in following fields
Expand All @@ -659,7 +689,8 @@ public void finish() {
PluginBundleManager.EXTRA_ARGUMENTS,
PluginBundleManager.EXTRA_WORKDIR,
PluginBundleManager.EXTRA_STDIN,
PluginBundleManager.EXTRA_SESSION_ACTION
PluginBundleManager.EXTRA_SESSION_ACTION,
PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL
});
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/termux/tasker/FireReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void onReceive(final Context context, final Intent intent) {

if (executionCommand.inBackground) {
executionCommand.stdin = IntentUtils.getStringExtraIfSet(intent, PluginBundleManager.EXTRA_STDIN, null);
executionCommand.backgroundCustomLogLevel = IntentUtils.getIntegerExtraIfSet(intent, PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, null);
} else {
executionCommand.sessionAction = IntentUtils.getStringExtraIfSet(intent, PluginBundleManager.EXTRA_SESSION_ACTION, null);
}
Expand Down Expand Up @@ -187,6 +188,7 @@ public void onReceive(final Context context, final Intent intent) {
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_WORKDIR, executionCommand.workingDirectory);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_STDIN, executionCommand.stdin);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_SESSION_ACTION, executionCommand.sessionAction);
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, DataUtils.getStringFromInteger(executionCommand.backgroundCustomLogLevel, null));
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND, executionCommand.inBackground);

// Send execution intent to TERMUX_SERVICE
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/com/termux/tasker/PluginBundleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class PluginBundleManager {
*/
public static final String EXTRA_SESSION_ACTION = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.SESSION_ACTION"; // Default: "com.termux.tasker.extra.SESSION_ACTION"

/** The {@code String} extra for custom log levels for background commands between
* {@link Logger#LOG_LEVEL_OFF} and {@link Logger#MAX_LOG_LEVEL} as per
* https://github.com/termux/termux-app/commit/60f37bde.
*/
public static final String EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.BACKGROUND_CUSTOM_LOG_LEVEL"; // Default: "com.termux.tasker.extra.BACKGROUND_CUSTOM_LOG_LEVEL"

/** The {@code boolean} extra for whether the executable should be run inside a terminal. */
public static final String EXTRA_TERMINAL = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.TERMINAL"; // Default: "com.termux.tasker.extra.TERMINAL"

Expand Down Expand Up @@ -80,6 +86,7 @@ public static String parseBundle(@NonNull final Context context, final Bundle bu
* - EXTRA_WORKDIR
* - EXTRA_STDIN
* - EXTRA_SESSION_ACTION
* - EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL
* - EXTRA_TERMINAL
* - EXTRA_WAIT_FOR_RESULT
* - VARIABLE_REPLACE_KEYS
Expand All @@ -98,13 +105,13 @@ public static String parseBundle(@NonNull final Context context, final Bundle bu
}

/*
* Check if bundle contains at least 3 keys but no more than 9.
* Check if bundle contains at least 3 keys but no more than 10.
* Run this test after checking for required Bundle extras above so that the error message
* is more useful. (E.g. the caller will see what extras are missing, rather than just a
* message that there is the wrong number).
*/
if (bundle.keySet().size() < 3 || bundle.keySet().size() > 9) {
return String.format("The bundle must contain 3-9 keys, but currently contains %d keys.", bundle.keySet().size());
if (bundle.keySet().size() < 3 || bundle.keySet().size() > 10) {
return String.format("The bundle must contain 3-10 keys, but currently contains %d keys.", bundle.keySet().size());
}

if (TextUtils.isEmpty(bundle.getString(EXTRA_EXECUTABLE))) {
Expand All @@ -131,6 +138,7 @@ public static String parseBundle(@NonNull final Context context, final Bundle bu
public static Bundle generateBundle(@NonNull final Context context, final String executable,
final String arguments, final String workingDirectory,
final String stdin, final String sessionAction,
final String backgroundCustomLogLevel,
final boolean inTerminal, final boolean waitForResult) {
final Bundle result = new Bundle();
result.putString(EXTRA_EXECUTABLE, executable);
Expand All @@ -141,6 +149,7 @@ public static Bundle generateBundle(@NonNull final Context context, final String

if (!inTerminal) {
result.putString(EXTRA_STDIN, stdin);
result.putString(EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, backgroundCustomLogLevel);
} else {
result.putString(EXTRA_SESSION_ACTION, sessionAction);
}
Expand All @@ -163,13 +172,15 @@ public static Bundle generateBundle(@NonNull final Context context, final String
public static String generateBlurb(@NonNull final Context context, final String executable,
final String arguments, final String workingDirectory,
final String stdin, final String sessionAction,
final String backgroundCustomLogLevel,
final boolean inTerminal, final boolean waitForResult) {
StringBuilder builder = new StringBuilder();
builder.append(context.getString(R.string.blurb_executable_and_arguments, executable, arguments == null ? "" : " " + arguments));
builder.append("\n\n").append(context.getString(R.string.blurb_working_directory, (!DataUtils.isNullOrEmpty(workingDirectory) ? UNICODE_CHECK : UNICODE_UNCHECK)));

if (!inTerminal) {
builder.append("\n").append(context.getString(R.string.blurb_stdin, (!DataUtils.isNullOrEmpty(stdin) ? UNICODE_CHECK : UNICODE_UNCHECK)));
builder.append("\n").append(context.getString(R.string.blurb_custom_log_level, backgroundCustomLogLevel));
} else {
if (!DataUtils.isNullOrEmpty(sessionAction))
builder.append("\n").append(context.getString(R.string.blurb_session_action, sessionAction));
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/res/layout/edit_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,46 @@





<!-- Background Custom Log Level -->
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:padding="@dimen/activity_view_padding_mini"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text="@string/title_background_custom_log_level"
tools:labelFor="@id/background_custom_log_level" />

<EditText
android:id="@+id/background_custom_log_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:importantForAutofill="no" />

</LinearLayout>
</androidx.cardview.widget.CardView>





<CheckBox
android:id="@+id/in_terminal"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<string name="title_working_directory_path">Working directory path</string>
<string name="title_stdin">Stdin</string>
<string name="title_session_action">Terminal Session Action</string>
<string name="title_background_custom_log_level">Custom Log Level</string>
<string name="title_execute_in_terminal">Execute in a terminal session</string>
<string name="title_wait_for_result">Wait for result for commands (Requires timeout > 0)</string>

Expand All @@ -45,6 +46,7 @@
<string name="blurb_executable_and_arguments">%1$s%2$s</string>
<string name="blurb_working_directory">Working Directory %1$s</string>
<string name="blurb_stdin">Stdin %1$s</string>
<string name="blurb_custom_log_level">Custom Log Level %1$s</string>
<string name="blurb_session_action">Session Action %1$s</string>
<string name="blurb_in_terminal">Terminal Session %1$s</string>
<string name="blurb_wait_for_result">Wait For Result %1$s</string>
Expand Down

0 comments on commit 5bf1518

Please sign in to comment.