From 5bf151898b9b6d38abc31b376d10d6bf410652f5 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sat, 11 Sep 2021 16:49:01 +0500 Subject: [PATCH] Added: Add support for custom log level for background commands Values must be between `Logger.LOG_LEVEL_OFF (0)` and `Logger.MAX_LOG_LEVEL` (currently `Logger.LOG_LEVEL_VERBOSE (3)` as per https://github.com/termux/termux-app/commit/60f37bde --- .../tasker/EditConfigurationActivity.java | 37 +++++++++++++++-- .../java/com/termux/tasker/FireReceiver.java | 2 + .../termux/tasker/PluginBundleManager.java | 17 ++++++-- app/src/main/res/layout/edit_activity.xml | 40 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 5 files changed, 92 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/termux/tasker/EditConfigurationActivity.java b/app/src/main/java/com/termux/tasker/EditConfigurationActivity.java index b53faf2..3059b46 100644 --- a/app/src/main/java/com/termux/tasker/EditConfigurationActivity.java +++ b/app/src/main/java/com/termux/tasker/EditConfigurationActivity.java @@ -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; @@ -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); @@ -126,6 +128,7 @@ protected void onCreate(final Bundle savedInstanceState) { setWorkingDirectoryPathViews(); setStdinView(); setSessionActionViews(); + setBackgroundCustomLogLevelViews(); setInTerminalView(); // Currently savedInstanceState bundle is not supported @@ -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); } @@ -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); } }); } @@ -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); @@ -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(); @@ -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); @@ -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 @@ -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 }); } diff --git a/app/src/main/java/com/termux/tasker/FireReceiver.java b/app/src/main/java/com/termux/tasker/FireReceiver.java index 2526182..fff5c18 100644 --- a/app/src/main/java/com/termux/tasker/FireReceiver.java +++ b/app/src/main/java/com/termux/tasker/FireReceiver.java @@ -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); } @@ -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 diff --git a/app/src/main/java/com/termux/tasker/PluginBundleManager.java b/app/src/main/java/com/termux/tasker/PluginBundleManager.java index 198b187..d196acf 100644 --- a/app/src/main/java/com/termux/tasker/PluginBundleManager.java +++ b/app/src/main/java/com/termux/tasker/PluginBundleManager.java @@ -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" @@ -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 @@ -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))) { @@ -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); @@ -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); } @@ -163,6 +172,7 @@ 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)); @@ -170,6 +180,7 @@ public static String generateBlurb(@NonNull final Context context, final String 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)); diff --git a/app/src/main/res/layout/edit_activity.xml b/app/src/main/res/layout/edit_activity.xml index f709e4b..c62fd00 100644 --- a/app/src/main/res/layout/edit_activity.xml +++ b/app/src/main/res/layout/edit_activity.xml @@ -260,6 +260,46 @@ + + + + + + + + + + + + + + + + + Working directory path Stdin Terminal Session Action + Custom Log Level Execute in a terminal session Wait for result for commands (Requires timeout > 0) @@ -45,6 +46,7 @@ %1$s%2$s Working Directory %1$s Stdin %1$s + Custom Log Level %1$s Session Action %1$s Terminal Session %1$s Wait For Result %1$s