Cleaned up JNI wrapper, android light sensor resource example, notification mechanism...
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / java / org / iotivity / service / resourcecontainer / AndroidBundleResource.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 org.iotivity.service.resourcecontainer;
22
23 import java.util.HashMap;
24 import java.util.Set;
25 import android.content.Context;
26 import android.util.Log;
27
28 /**
29  * Basic BundleResource that should be used as a base class by a bundle
30  * resources. A concrete technology has to override the setAttribute and
31  * getAttribute method and map the according reads and writes to the technology
32  * specific messages.
33  */
34 public abstract class AndroidBundleResource {
35     protected String                m_name, m_uri, m_resourceType, m_address;
36
37     protected RcsResourceAttributes m_attributes = new RcsResourceAttributes();
38
39     protected Context               m_context;
40     
41     long mNativeHandle;
42     
43     protected native void updateNativeInstance(RcsResourceAttributes update);
44
45     public AndroidBundleResource() {
46         initAttributes();
47     }
48
49     public AndroidBundleResource(Context context) {
50         this();
51         this.m_context = context;
52     }
53
54     /**
55      * Initialize the internal attribute structure.
56      */
57     protected abstract void initAttributes();
58
59     /**
60      * Set the attribute (map to a send command for the according protocol)
61      * 
62      * @param key
63      *            name of the attribute to be set
64      * @param value
65      *            new value of the attribute
66      */
67     protected final void setAttribute(String key, RcsValue value, boolean notify) {
68         m_attributes.put(key, value);
69         
70         if(notify){
71             updateNativeInstance(m_attributes);
72         }
73     }
74     
75     /**
76      * Set the attribute (map to a send command for the according protocol)
77      * 
78      * @param key
79      *            name of the attribute to be set
80      * @param value
81      *            new value of the attribute
82      */
83     protected final void setAttribute(String key, RcsValue value) {
84         setAttribute(key, value, false);
85     }
86
87     /**
88      * Set the attribute (map to a send command for the according protocol)
89      * 
90      * @param key
91      *            name of the attribute to be set
92      * @param value
93      *            new value of the attribute
94      */
95     protected final void setAttributes(RcsResourceAttributes value, boolean notify) {
96         m_attributes.put(value);
97         
98         if(notify){
99             updateNativeInstance(m_attributes);
100         }
101     }
102
103     protected final void setAttributes(RcsResourceAttributes value) {
104         setAttributes(value, false);
105     }
106
107     /**
108      * Set the attribute (map to a send command for the according protocol)
109      * 
110      * @param key
111      *            name of the attribute to be set
112      * @param value
113      *            new value of the attribute
114      */
115     public abstract void handleSetAttributesRequest(RcsResourceAttributes value);
116
117     /**
118      * Retrieve the attribute (only data)
119      * 
120      * @param key
121      *            name of the attribute to be read
122      * @return Value of the attribute
123      */
124     protected final RcsValue getAttribute(String key) {
125         return m_attributes.get(key);
126     }
127
128     protected final RcsResourceAttributes getAttributes() {
129         RcsResourceAttributes ret = new RcsResourceAttributes(this.m_attributes);             
130         return ret;
131     }
132
133     /**
134      * Retrieve the attribute (map to read command)
135      * 
136      * @param key
137      *            name of the attribute to be set
138      * @param value
139      *            new value of the attribute
140      */
141     public abstract RcsResourceAttributes handleGetAttributesRequest();
142
143     /**
144      * Attribute keys provided through by the bundle resource.
145      * 
146      * @return Name of attribute keys as string array
147      */
148     public String[] getAttributeKeys() {
149             Set<String> keys = m_attributes.keySet();
150             return keys.toArray(new String[keys.size()]);
151         }
152
153     /**
154      * Setter for the uri property
155      * 
156      * @param uri
157      *            URI of the resource
158      */
159     public void setURI(String uri) {
160         this.m_uri = uri;
161     }
162
163     /**
164      * Returns the URI of the resource
165      * 
166      * @return Resource URI
167      */
168     public String getURI() {
169         return m_uri;
170     }
171
172     /**
173      * Sets the resource type property
174      * 
175      * @param resourceType
176      *            OIC resource type
177      */
178     public void setResourceType(String resourceType) {
179         this.m_resourceType = resourceType;
180     }
181
182     /**
183      * Getter for the resource type
184      * 
185      * @return OIC resource type
186      */
187     public String getResourceType() {
188         return m_resourceType;
189     }
190
191     /**
192      * Sets the technology specific address information (e.g., ZigBee short or
193      * long identifier)
194      * 
195      * @param address
196      *            Resource address
197      */
198     public void setAddress(String address) {
199         this.m_address = address;
200     }
201
202     /**
203      * Returns the technology specific address information
204      * 
205      * @return Resource address
206      */
207     public String getAddress() {
208         return m_address;
209     }
210
211     /**
212      * Sets the name property of the resource
213      * 
214      * @param name
215      *            Resource name
216      */
217     public void setName(String name) {
218         this.m_name = name;
219     }
220
221     /**
222      * Returns the name property of the resource
223      * 
224      * @return Resource name
225      */
226     public String getName() {
227         return m_name;
228     }
229 }