Imported Upstream version 0.9.2
[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                         Log.i(LOG_TAG, logMessage);
231                 }
232             }
233         };
234
235     }
236
237     private void userInputDialog() {
238
239         ResourceInformation configurationCollection = collectionList
240                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
241         if (null == configurationCollection
242                 || null == configurationCollection.resource) {
243             displayToastMessage("Configuration collection resource does not exist!");
244         }
245         if (false == configurationResourceFlag) {
246             displayToastMessage("Configuration resource does not exist!");
247         } else {
248             final Dialog dialog = new Dialog(mcontext);
249             dialog.setContentView(R.layout.userinputforregionvalue);
250             dialog.setTitle("Enter the Region Value");
251
252             dialog.setCancelable(false);
253             dialog.show();
254             Button ok = (Button) dialog.findViewById(R.id.ok);
255             Button cancel = (Button) dialog.findViewById(R.id.cancel);
256
257             ok.setOnClickListener(new OnClickListener() {
258                 @Override
259                 public void onClick(View v) {
260
261                     EditText regionValue = (EditText) dialog
262                             .findViewById(R.id.region);
263                     region = regionValue.getText().toString();
264                     if (region.equalsIgnoreCase("")) {
265                         String toastmessage = "Please enter the Region Value";
266                         displayToastMessage(toastmessage);
267                     } else {
268                         dialog.dismiss();
269                         updateConfiguration(region);
270                     }
271                 }
272             });
273             cancel.setOnClickListener(new OnClickListener() {
274                 @Override
275                 public void onClick(View v) {
276                     dialog.dismiss();
277                 }
278             });
279         }
280     }
281
282     @Override
283     public void onBackPressed() {
284         super.onBackPressed();
285
286         // unregistering resource from the platform
287         deleteResources();
288         resourceList.clear();
289         resourceList = null;
290         collectionList.clear();
291         collectionList = null;
292     }
293
294     private void setResourceListener() {
295         thingsManager
296                 .setFindCandidateResourceListener(new IFindCandidateResourceListener() {
297
298                     @Override
299                     synchronized public void onResourceCallback(
300                             Vector<OcResource> resources) {
301                         Log.i(LOG_TAG, "onResourceCallback: enter");
302                         for (int i = 0; i < resources.size(); i++) {
303                             OcResource resource = resources.get(i);
304                             String uri = resource.getUri();
305                             String host = resource.getHost();
306
307                             // Display resource information
308                             Log.i(LOG_TAG, "Resource URI:" + uri);
309                             Log.i(LOG_TAG, "Resource HOST: " + host);
310                             Log.i(LOG_TAG, "Resource types: ");
311                             logMessage = logMessage + "Resource URI : " + uri
312                                     + "\n";
313                             logMessage = logMessage + "Resource Host : " + host
314                                     + "\n";
315
316                             List<String> resourcetypes = resource
317                                     .getResourceTypes();
318                             for (int j = 0; j < resourcetypes.size(); j++) {
319                                 Log.i(LOG_TAG, resourcetypes.get(j));
320                                 logMessage = logMessage + "ResourceType "
321                                         + (j + 1) + " : "
322                                         + resourcetypes.get(j) + "\n";
323                             }
324
325                             Log.i(LOG_TAG, "Interface types: ");
326                             List<String> interfacetypes = resource
327                                     .getResourceInterfaces();
328                             for (int j = 0; j < interfacetypes.size(); j++) {
329                                 Log.i(LOG_TAG, interfacetypes.get(j));
330                                 logMessage = logMessage + "interfacetype "
331                                         + (j + 1) + " : "
332                                         + interfacetypes.get(j) + "\n";
333                             }
334
335                             try {
336                                 if (true == uri.contains("/resourceset")) {
337                                     collectionFound(resource);
338                                 } else {
339                                     resourceFound(resource);
340                                 }
341                             } catch (OcException e) {
342                                 Log.e(LOG_TAG,
343                                         "OcExcepion occured! " + e.toString());
344                             }
345                         }
346                         if (messageCount == 3) {
347                             msg = Message.obtain();
348                             msg.what = 0;
349                             mHandler.sendMessage(msg);
350                         }
351                     }
352                 });
353     }
354
355     private void setConfigurationListener() {
356         thingsManager.setConfigurationListener(new IConfigurationListener() {
357             @Override
358             public void onBootStrapCallback(
359                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
360                     int errorValue) {
361                 Log.i(LOG_TAG, "onBootStrapCallback: enter");
362             }
363
364             @Override
365             public void onUpdateConfigurationsCallback(
366                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
367                     int errorValue) {
368                 Log.i(LOG_TAG, "onUpdateConfigurationsCallback: enter");
369                 Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
370                 if (rep.hasAttribute("loc")) {
371                     logMessage = logMessage + "Location : "
372                             + rep.getValueString("loc") + "\n";
373                 }
374                 if (rep.hasAttribute("st")) {
375                     logMessage = logMessage + "System Time : "
376                             + rep.getValueString("loc") + "\n";
377                 }
378                 if (rep.hasAttribute("c")) {
379                     logMessage = logMessage + "Currency : "
380                             + rep.getValueString("c") + "\n";
381                 }
382                 if (rep.hasAttribute("r")) {
383                     logMessage = logMessage + "Region : "
384                             + rep.getValueString("r") + "\n";
385                 }
386
387             }
388
389             @Override
390             public void onGetConfigurationsCallback(
391                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
392                     int errorValue) {
393                 Log.i(LOG_TAG, "onGetConfigurationsCallback: enter");
394                 Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
395                 logMessage = logMessage + "Resource URI : " + rep.getUri()
396                         + "\n";
397
398                 if (rep.hasAttribute("loc")) {
399                     logMessage = logMessage + "Location : "
400                             + rep.getValueString("loc") + "\n";
401                 }
402                 if (rep.hasAttribute("st")) {
403                     logMessage = logMessage + "System Time : "
404                             + rep.getValueString("st") + "\n";
405                 }
406                 if (rep.hasAttribute("c")) {
407                     logMessage = logMessage + "Currency : "
408                             + rep.getValueString("c") + "\n";
409                 }
410                 if (rep.hasAttribute("r")) {
411                     logMessage = logMessage + "Region : "
412                             + rep.getValueString("r") + "\n";
413                 }
414
415                 msg = Message.obtain();
416                 msg.what = 0;
417                 mHandler.sendMessage(msg);
418             }
419         });
420     }
421
422     private void setDiagnosticsListener() {
423         thingsManager.setDiagnosticsListener(new IDiagnosticsListener() {
424
425             @Override
426             public void onRebootCallback(Vector<OcHeaderOption> headerOptions,
427                     OcRepresentation rep, int errorValue) {
428                 Log.i(LOG_TAG, "onRebootCallback: enter");
429             }
430
431             @Override
432             public void onFactoryResetCallback(
433                     Vector<OcHeaderOption> headerOptions, OcRepresentation rep,
434                     int errorValue) {
435                 Log.i(LOG_TAG, "onFactoryResetCallback: enter");
436             }
437         });
438     }
439
440     /**
441      * This method find the resources available in network.
442      */
443     private void findCandidateResources(Vector<String> resourceTypes) {
444         OCStackResult result = thingsManager.findCandidateResources(
445                 resourceTypes, 5);
446         if (OCStackResult.OC_STACK_OK != result) {
447             Log.e(LOG_TAG, "Error while calling findCandidateResources");
448             String toastmessage = "findCandidateResources API returned error! ["
449                     + result + "]";
450             displayToastMessage(toastmessage);
451         }
452         if (messageCount == 1)
453             logMessage = logMessage + "API RESULT : " + result.toString()
454                     + "\n";
455     }
456
457     /**
458      * This method gets the configuration data from con-server.
459      */
460     private void getConfiguration() {
461         Log.i(LOG_TAG, "There are " + resourceList.size()
462                 + " servers present in network");
463         ResourceInformation configurationCollection = collectionList
464                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
465         if (null == configurationCollection
466                 || null == configurationCollection.resource) {
467             displayToastMessage("Configuration collection resource does not exist!");
468             return;
469         }
470         if (false == configurationResourceFlag) {
471
472             displayToastMessage("Configuration resource does not exist!");
473             return;
474         }
475
476         String name = "all";
477         Vector<String> configs = new Vector<String>();
478         configs.add(name);
479
480         OCStackResult result = OCStackResult.OC_STACK_ERROR;
481         try {
482             result = thingsManager.getConfigurations(
483                     configurationCollection.resource, configs);
484         } catch (OcException e) {
485             e.printStackTrace();
486         }
487
488         if (OCStackResult.OC_STACK_OK != result) {
489             Log.e(LOG_TAG, "getConfigurations failed!");
490             String toastmessage = "getConfigurations API returned error! ["
491                     + result + "]";
492             displayToastMessage(toastmessage);
493         }
494         logMessage = "API RESULT : " + result.toString() + "\n";
495         msg = Message.obtain();
496         msg.what = 0;
497         mHandler.sendMessage(msg);
498     }
499
500     /**
501      * This method update the configuration resource region attribute to given
502      * value.
503      *
504      * @param region
505      */
506     private void updateConfiguration(String region) {
507         ResourceInformation configurationCollection = collectionList
508                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
509         String name = "r";
510         Map<String, String> configurations = new HashMap<String, String>();
511
512         try {
513             configurations.put(name, region);
514         } catch (Exception e) {
515             Log.e(LOG_TAG, "Exception occured! " + e.toString());
516         }
517
518         OCStackResult result = OCStackResult.OC_STACK_ERROR;
519         try {
520             result = thingsManager.updateConfigurations(
521                     configurationCollection.resource, configurations);
522         } catch (OcException e) {
523             e.printStackTrace();
524         }
525         if (OCStackResult.OC_STACK_OK != result) {
526             Log.e(LOG_TAG, "updateConfigurations failed!");
527             String toastmessage = "updateConfigurations API returned error! ["
528                     + result + "]";
529             displayToastMessage(toastmessage);
530         }
531         logMessage = "API RESULT : " + result.toString() + "\n";
532         logMessage = logMessage + "Updating region to " + region;
533         msg = Message.obtain();
534         msg.what = 0;
535         mHandler.sendMessage(msg);
536     }
537
538     /**
539      * This method send request to reset all the configuration attribute values
540      * to default.
541      */
542     private void factoryReset() {
543         ResourceInformation diagnosticsCollection = collectionList
544                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
545         if (null == diagnosticsCollection
546                 || null == diagnosticsCollection.resource) {
547             displayToastMessage("Diagnostic collection does not exist!");
548             return;
549         }
550
551         if (false == diagnosticsResourceFlag) {
552             displayToastMessage("Diagnostic resource does not exist!");
553             return;
554         }
555
556         OCStackResult result = OCStackResult.values()[30];
557
558         try {
559             result = thingsManager.factoryReset(diagnosticsCollection.resource);
560         } catch (OcException e) {
561             e.printStackTrace();
562         }
563
564         if (OCStackResult.OC_STACK_OK != result) {
565             Log.e(LOG_TAG, "factoryReset failed!");
566             String toastmessage = "factoryReset API returned error! [" + result
567                     + "]";
568             displayToastMessage(toastmessage);
569
570         }
571         logMessage = "API RESULT : " + result.toString() + "\n";
572         msg = Message.obtain();
573         msg.what = 0;
574         mHandler.sendMessage(msg);
575     }
576
577     /**
578      * This method send request to reboot server.
579      */
580     private void reboot() {
581         ResourceInformation diagnosticsCollection = collectionList
582                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
583         if (null == diagnosticsCollection
584                 || null == diagnosticsCollection.resource) {
585             displayToastMessage("Diagnostic collection does not exist!");
586             return;
587         }
588         if (false == diagnosticsResourceFlag) {
589             displayToastMessage("Diagnostic resource does not exist!");
590             return;
591         }
592
593         OCStackResult result = OCStackResult.OC_STACK_ERROR;
594         try {
595             result = thingsManager.reboot(diagnosticsCollection.resource);
596         } catch (OcException e) {
597             e.printStackTrace();
598         }
599
600         if (OCStackResult.OC_STACK_OK != result) {
601             Log.e(LOG_TAG, "reboot failed!");
602             String toastmessage = "reboot API returned error! [" + result + "]";
603             displayToastMessage(toastmessage);
604         }
605         logMessage = "API RESULT : " + result.toString() + "\n";
606         msg = Message.obtain();
607         msg.what = 0;
608         mHandler.sendMessage(msg);
609     }
610
611     /**
612      * This method is for getting list of all supported configuration values.
613      * Response will be in JSON format (key-value pair).
614      */
615     private String getListOfSupportedConfigurationUnits() {
616         return thingsManager.getListOfSupportedConfigurationUnits();
617     }
618
619     private void displayToastMessage(String message) {
620         Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
621         toast.show();
622         Log.i(LOG_TAG, message);
623     }
624
625     private void collectionFound(OcResource resource) {
626         String uri = resource.getUri();
627         ResourceInformation resourceInfo = collectionList.get(uri);
628         if (null == resourceInfo) {
629             Log.e(LOG_TAG, "Collection is not created!");
630             return;
631         }
632
633         if (null != resourceInfo.resource) {
634             Log.e(LOG_TAG, "Collection resource already updated!");
635             return;
636         }
637
638         resourceInfo.resource = resource;
639         if (3 == messageCount) {
640             findGroupPressedFlag = true;
641         }
642     }
643
644     /**
645      * This callback will be invoked when the interested resource discovered in
646      * network.
647      */
648     private void resourceFound(OcResource resource) throws OcException {
649         String uri = resource.getUri();
650         String host = resource.getHost();
651
652         // Check if the resource already exist in the map table
653         ResourceInformation resourceInfo = resourceList.get(uri + host);
654         if (null != resourceInfo) {
655             Log.e(LOG_TAG, "Resource already exists!");
656             return;
657         }
658
659         // Create resource
660         resourceInfo = new ResourceInformation();
661         resourceInfo.resource = resource;
662         if (uri.equalsIgnoreCase("/oic/con")) {
663             ResourceInformation collectionResource = collectionList
664                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
665             if (null == collectionResource
666                     || null == collectionResource.resourceHandle) {
667                 Log.e(LOG_TAG, "Invalid Configuration collection!");
668                 return;
669             }
670
671             ResourceInformation resourceInfoCollection;
672             resourceInfoCollection = collectionList
673                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
674             OcResourceHandle handle;
675             handle = resourceInfoCollection.resourceHandle;
676             resourceInfo.resourceHandle = handle;
677             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
678                     resource, handle);
679
680             resourceList.put(uri + host, resourceInfo);
681             configurationResourceFlag = true;
682         } else if (uri.equalsIgnoreCase("/oic/diag")) {
683             ResourceInformation diagnosticResource = collectionList
684                     .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
685             if (null == diagnosticResource
686                     || null == diagnosticResource.resourceHandle) {
687                 Log.e(LOG_TAG, "Invalid Configuration collection!");
688                 return;
689             }
690
691             ResourceInformation resourceInfoCollection;
692             resourceInfoCollection = collectionList
693                     .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
694             OcResourceHandle handle;
695             handle = resourceInfoCollection.resourceHandle;
696             resourceInfo.resourceHandle = handle;
697             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
698                     resource, handle);
699
700             resourceList.put(uri + host, resourceInfo);
701             diagnosticsResourceFlag = true;
702         } else if (uri.equalsIgnoreCase("/factorySet")) {
703             ResourceInformation factorysetResource = collectionList
704                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
705             if (null == factorysetResource
706                     || null == factorysetResource.resourceHandle) {
707                 Log.e(LOG_TAG, "Invalid Configuration collection!");
708                 return;
709             }
710
711             ResourceInformation resourceInfoCollection;
712             resourceInfoCollection = collectionList
713                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
714             OcResourceHandle handle;
715             handle = resourceInfoCollection.resourceHandle;
716             resourceInfo.resourceHandle = handle;
717             resourceInfo.resourceHandle = thingsManager.bindResourceToGroup(
718                     resource, handle);
719
720             resourceList.put(uri + host, resourceInfo);
721             factorySetResourceFlag = true;
722         } else {
723             Log.e(LOG_TAG, "Resource is of different type: " + uri);
724             return;
725         }
726     }
727
728     private void createResourceCollection(String uri, String typename)
729             throws OcException {
730
731         Map<String, OcResourceHandle> groupList = new HashMap<String, OcResourceHandle>();
732
733         Log.i(LOG_TAG, "createGroup: " + typename);
734         // Check if the resource collection is already created
735         if (null != collectionList.get(uri)) {
736             Log.e(LOG_TAG, "Collection is already exist!");
737             return;
738         }
739
740         OcResourceHandle resourceHandle = null;
741
742         // Crate group
743         OCStackResult result = thingsManager.createGroup(typename);
744         if ((OCStackResult.OC_STACK_OK != result)) {
745             Log.e(LOG_TAG, "createGroup returned error: " + result.name());
746             return;
747         } else {
748             Log.e(LOG_TAG, "createGroup returned: " + result.name());
749         }
750         groupList = thingsManager.getGroupList();
751         if (groupList.containsKey(typename)) {
752             resourceHandle = groupList.get(typename);
753         } else {
754             Log.e(LOG_TAG, "group does not contain groupResourceType: "
755                     + result.name());
756         }
757
758         if (null == resourceHandle) {
759             Log.e(LOG_TAG, " createResourceCollection : resourceHandle is NULL");
760
761         }
762         // Add the collection resource to map table
763         ResourceInformation resourceInfo = new ResourceInformation();
764         resourceInfo.resourceHandle = resourceHandle;
765         collectionList.put(uri, resourceInfo);
766         Log.i(LOG_TAG, "size of collectionList : " + collectionList.size());
767     }
768
769     private void deleteResources() {
770         Log.i(LOG_TAG, "deleteResources: enter");
771         try {
772             // unregister all resources
773             for (ResourceInformation resource : resourceList.values()) {
774                 if (null != resource.resourceHandle) {
775                     if (resource.resource.getUri().equalsIgnoreCase(
776                             CONFIGURATION_RESOURCE_URI)) {
777                         ResourceInformation collectionResource = collectionList
778                                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
779                         if (null != collectionResource
780                                 && null != collectionResource.resourceHandle) {
781                             OcPlatform
782                                     .unregisterResource(resource.resourceHandle);
783                             Log.i(LOG_TAG, "unregistered resource"
784                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
785
786                         }
787                     } else if (resource.resource.getUri().equalsIgnoreCase(
788                             DIAGNOSTIC_RESOURCE_URI)) {
789                         ResourceInformation diagnosticResource = collectionList
790                                 .get(DIAGNOSTIC_COLLECTION_RESOURCE_URI);
791                         if (null != diagnosticResource
792                                 && null != diagnosticResource.resourceHandle) {
793                             OcPlatform
794                                     .unregisterResource(resource.resourceHandle);
795                             Log.i(LOG_TAG, "unregistered resource"
796                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
797                         }
798                     } else if (resource.resource.getUri().equalsIgnoreCase(
799                             FACTORYSET_RESOURCE_URI)) {
800                         ResourceInformation factorysetResource = collectionList
801                                 .get(FACTORYSET_COLLECTION_RESOURCE_URI);
802                         if (null != factorysetResource
803                                 && null != factorysetResource.resourceHandle) {
804                             OcPlatform
805                                     .unregisterResource(resource.resourceHandle);
806                             Log.i(LOG_TAG, "unregistered resource"
807                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
808
809                         }
810                     }
811                 }
812             }
813
814             // delete all the groups
815             thingsManager.deleteGroup(CONFIGURATION_COLLECTION_RESOURCE_TYPE);
816             thingsManager.deleteGroup(DIAGNOSTIC_COLLECTION_RESOURCE_TYPE);
817             thingsManager.deleteGroup(FACTORYSET_COLLECTION_RESOURCE_TYPE);
818         } catch (OcException e) {
819             Log.e(LOG_TAG, "OcException occured! " + e.toString());
820         }
821     }
822 }