1 package org.iotivity.ca.service;
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.os.Bundle;
7 import android.os.Handler;
8 import android.util.Log;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.EditText;
15 import android.widget.RelativeLayout;
16 import android.widget.TextView;
17 import android.widget.Toast;
20 import org.iotivity.ca.sample_service.R;
21 import org.iotivity.ca.service.FileChooser;
22 import org.iotivity.ca.service.FileChooser.FileSelectedListener;
24 public class MainActivity extends Activity {
26 static RMInterface RM = new RMInterface();
28 private final static String TAG = "Sample_Service : MainActivity";
30 private final CharSequence[] mNetworkCheckBoxItems = { Network.IP.name(),
31 Network.LE.name(), Network.EDR.name()};
33 private final CharSequence[] mDTLSCheckBoxItems = { DTLS.UNSECURED.name(),
34 DTLS.SECURED.name() };
36 private final CharSequence[] mMsgTyleCheckBoxItems = { MsgType.CON.name(),
37 MsgType.NON.name(), MsgType.ACK.name(), MsgType.RESET.name() };
39 private final CharSequence[] mResponseResultCheckBoxItems = {
40 ResponseResult.CA_CREATED.name(), ResponseResult.CA_DELETED.name(),
41 ResponseResult.CA_VALID.name(), ResponseResult.CA_CHANGED.name(),
42 ResponseResult.CA_CONTENT.name(), ResponseResult.CA_EMPTY.name(),
43 ResponseResult.CA_BAD_REQ.name(), ResponseResult.CA_BAD_OPT.name(),
44 ResponseResult.CA_NOT_FOUND.name(), ResponseResult.CA_INTERNAL_SERVER_ERROR.name(),
45 ResponseResult.CA_RETRANSMIT_TIMEOUT.name() };
48 SERVER, CLIENT, BOTH, UNKNOWN
51 private enum Network {
59 private enum MsgType {
63 private enum ResponseResult {
64 CA_CREATED, CA_DELETED, CA_VALID, CA_CHANGED, CA_CONTENT, CA_EMPTY,
65 CA_BAD_REQ, CA_BAD_OPT, CA_NOT_FOUND, CA_INTERNAL_SERVER_ERROR, CA_RETRANSMIT_TIMEOUT
68 private boolean mCheckedItems[] = {
69 false, false, false, false
72 private int mSelectedItems[] = { 0, 0, 0 };
74 private int mUnSelectedItems[] = { 0, 0, 0 };
76 private Mode mCurrentMode = Mode.UNKNOWN;
78 private RelativeLayout mSendNotificationLayout = null;
80 private RelativeLayout mSendRequestLayout = null;
82 private RelativeLayout mSendRequestToAllLayout = null;
84 private RelativeLayout mSendRequestSettingLayout = null;
86 private RelativeLayout mSendRequestToAllSettingLayout = null;
88 private RelativeLayout mSendResponseNotiSettingLayout = null;
90 private RelativeLayout mReceiveLayout = null;
92 private RelativeLayout mRequestTitleLayout = null;
94 private RelativeLayout mRequestToAllTitleLayout = null;
96 private RelativeLayout mResponseNotificationTitleLayout = null;
98 private RelativeLayout mHandleTitleLayout = null;
100 private TextView mMode_tv = null;
102 private TextView mNetwork_tv = null;
104 private EditText mNotification_ed = null;
106 private EditText mReqData_ed = null;
108 private EditText mReqToAllData_ed = null;
110 private Button mNotify_btn = null;
112 private Button mReqeust_btn = null;
114 private Button mReqeust_setting_btn = null;
116 private Button mReqeustToAll_btn = null;
118 private Button mReqeustToAll_setting_btn = null;
120 private Button mResponse_Notify_setting_btn = null;
122 private Button mResponse_btn = null;
124 private Button mGetNetworkInfo_btn = null;
126 private Button mRecv_btn = null;
128 private Button mBig_btn = null;
130 private Handler mLogHandler = null;
133 * Defined ConnectivityType in cacommon.c
135 * CA_IP = (1 << 0) CA_LE = (1 << 2) CA_EDR = (1 << 3)
137 private int CA_IP = (1 << 0);
138 private int CA_LE = (1 << 1);
139 private int CA_EDR = (1 << 2);
140 private int isSecured = 0;
141 private int msgType = 1;
142 private int responseValue = 0;
143 private int selectedNetworkType = -1;
144 private int selectedMsgType = 1;
145 private int selectedMsgSecured = 0;
146 private int selectedResponseValue = 0;
147 int selectedNetwork = -1;
148 int interestedNetwork = 0;
149 int uninterestedNetwork = 0;
150 private boolean isSendResponseSetting = false;
151 private boolean isSendRequestToAllSetting = false;
152 private boolean isBigData = false;
155 protected void onCreate(Bundle savedInstanceState) {
156 super.onCreate(savedInstanceState);
157 setContentView(R.layout.activity_main);
159 RM.setResponseListener(this);
161 mLogHandler = new Handler();
162 TextView logView = (TextView) findViewById(R.id.tv_result);
163 DLog.setTextView(mLogHandler, logView);
167 mReceiveLayout = (RelativeLayout) findViewById(R.id.layout_receive);
170 mSendRequestLayout = (RelativeLayout) findViewById(R.id.layout_request);
171 mSendRequestToAllLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all);
172 mSendRequestSettingLayout = (RelativeLayout)
173 findViewById(R.id.layout_request_setting_for_client);
174 mSendRequestToAllSettingLayout = (RelativeLayout)
175 findViewById(R.id.layout_request_to_all_setting_for_client);
176 mRequestTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_title);
177 mRequestToAllTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all_title);
178 mHandleTitleLayout = (RelativeLayout) findViewById(R.id.layout_handle_title);
181 mSendNotificationLayout = (RelativeLayout) findViewById(R.id.layout_notify);
182 mSendResponseNotiSettingLayout = (RelativeLayout)
183 findViewById(R.id.layout_request_setting_for_server);
184 mResponseNotificationTitleLayout = (RelativeLayout)
185 findViewById(R.id.layout_Response_Noti_title);
187 mMode_tv = (TextView) findViewById(R.id.tv_mode);
188 mNetwork_tv = (TextView) findViewById(R.id.tv_network);
190 mNotification_ed = (EditText) findViewById(R.id.et_notification);
191 mReqData_ed = (EditText) findViewById(R.id.et_req_data);
192 mReqToAllData_ed = (EditText) findViewById(R.id.et_req_to_all_data);
194 mResponse_btn = (Button) findViewById(R.id.btn_sendresponse);
195 mNotify_btn = (Button) findViewById(R.id.btn_notify);
196 mReqeust_btn = (Button) findViewById(R.id.btn_Request);
197 mReqeust_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_client);
198 mReqeustToAll_btn = (Button) findViewById(R.id.btn_request_to_all);
199 mReqeustToAll_setting_btn = (Button)
200 findViewById(R.id.btn_request_to_all_setting_for_client);
201 mResponse_Notify_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_server);
202 mGetNetworkInfo_btn = (Button) findViewById(R.id.btn_get_network_info);
203 mRecv_btn = (Button) findViewById(R.id.btn_receive);
204 mBig_btn = (Button) findViewById(R.id.btn_big_data);
205 mBig_btn.setOnClickListener(mSelectLargeDataButtonHandler);
207 mResponse_btn.setOnClickListener(mSendResponseHandler);
208 mNotify_btn.setOnClickListener(mNotifyHandler);
209 mReqeust_btn.setOnClickListener(mSendRequestHandler);
210 mReqeust_setting_btn.setOnClickListener(mSendRequestSettingHandler);
211 mReqeustToAll_btn.setOnClickListener(mSendRequestToAllHandler);
212 mReqeustToAll_setting_btn.setOnClickListener(mSendRequestToAllSettingHandler);
213 mResponse_Notify_setting_btn
214 .setOnClickListener(mSendResponseNotiSettingHandler);
215 mRecv_btn.setOnClickListener(mResponseHandler);
216 mGetNetworkInfo_btn.setOnClickListener(mGetNetworkInfoHandler);
218 showSelectModeView();
220 // Initialize Connectivity Abstraction
221 RM.RMInitialize(getApplicationContext());
224 RM.RMRegisterHandler();
227 private void showSelectModeView() {
229 mSendNotificationLayout.setVisibility(View.INVISIBLE);
230 mSendRequestLayout.setVisibility(View.INVISIBLE);
231 mSendRequestToAllLayout.setVisibility(View.INVISIBLE);
232 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
233 mSendRequestToAllSettingLayout.setVisibility(View.INVISIBLE);
234 mReceiveLayout.setVisibility(View.INVISIBLE);
235 mRequestTitleLayout.setVisibility(View.INVISIBLE);
236 mRequestToAllTitleLayout.setVisibility(View.INVISIBLE);
237 mHandleTitleLayout.setVisibility(View.INVISIBLE);
238 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
239 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
241 mMode_tv.setText("Select Mode (Server or Client)");
242 Log.i(TAG, "Select Mode (Server or Client)");
245 private void showNetworkView() {
247 mNetwork_tv.setText("Select Network Type");
248 Log.i(TAG, "Select Network Type");
251 private void showModeView() {
253 if (mCurrentMode == Mode.SERVER) {
255 mSendNotificationLayout.setVisibility(View.VISIBLE);
256 mSendRequestLayout.setVisibility(View.INVISIBLE);
257 mSendRequestToAllLayout.setVisibility(View.VISIBLE);
258 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
259 mSendRequestToAllSettingLayout.setVisibility(View.VISIBLE);
260 mReceiveLayout.setVisibility(View.VISIBLE);
262 mRequestTitleLayout.setVisibility(View.INVISIBLE);
263 mRequestToAllTitleLayout.setVisibility(View.VISIBLE);
264 mHandleTitleLayout.setVisibility(View.VISIBLE);
266 mResponseNotificationTitleLayout.setVisibility(View.VISIBLE);
267 mSendResponseNotiSettingLayout.setVisibility(View.VISIBLE);
269 mNetwork_tv.setText("");
271 } else if (mCurrentMode == Mode.CLIENT) {
273 mSendNotificationLayout.setVisibility(View.INVISIBLE);
274 mSendRequestLayout.setVisibility(View.VISIBLE);
275 mSendRequestToAllLayout.setVisibility(View.VISIBLE);
276 mSendRequestSettingLayout.setVisibility(View.VISIBLE);
277 mSendRequestToAllSettingLayout.setVisibility(View.VISIBLE);
278 mReceiveLayout.setVisibility(View.VISIBLE);
280 mRequestTitleLayout.setVisibility(View.VISIBLE);
281 mRequestToAllTitleLayout.setVisibility(View.VISIBLE);
282 mHandleTitleLayout.setVisibility(View.VISIBLE);
284 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
285 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
287 mNetwork_tv.setText("");
292 protected void onDestroy() {
295 // Terminate Connectivity Abstraction
297 android.os.Process.killProcess(android.os.Process.myPid());
301 public boolean onCreateOptionsMenu(Menu menu) {
303 menu.add(0, 1, Menu.NONE, "Start Server");
304 menu.add(0, 2, Menu.NONE, "Start Client");
305 menu.add(0, 3, Menu.NONE, "Select Network");
311 public boolean onOptionsItemSelected(MenuItem item) {
313 switch (item.getItemId()) {
317 RM.RMStartListeningServer();
319 if (interestedNetwork == 0) {
320 mCurrentMode = Mode.SERVER;
321 mMode_tv.setText("MODE: " + mCurrentMode.toString());
322 Log.i(TAG, "MODE: " + mCurrentMode.toString());
326 mCurrentMode = Mode.SERVER;
327 mMode_tv.setText("MODE: " + mCurrentMode.toString());
328 Log.i(TAG, "MODE: " + mCurrentMode.toString());
336 RM.RMStartDiscoveryServer();
338 if (interestedNetwork == 0) {
339 mCurrentMode = Mode.CLIENT;
340 mMode_tv.setText("MODE: " + mCurrentMode.toString());
341 Log.i(TAG, "MODE: " + mCurrentMode.toString());
345 mCurrentMode = Mode.CLIENT;
346 mMode_tv.setText("MODE: " + mCurrentMode.toString());
347 Log.i(TAG, "MODE: " + mCurrentMode.toString());
355 checkInterestedNetwork("Select Network");
360 return super.onOptionsItemSelected(item);
363 private OnClickListener mSendResponseHandler = new OnClickListener() {
366 public void onClick(View v) {
368 DLog.v(TAG, "SendResponse click");
369 if ( selectedNetwork != -1) {
370 RM.RMSendResponse(selectedNetwork, isSecured, msgType, responseValue);
373 DLog.v(TAG, "Please Select Network Type");
378 private OnClickListener mNotifyHandler = new OnClickListener() {
381 public void onClick(View v) {
383 DLog.v(TAG, "SendNotification click");
384 if ( selectedNetwork != -1) {
385 RM.RMSendNotification(mNotification_ed.getText().toString(),
386 null, selectedNetwork, isSecured, msgType);
389 DLog.v(TAG, "Please Select Network Type");
394 private OnClickListener mSendRequestHandler = new OnClickListener() {
397 public void onClick(View v) {
399 DLog.v(TAG, "SendRequest click");
400 if ( selectedNetwork != -1) {
401 RM.RMSendRequest(mReqData_ed.getText().toString(), null,
402 selectedNetwork, isSecured, msgType, false);
405 DLog.v(TAG, "Please Select Network Type");
410 private OnClickListener mSendRequestSettingHandler = new OnClickListener() {
413 public void onClick(View v) {
414 checkSendNetworkType("Select Send Network Type");
418 private OnClickListener mSendRequestToAllHandler = new OnClickListener() {
421 public void onClick(View v) {
423 DLog.v(TAG, "SendRequestToAll click");
424 if ( selectedNetwork != -1) {
425 RM.RMSendReqestToAll(mReqToAllData_ed.getText().toString(), selectedNetwork);
428 DLog.v(TAG, "Please Select Network Type");
433 private OnClickListener mSendRequestToAllSettingHandler = new OnClickListener() {
436 public void onClick(View v) {
437 isSendRequestToAllSetting = true;
438 checkSendNetworkType("Select Send Network Type");
442 private OnClickListener mSendResponseNotiSettingHandler = new OnClickListener() {
445 public void onClick(View v) {
446 isSendResponseSetting = true;
447 checkSendNetworkType("Select Send Network Type");
451 private OnClickListener mGetNetworkInfoHandler = new OnClickListener() {
453 public void onClick(View v) {
455 RM.RMGetNetworkInfomation();
459 private OnClickListener mResponseHandler = new OnClickListener() {
462 public void onClick(View v) {
464 RM.RMHandleRequestResponse();
468 private OnClickListener mSelectLargeDataButtonHandler = new OnClickListener() {
470 public void onClick(View v) {
473 checkSendNetworkType("Select Send Network Type");
477 private void checkInterestedNetwork(String title) {
479 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
480 builder.setTitle(title)
481 .setMultiChoiceItems(mNetworkCheckBoxItems, mCheckedItems,
482 new DialogInterface.OnMultiChoiceClickListener() {
485 public void onClick(DialogInterface dialog,
486 int which, boolean isChecked) {
490 mSelectedItems[which] = 1;
491 mUnSelectedItems[which] = 0;
493 } else if (mSelectedItems[which] == 1) {
495 mSelectedItems[which] = 0;
496 mUnSelectedItems[which] = 1;
500 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
503 public void onClick(DialogInterface dialog, int which) {
505 interestedNetwork = 0;
507 for (int i = 0; i < mSelectedItems.length; i++) {
508 if (mSelectedItems[i] == 1) {
509 interestedNetwork |= (1 << i);
512 if(0 != interestedNetwork)
513 RM.RMSelectNetwork(interestedNetwork);
515 uninterestedNetwork = 0;
517 for (int i = 0; i < mUnSelectedItems.length; i++) {
518 if (mUnSelectedItems[i] == 1) {
519 uninterestedNetwork |= (1 << i);
522 if(0 != uninterestedNetwork)
523 RM.RMUnSelectNetwork(uninterestedNetwork);
529 private void checkMsgSecured(String title) {
531 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
533 builder.setTitle(title)
534 .setSingleChoiceItems(mDTLSCheckBoxItems, selectedMsgSecured,
535 new DialogInterface.OnClickListener() {
538 public void onClick(DialogInterface dialog,
540 selectedMsgSecured = which;
543 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
546 public void onClick(DialogInterface dialog, int which) {
548 if (selectedMsgSecured == DTLS.SECURED.ordinal()) {
550 DLog.v(TAG, "Send secured message");
552 } else if (selectedMsgSecured == DTLS.UNSECURED.ordinal()) {
554 DLog.v(TAG, "Send unsecured message");
556 checkMsgType("Select Msg Type");
562 private void checkMsgType(String title) {
564 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
565 builder.setTitle(title)
566 .setSingleChoiceItems(mMsgTyleCheckBoxItems, selectedMsgType,
567 new DialogInterface.OnClickListener() {
570 public void onClick(DialogInterface dialog,
572 selectedMsgType = which;
575 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
578 public void onClick(DialogInterface dialog, int which) {
580 if (selectedMsgType == MsgType.CON.ordinal()) {
582 DLog.v(TAG, "Message Type is CON");
584 } else if (selectedMsgType == MsgType.NON.ordinal()) {
586 DLog.v(TAG, "Message Type is NON");
587 } else if (selectedMsgType == MsgType.ACK.ordinal()) {
589 DLog.v(TAG, "Message Type is ACK");
590 } else if (selectedMsgType == MsgType.RESET.ordinal()) {
592 DLog.v(TAG, "Message Type is RESET");
595 if (isSendResponseSetting == true && msgType != 3) {
596 checkResponseResult("Select Value of Response Result");
597 isSendResponseSetting = false;
603 private void checkResponseResult(String title) {
605 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
606 builder.setTitle(title)
607 .setSingleChoiceItems(mResponseResultCheckBoxItems, selectedResponseValue,
608 new DialogInterface.OnClickListener() {
611 public void onClick(DialogInterface dialog,
613 selectedResponseValue = which;
616 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
619 public void onClick(DialogInterface dialog, int which) {
621 if (selectedResponseValue == ResponseResult.CA_CREATED.ordinal()) {
623 DLog.v(TAG, "Response Value is CA_CREATED");
624 } else if (selectedResponseValue == ResponseResult.CA_DELETED
627 DLog.v(TAG, "Response Value is CA_DELETED");
628 } else if (selectedResponseValue == ResponseResult.CA_VALID
631 DLog.v(TAG, "Response Value is CA_VALID");
632 } else if (selectedResponseValue == ResponseResult.CA_CHANGED
635 DLog.v(TAG, "Response Value is CA_CHANGED");
636 } else if (selectedResponseValue == ResponseResult.CA_CONTENT
639 DLog.v(TAG, "Response Value is CA_CONTENT");
640 } else if (selectedResponseValue == ResponseResult.CA_EMPTY
643 DLog.v(TAG, "Response Value is CA_EMPTY");
644 } else if (selectedResponseValue == ResponseResult.CA_BAD_REQ
647 DLog.v(TAG, "Response Value is CA_BAD_REQ");
648 } else if (selectedResponseValue == ResponseResult.CA_BAD_OPT
651 DLog.v(TAG, "Response Value is CA_BAD_OPT");
652 } else if (selectedResponseValue == ResponseResult.CA_NOT_FOUND
655 DLog.v(TAG, "Response Value is CA_NOT_FOUND");
656 } else if (selectedResponseValue ==
657 ResponseResult.CA_INTERNAL_SERVER_ERROR
660 DLog.v(TAG, "Response Value is CA_INTERNAL_SERVER_ERROR");
661 } else if (selectedResponseValue == ResponseResult.CA_RETRANSMIT_TIMEOUT
664 DLog.v(TAG, "Response Value is CA_RETRANSMIT_TIMEOUT");
670 private void checkSendNetworkType(String title) {
671 selectedNetworkType = -1;
672 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
674 builder.setTitle(title)
675 .setSingleChoiceItems(mNetworkCheckBoxItems, -1,
676 new DialogInterface.OnClickListener() {
679 public void onClick(DialogInterface dialog,
681 selectedNetworkType = which;
684 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
687 public void onClick(DialogInterface dialog, int which) {
689 if (selectedNetworkType == Network.IP.ordinal()) {
690 selectedNetwork = CA_IP;
691 DLog.v(TAG, "Selected Network is IP");
692 } else if (selectedNetworkType == Network.LE.ordinal()) {
693 selectedNetwork = CA_LE;
694 DLog.v(TAG, "Selected Network is LE");
695 } else if (selectedNetworkType == Network.EDR.ordinal()) {
696 selectedNetwork = CA_EDR;
697 DLog.v(TAG, "Selected Network is EDR");
699 DLog.v(TAG, "Selected Network is NULL");
700 selectedNetwork = -1;
705 new FileChooser(MainActivity.this).setFileListener(new FileSelectedListener() {
706 public void fileSelected(final File file) {
707 if (selectedNetwork != -1) {
709 String path = file.getAbsolutePath();
710 Log.d(TAG, "File Path: " + path);
712 RM.RMSendRequest(mReqData_ed.getText().toString(), path,
713 selectedNetwork, isSecured, msgType, true);
715 Toast.makeText(getApplicationContext(),
716 "Request Setting Fisrt!!", Toast.LENGTH_LONG).show();
722 if (isSendRequestToAllSetting != true) {
723 checkMsgSecured("Select DTLS Type");
727 isSendRequestToAllSetting = false;
733 public void OnResponseReceived(String subject, String receivedData) {
734 String callbackData = subject + receivedData;
735 DLog.v(TAG, callbackData);
737 if (subject.equals(getString(R.string.remote_address))) {
738 StringBuilder sb = new StringBuilder();
739 sb.append(getString(R.string.coap_prefix)).append(receivedData);
740 mReqData_ed.setText(sb.toString());
741 mNotification_ed.setText(sb.toString());
742 } else if (subject.equals(getString(R.string.remote_port))) {
743 StringBuilder sb = new StringBuilder();
744 String uri = mReqData_ed.getText().toString();
746 if (null != receivedData && uri.contains("."))
748 sb.append(":").append(receivedData);
750 sb.append(getString(R.string.uri));
751 mReqData_ed.setText(sb.toString());
752 mNotification_ed.setText(sb.toString());