added OCAccountManager class for Android and JNI method
authorJaewook Jung <jw0213.jung@samsung.com>
Mon, 1 Aug 2016 08:50:44 +0000 (17:50 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 17 Aug 2016 08:18:41 +0000 (08:18 +0000)
Since OCAccountManager C++ class has been merged, I added implementation of the java class.
(https://gerrit.iotivity.org/gerrit/#/c/9835/)

Change-Id: I2a20dd976fa4ec6167d68f72d5dff3f2c123089d
Signed-off-by: Jaewook Jung <jw0213.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9967
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
19 files changed:
android/android_api/SConscript
android/android_api/base/build.gradle
android/android_api/base/jni/Android.mk
android/android_api/base/jni/JniListenerManager.h
android/android_api/base/jni/JniOcAccountManager.cpp [new file with mode: 0644]
android/android_api/base/jni/JniOcAccountManager.h [new file with mode: 0644]
android/android_api/base/jni/JniOcPlatform.cpp
android/android_api/base/jni/JniOcPlatform.h
android/android_api/base/jni/JniOcStack.cpp
android/android_api/base/jni/JniOcStack.h
android/android_api/base/jni/JniOnDeleteListener.cpp
android/android_api/base/jni/JniOnDeleteListener.h
android/android_api/base/jni/JniOnGetListener.cpp
android/android_api/base/jni/JniOnGetListener.h
android/android_api/base/jni/JniOnPostListener.cpp
android/android_api/base/jni/JniOnPostListener.h
android/android_api/base/src/main/java/org/iotivity/base/OcAccountManager.java [new file with mode: 0644]
android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java
build_common/SConscript

index af7bda9..24c85a2 100644 (file)
@@ -10,6 +10,12 @@ if env.get('RELEASE'):
 else:
        ANDROID_RELEASE="debug"
 ANDROID_SECURED = env.get('SECURED')
+
+if env.get('WITH_CLOUD'):
+       ANDROID_CLOUD=1
+else:
+       ANDROID_CLOUD=0
+
 ANDROID_MQ = env.get('WITH_MQ')
 ANDROID_MQ_SUB = 0
 ANDROID_MQ_PUB = 0
@@ -60,11 +66,11 @@ def ensure_libs(target, source, env):
 
 # build android_api
 jdk_env = Environment(ENV=os.environ)
-jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + ' build -b' + os.getcwd()+'/build.gradle -PTARGET_ARCH=%s -PRELEASE=%s -PSECURED=%s -DSECURE=%s -PRD_MODE=%s -PWITH_MQ_PUB=%s -PWITH_MQ_SUB=%s -PWITH_MQ_BROKER=%s --stacktrace' %(ANDROID_TARGET_ARCH, ANDROID_RELEASE, ANDROID_SECURED, ANDROID_SECURED, ANDROID_RD_MODE, ANDROID_MQ_PUB, ANDROID_MQ_SUB, ANDROID_MQ_BROKER), emitter = ensure_libs)
+jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + ' build -b' + os.getcwd()+'/build.gradle -PTARGET_ARCH=%s -PRELEASE=%s -PSECURED=%s -DSECURE=%s -PWITH_CLOUD=%s -PRD_MODE=%s -PWITH_MQ_PUB=%s -PWITH_MQ_SUB=%s -PWITH_MQ_BROKER=%s --stacktrace' %(ANDROID_TARGET_ARCH, ANDROID_RELEASE, ANDROID_SECURED, ANDROID_SECURED, ANDROID_CLOUD, ANDROID_RD_MODE, ANDROID_MQ_PUB, ANDROID_MQ_SUB, ANDROID_MQ_BROKER), emitter = ensure_libs)
 jdk_env['BUILD_DIR'] = env.get('BUILD_DIR')
 cmdBuildApi=jdk_env.Gradle(target="base/objs", source="base/src/main/java/org/iotivity/base/OcResource.java")
 
-jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + ' build -b' + 'android/examples/build.gradle -PTARGET_ARCH=%s -PRELEASE=%s -PSECURED=%s -DSECURE=%s -PRD_MODE=%s -PWITH_MQ_PUB=%s -PWITH_MQ_SUB=%s -PWITH_MQ_BROKER=%s --stacktrace' %(ANDROID_TARGET_ARCH, ANDROID_RELEASE, ANDROID_SECURED, ANDROID_SECURED, ANDROID_RD_MODE, ANDROID_MQ_PUB, ANDROID_MQ_SUB, ANDROID_MQ_BROKER))
+jdk_env['BUILDERS']['Gradle'] = Builder(action = env.get('ANDROID_GRADLE') + ' build -b' + 'android/examples/build.gradle -PTARGET_ARCH=%s -PRELEASE=%s -PSECURED=%s -DSECURE=%s -PWITH_CLOUD=%s -PRD_MODE=%s -PWITH_MQ_PUB=%s -PWITH_MQ_SUB=%s -PWITH_MQ_BROKER=%s --stacktrace' %(ANDROID_TARGET_ARCH, ANDROID_RELEASE, ANDROID_SECURED, ANDROID_SECURED, ANDROID_CLOUD, ANDROID_RD_MODE, ANDROID_MQ_PUB, ANDROID_MQ_SUB, ANDROID_MQ_BROKER))
 cmdBuildExamples=jdk_env.Gradle(target="../examples/devicediscoveryclient/apk", source="../examples/devicediscoveryclient/src/main/java/org/iotivity/base/examples/DeviceDiscoveryClient.java")
 
 # android examples require android api to be built before being invoked
