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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. This software is provided 'as-is', without any express or implied
  37. warranty. In no event will the authors be held liable for any damages
  38. arising from the use of this software.
  39. Permission is granted to anyone to use this software for any purpose,
  40. including commercial applications, and to alter it and redistribute it
  41. freely, subject to the following restrictions:
  42. 1. The origin of this software must not be misrepresented; you must not
  43. claim that you wrote the original software. If you use this software
  44. in a product, an acknowledgment in the product documentation would be
  45. appreciated but is not required.
  46. 2. Altered source versions must be plainly marked as such, and must not be
  47. misrepresented as being the original software.
  48. 3. This notice may not be removed or altered from any source distribution.
  49. */
  50. #include "power_android.h"
  51. #include "core/error_macros.h"
  52. static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder) {
  53. if (refholder->m_env) {
  54. JNIEnv *env = refholder->m_env;
  55. (*env)->PopLocalFrame(env, NULL);
  56. --s_active;
  57. }
  58. }
  59. static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func) {
  60. struct LocalReferenceHolder refholder;
  61. refholder.m_env = NULL;
  62. refholder.m_func = func;
  63. return refholder;
  64. }
  65. static bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env) {
  66. const int capacity = 16;
  67. if ((*env)->PushLocalFrame(env, capacity) < 0) {
  68. return false;
  69. }
  70. ++s_active;
  71. refholder->m_env = env;
  72. return true;
  73. }
  74. static SDL_bool LocalReferenceHolder_IsActive(void) {
  75. return s_active > 0;
  76. }
  77. ANativeWindow *Android_JNI_GetNativeWindow(void) {
  78. ANativeWindow *anw;
  79. jobject s;
  80. JNIEnv *env = Android_JNI_GetEnv();
  81. s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
  82. anw = ANativeWindow_fromSurface(env, s);
  83. (*env)->DeleteLocalRef(env, s);
  84. return anw;
  85. }
  86. /*
  87. * CODE CHUNK IMPORTED FROM SDL 2.0
  88. * returns 0 on success or -1 on error (others undefined then)
  89. * returns truthy or falsy value in plugged, charged and battery
  90. * returns the value in seconds and percent or -1 if not available
  91. */
  92. int Android_JNI_GetPowerInfo(int *plugged, int *charged, int *battery, int *seconds, int *percent) {
  93. env = Android_JNI_GetEnv();
  94. refs = LocalReferenceHolder_Setup(__FUNCTION__);
  95. if (!LocalReferenceHolder_Init(&refs, env)) {
  96. LocalReferenceHolder_Cleanup(&refs);
  97. return -1;
  98. }
  99. mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;");
  100. context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid);
  101. action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED");
  102. cls = (*env)->FindClass(env, "android/content/IntentFilter");
  103. mid = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
  104. filter = (*env)->NewObject(env, cls, mid, action);
  105. (*env)->DeleteLocalRef(env, action);
  106. mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;");
  107. intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter);
  108. (*env)->DeleteLocalRef(env, filter);
  109. cls = (*env)->GetObjectClass(env, intent);
  110. imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I");
  111. // Watch out for C89 scoping rules because of the macro
  112. #define GET_INT_EXTRA(var, key) \
  113. int var; \
  114. iname = (*env)->NewStringUTF(env, key); \
  115. var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \
  116. (*env)->DeleteLocalRef(env, iname);
  117. bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z");
  118. // Watch out for C89 scoping rules because of the macro
  119. #define GET_BOOL_EXTRA(var, key) \
  120. int var; \
  121. bname = (*env)->NewStringUTF(env, key); \
  122. var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \
  123. (*env)->DeleteLocalRef(env, bname);
  124. if (plugged) {
  125. // Watch out for C89 scoping rules because of the macro
  126. GET_INT_EXTRA(plug, "plugged") // == BatteryManager.EXTRA_PLUGGED (API 5)
  127. if (plug == -1) {
  128. LocalReferenceHolder_Cleanup(&refs);
  129. return -1;
  130. }
  131. // 1 == BatteryManager.BATTERY_PLUGGED_AC
  132. // 2 == BatteryManager.BATTERY_PLUGGED_USB
  133. *plugged = (0 < plug) ? 1 : 0;
  134. }
  135. if (charged) {
  136. // Watch out for C89 scoping rules because of the macro
  137. GET_INT_EXTRA(status, "status") // == BatteryManager.EXTRA_STATUS (API 5)
  138. if (status == -1) {
  139. LocalReferenceHolder_Cleanup(&refs);
  140. return -1;
  141. }
  142. // 5 == BatteryManager.BATTERY_STATUS_FULL
  143. *charged = (status == 5) ? 1 : 0;
  144. }
  145. if (battery) {
  146. GET_BOOL_EXTRA(present, "present") // == BatteryManager.EXTRA_PRESENT (API 5)
  147. *battery = present ? 1 : 0;
  148. }
  149. if (seconds) {
  150. *seconds = -1; // not possible
  151. }
  152. if (percent) {
  153. int level;
  154. int scale;
  155. // Watch out for C89 scoping rules because of the macro
  156. {
  157. GET_INT_EXTRA(level_temp, "level") // == BatteryManager.EXTRA_LEVEL (API 5)
  158. level = level_temp;
  159. }
  160. // Watch out for C89 scoping rules because of the macro
  161. {
  162. GET_INT_EXTRA(scale_temp, "scale") // == BatteryManager.EXTRA_SCALE (API 5)
  163. scale = scale_temp;
  164. }
  165. if ((level == -1) || (scale == -1)) {
  166. LocalReferenceHolder_Cleanup(&refs);
  167. return -1;
  168. }
  169. *percent = level * 100 / scale;
  170. }
  171. (*env)->DeleteLocalRef(env, intent);
  172. LocalReferenceHolder_Cleanup(&refs);
  173. return 0;
  174. }
  175. bool PowerAndroid::GetPowerInfo_Android() {
  176. int battery;
  177. int plugged;
  178. int charged;
  179. if (Android_JNI_GetPowerInfo(&plugged, &charged, &battery, &this->nsecs_left, &this->percent_left) != -1) {
  180. if (plugged) {
  181. if (charged) {
  182. this->power_state = OS::POWERSTATE_CHARGED;
  183. } else if (battery) {
  184. this->power_state = OS::POWERSTATE_CHARGING;
  185. } else {
  186. this->power_state = OS::POWERSTATE_NO_BATTERY;
  187. this->nsecs_left = -1;
  188. this->percent_left = -1;
  189. }
  190. } else {
  191. this->power_state = OS::POWERSTATE_ON_BATTERY;
  192. }
  193. } else {
  194. this->power_state = OS::POWERSTATE_UNKNOWN;
  195. this->nsecs_left = -1;
  196. this->percent_left = -1;
  197. }
  198. return true;
  199. }
  200. OS::PowerState PowerAndroid::get_power_state() {
  201. if (GetPowerInfo_Android()) {
  202. return power_state;
  203. } else {
  204. WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN");
  205. return OS::POWERSTATE_UNKNOWN;
  206. }
  207. }
  208. int PowerAndroid::get_power_seconds_left() {
  209. if (GetPowerInfo_Android()) {
  210. return nsecs_left;
  211. } else {
  212. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  213. return -1;
  214. }
  215. }
  216. int PowerAndroid::get_power_percent_left() {
  217. if (GetPowerInfo_Android()) {
  218. return percent_left;
  219. } else {
  220. WARN_PRINT("Power management is not implemented on this platform, defaulting to -1");
  221. return -1;
  222. }
  223. }
  224. PowerAndroid::PowerAndroid() :
  225. nsecs_left(-1),
  226. percent_left(-1),
  227. power_state(OS::POWERSTATE_UNKNOWN) {
  228. }
  229. PowerAndroid::~PowerAndroid() {
  230. }