Repo created
This commit is contained in:
parent
81b91f4139
commit
f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions
70
TMessagesProj/jni/voip/libtgvoip/os/android/JNIUtilities.h
Normal file
70
TMessagesProj/jni/voip/libtgvoip/os/android/JNIUtilities.h
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//
|
||||
// Created by Grishka on 15.08.2018.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_JNIUTILITIES_H
|
||||
#define LIBTGVOIP_JNIUTILITIES_H
|
||||
|
||||
#include <functional>
|
||||
#include <jni.h>
|
||||
#include <stdarg.h>
|
||||
#include <string>
|
||||
#include "../../Buffers.h"
|
||||
|
||||
extern JavaVM* sharedJVM;
|
||||
|
||||
namespace tgvoip{
|
||||
namespace jni{
|
||||
|
||||
inline JNIEnv *GetEnv() {
|
||||
JNIEnv *env = nullptr;
|
||||
sharedJVM->GetEnv((void **) &env, JNI_VERSION_1_6);
|
||||
return env;
|
||||
}
|
||||
|
||||
inline void DoWithJNI(std::function<void(JNIEnv*)> f){
|
||||
JNIEnv *env=GetEnv();
|
||||
bool didAttach=false;
|
||||
if(!env){
|
||||
sharedJVM->AttachCurrentThread(&env, NULL);
|
||||
didAttach=true;
|
||||
}
|
||||
|
||||
f(env);
|
||||
|
||||
if(didAttach){
|
||||
sharedJVM->DetachCurrentThread();
|
||||
}
|
||||
}
|
||||
|
||||
inline void AttachAndCallVoidMethod(jmethodID method, jobject obj, ...){
|
||||
if(!method || !obj)
|
||||
return;
|
||||
va_list va;
|
||||
va_start(va, obj);
|
||||
DoWithJNI([&va, method, obj](JNIEnv* env){
|
||||
env->CallVoidMethodV(obj, method, va);
|
||||
});
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
inline std::string JavaStringToStdString(JNIEnv* env, jstring jstr){
|
||||
if(!jstr)
|
||||
return "";
|
||||
const char* jchars=env->GetStringUTFChars(jstr, NULL);
|
||||
std::string str(jchars);
|
||||
env->ReleaseStringUTFChars(jstr, jchars);
|
||||
return str;
|
||||
}
|
||||
|
||||
inline jbyteArray BufferToByteArray(JNIEnv* env, Buffer& buf){
|
||||
jbyteArray arr=env->NewByteArray((jsize)buf.Length());
|
||||
jbyte* elements=env->GetByteArrayElements(arr, NULL);
|
||||
memcpy(elements, *buf, buf.Length());
|
||||
env->ReleaseByteArrayElements(arr, elements, 0);
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LIBTGVOIP_JNIUTILITIES_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue