Modify android sample client application
authorMinji Park <minjii.park@samsung.com>
Tue, 12 Jan 2016 12:30:25 +0000 (21:30 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 13 Jan 2016 08:55:04 +0000 (08:55 +0000)
change client application to make discover resource and get attribute data

Change-Id: I6cd7ac52e4af20d528727d66b2cfd76881d87aff
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4789
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-container/examples/BMISensorBundle/src/BMISensorResource.cpp
service/resource-container/examples/DiscomfortIndexSensorBundle/src/DiscomfortIndexSensorResource.cpp
service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ContainerClientActivity.java
service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListAdapter.java [new file with mode: 0644]
service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListItem.java [new file with mode: 0644]
service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/Utils.java
service/resource-container/examples/android/RCSampleClientApp/app/src/main/res/layout/activity_container_client.xml
service/resource-container/examples/android/RCSampleClientApp/app/src/main/res/layout/discovered_resource.xml [new file with mode: 0644]
service/resource-container/examples/android/RCSampleServerApp/app/src/main/java/org/iotivity/service/sample/container/ResourceContainer.java

index 9bfe09f..318f5e7 100644 (file)
@@ -47,7 +47,7 @@ void BMISensorResource::executeLogic()
     std::string strBMIResult;
 
     if (m_pBMISensor->executeBMISensorLogic(&m_mapInputData, &strBMIResult) != -1)
-        setAttribute("BMIresult", RCSResourceAttributes::Value(strBMIResult.c_str()));
+        setAttribute("BMIresult", RCSResourceAttributes::Value(strBMIResult.c_str())), true;
 }
 
 void BMISensorResource::onUpdatedInputResource(const std::string attributeName,
index bc65a4e..786d47f 100644 (file)
@@ -51,7 +51,7 @@ void DiscomfortIndexSensorResource::executeLogic()
 
     m_pDiscomfortIndexSensor->executeDISensorLogic(&m_mapInputData, &strDiscomfortIndex);
 
-    setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()));
+    setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()), true);
 
     for (auto it : m_mapInputData)
     {
index 5ec437c..86a005f 100644 (file)
@@ -20,8 +20,11 @@ package org.iotivity.service.sample.client;
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.util.Log;
 import android.view.View;
+import android.widget.ListView;
 import android.widget.TextView;
 
 import org.iotivity.service.RcsException;
@@ -29,47 +32,132 @@ import org.iotivity.service.client.RcsAddress;
 import org.iotivity.service.client.RcsDiscoveryManager;
 import org.iotivity.service.client.RcsRemoteResourceObject;
 
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * It contains the discover resource API for Discovering Container Resource
  */
+
 public class ContainerClientActivity extends Activity implements
         RcsDiscoveryManager.OnResourceDiscoveredListener {
+    public static final int MSG_ID_ATTRIBUTE_RECEIVED  = 0;
+    public static final int MSG_ID_CACHEDATA_RECEIVED  = 1;
+
     private final String LOG_TAG = "[SampleClient] "
                                          + this.getClass().getSimpleName();
+    private final String RESOURCE_DISCOVERY_URI_KEYWORD = "discomfort";
+
+    private Handler    mHandler;
 
+    private ResourceListAdapter mResourceListViewAdapter;
+
+    private ListView mResourceListView;
     private TextView     mLogView;
 
+    private List<String> mFoundResourceUris;
+    private List<ResourceListItem> mFoundResources;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_container_client);
+
+        mFoundResourceUris = new ArrayList<String>();
+        mFoundResources = new ArrayList<ResourceListItem>();
+
+        mHandler = new ContainerClientHandler(this);
+
+        mResourceListViewAdapter = new ResourceListAdapter(this, R.layout.discovered_resource, mFoundResources);
+        mResourceListView = (ListView) findViewById(R.id.ResourceListView);
+        mResourceListView.setAdapter(mResourceListViewAdapter);
+
         mLogView = (TextView) findViewById(R.id.log);
     }
 
     public void onDiscoverResourceClick(View v) {
         try {
-            RcsDiscoveryManager.getInstance().discoverResourceByType(
-                    RcsAddress.multicast(), "oic.r.discomfortindex",
+            RcsDiscoveryManager.getInstance().discoverResource(
+                    RcsAddress.multicast(),
                     ContainerClientActivity.this);
+
+            mFoundResourceUris.clear();
+            mFoundResources.clear();
+            mResourceListViewAdapter.notifyDataSetChanged();
+
             mLogView.setText("");
         } catch (RcsException e) {
             e.printStackTrace();
         }
     }
 
+    private boolean IsFoundResource(String resourceUri)
+    {
+        if (mFoundResourceUris.contains(resourceUri))
+            return true;
+
+        return false;
+    }
+
     @Override
     public void onResourceDiscovered(
             final RcsRemoteResourceObject discoveredResource) {
-        Log.i(LOG_TAG, "onResourceDiscovered");
 
         runOnUiThread(new Runnable() {
             public void run() {
                 try {
-                    mLogView.setText(Utils.resourceInfo(discoveredResource));
+                    String uri = discoveredResource.getUri();
+                    Log.i(LOG_TAG, "onResourceDiscovered -- " + uri);
+
+                    if (uri.contains(RESOURCE_DISCOVERY_URI_KEYWORD) && !IsFoundResource(uri)) {
+                        mFoundResourceUris.add(uri);
+                        mFoundResources.add(new ResourceListItem(uri, discoveredResource, ContainerClientActivity.this.getHandler()));
+                        mResourceListViewAdapter.notifyDataSetChanged();
+                    }
                 } catch (RcsException e) {
-                    Utils.showError(ContainerClientActivity.this, LOG_TAG, e);
+                    e.printStackTrace();
                 }
             }
         });
     }
-}
+
+    private void printLog(String message) {
+        Log.i(LOG_TAG, message);
+        mLogView.setText(message);
+    }
+
+    public Handler getHandler() {
+        return mHandler;
+    }
+
+    private static class ContainerClientHandler extends Handler {
+        private WeakReference<ContainerClientActivity> mActivityRef;
+        private String logMsg;
+
+        private ContainerClientHandler(ContainerClientActivity activity) {
+            mActivityRef = new WeakReference<>(activity);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            super.handleMessage(msg);
+
+            ContainerClientActivity activity = mActivityRef.get();
+            if (activity == null) return;
+
+            switch (msg.what) {
+                case MSG_ID_ATTRIBUTE_RECEIVED:
+                    logMsg = "-- ATTRIBUTE_RECEIVED\n";
+                    logMsg += msg.obj.toString();
+                    break;
+
+                case MSG_ID_CACHEDATA_RECEIVED:
+                    logMsg = "-- CACHEDATA_RECEIVED\n";
+                    logMsg += msg.obj.toString();
+                    break;
+            }
+            activity.printLog(logMsg);
+        }
+    }
+}
\ No newline at end of file
diff --git a/service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListAdapter.java b/service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListAdapter.java
new file mode 100644 (file)
index 0000000..8145bc8
--- /dev/null
@@ -0,0 +1,120 @@
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 org.iotivity.service.sample.client;
+
+import android.app.Activity;
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.TextView;
+import android.widget.ToggleButton;
+
+import org.iotivity.service.RcsException;
+
+import java.util.List;
+
+public class ResourceListAdapter extends ArrayAdapter<ResourceListItem> {
+
+    private Context mContext;
+    private int mLayoutId;
+    private List<ResourceListItem> foundResources;
+
+    public ResourceListAdapter(Context context, int layoutId, List<ResourceListItem> foundResources)
+    {
+        super(context, layoutId, foundResources);
+
+        this.mLayoutId = layoutId;
+        this.mContext = context;
+        this.foundResources = foundResources;
+    }
+
+    @Override
+    public View getView(final int position, View convertView, final ViewGroup parent) {
+        if (convertView == null)
+        {
+            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
+            convertView = inflater.inflate(mLayoutId, parent, false);
+        }
+
+        String resourceUri = foundResources.get(position).mResourceUri;
+
+        TextView textViewUri = (TextView) convertView.findViewById(R.id.textView_uri);
+        textViewUri.setText(resourceUri);
+
+        textViewUri.setOnClickListener(new View.OnClickListener() {
+
+            @Override
+            public void onClick(View view) {
+                try {
+                    TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+                    textView.setText(Utils.resourceInfo(foundResources.get(position).mResource));
+                } catch (RcsException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+
+        Button buttonGet = (Button) convertView.findViewById(R.id.button_get);
+        buttonGet.setOnClickListener(new View.OnClickListener() {
+
+            @Override
+            public void onClick(View view) {
+                ResourceListItem selectedResource = foundResources.get(position);
+                try {
+                    TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+                    textView.setText("Get Remote Attributes from \'" + selectedResource.mResourceUri + "\'");
+                    selectedResource.mResource.getRemoteAttributes(selectedResource.mGetListener);
+                } catch (RcsException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+
+        ToggleButton buttonObserve = (ToggleButton) convertView.findViewById(R.id.button_observe);
+        buttonObserve.setOnClickListener(new View.OnClickListener() {
+
+            @Override
+            public void onClick(View view) {
+                ResourceListItem selectedResource = foundResources.get(position);
+                TextView textView = (TextView) ((Activity) mContext).findViewById(R.id.log);
+
+                try {
+                    if (!selectedResource.mCacheStarted) {
+                        textView.setText("Start Observe \'" + selectedResource.mResourceUri + "\'");
+                        selectedResource.mResource.startCaching(selectedResource.mCacheListener);
+                        selectedResource.mCacheStarted = true;
+                        ((ToggleButton) view).setChecked(true);
+                    } else {
+                        textView.setText("Stop Observing \'" + selectedResource.mResourceUri + "\'");
+                        selectedResource.mResource.stopCaching();
+                        selectedResource.mCacheStarted = false;
+                        ((ToggleButton) view).setChecked(false);
+                    }
+                } catch (RcsException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+
+        return convertView;
+    }
+}
\ No newline at end of file
diff --git a/service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListItem.java b/service/resource-container/examples/android/RCSampleClientApp/app/src/main/java/org/iotivity/service/sample/client/ResourceListItem.java
new file mode 100644 (file)
index 0000000..e9f205f
--- /dev/null
@@ -0,0 +1,101 @@
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 org.iotivity.service.sample.client;
+
+import android.os.Handler;
+import android.util.Log;
+
+import org.iotivity.service.RcsException;
+import org.iotivity.service.RcsResourceAttributes;
+import org.iotivity.service.client.RcsRemoteResourceObject;
+
+public class ResourceListItem {
+    private final String LOG_TAG = "[RCSampleClient] "
+            + this.getClass().getSimpleName();
+
+    public String mResourceUri;
+    public RcsRemoteResourceObject mResource;
+    public RemoteResourceGetListener mGetListener;
+    public RemoteResourceCacheListener mCacheListener;
+    public boolean mCacheStarted;
+
+    private Handler mHandler;
+
+    public ResourceListItem(String resourceUri, RcsRemoteResourceObject resource, Handler handler)
+    {
+        mResourceUri = resourceUri;
+        mResource = resource;
+
+        mGetListener = new RemoteResourceGetListener();
+        mCacheListener = new RemoteResourceCacheListener();
+        mCacheStarted = false;
+
+        mHandler = handler;
+    }
+
+    private class RemoteResourceGetListener implements RcsRemoteResourceObject.OnRemoteAttributesReceivedListener
+    {
+
+        @Override
+        public void onAttributesReceived(final RcsResourceAttributes rcsResourceAttributes, int eCode) {
+            Log.i(LOG_TAG, "onAttributesReceived -- " + mResourceUri);
+
+            new Thread(new Runnable() {
+
+                @Override
+                public void run() {
+                    try {
+                        String logMessage;
+                        logMessage = "-- " + mResourceUri + "\n";
+                        logMessage += Utils.resourceAttributes(rcsResourceAttributes);
+
+                        mHandler.obtainMessage(ContainerClientActivity.MSG_ID_ATTRIBUTE_RECEIVED, logMessage).sendToTarget();
+                    } catch (RcsException e) {
+                        e.printStackTrace();
+                    }
+                 }
+            }).run();
+        }
+    }
+
+    private class RemoteResourceCacheListener implements RcsRemoteResourceObject.OnCacheUpdatedListener
+    {
+
+        @Override
+        public void onCacheUpdated(final RcsResourceAttributes rcsResourceAttributes) {
+            Log.i(LOG_TAG, "onCacheUpdated -- " + mResourceUri);
+
+            new Thread(new Runnable() {
+
+                @Override
+                public void run() {
+                    try {
+                        String logMessage;
+                        logMessage = "-- " + mResourceUri + "\n";
+                        logMessage += Utils.resourceAttributes(rcsResourceAttributes);
+
+                        mHandler.obtainMessage(ContainerClientActivity.MSG_ID_CACHEDATA_RECEIVED, logMessage).sendToTarget();
+                    } catch (RcsException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }).run();
+        }
+    }
+}
\ No newline at end of file
index a39afd1..46e2828 100644 (file)
@@ -1,10 +1,25 @@
-package org.iotivity.service.sample.client;
+/******************************************************************
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ * <p>
+ * <p>
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.
+ ******************************************************************/
 
-import android.content.Context;
-import android.util.Log;
-import android.widget.Toast;
+package org.iotivity.service.sample.client;
 
 import org.iotivity.service.RcsException;
+import org.iotivity.service.RcsResourceAttributes;
 import org.iotivity.service.client.RcsRemoteResourceObject;
 
 public class Utils {
@@ -27,13 +42,15 @@ public class Utils {
         return sb.toString();
     }
 
-    public static void showError(Context ctx, String tag, String msg) {
-        Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
-        Log.e(tag, msg);
-    }
+    public static String resourceAttributes(RcsResourceAttributes attr)
+            throws RcsException {
+        StringBuilder sb = new StringBuilder();
+
+        for (String key : attr.keySet())
+        {
+            sb.append(key + " : " + attr.get(key) + "\n");
+        }
 
-    public static void showError(Context ctx, String tag, Exception e) {
-        Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_SHORT).show();
-        Log.e(tag, e.getMessage(), e);
+        return sb.toString();
     }
 }
index 09634a3..2d3f7a0 100644 (file)
@@ -4,14 +4,17 @@
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:id="@+id/main"
-    android:weightSum="1">
+    android:weightSum="1"
+    android:padding="5dp">
 
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:onClick="onDiscoverResourceClick"
         android:text="@string/discover_resource"
-        android:id="@+id/di" />
+        android:id="@+id/di"
+        android:layout_alignParentTop="true"
+        android:layout_alignParentStart="true" />
 
     <TextView
         android:id="@+id/log"
         android:layout_height="250dp"
         android:ems="10"
         android:layout_weight="0.26"
+        android:layout_alignParentStart="true"
+        android:layout_alignParentBottom="true"
+        android:background="#32437200"
+        android:padding="10dp" />
+
+    <ListView
+        android:layout_width="wrap_content"
+        android:layout_height="376dp"
+        android:id="@+id/ResourceListView"
         android:layout_below="@+id/di"
-        android:layout_alignParentStart="true" />
+        android:layout_alignParentStart="true"
+        android:layout_above="@+id/log" />
 
 </RelativeLayout>
diff --git a/service/resource-container/examples/android/RCSampleClientApp/app/src/main/res/layout/discovered_resource.xml b/service/resource-container/examples/android/RCSampleClientApp/app/src/main/res/layout/discovered_resource.xml
new file mode 100644 (file)
index 0000000..40ec219
--- /dev/null
@@ -0,0 +1,33 @@
+<?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">
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="/softsensor/discomfortIndex/1"
+        android:id="@+id/textView_uri"
+        android:layout_alignBaseline="@+id/button_get"
+        android:layout_alignBottom="@+id/button_get"
+        android:layout_alignParentStart="true"
+        android:paddingStart="10dp"
+        android:textStyle="bold" />
+
+    <Button
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Get"
+        android:id="@+id/button_get"
+        android:layout_alignParentTop="true"
+        android:layout_toStartOf="@+id/button_observe" />
+
+    <ToggleButton
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/button_observe"
+        android:layout_alignBottom="@+id/button_get"
+        android:layout_alignParentEnd="true"
+        android:textOff="Observe"
+        android:textOn="Observe" />
+
+</RelativeLayout>
\ No newline at end of file
index e3842dd..8a3c6e3 100755 (executable)
@@ -21,8 +21,6 @@
 package org.iotivity.service.sample.container;
 
 import android.content.Context;
-import android.content.pm.ApplicationInfo;
-import android.content.pm.PackageManager;
 import android.os.Message;
 
 import android.os.PowerManager;
@@ -36,8 +34,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import dalvik.system.DexFile;
-
 /**
  * For calling the Resource Container APIs as per user selection on UI and for
  * updating the UI
@@ -57,8 +53,6 @@ public class ResourceContainer {
     PowerManager pm = null;
     PowerManager.WakeLock wl = null;
 
-
-
     // constructor
     public ResourceContainer() {
         resourceContainerActivityInstance = ResourceContainerActivity
@@ -277,6 +271,5 @@ public class ResourceContainer {
         msg = Message.obtain();
         msg.what = 1;
         resourceContainerActivityInstance.getHandler().sendMessage(msg);
-        //containerInstance.registerAndroidResource("testBundle", testResource);
     }
 }