Android: Detect and report power save mode.

This shows a message when it's on, or when power drops to low.
This commit is contained in:
Unknown W. Brackets 2016-07-24 17:04:06 -07:00
parent eb72a746b9
commit 1f7a137926
7 changed files with 89 additions and 1 deletions

View File

@ -56,9 +56,10 @@ static bool windowHidden = false;
static double lastActivity = 0.0; static double lastActivity = 0.0;
static double lastKeepAwake = 0.0; static double lastKeepAwake = 0.0;
static GraphicsContext *graphicsContext; static GraphicsContext *graphicsContext;
static bool powerSaving = false;
void Core_SetGraphicsContext(GraphicsContext *ctx) { void Core_SetGraphicsContext(GraphicsContext *ctx) {
graphicsContext = ctx; graphicsContext = ctx;
} }
void Core_NotifyWindowHidden(bool hidden) { void Core_NotifyWindowHidden(bool hidden) {
@ -120,6 +121,14 @@ void Core_WaitInactive(int milliseconds) {
} }
} }
void Core_SetPowerSaving(bool mode) {
powerSaving = mode;
}
bool Core_GetPowerSaving() {
return powerSaving;
}
bool UpdateScreenScale(int width, int height, bool smallWindow) { bool UpdateScreenScale(int width, int height, bool smallWindow) {
g_dpi = 72; g_dpi = 72;
g_dpi_scale = 1.0f; g_dpi_scale = 1.0f;

View File

@ -52,3 +52,6 @@ bool UpdateScreenScale(int width, int height, bool smallWindow);
// Don't run the core when minimized etc. // Don't run the core when minimized etc.
void Core_NotifyWindowHidden(bool hidden); void Core_NotifyWindowHidden(bool hidden);
void Core_NotifyActivity(); void Core_NotifyActivity();
void Core_SetPowerSaving(bool mode);
bool Core_GetPowerSaving();

View File

@ -196,6 +196,11 @@ void EmuScreen::bootComplete() {
} }
} }
if (Core_GetPowerSaving()) {
I18NCategory *sy = GetI18NCategory("System");
osm.Show(sy->T("WARNING: Battery save mode is on"), 2.0f, 0xFFFFFF, -1, true, "core_powerSaving");
}
System_SendMessage("event", "startgame"); System_SendMessage("event", "startgame");
} }

View File

@ -784,6 +784,14 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) {
// Show for the same duration as the preview. // Show for the same duration as the preview.
osm.Show(msg, 2.0f, 0xFFFFFF, -1, true, "savestate_slot"); osm.Show(msg, 2.0f, 0xFFFFFF, -1, true, "savestate_slot");
} }
if (msg == "core_powerSaving") {
if (value != "false") {
I18NCategory *sy = GetI18NCategory("System");
osm.Show(sy->T("WARNING: Battery save mode is on"), 2.0f, 0xFFFFFF, -1, true, "core_powerSaving");
}
Core_SetPowerSaving(value != "false");
}
} }
void NativeUpdate(InputState &input) { void NativeUpdate(InputState &input) {

View File

@ -96,6 +96,14 @@
</intent-filter> </intent-filter>
</activity> </activity>
<receiver android:name=".PowerSaveModeReceiver">
<intent-filter>
<action android:name="android.os.action.POWER_SAVE_MODE_CHANGED" />
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
</intent-filter>
</receiver>
<meta-data <meta-data
android:name="xperiaplayoptimized_content" android:name="xperiaplayoptimized_content"
android:resource="@drawable/ic_launcher" /> android:resource="@drawable/ic_launcher" />

View File

@ -28,6 +28,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.PowerManager;
import android.os.Vibrator; import android.os.Vibrator;
import android.text.InputType; import android.text.InputType;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
@ -124,6 +125,16 @@ public class NativeActivity extends Activity implements SurfaceHolder.Callback {
} }
} }
@TargetApi(21)
private void sendPowerSaving() {
final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if (pm.isPowerSaveMode()) {
NativeApp.sendMessage("core_powerSaving", "true");
} else {
NativeApp.sendMessage("core_powerSaving", "false");
}
}
String getApplicationLibraryDir(ApplicationInfo application) { String getApplicationLibraryDir(ApplicationInfo application) {
String libdir = null; String libdir = null;
try { try {
@ -272,6 +283,9 @@ public class NativeActivity extends Activity implements SurfaceHolder.Callback {
javaGL = "true".equalsIgnoreCase(NativeApp.queryConfig("androidJavaGL")); javaGL = "true".equalsIgnoreCase(NativeApp.queryConfig("androidJavaGL"));
sendInitialGrants(); sendInitialGrants();
if (Build.VERSION.SDK_INT >= 21) {
sendPowerSaving();
}
// OK, config should be initialized, we can query for screen rotation. // OK, config should be initialized, we can query for screen rotation.
if (Build.VERSION.SDK_INT >= 9) { if (Build.VERSION.SDK_INT >= 9) {

View File

@ -0,0 +1,41 @@
package org.ppsspp.ppsspp;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
public class PowerSaveModeReceiver extends BroadcastReceiver {
private boolean isPowerSaving = false;
private boolean isBatteryLow = false;
@Override
public void onReceive(final Context context, final Intent intent) {
if (Build.VERSION.SDK_INT >= 21) {
isPowerSaving = getPowerSaving(context);
} else {
isPowerSaving = false;
}
final String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_LOW)) {
isBatteryLow = true;
} else if (action.equals(Intent.ACTION_BATTERY_OKAY)) {
isBatteryLow = false;
}
if (isBatteryLow || isPowerSaving) {
NativeApp.sendMessage("core_powerSaving", "true");
} else {
NativeApp.sendMessage("core_powerSaving", "false");
}
}
@TargetApi(21)
private boolean getPowerSaving(final Context context) {
final PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
return pm.isPowerSaveMode();
}
}