1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "JniOnDeleteResourceListener.h"
22 JniOnDeleteResourceListener::JniOnDeleteResourceListener(JNIEnv *env, jobject jListener,
23 RemoveListenerCallback removeListenerCallback)
25 m_jwListener = env->NewWeakGlobalRef(jListener);
26 m_removeListenerCallback = removeListenerCallback;
29 JniOnDeleteResourceListener::~JniOnDeleteResourceListener()
31 LOGI("~JniOnDeleteResourceListener()");
35 JNIEnv *env = GetJNIEnv(ret);
40 env->DeleteWeakGlobalRef(m_jwListener);
41 m_jwListener = nullptr;
42 if (JNI_EDETACHED == ret)
44 g_jvm->DetachCurrentThread();
49 void JniOnDeleteResourceListener::onDeleteResourceCallback(const int eCode)
51 jint envRet = JNI_ERR;
52 JNIEnv *env = GetJNIEnv(envRet);
58 jobject jListener = env->NewLocalRef(m_jwListener);
61 checkExAndRemoveListener(env);
62 if (JNI_EDETACHED == envRet)
64 g_jvm->DetachCurrentThread();
68 jclass clsL = env->GetObjectClass(jListener);
71 checkExAndRemoveListener(env);
72 if (JNI_EDETACHED == envRet)
74 g_jvm->DetachCurrentThread();
79 jint jres = static_cast<jint>(eCode);
80 jmethodID midL = env->GetMethodID(clsL, "onDeleteResourceCompleted", "(I)V");
83 checkExAndRemoveListener(env);
84 if (JNI_EDETACHED == envRet)
86 g_jvm->DetachCurrentThread();
90 env->CallVoidMethod(jListener, midL, jres);
92 checkExAndRemoveListener(env);
93 if (JNI_EDETACHED == envRet)
95 g_jvm->DetachCurrentThread();
99 void JniOnDeleteResourceListener::checkExAndRemoveListener(JNIEnv* env)
101 if (env->ExceptionCheck())
103 jthrowable ex = env->ExceptionOccurred();
104 env->ExceptionClear();
105 m_removeListenerCallback(env, m_jwListener);
106 env->Throw((jthrowable)ex);
110 m_removeListenerCallback(env, m_jwListener);