1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 package org.iotivity.service.sample.container;
23 import android.content.Context;
24 import android.os.Message;
26 import android.os.PowerManager;
27 import android.util.Log;
29 import org.iotivity.service.resourcecontainer.RcsBundleInfo;
30 import org.iotivity.service.resourcecontainer.RcsResourceContainer;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
38 * For calling the Resource Container APIs as per user selection on UI and for
41 * It contains all the Resource Container APIs.
43 public class ResourceContainer {
45 private RcsResourceContainer containerInstance;
46 public static String logMessage;
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;
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);
66 public void startContainer(String sdCardPath) {
68 String configFile = sdCardPath + "/ResourceContainerConfig.xml";
69 Log.i("startContainer : config path : ", configFile);
72 wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
73 "ResourceContainer sample");
75 containerInstance.startContainer(configFile);
77 logMessage = "Container Started ";
79 ResourceContainerActivity.setMessageLog(logMessage);
80 msg = Message.obtain();
82 resourceContainerActivityInstance.getHandler().sendMessage(msg);
84 msg = Message.obtain();
86 resourceContainerActivityInstance.getHandler().sendMessage(msg);
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();
95 String id = ((RcsBundleInfo) it.next()).getID();
96 if (id.equals(ExampleBundleDescription.DIBundle.mBundleId)) {
97 ExampleBundleDescription.DIBundle.isStarted = true;
99 else if (id.equals(ExampleBundleDescription.DIAndroidBundle.mBundleId)) {
100 ExampleBundleDescription.DIAndroidBundle.isStarted = true;
107 public void stopContainer() {
110 containerInstance.stopContainer();
111 logMessage = "Container stopped";
115 logMessage = "Container not started";
118 ResourceContainerActivity.setMessageLog(logMessage);
119 msg = Message.obtain();
121 resourceContainerActivityInstance.getHandler().sendMessage(msg);
124 // List Bundle Resources
125 public void listBundleResources(String bundleId) {
127 List<String> bundleResources = containerInstance
128 .listBundleResources(bundleId);
129 Iterator<String> it = bundleResources.iterator();
132 if (0 == bundleResources.size()) {
133 logMessage = logMessage + "No resource found in the bundle" + "\n";
135 while (it.hasNext()) {
136 String element = (String) it.next();
137 logMessage = logMessage + element + "\n";
140 ResourceContainerActivity.setMessageLog(logMessage);
141 msg = Message.obtain();
143 resourceContainerActivityInstance.getHandler().sendMessage(msg);
147 public void listBundles() {
149 List<RcsBundleInfo> bundleList = containerInstance.listBundles();
150 Iterator<RcsBundleInfo> it = bundleList.iterator();
153 logMessage = logMessage + "size of bundleList : " + bundleList.size()
156 while (it.hasNext()) {
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";
167 ResourceContainerActivity.setMessageLog(logMessage);
168 msg = Message.obtain();
170 resourceContainerActivityInstance.getHandler().sendMessage(msg);
173 public boolean bundleExists(String bundleId)
175 List<RcsBundleInfo> bundleList = containerInstance.listBundles();
176 Iterator<RcsBundleInfo> it = bundleList.iterator();
180 if (it.next().getID().equals(bundleId))
188 public void addBundle(BundleInformation bundle) {
189 if (bundleExists(bundle.mBundleId))
190 logMessage = "Bundle \'" + bundle.mBundleId + "\' already added" + "\n";
194 .addBundle(bundle.mBundleId, bundle.mBundleUri,
195 bundle.mBundlePath, bundle.mActivator, bundle.mBundleParams);
197 logMessage = "bundle to add : " + "\n";
198 logMessage = logMessage + "ID : " + bundle.mBundleId + "\n";
199 logMessage = logMessage + "Uri: " + bundle.mBundleUri + "\n";
200 logMessage = logMessage
204 logMessage = logMessage + "bundle added successfully" + "\n";
207 ResourceContainerActivity.setMessageLog(logMessage);
208 msg = Message.obtain();
210 resourceContainerActivityInstance.getHandler().sendMessage(msg);
214 public void removeBundle(BundleInformation bundle) {
216 if (!bundleExists(bundle.mBundleId))
217 logMessage = "Bundle \'" + bundle.mBundleId + "\' not added" + "\n";
220 containerInstance.removeBundle(bundle.mBundleId);
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";
228 ResourceContainerActivity.setMessageLog(logMessage);
229 msg = Message.obtain();
231 resourceContainerActivityInstance.getHandler().sendMessage(msg);
235 public void startBundle(BundleInformation bundle) {
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";
242 bundle.isStarted = true;
243 containerInstance.startBundle(bundle.mBundleId);
245 logMessage = " bundle to start" + "\n";
246 logMessage += " ID : " + bundle.mBundleId + "\n\n";
247 logMessage += " bundle started successfully" + "\n";
250 ResourceContainerActivity.setMessageLog(logMessage);
251 msg = Message.obtain();
253 resourceContainerActivityInstance.getHandler().sendMessage(msg);
257 public void stopBundle(BundleInformation bundle) {
259 if (!bundleExists(bundle.mBundleId)) {
260 logMessage = "Bundle \'" + bundle.mBundleId + "\' not added" + "\n";
262 else if (!bundle.isStarted) {
263 logMessage = "Bundle \'" + bundle.mBundleId + "\' is not Started" + "\n";
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";
272 ResourceContainerActivity.setMessageLog(logMessage);
273 msg = Message.obtain();
275 resourceContainerActivityInstance.getHandler().sendMessage(msg);
279 class ExampleBundleDescription
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>());
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>());
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>());
300 class BundleInformation
306 Map<String, String> mBundleParams;
310 public BundleInformation(String id, String uri, String path, String activator, Map<String, String> params)
315 mActivator = activator;
316 mBundleParams = params;