FileDocCategorySizeDatePackage
BatteryController.javaAPI DocAndroid 5.1 API10294Thu Mar 12 22:22:42 GMT 2015com.android.server.job.controllers

BatteryController

public class BatteryController extends StateController
Simple controller that tracks whether the phone is charging or not. The phone is considered to be charging when it's been plugged in for more than two minutes, and the system has broadcast ACTION_BATTERY_OK.

Fields Summary
private static final String
TAG
private static final Object
sCreationLock
private static volatile BatteryController
sController
private static final String
ACTION_CHARGING_STABLE
private static final long
STABLE_CHARGING_THRESHOLD_MILLIS
Wait this long after phone is plugged in before doing any work.
private List
mTrackedTasks
private ChargingTracker
mChargeTracker
Constructors Summary
private BatteryController(com.android.server.job.StateChangedListener stateChangedListener, android.content.Context context)

        super(stateChangedListener, context);
        mChargeTracker = new ChargingTracker();
        mChargeTracker.startTracking();
    
Methods Summary
public voiddumpControllerState(java.io.PrintWriter pw)

        pw.println("Batt.");
        pw.println("Stable power: " + mChargeTracker.isOnStablePower());
        synchronized (mTrackedTasks) {
            Iterator<JobStatus> it = mTrackedTasks.iterator();
            if (it.hasNext()) {
                pw.print(String.valueOf(it.next().hashCode()));
            }
            while (it.hasNext()) {
                pw.print("," + String.valueOf(it.next().hashCode()));
            }
            pw.println();
        }
    
public static com.android.server.job.controllers.BatteryControllerget(com.android.server.job.JobSchedulerService taskManagerService)


         
        synchronized (sCreationLock) {
            if (sController == null) {
                sController = new BatteryController(taskManagerService,
                        taskManagerService.getContext());
            }
        }
        return sController;
    
public static com.android.server.job.controllers.BatteryControllergetForTesting(com.android.server.job.StateChangedListener stateChangedListener, android.content.Context context)

        return new BatteryController(stateChangedListener, context);
    
public com.android.server.job.controllers.BatteryController$ChargingTrackergetTracker()

        return mChargeTracker;
    
private voidmaybeReportNewChargingState()

        final boolean stablePower = mChargeTracker.isOnStablePower();
        if (DEBUG) {
            Slog.d(TAG, "maybeReportNewChargingState: " + stablePower);
        }
        boolean reportChange = false;
        synchronized (mTrackedTasks) {
            for (JobStatus ts : mTrackedTasks) {
                boolean previous = ts.chargingConstraintSatisfied.getAndSet(stablePower);
                if (previous != stablePower) {
                    reportChange = true;
                }
            }
        }
        // Let the scheduler know that state has changed. This may or may not result in an
        // execution.
        if (reportChange) {
            mStateChangedListener.onControllerStateChanged();
        }
        // Also tell the scheduler that any ready jobs should be flushed.
        if (stablePower) {
            mStateChangedListener.onRunJobNow(null);
        }
    
public voidmaybeStartTrackingJob(JobStatus taskStatus)

        final boolean isOnStablePower = mChargeTracker.isOnStablePower();
        if (taskStatus.hasChargingConstraint()) {
            synchronized (mTrackedTasks) {
                mTrackedTasks.add(taskStatus);
                taskStatus.chargingConstraintSatisfied.set(isOnStablePower);
            }
        }
        if (isOnStablePower) {
            mChargeTracker.setStableChargingAlarm();
        }
    
public voidmaybeStopTrackingJob(JobStatus taskStatus)

        if (taskStatus.hasChargingConstraint()) {
            synchronized (mTrackedTasks) {
                mTrackedTasks.remove(taskStatus);
            }
        }