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