#ifndef __ANDROID_HELPER_H__ #define __ANDROID_HELPER_H__ #ifdef __ANDROID__ #include #include #include #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING #include #include #if 0 #ifndef ALOG #define ALOG(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) #ifdef NDEBUG #define ALOG(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0) #else #define ALOGV(...) __ALOGV(__VA_ARGS__) #endif #endif #endif // 0 #define MP_TAG "MPLOG" #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, MP_TAG,__VA_ARGS__) #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, MP_TAG,__VA_ARGS__) #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, MP_TAG, __VA_ARGS__) #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, MP_TAG, __VA_ARGS__) #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, MP_TAG,__VA_ARGS__) #define AASSERT(cond, fmt, ...) \ if (!(cond)) { \ __android_log_assert(#cond, MP_TAG, fmt, ##__VA_ARGS__); \ } inline std::string jstring2string(JNIEnv *env, jstring jStr) { if (!jStr) return ""; const jclass stringClass = env->GetObjectClass(jStr); const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B"); const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8")); size_t length = (size_t) env->GetArrayLength(stringJbytes); jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL); std::string ret = std::string((char *)pBytes, length); env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT); env->DeleteLocalRef(stringJbytes); env->DeleteLocalRef(stringClass); return ret; } inline std::wstring utf8_to_wstr(const std::string& src) { std::wstring_convert> converter; return converter.from_bytes(src); } inline int property_get(const char* key, char* buf, int len) { int ret = 0; std::string command = "getprop "; command += key; FILE* file = popen(command.c_str(), "r"); if (file) { std::fgets(buf, len, file); // ret = std::atoi(output); pclose(file); } return ret; } #endif #endif // __ANDROID_HELPER_H__