index 257377f..01b294f 100755 (executable)
@@ -43,6 +43,7 @@ android {
         versionCode 1\r
         versionName "1.1.1"\r
         buildConfigField 'int', 'SECURED', SECURED\r
+        buildConfigField 'int', 'WITH_CLOUD', WITH_CLOUD\r
         buildConfigField "int", 'WITH_MQ_PUB', WITH_MQ_PUB\r
         buildConfigField "int", 'WITH_MQ_SUB', WITH_MQ_SUB\r
         buildConfigField "int", 'WITH_MQ_BROKER', WITH_MQ_BROKER\r
@@ -98,8 +99,8 @@ task buildNative(type: Exec) {
     if (System.env.ANDROID_NDK_HOME != null) {\r
         //for windows use 'ndk-build.cmd'\r
         //def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build.cmd')\r
-        def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')\r
-        commandLine ndkBuild, "APP_ABI=$TARGET_ARCH", "APP_OPTIM=$RELEASE", "SECURE=$SECURED", "RD_MODE=$RD_MODE", "WITH_MQ_PUB=$WITH_MQ_PUB", "WITH_MQ_SUB=$WITH_MQ_SUB", "WITH_MQ_BROKER=$WITH_MQ_BROKER"
+        def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
+        commandLine ndkBuild, "APP_ABI=$TARGET_ARCH", "APP_OPTIM=$RELEASE", "SECURE=$SECURED", "WITH_CLOUD=$WITH_CLOUD", "RD_MODE=$RD_MODE", "WITH_MQ_PUB=$WITH_MQ_PUB", "WITH_MQ_SUB=$WITH_MQ_SUB", "WITH_MQ_BROKER=$WITH_MQ_BROKER"
     } else {\r
         println '##################'\r
         println 'Skipping NDK build'\r
index 985f3a4..b59283b 100644 (file)
@@ -1,6 +1,7 @@
 LOCAL_PATH := $(call my-dir)\r
 TARGET_ARCH_ABI := $(APP_ABI)\r
 SECURED := $(SECURE)\r
+WITH_CLOUD := $(WITH_CLOUD)\r
 WITH_MQ_PUB := $(WITH_MQ_PUB)\r
 WITH_MQ_SUB := $(WITH_MQ_SUB)\r
 WITH_MQ_BROKER := $(WITH_MQ_BROKER)\r
@@ -57,6 +58,11 @@ include $(CLEAR_VARS)
 OIC_SRC_PATH := ../../../resource\r
 OIC_OUT_PATH := ../../../out\r
 LOCAL_MODULE    := ocstack-jni\r
+\r
+ifeq ($(WITH_CLOUD), 1)\r
+    LOCAL_CPPFLAGS += -DWITH_CLOUD\r
+endif\r
+\r
 MQ_FLAG = 0\r
 ifeq ($(WITH_MQ_PUB), 1)\r
 LOCAL_CFLAGS += -DWITH_MQ -DMQ_PUBLISHER\r
@@ -70,6 +76,7 @@ ifeq ($(WITH_MQ_BROKER), 1)
 LOCAL_CFLAGS += -DWITH_MQ -DMQ_BROKER\r
 MQ_FLAG = 1\r
 endif\r
+\r
 LOCAL_SRC_FILES :=  JniOcStack.cpp \\r
                     JniUtils.cpp \\r
                     JniEntityHandler.cpp \\r
@@ -112,6 +119,10 @@ ifeq ($(SECURED), 1)
                         JniDisplayPinListener.cpp\r
 endif\r
 \r
+ifeq ($(WITH_CLOUD), 1)\r
+    LOCAL_SRC_FILES +=  JniOcAccountManager.cpp\r
+endif\r
+\r
 LOCAL_LDLIBS := -llog\r
 LOCAL_STATIC_LIBRARIES := android-oc\r
 LOCAL_STATIC_LIBRARIES += android-octbstack\r
index 989dd3d..a1a2083 100644 (file)
@@ -27,6 +27,9 @@
 #define _JniListenerManager
 
 class JniOcResource;
+#ifdef WITH_CLOUD
+class JniOcAccountManager;
+#endif
 
 template <class T>
 class JniListenerManager
@@ -67,6 +70,7 @@ public:
             {
                 LOGD("OnEventListener: Failed to create global listener ref.");
                 delete onEventListener;
+                m_mapMutex.unlock();
                 return nullptr;
             }
             LOGD("OnEventListener: new listener");
@@ -76,6 +80,53 @@ public:
         return onEventListener;
     }
 
+#ifdef WITH_CLOUD
+    T* addListener(JNIEnv* env, jobject jListener, JniOcAccountManager* owner)
+    {
+        T *onEventListener = nullptr;
+
+        m_mapMutex.lock();
+
+        for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it)
+        {
+            if (env->IsSameObject(jListener, it->first))
+            {
+                auto refPair = it->second;
+                onEventListener = refPair.first;
+                refPair.second++;
+                it->second = refPair;
+                m_listenerMap.insert(*it);
+                LOGD("OnEventListener: ref. count is incremented");
+                break;
+            }
+        }
+
+        if (!onEventListener)
+        {
+            onEventListener = new T(env, jListener, owner);
+            jobject jgListener = env->NewGlobalRef(jListener);
+
+            if (jgListener)
+            {
+                m_listenerMap.insert(
+                        std::pair<jobject,
+                        std::pair<T*, int>>(jgListener, std::pair<T*, int>(onEventListener, 1)));
+            }
+            else
+            {
+                LOGD("OnEventListener: Failed to create global listener ref.");
+                delete onEventListener;
+                m_mapMutex.unlock();
+                return nullptr;
+            }
+            LOGD("OnEventListener: new listener");
+        }
+
+        m_mapMutex.unlock();
+        return onEventListener;
+    }
+#endif
+
     void removeListener(JNIEnv* env, jobject jListener)
     {
         m_mapMutex.lock();
diff --git a/android/android_api/base/jni/JniOcAccountManager.cpp b/android/android_api/base/jni/JniOcAccountManager.cpp
new file mode 100644 (file)
index 0000000..228bc49
--- /dev/null
@@ -0,0 +1,656 @@
+/*
+ *******************************************************************
+ *
+ * Copyright 2016 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.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+#include "JniOcAccountManager.h"
+#include "JniOcRepresentation.h"
+#include "JniUtils.h"
+
+JniOcAccountManager::JniOcAccountManager(std::shared_ptr<OCAccountManager> accountManager)
+    : m_sharedAccountManager(accountManager)
+{
+}
+
+JniOcAccountManager::~JniOcAccountManager()
+{
+    LOGD("~JniOcAccountManager()");
+
+    m_sharedAccountManager = nullptr;
+
+    jint envRet = JNI_ERR;
+    JNIEnv *env = GetJNIEnv(envRet);
+    if (nullptr == env)
+    {
+        return;
+    }
+
+    m_onGetManager.removeAllListeners(env);
+    m_onPostManager.removeAllListeners(env);
+    m_onDeleteManager.removeAllListeners(env);
+
+    if (JNI_EDETACHED == envRet)
+    {
+        g_jvm->DetachCurrentThread();
+    }
+}
+
+std::string JniOcAccountManager::host()
+{
+    return m_sharedAccountManager->host();
+}
+
+OCConnectivityType JniOcAccountManager::connectivityType() const
+{
+    return m_sharedAccountManager->connectivityType();
+}
+
+JniOcAccountManager* JniOcAccountManager::getJniOcAccountManagerPtr(JNIEnv *env, jobject thiz)
+{
+    JniOcAccountManager *accountManager = GetHandle<JniOcAccountManager>(env, thiz);
+    if (env->ExceptionCheck())
+    {
+        LOGE("Failed to get native handle from OcAccountManager");
+    }
+    if (!accountManager)
+    {
+        ThrowOcException(JNI_NO_NATIVE_POINTER, "");
+    }
+    return accountManager;
+}
+
+JniOnGetListener* JniOcAccountManager::addOnGetListener(JNIEnv* env, jobject jListener)
+{
+    return this->m_onGetManager.addListener(env, jListener, this);
+}
+
+JniOnPostListener* JniOcAccountManager::addOnPostListener(JNIEnv* env, jobject jListener)
+{
+    return this->m_onPostManager.addListener(env, jListener, this);
+}
+
+JniOnDeleteListener* JniOcAccountManager::addOnDeleteListener(JNIEnv* env, jobject jListener)
+{
+    return this->m_onDeleteManager.addListener(env, jListener, this);
+}
+
+void JniOcAccountManager::removeOnGetListener(JNIEnv* env, jobject jListener)
+{
+    this->m_onGetManager.removeListener(env, jListener);
+}
+
+void JniOcAccountManager::removeOnPostListener(JNIEnv* env, jobject jListener)
+{
+    this->m_onPostManager.removeListener(env, jListener);
+}
+
+void JniOcAccountManager::removeOnDeleteListener(JNIEnv* env, jobject jListener)
+{
+    this->m_onDeleteManager.removeListener(env, jListener);
+}
+
+OCStackResult JniOcAccountManager::signUp(JNIEnv* env, const std::string& authProvider,
+                                          const std::string& authCode, jobject jListener)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->signUp(authProvider, authCode, postCallback);
+}
+
+OCStackResult JniOcAccountManager::signUp(JNIEnv* env, const std::string& authProvider,
+                                          const std::string& authCode,
+                                          const QueryParamsMap& options, jobject jListener)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->signUp(authProvider, authCode, options, postCallback);
+}
+
+OCStackResult JniOcAccountManager::signIn(JNIEnv* env, const std::string& userUuid,
+                                          const std::string& accessToken, jobject jListener)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->signIn(userUuid, accessToken, postCallback);
+}
+
+OCStackResult JniOcAccountManager::signOut(JNIEnv* env, jobject jListener)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->signOut(postCallback);
+}
+
+OCStackResult JniOcAccountManager::refreshAccessToken(JNIEnv* env, const std::string& userUuid,
+                                                      const std::string& refreshToken,
+                                                      jobject jListener)
+{
+    JniOnPostListener *onPostListener = addOnPostListener(env, jListener);
+
+    PostCallback postCallback = [onPostListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onPostListener->onPostCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->refreshAccessToken(userUuid, refreshToken, postCallback);
+}
+
+OCStackResult JniOcAccountManager::searchUser(JNIEnv* env, const std::string& userUuid,
+                                              jobject jListener)
+{
+    JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
+
+    GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onGetListener->onGetCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->searchUser(userUuid, getCallback);
+}
+
+OCStackResult JniOcAccountManager::searchUser(JNIEnv* env, const QueryParamsMap& queryMap,
+                                              jobject jListener)
+{
+    JniOnGetListener *onGetListener = addOnGetListener(env, jListener);
+
+    GetCallback getCallback = [onGetListener](const HeaderOptions& opts,
+        const OCRepresentation& rep, const int eCode)
+    {
+        onGetListener->onGetCallback(opts, rep, eCode);
+    };
+
+    return m_sharedAccountManager->searchUser(queryMap, getCallback);
+}
+
+OCStackResult JniOcAccountManager::deleteDevice(JNIEnv* env, const std::string& deviceId,
+                                                jobject jListener)
+{
+    JniOnDeleteListener *onDeleteListener = addOnDeleteListener(env, jListener);
+
+    DeleteCallback deleteCallback = [onDeleteListener](const HeaderOptions& opts,
+        const int eCode)
+    {
+        onDeleteListener->onDeleteCallback(opts, eCode);
+    };
+
+    return m_sharedAccountManager->deleteDevice(deviceId, deleteCallback);
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    getHost
+* Signature: ()Ljava/lang/String;
+*/
+JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcAccountManager_getHost
+    (JNIEnv *env, jobject thiz)
+{
+    LOGD("OcAccountManager_getHost");
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return nullptr;
+    }
+
+    return env->NewStringUTF(accountManager->host().c_str());
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    getConnectivityTypeN
+* Signature: ()I
+*/
+JNIEXPORT jint JNICALL Java_org_iotivity_base_OcAccountManager_getConnectivityTypeN
+    (JNIEnv *env, jobject thiz)
+{
+    LOGD("OcAccountManager_getConnectivityType");
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return -1;
+    }
+
+    OCConnectivityType connectivityType = accountManager->connectivityType();
+    return static_cast<jint>(connectivityType);
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    signUp0
+* Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp0
+    (JNIEnv *env, jobject thiz, jstring jAuthProvider, jstring jAuthCode, jobject jListener)
+{
+    LOGD("OcAccountManager_signUp");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string authProvider;
+    std::string authCode;
+    if (jAuthProvider)
+    {
+        authProvider = env->GetStringUTFChars(jAuthProvider, nullptr);
+    }
+    if (jAuthCode)
+    {
+        authCode = env->GetStringUTFChars(jAuthCode, nullptr);
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->signUp(env,
+                                                      authProvider,
+                                                      authCode,
+                                                      jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_signUp");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    signUp1
+* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp1
+    (JNIEnv *env, jobject thiz, jstring jAuthProvider, jstring jAuthCode, jobject jOptionsMap,
+     jobject jListener)
+{
+    LOGD("OcAccountManager_signUp");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
+        return;
+    }
+    if (!jOptionsMap)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "options cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string authProvider;
+    std::string authCode;
+    if (jAuthProvider)
+    {
+        authProvider = env->GetStringUTFChars(jAuthProvider, nullptr);
+    }
+    if (jAuthCode)
+    {
+        authCode = env->GetStringUTFChars(jAuthCode, nullptr);
+    }
+
+    QueryParamsMap optionsMap;
+    JniUtils::convertJavaMapToQueryParamsMap(env, jOptionsMap, optionsMap);
+
+    try
+    {
+        OCStackResult result = accountManager->signUp(env,
+                                                      authProvider,
+                                                      authCode,
+                                                      optionsMap,
+                                                      jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_signUp");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    signIn0
+* Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signIn0
+    (JNIEnv *env, jobject thiz, jstring jUserUuid, jstring jAccessToken, jobject jListener)
+{
+    LOGD("OcAccountManager_signIn");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string userUuid;
+    std::string accessToken;
+    if (jUserUuid)
+    {
+        userUuid = env->GetStringUTFChars(jUserUuid, nullptr);
+    }
+    if (jAccessToken)
+    {
+        accessToken = env->GetStringUTFChars(jAccessToken, nullptr);
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->signIn(env,
+                                                      userUuid,
+                                                      accessToken,
+                                                      jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_signIn");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    signOut0
+* Signature: (Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signOut0
+    (JNIEnv *env, jobject thiz, jobject jListener)
+{
+    LOGD("OcAccountManager_signOut");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->signOut(env,
+                                                       jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_signOut");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    refreshAccessToken0
+* Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_refreshAccessToken0
+    (JNIEnv *env, jobject thiz, jstring jUserUuid, jstring jRefreshAccessToken, jobject jListener)
+{
+    LOGD("OcAccountManager_refreshAccessToken");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onPostListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string userUuid;
+    std::string refreshAccessToken;
+    if (jUserUuid)
+    {
+        userUuid = env->GetStringUTFChars(jUserUuid, nullptr);
+    }
+    if (jRefreshAccessToken)
+    {
+        refreshAccessToken = env->GetStringUTFChars(jRefreshAccessToken, nullptr);
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->refreshAccessToken(env,
+                                                                  userUuid,
+                                                                  refreshAccessToken,
+                                                                  jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_refreshAccessToken");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    searchUser0
+* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_searchUser0
+    (JNIEnv *env, jobject thiz, jstring jUserUuid, jobject jListener)
+{
+    LOGD("OcAccountManager_searchUser");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string userUuid;
+    if (jUserUuid)
+    {
+        userUuid = env->GetStringUTFChars(jUserUuid, nullptr);
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->searchUser(env,
+                                                          userUuid,
+                                                          jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_searchUser");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    searchUser1
+* Signature: (Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_searchUser1
+    (JNIEnv *env, jobject thiz, jobject jQueryMap, jobject jListener)
+{
+    LOGD("OcAccountManager_searchUser");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onGetListener cannot be null");
+        return;
+    }
+    if (!jQueryMap)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "queryMap cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    QueryParamsMap queryMap;
+    JniUtils::convertJavaMapToQueryParamsMap(env, jQueryMap, queryMap);
+
+    try
+    {
+        OCStackResult result = accountManager->searchUser(env,
+                                                          queryMap,
+                                                          jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_searchUser");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
+
+/*
+* Class:     org_iotivity_base_OcAccountManager
+* Method:    deleteDevice0
+* Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/onDeleteListener;)V
+*/
+JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_deleteDevice0
+    (JNIEnv *env, jobject thiz, jstring jDeviceId, jobject jListener)
+{
+    LOGD("OcAccountManager_deleteDevice");
+    if (!jListener)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteListener cannot be null");
+        return;
+    }
+
+    JniOcAccountManager *accountManager = JniOcAccountManager::getJniOcAccountManagerPtr(env,
+                                                                                         thiz);
+    if (!accountManager)
+    {
+        return;
+    }
+
+    std::string deviceId;
+    if (jDeviceId)
+    {
+        deviceId = env->GetStringUTFChars(jDeviceId, nullptr);
+    }
+
+    try
+    {
+        OCStackResult result = accountManager->deleteDevice(env,
+                                                            deviceId,
+                                                            jListener);
+
+        if (OC_STACK_OK != result)
+        {
+            ThrowOcException(result, "OcAccountManager_deleteDevice");
+        }
+    }
+    catch (OCException& e)
+    {
+        LOGE("%s", e.reason().c_str());
+        ThrowOcException(e.code(), e.reason().c_str());
+    }
+}
diff --git a/android/android_api/base/jni/JniOcAccountManager.h b/android/android_api/base/jni/JniOcAccountManager.h
new file mode 100644 (file)
index 0000000..24ae56f
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ *******************************************************************
+ *
+ * Copyright 2016 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.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
+#ifndef _Included_org_iotivity_base_OcAccountManager
+#define _Included_org_iotivity_base_OcAccountManager
+
+#include "JniOcStack.h"
+#include "OCAccountManager.h"
+#include "JniListenerManager.h"
+#include "JniOnGetListener.h"
+#include "JniOnPostListener.h"
+#include "JniOnDeleteListener.h"
+
+using namespace OC;
+
+class JniOcAccountManager
+{
+public:
+    JniOcAccountManager(std::shared_ptr<OCAccountManager> accountManager);
+    ~JniOcAccountManager();
+
+    std::string host();
+    OCConnectivityType connectivityType() const;
+
+    static JniOcAccountManager* getJniOcAccountManagerPtr(JNIEnv *env, jobject thiz);
+
+    OCStackResult signUp(JNIEnv* env, const std::string& authProvider, const std::string& authCode,
+                         jobject jListener);
+    OCStackResult signUp(JNIEnv* env, const std::string& authProvider, const std::string& authCode,
+                         const QueryParamsMap& options, jobject jListener);
+    OCStackResult signIn(JNIEnv* env, const std::string& userUuid, const std::string& accessToken,
+                         jobject jListener);
+    OCStackResult signOut(JNIEnv* env, jobject jListener);
+    OCStackResult refreshAccessToken(JNIEnv* env, const std::string& userUuid,
+                                     const std::string& refreshToken, jobject jListener);
+    OCStackResult searchUser(JNIEnv* env, const std::string& userUuid, jobject jListener);
+    OCStackResult searchUser(JNIEnv* env, const QueryParamsMap& queryMap, jobject jListener);
+    OCStackResult deleteDevice(JNIEnv* env, const std::string& deviceId, jobject jListener);
+
+    JniOnGetListener* addOnGetListener(JNIEnv* env, jobject jListener);
+    JniOnPostListener* addOnPostListener(JNIEnv* env, jobject jListener);
+    JniOnDeleteListener* addOnDeleteListener(JNIEnv* env, jobject jListener);
+
+    void removeOnGetListener(JNIEnv* env, jobject jListener);
+    void removeOnPostListener(JNIEnv* env, jobject jListener);
+    void removeOnDeleteListener(JNIEnv* env, jobject jListener);
+
+private:
+    JniListenerManager<JniOnGetListener> m_onGetManager;
+    JniListenerManager<JniOnPostListener> m_onPostManager;
+    JniListenerManager<JniOnDeleteListener> m_onDeleteManager;
+
+    std::shared_ptr<OCAccountManager> m_sharedAccountManager;
+};
+
+/* DO NOT EDIT THIS FILE BEYOND THIS LINE - it is machine generated */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    getHost
+    * Signature: ()Ljava/lang/String;
+    */
+    JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcAccountManager_getHost
+        (JNIEnv *, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    getConnectivityTypeN
+    * Signature: ()I
+    */
+    JNIEXPORT jint JNICALL Java_org_iotivity_base_OcAccountManager_getConnectivityTypeN
+        (JNIEnv *, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    signUp0
+    * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp0
+        (JNIEnv *, jobject, jstring, jstring, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    signUp1
+    * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signUp1
+        (JNIEnv *, jobject, jstring, jstring, jobject, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    signIn0
+    * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signIn0
+        (JNIEnv *, jobject, jstring, jstring, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    signOut0
+    * Signature: (Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_signOut0
+        (JNIEnv *, jobject, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    refreshAccessToken0
+    * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnPostListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_refreshAccessToken0
+        (JNIEnv *, jobject, jstring, jstring, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    searchUser0
+    * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_searchUser0
+        (JNIEnv *env, jobject thiz, jstring juserUuid, jobject jListener);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    searchUser1
+    * Signature: (Ljava/util/Map;Lorg/iotivity/base/OcAccountManager/OnGetListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_searchUser1
+        (JNIEnv *env, jobject thiz, jobject jQueryMap, jobject jListener);
+
+    /*
+    * Class:     org_iotivity_base_OcAccountManager
+    * Method:    deleteDevice0
+    * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcAccountManager/onDeleteListener;)V
+    */
+    JNIEXPORT void JNICALL Java_org_iotivity_base_OcAccountManager_deleteDevice0
+        (JNIEnv *env, jobject thiz, jstring jDeviceId, jobject jListener);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
index 00917c7..231f1c9 100644 (file)
 #include "JniUtils.h"
 #include "ocpayload.h"
 
+#ifdef WITH_CLOUD
+#include "JniOcAccountManager.h"
+#endif
+
 using namespace OC;
 
 JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener)
@@ -2418,7 +2422,6 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObj
     }
 
     JniOcResource *jniOcResource = new JniOcResource(resource);
-    jlong handle = reinterpret_cast<jlong>(jniOcResource);
 
     jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor);
     if (!jResource)
@@ -2773,3 +2776,63 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_deleteResourceFromRD1(
     return;
 #endif
 }
+
+/*
+* Class:     org_iotivity_base_OcPlatform
+* Method:    constructAccountManagerObject0
+* Signature: (Ljava/lang/String;I)Lorg/iotivity/base/OcAccountManager;
+*/
+JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0(
+    JNIEnv *env,
+    jclass clazz,
+    jstring jHost,
+    jint jConnectivityType)
+{
+#ifndef WITH_CLOUD
+    ThrowOcException(OC_STACK_ERROR,
+                     "OCAccountManager is not supported. (Please build with WITH_CLOUD=1 option)");
+    return nullptr;
+#else
+    LOGD("OcPlatform_constructAccountManagerObject");
+    if (!jHost)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null");
+        return nullptr;
+    }
+
+    const char* charHost = env->GetStringUTFChars(jHost, nullptr);
+    if (!charHost)
+    {
+        ThrowOcException(JNI_EXCEPTION, "charHost is null");
+        return nullptr;
+    }
+    std::string host(charHost);
+    env->ReleaseStringUTFChars(jHost, charHost);
+
+    std::shared_ptr<OCAccountManager> accountManager = OCPlatform::constructAccountManagerObject(
+        host,
+        static_cast<OCConnectivityType>(jConnectivityType));
+
+    if (!accountManager)
+    {
+        ThrowOcException(OC_STACK_ERROR, "Failed to create OCAccountManager");
+        return nullptr;
+    }
+
+    JniOcAccountManager *jniOcAccountManager = new JniOcAccountManager(accountManager);
+
+    jobject jAccountManager = env->NewObject(g_cls_OcAccountManager, g_mid_OcAccountManager_ctor);
+    if (!jAccountManager)
+    {
+        delete jniOcAccountManager;
+        return nullptr;
+    }
+    SetHandle<JniOcAccountManager>(env, jAccountManager, jniOcAccountManager);
+    if (env->ExceptionCheck())
+    {
+        delete jniOcAccountManager;
+        return nullptr;
+    }
+    return jAccountManager;
+#endif
+}
index f4b733f..e2bb0a3 100644 (file)
@@ -369,6 +369,15 @@ extern "C" {
      */
     JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_deleteResourceFromRD1
         (JNIEnv *, jclass, jstring, jint, jobjectArray, jobject, jint);
+
+    /*
+    * Class:     org_iotivity_base_OcPlatform
+    * Method:    constructAccountManagerObject0
+    * Signature: (Ljava/lang/String;I)Lorg/iotivity/base/OcAccountManager;
+    */
+    JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0
+        (JNIEnv *, jclass, jstring, jint);
+
 #ifdef __cplusplus
 }
 #endif
index 29666a4..ff0ceef 100644 (file)
@@ -79,6 +79,9 @@ jclass g_cls_OcOicSecAcl_resr = nullptr;
 jclass g_cls_OcOicSecAcl_validity = nullptr;
 jclass g_cls_OcOicSecPdAcl = nullptr;
 jclass g_cls_OcDirectPairDevice = nullptr;
+#ifdef WITH_CLOUD
+jclass g_cls_OcAccountManager = nullptr;
+#endif
 
 jmethodID g_mid_Integer_ctor = nullptr;
 jmethodID g_mid_Double_ctor = nullptr;
@@ -114,6 +117,9 @@ jmethodID g_mid_OcProvisionResult_ctor = nullptr;
 jmethodID g_mid_OcSecureResource_ctor = nullptr;
 jmethodID g_mid_OcDirectPairDevice_ctor = nullptr;
 jmethodID g_mid_OcDirectPairDevice_dev_ctor = nullptr;
+#ifdef WITH_CLOUD
+jmethodID g_mid_OcAccountManager_ctor = nullptr;
+#endif
 
 jmethodID g_mid_OcOicSecPdAcl_get_resources_cnt = nullptr;
 jmethodID g_mid_OcOicSecPdAcl_get_resources = nullptr;
@@ -477,6 +483,17 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
     VERIFY_VARIABLE_NULL(g_mid_OcDirectPairDevice_dev_ctor);
     env->DeleteLocalRef(clazz);
 
+#ifdef WITH_CLOUD
+    //OcAccountManager
+    clazz = env->FindClass("org/iotivity/base/OcAccountManager");
+    VERIFY_VARIABLE_NULL(clazz);
+    g_cls_OcAccountManager = (jclass)env->NewGlobalRef(clazz);
+    env->DeleteLocalRef(clazz);
+
+    g_mid_OcAccountManager_ctor = env->GetMethodID(g_cls_OcAccountManager, "<init>", "(J)V");
+    VERIFY_VARIABLE_NULL(g_mid_OcAccountManager_ctor);
+#endif
+
     //OicSecAcl
     clazz = env->FindClass("org/iotivity/base/OicSecAcl");
     VERIFY_VARIABLE_NULL(clazz);
@@ -619,6 +636,10 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
     env->DeleteGlobalRef(g_cls_OcResourceIdentifier);
     env->DeleteGlobalRef(g_cls_OcSecureResource);
     env->DeleteGlobalRef(g_cls_OcProvisionResult);
+    env->DeleteGlobalRef(g_cls_OcDirectPairDevice);
+#ifdef WITH_CLOUD
+    env->DeleteGlobalRef(g_cls_OcAccountManager);
+#endif
     env->DeleteGlobalRef(g_cls_OcOicSecAcl);
     env->DeleteGlobalRef(g_cls_OcOicSecAcl_ace);
     env->DeleteGlobalRef(g_cls_OcOicSecAcl_resr);
index 273a6ac..600f0ab 100644 (file)
@@ -85,6 +85,9 @@ extern jclass g_cls_OcSecureResource;
 extern jclass g_cls_OcOicSecAcl;
 extern jclass g_cls_OcOicSecPdAcl;
 extern jclass g_cls_OcDirectPairDevice;
+#ifdef WITH_CLOUD
+extern jclass g_cls_OcAccountManager;
+#endif
 
 extern jclass g_cls_OcOicSecAcl_ace;
 extern jclass g_cls_OcOicSecAcl_resr;
@@ -123,6 +126,9 @@ extern jmethodID g_mid_OcProvisionResult_ctor;
 extern jmethodID g_mid_OcSecureResource_ctor;
 extern jmethodID g_mid_OcDirectPairDevice_ctor;
 extern jmethodID g_mid_OcDirectPairDevice_dev_ctor;
+#ifdef WITH_CLOUD
+extern jmethodID g_mid_OcAccountManager_ctor;
+#endif
 
 extern jmethodID g_mid_OcOicSecAcl_get_rownerID;
 extern jmethodID g_mid_OcOicSecAcl_get_aces;
index 4f95a4c..70871e9 100644 (file)
@@ -23,6 +23,9 @@
 #include "JniOnDeleteListener.h"
 #include "JniOcResource.h"
 #include "JniUtils.h"
+#ifdef WITH_CLOUD
+#include "JniOcAccountManager.h"
+#endif
 
 JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
     : m_ownerResource(owner)
@@ -30,6 +33,14 @@ JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcRe
     m_jwListener = env->NewWeakGlobalRef(jListener);
 }
 
+#ifdef WITH_CLOUD
+JniOnDeleteListener::JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
+    : m_ownerAccountManager(owner)
+{
+    m_jwListener = env->NewWeakGlobalRef(jListener);
+}
+#endif
+
 JniOnDeleteListener::~JniOnDeleteListener()
 {
     if (m_jwListener)
@@ -143,11 +154,33 @@ void JniOnDeleteListener::checkExAndRemoveListener(JNIEnv* env)
     {
         jthrowable ex = env->ExceptionOccurred();
         env->ExceptionClear();
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnDeleteListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnDeleteListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnDeleteListener(env, m_jwListener);
+        }
+#endif
         env->Throw((jthrowable)ex);
     }
     else
     {
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnDeleteListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnDeleteListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnDeleteListener(env, m_jwListener);
+        }
+#endif
     }
 }
index 20de33f..9354232 100644 (file)
 using namespace OC;
 
 class JniOcResource;
+#ifdef WITH_CLOUD
+class JniOcAccountManager;
+#endif
 
 class JniOnDeleteListener
 {
 public:
     JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcResource* owner);
+#ifdef WITH_CLOUD
+    JniOnDeleteListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner);
+#endif
     ~JniOnDeleteListener();
 
     void onDeleteCallback(const HeaderOptions&, const int eCode);
@@ -39,7 +45,10 @@ public:
 private:
     jweak m_jwListener;
     JniOcResource* m_ownerResource;
+#ifdef WITH_CLOUD
+    JniOcAccountManager* m_ownerAccountManager;
+#endif
     void checkExAndRemoveListener(JNIEnv *env);
 };
 
-#endif
\ No newline at end of file
+#endif
index 1a46903..496c5a6 100644 (file)
@@ -23,6 +23,9 @@
 #include "JniOcResource.h"
 #include "JniOcRepresentation.h"
 #include "JniUtils.h"
+#ifdef WITH_CLOUD
+#include "JniOcAccountManager.h"
+#endif
 
 JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
     : m_ownerResource(owner)
@@ -30,6 +33,14 @@ JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcResource
     m_jwListener = env->NewWeakGlobalRef(jListener);
 }
 
+#ifdef WITH_CLOUD
+JniOnGetListener::JniOnGetListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
+    : m_ownerAccountManager(owner)
+{
+    m_jwListener = env->NewWeakGlobalRef(jListener);
+}
+#endif
+
 JniOnGetListener::~JniOnGetListener()
 {
     LOGD("~JniOnGetListener");
@@ -169,11 +180,33 @@ void JniOnGetListener::checkExAndRemoveListener(JNIEnv* env)
     {
         jthrowable ex = env->ExceptionOccurred();
         env->ExceptionClear();
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnGetListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnGetListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
+        }
+#endif
         env->Throw((jthrowable)ex);
     }
     else
     {
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnGetListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnGetListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnGetListener(env, m_jwListener);
+        }
+#endif
     }
 }
index a37b423..abffaa0 100644 (file)
 using namespace OC;
 
 class JniOcResource;
+#ifdef WITH_CLOUD
+class JniOcAccountManager;
+#endif
 
 class JniOnGetListener
 {
 public:
     JniOnGetListener(JNIEnv *env, jobject listener, JniOcResource* resource);
+#ifdef WITH_CLOUD
+    JniOnGetListener(JNIEnv *env, jobject listener, JniOcAccountManager* resource);
+#endif
     ~JniOnGetListener();
     void onGetCallback(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
+
 private:
     jweak m_jwListener;
     JniOcResource* m_ownerResource;
+#ifdef WITH_CLOUD
+    JniOcAccountManager* m_ownerAccountManager;
+#endif
     void checkExAndRemoveListener(JNIEnv *env);
 };
 
-#endif
\ No newline at end of file
+#endif
index ee75520..e28079b 100644 (file)
@@ -23,6 +23,9 @@
 #include "JniOcResource.h"
 #include "JniOcRepresentation.h"
 #include "JniUtils.h"
+#ifdef WITH_CLOUD
+#include "JniOcAccountManager.h"
+#endif
 
 JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
     : m_ownerResource(owner)
@@ -30,6 +33,14 @@ JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResour
     m_jwListener = env->NewWeakGlobalRef(jListener);
 }
 
+#ifdef WITH_CLOUD
+JniOnPostListener::JniOnPostListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner)
+    : m_ownerAccountManager(owner)
+{
+    m_jwListener = env->NewWeakGlobalRef(jListener);
+}
+#endif
+
 JniOnPostListener::~JniOnPostListener()
 {
     if (m_jwListener)
@@ -168,11 +179,33 @@ void JniOnPostListener::checkExAndRemoveListener(JNIEnv* env)
     {
         jthrowable ex = env->ExceptionOccurred();
         env->ExceptionClear();
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnPostListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnPostListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
+        }
+#endif
         env->Throw((jthrowable)ex);
     }
     else
     {
+#ifndef WITH_CLOUD
         m_ownerResource->removeOnPostListener(env, m_jwListener);
+#else
+        if (m_ownerResource)
+        {
+            m_ownerResource->removeOnPostListener(env, m_jwListener);
+        }
+        if (m_ownerAccountManager)
+        {
+            m_ownerAccountManager->removeOnPostListener(env, m_jwListener);
+        }
+#endif
     }
 }
index 5dfe525..bf3387c 100644 (file)
 using namespace OC;
 
 class JniOcResource;
+#ifdef WITH_CLOUD
+class JniOcAccountManager;
+#endif
 
 class JniOnPostListener
 {
 public:
     JniOnPostListener(JNIEnv *env, jobject jListener, JniOcResource* owner);
+#ifdef WITH_CLOUD
+    JniOnPostListener(JNIEnv *env, jobject jListener, JniOcAccountManager* owner);
+#endif
     ~JniOnPostListener();
 
     void onPostCallback(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode);
@@ -39,7 +45,10 @@ public:
 private:
     jweak m_jwListener;
     JniOcResource* m_ownerResource;
+#ifdef WITH_CLOUD
+    JniOcAccountManager* m_ownerAccountManager;
+#endif
     void checkExAndRemoveListener(JNIEnv *env);
 };
 
-#endif
\ No newline at end of file
+#endif
diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcAccountManager.java b/android/android_api/base/src/main/java/org/iotivity/base/OcAccountManager.java
new file mode 100644 (file)
index 0000000..28b7377
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+ *******************************************************************
+ *
+ * Copyright 2016 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 org.iotivity.base;
+
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map;
+
+public final class OcAccountManager {
+
+    private OcAccountManager(long nativeHandle) {
+        this.mNativeHandle = nativeHandle;
+    }
+
+    /**
+     * Method to get the host address of account server
+     *
+     * @return String host address
+     */
+    public native String getHost();
+
+    /**
+     * Method to get the connectivity type for account server
+     *
+     * @return EnumSet<OcConnectivityType></OcConnectivityType> connectivity type set
+     */
+    public EnumSet<OcConnectivityType> getConnectivityTypeSet() {
+        return OcConnectivityType.convertToEnumSet(
+                this.getConnectivityTypeN()
+        );
+    }
+
+    private native int getConnectivityTypeN();
+
+    /**
+     * Method for account registration to account server
+     *
+     * @param authProvider   Provider name used for authentication.
+     * @param authCode       The authorization code obtained by using an authorization server
+     *                       as an intermediary between the client and resource owner.
+     * @param onPostListener event handler The event handler will be invoked with a map of
+     *                       attribute name and values.
+     * @throws OcException if failure
+     */
+    public void signUp(String authProvider,
+                       String authCode,
+                       OnPostListener onPostListener) throws OcException {
+        this.signUp0(authProvider, authCode, onPostListener);
+    }
+
+    private native void signUp0(String authProvider,
+                                String authCode,
+                                OnPostListener onPostListener) throws OcException;
+
+    /**
+     * overload
+     *
+     * @param authProvider   Provider name used for authentication.
+     * @param authCode       The authorization code obtained by using an authorization server
+     *                       as an intermediary between the client and resource owner.
+     * @param options        The option values depends on auth provider.
+     * @param onPostListener event handler The event handler will be invoked with a map of
+     *                       attribute name and values.
+     * @throws OcException if failure
+     */
+    public void signUp(String authProvider,
+                       String authCode,
+                       Map<String, String> options,
+                       OnPostListener onPostListener) throws OcException {
+        this.signUp1(authProvider, authCode, options, onPostListener);
+    }
+
+    private native void signUp1(String authProvider,
+                                String authCode,
+                                Map<String, String> options,
+                                OnPostListener onPostListener) throws OcException;
+
+    /**
+     * Method for sign-in to account server
+     *
+     * @param userUuid       Identifier of the user obtained by account registration.
+     * @param accessToken    Identifier of the resource obtained by account registration.
+     * @param onPostListener event handler The event handler will be invoked with a map of
+     *                       attribute name and values.
+     * @throws OcException if failure
+     */
+    public void signIn(String userUuid,
+                       String accessToken,
+                       OnPostListener onPostListener) throws OcException {
+        this.signIn0(userUuid, accessToken, onPostListener);
+    }
+
+    private native void signIn0(String userUuid,
+                                String accessToken,
+                                OnPostListener onPostListener) throws OcException;
+
+    /**
+     * Method for sign-out to account server
+     *
+     * @param onPostListener event handler The event handler will be invoked with a map of
+     *                       attribute name and values.
+     * @throws OcException if failure
+     */
+    public void signOut(OnPostListener onPostListener) throws OcException {
+        this.signOut0(onPostListener);
+    }
+
+    private native void signOut0(OnPostListener onPostListener) throws OcException;
+
+    /**
+     * Method to refresh access token to account server
+     *
+     * @param userUuid       Identifier of the user obtained by account registration.
+     * @param refreshToken   Refresh token used for access token refresh.
+     * @param onPostListener event handler The event handler will be invoked with a map of
+     *                       attribute name and values.
+     * @throws OcException if failure
+     */
+    public void refreshAccessToken(String userUuid,
+                                   String refreshToken,
+                                   OnPostListener onPostListener) throws OcException {
+        this.refreshAccessToken0(userUuid, refreshToken, onPostListener);
+    }
+
+    private native void refreshAccessToken0(String userUuid,
+                                            String refreshToken,
+                                            OnPostListener onPostListener) throws OcException;
+
+    /**
+     * Method to get information of the user to account server
+     *
+     * @param userUuid      Identifier of the user to get information.
+     * @param onGetListener The event handler will be invoked with a map of attribute name and
+     *                      values. The event handler will also have the result from this Get
+     *                      operation This will have error codes
+     * @throws OcException if failure
+     */
+    public void searchUser(String userUuid,
+                           OnGetListener onGetListener) throws OcException {
+        this.searchUser0(userUuid, onGetListener);
+    }
+
+    private native void searchUser0(String userUuid,
+                                    OnGetListener onGetListener) throws OcException;
+
+    /**
+     * Overload
+     *
+     * @param queryMap       Map which can have the query key and value for specific users.
+     *                       account server can response information of more than one user.
+     * @param onGetListener  The event handler will be invoked with a map of attribute name and
+     *                       values. The event handler will also have the result from this Get
+     *                       operation This will have error codes
+     * @throws OcException if failure
+     */
+    public void searchUser(Map<String, String> queryMap,
+                           OnGetListener onGetListener) throws OcException {
+        this.searchUser1(queryMap, onGetListener);
+    }
+
+    private native void searchUser1(Map<String, String> queryMap,
+                                    OnGetListener onGetListener) throws OcException;
+
+    /**
+     * Method to delete the device registered on the account signed-in
+     *
+     * @param deviceId         Device ID to delete.
+     * @param onDeleteListener event handler The event handler will have headerOptionList
+     * @throws OcException if failure
+     */
+    public void deleteDevice(String deviceId,
+                             OnDeleteListener onDeleteListener) throws OcException {
+        this.deleteDevice0(deviceId, onDeleteListener);
+    }
+
+    private native void deleteDevice0(String deviceId,
+                                      OnDeleteListener onDeleteListener) throws OcException;
+
+    /**
+     * An OnGetListener can be registered via the resource get call.
+     * Event listeners are notified asynchronously
+     */
+    public interface OnGetListener {
+        public void onGetCompleted(List<OcHeaderOption> headerOptionList,
+                                   OcRepresentation ocRepresentation);
+
+        public void onGetFailed(Throwable ex);
+    }
+
+    /**
+     * An OnPostListener can be registered via the resource post call.
+     * Event listeners are notified asynchronously
+     */
+    public interface OnPostListener {
+            public void onPostCompleted(List<OcHeaderOption> headerOptionList,
+                                        OcRepresentation ocRepresentation);
+
+            public void onPostFailed(Throwable ex);
+    }
+
+    /**
+     * An OnDeleteListener can be registered via the resource delete call.
+     * Event listeners are notified asynchronously
+     */
+    public interface OnDeleteListener {
+        public void onDeleteCompleted(List<OcHeaderOption> headerOptionList);
+
+        public void onDeleteFailed(Throwable ex);
+    }
+
+    private long mNativeHandle;
+}
+
index 6fd9d31..5160221 100644 (file)
@@ -1340,4 +1340,39 @@ public final class OcPlatform {
         OcPlatform.initCheck();
         return sPlatformQualityOfService;
     }
+
+    /**
+     * Create an account manager object that can be used for doing request to account server.
+     * You can only create this object if OCPlatform was initialized to be a Client or
+     * Client/Server. Otherwise, this will return an empty shared ptr.
+     *
+     * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
+     *       methods of this object to work since device id is not generated on Client mode.
+     *
+     * @param host                Host IP Address of a account server.
+     * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
+     * @return new AccountManager object
+     * @throws OcException if failure
+     */
+    public static OcAccountManager constructAccountManagerObject(
+            String host,
+            EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
+        OcPlatform.initCheck();
+        int connTypeInt = 0;
+
+        for (OcConnectivityType connType : OcConnectivityType.values()) {
+            if (connectivityTypeSet.contains(connType))
+            {
+                connTypeInt |= connType.getValue();
+            }
+        }
+        return OcPlatform.constructAccountManagerObject0(
+                host,
+                connTypeInt
+                );
+    }
+
+    private static native OcAccountManager constructAccountManagerObject0(
+            String host,
+            int connectivityType) throws OcException;
 }
index cc98457..f8085d4 100644 (file)
@@ -87,7 +87,7 @@ help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map
 help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
 help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
 help_vars.Add(ListVariable('WITH_MQ', 'Build with MQ publisher/broker', 'OFF', ['OFF', 'SUB', 'PUB', 'BROKER']))
-help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including Cloud Connector and Cloud Client sample', False))
+help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including AccountManager class and Cloud Client sample', False))
 help_vars.Add(ListVariable('RD_MODE', 'Resource Directory build mode', 'CLIENT', ['CLIENT', 'SERVER']))
 
 help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))