linphonecore_jni.cc 356 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
linphonecore_jni.cc
Copyright (C) 2010  Belledonne Communications, Grenoble, France

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
 */
#include <jni.h>
20
#ifdef USE_JAVAH
21
#include "linphonecore_jni.h"
22
#endif
23
#include "linphone/core_utils.h"
24
#include <mediastreamer2/zrtp.h>
jehan's avatar
jehan committed
25

Guillaume Beraudo's avatar
Guillaume Beraudo committed
26

27 28
extern "C" {
#include "mediastreamer2/mediastream.h"
29
#include "mediastreamer2/mscommon.h"
30
#include "mediastreamer2/msmediaplayer.h"
31
#include "mediastreamer2/msutils.h"
32
#include "mediastreamer2/devices.h"
33
}
Simon Morlat's avatar
Simon Morlat committed
34
#include "mediastreamer2/msjava.h"
35 36
#include "linphone/core.h"
#include "linphone/tunnel.h"
37
#include "linphone/account_creator.h"
38
#include "linphone/wrapper_utils.h"
39 40

#include "private.h" /*Included for multicast and wifi lock management by linphonecore. Very ugly.*/
41 42
#include <cpu-features.h>

43
#include "linphone/lpconfig.h"
44

45
#ifdef __ANDROID__
46
#include <android/log.h>
47
#include <belle-sip/wakelock.h>
48
#endif /* __ANDROID__ */
49

