这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 5bf1518

Browse files
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 termux/termux-app@60f37bde
1 parent 6f6ddd0 commit 5bf1518

File tree

5 files changed

+92
-6
lines changed

5 files changed

+92
-6
lines changed

app/src/main/java/com/termux/tasker/EditConfigurationActivity.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public final class EditConfigurationActivity extends AbstractPluginActivity {
6464
private AutoCompleteTextView mWorkingDirectoryPathText;
6565
private TextView mStdinView;
6666
private EditText mSessionAction;
67+
private EditText mBackgroundCustomLogLevel;
6768
private CheckBox mInTerminalCheckbox;
6869
private CheckBox mWaitForResult;
6970
private TextView mExecutableAbsolutePathText;
@@ -113,6 +114,7 @@ protected void onCreate(final Bundle savedInstanceState) {
113114
mWorkingDirectoryPathText = findViewById(R.id.working_directory_path);
114115
mStdinView = findViewById(R.id.view_stdin);
115116
mSessionAction = findViewById(R.id.session_action);
117+
mBackgroundCustomLogLevel = findViewById(R.id.background_custom_log_level);
116118
mInTerminalCheckbox = findViewById(R.id.in_terminal);
117119
mWaitForResult = findViewById(R.id.wait_for_result);
118120
mExecutableAbsolutePathText = findViewById(R.id.executable_absolute_path);
@@ -126,6 +128,7 @@ protected void onCreate(final Bundle savedInstanceState) {
126128
setWorkingDirectoryPathViews();
127129
setStdinView();
128130
setSessionActionViews();
131+
setBackgroundCustomLogLevelViews();
129132
setInTerminalView();
130133

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

170+
final String backgroundCustomLogLevel = localeBundle.getString(PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL);
171+
mBackgroundCustomLogLevel.setText(backgroundCustomLogLevel);
172+
processBackgroundCustomLogLevel(backgroundCustomLogLevel);
173+
updateBackgroundCustomLogLevelViewVisibility(inTerminal);
174+
167175
final boolean waitForResult = localeBundle.getBoolean(PluginBundleManager.EXTRA_WAIT_FOR_RESULT);
168176
mWaitForResult.setChecked(waitForResult);
169177
}
@@ -280,12 +288,28 @@ private void updateSessionActionViewVisibility(boolean inTerminal) {
280288
}
281289

282290

291+
private void setBackgroundCustomLogLevelViews() {
292+
mBackgroundCustomLogLevel.addTextChangedListener(new AfterTextChangedWatcher() {
293+
@Override
294+
public void afterTextChanged(Editable editable) {
295+
processBackgroundCustomLogLevel(editable == null ? null : editable.toString());
296+
}
297+
});
298+
}
299+
300+
private void updateBackgroundCustomLogLevelViewVisibility(boolean inTerminal) {
301+
if (mBackgroundCustomLogLevel == null) return;
302+
mBackgroundCustomLogLevel.setVisibility(inTerminal ? View.GONE : View.VISIBLE);
303+
}
304+
305+
283306
private void setInTerminalView() {
284307
mInTerminalCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
285308
@Override
286309
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
287310
updateStdinViewVisibility(isChecked);
288311
updateSessionActionViewVisibility(isChecked);
312+
updateBackgroundCustomLogLevelViewVisibility(isChecked);
289313
}
290314
});
291315
}
@@ -582,6 +606,11 @@ private void processSessionAction(String sessionActionString) {
582606
TERMUX_SERVICE.MIN_VALUE_EXTRA_SESSION_ACTION, TERMUX_SERVICE.MAX_VALUE_EXTRA_SESSION_ACTION);
583607
}
584608

