From f366f09c4c79348e3e79f5e9073594f477d4cf7c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 24 Jul 2016 17:29:04 -0700 Subject: [PATCH] Android: Detect pre-L power saving. Sometimes users don't realize these are on, and they can slow down gameplay. Of course, they can also save battery. --- .../src/org/ppsspp/ppsspp/NativeActivity.java | 14 +--- .../ppsspp/ppsspp/PowerSaveModeReceiver.java | 68 +++++++++++++++---- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index 245f45c5b6..52cca0b46d 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -125,16 +125,6 @@ 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 libdir = null; try { @@ -283,9 +273,7 @@ public class NativeActivity extends Activity implements SurfaceHolder.Callback { javaGL = "true".equalsIgnoreCase(NativeApp.queryConfig("androidJavaGL")); sendInitialGrants(); - if (Build.VERSION.SDK_INT >= 21) { - sendPowerSaving(); - } + PowerSaveModeReceiver.initAndSend(this); // OK, config should be initialized, we can query for screen rotation. if (Build.VERSION.SDK_INT >= 9) { diff --git a/android/src/org/ppsspp/ppsspp/PowerSaveModeReceiver.java b/android/src/org/ppsspp/ppsspp/PowerSaveModeReceiver.java index 27a2b92f7d..1a016910d0 100644 --- a/android/src/org/ppsspp/ppsspp/PowerSaveModeReceiver.java +++ b/android/src/org/ppsspp/ppsspp/PowerSaveModeReceiver.java @@ -1,24 +1,22 @@ 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 boolean isPowerSaving = false; - private boolean isBatteryLow = false; + private static boolean isPowerSaving = false; + private static 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; @@ -26,16 +24,58 @@ public class PowerSaveModeReceiver extends BroadcastReceiver { 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"); } } - - @TargetApi(21) - private boolean getPowerSaving(final Context context) { - final PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); - return pm.isPowerSaveMode(); - } }