From: Tim Kourt Date: Thu, 24 Sep 2015 19:24:02 +0000 (-0700) Subject: Android: Adding GroupServer/Client examples X-Git-Tag: 1.2.0+RC1~1026 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e685fddfe38624da5495400df2a1b76f95db6886;p=platform%2Fupstream%2Fiotivity.git Android: Adding GroupServer/Client examples Change-Id: I2604a8dfccf6f55b37949e8ff0eda6b1b49d7713 Signed-off-by: Tim Kourt Reviewed-on: https://gerrit.iotivity.org/gerrit/3053 Tested-by: jenkins-iotivity Reviewed-by: Patrick Lankswert --- diff --git a/android/examples/groupclient/.gitignore b/android/examples/groupclient/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/android/examples/groupclient/.gitignore @@ -0,0 +1 @@ +/build diff --git a/android/examples/groupclient/build.gradle b/android/examples/groupclient/build.gradle new file mode 100644 index 0000000..b1653de --- /dev/null +++ b/android/examples/groupclient/build.gradle @@ -0,0 +1,24 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + applicationId "org.iotivity.base.examples.groupclient" + minSdkVersion 21 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/android/examples/groupclient/groupclient.iml b/android/examples/groupclient/groupclient.iml new file mode 100644 index 0000000..440b831 --- /dev/null +++ b/android/examples/groupclient/groupclient.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/examples/groupclient/proguard-rules.pro b/android/examples/groupclient/proguard-rules.pro new file mode 100644 index 0000000..d26150c --- /dev/null +++ b/android/examples/groupclient/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/rahul/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/android/examples/groupclient/src/main/AndroidManifest.xml b/android/examples/groupclient/src/main/AndroidManifest.xml new file mode 100644 index 0000000..863e2ee --- /dev/null +++ b/android/examples/groupclient/src/main/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/examples/groupclient/src/main/java/org/iotivity/base/examples/GroupClient.java b/android/examples/groupclient/src/main/java/org/iotivity/base/examples/GroupClient.java new file mode 100644 index 0000000..4bfa9ee --- /dev/null +++ b/android/examples/groupclient/src/main/java/org/iotivity/base/examples/GroupClient.java @@ -0,0 +1,295 @@ +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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 org.iotivity.base.examples; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.util.Log; +import android.view.View; +import android.widget.CompoundButton; +import android.widget.ScrollView; +import android.widget.TextView; +import android.widget.ToggleButton; + +import org.iotivity.base.ErrorCode; +import org.iotivity.base.ModeType; +import org.iotivity.base.OcConnectivityType; +import org.iotivity.base.OcException; +import org.iotivity.base.OcHeaderOption; +import org.iotivity.base.OcPlatform; +import org.iotivity.base.OcRepresentation; +import org.iotivity.base.OcResource; +import org.iotivity.base.PlatformConfig; +import org.iotivity.base.QualityOfService; +import org.iotivity.base.ServiceType; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * GroupClient + */ +public class GroupClient extends Activity implements + OcPlatform.OnResourceFoundListener, + OcResource.OnGetListener { + + private OcResource mFoundCollectionResource; + + /** + * A local method to configure and initialize platform and then search for the collection + * resources + */ + private synchronized void startGroupClient() { + Context context = this; + + PlatformConfig platformConfig = new PlatformConfig( + context, + ServiceType.IN_PROC, + ModeType.CLIENT, + "0.0.0.0", 0, + QualityOfService.LOW + ); + + msg("Configuring platform."); + OcPlatform.Configure(platformConfig); + + msg("Find all resources of type \"a.collection\"."); + try { + OcPlatform.findResource( + "", + OcPlatform.WELL_KNOWN_QUERY + "?rt=a.collection", + EnumSet.of(OcConnectivityType.CT_DEFAULT), + this + ); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to invoke find resource API"); + } + + printLine(); + } + + /** + * An event handler to be executed whenever a "findResource" request completes successfully + * + * @param foundResource found resource + */ + @Override + public synchronized void onResourceFound(OcResource foundResource) { + if (null == foundResource) { + msg("Found resource is invalid"); + return; + } + if (null != mFoundCollectionResource) { + msg("Found another resource, ignoring"); + return; + } + + // Get the resource URI + String resourceUri = foundResource.getUri(); + // Get the resource host address + String hostAddress = foundResource.getHost(); + msg("\tURI of the resource: " + resourceUri); + msg("\tHost address of the resource: " + hostAddress); + // Get the resource types + msg("\tList of resource types: "); + for (String resourceType : foundResource.getResourceTypes()) { + msg("\t\t" + resourceType); + } + msg("\tList of resource interfaces:"); + for (String resourceInterface : foundResource.getResourceInterfaces()) { + msg("\t\t" + resourceInterface); + } + msg("\tList of resource connectivity types:"); + for (OcConnectivityType connectivityType : foundResource.getConnectivityTypeSet()) { + msg("\t\t" + connectivityType); + } + + if (resourceUri.equals("/core/a/collection")) { + mFoundCollectionResource = foundResource; + + msg("Getting representation of a collection resource..."); + + Map queryParams = new HashMap<>(); + try { + mFoundCollectionResource.get( + "", + OcPlatform.DEFAULT_INTERFACE, + queryParams, + this + ); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Error occurred while invoking \"get\" API"); + } + } + + printLine(); + enableStartStopButton(); + } + + /** + * An event handler to be executed whenever a "get" request completes successfully + * + * @param list list of the header options + * @param representation representation of a resource + */ + @Override + public void onGetCompleted(List list, OcRepresentation representation) { + msg("Representation of a light collection resource:"); + for (OcRepresentation childRepresentation : representation.getChildren()) { + msg("\t\tURI: " + childRepresentation.getUri()); + } + } + + /** + * An event handler to be executed whenever a "get" request fails + * + * @param throwable exception + */ + @Override + public synchronized void onGetFailed(Throwable throwable) { + if (throwable instanceof OcException) { + OcException ocEx = (OcException) throwable; + Log.e(TAG, ocEx.toString()); + ErrorCode errCode = ocEx.getErrorCode(); + //do something based on errorCode + msg("Error code: " + errCode); + } + msg("Failed to get representation of a found collection resource"); + } + + /** + * A local method to reset group client + */ + private synchronized void stopGroupClient() { + mFoundCollectionResource = null; + + msg("Group Client is reset."); + printLine(); + + enableStartStopButton(); + } + + //****************************************************************************** + // End of the OIC specific code + //****************************************************************************** + + private final static String TAG = GroupClient.class.getSimpleName(); + private TextView mConsoleTextView; + private ScrollView mScrollView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_group_client); + + mConsoleTextView = (TextView) findViewById(R.id.consoleTextView); + mConsoleTextView.setMovementMethod(new ScrollingMovementMethod()); + mScrollView = (ScrollView) findViewById(R.id.scrollView); + mScrollView.fullScroll(View.FOCUS_DOWN); + final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + + if (null == savedInstanceState) { + toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + toggleButton.setEnabled(false); + if (isChecked) { + new Thread(new Runnable() { + public void run() { + startGroupClient(); + } + }).start(); + } else { + new Thread(new Runnable() { + public void run() { + stopGroupClient(); + } + }).start(); + } + } + }); + } else { + String consoleOutput = savedInstanceState.getString("consoleOutputString"); + mConsoleTextView.setText(consoleOutput); + boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked"); + toggleButton.setChecked(buttonCheked); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("consoleOutputString", mConsoleTextView.getText().toString()); + ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + outState.putBoolean("toggleButtonChecked", toggleButton.isChecked()); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + + String consoleOutput = savedInstanceState.getString("consoleOutputString"); + mConsoleTextView.setText(consoleOutput); + + final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked"); + toggleButton.setChecked(buttonCheked); + } + + private void msg(final String text) { + runOnUiThread(new Runnable() { + public void run() { + mConsoleTextView.append("\n"); + mConsoleTextView.append(text); + mScrollView.fullScroll(View.FOCUS_DOWN); + } + }); + Log.i(TAG, text); + } + + private void printLine() { + msg("------------------------------------------------------------------------"); + } + + private void sleep(int seconds) { + try { + Thread.sleep(seconds * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + Log.e(TAG, e.toString()); + } + } + + private void enableStartStopButton() { + runOnUiThread(new Runnable() { + public void run() { + ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + toggleButton.setEnabled(true); + } + }); + } +} diff --git a/android/examples/groupclient/src/main/res/drawable/iotivityicon.png b/android/examples/groupclient/src/main/res/drawable/iotivityicon.png new file mode 100644 index 0000000..e1e4aa7 Binary files /dev/null and b/android/examples/groupclient/src/main/res/drawable/iotivityicon.png differ diff --git a/android/examples/groupclient/src/main/res/drawable/iotivitylogo.png b/android/examples/groupclient/src/main/res/drawable/iotivitylogo.png new file mode 100644 index 0000000..a7d3115 Binary files /dev/null and b/android/examples/groupclient/src/main/res/drawable/iotivitylogo.png differ diff --git a/android/examples/groupclient/src/main/res/layout/activity_group_client.xml b/android/examples/groupclient/src/main/res/layout/activity_group_client.xml new file mode 100644 index 0000000..cebb389 --- /dev/null +++ b/android/examples/groupclient/src/main/res/layout/activity_group_client.xml @@ -0,0 +1,36 @@ + + + + + + + \ No newline at end of file diff --git a/android/examples/groupclient/src/main/res/mipmap-hdpi/iotivityicon.png b/android/examples/groupclient/src/main/res/mipmap-hdpi/iotivityicon.png new file mode 100644 index 0000000..e1e4aa7 Binary files /dev/null and b/android/examples/groupclient/src/main/res/mipmap-hdpi/iotivityicon.png differ diff --git a/android/examples/groupclient/src/main/res/mipmap-mdpi/iotivityicon.png b/android/examples/groupclient/src/main/res/mipmap-mdpi/iotivityicon.png new file mode 100644 index 0000000..2e7bce6 Binary files /dev/null and b/android/examples/groupclient/src/main/res/mipmap-mdpi/iotivityicon.png differ diff --git a/android/examples/groupclient/src/main/res/mipmap-xhdpi/iotivityicon.png b/android/examples/groupclient/src/main/res/mipmap-xhdpi/iotivityicon.png new file mode 100644 index 0000000..afa486b Binary files /dev/null and b/android/examples/groupclient/src/main/res/mipmap-xhdpi/iotivityicon.png differ diff --git a/android/examples/groupclient/src/main/res/mipmap-xxhdpi/iotivityicon.png b/android/examples/groupclient/src/main/res/mipmap-xxhdpi/iotivityicon.png new file mode 100644 index 0000000..3e6bc6a Binary files /dev/null and b/android/examples/groupclient/src/main/res/mipmap-xxhdpi/iotivityicon.png differ diff --git a/android/examples/groupclient/src/main/res/values-v21/styles.xml b/android/examples/groupclient/src/main/res/values-v21/styles.xml new file mode 100644 index 0000000..dba3c41 --- /dev/null +++ b/android/examples/groupclient/src/main/res/values-v21/styles.xml @@ -0,0 +1,5 @@ + + + + diff --git a/android/examples/groupclient/src/main/res/values-w820dp/dimens.xml b/android/examples/groupclient/src/main/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/android/examples/groupclient/src/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/android/examples/groupclient/src/main/res/values/dimens.xml b/android/examples/groupclient/src/main/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/android/examples/groupclient/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/android/examples/groupclient/src/main/res/values/strings.xml b/android/examples/groupclient/src/main/res/values/strings.xml new file mode 100644 index 0000000..d8b8630 --- /dev/null +++ b/android/examples/groupclient/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Group Client + diff --git a/android/examples/groupclient/src/main/res/values/styles.xml b/android/examples/groupclient/src/main/res/values/styles.xml new file mode 100644 index 0000000..ff6c9d2 --- /dev/null +++ b/android/examples/groupclient/src/main/res/values/styles.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/android/examples/groupserver/.gitignore b/android/examples/groupserver/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/android/examples/groupserver/.gitignore @@ -0,0 +1 @@ +/build diff --git a/android/examples/groupserver/build.gradle b/android/examples/groupserver/build.gradle new file mode 100644 index 0000000..209149f --- /dev/null +++ b/android/examples/groupserver/build.gradle @@ -0,0 +1,24 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 22 + buildToolsVersion "22.0.1" + + defaultConfig { + applicationId "org.iotivity.base.examples.groupserver" + minSdkVersion 21 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/android/examples/groupserver/groupserver.iml b/android/examples/groupserver/groupserver.iml new file mode 100644 index 0000000..b648b2f --- /dev/null +++ b/android/examples/groupserver/groupserver.iml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/examples/groupserver/proguard-rules.pro b/android/examples/groupserver/proguard-rules.pro new file mode 100644 index 0000000..d26150c --- /dev/null +++ b/android/examples/groupserver/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/rahul/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/android/examples/groupserver/src/main/AndroidManifest.xml b/android/examples/groupserver/src/main/AndroidManifest.xml new file mode 100644 index 0000000..34a0806 --- /dev/null +++ b/android/examples/groupserver/src/main/AndroidManifest.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/examples/groupserver/src/main/java/org/iotivity/base/examples/GroupServer.java b/android/examples/groupserver/src/main/java/org/iotivity/base/examples/GroupServer.java new file mode 100644 index 0000000..06089b0 --- /dev/null +++ b/android/examples/groupserver/src/main/java/org/iotivity/base/examples/GroupServer.java @@ -0,0 +1,333 @@ +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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 org.iotivity.base.examples; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; +import android.util.Log; +import android.view.View; +import android.widget.CompoundButton; +import android.widget.ScrollView; +import android.widget.TextView; +import android.widget.ToggleButton; + +import org.iotivity.base.ModeType; +import org.iotivity.base.OcConnectivityType; +import org.iotivity.base.OcException; +import org.iotivity.base.OcPlatform; +import org.iotivity.base.OcResource; +import org.iotivity.base.OcResourceHandle; +import org.iotivity.base.PlatformConfig; +import org.iotivity.base.QualityOfService; +import org.iotivity.base.ResourceProperty; +import org.iotivity.base.ServiceType; + +import java.util.EnumSet; +import java.util.LinkedList; +import java.util.List; + +/** + * GroupServer + */ +public class GroupServer extends Activity implements OcPlatform.OnResourceFoundListener { + + private OcResourceHandle mCollectionResourceHandle; + private List mProxyResourceHandleList = new LinkedList<>(); + + /** + * A local method to configure and initialize platform, register a collection resource + * and then search for the light resources.In addition it creates a local light resource and + * adds it to the collection. + */ + private synchronized void startGroupServer() { + Context context = this; + + PlatformConfig platformConfig = new PlatformConfig( + context, + ServiceType.IN_PROC, + ModeType.CLIENT_SERVER, + "0.0.0.0", 0, + QualityOfService.LOW + ); + + msg("Configuring platform."); + OcPlatform.Configure(platformConfig); + + String resourceUri = "/core/a/collection"; + String resourceTypeName = "a.collection"; + msg("Registering a collection resource."); + try { + mCollectionResourceHandle = OcPlatform.registerResource( + resourceUri, //resource URI + resourceTypeName, //resource type name + OcPlatform.BATCH_INTERFACE, //using batch interface + null, //use default entity handler + EnumSet.of(ResourceProperty.DISCOVERABLE) + ); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to register a collection resource"); + } + + if (null != mCollectionResourceHandle) { + try { + OcPlatform.bindInterfaceToResource( + mCollectionResourceHandle, + OcPlatform.GROUP_INTERFACE); + + OcPlatform.bindInterfaceToResource( + mCollectionResourceHandle, + OcPlatform.DEFAULT_INTERFACE); + } catch (OcException e) { + e.printStackTrace(); + } + + } + + msg("Sending request to find all resources with \"core.light\" type name"); + try { + OcPlatform.findResource( + "", + OcPlatform.WELL_KNOWN_QUERY + "?rt=core.light", + EnumSet.of(OcConnectivityType.CT_DEFAULT), + this); + + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to invoke find resource API"); + } + + OcResourceHandle localLightResourceHandle = null; + msg("Registering a local light resource"); + try { + localLightResourceHandle = OcPlatform.registerResource( + "/a/light/local", //resource URI + "core.light", //resource type name + OcPlatform.DEFAULT_INTERFACE, //using default interface + null, //use default entity handler + EnumSet.of(ResourceProperty.DISCOVERABLE) + ); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to register a local ligh resource"); + } + + if (null != localLightResourceHandle) { + msg("Binding a found resource proxy handle to the collection resource"); + try { + OcPlatform.bindResource(mCollectionResourceHandle, localLightResourceHandle); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to bind found resource proxy handle to the collection resource"); + } + mProxyResourceHandleList.add(localLightResourceHandle); + } + + printLine(); + } + + /** + * An event handler to be executed whenever a "findResource" request completes successfully + * + * @param foundResource found resource + */ + @Override + public synchronized void onResourceFound(OcResource foundResource) { + if (null == foundResource) { + msg("Found resource is invalid"); + return; + } + msg("Found resource with \"core.light\" type name\"."); + // Get the resource host address + String hostAddress = foundResource.getHost(); + // Get the resource URI + String resourceUri = foundResource.getUri(); + msg("\tHost address of the resource: " + hostAddress); + msg("\tURI of the resource: " + resourceUri); + // Get the resource types + msg("\tList of resource types: "); + for (String resourceType : foundResource.getResourceTypes()) { + msg("\t\t" + resourceType); + } + msg("\tList of resource interfaces:"); + for (String resourceInterface : foundResource.getResourceInterfaces()) { + msg("\t\t" + resourceInterface); + } + msg("\tList of resource connectivity types:"); + for (OcConnectivityType connectivityType : foundResource.getConnectivityTypeSet()) { + msg("\t\t" + connectivityType); + } + + //In this example we are only interested in the light resources + if (resourceUri.equals("/a/light")) { + msg("Registering a found resource as a local proxy resource"); + OcResourceHandle proxyResourceHandle = null; + try { + proxyResourceHandle = OcPlatform.registerResource(foundResource); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to register a found resource as a local proxy resource"); + } + + if (null != proxyResourceHandle) { + msg("Binding a found resource proxy handle to the collection resource"); + try { + OcPlatform.bindResource(mCollectionResourceHandle, proxyResourceHandle); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to bind found resource proxy handle to the collection resource"); + } + mProxyResourceHandleList.add(proxyResourceHandle); + } + } + + printLine(); + enableStartStopButton(); + } + + /** + * A local method to reset group server + */ + private synchronized void stopGroupServer() { + msg("Unregistering resources"); + for (OcResourceHandle proxyResourceHandle : mProxyResourceHandleList) { + try { + OcPlatform.unbindResource(mCollectionResourceHandle, proxyResourceHandle); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to unbind a proxy resource"); + } + try { + OcPlatform.unregisterResource(proxyResourceHandle); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to unregister a proxy resource"); + } + } + mProxyResourceHandleList.clear(); + + if (null != mCollectionResourceHandle) { + try { + OcPlatform.unregisterResource(mCollectionResourceHandle); + } catch (OcException e) { + Log.e(TAG, e.toString()); + msg("Failed to unregister a collection resource"); + } + } + msg("Group Server is reset."); + + printLine(); + enableStartStopButton(); + } + + //****************************************************************************** + // End of the OIC specific code + //****************************************************************************** + + private final static String TAG = GroupServer.class.getSimpleName(); + private TextView mConsoleTextView; + private ScrollView mScrollView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_group_server); + + mConsoleTextView = (TextView) findViewById(R.id.consoleTextView); + mConsoleTextView.setMovementMethod(new ScrollingMovementMethod()); + mScrollView = (ScrollView) findViewById(R.id.scrollView); + mScrollView.fullScroll(View.FOCUS_DOWN); + final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + + if (null == savedInstanceState) { + toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + toggleButton.setEnabled(false); + if (isChecked) { + new Thread(new Runnable() { + public void run() { + startGroupServer(); + } + }).start(); + } else { + new Thread(new Runnable() { + public void run() { + stopGroupServer(); + } + }).start(); + } + } + }); + } else { + String consoleOutput = savedInstanceState.getString("consoleOutputString"); + mConsoleTextView.setText(consoleOutput); + boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked"); + toggleButton.setChecked(buttonCheked); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("consoleOutputString", mConsoleTextView.getText().toString()); + ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + outState.putBoolean("toggleButtonChecked", toggleButton.isChecked()); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + + String consoleOutput = savedInstanceState.getString("consoleOutputString"); + mConsoleTextView.setText(consoleOutput); + + final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + boolean buttonCheked = savedInstanceState.getBoolean("toggleButtonChecked"); + toggleButton.setChecked(buttonCheked); + } + + private void msg(final String text) { + runOnUiThread(new Runnable() { + public void run() { + mConsoleTextView.append("\n"); + mConsoleTextView.append(text); + mScrollView.fullScroll(View.FOCUS_DOWN); + } + }); + Log.i(TAG, text); + } + + private void printLine() { + msg("------------------------------------------------------------------------"); + } + + private void enableStartStopButton() { + runOnUiThread(new Runnable() { + public void run() { + ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton); + toggleButton.setEnabled(true); + } + }); + } +} \ No newline at end of file diff --git a/android/examples/groupserver/src/main/res/drawable/iotivityicon.png b/android/examples/groupserver/src/main/res/drawable/iotivityicon.png new file mode 100644 index 0000000..e1e4aa7 Binary files /dev/null and b/android/examples/groupserver/src/main/res/drawable/iotivityicon.png differ diff --git a/android/examples/groupserver/src/main/res/drawable/iotivitylogo.png b/android/examples/groupserver/src/main/res/drawable/iotivitylogo.png new file mode 100644 index 0000000..a7d3115 Binary files /dev/null and b/android/examples/groupserver/src/main/res/drawable/iotivitylogo.png differ diff --git a/android/examples/groupserver/src/main/res/layout/activity_group_server.xml b/android/examples/groupserver/src/main/res/layout/activity_group_server.xml new file mode 100644 index 0000000..2befa76 --- /dev/null +++ b/android/examples/groupserver/src/main/res/layout/activity_group_server.xml @@ -0,0 +1,36 @@ + + + + + + + \ No newline at end of file diff --git a/android/examples/groupserver/src/main/res/mipmap-hdpi/iotivityicon.png b/android/examples/groupserver/src/main/res/mipmap-hdpi/iotivityicon.png new file mode 100644 index 0000000..e1e4aa7 Binary files /dev/null and b/android/examples/groupserver/src/main/res/mipmap-hdpi/iotivityicon.png differ diff --git a/android/examples/groupserver/src/main/res/mipmap-mdpi/iotivityicon.png b/android/examples/groupserver/src/main/res/mipmap-mdpi/iotivityicon.png new file mode 100644 index 0000000..2e7bce6 Binary files /dev/null and b/android/examples/groupserver/src/main/res/mipmap-mdpi/iotivityicon.png differ diff --git a/android/examples/groupserver/src/main/res/mipmap-xhdpi/iotivityicon.png b/android/examples/groupserver/src/main/res/mipmap-xhdpi/iotivityicon.png new file mode 100644 index 0000000..afa486b Binary files /dev/null and b/android/examples/groupserver/src/main/res/mipmap-xhdpi/iotivityicon.png differ diff --git a/android/examples/groupserver/src/main/res/mipmap-xxhdpi/iotivityicon.png b/android/examples/groupserver/src/main/res/mipmap-xxhdpi/iotivityicon.png new file mode 100644 index 0000000..3e6bc6a Binary files /dev/null and b/android/examples/groupserver/src/main/res/mipmap-xxhdpi/iotivityicon.png differ diff --git a/android/examples/groupserver/src/main/res/values-v21/styles.xml b/android/examples/groupserver/src/main/res/values-v21/styles.xml new file mode 100644 index 0000000..dba3c41 --- /dev/null +++ b/android/examples/groupserver/src/main/res/values-v21/styles.xml @@ -0,0 +1,5 @@ + + + + diff --git a/android/examples/groupserver/src/main/res/values-w820dp/dimens.xml b/android/examples/groupserver/src/main/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/android/examples/groupserver/src/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/android/examples/groupserver/src/main/res/values/dimens.xml b/android/examples/groupserver/src/main/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/android/examples/groupserver/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/android/examples/groupserver/src/main/res/values/strings.xml b/android/examples/groupserver/src/main/res/values/strings.xml new file mode 100644 index 0000000..7079346 --- /dev/null +++ b/android/examples/groupserver/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + Group Server + diff --git a/android/examples/groupserver/src/main/res/values/styles.xml b/android/examples/groupserver/src/main/res/values/styles.xml new file mode 100644 index 0000000..ff6c9d2 --- /dev/null +++ b/android/examples/groupserver/src/main/res/values/styles.xml @@ -0,0 +1,8 @@ + + + + + +