From e8d0df9ad0ed8a6ed1358e592b4bca6ee764ca4e Mon Sep 17 00:00:00 2001 From: Kieran Coldron Date: Tue, 14 Aug 2018 05:04:12 +0100 Subject: [PATCH] Added values array and indexs Add index of values Added index for other dialogs --- .../java/com/termux/api/DialogActivity.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/termux/api/DialogActivity.java b/app/src/main/java/com/termux/api/DialogActivity.java index 5c91122e2..910c2dc5f 100644 --- a/app/src/main/java/com/termux/api/DialogActivity.java +++ b/app/src/main/java/com/termux/api/DialogActivity.java @@ -45,6 +45,7 @@ import com.termux.api.util.TermuxApiLogger; import com.termux.api.util.TermuxApiPermissionActivity; +import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale; @@ -126,7 +127,20 @@ public void writeJson(JsonWriter out) throws Exception { out.name("code").value(result.code); out.name("text").value(result.text); - + if(result.index > -1) { + out.name("index").value(result.index); + } + if (result.values.size() > 0) { + out.name("values"); + out.beginArray(); + for (Value value : result.values) { + out.beginObject(); + out.name("index").value(value.index); + out.name("text").value(value.text); + out.endObject(); + } + out.endArray(); + } if (!result.error.equals("")) { out.name("error").value(result.error); } @@ -204,9 +218,16 @@ static class InputResult { public String text = ""; public String error = ""; public int code = 0; + public static int index = -1; + public List values = new ArrayList<>(); } + public static class Value { + public int index = -1; + public String text = ""; + } + /** * -------------------------------------- * InputMethod Implementations @@ -254,15 +275,21 @@ LinearLayout createWidgetView(AppCompatActivity activity) { String getResult() { int checkBoxCount = widgetView.getChildCount(); + List values = new ArrayList<>(); StringBuilder sb = new StringBuilder(); sb.append("["); for (int j = 0; j < checkBoxCount; ++j) { CheckBox box = widgetView.findViewById(j); if (box.isChecked()) { + Value value = new Value(); + value.index = j; + value.text = box.getText().toString(); + values.add(value); sb.append(box.getText().toString()).append(", "); } } + inputResult.values = values; // remove trailing comma and add closing bracket return sb.toString().replaceAll(", $", "") + "]"; } @@ -550,6 +577,7 @@ RadioGroup createWidgetView(AppCompatActivity activity) { String getResult() { int radioIndex = radioGroup.indexOfChild(widgetView.findViewById(radioGroup.getCheckedRadioButtonId())); RadioButton radioButton = (RadioButton) radioGroup.getChildAt(radioIndex); + InputResult.index = radioIndex; return (radioButton != null) ? radioButton.getText().toString() : ""; } } @@ -606,9 +634,10 @@ public void setupDialog(final Dialog dialog, int style) { NestedScrollView scrollView = new NestedScrollView(getContext()); final String[] values = getInputValues(Objects.requireNonNull(getActivity()).getIntent()); - for (final String value : values) { + for (int i = 0; i < values.length; ++i) { + final int j = i; final TextView textView = new TextView(getContext()); - textView.setText(value); + textView.setText(values[j]); textView.setTextSize(20); textView.setPadding(56, 56, 56, 56); textView.setOnClickListener(new View.OnClickListener() { @@ -616,7 +645,8 @@ public void setupDialog(final Dialog dialog, int style) { @Override public void onClick(View view) { InputResult result = new InputResult(); - result.text = value; + result.text = values[j]; + result.index = j; dialog.dismiss(); resultListener.onResult(result); } @@ -687,6 +717,7 @@ static class SpinnerInputMethod extends InputDialog { @Override String getResult() { + InputResult.index = widgetView.getSelectedItemPosition(); return widgetView.getSelectedItem().toString(); }