Ghislain MARY's avatar
Ghislain MARY committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#define RETURN_USER_DATA_OBJECT(javaclass, funcprefix, cobj) \
	{ \
		jclass jUserDataObjectClass; \
		jmethodID jUserDataObjectCtor; \
		jobject jUserDataObj; \
		jUserDataObj = (jobject)funcprefix ## _get_user_data(cobj); \
		if (jUserDataObj == NULL) { \
			jUserDataObjectClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/" javaclass)); \
			jUserDataObjectCtor = env->GetMethodID(jUserDataObjectClass,"<init>", "(J)V"); \
			jUserDataObj = env->NewObject(jUserDataObjectClass, jUserDataObjectCtor, (jlong)funcprefix ## _ref(cobj)); \
			jUserDataObj = env->NewGlobalRef(jUserDataObj); \
			funcprefix ## _set_user_data(cobj, jUserDataObj); \
			env->DeleteGlobalRef(jUserDataObjectClass); \
		} \
		return jUserDataObj; \
	}

67
static JavaVM *jvm=0;
68
static const char* LogDomain = "Linphone";
69 70 71
static jclass handler_class;
static jmethodID loghandler_id;
static jobject handler_obj=NULL;
72

73
static jobject create_java_linphone_content(JNIEnv *env, const LinphoneContent *content);
74
static jobject create_java_linphone_buffer(JNIEnv *env, const LinphoneBuffer *buffer);
75
static LinphoneBuffer* create_c_linphone_buffer_from_java_linphone_buffer(JNIEnv *env, jobject jbuffer);
76
static jobject getTunnelConfig(JNIEnv *env, LinphoneTunnelConfig *cfg);
77

78
#ifdef __ANDROID__
79
void linphone_android_log_handler(int prio, char *str) {
80 81
	char *current;
	char *next;
82 83

	if (strlen(str) < 512) {
84
		__android_log_write(prio, LogDomain, str);
85 86 87
	} else {
		current = str;
		while ((next = strchr(current, '\n')) != NULL) {
88

89
			*next = '\0';
90 91
			if (next != str && next[-1] == '\r')
				next[-1] = '\0';
92
			__android_log_write(prio, LogDomain, current);
93 94
			current = next + 1;
		}
95
		__android_log_write(prio, LogDomain, current);
96
	}
97
}
98

99
extern "C" void  Java_org_linphone_core_LinphoneCoreFactoryImpl__1setLogHandler(JNIEnv *env, jobject jfactory, jobject jhandler) {
Sylvain Berfini's avatar
Sylvain Berfini committed
100 101 102 103 104
	if (handler_obj) {
		env->DeleteGlobalRef(handler_obj);
		handler_obj = NULL;
	}
	if (jhandler) {
105 106
		handler_class = (jclass) env->GetObjectClass(jhandler);
		loghandler_id = env->GetMethodID(handler_class, "log", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)V");
107

108 109 110
		if (loghandler_id == NULL) {
			ms_fatal("log method not found");
		}
Sylvain Berfini's avatar
Sylvain Berfini committed
111 112 113 114
		handler_obj = env->NewGlobalRef(jhandler);
	}
}

Simon Morlat's avatar
Simon Morlat committed
115
static void linphone_android_ortp_log_handler(const char *domain, OrtpLogLevel lev, const char *fmt, va_list args) {
Erwan Croze's avatar
Erwan Croze committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	char* str = bctbx_strdup_vprintf(fmt, args);
    const char *levname = "undef";

    if (str == NULL) return;

    int prio;
    switch(lev) {
    	case ORTP_DEBUG:	prio = ANDROID_LOG_DEBUG;	levname="debug"; break;
    	case ORTP_MESSAGE:	prio = ANDROID_LOG_INFO;	levname="message"; break;
    	case ORTP_WARNING:	prio = ANDROID_LOG_WARN;	levname="warning"; break;
    	case ORTP_ERROR:	prio = ANDROID_LOG_ERROR;	levname="error"; break;
    	case ORTP_FATAL:	prio = ANDROID_LOG_FATAL;	levname="fatal"; break;
    	default:			prio = ANDROID_LOG_DEFAULT;	break;
    }

    if (handler_obj) {
    	JNIEnv *env = ms_get_jni_env();
    	jstring jdomain = env->NewStringUTF(LogDomain);
    	jstring jlevname = env->NewStringUTF(levname);
    	jstring jstr = env->NewStringUTF(str);
    	env->CallVoidMethod(handler_obj, loghandler_id, jdomain, (jint)lev, jlevname, jstr, NULL);
    	if (jdomain) env->DeleteLocalRef(jdomain);
    	if (jlevname) env->DeleteLocalRef(jlevname);
    	if (jstr) env->DeleteLocalRef(jstr);
    } else {
    	linphone_android_log_handler(prio, str);
    }
    bctbx_free(str);
144 145
}

146 147 148
int dumbMethodForAllowingUsageOfCpuFeaturesFromStaticLibMediastream() {
	return (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM && (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0);
}
149 150 151 152

int dumbMethodForAllowingUsageOfMsAudioDiffFromStaticLibMediastream() {
	return ms_audio_diff(NULL, NULL, NULL, 0, NULL, NULL);
}
153 154 155 156

extern "C" void setAndroidLogHandler() {
	linphone_core_enable_logs_with_cb(linphone_android_ortp_log_handler);
}
157 158 159 160 161 162

extern "C" void setMediastreamerAndroidContext(JNIEnv *env, void *context) {
	jclass ms_class = env->FindClass("org/linphone/mediastream/MediastreamerAndroidContext");
	jmethodID set_context = env->GetStaticMethodID(ms_class, "setContext", "(Ljava/lang/Object;)V");
	env->CallStaticVoidMethod(ms_class, set_context, (jobject)context);
}
163
#endif /* __ANDROID__ */
164

165

166
extern "C" jint   JNI_OnLoad(JavaVM *ajvm, void *reserved)
167
{
168
#ifdef __ANDROID__
Simon Morlat's avatar
Simon Morlat committed
169
	ms_set_jvm(ajvm);
170
#endif /* __ANDROID__ */
171 172 173 174
	jvm=ajvm;
	return JNI_VERSION_1_2;
}

175 176 177 178 179 180 181 182
static const char* GetStringUTFChars(JNIEnv* env, jstring string) {
	const char *cstring = string ? env->GetStringUTFChars(string, NULL) : NULL;
	return cstring;
}

static void ReleaseStringUTFChars(JNIEnv* env, jstring string, const char *cstring) {
	if (string) env->ReleaseStringUTFChars(string, cstring);
}
183

jehan's avatar
jehan committed
184
//LinphoneFactory
jehan's avatar
jehan committed
185
extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setDebugMode(JNIEnv*  env
jehan's avatar
jehan committed
186
		,jobject  thiz
187 188
		,jboolean isDebug
		,jstring  jdebugTag) {
jehan's avatar
jehan committed
189
	if (isDebug) {
190
		LogDomain = GetStringUTFChars(env, jdebugTag);
191
		linphone_core_enable_logs_with_cb(linphone_android_ortp_log_handler);
jehan's avatar
jehan committed
192 193 194 195
	} else {
		linphone_core_disable_logs();
	}
}
196

197 198 199 200 201 202 203 204
extern "C" jobject Java_org_linphone_core_LinphoneCoreFactoryImpl__1createTunnelConfig(JNIEnv*  env, jobject  thiz){
	jobject jobj;
	LinphoneTunnelConfig *cfg =  linphone_tunnel_config_new();
	jobj = getTunnelConfig(env, cfg); //this will take a ref.
	linphone_tunnel_config_unref(cfg);
	return jobj;
}

205 206 207 208 209 210 211 212 213 214
extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_enableLogCollection(JNIEnv* env
		,jobject  thiz
		,jboolean enable) {
	linphone_core_enable_log_collection(enable ? LinphoneLogCollectionEnabledWithoutPreviousLogHandler : LinphoneLogCollectionDisabled);
}

extern "C" void Java_org_linphone_core_LinphoneCoreFactoryImpl_setLogCollectionPath(JNIEnv* env
		,jobject  thiz
		,jstring jpath) {

215
	const char* path = GetStringUTFChars(env, jpath);
216
	linphone_core_set_log_collection_path(path);
217
	ReleaseStringUTFChars(env, jpath, path);
218
}
Erwan Croze's avatar
Erwan Croze committed
219

220 221 222 223
extern "C" jlong Java_org_linphone_core_LinphoneCoreFactoryImpl_createErrorInfoNative(JNIEnv*  env, jobject  thiz){
	return (jlong)linphone_factory_create_error_info(linphone_factory_get());
}

Erwan Croze's avatar
Erwan Croze committed
224 225 226 227
extern "C" jobjectArray Java_org_linphone_core_LinphoneCoreFactoryImpl_getAllDialPlanNative(JNIEnv *env, jobject thiz) {
	jclass addr_class = env->FindClass("org/linphone/core/DialPlanImpl");
	jmethodID addr_constructor = env->GetMethodID(addr_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)V");
	jobjectArray jaddr_array;
228 229 230
	size_t i;
	const bctbx_list_t *countries = linphone_dial_plan_get_all_list();
	size_t size = bctbx_list_size(countries);
Erwan Croze's avatar
Erwan Croze committed
231

232
	jaddr_array = env->NewObjectArray((int)size, addr_class, NULL);
Erwan Croze's avatar
Erwan Croze committed
233

234 235
	for (i = 0; i < size; i++) {
		LinphoneDialPlan* dp = (LinphoneDialPlan*)countries->data;
Erwan Croze's avatar
Erwan Croze committed
236

237 238 239 240 241
		jstring jcountry = env->NewStringUTF(linphone_dial_plan_get_country(dp));
		jstring jiso = env->NewStringUTF(linphone_dial_plan_get_iso_country_code(dp));
		jstring jccc = env->NewStringUTF(linphone_dial_plan_get_country_calling_code(dp));
		jint jnnl = (jint)linphone_dial_plan_get_national_number_length(dp);
		jstring jicp = env->NewStringUTF(linphone_dial_plan_get_international_call_prefix(dp));
Erwan Croze's avatar
Erwan Croze committed
242 243 244

		jobject jaddr = env->NewObject(addr_class, addr_constructor, jcountry, jiso, jccc, jnnl, jicp);

245
		env->SetObjectArrayElement(jaddr_array, (int)i, jaddr);
Erwan Croze's avatar
Erwan Croze committed
246 247 248 249 250

		env->DeleteLocalRef(jcountry);
		env->DeleteLocalRef(jiso);
		env->DeleteLocalRef(jccc);
		env->DeleteLocalRef(jicp);
251
		countries = countries->next;
Erwan Croze's avatar
Erwan Croze committed
252 253 254 255
	}
	return jaddr_array;
}

256 257
// LinphoneCore

Sylvain Berfini's avatar
Sylvain Berfini committed
258
class LinphoneJavaBindings {
259
public:
Sylvain Berfini's avatar
Sylvain Berfini committed
260 261
	LinphoneJavaBindings(JNIEnv *env) {
		listenerClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCoreListener"));
262

263 264
		authMethodClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$AuthMethod"));
		authMethodFromIntId = env->GetStaticMethodID(authMethodClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$AuthMethod;");
265

266
		/*void generalState(LinphoneCore lc,int state); */
267 268
		globalStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$GlobalState"));
		globalStateFromIntId = env->GetStaticMethodID(globalStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$GlobalState;");
269
		globalStateId = env->GetMethodID(listenerClass,"globalState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$GlobalState;Ljava/lang/String;)V");
270

271 272 273
		/*registrationState(LinphoneCore lc, LinphoneProxyConfig cfg, LinphoneCore.RegistrationState cstate, String smessage);*/
		registrationStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RegistrationState"));
		registrationStateFromIntId = env->GetStaticMethodID(registrationStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RegistrationState;");
274
		registrationStateId = env->GetMethodID(listenerClass,"registrationState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneProxyConfig;Lorg/linphone/core/LinphoneCore$RegistrationState;Ljava/lang/String;)V");
275

276 277 278
		/*callState(LinphoneCore lc, LinphoneCall call, LinphoneCall.State cstate,String message);*/
		callStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCall$State"));
		callStateFromIntId = env->GetStaticMethodID(callStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCall$State;");
279
		callStateId = env->GetMethodID(listenerClass,"callState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;Ljava/lang/String;)V");
280

281
		transferStateId = env->GetMethodID(listenerClass,"transferState","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCall$State;)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
282

283 284 285
		/*callStatsUpdated(LinphoneCore lc, LinphoneCall call, LinphoneCallStats stats);*/
		callStatsUpdatedId = env->GetMethodID(listenerClass, "callStatsUpdated", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneCallStats;)V");

286
		/*callEncryption(LinphoneCore lc, LinphoneCall call, boolean encrypted,String auth_token);*/
287
		callEncryptionChangedId = env->GetMethodID(listenerClass,"callEncryptionChanged","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;ZLjava/lang/String;)V");
288

jehan's avatar
jehan committed
289 290 291
		/*void ecCalibrationStatus(LinphoneCore.EcCalibratorStatus status, int delay_ms, Object data);*/
		ecCalibratorStatusClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$EcCalibratorStatus"));
		ecCalibratorStatusFromIntId = env->GetStaticMethodID(ecCalibratorStatusClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;");
292
		ecCalibrationStatusId = env->GetMethodID(listenerClass,"ecCalibrationStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$EcCalibratorStatus;ILjava/lang/Object;)V");
293

294
		/*void newSubscriptionRequest(LinphoneCore lc, LinphoneFriend lf, String url)*/
295
		newSubscriptionRequestId = env->GetMethodID(listenerClass,"newSubscriptionRequest","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;Ljava/lang/String;)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
296

297 298
		authInfoRequestedId = env->GetMethodID(listenerClass,"authInfoRequested","(Lorg/linphone/core/LinphoneCore;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
		authenticationRequestedId = env->GetMethodID(listenerClass,"authenticationRequested","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneAuthInfo;Lorg/linphone/core/LinphoneCore$AuthMethod;)V");
299 300

		/*void notifyPresenceReceived(LinphoneCore lc, LinphoneFriend lf);*/
301
		notifyPresenceReceivedId = env->GetMethodID(listenerClass,"notifyPresenceReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriend;)V");
302

Simon Morlat's avatar
Simon Morlat committed
303
		messageReceivedId = env->GetMethodID(listenerClass,"messageReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");
304

305 306
		messageReceivedUnableToDecryptedId = env->GetMethodID(listenerClass,"messageReceivedUnableToDecrypted","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;Lorg/linphone/core/LinphoneChatMessage;)V");

307
		isComposingReceivedId = env->GetMethodID(listenerClass,"isComposingReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatRoom;)V");
308

Sylvain Berfini's avatar
Sylvain Berfini committed
309
		dtmfReceivedId = env->GetMethodID(listenerClass,"dtmfReceived","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;I)V");
310

311
		infoReceivedId = env->GetMethodID(listenerClass,"infoReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCall;Lorg/linphone/core/LinphoneInfoMessage;)V");
312

313 314
		subscriptionStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionState"));
		subscriptionStateFromIntId = env->GetStaticMethodID(subscriptionStateClass,"fromInt","(I)Lorg/linphone/core/SubscriptionState;");
315
		subscriptionStateId = env->GetMethodID(listenerClass,"subscriptionStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/SubscriptionState;)V");
316

317 318
		publishStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/PublishState"));
		publishStateFromIntId = env->GetStaticMethodID(publishStateClass,"fromInt","(I)Lorg/linphone/core/PublishState;");
319
		publishStateId = env->GetMethodID(listenerClass,"publishStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Lorg/linphone/core/PublishState;)V");
320

321
		notifyRecvId = env->GetMethodID(listenerClass,"notifyReceived", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneEvent;Ljava/lang/String;Lorg/linphone/core/LinphoneContent;)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
322

323 324
		networkReachableId = env->GetMethodID(listenerClass,"networkReachableChanged", "(Lorg/linphone/core/LinphoneCore;Z)V");

325 326
		configuringStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$RemoteProvisioningState"));
		configuringStateFromIntId = env->GetStaticMethodID(configuringStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;");
327
		configuringStateId = env->GetMethodID(listenerClass,"configuringStatus","(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$RemoteProvisioningState;Ljava/lang/String;)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
328

329
		fileTransferProgressIndicationId = env->GetMethodID(listenerClass, "fileTransferProgressIndication", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;I)V");
330

331 332 333
		fileTransferSendId = env->GetMethodID(listenerClass, "fileTransferSend", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;Ljava/nio/ByteBuffer;I)I");

		fileTransferRecvId = env->GetMethodID(listenerClass, "fileTransferRecv", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneChatMessage;Lorg/linphone/core/LinphoneContent;[BI)V");
334

335 336
		logCollectionUploadStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCore$LogCollectionUploadState"));
		logCollectionUploadStateFromIntId = env->GetStaticMethodID(logCollectionUploadStateClass, "fromInt", "(I)Lorg/linphone/core/LinphoneCore$LogCollectionUploadState;");
Sylvain Berfini's avatar
Sylvain Berfini committed
337
		logCollectionUploadProgressId = env->GetMethodID(listenerClass, "uploadProgressIndication", "(Lorg/linphone/core/LinphoneCore;II)V");
338
		logCollectionUploadStateId = env->GetMethodID(listenerClass, "uploadStateChanged", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneCore$LogCollectionUploadState;Ljava/lang/String;)V");
339 340 341

		chatMessageStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessage$State"));
		chatMessageStateFromIntId = env->GetStaticMethodID(chatMessageStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneChatMessage$State;");
342

343 344
		authInfoClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAuthInfoImpl"));
		authInfoCtrId = env->GetMethodID(authInfoClass,"<init>", "(J)V");
345

346
		proxyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneProxyConfigImpl"));
347
		proxyCtrId = env->GetMethodID(proxyClass,"<init>", "(Lorg/linphone/core/LinphoneCoreImpl;J)V");
348 349 350

		callClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallImpl"));
		callCtrId = env->GetMethodID(callClass,"<init>", "(J)V");
351

352
		chatMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatMessageImpl"));
353 354 355
		if (chatMessageClass) {
			chatMessageCtrId = env->GetMethodID(chatMessageClass,"<init>", "(J)V");
		}
356

357 358 359
		chatRoomClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneChatRoomImpl"));
		chatRoomCtrId = env->GetMethodID(chatRoomClass,"<init>", "(J)V");

360
		friendClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneFriendImpl"));
361
		friendCtrId = env->GetMethodID(friendClass,"<init>", "(J)V");
362

363
		friendListClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneFriendListImpl"));
364 365 366
		friendListCtrId = env->GetMethodID(friendListClass,"<init>", "(J)V");
		friendListCreatedId = env->GetMethodID(listenerClass, "friendListCreated", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriendList;)V");
		friendListRemovedId = env->GetMethodID(listenerClass, "friendListRemoved", "(Lorg/linphone/core/LinphoneCore;Lorg/linphone/core/LinphoneFriendList;)V");
367 368
		friendListSyncStateClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneFriendList$State"));
		friendListSyncStateFromIntId = env->GetStaticMethodID(friendListSyncStateClass,"fromInt","(I)Lorg/linphone/core/LinphoneFriendList$State;");
369

370 371 372
		natPolicyClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneNatPolicyImpl"));
		natPolicyCtrId = env->GetMethodID(natPolicyClass, "<init>", "(J)V");

jehan's avatar
jehan committed
373
		addressClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAddressImpl"));
374
		addressCtrId = env->GetMethodID(addressClass,"<init>", "(J)V");
jehan's avatar
jehan committed
375

376
		callStatsClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneCallStatsImpl"));
377
		callStatsId = env->GetMethodID(callStatsClass, "<init>", "(J)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
378

379 380
		infoMessageClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneInfoMessageImpl"));
		infoMessageCtor = env->GetMethodID(infoMessageClass,"<init>", "(J)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
381

382 383
		linphoneEventClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneEventImpl"));
		linphoneEventCtrId = env->GetMethodID(linphoneEventClass,"<init>", "(J)V");
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
384

385 386
		subscriptionDirClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/SubscriptionDir"));
		subscriptionDirFromIntId = env->GetStaticMethodID(subscriptionDirClass,"fromInt","(I)Lorg/linphone/core/SubscriptionDir;");
387

388 389
		msFactoryClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/mediastream/Factory"));
		msFactoryCtrId = env->GetMethodID(msFactoryClass,"<init>", "(J)V");
390

391 392
		accountCreatorClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreatorImpl"));
		accountCreatorCtrId = env->GetMethodID(accountCreatorClass, "<init>", "(J)V");
Erwan Croze's avatar
Erwan Croze committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406
		accountCreatorRequestStatusClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$RequestStatus"));
		accountCreatorRequestStatusFromIntId = env->GetStaticMethodID(accountCreatorRequestStatusClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$RequestStatus;");
		accountCreatorUsernameCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$UsernameCheck"));
		accountCreatorUsernameCheckFromIntId = env->GetStaticMethodID(accountCreatorUsernameCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$UsernameCheck;");
		accountCreatorPasswordCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$PasswordCheck"));
		accountCreatorPasswordCheckFromIntId = env->GetStaticMethodID(accountCreatorPasswordCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$PasswordCheck;");
		accountCreatorEmailCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$EmailCheck"));
		accountCreatorEmailCheckFromIntId = env->GetStaticMethodID(accountCreatorEmailCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$EmailCheck;");
		accountCreatorLanguageCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$LanguageCheck"));
		accountCreatorLanguageCheckFromIntId = env->GetStaticMethodID(accountCreatorLanguageCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$LanguageCheck;");
		accountCreatorCodeActivationCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$ActivationCodeCheck"));
		accountCreatorCodeActivationCheckFromIntId = env->GetStaticMethodID(accountCreatorCodeActivationCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$ActivationCodeCheck;");
		accountCreatorPhoneNumberCheckClass = (jclass)env->NewGlobalRef(env->FindClass("org/linphone/core/LinphoneAccountCreator$PhoneNumberCheck"));
		accountCreatorPhoneNumberCheckFromIntId = env->GetStaticMethodID(accountCreatorPhoneNumberCheckClass,"fromInt","(I)Lorg/linphone/core/LinphoneAccountCreator$PhoneNumberCheck;");
407
	}
408

Sylvain Berfini's avatar
Sylvain Berfini committed
409 410 411
	void setCore(jobject c) {
		core = c;
	}
412

Sylvain Berfini's avatar
Sylvain Berfini committed
413 414 415
	jobject getCore() {
		return core;
	}
416

Sylvain Berfini's avatar
Sylvain Berfini committed
417
	~LinphoneJavaBindings() {
418 419
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
420
		env->DeleteGlobalRef(listenerClass);
421
		env->DeleteGlobalRef(authMethodClass);
422
		env->DeleteGlobalRef(globalStateClass);
423
		env->DeleteGlobalRef(configuringStateClass);
424 425
		env->DeleteGlobalRef(registrationStateClass);
		env->DeleteGlobalRef(callStateClass);
426
		env->DeleteGlobalRef(chatMessageStateClass);
427
		env->DeleteGlobalRef(authInfoClass);
428 429
		env->DeleteGlobalRef(proxyClass);
		env->DeleteGlobalRef(callClass);
430
		env->DeleteGlobalRef(chatMessageClass);
431 432
		env->DeleteGlobalRef(chatRoomClass);
		env->DeleteGlobalRef(friendClass);
433
		env->DeleteGlobalRef(friendListClass);
434
		env->DeleteGlobalRef(friendListSyncStateClass);
435
		env->DeleteGlobalRef(natPolicyClass);
436
		env->DeleteGlobalRef(infoMessageClass);
437 438 439
		env->DeleteGlobalRef(linphoneEventClass);
		env->DeleteGlobalRef(subscriptionStateClass);
		env->DeleteGlobalRef(subscriptionDirClass);
440
		env->DeleteGlobalRef(logCollectionUploadStateClass);
441
		env->DeleteGlobalRef(msFactoryClass);
442
		env->DeleteGlobalRef(accountCreatorClass);
Erwan Croze's avatar
Erwan Croze committed
443 444 445 446 447 448 449
		env->DeleteGlobalRef(accountCreatorRequestStatusClass);
		env->DeleteGlobalRef(accountCreatorUsernameCheckClass);
		env->DeleteGlobalRef(accountCreatorPasswordCheckClass);
		env->DeleteGlobalRef(accountCreatorEmailCheckClass);
		env->DeleteGlobalRef(accountCreatorLanguageCheckClass);
		env->DeleteGlobalRef(accountCreatorCodeActivationCheckClass);
		env->DeleteGlobalRef(accountCreatorPhoneNumberCheckClass);
450
	}
451

452 453
	jobject core;

454
	jclass listenerClass;
455 456
	jmethodID newSubscriptionRequestId;
	jmethodID notifyPresenceReceivedId;
457
	jmethodID messageReceivedId;
458
	jmethodID messageReceivedUnableToDecryptedId;
459
	jmethodID isComposingReceivedId;
Sylvain Berfini's avatar
Sylvain Berfini committed
460
	jmethodID dtmfReceivedId;
461
	jmethodID callStatsUpdatedId;
462
	jmethodID transferStateId;
463
	jmethodID infoReceivedId;
464
	jmethodID subscriptionStateId;
465
	jmethodID authInfoRequestedId;
466
	jmethodID authenticationRequestedId;
467
	jmethodID publishStateId;
468
	jmethodID notifyRecvId;
469
	jmethodID networkReachableId;
470

471 472
	jclass authMethodClass;
	jmethodID authMethodFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
473

474 475 476
	jclass configuringStateClass;
	jmethodID configuringStateId;
	jmethodID configuringStateFromIntId;
477 478 479 480 481 482 483 484 485 486 487 488 489

	jclass globalStateClass;
	jmethodID globalStateId;
	jmethodID globalStateFromIntId;

	jclass registrationStateClass;
	jmethodID registrationStateId;
	jmethodID registrationStateFromIntId;

	jclass callStateClass;
	jmethodID callStateId;
	jmethodID callStateFromIntId;

490 491 492
	jclass callStatsClass;
	jmethodID callStatsId;

493 494 495
	jclass chatMessageStateClass;
	jmethodID chatMessageStateFromIntId;

496 497
	jmethodID callEncryptionChangedId;

jehan's avatar
jehan committed
498 499 500
	jclass ecCalibratorStatusClass;
	jmethodID ecCalibrationStatusId;
	jmethodID ecCalibratorStatusFromIntId;
501

502 503
	jclass authInfoClass;
	jmethodID authInfoCtrId;
jehan's avatar
jehan committed
504

505 506 507 508 509 510
	jclass proxyClass;
	jmethodID proxyCtrId;

	jclass callClass;
	jmethodID callCtrId;

511 512 513
	jclass chatMessageClass;
	jmethodID chatMessageCtrId;

514 515 516 517 518 519
	jclass chatRoomClass;
	jmethodID chatRoomCtrId;

	jclass friendClass;
	jmethodID friendCtrId;

520 521 522 523
	jclass friendListClass;
	jmethodID friendListCtrId;
	jmethodID friendListCreatedId;
	jmethodID friendListRemovedId;
524 525
	jclass friendListSyncStateClass;
	jmethodID friendListSyncStateFromIntId;
526

527 528 529
	jclass natPolicyClass;
	jmethodID natPolicyCtrId;

jehan's avatar
jehan committed
530 531
	jclass addressClass;
	jmethodID addressCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
532

533 534
	jclass infoMessageClass;
	jmethodID infoMessageCtor;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
535

536 537
	jclass linphoneEventClass;
	jmethodID linphoneEventCtrId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
538

539 540
	jclass subscriptionStateClass;
	jmethodID subscriptionStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
541

542 543
	jclass publishStateClass;
	jmethodID publishStateFromIntId;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
544

545 546
	jclass subscriptionDirClass;
	jmethodID subscriptionDirFromIntId;
jehan's avatar
jehan committed
547

548
	jmethodID fileTransferProgressIndicationId;
549 550
	jmethodID fileTransferSendId;
	jmethodID fileTransferRecvId;
551

552 553 554 555
	jclass logCollectionUploadStateClass;
	jmethodID logCollectionUploadStateId;
	jmethodID logCollectionUploadStateFromIntId;
	jmethodID logCollectionUploadProgressId;
556

557 558
	jclass msFactoryClass;
	jmethodID msFactoryCtrId;
559

560 561
	jclass accountCreatorClass;
	jmethodID accountCreatorCtrId;
Erwan Croze's avatar
Erwan Croze committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575
	jclass accountCreatorRequestStatusClass;
	jmethodID accountCreatorRequestStatusFromIntId;
	jclass accountCreatorUsernameCheckClass;
	jmethodID accountCreatorUsernameCheckFromIntId;
	jclass accountCreatorPasswordCheckClass;
	jmethodID accountCreatorPasswordCheckFromIntId;
	jclass accountCreatorEmailCheckClass;
	jmethodID accountCreatorEmailCheckFromIntId;
	jclass accountCreatorLanguageCheckClass;
	jmethodID accountCreatorLanguageCheckFromIntId;
	jclass accountCreatorCodeActivationCheckClass;
	jmethodID accountCreatorCodeActivationCheckFromIntId;
	jclass accountCreatorPhoneNumberCheckClass;
	jmethodID accountCreatorPhoneNumberCheckFromIntId;
Sylvain Berfini's avatar
Sylvain Berfini committed
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
};

/*
 * returns the java LinphoneProxyConfig associated with a C LinphoneProxyConfig.
**/
jobject getProxy(JNIEnv *env, LinphoneProxyConfig *proxy, jobject core){
	jobject jobj=0;

	if (proxy!=NULL){
		LinphoneCore *lc = linphone_proxy_config_get_core(proxy);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up=linphone_proxy_config_get_user_data(proxy);

		if (up==NULL){
			jobj=env->NewObject(ljb->proxyClass, ljb->proxyCtrId, core, (jlong)proxy);
			linphone_proxy_config_set_user_data(proxy,(void*)env->NewWeakGlobalRef(jobj));
			linphone_proxy_config_ref(proxy);
		}else{
595
 			//promote the weak ref to local ref
Sylvain Berfini's avatar
Sylvain Berfini committed
596 597 598 599 600 601 602 603 604 605 606
			jobj=env->NewLocalRef((jobject)up);
			if (jobj == NULL){
				//the weak ref was dead
				jobj=env->NewObject(ljb->proxyClass, ljb->proxyCtrId, core, (jlong)proxy);
				linphone_proxy_config_set_user_data(proxy,(void*)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

607 608 609 610 611 612 613 614 615 616
jobject getAuthInfo(JNIEnv *env, LinphoneCore *lc, LinphoneAuthInfo *authInfo, jobject core){
	jobject jobj = 0;

	if (authInfo && lc) {
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
		jobj = env->NewObject(ljb->authInfoClass, ljb->authInfoCtrId, core, (jlong)authInfo);
	}
	return jobj;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
617 618 619 620 621 622 623
jobject getCall(JNIEnv *env, LinphoneCall *call){
	jobject jobj=0;

	if (call!=NULL){
		LinphoneCore *lc = linphone_call_get_core(call);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

624
		void *up=linphone_call_get_user_data(call);
Sylvain Berfini's avatar
Sylvain Berfini committed
625 626 627 628

		if (up==NULL){
			jobj=env->NewObject(ljb->callClass, ljb->callCtrId, (jlong)call);
			jobj=env->NewGlobalRef(jobj);
629
			linphone_call_set_user_data(call,(void*)jobj);
Sylvain Berfini's avatar
Sylvain Berfini committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
			linphone_call_ref(call);
		}else{
			jobj=(jobject)up;
		}
	}
	return jobj;
}

jobject getChatMessage(JNIEnv *env, LinphoneChatMessage *msg){
	jobject jobj = 0;

	if (msg != NULL){
		LinphoneChatRoom *room = linphone_chat_message_get_chat_room(msg);
		LinphoneCore *lc = linphone_chat_room_get_core(room);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up = linphone_chat_message_get_user_data(msg);

		if (up == NULL) {
649 650 651 652
			jobj = env->NewObject(ljb->chatMessageClass, ljb->chatMessageCtrId, (jlong)msg);
			void *userdata = (void*)env->NewWeakGlobalRef(jobj);
			linphone_chat_message_set_user_data(msg, userdata);
			linphone_chat_message_ref(msg);
Sylvain Berfini's avatar
Sylvain Berfini committed
653
		} else {
654 655 656 657 658 659
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
				// takes implicit local ref
				jobj = env->NewObject(ljb->chatMessageClass, ljb->chatMessageCtrId, (jlong)msg);
				linphone_chat_message_set_user_data(msg, (void*)env->NewWeakGlobalRef(jobj));
			}
Sylvain Berfini's avatar
Sylvain Berfini committed
660 661 662 663 664
		}
	}
	return jobj;
}

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
jobject getChatRoom(JNIEnv *env, LinphoneChatRoom *room) {
	jobject jobj = 0;

	if (room != NULL){
		LinphoneCore *lc = linphone_chat_room_get_core(room);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up = linphone_chat_room_get_user_data(room);
		if (up == NULL) {
			// take implicit local ref
			jobj = env->NewObject(ljb->chatRoomClass, ljb->chatRoomCtrId, (jlong)room);
			linphone_chat_room_set_user_data(room, (void*)env->NewWeakGlobalRef(jobj));
			linphone_chat_room_ref(room);
		} else {
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
				// takes implicit local ref
682
				jobj = env->NewObject(ljb->chatRoomClass, ljb->chatRoomCtrId, (jlong)room);
683 684 685 686 687 688 689
				linphone_chat_room_set_user_data(room, (void*)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

690
jobject getFriend(JNIEnv *env, LinphoneFriend *lfriend){
Sylvain Berfini's avatar
Sylvain Berfini committed
691 692
	jobject jobj=0;

693

Sylvain Berfini's avatar
Sylvain Berfini committed
694 695 696 697 698 699 700
	if (lfriend != NULL){
		LinphoneCore *lc = linphone_friend_get_core(lfriend);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up=linphone_friend_get_user_data(lfriend);

		if (up == NULL){
701 702

			// take implicit local ref
Sylvain Berfini's avatar
Sylvain Berfini committed
703 704 705 706 707 708 709
			jobj=env->NewObject(ljb->friendClass, ljb->friendCtrId, (jlong)lfriend);
			linphone_friend_set_user_data(lfriend,(void*)env->NewWeakGlobalRef(jobj));
			linphone_friend_ref(lfriend);
		}else{

			jobj=env->NewLocalRef((jobject)up);
			if (jobj == NULL){
710
				// takes implicit local ref
Sylvain Berfini's avatar
Sylvain Berfini committed
711 712 713 714 715 716 717 718
				jobj=env->NewObject(ljb->friendClass, ljb->friendCtrId, (jlong)lfriend);
				linphone_friend_set_user_data(lfriend,(void*)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
jobject getFriendList(JNIEnv *env, LinphoneFriendList *lfriendList){
	jobject jobj=0;

	if (lfriendList != NULL){
		LinphoneCore *lc = linphone_friend_list_get_core(lfriendList);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up=linphone_friend_list_get_user_data(lfriendList);

		if (up == NULL){
			jobj=env->NewObject(ljb->friendListClass, ljb->friendListCtrId, (jlong)lfriendList);
			linphone_friend_list_set_user_data(lfriendList,(void*)env->NewWeakGlobalRef(jobj));
			linphone_friend_list_ref(lfriendList);
		}else{

			jobj=env->NewLocalRef((jobject)up);
			if (jobj == NULL){
				jobj=env->NewObject(ljb->friendListClass, ljb->friendListCtrId, (jlong)lfriendList);
				linphone_friend_list_set_user_data(lfriendList,(void*)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
jobject getNatPolicy(JNIEnv *env, LinphoneNatPolicy *lNatPolicy) {
	jobject jobj = 0;

	if (lNatPolicy != NULL) {
		LinphoneCore *lc = lNatPolicy->lc;
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up = linphone_nat_policy_get_user_data(lNatPolicy);
		if (up == NULL) {
			jobj = env->NewObject(ljb->natPolicyClass, ljb->natPolicyCtrId, (jlong)lNatPolicy);
			linphone_nat_policy_set_user_data(lNatPolicy, (void *)env->NewWeakGlobalRef(jobj));
			linphone_nat_policy_ref(lNatPolicy);
		} else {
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
				jobj = env->NewObject(ljb->natPolicyClass, ljb->natPolicyCtrId, (jlong)lNatPolicy);
				linphone_nat_policy_set_user_data(lNatPolicy, (void *)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780
jobject getEvent(JNIEnv *env, LinphoneEvent *lev){
	if (lev==NULL) return NULL;
	jobject jev=(jobject)linphone_event_get_user_data(lev);
	if (jev==NULL){
		LinphoneCore *lc = linphone_event_get_core(lev);
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		jev=env->NewObject(ljb->linphoneEventClass, ljb->linphoneEventCtrId, (jlong)linphone_event_ref(lev));
		jev=env->NewGlobalRef(jev);
		linphone_event_set_user_data(lev,jev);
	}
	return jev;
}

781 782 783 784 785 786
jobject getXmlRpcRequest(JNIEnv *env, LinphoneXmlRpcRequest *lrequest) {
	jobject jobj = 0;

	if (lrequest != NULL) {
		jclass xmlRpcSessionClass = (jclass)env->FindClass("org/linphone/core/LinphoneXmlRpcRequestImpl");
		jmethodID xmlRpcSessionCtrId = env->GetMethodID(xmlRpcSessionClass, "<init>", "(J)V");
787

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
		void *up = linphone_xml_rpc_request_get_user_data(lrequest);
		if (up == NULL) {
			jobj = env->NewObject(xmlRpcSessionClass, xmlRpcSessionCtrId, (jlong)lrequest);
			linphone_xml_rpc_request_set_user_data(lrequest, (void *)env->NewWeakGlobalRef(jobj));
			linphone_xml_rpc_request_ref(lrequest);
		} else {
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
				jobj = env->NewObject(xmlRpcSessionClass, xmlRpcSessionCtrId, (jlong)lrequest);
				linphone_xml_rpc_request_set_user_data(lrequest, (void *)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

804 805 806 807 808 809 810 811 812
jobject getAccountCreator(JNIEnv *env, LinphoneAccountCreator *account_creator) {
	jobject jobj = 0;

	if (account_creator != NULL) {
		LinphoneCore *lc = account_creator->core;
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);

		void *up = linphone_account_creator_get_user_data(account_creator);
		if (up == NULL) {
813
			jobj = env->NewObject(ljb->accountCreatorClass, ljb->accountCreatorCtrId, (jlong)account_creator);
814 815 816 817 818
			linphone_account_creator_set_user_data(account_creator, (void *)env->NewWeakGlobalRef(jobj));
			linphone_account_creator_ref(account_creator);
		} else {
			jobj = env->NewLocalRef((jobject)up);
			if (jobj == NULL) {
819
				jobj = env->NewObject(ljb->accountCreatorClass, ljb->accountCreatorCtrId, (jlong)account_creator);
820 821 822 823 824 825 826
				linphone_account_creator_set_user_data(account_creator, (void *)env->NewWeakGlobalRef(jobj));
			}
		}
	}
	return jobj;
}

Sylvain Berfini's avatar
Sylvain Berfini committed
827 828 829 830 831
class LinphoneCoreData {
public:
	LinphoneCoreData(JNIEnv *env, jobject lc, LinphoneCoreVTable *vTable, jobject alistener, LinphoneJavaBindings *ljb) {
		core = env->NewGlobalRef(lc);
		listener = env->NewGlobalRef(alistener);
832

Sylvain Berfini's avatar
Sylvain Berfini committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
		memset(vTable, 0, sizeof(LinphoneCoreVTable));

		if (ljb->globalStateId) {
			vTable->global_state_changed = globalStateChange;
		}

		if (ljb->registrationStateId) {
			vTable->registration_state_changed = registrationStateChange;
		}

		if (ljb->callStateId) {
			vTable->call_state_changed = callStateChange;
		}

		if (ljb->transferStateId) {
			vTable->transfer_state_changed = transferStateChanged;
		}

		if (ljb->callStatsUpdatedId) {
			vTable->call_stats_updated = callStatsUpdated;
		}

		if (ljb->callEncryptionChangedId) {
			vTable->call_encryption_changed = callEncryptionChange;
		}

		if (ljb->newSubscriptionRequestId) {
			vTable->new_subscription_requested = new_subscription_requested;
		}

		if (ljb->authInfoRequestedId) {
			vTable->auth_info_requested = authInfoRequested;
		}
866

867 868 869
		if (ljb->authenticationRequestedId) {
			vTable->authentication_requested = authenticationRequested;
		}
Sylvain Berfini's avatar
Sylvain Berfini committed
870 871 872 873 874 875 876 877 878

		if (ljb->notifyPresenceReceivedId) {
			vTable->notify_presence_received = notify_presence_received;
		}

		if (ljb->messageReceivedId) {
			vTable->message_received = message_received;
		}

879 880 881 882
		if (ljb->messageReceivedUnableToDecryptedId) {
		    vTable->message_received_unable_decrypt = message_received_unable_decrypt;
		}

Sylvain Berfini's avatar
Sylvain Berfini committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
		if (ljb->isComposingReceivedId) {
			vTable->is_composing_received = is_composing_received;
		}

		if (ljb->dtmfReceivedId) {
			vTable->dtmf_received = dtmf_received;
		}

		if (ljb->infoReceivedId) {
			vTable->info_received = infoReceived;
		}

		if (ljb->subscriptionStateId) {
			vTable->subscription_state_changed = subscriptionStateChanged;
		}

		if (ljb->publishStateId) {
			vTable->publish_state_changed = publishStateChanged;
		}

		if (ljb->notifyRecvId) {
			vTable->notify_received = notifyReceived;
		}

907 908 909 910
		if (ljb->networkReachableId) {
			vTable->network_reachable = networkReachableCb;
		}

Sylvain Berfini's avatar
Sylvain Berfini committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
		if (ljb->configuringStateId) {
			vTable->configuring_status = configuringStatus;
		}

		if (ljb->fileTransferProgressIndicationId) {
			vTable->file_transfer_progress_indication = fileTransferProgressIndication;
		}

		if (ljb->fileTransferSendId) {
			vTable->file_transfer_send = fileTransferSend;
		}

		if (ljb->fileTransferRecvId) {
			vTable->file_transfer_recv = fileTransferRecv;
		}

		if (ljb->logCollectionUploadProgressId) {
			vTable->log_collection_upload_progress_indication = logCollectionUploadProgressIndication;
		}
		if (ljb->logCollectionUploadStateId) {
			vTable->log_collection_upload_state_changed = logCollectionUploadStateChange;
		}
933

934 935 936 937 938 939
		if (ljb->friendListCreatedId) {
			vTable->friend_list_created = friendListCreated;
		}
		if (ljb->friendListRemovedId) {
			vTable->friend_list_removed = friendListRemoved;
		}
Sylvain Berfini's avatar
Sylvain Berfini committed
940
	}
941

Sylvain Berfini's avatar
Sylvain Berfini committed
942 943 944 945 946 947
	~LinphoneCoreData() {
		JNIEnv *env = 0;
		jvm->AttachCurrentThread(&env,NULL);
		env->DeleteGlobalRef(core);
		env->DeleteGlobalRef(listener);
	}
948

Sylvain Berfini's avatar
Sylvain Berfini committed
949 950
	jobject core;
	jobject listener;
951

952
	LinphoneCoreVTable vTable;
953

954
	static void authInfoRequested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
955 956 957 958 959 960
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}
961

Sylvain Berfini's avatar
Sylvain Berfini committed
962
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
963 964
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
965 966 967
		jstring r = realm ? env->NewStringUTF(realm) : NULL;
		jstring u = username ? env->NewStringUTF(username) : NULL;
		jstring d = domain ? env->NewStringUTF(domain) : NULL;
968
		env->CallVoidMethod(lcData->listener,
Sylvain Berfini's avatar
Sylvain Berfini committed
969
							ljb->authInfoRequestedId,
970
							lcData->core,
971 972
							r,
							u,
973
							d
974
   						);
975
		handle_possible_java_exception(env, lcData->listener);
976 977 978 979 980 981 982 983 984
		if (r) {
			env->DeleteLocalRef(r);
		}
		if (u) {
			env->DeleteLocalRef(u);
		}
		if (d) {
			env->DeleteLocalRef(d);
		}
985
	}
986 987 988 989 990 991 992
	static void authenticationRequested(LinphoneCore *lc, LinphoneAuthInfo *auth_info, LinphoneAuthMethod method) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}
993

994 995 996 997 998 999 1000 1001 1002 1003 1004
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
		env->CallVoidMethod(lcData->listener,
							ljb->authenticationRequestedId,
							lcData->core,
							getAuthInfo(env, lc, auth_info, lcData->core),
							env->CallStaticObjectMethod(ljb->authMethodClass,ljb->authMethodFromIntId,(jint)method)
   						);
		handle_possible_java_exception(env, lcData->listener);
	}
1005 1006 1007 1008 1009 1010 1011 1012
	static void setCoreIfNotDone(JNIEnv *env, jobject jcore, LinphoneCore *lc){
		jclass objClass = env->GetObjectClass(jcore);
		jfieldID myFieldID = env->GetFieldID(objClass, "nativePtr", "J");
		jlong fieldVal = env->GetLongField(jcore, myFieldID);
		if (fieldVal == 0){
			env->SetLongField(jcore, myFieldID, (jlong)lc);
		}
	}
1013

1014 1015 1016 1017
	static void globalStateChange(LinphoneCore *lc, LinphoneGlobalState gstate,const char* message) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
1018
			ms_error("cannot attach VM");
1019 1020
			return;
		}
1021

Sylvain Berfini's avatar
Sylvain Berfini committed
1022
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1023 1024
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1025

1026 1027 1028
		jobject jcore = lcData->core;
		/*at this stage, the java object may not be aware of its C object, because linphone_core_new() hasn't returned yet.*/
		setCoreIfNotDone(env,jcore, lc);
1029

1030
		jstring msg = message ? env->NewStringUTF(message) : NULL;
1031
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1032
							,ljb->globalStateId
1033
							,lcData->core
Sylvain Berfini's avatar
Sylvain Berfini committed
1034
							,env->CallStaticObjectMethod(ljb->globalStateClass,ljb->globalStateFromIntId,(jint)gstate),
1035
							msg);
1036
		handle_possible_java_exception(env, lcData->listener);
1037 1038 1039
		if (msg) {
			env->DeleteLocalRef(msg);
		}
1040
	}
1041
	static void registrationStateChange(LinphoneCore *lc, LinphoneProxyConfig* proxy,LinphoneRegistrationState state,const char* message) {
1042 1043
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
1044
		jobject jproxy;
1045
		if (result != 0) {
1046
			ms_error("cannot attach VM");
1047 1048
			return;
		}
1049

Sylvain Berfini's avatar
Sylvain Berfini committed
1050
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1051 1052
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1053
		jstring msg = message ? env->NewStringUTF(message) : NULL;
1054
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1055
							,ljb->registrationStateId
1056
							,lcData->core
1057
							,(jproxy=getProxy(env,proxy,lcData->core))
Sylvain Berfini's avatar
Sylvain Berfini committed
1058
							,env->CallStaticObjectMethod(ljb->registrationStateClass,ljb->registrationStateFromIntId,(jint)state),
1059
							msg);
1060
		handle_possible_java_exception(env, lcData->listener);
1061 1062 1063
		if (msg) {
			env->DeleteLocalRef(msg);
		}
1064 1065 1066
		if (jproxy) {
			env->DeleteLocalRef(jproxy);
		}
1067
	}
1068

1069
	static void callStateChange(LinphoneCore *lc, LinphoneCall* call,LinphoneCallState state,const char* message) {
1070 1071
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
1072
		jobject jcall;
1073
		if (result != 0) {
1074
			ms_error("cannot attach VM");
1075 1076
			return;
		}
1077

Sylvain Berfini's avatar
Sylvain Berfini committed
1078
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1079 1080
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1081
		jstring msg = message ? env->NewStringUTF(message) : NULL;
1082
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1083
							,ljb->callStateId
1084
							,lcData->core
1085
							,(jcall=getCall(env,call))
Sylvain Berfini's avatar
Sylvain Berfini committed
1086
							,env->CallStaticObjectMethod(ljb->callStateClass,ljb->callStateFromIntId,(jint)state),
1087
							msg);
1088
		handle_possible_java_exception(env, lcData->listener);
1089
		if (state==LinphoneCallReleased) {
1090
			linphone_call_set_user_data(call,NULL);
1091 1092
			env->DeleteGlobalRef(jcall);
		}
1093 1094 1095
		if (msg) {
			env->DeleteLocalRef(msg);
		}
1096
	}
1097 1098 1099 1100
	static void callEncryptionChange(LinphoneCore *lc, LinphoneCall* call, bool_t encrypted,const char* authentication_token) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
1101
			ms_error("cannot attach VM");
1102 1103
			return;
		}
1104

Sylvain Berfini's avatar
Sylvain Berfini committed
1105
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1106 1107
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1108
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1109
							,ljb->callEncryptionChangedId
1110
							,lcData->core
1111
							,getCall(env,call)
1112 1113
							,encrypted
							,authentication_token ? env->NewStringUTF(authentication_token) : NULL);
1114
		handle_possible_java_exception(env, lcData->listener);
1115
	}
1116
	static void notify_presence_received(LinphoneCore *lc,  LinphoneFriend *my_friend) {
1117 1118
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
1119
		jobject jfriend = NULL;
1120

1121
		if (result != 0) {
1122
			ms_error("cannot attach VM");
1123 1124
			return;
		}
1125

Sylvain Berfini's avatar
Sylvain Berfini committed
1126
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1127 1128
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1129
		jfriend = getFriend(env, my_friend);
1130
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1131
							,ljb->notifyPresenceReceivedId
1132
							,lcData->core
1133
							,jfriend);
1134
		handle_possible_java_exception(env, lcData->listener);
1135
		env->DeleteLocalRef(jfriend);
1136
	}
1137
	static void new_subscription_requested(LinphoneCore *lc,  LinphoneFriend *my_friend, const char* url) {
1138 1139 1140
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
1141
			ms_error("cannot attach VM");
1142 1143
			return;
		}
1144

Sylvain Berfini's avatar
Sylvain Berfini committed
1145
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1146 1147
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
1148
		jobject jfriend =  getFriend(env, my_friend);
1149
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1150
							,ljb->newSubscriptionRequestId
1151
							,lcData->core
1152
							,jfriend
1153
							,url ? env->NewStringUTF(url) : NULL);
1154
		env->DeleteLocalRef(jfriend);
1155
		handle_possible_java_exception(env, lcData->listener);
1156
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
1157 1158 1159 1160
	static void dtmf_received(LinphoneCore *lc, LinphoneCall *call, int dtmf) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
1161
			ms_error("cannot attach VM");
Sylvain Berfini's avatar
Sylvain Berfini committed
1162 1163
			return;
		}
1164

Sylvain Berfini's avatar
Sylvain Berfini committed
1165
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1166 1167
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
Sylvain Berfini's avatar
Sylvain Berfini committed
1168
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1169
							,ljb->dtmfReceivedId
Sylvain Berfini's avatar
Sylvain Berfini committed
1170
							,lcData->core
1171
							,getCall(env,call)
Sylvain Berfini's avatar
Sylvain Berfini committed
1172
							,dtmf);
1173
		handle_possible_java_exception(env, lcData->listener);
Sylvain Berfini's avatar
Sylvain Berfini committed
1174
	}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
1175
	static void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
1176 1177 1178 1179 1180
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
1181
		}
1182

1183 1184
		jobject jmsg;
		jobject jroom;
Sylvain Berfini's avatar
Sylvain Berfini committed
1185
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
1186 1187 1188 1189
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData = (LinphoneCoreData*)linphone_core_v_table_get_user_data(table);
		/*note: we call linphone_chat_message_ref() because the application does not acquire the object when invoked from a callback*/
		env->CallVoidMethod(lcData->listener
Sylvain Berfini's avatar
Sylvain Berfini committed
1190
							,ljb->messageReceivedId
1191
							,lcData->core
1192 1193
							,(jroom = getChatRoom(env, room))
							,(jmsg = getChatMessage(env, msg)));
1194
		handle_possible_java_exception(env, lcData->listener);
1195

1196 1197 1198 1199 1200 1201
		if (jmsg) {
			env->DeleteLocalRef(jmsg);
		}
		if (jroom) {
			env->DeleteLocalRef(jroom);
		}
1202
	}
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

	static void message_received_unable_decrypt(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg) {
		JNIEnv *env = 0;
		jint result = jvm->AttachCurrentThread(&env,NULL);
		if (result != 0) {
			ms_error("cannot attach VM");
			return;
		}

		jobject jmsg;
		jobject jroom;
		LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_core_get_user_data(lc);
		LinphoneCoreVTable *table = linphone_core_get_current_vtable(lc);
		LinphoneCoreData* lcData