Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / Android / plugin.gear.noti / src / oic / plugin / gear / noti / EntityHandlerNoti.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 /// @file EntityHandlerNoti.java
22
23 package oic.plugin.gear.noti;
24
25 import java.util.UUID;
26
27 import oic.plugin.gear.noti.Activator.TemplateTypes;
28
29 import org.iotivity.base.EntityHandlerResult;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
33 import org.iotivity.base.OcResourceRequest;
34 import org.iotivity.base.OcResourceResponse;
35 import org.iotivity.base.RequestHandlerFlag;
36 import org.iotivity.base.RequestType;
37 import org.osgi.framework.BundleContext;
38
39 import android.util.Log;
40
41 public class EntityHandlerNoti implements OcPlatform.EntityHandler {
42
43     final private static String TAG = "EntityHandler";
44
45     public static String        textNoti;
46
47     public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
48         Log.e(TAG, "Entity Handler callback. LJH");
49
50         System.out.println("IN Server CPP entity Handler");
51         System.out.println("gear");
52         // OcResourceResponse response = new OcResourceResponse();
53         // int result = 1; // EH_ERROR
54         if (resourcerequest != null
55                 && resourcerequest.getResourceUri().equals("/a/galaxy/gear")) {
56             RequestType requestType = resourcerequest.getRequestType();
57             EnumSet<RequestHandlerFlag> handlerFlagSet = resourcerequest
58                     .getRequestHandlerFlagSet();
59
60             if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
61             {
62                 Log.e(TAG, "requestFlag : Init");
63             }
64             if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
65             {
66                 OcResourceResponse response = new OcResourceResponse();
67                 OcRepresentation representation = new OcRepresentation();
68                 response.setRequestHandle(resourcerequest
69                         .getRequestHandle());
70                 response.setResourceHandle(resourcerequest
71                         .getResourceHandle());
72                 Log.d("JUDO",
73                         "/******************************************************************************/");
74                 Log.d("JUDO", "Name: "
75                         + resourcerequest.getResourceRepresentation()
76                                 .getValueString("name"));
77                 Log.d("JUDO", "Name: "
78                         + resourcerequest.getResourceRepresentation()
79                                 .getValueString("power"));
80                 Log.d("JUDO", "Name: "
81                         + resourcerequest.getResourceRepresentation()
82                                 .getValueInt("brigthness"));
83                 Log.d("JUDO", "Name: "
84                         + resourcerequest.getResourceRepresentation()
85                                 .getValueInt("color"));
86                 Log.d("JUDO",
87                         "/******************************************************************************/");
88                 switch (requestType) {
89                     case GET:
90                         break;
91                     case PUT:
92                         textNoti = resourcerequest
93                                 .getResourceRepresentation()
94                                 .getValueString("power");
95                         perform(0);
96                         break;
97                     case POST:
98                         break;
99                 }
100                 response.setErrorCode(200);
101                 // representation.setUri("/a/galaxy/gear");
102                 representation.setValueString("name",
103                         Activator.myNotify.m_name);
104                 representation.setValueString("power",
105                         Activator.myNotify.m_power);
106                 representation.setValueInt("brightness", 0);
107                 representation.setValueInt("color", 0);
108                 response.setResourceRepresentation(representation);
109                 try {
110                     OcPlatform.sendResponse(response);
111                 } catch (OcException e) {
112                     // TODO Auto-generated catch block
113                     e.printStackTrace();
114                     return EntityHandlerResult.ERROR;
115                 }
116                 }
117             if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
118             {
119                 Log.e(TAG, "requestFlag : Observer");
120             }
121             return EntityHandlerResult.OK;
122         }
123         return EntityHandlerResult.ERROR;
124         /*
125          * if((requestFlag & 1 << 0) != 0 ) // init flag { Log.e(TAG,
126          * "requestFlag : Init"); } if((requestFlag & 1 << 1) != 0)
127          * //RequestFlag { if(requestType.equals("GET")) {
128          * 
129          * } else if(requestType.equals("PUT")) { textNoti =
130          * resourcerequest.getResourceRepresentation().getValueString("power");
131          * perform(0); } else if(requestType.equals("POST")) { }
132          * OcRepresentation representation = new OcRepresentation();
133          * OcResourceResponse response = new OcResourceResponse();
134          * response.setRequestHandle(resourcerequest.getRequestHandle());
135          * response.setResourceHandle(resourcerequest.getResourceHandle());
136          * representation.setUri("/a/galaxy/gear");
137          * representation.setValueString("name", Activator.myNotify.m_name);
138          * representation.setValueString("power", Activator.myNotify.m_power);
139          * representation.setValueInt("brightness", 0);
140          * representation.setValueInt("color", 0); response.setErrorCode(200);
141          * response.setResourceRepresentation(representation); try {
142          * OcPlatform.sendResponse(response); } catch (OcException e) { // TODO
143          * Auto-generated catch block e.printStackTrace(); return
144          * EntityHandlerResult.ERROR; } } if((requestFlag & 1 << 2) != 0)
145          * //ObserverFlag { Log.e(TAG, "requestFlag : Observer"); } } return
146          * EntityHandlerResult.OK;
147          */
148
149     }
150
151     public void stop(BundleContext bundleContext) throws Exception {
152         Activator.AppContext = null;
153         Activator.mRichNotificationManager.stop();
154     }
155
156     public void perform(int primary) {
157         if (primary < 0 || primary >= TemplateTypes.values().length) {
158             return;
159         }
160
161         Log.w(TAG, "Sending Notification");
162
163         switch (TemplateTypes.values()[primary]) {
164             case SMALL_HEADER:
165                 performExample(new SmallHeaderExample(Activator.AppContext));
166                 break;
167         }
168     }
169
170     private void performExample(IExample example) {
171         UUID uuid = Activator.mRichNotificationManager.notify(example
172                 .createRichNoti());
173         Log.w(TAG, "Notification ID: " + uuid);
174     }
175 }