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;
18 import org.iotivity.ca.sample_service.R;
20 public class MainActivity extends Activity {
22 static RMInterface RM = new RMInterface();
24 private final static String TAG = "Sample_Service : MainActivity";
26 private final CharSequence[] mNetworkCheckBoxItems = { Network.IP.name(),
27 Network.LE.name(), Network.EDR.name()};
29 private final CharSequence[] mDTLSCheckBoxItems = { DTLS.UNSECURED.name(),
30 DTLS.SECURED.name() };
32 private final CharSequence[] mMsgTyleCheckBoxItems = { MsgType.CON.name(),
33 MsgType.NON.name(), MsgType.ACK.name(), MsgType.RESET.name() };
35 private final CharSequence[] mResponseResultCheckBoxItems = {
36 ResponseResult.CA_SUCCESS.name(), ResponseResult.CA_CREATED.name(),
37 ResponseResult.CA_DELETED.name(), ResponseResult.CA_VALID.name(),
38 ResponseResult.CA_CHANGED.name(), ResponseResult.CA_CONTENT.name(),
39 ResponseResult.CA_EMPTY.name(), ResponseResult.CA_BAD_REQ.name(),
40 ResponseResult.CA_BAD_OPT.name(), ResponseResult.CA_NOT_FOUND.name(),
41 ResponseResult.CA_INTERNAL_SERVER_ERROR.name(),
42 ResponseResult.CA_RETRANSMIT_TIMEOUT.name() };
45 SERVER, CLIENT, BOTH, UNKNOWN
48 private enum Network {
56 private enum MsgType {
60 private enum ResponseResult {
61 CA_SUCCESS, CA_CREATED, CA_DELETED, CA_VALID, CA_CHANGED, CA_CONTENT, CA_EMPTY,
62 CA_BAD_REQ, CA_BAD_OPT, CA_NOT_FOUND, CA_INTERNAL_SERVER_ERROR, CA_RETRANSMIT_TIMEOUT
65 private boolean mCheckedItems[] = {
66 false, false, false, false
69 private int mSelectedItems[] = { 0, 0, 0 };
71 private int mUnSelectedItems[] = { 0, 0, 0 };
73 private Mode mCurrentMode = Mode.UNKNOWN;
75 private RelativeLayout mSendNotificationLayout = null;
77 private RelativeLayout mSendRequestLayout = null;
79 private RelativeLayout mSendRequestToAllLayout = null;
81 private RelativeLayout mSendRequestSettingLayout = null;
83 private RelativeLayout mSendRequestToAllSettingLayout = null;
85 private RelativeLayout mSendResponseNotiSettingLayout = null;
87 private RelativeLayout mReceiveLayout = null;
89 private RelativeLayout mRequestTitleLayout = null;
91 private RelativeLayout mRequestToAllTitleLayout = null;
93 private RelativeLayout mResponseNotificationTitleLayout = null;
95 private RelativeLayout mHandleTitleLayout = null;
97 private TextView mMode_tv = null;
99 private TextView mNetwork_tv = null;
101 private EditText mNotification_ed = null;
103 private EditText mReqData_ed = null;
105 private EditText mReqToAllData_ed = null;
107 private Button mNotify_btn = null;
109 private Button mReqeust_btn = null;
111 private Button mReqeust_setting_btn = null;
113 private Button mReqeustToAll_btn = null;
115 private Button mReqeustToAll_setting_btn = null;
117 private Button mResponse_Notify_setting_btn = null;
119 private Button mResponse_btn = null;
121 private Button mGetNetworkInfo_btn = null;
123 private Button mRecv_btn = null;
125 private Handler mLogHandler = null;
128 * Defined ConnectivityType in cacommon.c
130 * CA_IP = (1 << 0) CA_LE = (1 << 2) CA_EDR = (1 << 3)
132 private int CA_IP = (1 << 0);
133 private int CA_LE = (1 << 1);
134 private int CA_EDR = (1 << 2);
135 private int isSecured = 0;
136 private int msgType = 1;
137 private int responseValue = 0;
138 private int selectedNetworkType = -1;
139 private int selectedMsgType = 1;
140 private int selectedMsgSecured = 0;
141 private int selectedResponseValue = 0;
142 int selectedNetwork = -1;
143 int interestedNetwork = 0;
144 int uninterestedNetwork = 0;
145 private boolean isSendResponseSetting = false;
146 private boolean isSendRequestToAllSetting = false;
149 protected void onCreate(Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 setContentView(R.layout.activity_main);
153 RM.setResponseListener(this);
155 mLogHandler = new Handler();
156 TextView logView = (TextView) findViewById(R.id.tv_result);
157 DLog.setTextView(mLogHandler, logView);
161 mReceiveLayout = (RelativeLayout) findViewById(R.id.layout_receive);
164 mSendRequestLayout = (RelativeLayout) findViewById(R.id.layout_request);
165 mSendRequestToAllLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all);
166 mSendRequestSettingLayout = (RelativeLayout)
167 findViewById(R.id.layout_request_setting_for_client);
168 mSendRequestToAllSettingLayout = (RelativeLayout)
169 findViewById(R.id.layout_request_to_all_setting_for_client);
170 mRequestTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_title);
171 mRequestToAllTitleLayout = (RelativeLayout) findViewById(R.id.layout_request_to_all_title);
172 mHandleTitleLayout = (RelativeLayout) findViewById(R.id.layout_handle_title);
175 mSendNotificationLayout = (RelativeLayout) findViewById(R.id.layout_notify);
176 mSendResponseNotiSettingLayout = (RelativeLayout)
177 findViewById(R.id.layout_request_setting_for_server);
178 mResponseNotificationTitleLayout = (RelativeLayout)
179 findViewById(R.id.layout_Response_Noti_title);
181 mMode_tv = (TextView) findViewById(R.id.tv_mode);
182 mNetwork_tv = (TextView) findViewById(R.id.tv_network);
184 mNotification_ed = (EditText) findViewById(R.id.et_notification);
185 mReqData_ed = (EditText) findViewById(R.id.et_req_data);
186 mReqToAllData_ed = (EditText) findViewById(R.id.et_req_to_all_data);
188 mResponse_btn = (Button) findViewById(R.id.btn_sendresponse);
189 mNotify_btn = (Button) findViewById(R.id.btn_notify);
190 mReqeust_btn = (Button) findViewById(R.id.btn_Request);
191 mReqeust_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_client);
192 mReqeustToAll_btn = (Button) findViewById(R.id.btn_request_to_all);
193 mReqeustToAll_setting_btn = (Button)
194 findViewById(R.id.btn_request_to_all_setting_for_client);
195 mResponse_Notify_setting_btn = (Button) findViewById(R.id.btn_Request_setting_for_server);
196 mGetNetworkInfo_btn = (Button) findViewById(R.id.btn_get_network_info);
197 mRecv_btn = (Button) findViewById(R.id.btn_receive);
199 mResponse_btn.setOnClickListener(mSendResponseHandler);
200 mNotify_btn.setOnClickListener(mNotifyHandler);
201 mReqeust_btn.setOnClickListener(mSendRequestHandler);
202 mReqeust_setting_btn.setOnClickListener(mSendRequestSettingHandler);
203 mReqeustToAll_btn.setOnClickListener(mSendRequestToAllHandler);
204 mReqeustToAll_setting_btn.setOnClickListener(mSendRequestToAllSettingHandler);
205 mResponse_Notify_setting_btn
206 .setOnClickListener(mSendResponseNotiSettingHandler);
207 mRecv_btn.setOnClickListener(mResponseHandler);
208 mGetNetworkInfo_btn.setOnClickListener(mGetNetworkInfoHandler);
210 showSelectModeView();
212 // Initialize Connectivity Abstraction
213 RM.RMInitialize(getApplicationContext());
216 RM.RMRegisterHandler();
219 private void showSelectModeView() {
221 mSendNotificationLayout.setVisibility(View.INVISIBLE);
222 mSendRequestLayout.setVisibility(View.INVISIBLE);
223 mSendRequestToAllLayout.setVisibility(View.INVISIBLE);
224 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
225 mSendRequestToAllSettingLayout.setVisibility(View.INVISIBLE);
226 mReceiveLayout.setVisibility(View.INVISIBLE);
227 mRequestTitleLayout.setVisibility(View.INVISIBLE);
228 mRequestToAllTitleLayout.setVisibility(View.INVISIBLE);
229 mHandleTitleLayout.setVisibility(View.INVISIBLE);
230 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
231 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
233 mMode_tv.setText("Select Mode (Server or Client)");
234 Log.i(TAG, "Select Mode (Server or Client)");
237 private void showNetworkView() {
239 mNetwork_tv.setText("Select Network Type");
240 Log.i(TAG, "Select Network Type");
243 private void showModeView() {
245 if (mCurrentMode == Mode.SERVER) {
247 mSendNotificationLayout.setVisibility(View.VISIBLE);
248 mSendRequestLayout.setVisibility(View.INVISIBLE);
249 mSendRequestToAllLayout.setVisibility(View.VISIBLE);
250 mSendRequestSettingLayout.setVisibility(View.INVISIBLE);
251 mSendRequestToAllSettingLayout.setVisibility(View.VISIBLE);
252 mReceiveLayout.setVisibility(View.VISIBLE);
254 mRequestTitleLayout.setVisibility(View.INVISIBLE);
255 mRequestToAllTitleLayout.setVisibility(View.VISIBLE);
256 mHandleTitleLayout.setVisibility(View.VISIBLE);
258 mResponseNotificationTitleLayout.setVisibility(View.VISIBLE);
259 mSendResponseNotiSettingLayout.setVisibility(View.VISIBLE);
261 mNetwork_tv.setText("");
263 } else if (mCurrentMode == Mode.CLIENT) {
265 mSendNotificationLayout.setVisibility(View.INVISIBLE);
266 mSendRequestLayout.setVisibility(View.VISIBLE);
267 mSendRequestToAllLayout.setVisibility(View.VISIBLE);
268 mSendRequestSettingLayout.setVisibility(View.VISIBLE);
269 mSendRequestToAllSettingLayout.setVisibility(View.VISIBLE);
270 mReceiveLayout.setVisibility(View.VISIBLE);
272 mRequestTitleLayout.setVisibility(View.VISIBLE);
273 mRequestToAllTitleLayout.setVisibility(View.VISIBLE);
274 mHandleTitleLayout.setVisibility(View.VISIBLE);
276 mResponseNotificationTitleLayout.setVisibility(View.INVISIBLE);
277 mSendResponseNotiSettingLayout.setVisibility(View.INVISIBLE);
279 mNetwork_tv.setText("");
284 protected void onDestroy() {
287 // Terminate Connectivity Abstraction
289 android.os.Process.killProcess(android.os.Process.myPid());
293 public boolean onCreateOptionsMenu(Menu menu) {
295 menu.add(0, 1, Menu.NONE, "Start Server");
296 menu.add(0, 2, Menu.NONE, "Start Client");
297 menu.add(0, 3, Menu.NONE, "Select Network");
303 public boolean onOptionsItemSelected(MenuItem item) {
305 switch (item.getItemId()) {
309 RM.RMStartListeningServer();
311 if (interestedNetwork == 0) {
312 mCurrentMode = Mode.SERVER;
313 mMode_tv.setText("MODE: " + mCurrentMode.toString());
314 Log.i(TAG, "MODE: " + mCurrentMode.toString());
318 mCurrentMode = Mode.SERVER;
319 mMode_tv.setText("MODE: " + mCurrentMode.toString());
320 Log.i(TAG, "MODE: " + mCurrentMode.toString());
328 RM.RMStartDiscoveryServer();
330 if (interestedNetwork == 0) {
331 mCurrentMode = Mode.CLIENT;
332 mMode_tv.setText("MODE: " + mCurrentMode.toString());
333 Log.i(TAG, "MODE: " + mCurrentMode.toString());
337 mCurrentMode = Mode.CLIENT;
338 mMode_tv.setText("MODE: " + mCurrentMode.toString());
339 Log.i(TAG, "MODE: " + mCurrentMode.toString());
347 checkInterestedNetwork("Select Network");
352 return super.onOptionsItemSelected(item);
355 private OnClickListener mSendResponseHandler = new OnClickListener() {
358 public void onClick(View v) {
360 DLog.v(TAG, "SendResponse click");
361 if ( selectedNetwork != -1) {
362 RM.RMSendResponse(selectedNetwork, isSecured, msgType, responseValue);
365 DLog.v(TAG, "Please Select Network Type");
370 private OnClickListener mNotifyHandler = new OnClickListener() {
373 public void onClick(View v) {
375 DLog.v(TAG, "SendNotification click");
376 if ( selectedNetwork != -1) {
377 RM.RMSendNotification(mNotification_ed.getText().toString(),
378 null, selectedNetwork, isSecured, msgType, responseValue);
381 DLog.v(TAG, "Please Select Network Type");
386 private OnClickListener mSendRequestHandler = new OnClickListener() {
389 public void onClick(View v) {
391 DLog.v(TAG, "SendRequest click");
392 if ( selectedNetwork != -1) {
393 RM.RMSendRequest(mReqData_ed.getText().toString(), null,
394 selectedNetwork, isSecured, msgType);
397 DLog.v(TAG, "Please Select Network Type");
402 private OnClickListener mSendRequestSettingHandler = new OnClickListener() {
405 public void onClick(View v) {
406 checkSendNetworkType("Select Send Network Type");
410 private OnClickListener mSendRequestToAllHandler = new OnClickListener() {
413 public void onClick(View v) {
415 DLog.v(TAG, "SendRequestToAll click");
416 if ( selectedNetwork != -1) {
417 RM.RMSendReqestToAll(mReqToAllData_ed.getText().toString(), selectedNetwork);
420 DLog.v(TAG, "Please Select Network Type");
425 private OnClickListener mSendRequestToAllSettingHandler = new OnClickListener() {
428 public void onClick(View v) {
429 isSendRequestToAllSetting = true;
430 checkSendNetworkType("Select Send Network Type");
434 private OnClickListener mSendResponseNotiSettingHandler = new OnClickListener() {
437 public void onClick(View v) {
438 isSendResponseSetting = true;
439 checkSendNetworkType("Select Send Network Type");
443 private OnClickListener mGetNetworkInfoHandler = new OnClickListener() {
445 public void onClick(View v) {
447 RM.RMGetNetworkInfomation();
451 private OnClickListener mResponseHandler = new OnClickListener() {
454 public void onClick(View v) {
456 RM.RMHandleRequestResponse();
460 private void checkInterestedNetwork(String title) {
462 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
463 builder.setTitle(title)
464 .setMultiChoiceItems(mNetworkCheckBoxItems, mCheckedItems,
465 new DialogInterface.OnMultiChoiceClickListener() {
468 public void onClick(DialogInterface dialog,
469 int which, boolean isChecked) {
473 mSelectedItems[which] = 1;
474 mUnSelectedItems[which] = 0;
476 } else if (mSelectedItems[which] == 1) {
478 mSelectedItems[which] = 0;
479 mUnSelectedItems[which] = 1;
483 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
486 public void onClick(DialogInterface dialog, int which) {
488 interestedNetwork = 0;
490 for (int i = 0; i < mSelectedItems.length; i++) {
491 if (mSelectedItems[i] == 1) {
492 interestedNetwork |= (1 << i);
495 if(0 != interestedNetwork)
496 RM.RMSelectNetwork(interestedNetwork);
498 uninterestedNetwork = 0;
500 for (int i = 0; i < mUnSelectedItems.length; i++) {
501 if (mUnSelectedItems[i] == 1) {
502 uninterestedNetwork |= (1 << i);
505 if(0 != uninterestedNetwork)
506 RM.RMUnSelectNetwork(uninterestedNetwork);
512 private void checkMsgSecured(String title) {
514 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
516 builder.setTitle(title)
517 .setSingleChoiceItems(mDTLSCheckBoxItems, selectedMsgSecured,
518 new DialogInterface.OnClickListener() {
521 public void onClick(DialogInterface dialog,
523 selectedMsgSecured = which;
526 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
529 public void onClick(DialogInterface dialog, int which) {
531 if (selectedMsgSecured == DTLS.SECURED.ordinal()) {
533 DLog.v(TAG, "Send secured message");
535 } else if (selectedMsgSecured == DTLS.UNSECURED.ordinal()) {
537 DLog.v(TAG, "Send unsecured message");
539 checkMsgType("Select Msg Type");
545 private void checkMsgType(String title) {
547 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
548 builder.setTitle(title)
549 .setSingleChoiceItems(mMsgTyleCheckBoxItems, selectedMsgType,
550 new DialogInterface.OnClickListener() {
553 public void onClick(DialogInterface dialog,
555 selectedMsgType = which;
558 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
561 public void onClick(DialogInterface dialog, int which) {
563 if (selectedMsgType == MsgType.CON.ordinal()) {
565 DLog.v(TAG, "Message Type is CON");
567 } else if (selectedMsgType == MsgType.NON.ordinal()) {
569 DLog.v(TAG, "Message Type is NON");
570 } else if (selectedMsgType == MsgType.ACK.ordinal()) {
572 DLog.v(TAG, "Message Type is ACK");
573 } else if (selectedMsgType == MsgType.RESET.ordinal()) {
575 DLog.v(TAG, "Message Type is RESET");
578 if (isSendResponseSetting == true && msgType != 3) {
579 checkResponseResult("Select Value of Response Result");
580 isSendResponseSetting = false;
586 private void checkResponseResult(String title) {
588 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
589 builder.setTitle(title)
590 .setSingleChoiceItems(mResponseResultCheckBoxItems, selectedResponseValue,
591 new DialogInterface.OnClickListener() {
594 public void onClick(DialogInterface dialog,
596 selectedResponseValue = which;
599 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
602 public void onClick(DialogInterface dialog, int which) {
604 if (selectedResponseValue == ResponseResult.CA_SUCCESS.ordinal()) {
606 DLog.v(TAG, "Response Value is CA_SUCCESS");
607 } else if (selectedResponseValue == ResponseResult.CA_CREATED
610 DLog.v(TAG, "Response Value is CA_CREATED");
611 } else if (selectedResponseValue == ResponseResult.CA_DELETED
614 DLog.v(TAG, "Response Value is CA_DELETED");
615 } else if (selectedResponseValue == ResponseResult.CA_VALID
618 DLog.v(TAG, "Response Value is CA_VALID");
619 } else if (selectedResponseValue == ResponseResult.CA_CHANGED
622 DLog.v(TAG, "Response Value is CA_CHANGED");
623 } else if (selectedResponseValue == ResponseResult.CA_CONTENT
626 DLog.v(TAG, "Response Value is CA_CONTENT");
627 } else if (selectedResponseValue == ResponseResult.CA_EMPTY
630 DLog.v(TAG, "Response Value is CA_EMPTY");
631 } else if (selectedResponseValue == ResponseResult.CA_BAD_REQ
634 DLog.v(TAG, "Response Value is CA_BAD_REQ");
635 } else if (selectedResponseValue == ResponseResult.CA_BAD_OPT
638 DLog.v(TAG, "Response Value is CA_BAD_OPT");
639 } else if (selectedResponseValue == ResponseResult.CA_NOT_FOUND
642 DLog.v(TAG, "Response Value is CA_NOT_FOUND");
643 } else if (selectedResponseValue ==
644 ResponseResult.CA_INTERNAL_SERVER_ERROR
647 DLog.v(TAG, "Response Value is CA_INTERNAL_SERVER_ERROR");
648 } else if (selectedResponseValue == ResponseResult.CA_RETRANSMIT_TIMEOUT
651 DLog.v(TAG, "Response Value is CA_RETRANSMIT_TIMEOUT");
657 private void checkSendNetworkType(String title) {
658 selectedNetworkType = -1;
659 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
661 builder.setTitle(title)
662 .setSingleChoiceItems(mNetworkCheckBoxItems, -1,
663 new DialogInterface.OnClickListener() {
666 public void onClick(DialogInterface dialog,
668 selectedNetworkType = which;
671 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
674 public void onClick(DialogInterface dialog, int which) {
676 if (selectedNetworkType == Network.IP.ordinal()) {
677 selectedNetwork = CA_IP;
678 DLog.v(TAG, "Selected Network is CA_IP");
679 } else if (selectedNetworkType == Network.LE.ordinal()) {
680 selectedNetwork = CA_LE;
681 DLog.v(TAG, "Selected Network is LE");
682 } else if (selectedNetworkType == Network.EDR.ordinal()) {
683 selectedNetwork = CA_EDR;
684 DLog.v(TAG, "Selected Network is EDR");
686 DLog.v(TAG, "Selected Network is NULL");
687 selectedNetwork = -1;
690 if (isSendRequestToAllSetting != true) {
691 checkMsgSecured("Select DTLS Type");
693 isSendRequestToAllSetting = false;
698 public void OnResponseReceived(String subject, String receivedData) {
699 String callbackData = subject + receivedData;
700 DLog.v(TAG, callbackData);
702 if (subject.equals(getString(R.string.remote_address))) {
703 StringBuilder sb = new StringBuilder();
704 sb.append(getString(R.string.coap_prefix)).append(receivedData);
705 mReqData_ed.setText(sb.toString());
706 mNotification_ed.setText(sb.toString());
707 } else if (subject.equals(getString(R.string.remote_port))) {
708 StringBuilder sb = new StringBuilder();
709 String uri = mReqData_ed.getText().toString();
710 if (null != receivedData && uri.contains("."))
712 sb.append(uri).append(":").append(receivedData);
714 sb.append(getString(R.string.uri));
715 mReqData_ed.setText(sb.toString());
716 mNotification_ed.setText(sb.toString());