Imported Upstream version 0.9.2
[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.IActionListener;
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] " + this.getClass()
42                                                                    .getSimpleName();
43     private static Message   msg;
44     private String           logMessage;
45     private GroupApiActivity groupApiActivityObj = null;
46
47     @Override
48     public void onPostResponseCallback(Vector<OcHeaderOption> headerOptions,
49             OcRepresentation rep, int errorValue) {
50         Log.i(LOG_TAG, "Got Callback : onPostResponseCallback");
51
52         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
53         logMessage = "API Result : SUCCESS" + "\n";
54         logMessage = logMessage
55                 + "Recieved Callback for called API (OnPostCallback)" + "\n";
56
57         // sending message to handler of GroupApiActivity to Update the UI
58         GroupApiActivity.setMessageLog(logMessage);
59         msg = Message.obtain();
60         msg.what = 1;
61         groupApiActivityObj.getHandler().sendMessage(msg);
62
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
109     @Override
110     public void onPutResponseCallback(Vector<OcHeaderOption> headerOptions,
111             OcRepresentation rep, int errorValue) {
112         Log.i(LOG_TAG, "Got Callback : onPutResponseCallback");
113
114         groupApiActivityObj = GroupApiActivity.getGroupApiActivityObj();
115         logMessage = "API Result : SUCCESS" + "\n";
116         logMessage = logMessage
117                 + "Recieved Callback for called API (onPutResponseCallback)"
118                 + "\n";
119         GroupApiActivity.setMessageLog(logMessage);
120         // sending message to handler of GroupApiActivity to Update the UI
121         msg = Message.obtain();
122         msg.what = 1;
123         groupApiActivityObj.getHandler().sendMessage(msg);
124
125     }
126
127 }