The Battle for Wesnoth  1.19.13+dev
battery_info.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 - 2025
3  by Martin HrubĂ˝ <hrubymar10@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "battery_info.hpp"
17 
18 #ifdef _WIN32
20 #endif
21 
22 #ifdef __APPLE__
23 #include "apple_battery_info.hpp"
24 #endif
25 
26 #ifdef HAVE_LIBDBUS
28 #endif
29 
30 #ifdef __ANDROID__
31 #include <SDL2/SDL_system.h> // For SDL Android functions
32 #include <jni.h>
33 #endif
34 
35 namespace desktop {
36 namespace battery_info {
37 
39 {
40 #if defined(_WIN32)
42 #elif defined(__APPLE__)
44 #elif defined(HAVE_LIBDBUS)
46 #elif defined(__ANDROID__)
47  return true;
48 #else
49  return false;
50 #endif
51 }
52 
54 {
55 #if defined(_WIN32)
57 #elif defined(__APPLE__)
59 #elif defined(HAVE_LIBDBUS)
61 #elif defined(__ANDROID__)
62  // call the helper method WesnothActivity.getBatteryPercentage() using JNI
63  JNIEnv* env = reinterpret_cast<JNIEnv*>(SDL_AndroidGetJNIEnv());
64  jobject wesnoth_instance = reinterpret_cast<jobject>(SDL_AndroidGetActivity());
65  jclass wesnoth_activity(env->GetObjectClass(wesnoth_instance));
66  jmethodID percentage = env->GetMethodID(wesnoth_activity, "getBatteryPercentage", "()D");
67  return env->CallDoubleMethod(wesnoth_instance, percentage);
68 #else
69  return -1;
70 #endif
71 }
72 
73 } // end namespace battery_info
74 } // end namespace desktop
static bool does_device_have_battery()
static double get_battery_percentage()
double get_battery_percentage()
bool does_device_have_battery()
double get_battery_percentage()