Remove resource directory dependency in lib OC
[platform/upstream/iotivity.git] / android / examples / simplebase / src / main / java / org / iotivity / base / examples / CloudFragment.java
1 /*
2  * ******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.base.examples;
24
25 import android.app.Activity;
26 import android.app.AlertDialog;
27 import android.app.Fragment;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.os.Bundle;
32 import android.util.Log;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.Button;
37 import android.widget.CheckBox;
38 import android.widget.CompoundButton;
39 import android.widget.EditText;
40 import android.widget.LinearLayout;
41 import android.widget.ScrollView;
42 import android.widget.Switch;
43 import android.widget.TextView;
44 import android.widget.Toast;
45
46 import org.iotivity.base.EntityHandlerResult;
47 import org.iotivity.base.ErrorCode;
48 import org.iotivity.base.ModeType;
49 import org.iotivity.base.OcAccountManager;
50 import org.iotivity.base.OcConnectivityType;
51 import org.iotivity.base.OcException;
52 import org.iotivity.base.OcHeaderOption;
53 import org.iotivity.base.OcPlatform;
54 import org.iotivity.base.OcPresenceHandle;
55 import org.iotivity.base.OcRepresentation;
56 import org.iotivity.base.OcResource;
57 import org.iotivity.base.OcResourceHandle;
58 import org.iotivity.base.OcRDClient;
59 import org.iotivity.base.PlatformConfig;
60 import org.iotivity.base.QualityOfService;
61 import org.iotivity.base.ResourceProperty;
62 import org.iotivity.base.ServiceType;
63
64 import java.util.ArrayList;
65 import java.util.EnumSet;
66 import java.util.HashMap;
67 import java.util.LinkedList;
68 import java.util.List;
69 import java.util.Map;
70 import java.util.Objects;
71 import java.util.regex.Pattern;
72
73 /**
74  * This class is for messaging between the cloud server and the client.
75  * It can handle cloud API manually.
76  */
77 public class CloudFragment extends Fragment implements
78         View.OnClickListener, CompoundButton.OnCheckedChangeListener,
79         OcResource.OnObserveListener,
80         OcResource.OnMQTopicFoundListener, OcResource.OnMQTopicCreatedListener,
81         OcResource.OnMQTopicSubscribeListener {
82
83     private static final String TAG = "OIC_SIMPLE_CLOUD";
84     private final String EOL = System.getProperties().getProperty("line.separator");
85     private final Pattern ADDRESS_PORT
86             = Pattern.compile(
87             "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])" +
88                     "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
89                     "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
90                     "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])" +
91                     ":([0-9]{1,5}))");
92
93     private Activity mActivity;
94     private Context mContext;
95
96     private QualityOfService mQos = QualityOfService.LOW;
97     private boolean mSecured = false;
98
99     private LinearLayout mAccountLayout;
100     private LinearLayout mRDLayout;
101     private LinearLayout mMQLayout;
102
103     private TextView mAccountText;
104     private TextView mRDText;
105     private TextView mMQText;
106     private ScrollView mScrollView;
107     private TextView mActionLog;
108     private TextView mResultLog;
109
110     private OcAccountManager mAccountManager;
111     private String mAccessToken;
112     private String mRefreshToken;
113     private String mUserUuid;
114     private String mGroupId;
115     private String mGroupMasterId;
116     private String mInviterUserId;
117     private String mInviteeUuid;
118     private final int REQUEST_LOGIN = 1;
119
120     private OcResourceHandle localLightResourceHandle = null;
121     private List<OcResourceHandle> mResourceHandleList = new LinkedList<>();
122     private OcPresenceHandle mOcPresenceHandle = null;
123     private Button mDevicePresenceButton;
124
125     private OcResource MQbrokerResource = null;
126     private OcResource currentTopicResource = null;
127     private boolean switchingFlag = true;
128     private int roomNum = 1;
129     private String defaultTopicFullName = Common.MQ_DEFAULT_TOPIC_URI;
130
131     // variables related observer
132     private int maxSequenceNumber = 0xFFFFFF;
133
134     @Override
135     public void onCreate(Bundle savedInstanceState) {
136         super.onCreate(savedInstanceState);
137
138         mActivity = getActivity();
139         mContext = mActivity.getBaseContext();
140         initOcPlatform(ModeType.CLIENT_SERVER);
141     }
142
143     private void initOcPlatform(ModeType type) {
144         PlatformConfig cfg = new PlatformConfig(mActivity, mContext,
145                 ServiceType.IN_PROC,
146                 type,
147                 Common.IP_ADDRESS,
148                 Common.IP_PORT,
149                 mQos);
150         OcPlatform.Configure(cfg);
151     }
152
153     private void signUp() {
154         try {
155             mAccountManager = OcPlatform.constructAccountManagerObject(
156                     Common.HOST,
157                     EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));
158         } catch (OcException e) {
159             e.printStackTrace();
160         }
161
162         Intent intentLogin = new Intent(mContext, LoginActivity.class);
163         startActivityForResult(intentLogin, REQUEST_LOGIN);
164     }
165
166     private void signIn() {
167         try {
168             msg("signIn");
169             mAccountManager.signIn(mUserUuid, mAccessToken, onSignIn);
170         } catch (OcException e) {
171             e.printStackTrace();
172         }
173     }
174
175     private void signOut() {
176         try {
177             msg("signOut");
178             mAccountManager.signOut(mAccessToken, onSignOut);
179         } catch (OcException e) {
180             e.printStackTrace();
181         }
182     }
183
184     private void createGroup() {
185         try {
186             msg("createGroup");
187             mAccountManager.createGroup(onCreateGroup);
188         } catch (OcException e) {
189             e.printStackTrace();
190         }
191     }
192
193     private void deleteGroup() {
194         if (mGroupId == null) {
195             msg("there is no any group");
196         } else {
197             try {
198                 msg("deleteGroup");
199                 mAccountManager.deleteGroup(mGroupId, onDeleteGroup);
200             } catch (OcException e) {
201                 e.printStackTrace();
202             }
203         }
204     }
205
206     private void inviteUser() {
207         if (mGroupId == null) {
208             msg("there is no any group");
209         } else {
210             showInviteUser();
211         }
212     }
213
214     private void addPropertyValueToGroup() {
215         if (mGroupId == null) {
216             msg("there is no any group");
217         } else {
218             showPostPropertyValueToGroup(0);
219         }
220     }
221
222     private void deletePropertyValueFromGroup() {
223         if (mGroupId == null) {
224             msg("there is no any group");
225         } else {
226             showPostPropertyValueToGroup(1);
227         }
228     }
229
230     private void updatePropertyValueOnGroup() {
231         if (mGroupId == null) {
232             msg("there is no any group");
233         } else {
234             showPostPropertyValueToGroup(2);
235         }
236     }
237     // FOR WEB-VIEW
238     @Override
239     public void onActivityResult(int requestCode, int resultCode, Intent data) {
240         super.onActivityResult(requestCode, resultCode, data);
241         if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOGIN) {
242             String authCode = data.getStringExtra("authCode");
243             msg("\tauthCode: " + authCode);
244
245             try {
246                 msg("Sign Up");
247                 mAccountManager.signUp("github", authCode, onSignUp);
248             } catch (OcException e) {
249                 e.printStackTrace();
250             }
251         }
252     }
253
254     OcAccountManager.OnPostListener onSignUp = new OcAccountManager.OnPostListener() {
255         @Override
256         public synchronized void onPostCompleted(List<OcHeaderOption> list,
257                                                  OcRepresentation ocRepresentation) {
258             msg("signUp was successful");
259             try {
260                 mUserUuid = ocRepresentation.getValue("uid");
261                 mAccessToken = ocRepresentation.getValue("accesstoken");
262                 mRefreshToken = ocRepresentation.getValue("refreshtoken");
263                 String tokenType = ocRepresentation.getValue("tokentype");
264                 msg("\tuserID: " + mUserUuid);
265                 msg("\taccessToken: " + mAccessToken);
266                 msg("\trefreshToken: " + mRefreshToken);
267                 msg("\ttokenType: " + tokenType);
268
269                 if (ocRepresentation.hasAttribute("expiresin")) {
270                     int expiresIn = ocRepresentation.getValue("expiresin");
271                     msg("\texpiresIn: " + expiresIn);
272                 }
273             } catch (OcException e) {
274                 Log.e(TAG, e.toString());
275             }
276         }
277
278         @Override
279         public synchronized void onPostFailed(Throwable throwable) {
280             msg("Failed to signUp");
281             if (throwable instanceof OcException) {
282                 OcException ocEx = (OcException) throwable;
283                 Log.e(TAG, ocEx.toString());
284                 ErrorCode errCode = ocEx.getErrorCode();
285                 msg("Error code: " + errCode);
286             }
287         }
288     };
289
290     OcAccountManager.OnPostListener onSignIn = new OcAccountManager.OnPostListener() {
291         @Override
292         public synchronized void onPostCompleted(List<OcHeaderOption> list,
293                                                  OcRepresentation ocRepresentation) {
294             msg("signIn was successful");
295             try {
296                 msg("observeInvitation");
297                 mAccountManager.observeInvitation(onObserveInvitation);
298                 msg("observeGroup");
299                 mAccountManager.observeGroup(onObserveGroup);
300                 msg("getGroupList");
301                 mAccountManager.getGroupInfoAll(onGetGroupInfoAll);
302             } catch (OcException e) {
303                 Log.e(TAG, e.toString());
304             }
305         }
306
307         @Override
308         public synchronized void onPostFailed(Throwable throwable) {
309             msg("Failed to signIn");
310             if (throwable instanceof OcException) {
311                 OcException ocEx = (OcException) throwable;
312                 Log.e(TAG, ocEx.toString());
313                 ErrorCode errCode = ocEx.getErrorCode();
314                 msg("Error code: " + errCode);
315             }
316         }
317     };
318
319     OcAccountManager.OnPostListener onSignOut = new OcAccountManager.OnPostListener() {
320         @Override
321         public synchronized void onPostCompleted(List<OcHeaderOption> list,
322                                                  OcRepresentation ocRepresentation) {
323             msg("signOut was successful");
324         }
325
326         @Override
327         public synchronized void onPostFailed(Throwable throwable) {
328             msg("Failed to signOut");
329             if (throwable instanceof OcException) {
330                 OcException ocEx = (OcException) throwable;
331                 Log.e(TAG, ocEx.toString());
332                 ErrorCode errCode = ocEx.getErrorCode();
333                 msg("Error code: " + errCode);
334             }
335         }
336     };
337
338     OcAccountManager.OnGetListener onGetGroupInfoAll = new OcAccountManager.OnGetListener() {
339         @Override
340         public void onGetCompleted(List<OcHeaderOption> list, OcRepresentation ocRepresentation) {
341             msg("getGroupInfoAll was successful");
342             try {
343
344                 OcRepresentation[] gidlist = ocRepresentation.getValue("groups");
345                 if (gidlist == null || gidlist.length == 0) {
346                     msg("\tgroup list is empty");
347                     mGroupId = null;
348                 } else {
349                     msg("\tgroup list");
350
351                     for (OcRepresentation group : gidlist) {
352                         String gid = group.getValue("gid");
353                         String gname = group.getValue("gname");
354                         String owner = group.getValue("owner");
355
356                         msg("\t\t-GroupID : " + gid);
357                         msg("\t\t Group name : " + gname);
358                         msg("\t\t Owner : " + owner);
359
360                         String[] members = group.getValue("members");
361                         if (members != null && members.length != 0) {
362                             msg("\t\t members :");
363                             for (String member : members) {
364                                 msg("\t\t\t" + member);
365                             }
366                         }
367
368                         String[] devices = group.getValue("devices");
369                         if (devices != null && devices.length != 0) {
370                             msg("\t\t devices");
371                             for (String device : devices) {
372                                 msg("\t\t\t" + device);
373                             }
374                         }
375
376                         if (group.hasAttribute("parent")) {
377                             msg("\t\t parent group : " + group.getValue("parent"));
378                         }
379
380                         mGroupId = gid;
381                     }
382                     msg("\tcurrent group is " + mGroupId);
383                 }
384             } catch (OcException e) {
385                 e.printStackTrace();
386             }
387         }
388
389         @Override
390         public void onGetFailed(Throwable throwable) {
391             msg("Failed to getGroupInfoAll");
392             if (throwable instanceof OcException) {
393                 OcException ocEx = (OcException) throwable;
394                 Log.e(TAG, ocEx.toString());
395                 ErrorCode errCode = ocEx.getErrorCode();
396                 msg("Error code: " + errCode);
397             }
398         }
399     };
400
401     OcAccountManager.OnGetListener onGetGroupInfo = new OcAccountManager.OnGetListener() {
402         @Override
403         public void onGetCompleted(List<OcHeaderOption> list, OcRepresentation ocRepresentation) {
404             msg("getGroupInfo was successful");
405
406             Map<String, Object> valueMap = ocRepresentation.getValues();
407
408             for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
409                 msg("\tproperty: " + entry.getKey() + " value: " + entry.getValue());
410             }
411         }
412
413         @Override
414         public void onGetFailed(Throwable throwable) {
415             msg("Failed to getGroupInfo");
416             if (throwable instanceof OcException) {
417                 OcException ocEx = (OcException) throwable;
418                 Log.e(TAG, ocEx.toString());
419                 ErrorCode errCode = ocEx.getErrorCode();
420                 msg("Error code: " + errCode);
421             }
422         }
423     };
424
425     OcAccountManager.OnObserveListener onObserveInvitation =
426             new OcAccountManager.OnObserveListener() {
427                 @Override
428                 public void onObserveCompleted(List<OcHeaderOption> list,
429                                                OcRepresentation ocRepresentation, int i) {
430                     msg("observeInvitation was successful");
431                     try {
432                         if (REGISTER == i) {
433                             msg("REGISTER was successful");
434
435                             OcRepresentation[] sendInvitationList =
436                                     ocRepresentation.getValue("invite");
437                             if (sendInvitationList != null && sendInvitationList.length != 0) {
438                                 msg("\tList of invitation that I sent");
439                                 for (OcRepresentation invitation : sendInvitationList) {
440                                     String gid = invitation.getValue("gid");
441                                     String mid = invitation.getValue("mid");
442                                     msg("\t\t-GroupID : " + gid);
443                                     msg("\t\t InviteeID : " + mid);
444                                 }
445                             }
446
447                             OcRepresentation[] receiveInvitationList =
448                                     ocRepresentation.getValue("invited");
449                             if (receiveInvitationList != null && receiveInvitationList.length != 0) {
450                                 msg("\tList of invitation that I received");
451                                 for (OcRepresentation invitation : receiveInvitationList) {
452                                     String gid = invitation.getValue("gid");
453                                     String mid = invitation.getValue("mid");
454                                     msg("\t\t-GroupID : " + gid);
455                                     msg("\t\t InviterID : " + mid);
456                                 }
457                             }
458                         } else if (DEREGISTER == i) {
459                             msg("DEREGISTER was successful");
460                         } else {
461                             OcRepresentation[] sendInvitationList =
462                                     ocRepresentation.getValue("invite");
463                             if (sendInvitationList != null && sendInvitationList.length != 0) {
464                                 msg("\tList of invitation that I sent");
465                                 for (OcRepresentation invitation : sendInvitationList) {
466                                     String gid = invitation.getValue("gid");
467                                     String mid = invitation.getValue("mid");
468                                     msg("\t\t-GroupID : " + gid);
469                                     msg("\t\t InviteeID : " + mid);
470                                 }
471                             }
472
473                             OcRepresentation[] receivInvitationList =
474                                     ocRepresentation.getValue("invited");
475                             if (receivInvitationList != null && receivInvitationList.length != 0) {
476                                 msg("\tList of invitation that I received");
477                                 for (OcRepresentation invitation : receivInvitationList) {
478                                     mGroupId = invitation.getValue("gid");
479                                     mGroupMasterId = invitation.getValue("mid");
480                                     msg("\t\t-GroupID : " + mGroupId);
481                                     msg("\t\t InviterID : " + mGroupMasterId);
482                                 }
483                                 msg("searchUser");
484                                 mAccountManager.searchUser(mGroupMasterId, onSearchUserForInvitee);
485                             }
486                         }
487                     } catch (OcException e) {
488                         e.printStackTrace();
489                     }
490                 }
491
492                 @Override
493                 public void onObserveFailed(Throwable throwable) {
494                     msg("Failed to observeInvitation");
495                     if (throwable instanceof OcException) {
496                         OcException ocEx = (OcException) throwable;
497                         Log.e(TAG, ocEx.toString());
498                         ErrorCode errCode = ocEx.getErrorCode();
499                         msg("Error code: " + errCode);
500                     }
501                 }
502             };
503
504     OcAccountManager.OnGetListener onSearchUserForInvitee = new OcAccountManager.OnGetListener() {
505         @Override
506         public synchronized void onGetCompleted(List<OcHeaderOption> list,
507                                                 OcRepresentation ocRepresentation) {
508             msg("searchUser was successful");
509             try {
510                 OcRepresentation[] userList = ocRepresentation.getValue("ulist");
511                 for (OcRepresentation user : userList) {
512                     String inviterUuid = user.getValue("uid");
513                     Log.d(TAG, "inviterUuid : " + inviterUuid);
514
515                     OcRepresentation userInfo = user.getValue("uinfo");
516                     mInviterUserId = userInfo.getValue("userid");
517                 }
518
519                 mActivity.runOnUiThread(new Runnable() {
520                     public void run() {
521                         showInviteMsg(mInviterUserId);
522                     }
523                 });
524
525             } catch (OcException e) {
526                 e.printStackTrace();
527             }
528         }
529
530         @Override
531         public synchronized void onGetFailed(Throwable throwable) {
532             msg("Failed to searchUser");
533             if (throwable instanceof OcException) {
534                 OcException ocEx = (OcException) throwable;
535                 Log.e(TAG, ocEx.toString());
536                 ErrorCode errCode = ocEx.getErrorCode();
537                 msg("Error code: " + errCode);
538             }
539         }
540     };
541
542     OcAccountManager.OnDeleteListener onDeleteGroup = new OcAccountManager.OnDeleteListener() {
543         @Override
544         public void onDeleteCompleted(List<OcHeaderOption> list) {
545             msg("deleteGroup was successful");
546         }
547
548         @Override
549         public void onDeleteFailed(Throwable throwable) {
550             msg("Failed to deleteGroup");
551         }
552     };
553
554     OcAccountManager.OnDeleteListener onReplyToInvitation = new OcAccountManager.OnDeleteListener() {
555         @Override
556         public void onDeleteCompleted(List<OcHeaderOption> list) {
557             msg("replyToInvitation was successful");
558             try {
559                 mAccountManager.getGroupInfoAll(onGetGroupInfoAll);
560             } catch (OcException e) {
561                 e.printStackTrace();
562             }
563         }
564
565         @Override
566         public void onDeleteFailed(Throwable throwable) {
567             msg("Failed to deleteInvitation");
568         }
569     };
570
571     OcAccountManager.OnGetListener onSearchUserForInviter = new OcAccountManager.OnGetListener() {
572         @Override
573         public synchronized void onGetCompleted(List<OcHeaderOption> list,
574                                                 OcRepresentation ocRepresentation) {
575             msg("searchUser was successful");
576             try {
577                 OcRepresentation[] userList = ocRepresentation.getValue("ulist");
578                 for (OcRepresentation user : userList) {
579                     mInviteeUuid = user.getValue("uid");
580                     OcRepresentation userInfo = user.getValue("uinfo");
581                     String inviteeUserId = userInfo.getValue("userid");
582                     Log.d(TAG, "inviteeUserId : " + inviteeUserId);
583                 }
584                 msg("sendInvitation");
585                 mAccountManager.sendInvitation(mGroupId, mInviteeUuid, onSendInvitation);
586             } catch (OcException e) {
587                 e.printStackTrace();
588             }
589         }
590
591         @Override
592         public synchronized void onGetFailed(Throwable throwable) {
593             msg("Failed to searchUser");
594             if (throwable instanceof OcException) {
595                 OcException ocEx = (OcException) throwable;
596                 Log.e(TAG, ocEx.toString());
597                 ErrorCode errCode = ocEx.getErrorCode();
598                 msg("Error code: " + errCode);
599             }
600         }
601     };
602
603     OcAccountManager.OnPostListener onCreateGroup = new OcAccountManager.OnPostListener() {
604         @Override
605         public synchronized void onPostCompleted(List<OcHeaderOption> list,
606                                                  OcRepresentation ocRepresentation) {
607             msg("createGroup was successful");
608             try {
609                 mGroupId = ocRepresentation.getValue("gid");
610                 msg("\tgroupId: " + mGroupId);
611
612                 msg("getGroupInfo");
613                 mAccountManager.getGroupInfo(mGroupId, onGetGroupInfo);
614             } catch (OcException e) {
615                 Log.e(TAG, e.toString());
616             }
617         }
618
619         @Override
620         public synchronized void onPostFailed(Throwable throwable) {
621             msg("Failed to createGroup");
622             if (throwable instanceof OcException) {
623                 OcException ocEx = (OcException) throwable;
624                 Log.e(TAG, ocEx.toString());
625                 ErrorCode errCode = ocEx.getErrorCode();
626                 msg("Error code: " + errCode);
627             }
628         }
629     };
630
631     OcAccountManager.OnObserveListener onObserveGroup = new OcAccountManager.OnObserveListener() {
632         @Override
633         public void onObserveCompleted(List<OcHeaderOption> list,
634                                        OcRepresentation ocRepresentation, int i) {
635             msg("observeGroup was successful");
636             try {
637                 if (REGISTER == i) {
638                     msg("REGISTER was successful");
639                 } else if (DEREGISTER == i) {
640                     msg("DEREGISTER was successful");
641                 } else {
642                     String gid = ocRepresentation.getValue("gid");
643                     msg("\tGroupID: " + gid);
644
645                     String gmid = ocRepresentation.getValue("gmid");
646                     msg("\tGroupMasterID: " + gmid);
647
648                     String[] midlist = ocRepresentation.getValue("midlist");
649                     if (midlist == null || midlist.length == 0) {
650                         msg("\tMember List is empty");
651                     } else {
652                         msg("\tMember List(" + midlist.length + ")");
653                         for (String mid : midlist) {
654                             msg("\t : " + mid);
655                         }
656                     }
657
658                     String[] dilist = ocRepresentation.getValue("dilist");
659                     if (dilist == null || dilist.length == 0) {
660                         msg("\tDevice List is empty");
661                     } else {
662                         msg("\tDevice List(" + dilist.length + ")");
663                         for (String di : dilist) {
664                             msg("\t : " + di);
665                         }
666                     }
667                 }
668             } catch (OcException e) {
669                 e.printStackTrace();
670             }
671         }
672
673         @Override
674         public void onObserveFailed(Throwable throwable) {
675             msg("Failed to observeGroup");
676             if (throwable instanceof OcException) {
677                 OcException ocEx = (OcException) throwable;
678                 Log.e(TAG, ocEx.toString());
679                 ErrorCode errCode = ocEx.getErrorCode();
680                 msg("Error code: " + errCode);
681             }
682         }
683     };
684
685     OcAccountManager.OnPostListener onPostPropertyValue = new OcAccountManager.OnPostListener() {
686         @Override
687         public synchronized void onPostCompleted(List<OcHeaderOption> list,
688                                                  OcRepresentation ocRepresentation) {
689             msg("Post was successful (changed property value)");
690         }
691
692         @Override
693         public synchronized void onPostFailed(Throwable throwable) {
694             msg("Failed to Post (changed property value)");
695             if (throwable instanceof OcException) {
696                 OcException ocEx = (OcException) throwable;
697                 Log.e(TAG, ocEx.toString());
698                 ErrorCode errCode = ocEx.getErrorCode();
699                 msg("Error code: " + errCode);
700             }
701         }
702     };
703
704     OcAccountManager.OnPostListener onSendInvitation = new OcAccountManager.OnPostListener() {
705         @Override
706         public synchronized void onPostCompleted(List<OcHeaderOption> list,
707                                                  OcRepresentation ocRepresentation) {
708             msg("sendInvitation was successful");
709         }
710
711         @Override
712         public synchronized void onPostFailed(Throwable throwable) {
713             msg("Failed to sendInvitation");
714             if (throwable instanceof OcException) {
715                 OcException ocEx = (OcException) throwable;
716                 Log.e(TAG, ocEx.toString());
717                 ErrorCode errCode = ocEx.getErrorCode();
718                 msg("Error code: " + errCode);
719             }
720         }
721     };
722
723     // ******************************************************************************
724     // End of the Account Manager specific code
725     // ******************************************************************************
726
727     OcRDClient.OnPublishResourceListener resourcePublishListener =
728             new OcRDClient.OnPublishResourceListener() {
729                 @Override
730                 public void onPublishResourceCompleted(OcRepresentation ocRepresentation) {
731                     msg("onPublishResourceCompleted");
732
733                     for (OcRepresentation child : ocRepresentation.getChildren()) {
734                         try {
735                             msg("\tPublished Resource URI : " + child.getValue("href"));
736                         } catch (OcException e) {
737                             e.printStackTrace();
738                         }
739                     }
740                 }
741
742                 @Override
743                 public void onPublishResourceFailed(Throwable throwable) {
744                     msg("onPublishResourceFailed has failed");
745                 }
746             };
747
748     OcRDClient.OnDeleteResourceListener resourceDeleteListener =
749             new OcRDClient.OnDeleteResourceListener() {
750                 @Override
751                 public void onDeleteResourceCompleted(int resultCode) {
752                     msg("onDeleteResourceCompleted, result is " + resultCode);
753                 }
754             };
755
756     private void createResource() {
757         if (localLightResourceHandle == null) {
758             try {
759                 localLightResourceHandle = OcPlatform.registerResource(
760                         Common.RESOURCE_URI,            //resource URI
761                         Common.RESOURCE_TYPE,           //resource type name
762                         Common.RESOURCE_INTERFACE,      //using default interface
763                         null,                           //use default entity handler
764                         EnumSet.of(ResourceProperty.DISCOVERABLE)
765                 );
766                 mResourceHandleList.add(localLightResourceHandle);
767                 msg("Create Local Resource is success.");
768             } catch (OcException e) {
769                 Log.e(TAG, e.toString());
770             }
771         }
772     }
773
774     private void publishResourceToRD() {
775         // Create Local Resource.
776         createResource();
777
778         try {
779             // Publish Virtual Resource to Resource-Directory.
780             Log.d(TAG, "Publish Virtual Resource to Resource-Directory.");
781             OcRDClient.publishResourceToRD(
782                     Common.HOST, EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP),
783                     resourcePublishListener
784             );
785
786             // Publish Local Resource to Resource-Directory.
787             Log.d(TAG, "Publish Local Resource to Resource-Directory.");
788             OcRDClient.publishResourceToRD(
789                     Common.HOST, EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP), mResourceHandleList,
790                     resourcePublishListener
791             );
792         } catch (OcException e) {
793             Log.e(TAG, e.toString());
794         }
795     }
796
797     private void deleteResourceFromRD() {
798         try {
799             // Delete Resource from Resource-Directory.
800             Log.d(TAG, "Delete Resource from Resource-Directory.");
801             OcRDClient.deleteResourceFromRD(
802                     Common.HOST, EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP),
803                     resourceDeleteListener
804             );
805         } catch (OcException e) {
806             Log.e(TAG, e.toString());
807         }
808     }
809
810     private void subscribeDevicePresence() {
811         try {
812             if (null == mOcPresenceHandle) {
813                 List<String> di = new ArrayList<>();
814                 mOcPresenceHandle = OcPlatform.subscribeDevicePresence(
815                         Common.HOST, di, EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP), this);
816                 mDevicePresenceButton.setText(R.string.unsub_presence);
817             } else {
818                 OcPlatform.unsubscribePresence(mOcPresenceHandle);
819                 mOcPresenceHandle = null;
820                 mDevicePresenceButton.setText(R.string.sub_presence);
821             }
822         } catch (OcException e) {
823             e.printStackTrace();
824         }
825     }
826
827     @Override
828     public void onObserveCompleted(List<OcHeaderOption> list,
829                                    OcRepresentation ocRepresentation, int sequenceNumber) {
830         if (sequenceNumber != maxSequenceNumber + 1) {
831             msg("OBSERVE Result:");
832             msg("\tSequenceNumber:" + sequenceNumber);
833             try {
834                 if (ocRepresentation.hasAttribute("prslist")) {
835                     OcRepresentation[] prslist = ocRepresentation.getValue("prslist");
836                     if (prslist != null) {
837                         msg("\tDevice Presence");
838                         for (OcRepresentation prs : prslist) {
839                             msg("\t\tDevice ID : " + prs.getValue("di"));
840                             msg("\t\tState : " + prs.getValue("state"));
841                         }
842                     }
843                 }
844             } catch (OcException e) {
845                 e.printStackTrace();
846             }
847         } else {
848             msg("Successful unsubscribePresence");
849         }
850     }
851
852     @Override
853     public void onObserveFailed(Throwable throwable) {
854
855     }
856
857     // ******************************************************************************
858     // End of the Resource Directory specific code
859     // ******************************************************************************
860
861     void getMQBroker() {
862         List<String> resourceTypeList = new ArrayList<>();
863         List<String> resourceInterfaceList = new ArrayList<>();
864         resourceInterfaceList.add(Common.RESOURCE_INTERFACE);
865         resourceTypeList.add("ocf.wk.ps");
866         try {
867             MQbrokerResource = OcPlatform.constructResourceObject(
868                     Common.HOST,
869                     Common.MQ_BROKER_URI,
870                     EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP, OcConnectivityType.CT_IP_USE_V4),
871                     false,
872                     resourceTypeList, resourceInterfaceList);
873
874             msg("found MQ broker : " + MQbrokerResource.getHost());
875
876             discoveryMQTopics();
877
878         } catch (OcException e) {
879             e.printStackTrace();
880         }
881     }
882
883     void discoveryMQTopics() {
884         try {
885             if (null != MQbrokerResource) {
886                 MQbrokerResource.discoveryMQTopics(
887                         new HashMap<String, String>(),
888                         this, QualityOfService.LOW);
889             }
890
891         } catch (OcException e) {
892             e.printStackTrace();
893         }
894     }
895
896     @Override
897     synchronized public void onTopicDiscoveried(OcResource ocResource) {
898         synchronized (this) {
899             String resourceUri = ocResource.getUri();
900
901             msg("onTopicDiscoveried : " + resourceUri + " found");
902         }
903     }
904
905     @Override
906     public void onDiscoveryTopicFailed(Throwable ex, String uri) {
907         Log.e(TAG, "onFindTopicFailed : ", ex);
908
909         if (ex instanceof OcException) {
910             OcException ocEx = (OcException) ex;
911             ErrorCode errCode = ocEx.getErrorCode();
912             Log.d(TAG, "onFindTopicFailed Code: " + errCode);
913             Log.d(TAG, "onFindTopicFailed Code: " + errCode.ordinal());
914             Log.d(TAG, "onFindTopicFailed uri: " + uri);
915
916         } else {
917             Log.e(TAG, ex.getMessage());
918         }
919     }
920
921     void createMQTopic() {
922         try {
923             if (null != MQbrokerResource) {
924                 Map<String, String> queryParameters = new HashMap<>();
925                 queryParameters.put("rt", "light");
926                 MQbrokerResource.createMQTopic(
927                         new OcRepresentation(),
928                         defaultTopicFullName,
929                         queryParameters,
930                         this,
931                         QualityOfService.LOW);
932             }
933         } catch (OcException e) {
934             e.printStackTrace();
935         }
936     }
937
938     @Override
939     synchronized public void onTopicResourceCreated(OcResource ocResource) {
940         synchronized (this) {
941             Log.d(TAG, "onTopicResourceCreated");
942             currentTopicResource = ocResource;
943
944             msg("onTopicResourceCreated : " + currentTopicResource.getUri());
945         }
946     }
947
948     @Override
949     public void onCreateTopicFailed(Throwable ex, String uri) {
950         Log.e(TAG, "onCreateTopicFailed : ", ex);
951
952         if (ex instanceof OcException) {
953             OcException ocEx = (OcException) ex;
954             ErrorCode errCode = ocEx.getErrorCode();
955             Log.d(TAG, "onCreateTopicFailed error Code: " + errCode);
956             Log.d(TAG, "onCreateTopicFailed error Code: " + errCode.ordinal());
957             Log.d(TAG, "onCreateTopicFailed error uri: " + uri);
958
959             // retry to create after increase room number
960             defaultTopicFullName = Common.MQ_DEFAULT_TOPIC_URI + (roomNum++);
961             createMQTopic();
962         } else {
963             Log.e(TAG, ex.getMessage());
964         }
965     }
966
967     void subscribeMQTopic() {
968         Map<String, String> queryParameters = new HashMap<>();
969         queryParameters.put("rt", "light");
970
971         try {
972             if (null != currentTopicResource) {
973                 currentTopicResource.subscribeMQTopic(
974                         queryParameters,
975                         this,
976                         QualityOfService.LOW);
977             }
978
979         } catch (OcException e) {
980             e.printStackTrace();
981         }
982     }
983
984     @Override
985     synchronized public void onSubScribeCompleted(List<OcHeaderOption> headerOptionList,
986                                                   OcRepresentation ocRepresentation,
987                                                   int sequenceNumber) {
988         synchronized (this) {
989             String resourceUri = ocRepresentation.getUri();
990             Log.d(TAG, "onSubScribeCompleted sequenceNumber : " + sequenceNumber);
991
992             try {
993                 int subFlag;
994                 OcRepresentation val = ocRepresentation.getValue("message");
995
996                 if (sequenceNumber == 0) {
997                     Log.d(TAG, "onSubScribeCompleted : " + resourceUri);
998                     subFlag = 0;
999                 } else {
1000                     subFlag = 1;
1001                 }
1002
1003                 if (subFlag == 0) {
1004                     msg("onSubScribeCompleted : " + resourceUri);
1005                 } else {
1006                     Log.d(TAG, "onSubScribeCompleted : " + resourceUri);
1007                     Log.d(TAG, "onSubScribeCompleted : " + val.getValue("blue"));
1008                     Log.d(TAG, "onSubScribeCompleted : " + val.getValue("red"));
1009
1010                     msg("onSubScribeCompleted : " + resourceUri + ", blue light is "
1011                             + val.getValue("blue").toString());
1012                     msg("onSubScribeCompleted : " + resourceUri + ", red light is "
1013                             + val.getValue("red").toString());
1014                 }
1015             } catch (OcException e) {
1016                 e.printStackTrace();
1017             }
1018         }
1019     }
1020
1021     @Override
1022     synchronized public void onUnsubScribeCompleted(OcRepresentation ocRepresentation,
1023                                                     int sequenceNumber) {
1024         synchronized (this) {
1025             String resourceUri = ocRepresentation.getUri();
1026             Log.d(TAG, "onUnsubScribeCompleted sequenceNumber : " + sequenceNumber);
1027
1028             if (sequenceNumber == maxSequenceNumber + 1) {
1029                 Log.d(TAG, "onUnsubScribeCompleted : " + resourceUri);
1030                 msg("onUnsubScribeCompleted : " + resourceUri);
1031             }
1032         }
1033     }
1034
1035     @Override
1036     public void onSubScribeFailed(Throwable ex) {
1037         Log.d(TAG, "onSubScribeFailed : ", ex);
1038
1039         if (ex instanceof OcException) {
1040             OcException ocEx = (OcException) ex;
1041             ErrorCode errCode = ocEx.getErrorCode();
1042             Log.d(TAG, "onSubScribeFailed error Code: " + errCode);
1043             Log.d(TAG, "onSubScribeFailed error Code: " + errCode.ordinal());
1044
1045
1046         } else {
1047             Log.e(TAG, ex.getMessage());
1048         }
1049     }
1050
1051
1052     void unsubscribeMQTopic() {
1053
1054         try {
1055             if (null != currentTopicResource) {
1056                 currentTopicResource.unsubscribeMQTopic(QualityOfService.LOW);
1057             }
1058
1059         } catch (OcException e) {
1060             e.printStackTrace();
1061         }
1062     }
1063
1064     void publishMQTopic() {
1065
1066         try {
1067             OcRepresentation rep = new OcRepresentation();
1068             OcRepresentation msg = new OcRepresentation();
1069
1070             if (switchingFlag) {
1071                 msg.setValue("blue", "on");
1072                 msg.setValue("red", "off");
1073                 switchingFlag = false;
1074             } else {
1075                 msg.setValue("blue", "off");
1076                 msg.setValue("red", "on");
1077                 switchingFlag = true;
1078             }
1079             rep.setValue("message", msg);
1080
1081             if (null != currentTopicResource) {
1082                 currentTopicResource.publishMQTopic(rep,
1083                         new HashMap<String, String>(),
1084                         mqPublishListener,
1085                         QualityOfService.LOW);
1086             }
1087
1088         } catch (OcException e) {
1089             e.printStackTrace();
1090         }
1091     }
1092
1093     OcResource.OnPostListener mqPublishListener =
1094             new OcResource.OnPostListener() {
1095                 @Override
1096                 public void onPostCompleted(List<OcHeaderOption> list,
1097                                             OcRepresentation ocRepresentation) {
1098                     Log.i(TAG, "onPublish completed");
1099                     msg("onPublish completed");
1100                 }
1101
1102                 @Override
1103                 public void onPostFailed(Throwable throwable) {
1104                     Log.e(TAG, "onPublish failed");
1105                     msg("onPublish failed");
1106                 }
1107             };
1108
1109     void requestMQPublish() {
1110
1111         try {
1112             if (null != currentTopicResource) {
1113                 currentTopicResource.requestMQPublish(
1114                         new HashMap<String, String>(),
1115                         mqReqPubListener,
1116                         QualityOfService.LOW);
1117             }
1118         } catch (OcException e) {
1119             e.printStackTrace();
1120         }
1121     }
1122
1123     OcResource.OnPostListener mqReqPubListener =
1124             new OcResource.OnPostListener() {
1125                 @Override
1126                 public void onPostCompleted(List<OcHeaderOption> list,
1127                                             OcRepresentation ocRepresentation) {
1128                     Log.i(TAG, "onRequestPublish completed");
1129                     msg("onPublish completed");
1130                 }
1131
1132                 @Override
1133                 public void onPostFailed(Throwable throwable) {
1134                     Log.e(TAG, "onRequestPublish failed");
1135                     msg("onRequestPublish failed");
1136                 }
1137             };
1138
1139     // ******************************************************************************
1140     // End of the Message Queue specific code
1141     // ******************************************************************************
1142
1143     private void showTCPInput() {
1144
1145         LayoutInflater layoutInflater = LayoutInflater.from(mContext);
1146         View inputView = layoutInflater.inflate(R.layout.input, null);
1147         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
1148         alertDialogBuilder.setView(inputView);
1149
1150         final EditText editText = (EditText) inputView.getRootView().findViewById(R.id.inputText);
1151         final CheckBox isSecured = (CheckBox) inputView.getRootView().findViewById(R.id.secured);
1152
1153         StringBuilder sb = new StringBuilder();
1154         sb.append(Common.TCP_ADDRESS);
1155         sb.append(Common.PORT_SEPARATOR);
1156         sb.append(Common.TCP_PORT);
1157         editText.setText(sb.toString());
1158
1159         isSecured.setVisibility(View.VISIBLE);
1160         isSecured.setChecked(mSecured);
1161
1162         alertDialogBuilder
1163                 .setCancelable(true)
1164                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
1165                     public void onClick(DialogInterface dialog, int id) {
1166                         if (editText.getText().length() != 0) {
1167                             final String hosts = editText.getText().toString();
1168                             boolean isValid = ADDRESS_PORT.matcher(hosts).matches();
1169                             if (isValid) {
1170                                 final String host[] = hosts.split(Common.PORT_SEPARATOR);
1171                                 Common.TCP_ADDRESS = host[0];
1172                                 Common.TCP_PORT = host[1];
1173                                 mSecured = isSecured.isChecked();
1174
1175                                 StringBuilder sb = new StringBuilder();
1176                                 if (mSecured) {
1177                                     sb.append(Common.COAPS_TCP);
1178                                 } else {
1179                                     sb.append(Common.COAP_TCP);
1180                                 }
1181                                 sb.append(Common.TCP_ADDRESS);
1182                                 sb.append(Common.PORT_SEPARATOR);
1183                                 sb.append(Common.TCP_PORT);
1184                                 Common.HOST = sb.toString();
1185                             } else {
1186                                 Toast.makeText(mContext, "Invalid IP", Toast.LENGTH_SHORT).show();
1187                                 showTCPInput();
1188                             }
1189                         }
1190                     }
1191                 })
1192                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
1193                     public void onClick(DialogInterface dialog, int id) {
1194                         dialog.cancel();
1195                     }
1196                 });
1197
1198         AlertDialog alert = alertDialogBuilder.create();
1199         alert.show();
1200     }
1201
1202     private void showInviteUser() {
1203
1204         LayoutInflater layoutInflater = LayoutInflater.from(mContext);
1205         View inputView = layoutInflater.inflate(R.layout.input, null);
1206         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
1207         alertDialogBuilder.setView(inputView);
1208
1209         final TextView textView = (TextView) inputView.getRootView().findViewById(R.id.inputView);
1210         textView.setText("Please enter user ID to invite.");
1211         final EditText editText = (EditText) inputView.getRootView().findViewById(R.id.inputText);
1212
1213         alertDialogBuilder
1214                 .setCancelable(true)
1215                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
1216                     public void onClick(DialogInterface dialog, int id) {
1217                         if (editText.getText().length() != 0) {
1218                             final String userID = editText.getText().toString();
1219
1220                             try {
1221                                 Map<String, String> option = new HashMap<>();
1222                                 option.put("userid", userID);
1223
1224                                 msg("searchUser (User ID: " + userID + ")");
1225                                 mAccountManager.searchUser(option, onSearchUserForInviter);
1226                             } catch (OcException e) {
1227                                 e.printStackTrace();
1228                             }
1229                         }
1230                     }
1231                 })
1232                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
1233                     public void onClick(DialogInterface dialog, int id) {
1234                         dialog.cancel();
1235                     }
1236                 });
1237
1238         AlertDialog alert = alertDialogBuilder.create();
1239         alert.show();
1240     }
1241
1242
1243     private void showPostPropertyValueToGroup(int operation) {
1244         LayoutInflater layoutInflater = LayoutInflater.from(mContext);
1245         View inputView = layoutInflater.inflate(R.layout.input, null);
1246         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
1247         alertDialogBuilder.setView(inputView);
1248
1249         final int op = operation;
1250
1251         final TextView textView = (TextView) inputView.getRootView().findViewById(R.id.inputView);
1252         final EditText editText = (EditText) inputView.getRootView().findViewById(R.id.inputText);
1253         final TextView textView2 = (TextView) inputView.getRootView().findViewById(R.id.inputView2);
1254         final EditText editText2 = (EditText) inputView.getRootView().findViewById(R.id.inputText2);
1255
1256         textView.setText("Please enter property.");
1257         textView2.setText("Please enter value(String Array).");
1258         textView2.setVisibility(View.VISIBLE);
1259         editText2.setVisibility(View.VISIBLE);
1260
1261
1262         alertDialogBuilder
1263                 .setCancelable(true)
1264                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
1265                     public void onClick(DialogInterface dialog, int id) {
1266                         if (editText.getText().length() != 0 && editText2.getText().length() != 0){
1267                             final String property = editText.getText().toString();
1268                             final String value = editText2.getText().toString();
1269                             String[] values = {value};
1270
1271                             try {
1272                                 OcRepresentation propertyValue = new OcRepresentation();
1273                                 propertyValue.setValue(property, values);
1274
1275                                 if (op == 0) {
1276                                     msg("addPropertyValueToGroup");
1277                                     mAccountManager.addPropertyValueToGroup(mGroupId, propertyValue, onPostPropertyValue);
1278                                 } else if (op == 1){
1279                                     msg("deletePropertyValueFromGroup");
1280                                     mAccountManager.deletePropertyValueFromGroup(mGroupId, propertyValue, onPostPropertyValue);
1281                                 } else if (op == 2){
1282                                     msg("updatePropertyValueOnGroup");
1283                                     mAccountManager.updatePropertyValueOnGroup(mGroupId, propertyValue, onPostPropertyValue);
1284                                 }
1285                             } catch (OcException e) {
1286                                 e.printStackTrace();
1287                             }
1288                         }
1289                     }
1290                 })
1291                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
1292                     public void onClick(DialogInterface dialog, int id) {
1293                         dialog.cancel();
1294                     }
1295                 });
1296
1297         AlertDialog alert = alertDialogBuilder.create();
1298         alert.show();
1299     }
1300
1301     private void showInviteMsg(String userID) {
1302
1303         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
1304         alertDialogBuilder.setTitle("Invitation");
1305         StringBuilder sb = new StringBuilder();
1306         sb.append("Invited from ");
1307         sb.append(userID);
1308         sb.append(EOL);
1309         sb.append("Accept?");
1310         alertDialogBuilder.setMessage(sb.toString());
1311         alertDialogBuilder
1312                 .setCancelable(true)
1313                 .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
1314                     public void onClick(DialogInterface dialog, int id) {
1315                         try {
1316                             msg("replyToInvitation (accept:yes)");
1317                             mAccountManager.replyToInvitation(mGroupId, true, onReplyToInvitation);
1318                         } catch (OcException e) {
1319                             e.printStackTrace();
1320                         }
1321                     }
1322                 })
1323                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
1324                     public void onClick(DialogInterface dialog, int id) {
1325                         dialog.cancel();
1326                         try {
1327                             msg("replyToInvitation (accept:no)");
1328                             mAccountManager.replyToInvitation(mGroupId, false, onReplyToInvitation);
1329                         } catch (OcException e) {
1330                             e.printStackTrace();
1331                         }
1332                     }
1333                 });
1334
1335         AlertDialog alert = alertDialogBuilder.create();
1336         alert.show();
1337     }
1338
1339     private void msg(final String text) {
1340         mActivity.runOnUiThread(new Runnable() {
1341             public void run() {
1342                 mResultLog.append(EOL);
1343                 mResultLog.append(text);
1344                 mScrollView.fullScroll(View.FOCUS_DOWN);
1345             }
1346         });
1347         Log.i(TAG, text);
1348     }
1349
1350     @Override
1351     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1352
1353         int visible;
1354         if (isChecked) {
1355             visible = View.VISIBLE;
1356         } else {
1357             visible = View.GONE;
1358         }
1359
1360         switch (buttonView.getId()) {
1361             case R.id.account_switch:
1362                 mAccountLayout.setVisibility(visible);
1363                 break;
1364             case R.id.rd_switch:
1365                 mRDLayout.setVisibility(visible);
1366                 break;
1367             case R.id.mq_switch:
1368                 mMQLayout.setVisibility(visible);
1369                 break;
1370         }
1371     }
1372
1373     @Override
1374     public void onClick(View view) {
1375         mActionLog.setText("[Action Log]" + EOL);
1376
1377         int viewID = view.getId();
1378         if (mAccountManager == null && (viewID == R.id.signin_button
1379                 || viewID == R.id.signout_button || viewID == R.id.creategroup_button
1380                 || viewID == R.id.invite_button || viewID == R.id.addpropertyvalue_button
1381                 || viewID == R.id.deletepropertyvalue_button || viewID == R.id.updatepropertyvalue_button)) {
1382             mActionLog.append("Do 'SignUp' first" + EOL);
1383             return;
1384         }
1385
1386         switch (viewID) {
1387             // Account
1388             case R.id.setip_button:
1389                 mActionLog.append("Set IP" + EOL);
1390                 showTCPInput();
1391                 break;
1392             case R.id.signup_button:
1393                 mActionLog.append("Sign Up" + EOL);
1394                 signUp();
1395                 break;
1396             case R.id.signin_button:
1397                 mActionLog.append("Sign In" + EOL);
1398                 signIn();
1399                 break;
1400             case R.id.signout_button:
1401                 mActionLog.append("Sign Out" + EOL);
1402                 signOut();
1403                 break;
1404             case R.id.creategroup_button:
1405                 mActionLog.append("Create Group" + EOL);
1406                 createGroup();
1407                 break;
1408             case R.id.deletegroup_button:
1409                 mActionLog.append("Delete Group" + EOL);
1410                 deleteGroup();
1411                 break;
1412             case R.id.invite_button:
1413                 mActionLog.append("Invite User" + EOL);
1414                 inviteUser();
1415                 break;
1416             case R.id.addpropertyvalue_button:
1417                 mActionLog.append("Add Property Value To Group" + EOL);
1418                 addPropertyValueToGroup();
1419                 break;
1420             case R.id.deletepropertyvalue_button:
1421                 mActionLog.append("Delete Property Value From Group" + EOL);
1422                 deletePropertyValueFromGroup();
1423                 break;
1424             case R.id.updatepropertyvalue_button:
1425                 mActionLog.append("Update Property Value On Group" + EOL);
1426                 updatePropertyValueOnGroup();
1427                 break;
1428
1429             // RD
1430             case R.id.rdpub_button:
1431                 mActionLog.append("Publish Resource To RD" + EOL);
1432                 publishResourceToRD();
1433                 break;
1434             case R.id.rddel_button:
1435                 mActionLog.append("Delete Resource From RD" + EOL);
1436                 deleteResourceFromRD();
1437                 break;
1438             case R.id.rddp_button:
1439                 mActionLog.append("Subscribe Device Presence" + EOL);
1440                 subscribeDevicePresence();
1441                 break;
1442
1443             // MQ
1444             case R.id.mqget_button:
1445                 mActionLog.append("Get MQ Broker" + EOL);
1446                 getMQBroker();
1447                 break;
1448             case R.id.mqcreate_button:
1449                 mActionLog.append("Create MQ Topic" + EOL);
1450                 createMQTopic();
1451                 break;
1452             case R.id.mqsub_button:
1453                 mActionLog.append("Subscribe MQ Topic" + EOL);
1454                 subscribeMQTopic();
1455                 break;
1456             case R.id.mqunsub_button:
1457                 mActionLog.append("Un-subscribe MQ Topic" + EOL);
1458                 unsubscribeMQTopic();
1459                 break;
1460             case R.id.mqpub_button:
1461                 mActionLog.append("Publish MQ Topic" + EOL);
1462                 publishMQTopic();
1463                 break;
1464             case R.id.mqreq_button:
1465                 mActionLog.append("Request MQ Publish" + EOL);
1466                 requestMQPublish();
1467                 break;
1468         }
1469     }
1470
1471     @Override
1472     public View onCreateView(LayoutInflater inflater, ViewGroup container,
1473                              Bundle savedInstanceState) {
1474         View rootView = inflater.inflate(R.layout.fragment_cloud, container, false);
1475
1476         mAccountLayout = (LinearLayout) rootView.findViewById(R.id.account_layout);
1477         mRDLayout = (LinearLayout) rootView.findViewById(R.id.rd_layout);
1478         mMQLayout = (LinearLayout) rootView.findViewById(R.id.mq_layout);
1479
1480         Switch accountSwitch = (Switch) rootView.findViewById(R.id.account_switch);
1481         Switch RDSwitch = (Switch) rootView.findViewById(R.id.rd_switch);
1482         Switch MQSwitch = (Switch) rootView.findViewById(R.id.mq_switch);
1483         accountSwitch.setOnCheckedChangeListener(this);
1484         RDSwitch.setOnCheckedChangeListener(this);
1485         MQSwitch.setOnCheckedChangeListener(this);
1486
1487         mAccountText = (TextView) rootView.findViewById(R.id.account_view);
1488         mRDText = (TextView) rootView.findViewById(R.id.rd_view);
1489         mMQText = (TextView) rootView.findViewById(R.id.mq_view);
1490         mScrollView = (ScrollView) rootView.findViewById(R.id.scroll_view);
1491         mActionLog = (TextView) rootView.findViewById(R.id.action_log_view);
1492         mResultLog = (TextView) rootView.findViewById(R.id.result_log_view);
1493
1494         Button setIPButton = (Button) rootView.findViewById(R.id.setip_button);
1495         Button signUpButton = (Button) rootView.findViewById(R.id.signup_button);
1496         Button signInButton = (Button) rootView.findViewById(R.id.signin_button);
1497         Button signOutButton = (Button) rootView.findViewById(R.id.signout_button);
1498         Button createGroupButton = (Button) rootView.findViewById(R.id.creategroup_button);
1499         Button inviteButton = (Button) rootView.findViewById(R.id.invite_button);
1500         Button addValueToGroup = (Button) rootView.findViewById(R.id.addpropertyvalue_button);
1501         Button deleteValueFromGroup = (Button) rootView.findViewById(R.id.deletepropertyvalue_button);
1502         Button updateValueOnGroup = (Button) rootView.findViewById(R.id.updatepropertyvalue_button);
1503         setIPButton.setOnClickListener(this);
1504         signUpButton.setOnClickListener(this);
1505         signInButton.setOnClickListener(this);
1506         signOutButton.setOnClickListener(this);
1507         createGroupButton.setOnClickListener(this);
1508         inviteButton.setOnClickListener(this);
1509         addValueToGroup.setOnClickListener(this);
1510         deleteValueFromGroup.setOnClickListener(this);
1511         updateValueOnGroup.setOnClickListener(this);
1512
1513         Button rdPubButton = (Button) rootView.findViewById(R.id.rdpub_button);
1514         Button rdDelButton = (Button) rootView.findViewById(R.id.rddel_button);
1515         mDevicePresenceButton = (Button) rootView.findViewById(R.id.rddp_button);
1516         rdPubButton.setOnClickListener(this);
1517         rdDelButton.setOnClickListener(this);
1518         mDevicePresenceButton.setOnClickListener(this);
1519
1520         Button mqBrokerButton = (Button) rootView.findViewById(R.id.mqget_button);
1521         Button createTopicButton = (Button) rootView.findViewById(R.id.mqcreate_button);
1522         Button subTopicButton = (Button) rootView.findViewById(R.id.mqsub_button);
1523         Button unsubTopicButton = (Button) rootView.findViewById(R.id.mqunsub_button);
1524         Button pubToicButton = (Button) rootView.findViewById(R.id.mqpub_button);
1525         mqBrokerButton.setOnClickListener(this);
1526         createTopicButton.setOnClickListener(this);
1527         subTopicButton.setOnClickListener(this);
1528         unsubTopicButton.setOnClickListener(this);
1529         pubToicButton.setOnClickListener(this);
1530
1531         return rootView;
1532     }
1533
1534     @Override
1535     public void onResume() {
1536         super.onResume();
1537     }
1538
1539     @Override
1540     public void onDestroy() {
1541         super.onDestroy();
1542     }
1543
1544 }