Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / examples / android / RCSampleServerApp / app / src / main / java / org / iotivity / service / sample / container / ResourceContainer.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.sample.container;
22
23 import android.content.Context;
24 import android.os.Message;
25
26 import android.os.PowerManager;
27 import android.util.Log;
28
29 import org.iotivity.service.resourcecontainer.RcsBundleInfo;
30 import org.iotivity.service.resourcecontainer.RcsResourceContainer;
31
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38  * For calling the Resource Container APIs as per user selection on UI and for
39  * updating the UI
40  *
41  * It contains all the Resource Container APIs.
42  */
43 public class ResourceContainer {
44
45     private RcsResourceContainer             containerInstance;
46     public static String                     logMessage;
47
48     private static ResourceContainerActivity resourceContainerActivityInstance;
49     private static Message                   msg;
50     public static boolean                    startBundleFlag;
51     private static boolean                   isStarted     = false;
52     PowerManager pm = null;
53     PowerManager.WakeLock wl = null;
54
55     // constructor
56     public ResourceContainer() {
57         resourceContainerActivityInstance = ResourceContainerActivity
58                 .getResourceContainerActivityObj();
59         containerInstance = new RcsResourceContainer(
60                 resourceContainerActivityInstance.getApplicationContext());
61         pm = (PowerManager) resourceContainerActivityInstance.getApplicationContext().
62                 getSystemService(Context.POWER_SERVICE);
63     }
64
65     // Start Container
66     public void startContainer(String sdCardPath) {
67
68         String configFile = sdCardPath + "/ResourceContainerConfig.xml";
69         Log.i("startContainer : config path : ", configFile);
70
71         if (!isStarted) {
72             wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
73                     "ResourceContainer sample");
74             wl.acquire();
75             containerInstance.startContainer(configFile);
76             isStarted = true;
77             logMessage = "Container Started ";
78
79             ResourceContainerActivity.setMessageLog(logMessage);
80             msg = Message.obtain();
81             msg.what = 1;
82             resourceContainerActivityInstance.getHandler().sendMessage(msg);
83
84             msg = Message.obtain();
85             msg.what = 0;
86             resourceContainerActivityInstance.getHandler().sendMessage(msg);
87
88             // initialize the information of the bundles
89             // which registered at the starting time of the container
90             List<RcsBundleInfo> bundleList = containerInstance.listBundles();
91             Iterator<RcsBundleInfo> it = bundleList.iterator();
92
93             while (it.hasNext())
94             {
95                 String id = ((RcsBundleInfo) it.next()).getID();
96                 if (id.equals(ExampleBundleDescription.DIBundle.mBundleId)) {
97                     ExampleBundleDescription.DIBundle.isStarted = true;
98                 }
99                 else if (id.equals(ExampleBundleDescription.DIAndroidBundle.mBundleId)) {
100                     ExampleBundleDescription.DIAndroidBundle.isStarted = true;
101                 }
102             }
103         }
104     }
105
106     // Stop Container
107     public void stopContainer() {
108
109         if (isStarted) {
110             containerInstance.stopContainer();
111             logMessage = "Container stopped";
112             isStarted = false;
113             wl.release();
114         } else {
115             logMessage = "Container not started";
116         }
117
118         ResourceContainerActivity.setMessageLog(logMessage);
119         msg = Message.obtain();
120         msg.what = 1;
121         resourceContainerActivityInstance.getHandler().sendMessage(msg);
122     }
123
124     // List Bundle Resources
125     public void listBundleResources(String bundleId) {
126
127         List<String> bundleResources = containerInstance
128                 .listBundleResources(bundleId);
129         Iterator<String> it = bundleResources.iterator();
130         logMessage = "";
131
132         if (0 == bundleResources.size()) {
133             logMessage = logMessage + "No resource found in the bundle" + "\n";
134         } else {
135             while (it.hasNext()) {
136                 String element = (String) it.next();
137                 logMessage = logMessage + element + "\n";
138             }
139         }
140         ResourceContainerActivity.setMessageLog(logMessage);
141         msg = Message.obtain();
142         msg.what = 1;
143         resourceContainerActivityInstance.getHandler().sendMessage(msg);
144     }
145
146     // List Bundles
147     public void listBundles() {
148
149         List<RcsBundleInfo> bundleList = containerInstance.listBundles();
150         Iterator<RcsBundleInfo> it = bundleList.iterator();
151         int i = 0;
152         logMessage = "";
153         logMessage = logMessage + "size of bundleList : " + bundleList.size()
154                 + "\n\n";
155
156         while (it.hasNext()) {
157             i++;
158             RcsBundleInfo object = (RcsBundleInfo) it.next();
159             logMessage += "Bundle : " + i + " -: \n";
160             logMessage += "ID : " + object.getID() + "\n";
161             logMessage += "Lib Path: " + object.getPath() + "\n";
162             if (!(object.getVersion().equalsIgnoreCase("")))
163                 logMessage += "version : " + object.getVersion() + "\n\n";
164             else
165                 logMessage += "\n";
166         }
167         ResourceContainerActivity.setMessageLog(logMessage);
168         msg = Message.obtain();
169         msg.what = 1;
170         resourceContainerActivityInstance.getHandler().sendMessage(msg);
171     }
172
173     public boolean bundleExists(String bundleId)
174     {
175         List<RcsBundleInfo> bundleList = containerInstance.listBundles();
176         Iterator<RcsBundleInfo> it = bundleList.iterator();
177
178         while (it.hasNext())
179         {
180             if (it.next().getID().equals(bundleId))
181                 return true;
182         }
183
184         return false;
185     }
186
187     // add Bundles
188     public void addBundle(BundleInformation bundle) {
189         if (bundleExists(bundle.mBundleId))
190             logMessage = "Bundle \'" + bundle.mBundleId + "\' already added" + "\n";
191
192         else {
193             containerInstance
194                     .addBundle(bundle.mBundleId, bundle.mBundleUri,
195                             bundle.mBundlePath, bundle.mActivator, bundle.mBundleParams);
196
197             logMessage = "bundle to add : " + "\n";
198             logMessage = logMessage + "ID : " + bundle.mBundleId + "\n";
199             logMessage = logMessage + "Uri: " + bundle.mBundleUri + "\n";
200             logMessage = logMessage
201                     + "Path : "
202                     + bundle.mBundlePath
203                     + "\n\n";
204             logMessage = logMessage + "bundle added successfully" + "\n";
205         }
206
207         ResourceContainerActivity.setMessageLog(logMessage);
208         msg = Message.obtain();
209         msg.what = 1;
210         resourceContainerActivityInstance.getHandler().sendMessage(msg);
211     }
212
213     // remove Bundles
214     public void removeBundle(BundleInformation bundle) {
215
216         if (!bundleExists(bundle.mBundleId))
217             logMessage = "Bundle \'" + bundle.mBundleId + "\' not added" + "\n";
218
219         else {
220             containerInstance.removeBundle(bundle.mBundleId);
221
222             bundle.isStarted = false;
223             logMessage = "bundle to remove : " + "\n";
224             logMessage = logMessage + "ID : " + bundle.mBundleId + "\n\n";
225             logMessage = logMessage + " bundle removed  successfully" + "\n";
226         }
227
228         ResourceContainerActivity.setMessageLog(logMessage);
229         msg = Message.obtain();
230         msg.what = 1;
231         resourceContainerActivityInstance.getHandler().sendMessage(msg);
232     }
233
234     // start Bundles
235     public void startBundle(BundleInformation bundle) {
236
237         if (!bundleExists(bundle.mBundleId)) {
238             logMessage = "Bundle \'" + bundle.mBundleId + "\' not added" + "\n";
239         } else if (bundle.isStarted) {
240             logMessage = "Bundle \'" + bundle.mBundleId + "\' already started" + "\n";
241         } else {
242             bundle.isStarted = true;
243             containerInstance.startBundle(bundle.mBundleId);
244
245             logMessage = " bundle to start" + "\n";
246             logMessage += " ID : " + bundle.mBundleId + "\n\n";
247             logMessage += " bundle started successfully" + "\n";
248         }
249
250         ResourceContainerActivity.setMessageLog(logMessage);
251         msg = Message.obtain();
252         msg.what = 1;
253         resourceContainerActivityInstance.getHandler().sendMessage(msg);
254     }
255
256     // Stop Bundles
257     public void stopBundle(BundleInformation bundle) {
258
259         if (!bundleExists(bundle.mBundleId)) {
260             logMessage = "Bundle \'" + bundle.mBundleId + "\' not added" + "\n";
261         }
262         else if (!bundle.isStarted) {
263             logMessage = "Bundle \'" + bundle.mBundleId + "\' is not Started" + "\n";
264         } else {
265             containerInstance.stopBundle(bundle.mBundleId);
266             bundle.isStarted = false;
267             logMessage = " bundle to stop" + "\n";
268             logMessage = logMessage + " ID : " + bundle.mBundleId  + "\n\n";
269             logMessage = logMessage + " bundle stopped successfully" + "\n";
270         }
271
272         ResourceContainerActivity.setMessageLog(logMessage);
273         msg = Message.obtain();
274         msg.what = 1;
275         resourceContainerActivityInstance.getHandler().sendMessage(msg);
276     }
277 }
278
279 class ExampleBundleDescription
280 {
281     static final BundleInformation BMIBundle =
282             new BundleInformation ("oic.bundle.BMISensor", "",
283                     "/data/data/org.iotivity.service.sample.resourcecontainer/files/" +
284                             "libBMISensorBundle.so",
285                     "bmisensor", new HashMap<String, String>());
286
287     static final BundleInformation DIBundle =
288             new BundleInformation ("oic.bundle.discomfortIndexSensor", "",
289                     "/data/data/org.iotivity.service.sample.resourcecontainer/files/" +
290                             "libDISensorBundle.so",
291                     "disensor", new HashMap<String, String>());
292
293     static final BundleInformation DIAndroidBundle =
294             new BundleInformation ("oic.android.sample", "",
295                     "org.iotivity.service.sample.androidbundle.apk",
296                     "org.iotivity.service.sample.androidbundle.SampleActivator",
297                     new HashMap<String, String>());
298 }
299
300 class BundleInformation
301 {
302     String mBundleId;
303     String mBundleUri;
304     String mBundlePath;
305     String mActivator;
306     Map<String, String> mBundleParams;
307
308     Boolean isStarted;
309
310     public BundleInformation(String id, String uri, String path, String activator, Map<String, String> params)
311     {
312         mBundleId = id;
313         mBundleUri = uri;
314         mBundlePath = path;
315         mActivator = activator;
316         mBundleParams = params;
317         isStarted = false;
318     }
319 }