power_android.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**************************************************************************/
  2. /* power_android.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. /*
  31. Adapted from corresponding SDL 2.0 code.
  32. */
  33. /*
  34. * Simple DirectMedia Layer
  35. * Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  36. *
  37. * This software is provided 'as-is', without any express or implied
  38. * warranty. In no event will the authors be held liable for any damages
  39. * arising from the use of this software.
  40. *
  41. * Permission is granted to anyone to use this software for any purpose,
  42. * including commercial applications, and to alter it and redistribute it
  43. * freely, subject to the following restrictions:
  44. *
  45. * 1. The origin of this software must not be misrepresented; you must not
  46. * claim that you wrote the original software. If you use this software
  47. * in a product, an acknowledgment in the product documentation would be
  48. * appreciated but is not required.
  49. * 2. Altered source versions must be plainly marked as such, and must not be
  50. * misrepresented as being the original software.
  51. * 3. This notice may not be removed or altered from any source distribution.
  52. */
  53. #include "power_android.h"
  54. #include "core/error_macros.h"
  55. static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder) {
  56. if (refholder->m_env) {
  57. JNIEnv *env = refholder->m_env;
  58. (*env)->PopLocalFrame(env, NULL);
  59. --s_active;
  60. }
  61. }
  62. static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func) {
  63. struct LocalReferenceHolder refholder;
  64. refholder.m_env = NULL;
  65. refholder.m_func = func;
  66. return refholder;
  67. }
  68. static bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env) {
  69. const int capacity = 16;
  70. if ((*env)->PushLocalFrame(env, capacity) < 0) {
  71. return false;
  72. }
  73. ++s_active;
  74. refholder->m_env = env;
  75. return true;
  76. }
  77. static SDL_bool LocalReferenceHolder_IsActive(void) {
  78. return s_active > 0;
  79. }
  80. ANativeWindow *Android_JNI_GetNativeWindow(void) {
  81. ANativeWindow *anw;
  82. jobject s;
  83. JNIEnv *env = Android_JNI_GetEnv();
  84. s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
  85. anw = ANativeWindow_fromSurface(env, s);
  86. (*env)->DeleteLocalRef(env, s);
  87. return anw;
  88. }
  89. /*
  90. * CODE CHUNK IMPORTED FROM SDL 2.0
  91. * returns 0 on success or -1 on error (others undefined then)
  92. * returns truthy or falsy value in plugged, charged and battery
  93. * returns the value in seconds and percent or -1 if not available
  94. */
  95. int Android_JNI_GetPowerInfo(int *plugged, int *charged, int *battery, int *seconds, int *percent) {
  96. env = Android_JNI_GetEnv();
  97. refs = LocalReferenceHolder_Setup(__FUNCTION__);
  98. if (!LocalReferenceHolder_Init(&refs, env)) {
  99. LocalReferenceHolder_Cleanup(&refs);
  100. return -1;
  101. }
  102. mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
  103. context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
  104. action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED");
  105. cls = (*env)->FindClass(env, "android/content/IntentFilter");
  106. mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
  107. filter = (*env)->NewObject(env, cls, mid, action);
  108. (*env)->DeleteLocalRef(env, action);
  109. mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
  110. intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter);
  111. (*env)->DeleteLocalRef(env, filter);
  112. cls = (*env)->GetObjectClass(env, intent);
  113. imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I");
  114. // Watch out for C89 scoping rules because of the macro
  115. #define GET_INT_EXTRA(var, key) \
  116. int var; \
  117. iname = (*env)->NewStringUTF(env, key); \
  118. var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \
  119. (*env)->DeleteLocalRef(env, iname);
  120. bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
  121. // Watch out for C89 scoping rules because of the macro
  122. #define GET_BOOL_EXTRA(var, key) \
  123. int var; \
  124. bname = (*env)->NewStringUTF(env, key); \
  125. var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \
  126. (*env)->DeleteLocalRef(env, bname);
  127. if (plugged) {
  128. // Watch out for C89 scoping rules because of the macro
  129. GET_INT_EXTRA(plug, "plugged") // == BatteryManager.EXTRA_PLUGGED (API 5)
  130. if (plug == -1) {
  131. LocalReferenceHolder_Cleanup(&refs);
  132. return -1;
  133. }
  134. // 1 == BatteryManager.BATTERY_PLUGGED_AC
  135. // 2 == BatteryManager.BATTERY_PLUGGED_USB
  136. *plugged = (0 < plug) ? 1 : 0;
  137. }
  138. if (charged) {
  139. // Watch out for C89 scoping rules because of the macro
  140. GET_INT_EXTRA(status, "status") // == BatteryManager.EXTRA_STATUS (API 5)
  141. if (status == -1) {
  142. LocalReferenceHolder_Cleanup(&refs);
  143. return -1;
  144. }
  145. // 5 == BatteryManager.BATTERY_STATUS_FULL
  146. *charged = (status == 5) ? 1 : 0;
  147. }
  148. if (battery) {
  149. GET_BOOL_EXTRA(present, "present") // == BatteryManager.EXTRA_PRESENT (API 5)
  150. *battery = present ? 1 : 0;
  151. }
  152. if (seconds) {
  153. *seconds = -1; // not possible
  154. }
  155. if (percent) {
  156. int level;
  157. int scale;
  158. // Watch out for C89 scoping rules because of the macro
  159. {
  160. GET_INT_EXTRA(level_temp, "level") // == BatteryManager.EXTRA_LEVEL (API 5)
  161. level = level_temp;
  162. }
  163. // Watch out for C89 scoping rules because of the macro
  164. {
  165. GET_INT_EXTRA(scale_temp, "scale") // == BatteryManager.EXTRA_SCALE (API 5)
  166. scale = scale_temp;
  167. }
  168. if ((level == -1) || (scale == -1)) {
  169. LocalReferenceHolder_Cleanup(&refs);
  170. return -1;
  171. }
  172. *percent = level * 100 / scale;
  173. }
  174. (*env)->DeleteLocalRef(env, intent);
  175. LocalReferenceHolder_Cleanup(&refs);
  176. return 0;
  177. }
  178. bool PowerAndroid::GetPowerInfo_Android() {
  179. int battery;
  180. int plugged;
  181. int charged;
  182. if (Android_JNI_GetPowerInfo(&plugged, &charged, &battery, &this->nsecs_left, &this->percent_left) != -1) {
  183. if (plugged) {
  184. if (charged) {
  185. this->power_state = OS::POWERSTATE_CHARGED;
  186. } else if (battery) {
  187. this->power_state = OS::POWERSTATE_CHARGING;
  188. } else {
  189. this->power_state = OS::POWERSTATE_NO_BATTERY;
  190. this->nsecs_left = -1;
  191. this->percent_left = -1;
  192. }
  193. } else {
  194. this->power_state = OS::POWERSTATE_ON_BATTERY;
  195. }
  196. } else {
  197. this->power_state = OS::POWERSTATE_UNKNOWN;
  198. this->nsecs_left = -1;
  199. this->percent_left = -1;
  200. }
  201. return true;
  202. }
  203. OS::PowerState PowerAndroid::get_power_state() {
  204. if (GetPowerInfo_Android()) {
  205. return power_state;
  206. } else {
  207. WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN");
  208. return OS::POWERSTATE_UNKNOWN;
  209. }
  210. }
  211. int PowerAndroid::get_power_seconds_left() {
  212. if (GetPowerInfo_Android()) {
  213. return nsecs_left;
  214. } else {
  215. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  216. return -1;
  217. }
  218. }
  219. int PowerAndroid::get_power_percent_left() {
  220. if (GetPowerInfo_Android()) {
  221. return percent_left;
  222. } else {
  223. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  224. return -1;
  225. }
  226. }
  227. PowerAndroid::PowerAndroid() :
  228. nsecs_left(-1),
  229. percent_left(-1),
  230. power_state(OS::POWERSTATE_UNKNOWN) {
  231. }
  232. PowerAndroid::~PowerAndroid() {
  233. }