Skip to content

Commit 628f5de

Browse files
author
Simon Pinfold
committed
Added support for sending a custom payload
1 parent 3d585c4 commit 628f5de

File tree

3 files changed

+173
-98
lines changed

3 files changed

+173
-98
lines changed

app/src/main/java/com/imgtec/hobbyist/fragments/menu/InteractiveModeFragment.java

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package com.imgtec.hobbyist.fragments.menu;
1616

1717
import android.app.Activity;
18+
import android.app.AlertDialog;
1819
import android.content.Context;
20+
import android.content.DialogInterface;
1921
import android.content.Intent;
2022
import android.content.IntentFilter;
2123
import android.net.ConnectivityManager;
2224
import android.os.Bundle;
2325
import android.os.Handler;
2426
import android.support.v4.app.Fragment;
27+
import android.text.Editable;
2528
import android.text.Html;
2629
import android.text.Spanned;
2730
import android.text.TextUtils;
@@ -33,6 +36,7 @@
3336
import android.widget.ArrayAdapter;
3437
import android.widget.Button;
3538
import android.widget.EditText;
39+
import android.widget.FrameLayout;
3640
import android.widget.ListView;
3741
import android.widget.RadioGroup;
3842
import android.widget.TextView;
@@ -116,6 +120,8 @@ public class InteractiveModeFragment extends NDListeningFragment implements Asyn
116120

117121
private FlowHelper flowHelper;
118122
private Handler handler = new Handler();
123+
private Button parametersButton;
124+
private String commandParameters;
119125

120126
public static InteractiveModeFragment newInstance() {
121127
return new InteractiveModeFragment();
@@ -132,6 +138,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
132138
sendButton = (Button) rootView.findViewById(R.id.sendCommandsButton);
133139
clearButton = (Button) rootView.findViewById(R.id.clearCommandsButton);
134140
messagesListView = (ListView) rootView.findViewById(R.id.messagesListView);
141+
parametersButton = (Button) rootView.findViewById(R.id.parametersButton);
142+
135143
return rootView;
136144
}
137145

@@ -260,6 +268,28 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
260268
}
261269

