Merge branch 'cloud-interface'
[platform/upstream/iotivity.git] / service / notification / android / notification-service / src / main / jni / common / JniNotificationCommon.cpp
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "JniNotificationCommon.h"\r
22 #include <cstddef>\r
23 \r
24 static jclass g_cls_NSException = NULL;\r
25 static jmethodID g_mid_NSException_ctor = NULL;\r
26 static jmethodID g_mid_NSException_setNativeExceptionLocation = NULL;\r
27 \r
28 static const char *NSResultToChar(const int nsresult)\r
29 {\r
30     switch (nsresult)\r
31     {\r
32         case JNI_NS_OK:\r
33             return "OK";\r
34         case JNI_NS_ERROR:\r
35             return "ERROR";\r
36         case JNI_NS_SUCCESS:\r
37             return "SUCCESS";\r
38         case JNI_NS_FAIL:\r
39             return "FAIL";\r
40         case JNI_NS_ALLOW:\r
41             return "ALLOW";\r
42         case JNI_NS_DENY:\r
43             return "DENY";\r
44         case JNI_EXCEPTION:\r
45             return "JNI_EXCEPTION";\r
46         case JNI_NO_NATIVE_POINTER:\r
47             return "JNI_NO_NATIVE_POINTER";\r
48         case JNI_INVALID_VALUE:\r
49             return "JNI_INVALID_VALUE";\r
50         default:\r
51             return "";\r
52     }\r
53 }\r
54 \r
55 \r
56 jobject getNSException(JNIEnv *env, const char *file, const char *functionName,\r
57                        const int line, const int code, const char *message)\r
58 {\r
59     const char *codeChar = NSResultToChar(code);\r
60     if (codeChar[0] == '\0')\r
61     {\r
62         codeChar = NSResultToChar(JNI_INVALID_VALUE);\r
63     }\r
64     jobject exception = env->NewObject(\r
65                             g_cls_NSException,\r
66                             g_mid_NSException_ctor,\r
67                             env->NewStringUTF(codeChar),\r
68                             env->NewStringUTF(message));\r
69     if (!exception)\r
70     {\r
71         return NULL;\r
72     }\r
73     env->CallVoidMethod(\r
74         exception,\r
75         g_mid_NSException_setNativeExceptionLocation,\r
76         env->NewStringUTF(file),\r
77         env->NewStringUTF(functionName),\r
78         line);\r
79     if (env->ExceptionCheck())\r
80     {\r
81         return NULL;\r
82     }\r
83     return exception;\r
84 }\r
85 \r
86 void throwNSException(JNIEnv *env, jobject exception)\r
87 {\r
88     env->Throw((jthrowable)exception);\r
89 }\r
90 \r
91 int NSExceptionInit(JNIEnv *env)\r
92 {\r
93     if (!env)\r
94     {\r
95         LOGE ("JNIEnv is null");\r
96         return JNI_ERR;\r
97     }\r
98 \r
99     //OcException\r
100     jclass localNSException = env->FindClass(\r
101                                   "org/iotivity/service/ns/common/NSException");\r
102     if (!localNSException)\r
103     {\r
104         LOGE ("Failed to get local NSException");\r
105         return JNI_ERR;\r
106     }\r
107     g_cls_NSException = (jclass)env->NewGlobalRef(localNSException);\r
108 \r
109 \r
110     g_mid_NSException_ctor = env->GetMethodID(g_cls_NSException,\r
111                              "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");\r
112     if (!g_mid_NSException_ctor)\r
113     {\r
114         LOGE ("Failed to Get MethodID");\r
115         return JNI_ERR;\r
116     }\r
117 \r
118     g_mid_NSException_setNativeExceptionLocation = env->GetMethodID(g_cls_NSException,\r
119             "setNativeExceptionLocation",\r
120             "(Ljava/lang/String;Ljava/lang/String;I)V");\r
121     if (!g_mid_NSException_setNativeExceptionLocation)\r
122     {\r
123         LOGE ("Failed to Get MethodID");\r
124         return JNI_ERR;\r
125     }\r
126 \r
127     env->DeleteLocalRef(localNSException);\r
128 \r
129     return JNI_CURRENT_VERSION;\r
130 }\r