02969850e431ca805f1f21f8ad303d8f66b688e9
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / Sample / src / com / tm / sample / ActionListener.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 package com.tm.sample;
22
23 import java.util.Vector;
24
25 import org.iotivity.base.OcHeaderOption;
26 import org.iotivity.base.OcRepresentation;
27 import org.iotivity.service.tm.Action;
28 import org.iotivity.service.tm.ActionSet;
29 import org.iotivity.service.tm.Capability;
30 import org.iotivity.service.tm.GroupManager.*;
31
32 import android.os.Message;
33 import android.util.Log;
34
35 /*
36  * For getting the put,get,observe and post Callback and updating
37  *  the UI i.e. logMessage(TextBox) of GroupApiActivity.
38  */
39 public class ActionListener implements IActionListener {
40
41     private final String     LOG_TAG             = "[TMSample] "
42                                                          + this.getClass()
43                                                                  .getSimpleName();
44     private static Message   msg;
45     private String           logMessage;
46     private GroupApiActivity groupApiActivityObj = null;
47
48     @Override
49     public void onPostResponseCallback(Vector<OcHeaderOption> headerOptions,
50             OcRepresentation rep, int errorValue) {
51         Log.i(LOG_TAG, "Got Callback : onPostResponseCallback");
52
53         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
54         logMessage = "API Result : SUCCESS" + "\n";
55         logMessage += "Recieved Callback for called API (OnPostCallback)"
56                 + "\n";
57
58         // sending message to handler of GroupApiActivity to Update the UI
59         GroupApiActivity.setMessageLog(logMessage);
60         msg = Message.obtain();
61         msg.what = 1;
62         groupApiActivityObj.getHandler().sendMessage(msg);
63     }
64
65     @Override
66     public void onGetResponseCallback(Vector<OcHeaderOption> headerOptions,
67             OcRepresentation rep, int errorValue) {
68         Log.i(LOG_TAG, "Got Callback : onGetResponseCallback");
69
70         String actionSetStr = rep.getValueString("ActionSet");
71         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
72         logMessage = "Recieved Callback for called API (onGetResponseCallback)"
73                 + "\n" + "ActionSet:" + actionSetStr;
74
75         GroupApiActivity.setMessageLog(logMessage);
76         // sending message to handler of GroupApiActivity to Update the UI
77         msg = Message.obtain();
78         msg.what = 1;
79         groupApiActivityObj.getHandler().sendMessage(msg);
80
81         if (actionSetStr != null) {
82             ActionSet actionSet = ActionSet.toActionSet(actionSetStr);
83             if (actionSet != null) {
84                 System.out.println("ActionSet Name : "
85                         + actionSet.actionsetName);
86                 for (int i = 0; i < actionSet.listOfAction.size(); i++) {
87                     Action action = actionSet.listOfAction.get(i);
88                     System.out.println("Target : " + action.target);
89
90                     Vector<Capability> listOfCapability = action.listOfCapability;
91                     for (int j = 0; j < listOfCapability.size(); j++) {
92                         Capability capability = listOfCapability.get(j);
93                         System.out.println("Capability : "
94                                 + capability.capability);
95                         System.out.println("Status : " + capability.status);
96                     }
97                 }
98             }
99         }
100
101         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
102         GroupApiActivity.setMessageLog(logMessage);
103         msg = Message.obtain();
104         msg.what = 1;
105         groupApiActivityObj.getHandler().sendMessage(msg);
106     }
107
108     @Override
109     public void onPutResponseCallback(Vector<OcHeaderOption> headerOptions,
110             OcRepresentation rep, int errorValue) {
111         Log.i(LOG_TAG, "Got Callback : onPutResponseCallback");
112
113         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
114         logMessage = "API Result : SUCCESS" + "\n";
115         logMessage += "Recieved Callback for called API (onPutResponseCallback)"
116                 + "\n";
117         GroupApiActivity.setMessageLog(logMessage);
118         // sending message to handler of GroupApiActivity to Update the UI
119         msg = Message.obtain();
120         msg.what = 1;
121         groupApiActivityObj.getHandler().sendMessage(msg);
122     }
123 }