262270
private void initButtonsListeners() {
271+
parametersButton.setOnClickListener(new View.OnClickListener() {
272+
@Override
273+
public void onClick(View v) {
274+
final EditText input = new EditText(getActivity());
275+
if (commandParameters == null){
276+
input.setText("");
277+
} else {
278+
input.setText(commandParameters);
279+
}
280+
new AlertDialog.Builder(getActivity())
281+
.setTitle("Set parameters")
282+
.setMessage("Enter your desired XML parameters")
283+
.setView(input)
284+
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
285+
public void onClick(DialogInterface dialog, int whichButton) {
286+
Editable value = input.getText();
287+
commandParameters = value.toString();
288+
}
289+
}).show();
290+
}
291+
});
292+
263293
/**
264294
* Click sends message written in {@link #messageText}.
265295
*/
@@ -268,8 +298,9 @@ private void initButtonsListeners() {
268298
public void onClick(View v) {
269299
showMessagesListAndEnableClearButton();
270300
final String commandString = commandEditText.getText().toString();
301+
final String paramsString = commandParameters;
271302
if (isCommandMode && !commandString.equals("")) {
272-
sendCommandMessage(commandString);
303+
sendCommandMessage(commandString, paramsString);
273304
setCommandUIEnabled(false);
274305
} else if (!isCommandMode && !recipientName.equals("")) {
275306
ActivitiesAndFragmentsHelper.hideSoftInput(appContext, messageText);
@@ -317,16 +348,17 @@ private void setCommandUIEnabled(boolean enabled) {
317348
/**
318349
* Send AsyncMessage command to Flow.
319350
*/
320-
private void sendCommandMessage(final String input) {
351+
private void sendCommandMessage(final String input, final String paramsString) {
321352
BackgroundExecutor.execute(new ExternalRun() {
322353
@Override
323354
public void execute() {
324355
try {
325356
String command = Command.prepareCommand(input);
326357
AsyncMessage asyncMsg = flowHelper.createAsyncCommandMessage(command);
358+
if (paramsString != null) asyncMsg.addNode("commandparams", paramsString);
327359
boolean isAsyncMessageSent = flowHelper.postAsyncMessage(asyncMsg);
328360
if (isAsyncMessageSent) {
329-
showCommandTXMessage(input);
361+
showCommandTXMessage(input, paramsString);
330362
} else {
331363
ActivitiesAndFragmentsHelper.showToast(appContext, R.string.message_send_other_problem, handler);
332364
}
@@ -344,11 +376,25 @@ public void execute() {
344376
* Add message to messageList and update UI adhered to messageListAdapter.
345377
*
346378
* @param commandString previously sent command text.
379+
* @param params
347380
*/
348-
private void showCommandTXMessage(String commandString) {
349-
messageList.add(0, Html.fromHtml("<font color='#006400'>" + TX_MESSAGE + "<br/>" +
350-
DateFormatter.now(appContext) + "<br/>" +
351-
TextUtils.htmlEncode(commandString) + "<br/></font>"));
381+
private void showCommandTXMessage(String commandString, String params) {
382+
String html = "<font color='#006400'>" + TX_MESSAGE + "<br/>" +
383+
DateFormatter.now(appContext) + "<br/>" +
384+
TextUtils.htmlEncode(commandString) + "<br/></font>";
385+
if (params != null && !params.equals("")) {
386+
try {
387+
html += "</font><font color='#228B22'>" + formatXML(params) + "</font>";
388+
} catch (TransformerException e) {
389+
e.printStackTrace();
390+
Log.e("InteractiveModeFragment.showCommandTXMessage", "Failed to format command parameters", e);
391+
html += "</font><font color='red'>Error formatting \"" + params + "\"</font>";
392+
}
393+
} else {
394+
html += "</font><font color='black'>No command parameters</font>";
395+
}
396+
397+
messageList.add(0, Html.fromHtml(html));
352398
removeMessageIfListIsTooLong();
353399
notifyMessageListAdapter();
354400
}
@@ -449,11 +495,11 @@ private void showCommandRXMessage(AsyncMessage msg) {
449495
private String formatXML(String input) throws TransformerException {
450496
// adapted from http://stackoverflow.com/a/139096
451497

452-
// add on temporary tags to ensure correct parsing
453-
// this is necessary as we are pretty-printing <i>part</i>
454-
// of a XML document, so we allow a simple string (content)
455-
// and allow multiple children at the "root" level
456-
input = "<t>" + input + "</t>";
498+
// as we are pretty-printing <i>part</i> of a XML
499+
// document we allow a simple string (content)
500+
if (!input.contains("<")) {
501+
return "&nbsp;&nbsp;&nbsp;&nbsp;" + input;
502+
}
457503

458504
Transformer transformer = TransformerFactory.newInstance().newTransformer();
459505
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
@@ -464,8 +510,8 @@ private String formatXML(String input) throws TransformerException {
464510
transformer.transform(source, result);
465511
String xmlString = result.getWriter().toString();
466512

467-
// remove temporary tags and newlines at beginning and end
468-
xmlString = xmlString.substring(3, xmlString.length()-5).replaceAll("(?m)^\\s*\r?\n", "");
513+
// remove newlines at beginning and end
514+
xmlString = xmlString.replaceAll("(?m)^\\s*\r?\n", "");
469515
if (xmlString.endsWith("\n")) xmlString = xmlString.substring(0, xmlString.length()-2);
470516

471517
// if this is a one-line string then add a tab to the beginning

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

Lines changed: 112 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -51,104 +51,132 @@
5151
/>
5252
</RadioGroup>
5353

54-
<LinearLayout
55-
android:layout_width="match_parent"
56-
android:layout_height="match_parent"
57-
android:orientation="vertical"
58-
android:background="@drawable/interactive_mode_buttons_bottom_line"
59-
android:layout_marginLeft="@dimen/interactive_mode_side_space"
60-
android:layout_marginRight="@dimen/interactive_mode_side_space"
61-
>
62-
63-
<TextView
64-
android:id="@+id/deviceName"
65-
android:textColor="@color/nice_gray"
66-
android:layout_marginTop="10dp"
67-
android:layout_gravity="center_horizontal"
68-
android:textSize="@dimen/interactive_mode_button_text_size"
69-
android:layout_width="wrap_content"
70-
android:layout_height="wrap_content"/>
71-
72-
<com.imgtec.hobbyist.views.InstantAutoCompleteTextView
73-
style="@style/RoundedEditText"
74-
android:id="@+id/commandTextButton"
75-
android:hint="@string/select_command"
76-
android:textColorHint="@color/nice_green"
77-
android:layout_marginTop="10dp"
78-
android:layout_marginBottom="15dp"
79-
android:gravity="left"
80-
android:maxLength="64"
81-
android:imeOptions="actionDone"
82-
android:inputType="textNoSuggestions"
83-
/>
84-
85-
<Button
86-
style="@style/RoundedEditText"
87-
android:id="@+id/searchUsersButton"
88-
android:text="@string/search_users"
89-
android:textColor="@color/nice_green"
90-
android:layout_marginTop="25dp"
91-
android:layout_marginBottom="15dp"
92-
android:gravity="left"
93-
android:visibility="gone"
94-
/>
95-
96-
<EditText
97-
style="@style/RoundedEditText"
98-
android:id="@+id/messageText"
99-
android:inputType="text"
100-
android:hint="@string/type_a_message"
101-
android:textColorHint="@color/nice_green"
102-
android:layout_marginBottom="15dp"
103-
android:visibility="gone"
104-
/>
54+
<FrameLayout
55+
android:layout_width="fill_parent"
56+
android:layout_height="fill_parent">
10557

10658
<LinearLayout
107-
android:id="@+id/controlPanel"
108-
android:orientation="horizontal"
10959
android:layout_width="match_parent"
110-
android:layout_height="wrap_content"
60+
android:layout_height="match_parent"
61+
android:orientation="vertical"
62+
android:background="@drawable/interactive_mode_buttons_bottom_line"
63+
android:layout_marginLeft="@dimen/interactive_mode_side_space"
64+
android:layout_marginRight="@dimen/interactive_mode_side_space"
11165
>
11266

67+
<TextView
68+
android:id="@+id/deviceName"
69+
android:textColor="@color/nice_gray"
70+
android:layout_marginTop="10dp"
71+
android:layout_gravity="center_horizontal"
72+
android:textSize="@dimen/interactive_mode_button_text_size"
73+
android:layout_width="wrap_content"
74+
android:layout_height="wrap_content"/>
75+
76+
<LinearLayout
77+
android:orientation="horizontal"
78+
android:layout_width="fill_parent"
79+
android:layout_height="wrap_content"
80+
android:weightSum="1">
81+
82+
<com.imgtec.hobbyist.views.InstantAutoCompleteTextView
83+
style="@style/RoundedEditText"
84+
android:id="@+id/commandTextButton"
85+
android:hint="@string/select_command"
86+
android:textColorHint="@color/nice_green"
87+
android:layout_marginTop="10dp"
88+
android:layout_marginBottom="15dp"
89+
android:gravity="left"
90+
android:maxLength="64"
91+
android:imeOptions="actionDone"
92+
android:inputType="textNoSuggestions"
93+
android:layout_width="fill_parent"
94+
android:layout_weight="0.63"
95+
android:layout_height="wrap_content"/>
96+
97+
</LinearLayout>
98+
11399
<Button
114-
android:id="@+id/sendCommandsButton"
115-
android:layout_weight="1"
116-
android:layout_width="0dp"
117-
android:layout_height="@dimen/button_height"
118-
android:text="@string/send_button_text"
100+
style="@style/RoundedEditText"
101+
android:id="@+id/searchUsersButton"
102+
android:text="@string/search_users"
103+
android:textColor="@color/nice_green"
104+
android:layout_marginTop="25dp"
119105
android:layout_marginBottom="15dp"
120-
android:layout_marginRight="15dp"
121-
android:textColor="@android:color/white"
122-
android:background="@drawable/green_and_grey_button_selector"
106+
android:gravity="left"
107+
android:visibility="gone"
123108
/>
124109

125-
<Button
126-
android:id="@+id/clearCommandsButton"
127-
android:layout_width="@dimen/button_height"
128-
android:layout_height="@dimen/button_height"
129-
android:text="@string/C_letter"
110+
<EditText
111+
style="@style/RoundedEditText"
112+
android:id="@+id/messageText"
113+
android:inputType="text"
114+
android:hint="@string/type_a_message"
115+
android:textColorHint="@color/nice_green"
130116
android:layout_marginBottom="15dp"
131-
android:textColor="@android:color/white"
132-
android:enabled="false"
133-
android:background="@drawable/gray_disabled_purple_enabled_selector"
117+
android:visibility="gone"
134118
/>
135119

136-
</LinearLayout>
120+
<LinearLayout
121+
android:id="@+id/controlPanel"
122+
android:orientation="horizontal"
123+
android:layout_width="match_parent"
124+
android:layout_height="wrap_content"
125+
>
126+
127+
<Button
128+
android:id="@+id/parametersButton"
129+
android:layout_width="wrap_content"
130+
android:layout_height="wrap_content"
131+
android:text="Parameters"
132+
android:layout_marginBottom="15dp"
133+
android:textColor="@android:color/white"
134+
android:background="@drawable/green_and_grey_button_not_pressed"
135+
android:paddingLeft="5dp"
136+
android:paddingRight="5dp"
137+
android:layout_marginRight="10dp"/>
138+
139+
<Button
140+
android:id="@+id/sendCommandsButton"
141+
android:layout_weight="0.35"
142+
android:layout_width="0dp"
143+
android:layout_height="@dimen/button_height"
144+
android:text="@string/send_button_text"
145+
android:layout_marginBottom="15dp"
146+
android:layout_marginRight="10dp"
147+
android:textColor="@android:color/white"
148+
android:background="@drawable/green_and_grey_button_selector"
149+
/>
150+
151+
<Button
152+
android:id="@+id/clearCommandsButton"
153+
android:layout_width="@dimen/button_height"
154+
android:layout_height="@dimen/button_height"
155+
android:text="@string/C_letter"
156+
android:layout_marginBottom="15dp"
157+
android:textColor="@android:color/white"
158+
android:enabled="false"
159+
android:background="@drawable/gray_disabled_purple_enabled_selector"
160+
/>
161+
162+
</LinearLayout>
163+
164+
165+
<ListView
166+
android:id="@+id/messagesListView"
167+
android:layout_width="match_parent"
168+
android:layout_height="match_parent"
169+
android:divider="@null"
170+
android:dividerHeight="0dp"
171+
android:background="@drawable/stroke_shape"
172+
android:layout_marginBottom="15dp"
173+
android:visibility="gone"
174+
>
175+
</ListView>
137176

138177

139-
<ListView
140-
android:id="@+id/messagesListView"
141-
android:layout_width="match_parent"
142-
android:layout_height="match_parent"
143-
android:divider="@null"
144-
android:dividerHeight="0dp"
145-
android:background="@drawable/stroke_shape"
146-
android:layout_marginBottom="15dp"
147-
android:visibility="gone"
148-
>
149-
</ListView>
150-
178+
</LinearLayout>
151179

152-
</LinearLayout>
180+
</FrameLayout>
153181

154182
</LinearLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,6 @@
247247
<string name="response_buffer_full">SEND BUFFER FULL PLEASE WAIT</string>
248248
<string name="response_offline">BOARD IS OFFLINE WILL DELIVER ONCE BOARD IS ONLINE</string>
249249
<string name="response_failed">SEND FAILED</string>
250+
<string name="xmlParametersPrompt">Enter your XML parameters here</string>
250251

251252
</resources>

0 commit comments

Comments
 (0)