mirror of
https://github.com/libretro/RetroArch
synced 2025-04-01 13:20:43 +00:00
support powerstate/battery level in Android
This commit is contained in:
parent
9d0c0b77ee
commit
7176bb19ad
@ -95,7 +95,7 @@ enum
|
|||||||
INTERNAL_STORAGE_NOT_WRITABLE
|
INTERNAL_STORAGE_NOT_WRITABLE
|
||||||
};
|
};
|
||||||
|
|
||||||
struct android_app *g_android;
|
struct android_app *g_android = NULL;
|
||||||
|
|
||||||
static pthread_key_t thread_key;
|
static pthread_key_t thread_key;
|
||||||
|
|
||||||
@ -1139,7 +1139,24 @@ static enum frontend_powerstate frontend_unix_get_powerstate(
|
|||||||
{
|
{
|
||||||
enum frontend_powerstate ret = FRONTEND_POWERSTATE_NONE;
|
enum frontend_powerstate ret = FRONTEND_POWERSTATE_NONE;
|
||||||
|
|
||||||
#ifndef ANDROID
|
#ifdef ANDROID
|
||||||
|
jint powerstate = ret;
|
||||||
|
jint battery_level = 0;
|
||||||
|
JNIEnv *env = jni_thread_getenv();
|
||||||
|
|
||||||
|
if (!env || !g_android)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
CALL_INT_METHOD(env, powerstate,
|
||||||
|
g_android->activity->clazz, g_android->getPowerstate);
|
||||||
|
|
||||||
|
CALL_INT_METHOD(env, battery_level,
|
||||||
|
g_android->activity->clazz, g_android->getBatteryLevel);
|
||||||
|
|
||||||
|
*percent = battery_level;
|
||||||
|
|
||||||
|
ret = (enum frontend_powerstate)powerstate;
|
||||||
|
#else
|
||||||
if (frontend_unix_powerstate_check_acpi_sysfs(&ret, seconds, percent))
|
if (frontend_unix_powerstate_check_acpi_sysfs(&ret, seconds, percent))
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
@ -1992,6 +2009,10 @@ static void frontend_unix_init(void *data)
|
|||||||
"onRetroArchExit", "()V");
|
"onRetroArchExit", "()V");
|
||||||
GET_METHOD_ID(env, android_app->isAndroidTV, class,
|
GET_METHOD_ID(env, android_app->isAndroidTV, class,
|
||||||
"isAndroidTV", "()Z");
|
"isAndroidTV", "()Z");
|
||||||
|
GET_METHOD_ID(env, android_app->getPowerstate, class,
|
||||||
|
"getPowerstate", "()I");
|
||||||
|
GET_METHOD_ID(env, android_app->getBatteryLevel, class,
|
||||||
|
"getBatteryLevel", "()I");
|
||||||
CALL_OBJ_METHOD(env, obj, android_app->activity->clazz,
|
CALL_OBJ_METHOD(env, obj, android_app->activity->clazz,
|
||||||
android_app->getIntent);
|
android_app->getIntent);
|
||||||
|
|
||||||
|
@ -160,6 +160,8 @@ struct android_app
|
|||||||
jmethodID getPendingIntentDownloadsLocation;
|
jmethodID getPendingIntentDownloadsLocation;
|
||||||
jmethodID getPendingIntentScreenshotsLocation;
|
jmethodID getPendingIntentScreenshotsLocation;
|
||||||
jmethodID isAndroidTV;
|
jmethodID isAndroidTV;
|
||||||
|
jmethodID getPowerstate;
|
||||||
|
jmethodID getBatteryLevel;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,10 @@ package com.retroarch.browser.retroactivity;
|
|||||||
|
|
||||||
import com.retroarch.browser.preferences.util.UserPreferences;
|
import com.retroarch.browser.preferences.util.UserPreferences;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.app.UiModeManager;
|
import android.app.UiModeManager;
|
||||||
|
import android.os.BatteryManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,6 +13,12 @@ import android.util.Log;
|
|||||||
*/
|
*/
|
||||||
public class RetroActivityCommon extends RetroActivityLocation
|
public class RetroActivityCommon extends RetroActivityLocation
|
||||||
{
|
{
|
||||||
|
public static int FRONTEND_POWERSTATE_NONE = 0;
|
||||||
|
public static int FRONTEND_POWERSTATE_NO_SOURCE = 1;
|
||||||
|
public static int FRONTEND_POWERSTATE_CHARGING = 2;
|
||||||
|
public static int FRONTEND_POWERSTATE_CHARGED = 3;
|
||||||
|
public static int FRONTEND_POWERSTATE_ON_POWER_SOURCE = 4;
|
||||||
|
|
||||||
// Exiting cleanly from NDK seems to be nearly impossible.
|
// Exiting cleanly from NDK seems to be nearly impossible.
|
||||||
// Have to use exit(0) to avoid weird things happening, even with runOnUiThread() approaches.
|
// Have to use exit(0) to avoid weird things happening, even with runOnUiThread() approaches.
|
||||||
// Use a separate JNI function to explicitly trigger the readback.
|
// Use a separate JNI function to explicitly trigger the readback.
|
||||||
@ -18,6 +27,46 @@ public class RetroActivityCommon extends RetroActivityLocation
|
|||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getBatteryLevel()
|
||||||
|
{
|
||||||
|
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||||
|
// This doesn't actually register anything (or need to) because we know this particular intent is sticky and we do not specify a BroadcastReceiver anyway
|
||||||
|
Intent batteryStatus = registerReceiver(null, ifilter);
|
||||||
|
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
|
||||||
|
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
|
||||||
|
|
||||||
|
float percent = ((float)level / (float)scale) * 100.0f;
|
||||||
|
|
||||||
|
Log.i("RetroActivity", "battery: level = " + level + ", scale = " + scale + ", percent = " + percent);
|
||||||
|
|
||||||
|
return (int)percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPowerstate()
|
||||||
|
{
|
||||||
|
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||||||
|
// This doesn't actually register anything (or need to) because we know this particular intent is sticky and we do not specify a BroadcastReceiver anyway
|
||||||
|
Intent batteryStatus = registerReceiver(null, ifilter);
|
||||||
|
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
||||||
|
boolean hasBattery = batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
|
||||||
|
boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING);
|
||||||
|
boolean isCharged = (status == BatteryManager.BATTERY_STATUS_FULL);
|
||||||
|
int powerstate = FRONTEND_POWERSTATE_NONE;
|
||||||
|
|
||||||
|
if (isCharged)
|
||||||
|
powerstate = FRONTEND_POWERSTATE_CHARGED;
|
||||||
|
else if (isCharging)
|
||||||
|
powerstate = FRONTEND_POWERSTATE_CHARGING;
|
||||||
|
else if (!hasBattery)
|
||||||
|
powerstate = FRONTEND_POWERSTATE_NO_SOURCE;
|
||||||
|
else
|
||||||
|
powerstate = FRONTEND_POWERSTATE_ON_POWER_SOURCE;
|
||||||
|
|
||||||
|
Log.i("RetroActivity", "power state = " + powerstate);
|
||||||
|
|
||||||
|
return powerstate;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isAndroidTV()
|
public boolean isAndroidTV()
|
||||||
{
|
{
|
||||||
Configuration config = getResources().getConfiguration();
|
Configuration config = getResources().getConfiguration();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user