FileDocCategorySizeDatePackage
BatteryController.javaAPI DocAndroid 5.1 API4754Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

BatteryController

public class BatteryController extends android.content.BroadcastReceiver

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private final ArrayList
mChangeCallbacks
private final android.os.PowerManager
mPowerManager
private int
mLevel
private boolean
mPluggedIn
private boolean
mCharging
private boolean
mCharged
private boolean
mPowerSave
Constructors Summary
public BatteryController(android.content.Context context)


       
        mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
        filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
        context.registerReceiver(this, filter);

        updatePowerSave();
    
Methods Summary
public voidaddStateChangedCallback(com.android.systemui.statusbar.policy.BatteryController$BatteryStateChangeCallback cb)

        mChangeCallbacks.add(cb);
        cb.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
    
public voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw, java.lang.String[] args)

        pw.println("BatteryController state:");
        pw.print("  mLevel="); pw.println(mLevel);
        pw.print("  mPluggedIn="); pw.println(mPluggedIn);
        pw.print("  mCharging="); pw.println(mCharging);
        pw.print("  mCharged="); pw.println(mCharged);
        pw.print("  mPowerSave="); pw.println(mPowerSave);
    
private voidfireBatteryLevelChanged()

        final int N = mChangeCallbacks.size();
        for (int i = 0; i < N; i++) {
            mChangeCallbacks.get(i).onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
        }
    
private voidfirePowerSaveChanged()

        final int N = mChangeCallbacks.size();
        for (int i = 0; i < N; i++) {
            mChangeCallbacks.get(i).onPowerSaveChanged();
        }
    
public booleanisPowerSave()

        return mPowerSave;
    
public voidonReceive(android.content.Context context, android.content.Intent intent)

        final String action = intent.getAction();
        if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
            mLevel = (int)(100f
                    * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                    / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
            mPluggedIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;

            final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
                    BatteryManager.BATTERY_STATUS_UNKNOWN);
            mCharged = status == BatteryManager.BATTERY_STATUS_FULL;
            mCharging = mCharged || status == BatteryManager.BATTERY_STATUS_CHARGING;

            fireBatteryLevelChanged();
        } else if (action.equals(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)) {
            updatePowerSave();
        } else if (action.equals(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING)) {
            setPowerSave(intent.getBooleanExtra(PowerManager.EXTRA_POWER_SAVE_MODE, false));
        }
    
public voidremoveStateChangedCallback(com.android.systemui.statusbar.policy.BatteryController$BatteryStateChangeCallback cb)

        mChangeCallbacks.remove(cb);
    
private voidsetPowerSave(boolean powerSave)

        if (powerSave == mPowerSave) return;
        mPowerSave = powerSave;
        if (DEBUG) Log.d(TAG, "Power save is " + (mPowerSave ? "on" : "off"));
        firePowerSaveChanged();
    
private voidupdatePowerSave()

        setPowerSave(mPowerManager.isPowerSaveMode());