Stop container functionality and bug fixed
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / java / org / iotivity / service / resourcecontainer / BundleResource.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 BundleResource {
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 BundleResource() {
46         initAttributes();
47     }
48
49     public BundleResource(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      * Deactivates the resource
119      */ 
120     public abstract void deactivateResource();
121
122     /**
123      * Retrieve the attribute (only data)
124      * 
125      * @param key
126      *            name of the attribute to be read
127      * @return Value of the attribute
128      */
129     protected final RcsValue getAttribute(String key) {
130         return m_attributes.get(key);
131     }
132
133     protected final RcsResourceAttributes getAttributes() {
134         RcsResourceAttributes ret = new RcsResourceAttributes(this.m_attributes);             
135         return ret;
136     }
137
138     /**
139      * Retrieve the attribute (map to read command)
140      * 
141      * @param key
142      *            name of the attribute to be set
143      * @param value
144      *            new value of the attribute
145      */
146     public abstract RcsResourceAttributes handleGetAttributesRequest();
147
148     /**
149      * Attribute keys provided through by the bundle resource.
150      * 
151      * @return Name of attribute keys as string array
152      */
153     public String[] getAttributeKeys() {
154             Set<String> keys = m_attributes.keySet();
155             return keys.toArray(new String[keys.size()]);
156         }
157
158     /**
159      * Setter for the uri property
160      * 
161      * @param uri
162      *            URI of the resource
163      */
164     public void setURI(String uri) {
165         this.m_uri = uri;
166     }
167
168     /**
169      * Returns the URI of the resource
170      * 
171      * @return Resource URI
172      */
173     public String getURI() {
174         return m_uri;
175     }
176
177     /**
178      * Sets the resource type property
179      * 
180      * @param resourceType
181      *            OIC resource type
182      */
183     public void setResourceType(String resourceType) {
184         this.m_resourceType = resourceType;
185     }
186
187     /**
188      * Getter for the resource type
189      * 
190      * @return OIC resource type
191      */
192     public String getResourceType() {
193         return m_resourceType;
194     }
195
196     /**
197      * Sets the technology specific address information (e.g., ZigBee short or
198      * long identifier)
199      * 
200      * @param address
201      *            Resource address
202      */
203     public void setAddress(String address) {
204         this.m_address = address;
205     }
206
207     /**
208      * Returns the technology specific address information
209      * 
210      * @return Resource address
211      */
212     public String getAddress() {
213         return m_address;
214     }
215
216     /**
217      * Sets the name property of the resource
218      * 
219      * @param name
220      *            Resource name
221      */
222     public void setName(String name) {
223         this.m_name = name;
224     }
225
226     /**
227      * Returns the name property of the resource
228      * 
229      * @return Resource name
230      */
231     public String getName() {
232         return m_name;
233     }
234 }