diff --git a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt index eee938ea1d..05317874bb 100644 --- a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt +++ b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/ExampleInstrumentedTest.kt @@ -27,6 +27,23 @@ class ExampleInstrumentedTest { userProfile.free() } + @Test + fun jni_user_profile_c_api() { + val userProfile = UserProfile.newInstance() + + assertFalse(userProfile.needsPush()) + assertFalse(userProfile.needsDump()) + + val name = userProfile.getName() + assertNull(name) + + val (toPush, seqNo) = userProfile.push() + assertEquals("d1:#i0e1:&de1:set_name(env->GetStringUTFChars(newName, nullptr)); } extern "C" JNIEXPORT jstring JNICALL -Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject obj) { - auto profile = ptrToProfile(env, obj); +Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject thiz) { + auto profile = ptrToProfile(env, thiz); auto name = profile->get_name(); + if (name == nullptr) return nullptr; jstring returnString = env->NewStringUTF(name->c_str()); return returnString; } extern "C" JNIEXPORT void JNICALL -Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject obj) { - auto profile = ptrToProfile(env, obj); +Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject thiz) { + auto profile = ptrToProfile(env, thiz); delete profile; } @@ -58,4 +59,29 @@ JNIEXPORT jboolean JNICALL Java_network_loki_messenger_libsession_1util_ConfigBase_dirty(JNIEnv *env, jobject thiz) { auto* configBase = ptrToConfigBase(env, thiz); return configBase->is_dirty(); +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConfigBase_needsPush(JNIEnv *env, jobject thiz) { + auto config = ptrToConfigBase(env, thiz); + return config->needs_push(); +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConfigBase_needsDump(JNIEnv *env, jobject thiz) { + auto config = ptrToConfigBase(env, thiz); + return config->needs_dump(); +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConfigBase_push(JNIEnv *env, jobject thiz) { + auto config = ptrToConfigBase(env, thiz); + auto pair = config->push(); + std::string to_push_str = pair.first; + jstring returnString = env->NewStringUTF(to_push_str.c_str()); + jlong seqNo = pair.second; + jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/ConfigWithSeqNo"); + jmethodID methodId = env->GetMethodID(returnObjectClass, "", "(Ljava/lang/String;J)V"); + jobject returnObject = env->NewObject(returnObjectClass, methodId, returnString, seqNo); + return returnObject; } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt index e765668d2f..046d1d70b1 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt @@ -1,5 +1,7 @@ package network.loki.messenger.libsession_util +import network.loki.messenger.libsession_util.util.ConfigWithSeqNo + sealed class ConfigBase(protected val /* yucky */ pointer: Long) { companion object { @@ -8,6 +10,9 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) { } } external fun dirty(): Boolean + external fun needsPush(): Boolean + external fun needsDump(): Boolean + external fun push(): ConfigWithSeqNo } class UserProfile(pointer: Long): ConfigBase(pointer) { @@ -18,6 +23,6 @@ class UserProfile(pointer: Long): ConfigBase(pointer) { external fun newInstance(): UserProfile } external fun setName(newName: String) - external fun getName(): String + external fun getName(): String? external fun free() } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Utils.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Utils.kt new file mode 100644 index 0000000000..51b0990299 --- /dev/null +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Utils.kt @@ -0,0 +1,28 @@ +package network.loki.messenger.libsession_util.util + +data class StringWithLen(private val bytes: ByteArray, private val len: Long) { // We might not need this class, could be helpful though + + override fun toString(): String = bytes.decodeToString() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as StringWithLen + + if (!bytes.contentEquals(other.bytes)) return false + if (len != other.len) return false + + return true + } + + override fun hashCode(): Int { + var result = bytes.contentHashCode() + result = 31 * result + len.hashCode() + return result + } + +} + + +data class ConfigWithSeqNo(val config: String, val seqNo: Long) \ No newline at end of file