1 package org.iotivity.ca.service;
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.content.Intent;
7 import android.nfc.NfcAdapter;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.util.Log;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.View;
14 import android.view.View.OnClickListener;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.RelativeLayout;
18 import android.widget.TextView;
19 import android.widget.Toast;
22 import org.iotivity.ca.sample_service.R;
23 import org.iotivity.ca.service.FileChooser;
24 import org.iotivity.ca.service.FileChooser.FileSelectedListener;
26 public class MainActivity extends Activity {
28 static RMInterface RM = new RMInterface();
30 private final static String TAG = "Sample_Service : MainActivity";
32 private final CharSequence[] mNetworkCheckBoxItems = { Network.IP.name(),
33 Network.LE.name(), Network.EDR.name(), Network.TCP.name(), Network.NFC.name()};
35 private final CharSequence[] mDTLSCheckBoxItems = { DTLS.UNSECURED.name(),
36 DTLS.SECURED.name() };
38 private final CharSequence[] mMsgTyleCheckBoxItems = { MsgType.CON.name(),
39 MsgType.NON.name(), MsgType.ACK.name(), MsgType.RESET.name() };
41 private final CharSequence[] mResponseResultCheckBoxItems = {
42 ResponseResult.CA_CREATED.name(), ResponseResult.CA_DELETED.name(),
43 ResponseResult.CA_VALID.name(), ResponseResult.CA_CHANGED.name(),
44 ResponseResult.CA_CONTENT.name(), ResponseResult.CA_EMPTY.name(),
45 ResponseResult.CA_BAD_REQ.name(), ResponseResult.CA_BAD_OPT.name(),
46 ResponseResult.CA_NOT_FOUND.name(), ResponseResult.CA_INTERNAL_SERVER_ERROR.name(),
47 ResponseResult.CA_RETRANSMIT_TIMEOUT.name() };
50 SERVER, CLIENT, BOTH, UNKNOWN
53 private enum Network {
61 private enum MsgType {
65 private enum ResponseResult {
66 CA_CREATED, CA_DELETED, CA_VALID, CA_CHANGED, CA_CONTENT, CA_EMPTY,
67 CA_BAD_REQ, CA_BAD_OPT, CA_NOT_FOUND, CA_INTERNAL_SERVER_ERROR, CA_RETRANSMIT_TIMEOUT
70 private boolean mCheckedItems[] = {
71 false, false, false, false
74 private int mSelectedItems[] = { 0, 0, 0, 0, 0, 0};
76 private int mUnSelectedItems[] = { 0, 0, 0, 0, 0, 0};
78 private Mode mCurrentMode = Mode.UNKNOWN;
80 private RelativeLayout mSendNotificationLayout = null;
82 private RelativeLayout mSendRequestLayout = null;
84 private RelativeLayout mSendRequestToAllLayout = null;
86 private RelativeLayout mSendRequestSettingLayout = null;
88 private RelativeLayout mSendRequestToAllSettingLayout = null;
90 private RelativeLayout mSendResponseNotiSettingLayout = null;
92 private RelativeLayout mReceiveLayout = null;
94 private RelativeLayout mRequestTitleLayout = null;
96 private RelativeLayout mRequestToAllTitleLayout = null;
98 private RelativeLayout mResponseNotificationTitleLayout = null;
100 private RelativeLayout mHandleTitleLayout = null;
102 private TextView mMode_tv = null;
104 private TextView mNetwork_tv = null;
106 private EditText mNotification_ed = null;
108 private EditText mReqData_ed = null;
110 private EditText mReqToAllData_ed = null;
112 private Button mNotify_btn = null;
114 private Button mReqeust_btn = null;
116 private Button mReqeust_setting_btn = null;
118 private Button mReqeustToAll_btn = null;
120 private Button mReqeustToAll_setting_btn = null;
122 private Button mResponse_Notify_setting_btn = null;
124 private Button mResponse_btn = null;
126 private Button mGetNetworkInfo_btn = null;
128 private Button mRecv_btn = null;
130 private Button mBig_btn = null;
132 private Handler mLogHandler = null;
135 * Defined ConnectivityType in cacommon.c
137 * CA_IP = (1 << 0) CA_LE = (1 << 1) CA_EDR = (1 << 2) CA_TCP = (1 << 4) CA_NFC= (1 << 5)
139 private int CA_IP = (1 << 0);
140 private int CA_LE = (1 << 1);
141 private int CA_EDR = (1 << 2);
142 private int CA_TCP = (1 << 4);
143 private int CA_NFC = (1 << 5);
144 private int isSecured = 0;
145 private int msgType = 1;
146 private int responseValue = 0;
147 private int selectedNetworkType = -1;
148 private int selectedMsgType = 1;
149 private int selectedMsgSecured = 0;
150 private int selectedResponseValue = 0;
151 int selectedNetwork = -1;
152 int interestedNetwork = 0;
153 int uninterestedNetwork = 0;
154 private boolean isSendResponseSetting = false;
155 private boolean isSendRequestToAllSetting = false;
156 private boolean isBigData = false;
159 protected void onCreate(Bundle savedInstanceState) {
160 super.onCreate(savedInstanceState);
161 setContentView(R.layout.activity_main);
163 RM.setResponseListener(this);
165 mLogHandler = new Handler();
166 TextView logView = (TextView) findViewById(R.id.tv_result);
167 DLog.setTextView(mLogHandler, logView);
171 mReceiveLayout = (RelativeLayout) findViewById(R.id.layout_receive);
174 mSendRequestLayout = (RelativeLayout) findViewById(R.id.layout_request);
175 mSendRequestToAllLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all);
176 mSendRequestSettingLayout = (RelativeLayout)
177 findViewById(R.id.layout_request_setting_for_client);
178 mSendRequestToAllSettingLayout = (RelativeLayout)
179 findViewById(R.id.layout_request_to_all_setting_for_client);
180 mRequestTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_title);
181 mRequestToAllTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all_title);
182 mHandleTitleLayout = (RelativeLayout) findViewById(R.id.layout_handle_title);
185 mSendNotificationLayout = (RelativeLayout) findViewById(R.id.layout_notify);
186 mSendResponseNotiSettingLayout = (RelativeLayout)
187 findViewById(R.id.layout_request_setting_for_server);
188 mResponseNotificationTitleLayout = (RelativeLayout)
189 findViewById(R.id.layout_Response_Noti_title);
191 mMode_tv = (TextView) findViewById(R.id.tv_mode);
192 mNetwork_tv = (TextView) findViewById(R.id.tv_network);
194 mNotification_ed = (EditText) findViewById(R.id.et_notification);
195 mReqData_ed = (EditText) findViewById(R.id.et_req_data);
196 mReqToAllData_ed = (EditText) findViewById(R.id.et_req_to_all_data);
198 mResponse_btn = (Button) findViewById(R.id.btn_sendresponse);
199 mNotify_btn = (Button) findViewById(R.id.btn_notify);
200 mReqeust_btn = (Button) findViewById(R.id.btn_Request);
201 mReqeust_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_client);
202 mReqeustToAll_btn = (Button) findViewById(R.id.btn_request_to_all);
203 mReqeustToAll_setting_btn = (Button)
204 findViewById(R.id.btn_request_to_all_setting_for_client);
205 mResponse_Notify_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_server);
206 mGetNetworkInfo_btn = (Button) findViewById(R.id.btn_get_network_info);
207 mRecv_btn = (Button) findViewById(R.id.btn_receive);
208 mBig_btn = (Button) findViewById(R.id.btn_big_data);
209 mBig_btn.setOnClickListener(mSelectLargeDataButtonHandler);
211 mResponse_btn.setOnClickListener(mSendResponseHandler);
212 mNotify_btn.setOnClickListener(mNotifyHandler);
213 mReqeust_btn.setOnClickListener(mSendRequestHandler);
214 mReqeust_setting_btn.setOnClickListener(mSendRequestSettingHandler);
215 mReqeustToAll_btn.setOnClickListener(mSendRequestToAllHandler);
216 mReqeustToAll_setting_btn.setOnClickListener(mSendRequestToAllSettingHandler);
217 mResponse_Notify_setting_btn
218 .setOnClickListener(mSendResponseNotiSettingHandler);
219 mRecv_btn.setOnClickListener(mResponseHandler);
220 mGetNetworkInfo_btn.setOnClickListener(mGetNetworkInfoHandler);
222 showSelectModeView();
224 // Initialize Connectivity Abstraction
225 RM.RMInitialize(getApplicationContext(), this);
227 RM.RMRegisterHandler();
230 private void showSelectModeView() {
232 mSendNotificationLayout.setVisibility(View.INVISIBLE);
233 mSendRequestLayout.setVisibility(View.INVISIBLE);
234 mSendRequestToAllLayout.setVisibility(View.INVISIBLE);
235 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
236 mSendRequestToAllSettingLayout.setVisibility(View.INVISIBLE);
237 mReceiveLayout.setVisibility(View.INVISIBLE);
238 mRequestTitleLayout.setVisibility(View.INVISIBLE);
239 mRequestToAllTitleLayout.setVisibility(View.INVISIBLE);
240 mHandleTitleLayout.setVisibility(View.INVISIBLE);
241 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
242 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
244 mMode_tv.setText("Select Mode (Server or Client)");
245 Log.i(TAG, "Select Mode (Server or Client)");
248 private void showNetworkView() {
250 mNetwork_tv.setText("Select Network Type");
251 Log.i(TAG, "Select Network Type");
254 private void showModeView() {
256 mReceiveLayout.setVisibility(View.VISIBLE);
257 mSendRequestToAllLayout.setVisibility(View.VISIBLE);
258 mSendRequestToAllSettingLayout.setVisibility(View.VISIBLE);
259 mRequestToAllTitleLayout.setVisibility(View.VISIBLE);
260 mHandleTitleLayout.setVisibility(View.VISIBLE);
262 if (mCurrentMode == Mode.SERVER) {
264 mSendNotificationLayout.setVisibility(View.VISIBLE);
265 mSendRequestLayout.setVisibility(View.INVISIBLE);
266 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
268 mRequestTitleLayout.setVisibility(View.INVISIBLE);
270 mResponseNotificationTitleLayout.setVisibility(View.VISIBLE);
271 mSendResponseNotiSettingLayout.setVisibility(View.VISIBLE);
273 } else if (mCurrentMode == Mode.CLIENT) {
275 mSendNotificationLayout.setVisibility(View.INVISIBLE);
276 mSendRequestLayout.setVisibility(View.VISIBLE);
277 mSendRequestSettingLayout.setVisibility(View.VISIBLE);
279 mRequestTitleLayout.setVisibility(View.VISIBLE);
281 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
282 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
284 mNetwork_tv.setText("");
288 public void onNewIntent(Intent intent) {
289 super.onNewIntent(intent);
290 Intent i = new Intent();
291 i.setAction(intent.getAction());
292 i.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES,
293 intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES));
295 Log.d(TAG, "Broadcast intent complete");
299 protected void onDestroy() {
302 // Terminate Connectivity Abstraction
304 android.os.Process.killProcess(android.os.Process.myPid());
308 public boolean onCreateOptionsMenu(Menu menu) {
310 menu.add(0, 1, Menu.NONE, "Start Server");
311 menu.add(0, 2, Menu.NONE, "Start Client");
312 menu.add(0, 3, Menu.NONE, "Select Network");
318 public boolean onOptionsItemSelected(MenuItem item) {
320 switch (item.getItemId()) {
324 RM.RMStartListeningServer();
326 if (interestedNetwork == 0) {
327 mCurrentMode = Mode.SERVER;
328 mMode_tv.setText("MODE: " + mCurrentMode.toString());
329 Log.i(TAG, "MODE: " + mCurrentMode.toString());
333 mCurrentMode = Mode.SERVER;
334 mMode_tv.setText("MODE: " + mCurrentMode.toString());
335 Log.i(TAG, "MODE: " + mCurrentMode.toString());
343 RM.RMStartDiscoveryServer();
345 if (interestedNetwork == 0) {
346 mCurrentMode = Mode.CLIENT;
347 mMode_tv.setText("MODE: " + mCurrentMode.toString());
348 Log.i(TAG, "MODE: " + mCurrentMode.toString());
352 mCurrentMode = Mode.CLIENT;
353 mMode_tv.setText("MODE: " + mCurrentMode.toString());
354 Log.i(TAG, "MODE: " + mCurrentMode.toString());
362 checkInterestedNetwork("Select Network");
367 return super.onOptionsItemSelected(item);
370 private OnClickListener mSendResponseHandler = new OnClickListener() {
373 public void onClick(View v) {
375 DLog.v(TAG, "SendResponse click");
376 if ( selectedNetwork != -1) {
377 RM.RMSendResponse(selectedNetwork, isSecured, msgType, responseValue);
380 DLog.v(TAG, "Please Select Network Type");
385 private OnClickListener mNotifyHandler = new OnClickListener() {
388 public void onClick(View v) {
390 DLog.v(TAG, "SendNotification click");
391 if ( selectedNetwork != -1) {
392 RM.RMSendNotification(mNotification_ed.getText().toString(),
393 null, selectedNetwork, isSecured, msgType);
396 DLog.v(TAG, "Please Select Network Type");
401 private OnClickListener mSendRequestHandler = new OnClickListener() {
404 public void onClick(View v) {
406 DLog.v(TAG, "SendRequest click");
407 if ( selectedNetwork != -1) {
408 RM.RMSendRequest(mReqData_ed.getText().toString(), null,
409 selectedNetwork, isSecured, msgType, false);
412 DLog.v(TAG, "Please Select Network Type");
417 private OnClickListener mSendRequestSettingHandler = new OnClickListener() {
420 public void onClick(View v) {
421 checkSendNetworkType("Select Send Network Type");
425 private OnClickListener mSendRequestToAllHandler = new OnClickListener() {
428 public void onClick(View v) {
430 DLog.v(TAG, "SendRequestToAll click");
431 if ( selectedNetwork != -1) {
432 RM.RMSendReqestToAll(mReqToAllData_ed.getText().toString(), selectedNetwork);
435 DLog.v(TAG, "Please Select Network Type");
440 private OnClickListener mSendRequestToAllSettingHandler = new OnClickListener() {
443 public void onClick(View v) {
444 isSendRequestToAllSetting = true;
445 checkSendNetworkType("Select Send Network Type");
449 private OnClickListener mSendResponseNotiSettingHandler = new OnClickListener() {
452 public void onClick(View v) {
453 isSendResponseSetting = true;
454 checkSendNetworkType("Select Send Network Type");
458 private OnClickListener mGetNetworkInfoHandler = new OnClickListener() {
460 public void onClick(View v) {
462 RM.RMGetNetworkInfomation();
466 private OnClickListener mResponseHandler = new OnClickListener() {
469 public void onClick(View v) {
471 RM.RMHandleRequestResponse();
475 private OnClickListener mSelectLargeDataButtonHandler = new OnClickListener() {
477 public void onClick(View v) {
480 checkSendNetworkType("Select Send Network Type");
484 private void checkInterestedNetwork(String title) {
486 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
487 builder.setTitle(title)
488 .setMultiChoiceItems(mNetworkCheckBoxItems, mCheckedItems,
489 new DialogInterface.OnMultiChoiceClickListener() {
492 public void onClick(DialogInterface dialog,
493 int which, boolean isChecked) {
495 // in case of TCP increase value,
496 // because CASample not support RA.
497 // which: 0(IP), 1(BT), 2(BLE), 3(TCP)
502 mSelectedItems[which] = 1;
503 mUnSelectedItems[which] = 0;
505 } else if (mSelectedItems[which] == 1) {
507 mSelectedItems[which] = 0;
508 mUnSelectedItems[which] = 1;
512 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
515 public void onClick(DialogInterface dialog, int which) {
517 interestedNetwork = 0;
519 for (int i = 0; i < mSelectedItems.length; i++) {
520 if (mSelectedItems[i] == 1) {
521 interestedNetwork |= (1 << i);
524 if(0 != interestedNetwork)
525 RM.RMSelectNetwork(interestedNetwork);
527 uninterestedNetwork = 0;
529 for (int i = 0; i < mUnSelectedItems.length; i++) {
530 if (mUnSelectedItems[i] == 1) {
531 uninterestedNetwork |= (1 << i);
534 if(0 != uninterestedNetwork)
535 RM.RMUnSelectNetwork(uninterestedNetwork);
541 private void checkMsgSecured(String title) {
543 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
545 builder.setTitle(title)
546 .setSingleChoiceItems(mDTLSCheckBoxItems, selectedMsgSecured,
547 new DialogInterface.OnClickListener() {
550 public void onClick(DialogInterface dialog,
552 selectedMsgSecured = which;
555 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
558 public void onClick(DialogInterface dialog, int which) {
560 if (selectedMsgSecured == DTLS.SECURED.ordinal()) {
562 DLog.v(TAG, "Send secured message");
564 } else if (selectedMsgSecured == DTLS.UNSECURED.ordinal()) {
566 DLog.v(TAG, "Send unsecured message");
568 checkMsgType("Select Msg Type");
574 private void checkMsgType(String title) {
576 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
577 builder.setTitle(title)
578 .setSingleChoiceItems(mMsgTyleCheckBoxItems, selectedMsgType,
579 new DialogInterface.OnClickListener() {
582 public void onClick(DialogInterface dialog,
584 selectedMsgType = which;
587 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
590 public void onClick(DialogInterface dialog, int which) {
592 if (selectedMsgType == MsgType.CON.ordinal()) {
594 DLog.v(TAG, "Message Type is CON");
596 } else if (selectedMsgType == MsgType.NON.ordinal()) {
598 DLog.v(TAG, "Message Type is NON");
599 } else if (selectedMsgType == MsgType.ACK.ordinal()) {
601 DLog.v(TAG, "Message Type is ACK");
602 } else if (selectedMsgType == MsgType.RESET.ordinal()) {
604 DLog.v(TAG, "Message Type is RESET");
607 if (isSendResponseSetting == true && msgType != 3) {
608 checkResponseResult("Select Value of Response Result");
609 isSendResponseSetting = false;
615 private void checkResponseResult(String title) {
617 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
618 builder.setTitle(title)
619 .setSingleChoiceItems(mResponseResultCheckBoxItems, selectedResponseValue,
620 new DialogInterface.OnClickListener() {
623 public void onClick(DialogInterface dialog,
625 selectedResponseValue = which;
628 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
631 public void onClick(DialogInterface dialog, int which) {
633 if (selectedResponseValue == ResponseResult.CA_CREATED.ordinal()) {
635 DLog.v(TAG, "Response Value is CA_CREATED");
636 } else if (selectedResponseValue == ResponseResult.CA_DELETED
639 DLog.v(TAG, "Response Value is CA_DELETED");
640 } else if (selectedResponseValue == ResponseResult.CA_VALID
643 DLog.v(TAG, "Response Value is CA_VALID");
644 } else if (selectedResponseValue == ResponseResult.CA_CHANGED
647 DLog.v(TAG, "Response Value is CA_CHANGED");
648 } else if (selectedResponseValue == ResponseResult.CA_CONTENT
651 DLog.v(TAG, "Response Value is CA_CONTENT");
652 } else if (selectedResponseValue == ResponseResult.CA_EMPTY
655 DLog.v(TAG, "Response Value is CA_EMPTY");
656 } else if (selectedResponseValue == ResponseResult.CA_BAD_REQ
659 DLog.v(TAG, "Response Value is CA_BAD_REQ");
660 } else if (selectedResponseValue == ResponseResult.CA_BAD_OPT
663 DLog.v(TAG, "Response Value is CA_BAD_OPT");
664 } else if (selectedResponseValue == ResponseResult.CA_NOT_FOUND
667 DLog.v(TAG, "Response Value is CA_NOT_FOUND");
668 } else if (selectedResponseValue ==
669 ResponseResult.CA_INTERNAL_SERVER_ERROR
672 DLog.v(TAG, "Response Value is CA_INTERNAL_SERVER_ERROR");
673 } else if (selectedResponseValue == ResponseResult.CA_RETRANSMIT_TIMEOUT
676 DLog.v(TAG, "Response Value is CA_RETRANSMIT_TIMEOUT");
682 private void checkSendNetworkType(String title) {
683 selectedNetworkType = -1;
684 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
686 builder.setTitle(title)
687 .setSingleChoiceItems(mNetworkCheckBoxItems, -1,
688 new DialogInterface.OnClickListener() {
691 public void onClick(DialogInterface dialog,
693 selectedNetworkType = which;
696 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
699 public void onClick(DialogInterface dialog, int which) {
701 if (selectedNetworkType == Network.IP.ordinal()) {
702 selectedNetwork = CA_IP;
703 DLog.v(TAG, "Selected Network is IP");
704 } else if (selectedNetworkType == Network.LE.ordinal()) {
705 selectedNetwork = CA_LE;
706 DLog.v(TAG, "Selected Network is LE");
707 } else if (selectedNetworkType == Network.EDR.ordinal()) {
708 selectedNetwork = CA_EDR;
709 DLog.v(TAG, "Selected Network is EDR");
710 } else if (selectedNetworkType == Network.TCP.ordinal()) {
711 selectedNetwork = CA_TCP;
712 DLog.v(TAG, "Selected Network is TCP");
713 } else if (selectedNetworkType == Network.NFC.ordinal()) {
714 selectedNetwork = CA_NFC;
715 DLog.v(TAG, "Selected Network is NFC");
717 DLog.v(TAG, "Selected Network is NULL");
718 selectedNetwork = -1;
723 new FileChooser(MainActivity.this).setFileListener(
724 new FileSelectedListener() {
725 public void fileSelected(final File file) {
726 if (selectedNetwork != -1) {
728 String path = file.getAbsolutePath();
729 Log.d(TAG, "File Path: " + path);
731 RM.RMSendRequest(mReqData_ed.getText().toString(), path,
732 selectedNetwork, isSecured, msgType, true);
734 Toast.makeText(getApplicationContext(),
735 "Request Setting Fisrt!!",
736 Toast.LENGTH_LONG).show();
742 if (isSendRequestToAllSetting != true) {
743 checkMsgSecured("Select DTLS Type");
747 isSendRequestToAllSetting = false;
753 public void OnResponseReceived(String subject, String receivedData) {
754 String callbackData = subject + receivedData;
755 DLog.v(TAG, callbackData);
757 if (subject.equals(getString(R.string.remote_address))) {
758 StringBuilder sb = new StringBuilder();
759 sb.append(getString(R.string.coap_prefix)).append(receivedData);
760 mReqData_ed.setText(sb.toString());
761 mNotification_ed.setText(sb.toString());
762 } else if (subject.equals(getString(R.string.remote_port))) {
763 StringBuilder sb = new StringBuilder();
764 String uri = mReqData_ed.getText().toString();
766 if (null != receivedData && uri.contains("."))
768 sb.append(":").append(receivedData);
770 sb.append(getString(R.string.uri));
771 mReqData_ed.setText(sb.toString());
772 mNotification_ed.setText(sb.toString());