Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / Sample / src / com / tm / sample / ConfigurationApiActivity.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 package com.tm.sample;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcHeaderOption;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcRepresentation;
32 import org.iotivity.base.OcResource;
33 import org.iotivity.base.OcResourceHandle;
34 import org.iotivity.service.tm.IConfigurationListener;
35 import org.iotivity.service.tm.IDiagnosticsListener;
36 import org.iotivity.service.tm.IFindCandidateResourceListener;
37 import org.iotivity.service.tm.OCStackResult;
38 import org.iotivity.service.tm.ThingsManager;
39
40 import android.app.Activity;
41 import android.app.Dialog;
42 import android.content.Context;
43 import android.os.Bundle;
44 import android.os.Handler;
45 import android.os.Message;
46 import android.util.Log;
47 import android.view.View;
48 import android.view.View.OnClickListener;
49 import android.widget.AdapterView;
50 import android.widget.AdapterView.OnItemClickListener;
51 import android.widget.ArrayAdapter;
52 import android.widget.Button;
53 import android.widget.EditText;
54 import android.widget.ListView;
55 import android.widget.Toast;
56
57 /*
58  * Activity for Handling all Configuration Apis as per user's selection on the UI.
59  * and updating of UI
60  */
61 public class ConfigurationApiActivity extends Activity {
62
63     private class ResourceInformation {
64         OcResource       resource       = null;
65         OcResourceHandle resourceHandle = null;
66     }
67
68     private final String                     LOG_TAG                                = "[TMSample] "
69                                                                                             + this.getClass()
70                                                                                                     .getSimpleName();
71
72     private final String                     CONFIGURATION_COLLECTION_RESOURCE_URI  = "/core/configuration/resourceset";
73     private final String                     CONFIGURATION_COLLECTION_RESOURCE_TYPE = "core.configuration.resourceset";
74     private final String                     DIAGNOSTIC_COLLECTION_RESOURCE_URI     = "/core/diagnostics/resourceset";
75     private final String                     DIAGNOSTIC_COLLECTION_RESOURCE_TYPE    = "core.diagnostics.resourceset";
76     private final String                     FACTORYSET_COLLECTION_RESOURCE_URI     = "/core/factoryset/resourceset";
77     private final String                     FACTORYSET_COLLECTION_RESOURCE_TYPE    = "core.factoryset.resourceset";
78
79     private final String                     CONFIGURATION_RESOURCE_URI             = "/oic/con";
80     private final String                     DIAGNOSTIC_RESOURCE_URI                = "/oic/diag";
81     private final String                     FACTORYSET_RESOURCE_URI                = "/factorySet";
82
83     private ListView                         list;
84     private ArrayAdapter<String>             configurationApis;
85     private ArrayList<String>                configurationApisList;
86
87     private static int                       messageCount                           = 0;
88     private static EditText                  logs;
89     private static String                    logMessage                             = "";
90     private static Handler                   mHandler;
91     private static Message                   msg;
92
93     private ThingsManager                    thingsManager                          = null;
94     private Map<String, ResourceInformation> resourceList                           = null;
95     private Map<String, ResourceInformation> collectionList                         = null;
96
97     public boolean                           configurationResourceFlag              = false;
98     public boolean                           factorySetResourceFlag                 = false;
99     public boolean                           diagnosticsResourceFlag                = false;
100
101     public static Context                    mcontext;
102     public String                            region                                 = "";
103     public boolean                           findGroupPressedFlag                   = false;
104
105     @Override
106     protected void onCreate(Bundle savedInstanceState) {
107         super.onCreate(savedInstanceState);
108         setContentView(R.layout.configapis);
109
110         mcontext = this;
111         thingsManager = new ThingsManager();
112
113         // set the listeners
114         setResourceListener();
115         setConfigurationListener();
116         setDiagnosticsListener();
117
118         // Create API menu list
119         configurationApisList = new ArrayList<String>();
120
121         logs = (EditText) findViewById(R.id.EditText);
122
123         list = (ListView) findViewById(R.id.configApisList);
124         configurationApisList.add("Find All Groups");
125         configurationApisList.add("Find All Resources");
126         configurationApisList.add("Get a Configuration Resource");
127         configurationApisList.add("Update Attribute (Region)");
128         configurationApisList.add("Factory Reset");
129         configurationApisList.add("Reboot");
130         configurationApisList.add("Get Supported Configuration Units");
131         configurationApis = new ArrayAdapter<String>(this,
132                 android.R.layout.simple_list_item_1, configurationApisList);
133         list.setAdapter(configurationApis);
134
135         // setting the Listener for calling the APIs as per User selection
136         list.setOnItemClickListener(new OnItemClickListener() {
137
138             @Override
139             public void onItemClick(AdapterView<?> parent, View view,
140                     int position, long id) {
141
142                 // Find All Groups
143                 if (position == 0) {
144                     Vector<String> resourceTypes = new Vector<String>();
145                     resourceTypes.add("core.configuration.resourceset");
146                     findCandidateResources(resourceTypes);
147
148                     logMessage = "";
149                     messageCount = 0;
150                     messageCount++;
151
152                     resourceTypes.clear();
153                     resourceTypes.add("core.diagnostics.resourceset");
154                     findCandidateResources(resourceTypes);
155
156                     messageCount++;
157
158                     resourceTypes.clear();
159                     resourceTypes.add("core.factoryset.resourceset");
160                     findCandidateResources(resourceTypes);
161
162                     messageCount++;
163
164                 } else if (position == 1) { // Find All Resources
165                     if (false == findGroupPressedFlag) {
166                         displayToastMessage("Configuration collection resource does not exist!");
167                     } else {
168                         Vector<String> resourceTypes = new Vector<String>();
169                         resourceTypes.add("oic.con");
170                         findCandidateResources(resourceTypes);
171
172                         logMessage = "";
173                         messageCount = 0;
174                         messageCount++;
175
176                         resourceTypes.clear();
177                         resourceTypes.add("oic.diag");
178                         findCandidateResources(resourceTypes);
179
180                         messageCount++;
181
182                         resourceTypes.clear();
183                         resourceTypes.add("factorySet");
184                         findCandidateResources(resourceTypes);
185
186                         messageCount++;
187                     }
188                 } else if (position == 2) { // Get Configuration
189                     getConfiguration();
190                 } else if (position == 3) { // Update Attribute (Region)
191                     userInputDialog();
192                 } else if (position == 4) { // Factory Reset
193                     factoryReset();
194                 } else if (position == 5) { // Reboot
195                     reboot();
196                 } else if (position == 6) { // Get Supported Configuration Units
197                     String configurationUnits;
198                     configurationUnits = getListOfSupportedConfigurationUnits();
199                     Log.i(LOG_TAG, "Configuration Units:" + configurationUnits);
200                     logMessage = configurationUnits;
201                     logs.setText("");
202                     logs.setText(logMessage);
203
204                 }
205             }
206         });
207
208         resourceList = new HashMap<String, ResourceInformation>();
209         collectionList = new HashMap<String, ResourceInformation>();
210
211         try {
212             createResourceCollection(CONFIGURATION_COLLECTION_RESOURCE_URI,
213                     CONFIGURATION_COLLECTION_RESOURCE_TYPE);
214             createResourceCollection(DIAGNOSTIC_COLLECTION_RESOURCE_URI,
215                     DIAGNOSTIC_COLLECTION_RESOURCE_TYPE);
216             createResourceCollection(FACTORYSET_COLLECTION_RESOURCE_URI,
217                     FACTORYSET_COLLECTION_RESOURCE_TYPE);
218         } catch (OcException e) {
219             Log.e(LOG_TAG, "OcException occured! " + e.toString());
220         }
221
222         // handler for updating the UI i.e. LogMessage TextBox
223         mHandler = new Handler() {
224             @Override
225             public void handleMessage(Message msg) {
226                 switch (msg.what) {
227                     case 0:
228                         logs.setText("");
229                         logs.setText(logMessage);
230                 }
231             }
232         };
233
234     }
235
236     private void userInputDialog() {
237
238         ResourceInformation configurationCollection = collectionList
239                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
240         if (null == configurationCollection
241                 || null == configurationCollection.resource) {
242             Log.e(LOG_TAG, "configuration collection resource doest not exist!");
243             displayToastMessage("Configuration collection resource does not exist!");
244         }
245         if (false == configurationResourceFlag) {
246             Log.e(LOG_TAG, "configuration resource doest not exist!");
247             displayToastMessage("Configuration resource does not exist!");
248         } else {
249             final Dialog dialog = new Dialog(mcontext);
250             dialog.setContentView(R.layout.userinputforregionvalue);
251             dialog.setTitle("Enter the Region Value");
252
253             dialog.setCancelable(false);
254             dialog.show();
255             Button ok = (Button) dialog.findViewById(R.id.ok);
256             Button cancel = (Button) dialog.findViewById(R.id.cancel);
257
258             ok.setOnClickListener(new OnClickListener() {
259                 @Override
260                 public void onClick(View v) {
261
262                     EditText regionValue = (EditText) dialog
263                             .findViewById(R.id.region);
264                     region = regionValue.getText().toString();
265                     if (region.equalsIgnoreCase("")) {
266                         String toastmessage = "Please enter the Region Value";
267                         displayToastMessage(toastmessage);
268                     } else {
269                         dialog.dismiss();
270                         updateConfiguration(region);
271                     }
272                 }
273             });
274             cancel.setOnClickListener(new OnClickListener() {
275                 @Override
276                 public void onClick(View v) {
277                     dialog.dismiss();
278                 }
279             });
280         }
281     }
282
283     @Override
284     public void onBackPressed() {
285         super.onBackPressed();
286
287         // unregistering resource from the platform
288         deleteResources();
289         resourceList.clear();
290         resourceList = null;
291         collectionList.clear();
292         collectionList = null;
293     }
294
295     private void setResourceListener() {
296         thingsManager
297                 .setFindCandidateResourceListener(new IFindCandidateResourceListener() {
298
299                     @Override
300                     synchronized public void onResourceCallback(
301                             Vector<OcResource> resources) {
302                         Log.i(LOG_TAG, "onResourceCallback: enter");
303                         for (int i = 0; i < resources.size(); i++) {
304                             OcResource resource = resources.get(i);
305                             String uri = resource.getUri();
306                             String host = resource.getHost();
307
308                             // Display resource information
309                             Log.i(LOG_TAG, "Resource URI:" + uri);
310                             Log.i(LOG_TAG, "Resource HOST: " + host);
311                             Log.i(LOG_TAG, "Resource types: ");
312                             logMessage = logMessage + "Resource URI : " + uri
313                                     + "\n";
314                             logMessage = logMessage + "Resource Host : " + host
315                                     + "\n";
316
317                             List<String> resourcetypes = resource
318                                     .getResourceTypes();
319                             for (int j = 0; j < resourcetypes.size(); j++) {
320                                 Log.i(LOG_TAG, resourcetypes.get(j));
321                                 logMessage = logMessage + "ResourceType "
322                                         + (j + 1) + " : "
323                                         + resourcetypes.get(j) + "\n";
324                             }
325
326                             Log.i(LOG_TAG, "Interface types: ");
327                             List<String> interfacetypes = resource
328                                     .getResourceInterfaces();
329                             for (int j = 0; j < interfacetypes.size(); j++) {
330                                 Log.i(LOG_TAG, interfacetypes.get(j));
331                                 logMessage = logMessage + "interfacetype "
332                                         + (j + 1) + " : "
333                                         + interfacetypes.get(j) + "\n";
334                             }
335
336                             try {
337                                 if (true == uri.contains("/resourceset")) {
338                                     collectionFound(resource);
339                                 } else {
340                                     resourceFound(resource);
341                                 }
342                             } catch (OcException e) {
343                                 Log.e(LOG_TAG,
344                                         "OcExcepion occured! " + e.toString());
345                             }
346                         }
347                         if (messageCount == 3) {
348                             msg = Message.obtain();
349                             msg.what = 0;
350                             mHandler.sendMessage(msg);
351                         }
352                     }
353                 });
354     }
355
356     private void setConfigurationListener() {
357         thingsManager.setConfigurationListener(new IConfigurationListener() {
358             @Override
359             public void onBootStrapCallback(
360                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
361                     int errorValue) {
362                 Log.i(LOG_TAG, "onBootStrapCallback: enter");
363             }
364
365             @Override
366             public void onUpdateConfigurationsCallback(
367                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
368                     int errorValue) {
369                 Log.i(LOG_TAG, "onUpdateConfigurationsCallback: enter");
370                 Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
371                 if (rep.hasAttribute("loc")) {
372                     logMessage = logMessage + "Location : "
373                             + rep.getValueString("loc") + "\n";
374                 }
375                 if (rep.hasAttribute("st")) {
376                     logMessage = logMessage + "System Time : "
377                             + rep.getValueString("loc") + "\n";
378                 }
379                 if (rep.hasAttribute("c")) {
380                     logMessage = logMessage + "Currency : "
381                             + rep.getValueString("c") + "\n";
382                 }
383                 if (rep.hasAttribute("r")) {
384                     logMessage = logMessage + "Region : "
385                             + rep.getValueString("r") + "\n";
386                 }
387
388             }
389
390             @Override
391             public void onGetConfigurationsCallback(
392                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
393                     int errorValue) {
394                 Log.i(LOG_TAG, "onGetConfigurationsCallback: enter");
395                 Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
396                 logMessage = logMessage + "Resource URI : " + rep.getUri()
397                         + "\n";
398
399                 if (rep.hasAttribute("loc")) {
400                     logMessage = logMessage + "Location : "
401                             + rep.getValueString("loc") + "\n";
402                 }
403                 if (rep.hasAttribute("st")) {
404                     logMessage = logMessage + "System Time : "
405                             + rep.getValueString("st") + "\n";
406                 }
407                 if (rep.hasAttribute("c")) {
408                     logMessage = logMessage + "Currency : "
409                             + rep.getValueString("c") + "\n";
410                 }
411                 if (rep.hasAttribute("r")) {
412                     logMessage = logMessage + "Region : "
413                             + rep.getValueString("r") + "\n";
414                 }
415
416                 msg = Message.obtain();
417                 msg.what = 0;
418                 mHandler.sendMessage(msg);
419             }
420         });
421     }
422
423     private void setDiagnosticsListener() {
424         thingsManager.setDiagnosticsListener(new IDiagnosticsListener() {
425
426             @Override
427             public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
428                     OcRepresentation rep, int errorValue) {
429                 Log.i(LOG_TAG, "onRebootCallback: enter");
430             }
431
432             @Override
433             public void onFactoryResetCallback(
434                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
435                     int errorValue) {
436                 Log.i(LOG_TAG, "onFactoryResetCallback: enter");
437             }
438         });
439     }
440
441     /**
442      * This method find the resources available in network.
443      */
444     private void findCandidateResources(Vector<String> resourceTypes) {
445         OCStackResult result = thingsManager.findCandidateResources(
446                 resourceTypes, 5);
447         if (OCStackResult.OC_STACK_OK != result) {
448             Log.e(LOG_TAG, "Error while calling findCandidateResources");
449             String toastmessage = "findCandidateResources API returned error! ["
450                     + result + "]";
451             displayToastMessage(toastmessage);
452         }
453         if (messageCount == 1)
454             logMessage = logMessage + "API RESULT : " + result.toString()
455                     + "\n";
456     }
457
458     /**
459      * This method gets the configuration data from con-server.
460      */
461     private void getConfiguration() {
462         Log.i(LOG_TAG, "There are " + resourceList.size()
463                 + " servers present in network");
464         ResourceInformation configurationCollection = collectionList
465                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
466         if (null == configurationCollection
467                 || null == configurationCollection.resource) {
468             Log.e(LOG_TAG, "configuration collection resource doest not exist!");
469             displayToastMessage("Configuration collection resource does not exist!");
470             return;
471         }
472         if (false == configurationResourceFlag) {
473
474             Log.e(LOG_TAG, "configuration resource doest not exist!");
475             displayToastMessage("Configuration resource does not exist!");
476             return;
477         }
478
479         String name = "all";
480         Vector<String> configs = new Vector<String>();
481         configs.add(name);
482
483         OCStackResult result = OCStackResult.OC_STACK_ERROR;
484         try {
485             result = thingsManager.getConfigurations(
486                     configurationCollection.resource, configs);
487         } catch (OcException e) {
488             e.printStackTrace();
489         }
490
491         if (OCStackResult.OC_STACK_OK != result) {
492             Log.e(LOG_TAG, "getConfigurations failed!");
493             String toastmessage = "getConfigurations API returned error! ["
494                     + result + "]";
495             displayToastMessage(toastmessage);
496         }
497         logMessage = "API RESULT : " + result.toString() + "\n";
498         msg = Message.obtain();
499         msg.what = 0;
500         mHandler.sendMessage(msg);
501     }
502
503     /**
504      * This method update the configuration resource region attribute to given
505      * value.
506      *
507      * @param region
508      */
509     private void updateConfiguration(String region) {
510         ResourceInformation configurationCollection = collectionList
511                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
512         String name = "r";
513         Map<String, String> configurations = new HashMap<String, String>();
514
515         try {
516             configurations.put(name, region);
517         } catch (Exception e) {
518             Log.e(LOG_TAG, "Exception occured! " + e.toString());
519         }
520
521         OCStackResult result = OCStackResult.OC_STACK_ERROR;
522         try {
523             result = thingsManager.updateConfigurations(
524                     configurationCollection.resource, configurations);
525         } catch (OcException e) {
526             e.printStackTrace();
527         }
528         if (OCStackResult.OC_STACK_OK != result) {
529             Log.e(LOG_TAG, "updateConfigurations failed!");
530             String toastmessage = "updateConfigurations API returned error! ["
531                     + result + "]";
532             displayToastMessage(toastmessage);
533         }
534         logMessage = "API RESULT : " + result.toString() + "\n";
535         logMessage = logMessage + "Updating region to " + region;
536         msg = Message.obtain();
537         msg.what = 0;
538         mHandler.sendMessage(msg);
539     }
540
541     /**
542      * This method send request to reset all the configuration attribute values
543      * to default.
544      */
545     private void factoryReset() {
546         ResourceInformation diagnosticsCollection = collectionList
547                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
548         if (null == diagnosticsCollection
549                 || null == diagnosticsCollection.resource) {
550             Log.e(LOG_TAG, "Diagnostic collection doest not exist!");
551             displayToastMessage("Diagnostic collection does not exist!");
552             return;
553         }
554
555         if (false == diagnosticsResourceFlag) {
556             Log.e(LOG_TAG, "Diagnostic resource doest not exist!");
557             displayToastMessage("Diagnostic resource does not exist!");
558             return;
559         }
560
561         OCStackResult result = OCStackResult.values()[30];
562
563         try {
564             result = thingsManager.factoryReset(diagnosticsCollection.resource);
565         } catch (OcException e) {
566             e.printStackTrace();
567         }
568
569         if (OCStackResult.OC_STACK_OK != result) {
570             Log.e(LOG_TAG, "factoryReset failed!");
571             String toastmessage = "factoryReset API returned error! [" + result
572                     + "]";
573             displayToastMessage(toastmessage);
574
575         }
576         logMessage = "API RESULT : " + result.toString() + "\n";
577         msg = Message.obtain();
578         msg.what = 0;
579         mHandler.sendMessage(msg);
580     }
581
582     /**
583      * This method send request to reboot server.
584      */
585     private void reboot() {
586         ResourceInformation diagnosticsCollection = collectionList
587                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
588         if (null == diagnosticsCollection
589                 || null == diagnosticsCollection.resource) {
590             Log.e(LOG_TAG, "Diagnostic collection doest not exist!");
591             displayToastMessage("Diagnostic collection does not exist!");
592             return;
593         }
594         if (false == diagnosticsResourceFlag) {
595             Log.e(LOG_TAG, "Diagnostic resource doest not exist!");
596             displayToastMessage("Diagnostic resource does not exist!");
597             return;
598         }
599
600         OCStackResult result = OCStackResult.OC_STACK_ERROR;
601         try {
602             result = thingsManager.reboot(diagnosticsCollection.resource);
603         } catch (OcException e) {
604             e.printStackTrace();
605         }
606
607         if (OCStackResult.OC_STACK_OK != result) {
608             Log.e(LOG_TAG, "reboot failed!");
609             String toastmessage = "reboot API returned error! [" + result + "]";
610             displayToastMessage(toastmessage);
611         }
612         logMessage = "API RESULT : " + result.toString() + "\n";
613         msg = Message.obtain();
614         msg.what = 0;
615         mHandler.sendMessage(msg);
616     }
617
618     /**
619      * This method is for getting list of all supported configuration values.
620      * Response will be in JSON format (key-value pair).
621      */
622     private String getListOfSupportedConfigurationUnits() {
623         return thingsManager.getListOfSupportedConfigurationUnits();
624     }
625
626     private void displayToastMessage(String message) {
627         Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
628         toast.show();
629     }
630
631     private void collectionFound(OcResource resource) {
632         String uri = resource.getUri();
633         ResourceInformation resourceInfo = collectionList.get(uri);
634         if (null == resourceInfo) {
635             Log.e(LOG_TAG, "Collection is not created!");
636             return;
637         }
638
639         if (null != resourceInfo.resource) {
640             Log.e(LOG_TAG, "Collection resource already updated!");
641             return;
642         }
643
644         resourceInfo.resource = resource;
645         if (3 == messageCount) {
646             findGroupPressedFlag = true;
647         }
648     }
649
650     /**
651      * This callback will be invoked when the interested resource discovered in
652      * network.
653      */
654     private void resourceFound(OcResource resource) throws OcException {
655         String uri = resource.getUri();
656         String host = resource.getHost();
657
658         // Check if the resource already exist in the map table
659         ResourceInformation resourceInfo = resourceList.get(uri + host);
660         if (null != resourceInfo) {
661             Log.e(LOG_TAG, "Resource already exists!");
662             return;
663         }
664
665         // Create resource
666         resourceInfo = new ResourceInformation();
667         resourceInfo.resource = resource;
668         if (uri.equalsIgnoreCase("/oic/con")) {
669             ResourceInformation collectionResource = collectionList
670                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
671             if (null == collectionResource
672                     || null == collectionResource.resourceHandle) {
673                 Log.e(LOG_TAG, "Invalid Configuration collection!");
674                 return;
675             }
676
677             ResourceInformation resourceInfoCollection;
678             resourceInfoCollection = collectionList
679                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
680             OcResourceHandle handle;
681             handle = resourceInfoCollection.resourceHandle;
682             resourceInfo.resourceHandle = handle;
683             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
684                     resource, handle);
685
686             resourceList.put(uri + host, resourceInfo);
687             configurationResourceFlag = true;
688         } else if (uri.equalsIgnoreCase("/oic/diag")) {
689             ResourceInformation diagnosticResource = collectionList
690                     .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
691             if (null == diagnosticResource
692                     || null == diagnosticResource.resourceHandle) {
693                 Log.e(LOG_TAG, "Invalid Configuration collection!");
694                 return;
695             }
696
697             ResourceInformation resourceInfoCollection;
698             resourceInfoCollection = collectionList
699                     .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
700             OcResourceHandle handle;
701             handle = resourceInfoCollection.resourceHandle;
702             resourceInfo.resourceHandle = handle;
703             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
704                     resource, handle);
705
706             resourceList.put(uri + host, resourceInfo);
707             diagnosticsResourceFlag = true;
708         } else if (uri.equalsIgnoreCase("/factorySet")) {
709             ResourceInformation factorysetResource = collectionList
710                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
711             if (null == factorysetResource
712                     || null == factorysetResource.resourceHandle) {
713                 Log.e(LOG_TAG, "Invalid Configuration collection!");
714                 return;
715             }
716
717             ResourceInformation resourceInfoCollection;
718             resourceInfoCollection = collectionList
719                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
720             OcResourceHandle handle;
721             handle = resourceInfoCollection.resourceHandle;
722             resourceInfo.resourceHandle = handle;
723             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
724                     resource, handle);
725
726             resourceList.put(uri + host, resourceInfo);
727             factorySetResourceFlag = true;
728         } else {
729             Log.e(LOG_TAG, "Resource is of different type: " + uri);
730             return;
731         }
732     }
733
734     private void createResourceCollection(String uri, String typename)
735             throws OcException {
736
737         Map<String, OcResourceHandle> groupList = new HashMap<String, OcResourceHandle>();
738
739         Log.i(LOG_TAG, "createGroup: " + typename);
740         // Check if the resource collection is already created
741         if (null != collectionList.get(uri)) {
742             Log.e(LOG_TAG, "Collection is already exist!");
743             return;
744         }
745
746         OcResourceHandle resourceHandle = null;
747
748         // Crate group
749         OCStackResult result = thingsManager.createGroup(typename);
750         if ((OCStackResult.OC_STACK_OK != result)) {
751             Log.e(LOG_TAG, "createGroup returned error: " + result.name());
752             return;
753         } else {
754             Log.e(LOG_TAG, "createGroup returned: " + result.name());
755         }
756         groupList = thingsManager.getGroupList();
757         if (groupList.containsKey(typename)) {
758             resourceHandle = groupList.get(typename);
759         } else {
760             Log.e(LOG_TAG, "group does not contain groupResourceType: "
761                     + result.name());
762         }
763
764         if (null == resourceHandle) {
765             Log.e(LOG_TAG, " createResourceCollection : resourceHandle is NULL");
766
767         }
768         // Add the collection resource to map table
769         ResourceInformation resourceInfo = new ResourceInformation();
770         resourceInfo.resourceHandle = resourceHandle;
771         collectionList.put(uri, resourceInfo);
772         Log.i(LOG_TAG, "size of collectionList : " + collectionList.size());
773     }
774
775     private void deleteResources() {
776         Log.i(LOG_TAG, "deleteResources: enter");
777         try {
778             // unregister all resources
779             for (ResourceInformation resource : resourceList.values()) {
780                 if (null != resource.resourceHandle) {
781                     if (resource.resource.getUri().equalsIgnoreCase(
782                             CONFIGURATION_RESOURCE_URI)) {
783                         ResourceInformation collectionResource = collectionList
784                                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
785                         if (null != collectionResource
786                                 && null != collectionResource.resourceHandle) {
787                             OcPlatform
788                                     .unregisterResource(resource.resourceHandle);
789                             Log.i(LOG_TAG, "unregistered resource"
790                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
791
792                         }
793                     } else if (resource.resource.getUri().equalsIgnoreCase(
794                             DIAGNOSTIC_RESOURCE_URI)) {
795                         ResourceInformation diagnosticResource = collectionList
796                                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
797                         if (null != diagnosticResource
798                                 && null != diagnosticResource.resourceHandle) {
799                             OcPlatform
800                                     .unregisterResource(resource.resourceHandle);
801                             Log.i(LOG_TAG, "unregistered resource"
802                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
803                         }
804                     } else if (resource.resource.getUri().equalsIgnoreCase(
805                             FACTORYSET_RESOURCE_URI)) {
806                         ResourceInformation factorysetResource = collectionList
807                                 .get(FACTORYSET_COLLECTION_RESOURCE_URI);
808                         if (null != factorysetResource
809                                 && null != factorysetResource.resourceHandle) {
810                             OcPlatform
811                                     .unregisterResource(resource.resourceHandle);
812                             Log.i(LOG_TAG, "unregistered resource"
813                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
814
815                         }
816                     }
817                 }
818             }
819
820             // delete all the groups
821             thingsManager.deleteGroup(CONFIGURATION_COLLECTION_RESOURCE_TYPE);
822             thingsManager.deleteGroup(DIAGNOSTIC_COLLECTION_RESOURCE_TYPE);
823             thingsManager.deleteGroup(FACTORYSET_COLLECTION_RESOURCE_TYPE);
824         } catch (OcException e) {
825             Log.e(LOG_TAG, "OcException occured! " + e.toString());
826         }
827     }
828 }