am 2e205e9b
: Merge "Settings: Use new PowerManager call to set low power mode." into lmp-dev
* commit '2e205e9b4b16133b29efba91e67bee8f20a51a88': Settings: Use new PowerManager call to set low power mode.
This commit is contained in:
@@ -16,14 +16,20 @@
|
|||||||
|
|
||||||
package com.android.settings.fuelgauge;
|
package com.android.settings.fuelgauge;
|
||||||
|
|
||||||
|
import static android.os.PowerManager.ACTION_POWER_SAVE_MODE_CHANGING;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.provider.Settings.Global;
|
import android.provider.Settings.Global;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
@@ -43,6 +49,7 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
|
|
||||||
private final Handler mHandler = new Handler();
|
private final Handler mHandler = new Handler();
|
||||||
private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
|
private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
|
||||||
|
private final Receiver mReceiver = new Receiver();
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private boolean mCreated;
|
private boolean mCreated;
|
||||||
@@ -50,6 +57,7 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
private SwitchBar mSwitchBar;
|
private SwitchBar mSwitchBar;
|
||||||
private Switch mSwitch;
|
private Switch mSwitch;
|
||||||
private boolean mValidListener;
|
private boolean mValidListener;
|
||||||
|
private PowerManager mPowerManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
@@ -76,6 +84,8 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
mTriggerPref.init(this);
|
mTriggerPref.init(this);
|
||||||
|
|
||||||
|
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,6 +98,7 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
mSettingsObserver.setListening(true);
|
mSettingsObserver.setListening(true);
|
||||||
|
mReceiver.setListening(true);
|
||||||
if (!mValidListener) {
|
if (!mValidListener) {
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
mSwitchBar.addOnSwitchChangeListener(this);
|
||||||
mValidListener = true;
|
mValidListener = true;
|
||||||
@@ -99,6 +110,7 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
mSettingsObserver.setListening(false);
|
mSettingsObserver.setListening(false);
|
||||||
|
mReceiver.setListening(false);
|
||||||
if (mValidListener) {
|
if (mValidListener) {
|
||||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||||
mValidListener = false;
|
mValidListener = false;
|
||||||
@@ -107,43 +119,78 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||||
|
mHandler.removeCallbacks(mStartMode);
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
|
mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
|
||||||
} else {
|
} else {
|
||||||
if (DEBUG) Log.d(TAG, "Stopping LOW_POWER_MODE from settings");
|
if (DEBUG) Log.d(TAG, "Stopping low power mode from settings");
|
||||||
Global.putInt(getContentResolver(), Global.LOW_POWER_MODE, 0);
|
trySetPowerSaveMode(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void trySetPowerSaveMode(boolean mode) {
|
||||||
|
if (!mPowerManager.setPowerSaveMode(mode)) {
|
||||||
|
if (DEBUG) Log.d(TAG, "Setting mode failed, fallback to current value");
|
||||||
|
mHandler.post(mUpdateSwitch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateSwitch() {
|
private void updateSwitch() {
|
||||||
final boolean checked = Global.getInt(getContentResolver(), Global.LOW_POWER_MODE, 0) != 0;
|
final boolean mode = mPowerManager.isPowerSaveMode();
|
||||||
if (checked == mSwitch.isChecked()) return;
|
if (DEBUG) Log.d(TAG, "updateSwitch: isChecked=" + mSwitch.isChecked() + " mode=" + mode);
|
||||||
|
if (mode == mSwitch.isChecked()) return;
|
||||||
|
|
||||||
// set listener to null so that that code below doesn't trigger onCheckedChanged()
|
// set listener to null so that that code below doesn't trigger onCheckedChanged()
|
||||||
if (mValidListener) {
|
if (mValidListener) {
|
||||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||||
}
|
}
|
||||||
mSwitch.setChecked(checked);
|
mSwitch.setChecked(mode);
|
||||||
if (mValidListener) {
|
if (mValidListener) {
|
||||||
mSwitchBar.addOnSwitchChangeListener(this);
|
mSwitchBar.addOnSwitchChangeListener(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final Runnable mUpdateSwitch = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
updateSwitch();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private final Runnable mStartMode = new Runnable() {
|
private final Runnable mStartMode = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
AsyncTask.execute(new Runnable() {
|
AsyncTask.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (DEBUG) Log.d(TAG, "Starting LOW_POWER_MODE from settings");
|
if (DEBUG) Log.d(TAG, "Starting low power mode from settings");
|
||||||
Global.putInt(getContentResolver(), Global.LOW_POWER_MODE, 1);
|
trySetPowerSaveMode(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private final class Receiver extends BroadcastReceiver {
|
||||||
|
private boolean mRegistered;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
if (DEBUG) Log.d(TAG, "Received " + intent.getAction());
|
||||||
|
mHandler.post(mUpdateSwitch);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListening(boolean listening) {
|
||||||
|
if (listening && !mRegistered) {
|
||||||
|
mContext.registerReceiver(this, new IntentFilter(ACTION_POWER_SAVE_MODE_CHANGING));
|
||||||
|
mRegistered = true;
|
||||||
|
} else if (!listening && mRegistered) {
|
||||||
|
mContext.unregisterReceiver(this);
|
||||||
|
mRegistered = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final class SettingsObserver extends ContentObserver {
|
private final class SettingsObserver extends ContentObserver {
|
||||||
private final Uri LOW_POWER_MODE_URI = Global.getUriFor(Global.LOW_POWER_MODE);
|
|
||||||
private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
|
private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
|
||||||
= Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
|
= Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
|
||||||
|
|
||||||
@@ -153,9 +200,6 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange, Uri uri) {
|
public void onChange(boolean selfChange, Uri uri) {
|
||||||
if (LOW_POWER_MODE_URI.equals(uri)) {
|
|
||||||
updateSwitch();
|
|
||||||
}
|
|
||||||
if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
|
if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
|
||||||
mTriggerPref.update(mContext);
|
mTriggerPref.update(mContext);
|
||||||
}
|
}
|
||||||
@@ -164,7 +208,6 @@ public class BatterySaverSettings extends SettingsPreferenceFragment
|
|||||||
public void setListening(boolean listening) {
|
public void setListening(boolean listening) {
|
||||||
final ContentResolver cr = getContentResolver();
|
final ContentResolver cr = getContentResolver();
|
||||||
if (listening) {
|
if (listening) {
|
||||||
cr.registerContentObserver(LOW_POWER_MODE_URI, false, this);
|
|
||||||
cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
|
cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
|
||||||
} else {
|
} else {
|
||||||
cr.unregisterContentObserver(this);
|
cr.unregisterContentObserver(this);
|
||||||
|
Reference in New Issue
Block a user