From a2b8ff8a6fa54b87e2c1d2e2d872422dcd077b7b Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 2 Feb 2016 11:23:03 +0100 Subject: [PATCH] Added LinphoneFriendList callbacks into JNI layer --- coreapi/linphonecore_jni.cc | 92 ++++++++++++++++++- .../org/linphone/core/LinphoneFriendList.java | 18 +++- .../linphone/core/LinphoneFriendListImpl.java | 9 +- 3 files changed, 112 insertions(+), 7 deletions(-) diff --git a/coreapi/linphonecore_jni.cc b/coreapi/linphonecore_jni.cc index 67e76792c..daf3ad2e4 100644 --- a/coreapi/linphonecore_jni.cc +++ b/coreapi/linphonecore_jni.cc @@ -2058,7 +2058,15 @@ extern "C" void Java_org_linphone_core_LinphoneCoreImpl_removeFriendList(JNIEnv* ,jlong lc ,jlong friendList ) { - linphone_core_remove_friend_list((LinphoneCore*)lc,(LinphoneFriendList*)friendList); + LinphoneFriendList *list = (LinphoneFriendList *)friendList; + LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(list); + if (cbs != NULL) { + jobject listener = (jobject) linphone_friend_list_cbs_get_user_data(cbs); + if (listener != NULL) { + env->DeleteGlobalRef(listener); + } + } + linphone_core_remove_friend_list((LinphoneCore*)lc, list); } extern "C" jobjectArray Java_org_linphone_core_LinphoneCoreImpl_getFriendList(JNIEnv* env @@ -3198,6 +3206,88 @@ extern "C" void Java_org_linphone_core_LinphoneFriendListImpl_synchronizeFriends linphone_friend_list_synchronize_friends_from_server((LinphoneFriendList*)list); } +static void contact_created(LinphoneFriendList *list, LinphoneFriend *lf) { + JNIEnv *env = 0; + jint result = jvm->AttachCurrentThread(&env,NULL); + if (result != 0) { + ms_error("cannot attach VM\n"); + return; + } + + LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(list); + jobject listener = (jobject) linphone_friend_list_cbs_get_user_data(cbs); + + if (listener == NULL) { + ms_error("contact_created() notification without listener"); + return ; + } + jclass clazz = (jclass) env->GetObjectClass(listener); + jmethodID method = env->GetMethodID(clazz, "onLinphoneFriendCreated","(Lorg/linphone/core/LinphoneFriendList;Lorg/linphone/core/LinphoneFriend;)V"); + jobject jlist = getFriendList(env, list); + jobject jfriend = getFriend(env, lf); + env->DeleteLocalRef(clazz); + env->CallVoidMethod(listener, method, jlist, jfriend); +} + +static void contact_updated(LinphoneFriendList *list, LinphoneFriend *lf_new, LinphoneFriend *lf_old) { + JNIEnv *env = 0; + jint result = jvm->AttachCurrentThread(&env,NULL); + if (result != 0) { + ms_error("cannot attach VM\n"); + return; + } + + LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(list); + jobject listener = (jobject) linphone_friend_list_cbs_get_user_data(cbs); + + if (listener == NULL) { + ms_error("contact_updated() notification without listener"); + return ; + } + jclass clazz = (jclass) env->GetObjectClass(listener); + jmethodID method = env->GetMethodID(clazz, "onLinphoneFriendUpdated","(Lorg/linphone/core/LinphoneFriendList;Lorg/linphone/core/LinphoneFriend;Lorg/linphone/core/LinphoneFriend;)V"); + jobject jlist = getFriendList(env, list); + jobject jfriend_new = getFriend(env, lf_new); + jobject jfriend_old = getFriend(env, lf_old); + env->DeleteLocalRef(clazz); + env->CallVoidMethod(listener, method, jlist, jfriend_new, jfriend_old); +} + +static void contact_removed(LinphoneFriendList *list, LinphoneFriend *lf) { + JNIEnv *env = 0; + jint result = jvm->AttachCurrentThread(&env,NULL); + if (result != 0) { + ms_error("cannot attach VM\n"); + return; + } + + LinphoneFriendListCbs *cbs = linphone_friend_list_get_callbacks(list); + jobject listener = (jobject) linphone_friend_list_cbs_get_user_data(cbs); + + if (listener == NULL) { + ms_error("contact_removed() notification without listener"); + return ; + } + jclass clazz = (jclass) env->GetObjectClass(listener); + jmethodID method = env->GetMethodID(clazz, "onLinphoneFriendDeleted","(Lorg/linphone/core/LinphoneFriendList;Lorg/linphone/core/LinphoneFriend;)V"); + jobject jlist = getFriendList(env, list); + jobject jfriend = getFriend(env, lf); + env->DeleteLocalRef(clazz); + env->CallVoidMethod(listener, method, jlist, jfriend); +} + +extern "C" void Java_org_linphone_core_LinphoneFriendListImpl_setListener(JNIEnv* env, jobject thiz, jlong ptr, jobject jlistener) { + jobject listener = env->NewGlobalRef(jlistener); + LinphoneFriendList *list = (LinphoneFriendList *)ptr; + LinphoneFriendListCbs *cbs; + + cbs = linphone_friend_list_get_callbacks(list); + linphone_friend_list_cbs_set_user_data(cbs, listener); + linphone_friend_list_cbs_set_contact_created(cbs, contact_created); + linphone_friend_list_cbs_set_contact_updated(cbs, contact_updated); + linphone_friend_list_cbs_set_contact_deleted(cbs, contact_removed); +} + extern "C" void Java_org_linphone_core_LinphoneFriendImpl_setAddress(JNIEnv* env ,jobject thiz ,jlong ptr diff --git a/java/common/org/linphone/core/LinphoneFriendList.java b/java/common/org/linphone/core/LinphoneFriendList.java index 836a99d70..eedadfaad 100644 --- a/java/common/org/linphone/core/LinphoneFriendList.java +++ b/java/common/org/linphone/core/LinphoneFriendList.java @@ -18,13 +18,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.linphone.core; - public interface LinphoneFriendList { - public void setRLSUri(String uri); + public void setRLSUri(String uri); public void addFriend(LinphoneFriend friend); public void updateSubscriptions(LinphoneProxyConfig proxyConfig,boolean onlyWhenRegistered); public LinphoneFriend findFriendByUri(String uri); public void setUri(String uri); public void synchronizeFriendsFromServer(); - long getNativePtr(); + long getNativePtr(); + + /** + * Set the callbacks associated with the LinphoneFriendList. + */ + void setListener(LinphoneFriendList.LinphoneFriendListListener listener); + + interface LinphoneFriendListListener { + void onLinphoneFriendCreated(LinphoneFriendList list, LinphoneFriend lf); + + void onLinphoneFriendUpdated(LinphoneFriendList list, LinphoneFriend newFriend, LinphoneFriend oldFriend); + + void onLinphoneFriendDeleted(LinphoneFriendList list, LinphoneFriend lf); + } } diff --git a/java/impl/org/linphone/core/LinphoneFriendListImpl.java b/java/impl/org/linphone/core/LinphoneFriendListImpl.java index 47dcfea44..3ec8a6efd 100644 --- a/java/impl/org/linphone/core/LinphoneFriendListImpl.java +++ b/java/impl/org/linphone/core/LinphoneFriendListImpl.java @@ -19,7 +19,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package org.linphone.core; import java.io.Serializable; -import org.linphone.core.LinphoneProxyConfigImpl; class LinphoneFriendListImpl implements LinphoneFriendList, Serializable { @@ -31,8 +30,7 @@ class LinphoneFriendListImpl implements LinphoneFriendList, Serializable { private native void updateSubscriptions(long nativePtr,long proxyConfigPtr,boolean onlyWhenRegistered); private native Object getCore(long ptr); private native LinphoneFriend findFriendByUri(long nativePtr,String uri); - - + private native void setListener(long ptr, LinphoneFriendListListener listener); protected LinphoneFriendListImpl(LinphoneCoreImpl core) { nativePtr = newLinphoneFriendList(core.nativePtr); @@ -82,6 +80,11 @@ class LinphoneFriendListImpl implements LinphoneFriendList, Serializable { } } + @Override + public void setListener(LinphoneFriendListListener listener) { + setListener(nativePtr, listener); + } + /*reserved for JNI */ protected LinphoneFriendListImpl(long aNativePtr) {