609+
private void processBackgroundCustomLogLevel(String backgroundCustomLogLevelString) {
610+
processIntFieldValue(mBackgroundCustomLogLevel, backgroundCustomLogLevelString,
611+
Logger.LOG_LEVEL_OFF, Logger.MAX_LOG_LEVEL);
612+
}
613+
585614
private void processIntFieldValue(EditText editText, String stringValue, int min, int max) {
586615
if (editText == null) return;
587616
editText.setError(null);
@@ -618,6 +647,7 @@ public void finish() {
618647
final String arguments = DataUtils.getDefaultIfUnset(mArgumentsText.getText() == null ? null : mArgumentsText.getText().toString(), null);
619648
final String workingDirectory = DataUtils.getDefaultIfUnset(mWorkingDirectoryPathText.getText() == null ? null : mWorkingDirectoryPathText.getText().toString(), null);
620649
final String sessionAction = DataUtils.getDefaultIfUnset(mSessionAction.getText() == null ? null : mSessionAction.getText().toString(), null);
650+
final String backgroundCustomLogLevel = DataUtils.getDefaultIfUnset(mBackgroundCustomLogLevel.getText() == null ? null : mBackgroundCustomLogLevel.getText().toString(), null);
621651
final boolean inTerminal = mInTerminalCheckbox.isChecked();
622652
final boolean waitForResult = mWaitForResult.isChecked();
623653

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

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

654684
// If host supports variable replacement when running plugin action, then
655685
// request it to replace variables in following fields
@@ -659,7 +689,8 @@ public void finish() {
659689
PluginBundleManager.EXTRA_ARGUMENTS,
660690
PluginBundleManager.EXTRA_WORKDIR,
661691
PluginBundleManager.EXTRA_STDIN,
662-
PluginBundleManager.EXTRA_SESSION_ACTION
692+
PluginBundleManager.EXTRA_SESSION_ACTION,
693+
PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL
663694
});
664695
}
665696

app/src/main/java/com/termux/tasker/FireReceiver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void onReceive(final Context context, final Intent intent) {
7777

7878
if (executionCommand.inBackground) {
7979
executionCommand.stdin = IntentUtils.getStringExtraIfSet(intent, PluginBundleManager.EXTRA_STDIN, null);
80+
executionCommand.backgroundCustomLogLevel = IntentUtils.getIntegerExtraIfSet(intent, PluginBundleManager.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, null);
8081
} else {
8182
executionCommand.sessionAction = IntentUtils.getStringExtraIfSet(intent, PluginBundleManager.EXTRA_SESSION_ACTION, null);
8283
}
@@ -187,6 +188,7 @@ public void onReceive(final Context context, final Intent intent) {
187188
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_WORKDIR, executionCommand.workingDirectory);
188189
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_STDIN, executionCommand.stdin);
189190
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_SESSION_ACTION, executionCommand.sessionAction);
191+
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, DataUtils.getStringFromInteger(executionCommand.backgroundCustomLogLevel, null));
190192
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND, executionCommand.inBackground);
191193

192194
// Send execution intent to TERMUX_SERVICE

