1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file EntityHandlerNoti.java
23 package oic.plugin.gear.noti;
25 import java.util.EnumSet;
26 import java.util.UUID;
28 import oic.plugin.gear.noti.Activator.TemplateTypes;
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;
40 import android.util.Log;
42 public class EntityHandlerNoti implements OcPlatform.EntityHandler {
44 final private static String TAG = "EntityHandler";
46 public static String textNoti;
48 public EntityHandlerResult handleEntity(OcResourceRequest resourcerequest) {
49 Log.e(TAG, "Entity Handler callback. LJH");
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();
61 if(handlerFlagSet.contains(RequestHandlerFlag.INIT))
63 Log.e(TAG, "requestFlag : Init");
65 if(handlerFlagSet.contains(RequestHandlerFlag.REQUEST))
67 OcResourceResponse response = new OcResourceResponse();
68 OcRepresentation representation = new OcRepresentation();
69 response.setRequestHandle(resourcerequest
71 response.setResourceHandle(resourcerequest
72 .getResourceHandle());
73 switch (requestType) {
78 textNoti = resourcerequest
79 .getResourceRepresentation()
81 } catch (OcException e) {
82 // TODO Auto-generated catch block
83 Log.e(TAG, e.getMessage());
90 response.setErrorCode(200);
91 // representation.setUri("/a/galaxy/gear");
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());
106 OcPlatform.sendResponse(response);
107 } catch (OcException e) {
108 // TODO Auto-generated catch block
110 return EntityHandlerResult.ERROR;
113 if(handlerFlagSet.contains(RequestHandlerFlag.OBSERVER))
115 Log.e(TAG, "requestFlag : Observer");
117 return EntityHandlerResult.OK;
119 return EntityHandlerResult.ERROR;
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")) {
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;
147 public void stop(BundleContext bundleContext) throws Exception {
148 Activator.AppContext = null;
149 Activator.mRichNotificationManager.stop();
152 public void perform(int primary) {
153 if (primary < 0 || primary >= TemplateTypes.values().length) {
157 Log.w(TAG, "Sending Notification");
159 switch (TemplateTypes.values()[primary]) {
161 performExample(new SmallHeaderExample(Activator.AppContext));
166 private void performExample(IExample example) {
167 UUID uuid = Activator.mRichNotificationManager.notify(example
169 Log.w(TAG, "Notification ID: " + uuid);