1313import com .termux .shared .models .TextIOInfo ;
1414import com .termux .shared .models .errors .Error ;
1515import com .termux .shared .termux .TermuxConstants ;
16+ import com .termux .shared .termux .TermuxConstants .TERMUX_APP .TERMUX_SERVICE ;
1617import com .termux .shared .file .FileUtils ;
1718import com .termux .shared .termux .TermuxUtils ;
1819import com .termux .tasker .utils .LoggerUtils ;
3435import android .widget .AutoCompleteTextView ;
3536import android .widget .CheckBox ;
3637import android .widget .CompoundButton ;
38+ import android .widget .EditText ;
3739import android .widget .TextView ;
3840
3941import java .io .File ;
@@ -61,6 +63,7 @@ public final class EditConfigurationActivity extends AbstractPluginActivity {
6163 private TextInputLayout mWorkingDirectoryPathTextLayout ;
6264 private AutoCompleteTextView mWorkingDirectoryPathText ;
6365 private TextView mStdinView ;
66+ private EditText mSessionAction ;
6467 private CheckBox mInTerminalCheckbox ;
6568 private CheckBox mWaitForResult ;
6669 private TextView mExecutableAbsolutePathText ;
@@ -109,6 +112,7 @@ protected void onCreate(final Bundle savedInstanceState) {
109112 mWorkingDirectoryPathTextLayout = findViewById (R .id .layout_working_directory_path );
110113 mWorkingDirectoryPathText = findViewById (R .id .working_directory_path );
111114 mStdinView = findViewById (R .id .view_stdin );
115+ mSessionAction = findViewById (R .id .session_action );
112116 mInTerminalCheckbox = findViewById (R .id .in_terminal );
113117 mWaitForResult = findViewById (R .id .wait_for_result );
114118 mExecutableAbsolutePathText = findViewById (R .id .executable_absolute_path );
@@ -121,6 +125,7 @@ protected void onCreate(final Bundle savedInstanceState) {
121125 setExecutionPathViews ();
122126 setWorkingDirectoryPathViews ();
123127 setStdinView ();
128+ setSessionActionViews ();
124129 setInTerminalView ();
125130
126131 // Currently savedInstanceState bundle is not supported
@@ -154,6 +159,11 @@ protected void onCreate(final Bundle savedInstanceState) {
154159 updateStdinViewText ();
155160 updateStdinViewVisibility (inTerminal );
156161
162+ final String sessionAction = localeBundle .getString (PluginBundleManager .EXTRA_SESSION_ACTION );
163+ mSessionAction .setText (sessionAction );
164+ processSessionAction (sessionAction );
165+ updateSessionActionViewVisibility (inTerminal );
166+
157167 final boolean waitForResult = localeBundle .getBoolean (PluginBundleManager .EXTRA_WAIT_FOR_RESULT );
158168 mWaitForResult .setChecked (waitForResult );
159169 }
@@ -192,13 +202,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
192202
193203
194204 private void setExecutionPathViews () {
195- mExecutablePathText .addTextChangedListener (new TextWatcher () {
196- @ Override
197- public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
198-
199- @ Override
200- public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
201-
205+ mExecutablePathText .addTextChangedListener (new AfterTextChangedWatcher () {
202206 @ Override
203207 public void afterTextChanged (Editable editable ) {
204208 processExecutablePath (editable == null ? null : editable .toString ());
@@ -211,13 +215,7 @@ public void afterTextChanged(Editable editable) {
211215
212216
213217 private void setWorkingDirectoryPathViews () {
214- mWorkingDirectoryPathText .addTextChangedListener (new TextWatcher () {
215- @ Override
216- public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
217-
218- @ Override
219- public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
220-
218+ mWorkingDirectoryPathText .addTextChangedListener (new AfterTextChangedWatcher () {
221219 @ Override
222220 public void afterTextChanged (Editable editable ) {
223221 processWorkingDirectoryPath (editable == null ? null : editable .toString ());
@@ -263,10 +261,22 @@ private void updateStdinViewText() {
263261
264262 private void updateStdinViewVisibility (boolean inTerminal ) {
265263 if (mStdinView == null ) return ;
266- if (inTerminal )
267- mStdinView .setVisibility (View .GONE );
268- else
269- mStdinView .setVisibility (View .VISIBLE );
264+ mStdinView .setVisibility (inTerminal ? View .GONE : View .VISIBLE );
265+ }
266+
267+
268+ private void setSessionActionViews () {
269+ mSessionAction .addTextChangedListener (new AfterTextChangedWatcher () {
270+ @ Override
271+ public void afterTextChanged (Editable editable ) {
272+ processSessionAction (editable == null ? null : editable .toString ());
273+ }
274+ });
275+ }
276+
277+ private void updateSessionActionViewVisibility (boolean inTerminal ) {
278+ if (mSessionAction == null ) return ;
279+ mSessionAction .setVisibility (inTerminal ? View .VISIBLE : View .GONE );
270280 }
271281
272282
@@ -275,6 +285,7 @@ private void setInTerminalView() {
275285 @ Override
276286 public void onCheckedChanged (CompoundButton buttonView , boolean isChecked ) {
277287 updateStdinViewVisibility (isChecked );
288+ updateSessionActionViewVisibility (isChecked );
278289 }
279290 });
280291 }
@@ -566,6 +577,32 @@ else if (workingDirectoryPathText.startsWith("$PREFIX/") || workingDirectoryPath
566577 mWorkingDirectoryPathText .showDropDown ();
567578 }
568579
580+ private void processSessionAction (String sessionActionString ) {
581+ processIntFieldValue (mSessionAction , sessionActionString ,
582+ TERMUX_SERVICE .MIN_VALUE_EXTRA_SESSION_ACTION , TERMUX_SERVICE .MAX_VALUE_EXTRA_SESSION_ACTION );
583+ }
584+
585+ private void processIntFieldValue (EditText editText , String stringValue , int min , int max ) {
586+ if (editText == null ) return ;
587+ editText .setError (null );
588+ if (DataUtils .isNullOrEmpty (stringValue )) return ;
589+ if (PluginUtils .isPluginHostAppVariableContainingString (stringValue )) return ;
590+
591+ Integer value = null ;
592+ boolean invalid = false ;
593+
594+ try {
595+ value = Integer .parseInt (stringValue );
596+ }
597+ catch (Exception e ) {
598+ invalid = true ;
599+ }
600+
601+ if (invalid || value < min || value > max ) {
602+ editText .setError (getString (R .string .error_int_not_in_range , min , max ));
603+ }
604+ }
605+
569606
570607
571608
@@ -577,9 +614,10 @@ public void finish() {
577614 return ;
578615 }
579616
580- final String executable = mExecutablePathText .getText () == null ? null : mExecutablePathText .getText ().toString ();
581- final String arguments = mArgumentsText .getText () == null ? null : mArgumentsText .getText ().toString ();
582- final String workingDirectory = mWorkingDirectoryPathText .getText () == null ? null : mWorkingDirectoryPathText .getText ().toString ();
617+ final String executable = DataUtils .getDefaultIfUnset (mExecutablePathText .getText () == null ? null : mExecutablePathText .getText ().toString (), null );
618+ final String arguments = DataUtils .getDefaultIfUnset (mArgumentsText .getText () == null ? null : mArgumentsText .getText ().toString (), null );
619+ final String workingDirectory = DataUtils .getDefaultIfUnset (mWorkingDirectoryPathText .getText () == null ? null : mWorkingDirectoryPathText .getText ().toString (), null );
620+ final String sessionAction = DataUtils .getDefaultIfUnset (mSessionAction .getText () == null ? null : mSessionAction .getText ().toString (), null );
583621 final boolean inTerminal = mInTerminalCheckbox .isChecked ();
584622 final boolean waitForResult = mWaitForResult .isChecked ();
585623
@@ -599,7 +637,7 @@ public void finish() {
599637 * stored in the Bundle, as Locale's classloader will not recognize it).
600638 */
601639 final Bundle resultBundle = PluginBundleManager .generateBundle (getApplicationContext (),
602- executable , arguments , workingDirectory , mStdin , inTerminal , waitForResult );
640+ executable , arguments , workingDirectory , mStdin , sessionAction , inTerminal , waitForResult );
603641 if (resultBundle == null ) {
604642 Logger .showToast (this , getString (R .string .error_generate_plugin_bundle_failed ), true );
605643 setResult (RESULT_CODE_FAILED , resultIntent );
@@ -610,7 +648,8 @@ public void finish() {
610648 Logger .logDebug (LOG_TAG , "Result bundle size: " + PluginBundleManager .getBundleSize (resultBundle ));
611649
612650 // The blurb is a concise status text to be displayed in the host's UI.
613- final String blurb = PluginBundleManager .generateBlurb (this , executable , arguments , workingDirectory , mStdin , inTerminal , waitForResult );
651+ final String blurb = PluginBundleManager .generateBlurb (this , executable , arguments ,
652+ workingDirectory , mStdin , sessionAction , inTerminal , waitForResult );
614653
615654 // If host supports variable replacement when running plugin action, then
616655 // request it to replace variables in following fields
@@ -619,7 +658,8 @@ public void finish() {
619658 PluginBundleManager .EXTRA_EXECUTABLE ,
620659 PluginBundleManager .EXTRA_ARGUMENTS ,
621660 PluginBundleManager .EXTRA_WORKDIR ,
622- PluginBundleManager .EXTRA_STDIN
661+ PluginBundleManager .EXTRA_STDIN ,
662+ PluginBundleManager .EXTRA_SESSION_ACTION
623663 });
624664 }
625665
@@ -689,4 +729,17 @@ public void onActivityResult(ActivityResult result) {
689729 });
690730 }
691731
732+
733+ static class AfterTextChangedWatcher implements TextWatcher {
734+ @ Override
735+ public void beforeTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
736+
737+ @ Override
738+ public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {}
739+
740+ @ Override
741+ public void afterTextChanged (Editable editable ) {
742+ }
743+ }
744+
692745}
0 commit comments