app/src/main/java/com/termux/tasker/PluginBundleManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class PluginBundleManager {
3737
*/
3838
public static final String EXTRA_SESSION_ACTION = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.SESSION_ACTION"; // Default: "com.termux.tasker.extra.SESSION_ACTION"
3939

40+
/** The {@code String} extra for custom log levels for background commands between
41+
* {@link Logger#LOG_LEVEL_OFF} and {@link Logger#MAX_LOG_LEVEL} as per
42+
* https://github.com/termux/termux-app/commit/60f37bde.
43+
*/
44+
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"
45+
4046
/** The {@code boolean} extra for whether the executable should be run inside a terminal. */
4147
public static final String EXTRA_TERMINAL = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.TERMINAL"; // Default: "com.termux.tasker.extra.TERMINAL"
4248

@@ -80,6 +86,7 @@ public static String parseBundle(@NonNull final Context context, final Bundle bu
8086
* - EXTRA_WORKDIR
8187
* - EXTRA_STDIN
8288
* - EXTRA_SESSION_ACTION
89+
* - EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL
8390
* - EXTRA_TERMINAL
8491
* - EXTRA_WAIT_FOR_RESULT
8592
* - VARIABLE_REPLACE_KEYS
@@ -98,13 +105,13 @@ public static String parseBundle(@NonNull final Context context, final Bundle bu
98105
}
99106

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

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

142150
if (!inTerminal) {
143151
result.putString(EXTRA_STDIN, stdin);
152+
result.putString(EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, backgroundCustomLogLevel);
144153
} else {
145154
result.putString(EXTRA_SESSION_ACTION, sessionAction);
146155
}
@@ -163,13 +172,15 @@ public static Bundle generateBundle(@NonNull final Context context, final String
163172
public static String generateBlurb(@NonNull final Context context, final String executable,
164173
final String arguments, final String workingDirectory,
165174
final String stdin, final String sessionAction,
175+
final String backgroundCustomLogLevel,
166176
final boolean inTerminal, final boolean waitForResult) {
167177
StringBuilder builder = new StringBuilder();
168178
builder.append(context.getString(R.string.blurb_executable_and_arguments, executable, arguments == null ? "" : " " + arguments));
169179
builder.append("\n\n").append(context.getString(R.string.blurb_working_directory, (!DataUtils.isNullOrEmpty(workingDirectory) ? UNICODE_CHECK : UNICODE_UNCHECK)));
170180

171181
if (!inTerminal) {
172182
builder.append("\n").append(context.getString(R.string.blurb_stdin, (!DataUtils.isNullOrEmpty(stdin) ? UNICODE_CHECK : UNICODE_UNCHECK)));
183+
builder.append("\n").append(context.getString(R.string.blurb_custom_log_level, backgroundCustomLogLevel));
173184
} else {
174185
if (!DataUtils.isNullOrEmpty(sessionAction))
175186
builder.append("\n").append(context.getString(R.string.blurb_session_action, sessionAction));

app/src/main/res/layout/edit_activity.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,46 @@
260260

261261

262262

263+
264+
265+
<!-- Background Custom Log Level -->
266+
<androidx.cardview.widget.CardView
267+
xmlns:card_view="http://schemas.android.com/apk/res-auto"
268+
android:layout_width="match_parent"
269+
android:layout_height="wrap_content"
270+
android:layout_marginTop="@dimen/activity_vertical_margin"
271+
android:padding="@dimen/activity_view_padding_mini"
272+
card_view:cardCornerRadius="4dp">
273+
<LinearLayout
274+
android:layout_width="match_parent"
275+
android:layout_height="wrap_content"
276+
android:layout_weight="1"
277+
android:orientation="vertical">
278+
279+
<TextView
280+
android:layout_width="match_parent"
281+
android:layout_height="wrap_content"
282+
android:gravity="start|center_vertical"
283+
android:textSize="14sp"
284+
android:textStyle="bold"
285+
android:textColor="@android:color/black"
286+
android:text="@string/title_background_custom_log_level"
287+
tools:labelFor="@id/background_custom_log_level" />
288+
289+
<EditText
290+
android:id="@+id/background_custom_log_level"
291+
android:layout_width="match_parent"
292+
android:layout_height="wrap_content"
293+
android:gravity="start|center_vertical"
294+
android:importantForAutofill="no" />
295+
296+
</LinearLayout>
297+
</androidx.cardview.widget.CardView>
298+
299+
300+
301+
302+
263303
<CheckBox
264304
android:id="@+id/in_terminal"
265305
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<string name="title_working_directory_path">Working directory path</string>
2222
<string name="title_stdin">Stdin</string>
2323
<string name="title_session_action">Terminal Session Action</string>
24+
<string name="title_background_custom_log_level">Custom Log Level</string>
2425
<string name="title_execute_in_terminal">Execute in a terminal session</string>
2526
<string name="title_wait_for_result">Wait for result for commands (Requires timeout > 0)</string>
2627

@@ -45,6 +46,7 @@
4546
<string name="blurb_executable_and_arguments">%1$s%2$s</string>
4647
<string name="blurb_working_directory">Working Directory %1$s</string>
4748
<string name="blurb_stdin">Stdin %1$s</string>
49+
<string name="blurb_custom_log_level">Custom Log Level %1$s</string>
4850
<string name="blurb_session_action">Session Action %1$s</string>
4951
<string name="blurb_in_terminal">Terminal Session %1$s</string>
5052
<string name="blurb_wait_for_result">Wait For Result %1$s</string>

0 commit comments

Comments
 (0)