toolkit.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // Created by #Suyghur, on 2021/06/12.
  3. //
  4. #include <logger.h>
  5. #include "include/toolkit.h"
  6. #include <sstream>
  7. jbyteArray ToolKit::GetJByteArray(JNIEnv *env, const char *src) {
  8. return nullptr;
  9. }
  10. jstring ToolKit::GetJString(JNIEnv *env, const char *src) {
  11. jclass clz = env->FindClass("java/lang/String");
  12. jmethodID methodID = env->GetMethodID(clz, "<init>", "([BLjava/lang/String;)V");
  13. jstring encoding = env->NewStringUTF("utf-8");
  14. auto length = static_cast<jsize>(strlen(src));
  15. jbyteArray bytes = env->NewByteArray(length);
  16. env->SetByteArrayRegion(bytes, 0, length, (jbyte *) src);
  17. auto result = (jstring) env->NewObject(clz, methodID, bytes, encoding);
  18. env->DeleteLocalRef(clz);
  19. env->DeleteLocalRef(encoding);
  20. env->DeleteLocalRef(bytes);
  21. return result;
  22. }
  23. std::string ToolKit::JString2String(JNIEnv *env, jstring jstr) {
  24. if (env == nullptr || jstr == nullptr) {
  25. return "";
  26. }
  27. char *tmp_ptr = nullptr;
  28. jclass clz = env->FindClass("java/lang/String");
  29. jstring encode_mode = env->NewStringUTF("UTF-8");
  30. jmethodID mid = env->GetMethodID(clz, "getBytes", "(Ljava/lang/String;)[B");
  31. auto byte_array = (jbyteArray) (env->CallObjectMethod(jstr, mid, encode_mode));
  32. jsize len = env->GetArrayLength(byte_array);
  33. if (len == 0) {
  34. free(tmp_ptr);
  35. return "";
  36. }
  37. jbyte *byte = env->GetByteArrayElements(byte_array, JNI_FALSE);
  38. if (len > 0) {
  39. tmp_ptr = (char *) malloc(len + 1);
  40. memcpy(tmp_ptr, byte, len);
  41. tmp_ptr[len] = 0;
  42. }
  43. env->ReleaseByteArrayElements(byte_array, byte, JNI_FALSE);
  44. std::string result(tmp_ptr);
  45. free(tmp_ptr);
  46. env->DeleteLocalRef(clz);
  47. env->DeleteLocalRef(encode_mode);
  48. env->DeleteLocalRef(byte_array);
  49. return result;
  50. }
  51. int ToolKit::String2Int(const std::string &s) {
  52. int value = 0;
  53. std::stringstream ss;
  54. ss << s;
  55. ss >> value;
  56. return value;
  57. }
  58. std::string ToolKit::GetPackageName(JNIEnv *env, jobject context) {
  59. jclass clz = env->GetObjectClass(context);
  60. if (clz == nullptr) {
  61. Logger::E(env, "Context clz is null");
  62. return "";
  63. }
  64. const char *method_name = "getPackageName";
  65. const char *sig = "()Ljava/lang/String;";
  66. jmethodID mid = env->GetMethodID(clz, method_name, sig);
  67. auto result = (jstring) env->CallObjectMethod(context, mid);
  68. env->DeleteLocalRef(clz);
  69. // free((void *) method_name);
  70. // free((void *) sig);
  71. return JString2String(env, result);
  72. }
  73. std::string ToolKit::GetServerVersion(JNIEnv *env) {
  74. jclass clz = env->FindClass("cn/yyxx/eyuangame/Version");
  75. if (clz == nullptr) {
  76. Logger::E(env, "Version clz is null");
  77. return "";
  78. }
  79. const char *field_name = "SERVER_VERSION";
  80. const char *sig = "Ljava/lang/String;";
  81. jfieldID fid = env->GetStaticFieldID(clz, field_name, sig);
  82. auto result = (jstring) env->GetStaticObjectField(clz, fid);
  83. env->DeleteLocalRef(clz);
  84. // free((void *) field_name);
  85. // free((void *) sig);
  86. return JString2String(env, result);
  87. }
  88. std::string ToolKit::GetClientVersion(JNIEnv *env) {
  89. jclass clz = env->FindClass("cn/yyxx/eyuangame/Version");
  90. if (clz == nullptr) {
  91. Logger::E(env, "Version clz is null");
  92. return "";
  93. }
  94. const char *field_name = "VERSION_NAME";
  95. const char *sig = "Ljava/lang/String;";
  96. jfieldID fid = env->GetStaticFieldID(clz, field_name, sig);
  97. auto result = (jstring) env->GetStaticObjectField(clz, fid);
  98. env->DeleteLocalRef(clz);
  99. // free((void *) field_name);
  100. // free((void *) sig);
  101. return JString2String(env, result);
  102. }
  103. std::string ToolKit::GetVersionName(JNIEnv *env, jobject context) {
  104. jclass clz = env->FindClass("cn/yyxx/support/AppUtils");
  105. if (clz == nullptr) {
  106. Logger::E(env, "AppUtils clz is null");
  107. return "";
  108. }
  109. const char *method_name = "getVersionName";
  110. const char *sig = "(Landroid/content/Context;)Ljava/lang/String;";
  111. jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
  112. auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context);
  113. env->DeleteLocalRef(clz);
  114. // free((void *) method_name);
  115. // free((void *) sig);
  116. return JString2String(env, result);
  117. }
  118. std::string ToolKit::GetVersionCode(JNIEnv *env, jobject context) {
  119. jclass clz = env->FindClass("cn/yyxx/support/AppUtils");
  120. if (clz == nullptr) {
  121. Logger::E(env, "AppUtils clz is null");
  122. return "";
  123. }
  124. const char *method_name = "getVersionCodeStr";
  125. const char *sig = "(Landroid/content/Context;)Ljava/lang/String;";
  126. jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
  127. auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context);
  128. env->DeleteLocalRef(clz);
  129. // free((void *) method_name);
  130. // free((void *) sig);
  131. return JString2String(env, result);
  132. }
  133. std::string ToolKit::GetAndroidDeviceId(JNIEnv *env, jobject context) {
  134. jclass clz = env->FindClass("cn/yyxx/support/device/DeviceInfoUtils");
  135. if (clz == nullptr) {
  136. Logger::E(env, "UUIDUtils clz is null");
  137. return "";
  138. }
  139. const char *method_name = "getAndroidDeviceId";
  140. const char *sig = "(Landroid/content/Context;)Ljava/lang/String;";
  141. jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
  142. auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context);
  143. env->DeleteLocalRef(clz);
  144. // free((void *) method_name);
  145. // free((void *) sig);
  146. return JString2String(env, result);
  147. }
  148. int ToolKit::GetNetworkType(JNIEnv *env, jobject context) {
  149. jclass clz = env->FindClass("cn/yyxx/support/device/DeviceInfoUtils");
  150. if (clz == nullptr) {
  151. Logger::E(env, "DeviceInfoUtils clz is null");
  152. return 0;
  153. }
  154. const char *method_name = "getNetworkClass";
  155. const char *sig = "(Landroid/content/Context;)Ljava/lang/String;";
  156. jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
  157. auto result = (jstring) env->CallStaticObjectMethod(clz, mid, context);
  158. env->DeleteLocalRef(clz);
  159. // free((void *) method_name);
  160. // free((void *) sig);
  161. return String2Int(JString2String(env, result));
  162. }
  163. std::string ToolKit::GetDeviceManufacturer() {
  164. char *manufacturer = (char *) malloc(128 * sizeof(char));
  165. __system_property_get("ro.product.manufacturer", manufacturer);
  166. std::string result = manufacturer;
  167. free(manufacturer);
  168. return std::string(result);
  169. }
  170. std::string ToolKit::GetDeviceModel() {
  171. char *model = (char *) malloc(128 * sizeof(char));
  172. __system_property_get("ro.product.model", model);
  173. std::string result = model;
  174. free(model);
  175. return result;
  176. }
  177. std::string ToolKit::GetMobileBrand() {
  178. char *brand = (char *) malloc(128 * sizeof(char));
  179. __system_property_get("ro.product.brand", brand);
  180. std::string result = brand;
  181. free(brand);
  182. return result;
  183. }
  184. std::string ToolKit::GetDeviceSoftwareVersion() {
  185. char *version = (char *) malloc(128 * sizeof(char));
  186. __system_property_get("ro.build.version.release", version);
  187. std::string result = version;
  188. free(version);
  189. return result;
  190. }
  191. bool ToolKit::IsEmulator(JNIEnv *env, jobject context) {
  192. jclass clz = env->FindClass("cn/yyxx/support/device/DeviceInfoUtils");
  193. if (clz == nullptr) {
  194. Logger::E(env, "DeviceInfoUtils clz is null");
  195. return "";
  196. }
  197. const char *method_name = "isEmulator";
  198. const char *sig = "(Landroid/content/Context;)Z";
  199. jmethodID mid = env->GetStaticMethodID(clz, method_name, sig);
  200. auto result = (jboolean) env->CallStaticBooleanMethod(clz, mid, context);
  201. env->DeleteLocalRef(clz);
  202. // free((void *) method_name);
  203. // free((void *) sig);
  204. return result == JNI_TRUE;
  205. }
  206. std::string ToolKit::ToJsonString(const Json::Value &root) {
  207. Json::StreamWriterBuilder builder;
  208. std::ostringstream oss;
  209. //无格式输出
  210. builder.settings_["indentation"] = "";
  211. std::unique_ptr<Json::StreamWriter> json_writer(builder.newStreamWriter());
  212. json_writer->write(root, &oss);
  213. return oss.str();
  214. }
  215. Json::Value ToolKit::ToJsonObject(const std::string &json) {
  216. Json::CharReaderBuilder builder;
  217. Json::CharReader *reader_ptr(builder.newCharReader());
  218. JSONCPP_STRING errs;
  219. Json::Value root;
  220. reader_ptr->parse(json.c_str(), json.c_str() + json.length(), &root, &errs);
  221. return root;
  222. }
  223. char *ToolKit::StrReverse(const char *src) {
  224. char *tmp = new char[strlen(src)];
  225. strcpy(tmp, src);
  226. char *ret = tmp;//用来最后返回数组指针
  227. char *p = tmp + strlen(src) - 1;
  228. while (p > tmp) {
  229. char t = *tmp;
  230. *tmp++ = *p;
  231. *p-- = t;
  232. }
  233. free(p);
  234. return ret;
  235. }