Skip to content
Permalink
Browse files

Update PowerSaveModeReceiver

  • Loading branch information...
Florin9doi committed Aug 13, 2019
1 parent aea4200 commit 18e7877526490410745a0f4349d90f08f6e6ebec
@@ -157,7 +157,6 @@ OpenSLContext::~OpenSLContext() {
bqPlayerObject = nullptr;
bqPlayerPlay = nullptr;
bqPlayerBufferQueue = nullptr;
bqPlayerMuteSolo = nullptr;
bqPlayerVolume = nullptr;
}

@@ -35,7 +35,6 @@ class OpenSLContext : public AudioContext {
SLObjectItf bqPlayerObject = nullptr;
SLPlayItf bqPlayerPlay = nullptr;
SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue = nullptr;
SLMuteSoloItf bqPlayerMuteSolo = nullptr;
SLVolumeItf bqPlayerVolume = nullptr;

// Double buffering.
@@ -703,6 +703,7 @@ protected void onDestroy() {
mSurfaceView.onDestroy();
mSurfaceView = null;
}
PowerSaveModeReceiver.destroy(this);
// TODO: Can we ensure that the GL thread has stopped rendering here?
// I've seen crashes that seem to indicate that sometimes it hasn't...
NativeApp.audioShutdown();
@@ -770,6 +771,7 @@ protected void onResume() {
mSurfaceView.onResume();
}
}
PowerSaveModeReceiver.sendPowerSaving(this);

gainAudioFocus(this.audioManager, this.audioFocusChangeListener);
NativeApp.resume();
@@ -5,32 +5,46 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

public class PowerSaveModeReceiver extends BroadcastReceiver {
private static final String TAG = PowerSaveModeReceiver.class.getSimpleName();
private static BroadcastReceiver saveModeReceiver = null;
private static boolean isPowerSaving = false;
private static boolean isBatteryLow = false;

private static final String TAG = "PowerSaveModeReceiver";

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

sendPowerSaving(context);
}

public static void initAndSend(final Activity activity) {
saveModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) {
sendPowerSaving(context);
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
activity.registerReceiver(saveModeReceiver, filter);

sendPowerSaving(activity);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
@@ -46,13 +60,20 @@ public void onChange(boolean selfChange, Uri uri) {
}
key = key.substring(key.lastIndexOf("/") + 1, key.length());
if (key.equals("user_powersaver_enable") || key.equals("psm_switch") || key.equals("powersaving_switch")) {
PowerSaveModeReceiver.sendPowerSaving(activity);
sendPowerSaving(activity);
}
}
});
}
}

public static void destroy(final Activity activity) {
if (saveModeReceiver != null) {
activity.unregisterReceiver(saveModeReceiver);
saveModeReceiver = null;
}
}

@TargetApi(21)
private static boolean getNativePowerSaving(final Context context) {
final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
@@ -67,7 +88,7 @@ private static boolean getExtraPowerSaving(final Context context) {
}
// Samsung (Touchwiz)
String s5Value = Settings.System.getString(context.getContentResolver(), "powersaving_switch");
boolean hasS5Value = s5Value != null && !s5Value.equals("");
boolean hasS5Value = !TextUtils.isEmpty(s5Value);
// On newer devices, psm_switch is always set, and powersaving_switch is used instead.
if ((!hasS5Value && getBooleanSetting(context, "psm_switch")) || getBooleanSetting(context, "powersaving_switch")) {
return true;
@@ -80,7 +101,7 @@ private static boolean getBooleanSetting(final Context context, final String nam
return value != null && value.equals("1");
}

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

0 comments on commit 18e7877

Please sign in to comment.
You can’t perform that action at this time.