Skip to content

Commit

Permalink
Merge pull request #8871 from unknownbrackets/android-power
Browse files Browse the repository at this point in the history
Detect power saving mode on Android
  • Loading branch information
hrydgard authored Aug 6, 2016
2 parents ea6b72d + 372798e commit ef95bb0
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 3 deletions.
11 changes: 10 additions & 1 deletion Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ static bool windowHidden = false;
static double lastActivity = 0.0;
static double lastKeepAwake = 0.0;
static GraphicsContext *graphicsContext;
static bool powerSaving = false;

void Core_SetGraphicsContext(GraphicsContext *ctx) {
graphicsContext = ctx;
graphicsContext = ctx;
}

void Core_NotifyWindowHidden(bool hidden) {
Expand Down Expand Up @@ -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) {
g_dpi = 72;
g_dpi_scale = 1.0f;
Expand Down
3 changes: 3 additions & 0 deletions Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ bool UpdateScreenScale(int width, int height, bool smallWindow);
// Don't run the core when minimized etc.
void Core_NotifyWindowHidden(bool hidden);
void Core_NotifyActivity();

void Core_SetPowerSaving(bool mode);
bool Core_GetPowerSaving();
3 changes: 2 additions & 1 deletion Core/Reporting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Core/Reporting.h"

#include "Common/CPUDetect.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/Config.h"
#include "Core/CwCheat.h"
Expand Down Expand Up @@ -427,7 +428,7 @@ namespace Reporting
postdata.Add("graphics", StringFromFormat("%d", payload.int1));
postdata.Add("speed", StringFromFormat("%d", payload.int2));
postdata.Add("gameplay", StringFromFormat("%d", payload.int3));
postdata.Add("crc", StringFromFormat("%08x", RetrieveCRC()));
postdata.Add("crc", StringFromFormat("%08x", Core_GetPowerSaving() ? 0 : RetrieveCRC()));
AddScreenshotData(postdata, payload.string2);
payload.string1.clear();
payload.string2.clear();
Expand Down
5 changes: 5 additions & 0 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
8 changes: 8 additions & 0 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,14 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) {
// Show for the same duration as the preview.
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) {
Expand Down
5 changes: 4 additions & 1 deletion UI/ReportScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "UI/PauseScreen.h"
#include "UI/ReportScreen.h"

#include "Core/Core.h"
#include "Core/Reporting.h"
#include "Core/Screenshot.h"
#include "Core/System.h"
Expand Down Expand Up @@ -224,7 +225,9 @@ void ReportScreen::CreateViews() {
}

#ifdef MOBILE_DEVICE
leftColumnItems->Add(new TextView(rp->T("FeedbackIncludeCRC", "Note: Battery will be used to send a disc CRC"), new LinearLayoutParams(Margins(12, 5, 0, 5))))->SetEnabledPtr(&enableReporting_);
if (!Core_GetPowerSaving()) {
leftColumnItems->Add(new TextView(rp->T("FeedbackIncludeCRC", "Note: Battery will be used to send a disc CRC"), new LinearLayoutParams(Margins(12, 5, 0, 5))))->SetEnabledPtr(&enableReporting_);
}
#endif

std::string path = GetSysDirectory(DIRECTORY_SCREENSHOT);
Expand Down
8 changes: 8 additions & 0 deletions android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@
</intent-filter>
</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
android:name="xperiaplayoptimized_content"
android:resource="@drawable/ic_launcher" />
Expand Down
2 changes: 2 additions & 0 deletions android/src/org/ppsspp/ppsspp/NativeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.Vibrator;
import android.text.InputType;
import android.util.DisplayMetrics;
Expand Down Expand Up @@ -272,6 +273,7 @@ public void Initialize() {
javaGL = "true".equalsIgnoreCase(NativeApp.queryConfig("androidJavaGL"));

sendInitialGrants();
PowerSaveModeReceiver.initAndSend(this);

// OK, config should be initialized, we can query for screen rotation.
if (Build.VERSION.SDK_INT >= 9) {
Expand Down
81 changes: 81 additions & 0 deletions android/src/org/ppsspp/ppsspp/PowerSaveModeReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.ppsspp.ppsspp;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;

public class PowerSaveModeReceiver extends BroadcastReceiver {
private static boolean isPowerSaving = false;
private static boolean isBatteryLow = false;

@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_LOW)) {
isBatteryLow = true;
} else if (action.equals(Intent.ACTION_BATTERY_OKAY)) {
isBatteryLow = false;
}

sendPowerSaving(context);
}

public static void initAndSend(final Activity activity) {
sendPowerSaving(activity);

activity.getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, new ContentObserver(null) {
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);

String key = uri.getPath();
key = key.substring(key.lastIndexOf("/") + 1, key.length());
if (key != null && (key.equals("user_powersaver_enable") || key.equals("psm_switch"))) {
PowerSaveModeReceiver.sendPowerSaving(activity);
}
}
});
}

@TargetApi(21)
private static boolean getNativePowerSaving(final Context context) {
final PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
return pm.isPowerSaveMode();
}

private static boolean getExtraPowerSaving(final Context context) {
// http://stackoverflow.com/questions/25065635/checking-for-power-saver-mode-programically
// HTC (Sense)
String htcValue = Settings.System.getString(context.getContentResolver(), "user_powersaver_enable");
if (htcValue != null && htcValue.equals("1")) {
return true;
}
// Samsung (Touchwiz)
String samsungValue = Settings.System.getString(context.getContentResolver(), "psm_switch");
if (samsungValue != null && samsungValue.equals("1")) {
return true;
}
return false;
}

private static void sendPowerSaving(final Context context) {
if (Build.VERSION.SDK_INT >= 21) {
isPowerSaving = getNativePowerSaving(context) || getExtraPowerSaving(context);
} else {
isPowerSaving = getExtraPowerSaving(context);
}

if (isBatteryLow || isPowerSaving) {
NativeApp.sendMessage("core_powerSaving", "true");
} else {
NativeApp.sendMessage("core_powerSaving", "false");
}
}
}

0 comments on commit ef95bb0

Please sign in to comment.