[Resource-Encapsulation] Android Sample Application for client side APIs.
authorJay Sharma <jay.sharma@samsung.com>
Tue, 25 Aug 2015 15:59:42 +0000 (21:29 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 28 Aug 2015 05:22:11 +0000 (05:22 +0000)
Change-Id: I1605fe00027c8bd6d2182a70e3dbb35f2d6c2b2d
Signed-off-by: Jay Sharma <jay.sharma@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2267
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
23 files changed:
service/resource-encapsulation/examples/android/SampleResourceClientApp/.classpath [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/.project [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/AndroidManifest.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/ic_launcher-web.png [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/project.properties [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/ic_launcher.png [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/iot.jpg [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-mdpi/ic_launcher.png [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xhdpi/ic_launcher.png [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xxhdpi/ic_launcher.png [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/activity_main.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/resourceclientapis.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/userinputforattributevalue.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/menu/main.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v11/styles.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v14/styles.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-w820dp/dimens.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/dimens.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/strings.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/styles.xml [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/MainActivity.java [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java [new file with mode: 0644]
service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClientActivity.java [new file with mode: 0644]

diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/.classpath b/service/resource-encapsulation/examples/android/SampleResourceClientApp/.classpath
new file mode 100644 (file)
index 0000000..3e9f007
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+       <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
+       <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
+       <classpathentry combineaccessrules="false" exported="true" kind="src" path="/java"/>
+       <classpathentry exported="true" kind="lib" path="E:/Projects/Mixed_backup/master_22_aug/android/android_api/base/build/intermediates/bundles/release/classes.jar"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="src" path="gen"/>
+       <classpathentry kind="output" path="bin/classes"/>
+</classpath>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/.project b/service/resource-encapsulation/examples/android/SampleResourceClientApp/.project
new file mode 100644 (file)
index 0000000..4aabe36
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>SampleResourceClientApp</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>com.android.ide.eclipse.adt.ApkBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/AndroidManifest.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/AndroidManifest.xml
new file mode 100644 (file)
index 0000000..63c3ff9
--- /dev/null
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.re.sampleclient"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+   <uses-sdk
+        android:minSdkVersion="8"
+        android:targetSdkVersion="21" />
+
+    <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+
+    <application
+        android:allowBackup="true"
+        android:icon="@drawable/iot"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name="com.re.sampleclient.MainActivity"
+            android:label="@string/app_name" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name="com.re.sampleclient.ResourceClientActivity"
+            android:label="@string/app_name" >
+        </activity>
+        <activity
+            android:name="com.re.sampleclient.ConfigurationApiActivity"
+            android:label="@string/app_name" >  
+        </activity>
+    </application>
+
+</manifest>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/ic_launcher-web.png b/service/resource-encapsulation/examples/android/SampleResourceClientApp/ic_launcher-web.png
new file mode 100644 (file)
index 0000000..a18cbb4
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/ic_launcher-web.png differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/project.properties b/service/resource-encapsulation/examples/android/SampleResourceClientApp/project.properties
new file mode 100644 (file)
index 0000000..6ab28b0
--- /dev/null
@@ -0,0 +1,15 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=android-21
+android.library.reference.1=../../../sdk/java
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/ic_launcher.png b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..288b665
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/ic_launcher.png differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/iot.jpg b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/iot.jpg
new file mode 100644 (file)
index 0000000..17c5a93
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-hdpi/iot.jpg differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-mdpi/ic_launcher.png b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-mdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..6ae570b
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-mdpi/ic_launcher.png differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xhdpi/ic_launcher.png b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xhdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..d4fb7cd
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xxhdpi/ic_launcher.png b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..85a6081
Binary files /dev/null and b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/drawable-xxhdpi/ic_launcher.png differ
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/activity_main.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/activity_main.xml
new file mode 100644 (file)
index 0000000..ec14de4
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+     <ListView
+              android:id="@+id/list"
+              android:layout_height="wrap_content"
+              android:layout_width="match_parent">
+         </ListView>
+</RelativeLayout>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/resourceclientapis.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/resourceclientapis.xml
new file mode 100644 (file)
index 0000000..c5a0c15
--- /dev/null
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent" >
+    
+       <Button
+        android:id="@+id/button1"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentTop="true"
+        android:text="Discover Resource" />
+       
+       
+        <ListView
+            android:id="@+id/groupaApiList"
+            android:layout_width="match_parent"
+            android:layout_height="300dp" 
+             android:layout_below="@id/button1">
+         </ListView>
+         
+         <EditText
+        android:id="@+id/EditText"
+        android:layout_width="fill_parent"
+        android:layout_height="250dp"
+        android:layout_alignParentLeft="true"
+        android:layout_below="@+id/groupaApiList"
+        android:layout_marginTop="15dp"
+        android:ems="10" 
+        android:editable="false" >
+
+        <requestFocus />
+    </EditText>
+      
+        
+</RelativeLayout>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/userinputforattributevalue.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/layout/userinputforattributevalue.xml
new file mode 100644 (file)
index 0000000..50b5f08
--- /dev/null
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+
+      <EditText
+          android:id="@+id/attributeValue"
+          android:layout_width="wrap_content"
+          android:layout_height="wrap_content"
+          android:layout_alignParentTop="true"
+          android:layout_centerHorizontal="true"
+          android:layout_marginTop="28dp"
+          android:ems="10"
+          android:hint="Enter the Temperature" />
+
+      <Button
+          android:id="@+id/ok"
+          android:layout_width="150dp"
+          android:layout_height="wrap_content"
+          android:layout_alignParentLeft="true"
+          android:layout_below="@+id/region"
+          android:layout_marginTop="101dp"
+          android:text="OK" />
+
+       <Button
+           android:id="@+id/cancel"
+           android:layout_width="150dp"
+           android:layout_height="wrap_content"
+           android:layout_alignBaseline="@+id/ok"
+           android:layout_alignBottom="@+id/ok"
+           android:layout_alignParentRight="true"
+           android:text="Cancel" />
+
+</RelativeLayout>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/menu/main.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/menu/main.xml
new file mode 100644 (file)
index 0000000..c002028
--- /dev/null
@@ -0,0 +1,9 @@
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+
+    <item
+        android:id="@+id/action_settings"
+        android:orderInCategory="100"
+        android:showAsAction="never"
+        android:title="@string/action_settings"/>
+
+</menu>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v11/styles.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v11/styles.xml
new file mode 100644 (file)
index 0000000..1074513
--- /dev/null
@@ -0,0 +1,6 @@
+<resources>
+
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+    </style>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v14/styles.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-v14/styles.xml
new file mode 100644 (file)
index 0000000..2d1ebfe
--- /dev/null
@@ -0,0 +1,9 @@
+  <resources>
+
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+    </style>
+
+    <style name="AppTheme" parent="AppBaseTheme">
+    </style>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-w820dp/dimens.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values-w820dp/dimens.xml
new file mode 100644 (file)
index 0000000..f3e7020
--- /dev/null
@@ -0,0 +1,10 @@
+<resources>
+
+    <!--
+         Example customization of dimensions originally defined in res/values/dimens.xml
+         (such as screen margins) for screens with more than 820dp of available width. This
+         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
+    -->
+    <dimen name="activity_horizontal_margin">64dp</dimen>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/dimens.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/dimens.xml
new file mode 100644 (file)
index 0000000..55c1e59
--- /dev/null
@@ -0,0 +1,7 @@
+<resources>
+
+    <!-- Default screen margins, per the Android Design guidelines. -->
+    <dimen name="activity_horizontal_margin">16dp</dimen>
+    <dimen name="activity_vertical_margin">16dp</dimen>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/strings.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/strings.xml
new file mode 100644 (file)
index 0000000..3c310ab
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">RESampleClient</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="action_settings">Settings</string>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/styles.xml b/service/resource-encapsulation/examples/android/SampleResourceClientApp/res/values/styles.xml
new file mode 100644 (file)
index 0000000..3ccf2b0
--- /dev/null
@@ -0,0 +1,12 @@
+<resources>
+
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+
+    </style>
+
+</resources>
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/MainActivity.java b/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/MainActivity.java
new file mode 100644 (file)
index 0000000..36fde7c
--- /dev/null
@@ -0,0 +1,139 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+package com.re.sampleclient;
+
+import java.util.ArrayList;
+
+import org.iotivity.base.ModeType;
+import org.iotivity.base.OcPlatform;
+import org.iotivity.base.PlatformConfig;
+import org.iotivity.base.QualityOfService;
+import org.iotivity.base.ServiceType;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+/**
+ * Starting Activity of the application responsible for configuring the
+ * OcPlatform and redirecting to ResourceClient activity
+ */
+public class MainActivity extends Activity {
+
+    private static MainActivity  activityObj;
+    private ArrayAdapter<String> apis;
+    private ArrayList<String>    apisList;
+    private ListView             list;
+    private final String         LOG_TAG = "[ReSample] "
+                                                 + this.getClass()
+                                                         .getSimpleName();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+        activityObj = this;
+
+        list = (ListView) findViewById(R.id.list);
+        apisList = new ArrayList<String>();
+
+        // adding the item to list that will be displayed on the UI.
+        apisList.add("Resource Client API's");
+        apis = new ArrayAdapter<String>(activityObj,
+                android.R.layout.simple_list_item_1, apisList);
+        list.setAdapter(apis);
+
+        // handling user's selection on the UI
+        list.setOnItemClickListener(new OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view,
+                    int position, long id) {
+
+                if (position == 0) {
+                    Intent intent = new Intent(activityObj,
+                            ResourceClientActivity.class);
+                    startActivity(intent);
+                }
+            }
+        });
+
+        // calling the method to check the Wi-fi connectivity and configuring
+        // the OcPlatform
+        configurePlatform();
+    }
+
+    @Override
+    public void onBackPressed() {
+        apisList.clear();
+        super.onBackPressed();
+    }
+
+    private void configurePlatform() {
+        // local Variables
+        ConnectivityManager connManager;
+        NetworkInfo wifi;
+        AlertDialog dialog;
+        PlatformConfig platformConfigObj;
+
+        // Check the wifi connectivity
+        connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
+        wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
+        if (false == wifi.isConnected()) {
+            // WiFi is not connected close the application
+            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
+            dialogBuilder.setTitle("Error");
+            dialogBuilder
+                    .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
+            dialogBuilder.setCancelable(false);
+            dialogBuilder.setPositiveButton("OK", new OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    // Closing the application
+                    activityObj.finish();
+                }
+            });
+
+            dialog = dialogBuilder.create();
+            dialog.show();
+            Log.i(LOG_TAG,
+                    "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
+            return;
+        }
+        // If wifi is connected calling the configure method for configuring the
+        // OcPlatform
+        platformConfigObj = new PlatformConfig(getApplicationContext(),
+                ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0", 0,
+                QualityOfService.LOW);
+
+        OcPlatform.Configure(platformConfigObj);
+        Log.i(LOG_TAG, "Configuration done Successfully");
+    }
+}
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java b/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java
new file mode 100644 (file)
index 0000000..a60ef91
--- /dev/null
@@ -0,0 +1,476 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+package com.re.sampleclient;
+
+import java.util.Vector;
+
+import org.iotivity.ResourceEncapsulation.client.ICacheUpdateListener;
+import org.iotivity.ResourceEncapsulation.client.IDiscoverResourceListener;
+import org.iotivity.ResourceEncapsulation.client.IRemoteAttributeListener;
+import org.iotivity.ResourceEncapsulation.client.IStateChangedCallbackListener;
+import org.iotivity.ResourceEncapsulation.client.RCSAddress;
+import org.iotivity.ResourceEncapsulation.client.RCSDiscoveryManager;
+import org.iotivity.ResourceEncapsulation.client.RCSRemoteResourceObject;
+
+import org.iotivity.ResourceEncapsulation.utils.CacheState;
+import org.iotivity.ResourceEncapsulation.common.RCSException;
+import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes;
+import org.iotivity.ResourceEncapsulation.utils.ResourceState;
+
+import android.os.Message;
+import android.util.Log;
+
+/**
+ * For calling the Resource Client APIs as per user selection on UI and for
+ * updating the UI
+ *
+ * It contains all the Resource Client APIs.
+ */
+public class ResourceClient {
+
+    private static final String            LOG_TAG                  = "[RESample] Client";
+    private static Message                 msg;
+    public String                          logMessage;
+
+    private final RCSDiscoveryManager      discoveyManagerObj;
+    public RCSRemoteResourceObject         remoteResourceObj;
+
+    private static ResourceClientActivity  resourcClientActivityObj = null;
+    private final DiscoverResourceListener discoverResourceListener;
+    private final StateChangedListener     stateChangedListener;
+    private final CacheUpdateListener      cacheUpdateListener;
+    private final RemoteAttributeListener  remoteAttributeListener;
+    static boolean                         monitoringState          = false;
+    static boolean                         cachingState             = false;
+
+    // constructor
+    public ResourceClient() {
+        discoveyManagerObj = new RCSDiscoveryManager();
+        discoverResourceListener = new DiscoverResourceListener();
+        stateChangedListener = new StateChangedListener();
+        cacheUpdateListener = new CacheUpdateListener();
+        remoteAttributeListener = new RemoteAttributeListener();
+
+        discoveyManagerObj
+                .setDiscoverResourceListener(discoverResourceListener);
+
+        resourcClientActivityObj = ResourceClientActivity
+                .getResourceClientActivityObj();
+    }
+
+    /**
+     * Listener for receiving Resource discovered in network.
+     */
+    private class DiscoverResourceListener implements IDiscoverResourceListener {
+
+        @Override
+        public void onResourceDiscovered(RCSRemoteResourceObject foundResource) {
+            Log.i(LOG_TAG, "onResourceDiscovered invoked");
+
+            String resourceURI = foundResource.getUri();
+            String hostAddress = foundResource.getAddress();
+            boolean isObservableflag = foundResource.isObservable();
+
+            Vector<String> resourceTypes = foundResource.getTypes();
+            Vector<String> resourceInterfaces = foundResource.getInterFaces();
+            remoteResourceObj = foundResource;
+
+            msg = Message.obtain();
+            msg.what = 0;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+
+            logMessage = "URI : " + resourceURI + "\n";
+            logMessage = logMessage + "Host : " + hostAddress + "\n";
+            for (int i = 0; i < resourceTypes.size(); i++) {
+                logMessage = logMessage + "resourceType : "
+                        + resourceTypes.get(i) + "\n";
+            }
+
+            for (int i = 0; i < resourceInterfaces.size(); i++) {
+                logMessage = logMessage + "resourceInterfaces : "
+                        + resourceInterfaces.get(i) + "\n";
+            }
+            logMessage = logMessage + "isObservable : " + isObservableflag
+                    + "\n";
+
+            resourcClientActivityObj.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        }
+    };
+
+    /**
+     * Listener for receiving Changed resource state as a result of
+     * startMonitoring API
+     */
+    private class StateChangedListener implements IStateChangedCallbackListener {
+
+        @Override
+        public void onStateChangedCallback(ResourceState resourceState) {
+            Log.i(LOG_TAG, "onStateChangedCallback invoked");
+
+            switch (resourceState) {
+                case NONE:
+                    Log.i(LOG_TAG, "Current Resource State : NOT_MONITORING");
+                    logMessage = "Current Resource State : NOT_MONITORING\n";
+                    break;
+
+                case ALIVE:
+                    Log.i(LOG_TAG, "Current Resource State : ALIVE");
+                    logMessage = "Current Resource State : ALIVE\n";
+                    break;
+
+                case REQUESTED:
+                    Log.i(LOG_TAG, "Current Resource State : REQUESTED");
+                    logMessage = "Current Resource State : REQUESTED\n";
+                    break;
+
+                case LOST_SIGNAL:
+                    Log.i(LOG_TAG, "Current Resource State : LOST_SIGNAL");
+                    logMessage = "Current Resource State : LOST_SIGNAL\n";
+                    logMessage = logMessage
+                            + "Start the server and press discover resource button"
+                            + "\n";
+                    break;
+
+                case DESTROYED:
+                    Log.i(LOG_TAG, "Current Resource State : DESTROYED");
+                    logMessage = "Current Resource State : DESTROYED\n";
+                    break;
+            }
+
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        }
+
+    }
+
+    /**
+     * Listener for receiving updated resource attributes as a result of
+     * startCaching() API with callback
+     */
+    private class CacheUpdateListener implements ICacheUpdateListener {
+        @Override
+        public void onCacheUpdatedCallback(RCSResourceAttributes attributes) {
+            Log.i(LOG_TAG, "onCacheUpdatedCallback invoked");
+
+            printAttributes(attributes);
+        }
+    };
+
+    /**
+     * Listener for receiving the resource attributes as a result of
+     * getRemoteAttributes() and setRemoteAttributes() API
+     */
+    private class RemoteAttributeListener implements IRemoteAttributeListener {
+        @Override
+        public void onRemoteAttributesGetCallback(
+                RCSResourceAttributes attributes) {
+
+            Log.i(LOG_TAG, "onRemoteAttributesGetCallback invoked");
+            printAttributes(attributes);
+        }
+    };
+
+    public void disocverResources() {
+        Log.d(LOG_TAG, "discover resources entry");
+        RCSAddress address = RCSAddress.multicast();
+        discoveyManagerObj.discoverResource(address);
+        Log.d(LOG_TAG, "discover resources exit");
+    }
+
+    public void startMonitoring() {
+        Log.d(LOG_TAG, "startMonitoring entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        if (true == monitoringState) {
+            logMessage = "Monitoring already started\n";
+            Log.e(LOG_TAG, "Monitoring already started");
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+            return;
+        }
+
+        remoteResourceObj.setStateChangedListener(stateChangedListener);
+
+        if (null != stateChangedListener) {
+            try {
+                remoteResourceObj.startMonitoring();
+                monitoringState = true;
+            } catch (RCSException e) {
+                e.printStackTrace();
+                return;
+            }
+        }
+        Log.d(LOG_TAG, "startMonitoring exit");
+    }
+
+    public void stopMonitoring() {
+        Log.d(LOG_TAG, "stopMonitoring entry");
+
+        if (monitoringState) {
+            if (null == remoteResourceObj) {
+                Log.e(LOG_TAG, "Remote Resource object NULL");
+                return;
+            }
+
+            remoteResourceObj.stopMonitoring();
+            monitoringState = false;
+            logMessage = "Stopped Resource Monitoring\n";
+        } else {
+            Log.i(LOG_TAG, "Monitoring not started");
+            logMessage = "Monitoring not started\n";
+        }
+
+        Log.d(LOG_TAG, "stopMonitoring exit");
+
+        ResourceClientActivity.setMessageLog(logMessage);
+        msg = Message.obtain();
+        msg.what = 1;
+        resourcClientActivityObj.getHandler().sendMessage(msg);
+    }
+
+    public void getRemoteAttributes() {
+        Log.d(LOG_TAG, "getRemoteAttributes entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        try {
+            remoteResourceObj
+                    .setRemoteAttributesListener(remoteAttributeListener);
+
+            if (null != remoteAttributeListener) {
+                remoteResourceObj.getRemoteAttributes();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+
+        Log.d(LOG_TAG, "getRemoteAttributes exit");
+    }
+
+    public void setRemoteAttributes(int value) {
+        Log.d(LOG_TAG, "setRemoteAttributes entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        try {
+            remoteResourceObj
+                    .setRemoteAttributesListener(remoteAttributeListener);
+
+            if (null != remoteAttributeListener) {
+                RCSResourceAttributes attr = new RCSResourceAttributes();
+                attr.setValueInt("Temperature", value);
+                remoteResourceObj.setRemoteAttributes(attr);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+
+        Log.d(LOG_TAG, "setRemoteAttributes exit");
+    }
+
+    public void startCaching(int cachingType) {
+        Log.d(LOG_TAG, "startCaching entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        if (true == cachingState) {
+            Log.d(LOG_TAG, "Caching already started");
+            logMessage = "Caching already started\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+            return;
+        }
+
+        if (0 == cachingType) {
+            Log.d(LOG_TAG, "startCaching without callback");
+            remoteResourceObj.setCacheUpdateListener(null);
+            logMessage = "Caching started without callback\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        } else {
+            Log.d(LOG_TAG, "startCaching with callback");
+            remoteResourceObj.setCacheUpdateListener(cacheUpdateListener);
+            logMessage = "Caching started\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        }
+
+        try {
+            remoteResourceObj.startCaching();
+            cachingState = true;
+        } catch (RCSException e) {
+            e.printStackTrace();
+            return;
+        }
+
+        Log.d(LOG_TAG, "startCaching exit");
+    }
+
+    public void getCacheState() {
+        Log.d(LOG_TAG, "getCacheState entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        CacheState cState = remoteResourceObj.getCacheState();
+        Log.i(LOG_TAG, "Cache State : " + cState.name());
+
+        logMessage = "Cache State : " + cState.toString() + "\n";
+        ResourceClientActivity.setMessageLog(logMessage);
+        msg = Message.obtain();
+        msg.what = 1;
+        resourcClientActivityObj.getHandler().sendMessage(msg);
+
+        Log.d(LOG_TAG, "getCacheState exit");
+    }
+
+    public void getCachedAttributes() {
+        Log.d(LOG_TAG, "getCachedAttributes entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+        if (true == cachingState) {
+            RCSResourceAttributes attributes = null;
+            try {
+                attributes = remoteResourceObj.getCachedAttributes();
+            } catch (RCSException e) {
+                e.printStackTrace();
+                return;
+            }
+            printAttributes(attributes);
+        } else {
+            logMessage = "Caching not started" + "\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        }
+        Log.d(LOG_TAG, "getCachedAttributes exit");
+    }
+
+    public void getCachedAttribute() {
+        Log.d(LOG_TAG, "getCachedAttribute entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        if (true == cachingState) {
+            String key = "Temperature";
+            String attribute;
+            try {
+                attribute = remoteResourceObj.getCachedAttribute(key);
+            } catch (RCSException e) {
+                e.printStackTrace();
+                return;
+            }
+            logMessage = "Key : " + key + "\n";
+            logMessage = logMessage + "Value : " + attribute + "\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        } else {
+            logMessage = "Caching not started" + "\n";
+            ResourceClientActivity.setMessageLog(logMessage);
+            msg = Message.obtain();
+            msg.what = 1;
+            resourcClientActivityObj.getHandler().sendMessage(msg);
+        }
+        Log.d(LOG_TAG, "getCachedAttribute exit");
+    }
+
+    public void stopCaching() {
+        Log.d(LOG_TAG, "stopCaching entry");
+
+        if (null == remoteResourceObj) {
+            Log.e(LOG_TAG, "Remote Resource object NULL");
+            return;
+        }
+
+        if (false == cachingState) {
+            Log.e(LOG_TAG, "Caching not started");
+            logMessage = "Caching not started\n";
+        } else {
+            remoteResourceObj.stopCaching();
+            cachingState = false;
+            logMessage = "Stopped Caching\n";
+        }
+
+        ResourceClientActivity.setMessageLog(logMessage);
+        msg = Message.obtain();
+        msg.what = 1;
+        resourcClientActivityObj.getHandler().sendMessage(msg);
+        Log.d(LOG_TAG, "stopCaching exit");
+    }
+
+    private void printAttributes(RCSResourceAttributes attributes) {
+        Log.d(LOG_TAG, "Printing Attributes");
+
+        try {
+            int temperature = attributes.getValueInt("Temperature");
+            Log.i(LOG_TAG, "Temperature : " + temperature);
+            logMessage = "Attributes : \n\nTemperature : " + temperature + "\n";
+        } catch (Exception e) {
+            Log.i(LOG_TAG, "Temperature attribute not found");
+            logMessage = "Attributes Temperature not found \n";
+            e.printStackTrace();
+            return;
+        }
+
+        ResourceClientActivity.setMessageLog(logMessage);
+        msg = Message.obtain();
+        msg.what = 1;
+        resourcClientActivityObj.getHandler().sendMessage(msg);
+    }
+}
\ No newline at end of file
diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClientActivity.java b/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClientActivity.java
new file mode 100644 (file)
index 0000000..6107664
--- /dev/null
@@ -0,0 +1,224 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+package com.re.sampleclient;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+
+import android.app.Activity;
+import android.app.Dialog;
+import android.content.Context;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.ListView;
+import android.widget.Toast;
+
+/*
+ * Activity for handling user's selection on UI for Resource Client APIs.
+ * & for updating UI.
+ */
+public class ResourceClientActivity extends Activity {
+
+    private ListView                      list;
+    private Button                        discoverResourceButton;
+    private ArrayAdapter<String>          groupApis;
+    private ArrayList<String>             clientApisList;
+    private static ResourceClientActivity clientApiActivityObj;
+    private ResourceClient                resourceClientObj;
+    private static Handler                mHandler;
+
+    private static EditText               logs;
+    private static String                 logMessage;
+
+    public static Context                 mcontext;
+    public static Calendar                scheduleTime;
+
+    private final String                  LOG_TAG = "[ReSample] "
+                                                          + this.getClass()
+                                                                  .getSimpleName(); ;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.resourceclientapis);
+
+        clientApiActivityObj = this;
+        mcontext = this;
+
+        resourceClientObj = new ResourceClient();
+        clientApisList = new ArrayList<String>();
+        list = (ListView) findViewById(R.id.groupaApiList);
+        discoverResourceButton = (Button) findViewById(R.id.button1);
+        logs = (EditText) findViewById(R.id.EditText);
+
+        // adding the item to list that will be displayed on the UI.
+        clientApisList.add("1. Start Monitoring"); // 0
+        clientApisList.add("2. Stop Monitoring"); // 1
+
+        clientApisList.add("3. Get Remote Attributes"); // 2
+        clientApisList.add("4. Set Remote Attributes"); // 3
+
+        clientApisList.add("5. Start Caching without callback"); // 4
+        clientApisList.add("6. Start Caching with callback"); // 5
+
+        clientApisList.add("7. Get Cache State"); // 6
+        clientApisList.add("8. Get All Cached Attributes"); // 7
+        clientApisList.add("9. Get Cached Attribute"); // 8
+
+        clientApisList.add("10. Stop Caching"); // 9
+
+        // handler for updating the UI i.e. MessageLog (TextBox) & ListView
+        mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message msg) {
+                switch (msg.what) {
+                    case 0:
+                        groupApis = new ArrayAdapter<String>(
+                                clientApiActivityObj,
+                                android.R.layout.simple_list_item_1,
+                                clientApisList);
+                        list.setAdapter(groupApis);
+                        list.bringToFront();
+                        break;
+                    case 1:
+                        logs.setText("");
+                        logs.setText(logMessage);
+                        Log.i(LOG_TAG, logMessage);
+                }
+            }
+        };
+        setHandler(mHandler);
+
+        // Discover ResourceButton Listener
+        discoverResourceButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                resourceClientObj.disocverResources();
+            }
+        });
+
+        // Listener for item clicked by the user on the UI
+        list.setOnItemClickListener(new OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view,
+                    int position, long id) {
+                if (position == 0) {
+                    resourceClientObj.startMonitoring();
+                } else if (position == 1) {
+                    resourceClientObj.stopMonitoring();
+                } else if (position == 2) {
+                    resourceClientObj.getRemoteAttributes();
+                } else if (position == 3) {
+                    userInputDialog();
+                } else if (position == 4) {
+                    resourceClientObj.startCaching(0);
+                } else if (position == 5) {
+                    resourceClientObj.startCaching(1);
+                } else if (position == 6) {
+                    resourceClientObj.getCacheState();
+                } else if (position == 7) {
+                    resourceClientObj.getCachedAttributes();
+                } else if (position == 8) {
+                    resourceClientObj.getCachedAttribute();
+                } else if (position == 9) {
+                    resourceClientObj.stopCaching();
+                }
+            }
+        });
+    }
+
+    // Dialog for taking user input for setting the attribute [Temperature]
+    // value
+    private void userInputDialog() {
+
+        if (null == resourceClientObj) {
+            displayToastMessage("Resource Object is NULL");
+        } else {
+            final Dialog dialog = new Dialog(mcontext);
+            dialog.setContentView(R.layout.userinputforattributevalue);
+            dialog.setTitle("Enter the Temperature Value");
+
+            dialog.setCancelable(false);
+            dialog.show();
+            Button ok = (Button) dialog.findViewById(R.id.ok);
+            Button cancel = (Button) dialog.findViewById(R.id.cancel);
+
+            ok.setOnClickListener(new OnClickListener() {
+                @Override
+                public void onClick(View v) {
+
+                    EditText regionValue = (EditText) dialog
+                            .findViewById(R.id.attributeValue);
+                    String value = regionValue.getText().toString();
+                    if (value.equalsIgnoreCase("")) {
+                        String toastmessage = "Please enter the Temperature Value";
+                        displayToastMessage(toastmessage);
+                    } else {
+                        dialog.dismiss();
+                        int val = Integer.parseInt(value);
+                        resourceClientObj.setRemoteAttributes(val);
+                    }
+                }
+            });
+            cancel.setOnClickListener(new OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    dialog.dismiss();
+                }
+            });
+        }
+    }
+
+    public static void setMessageLog(String message) {
+        logMessage = message;
+    }
+
+    @Override
+    public void onBackPressed() {
+        super.onBackPressed();
+    }
+
+    public static ResourceClientActivity getResourceClientActivityObj() {
+        return clientApiActivityObj;
+    }
+
+    public Handler getHandler() {
+        return mHandler;
+    }
+
+    public void setHandler(Handler mHandler) {
+        ResourceClientActivity.mHandler = mHandler;
+    }
+
+    public void displayToastMessage(String message) {
+        Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
+        toast.show();
+        Log.i(LOG_TAG, message);
+    }
+}