5
5
#include < vector>
6
6
#include " session/config/contacts.hpp"
7
7
#include " util.h"
8
+ #include " jni_utils.h"
8
9
9
10
inline session::config::Contacts *ptrToContacts (JNIEnv *env, jobject obj) {
10
- jclass contactsClass = env->FindClass (" network/loki/messenger/libsession_util/Contacts" );
11
- jfieldID pointerField = env->GetFieldID (contactsClass, " pointer" , " J" );
11
+ auto contactsClass = jni_utils::JavaLocalRef ( env, env ->FindClass (" network/loki/messenger/libsession_util/Contacts" ) );
12
+ jfieldID pointerField = env->GetFieldID (contactsClass. get () , " pointer" , " J" );
12
13
return (session::config::Contacts *) env->GetLongField (obj, pointerField);
13
14
}
14
15
15
16
inline jobject serialize_contact (JNIEnv *env, session::config::contact_info info) {
16
- jclass contactClass = env->FindClass (" network/loki/messenger/libsession_util/util/Contact" );
17
- jmethodID constructor = env->GetMethodID (contactClass, " <init>" , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZLnetwork/loki/messenger/libsession_util/util/UserPic;JLnetwork/loki/messenger/libsession_util/util/ExpiryMode;)V" );
18
- jstring id = env->NewStringUTF (info.session_id .data ());
19
- jstring name = env->NewStringUTF (info.name .data ());
20
- jstring nickname = env->NewStringUTF (info.nickname .data ());
17
+ auto contactClass = jni_utils::JavaLocalRef ( env, env ->FindClass (" network/loki/messenger/libsession_util/util/Contact" ) );
18
+ jmethodID constructor = env->GetMethodID (contactClass. get () , " <init>" , " (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZLnetwork/loki/messenger/libsession_util/util/UserPic;JLnetwork/loki/messenger/libsession_util/util/ExpiryMode;)V" );
19
+ auto id = jni_utils::JavaLocalRef ( env, env ->NewStringUTF (info.session_id .data () ));
20
+ auto name = jni_utils::JavaLocalRef ( env, env ->NewStringUTF (info.name .data () ));
21
+ auto nickname = jni_utils::JavaLocalRef ( env, env ->NewStringUTF (info.nickname .data () ));
21
22
jboolean approved, approvedMe, blocked;
22
23
approved = info.approved ;
23
24
approvedMe = info.approved_me ;
24
25
blocked = info.blocked ;
25
26
auto created = info.created ;
26
- jobject profilePic = util::serialize_user_pic (env, info.profile_picture );
27
- jobject returnObj = env->NewObject (contactClass, constructor, id, name, nickname, approved,
28
- approvedMe, blocked, profilePic, (jlong)info.priority ,
27
+ auto profilePic = jni_utils::JavaLocalRef (env, util::serialize_user_pic (env, info.profile_picture ) );
28
+ jobject returnObj = env->NewObject (contactClass. get () , constructor, id. get () , name. get () , nickname. get () , approved,
29
+ approvedMe, blocked, profilePic. get () , (jlong)info.priority ,
29
30
util::serialize_expiry (env, info.exp_mode , info.exp_timer ));
30
31
return returnObj;
31
32
}
@@ -44,35 +45,34 @@ inline session::config::contact_info deserialize_contact(JNIEnv *env, jobject in
44
45
" Lnetwork/loki/messenger/libsession_util/util/UserPic;" );
45
46
getPriority = env->GetFieldID (contactClass, " priority" , " J" );
46
47
getExpiry = env->GetFieldID (contactClass, " expiryMode" , " Lnetwork/loki/messenger/libsession_util/util/ExpiryMode;" );
47
- jstring name, nickname, account_id;
48
- account_id = static_cast <jstring>(env->GetObjectField (info, getId));
49
- name = static_cast <jstring>(env->GetObjectField (info, getName));
50
- nickname = static_cast <jstring>(env->GetObjectField (info, getNick));
48
+ auto account_id = jni_utils::JavaLocalRef (env, static_cast <jstring>(env->GetObjectField (info, getId)));
49
+ auto name = jni_utils::JavaLocalRef (env, static_cast <jstring>(env->GetObjectField (info, getName)));
50
+ auto nickname = jni_utils::JavaLocalRef (env, static_cast <jstring>(env->GetObjectField (info, getNick)));
51
51
bool approved, approvedMe, blocked, hidden;
52
52
int priority = env->GetLongField (info, getPriority);
53
53
approved = env->GetBooleanField (info, getApproved);
54
54
approvedMe = env->GetBooleanField (info, getApprovedMe);
55
55
blocked = env->GetBooleanField (info, getBlocked);
56
- jobject user_pic = env->GetObjectField (info, getUserPic);
57
- jobject expiry_mode = env->GetObjectField (info, getExpiry);
56
+ auto user_pic = jni_utils::JavaLocalRef ( env, env ->GetObjectField (info, getUserPic) );
57
+ auto expiry_mode = jni_utils::JavaLocalRef ( env, env ->GetObjectField (info, getExpiry) );
58
58
59
- auto expiry_pair = util::deserialize_expiry (env, expiry_mode);
59
+ auto expiry_pair = util::deserialize_expiry (env, expiry_mode. get () );
60
60
61
61
std::string url;
62
62
std::vector<unsigned char > key;
63
63
64
- if (user_pic != nullptr ) {
65
- auto deserialized_pic = util::deserialize_user_pic (env, user_pic);
64
+ if (user_pic. get () != nullptr ) {
65
+ auto deserialized_pic = util::deserialize_user_pic (env, user_pic. get () );
66
66
auto url_jstring = deserialized_pic.first ;
67
67
auto url_bytes = env->GetStringUTFChars (url_jstring, nullptr );
68
68
url = std::string (url_bytes);
69
69
env->ReleaseStringUTFChars (url_jstring, url_bytes);
70
70
key = util::vector_from_bytes (env, deserialized_pic.second );
71
71
}
72
72
73
- auto account_id_bytes = env->GetStringUTFChars (account_id, nullptr );
74
- auto name_bytes = name ? env->GetStringUTFChars (name, nullptr ) : nullptr ;
75
- auto nickname_bytes = nickname ? env->GetStringUTFChars (nickname, nullptr ) : nullptr ;
73
+ auto account_id_bytes = env->GetStringUTFChars (account_id. get () , nullptr );
74
+ auto name_bytes = name. get () ? env->GetStringUTFChars (name. get () , nullptr ) : nullptr ;
75
+ auto nickname_bytes = nickname. get () ? env->GetStringUTFChars (nickname. get () , nullptr ) : nullptr ;
76
76
77
77
auto contact_info = conf->get_or_construct (account_id_bytes);
78
78
if (name_bytes) {
@@ -90,12 +90,12 @@ inline session::config::contact_info deserialize_contact(JNIEnv *env, jobject in
90
90
contact_info.profile_picture = session::config::profile_pic ();
91
91
}
92
92
93
- env->ReleaseStringUTFChars (account_id, account_id_bytes);
93
+ env->ReleaseStringUTFChars (account_id. get () , account_id_bytes);
94
94
if (name_bytes) {
95
- env->ReleaseStringUTFChars (name, name_bytes);
95
+ env->ReleaseStringUTFChars (name. get () , name_bytes);
96
96
}
97
97
if (nickname_bytes) {
98
- env->ReleaseStringUTFChars (nickname, nickname_bytes);
98
+ env->ReleaseStringUTFChars (nickname. get () , nickname_bytes);
99
99
}
100
100
101
101
contact_info.priority = priority;
0 commit comments