public class

BatteryChargingTracker

extends BroadcastReceiverConstraintTracker<java.lang.Boolean>

 java.lang.Object

androidx.work.impl.constraints.trackers.ConstraintTracker<java.lang.Object>

androidx.work.impl.constraints.trackers.BroadcastReceiverConstraintTracker<java.lang.Boolean>

↳androidx.work.impl.constraints.trackers.BatteryChargingTracker

Overview

Tracks whether or not the device's battery is charging.

Summary

Fields
from ConstraintTracker<T>mAppContext, mTaskExecutor
Constructors
publicBatteryChargingTracker(Context context, TaskExecutor taskExecutor)

Create an instance of BatteryChargingTracker.

Methods
public abstract java.lang.ObjectgetInitialState()

Determines the initial state of the constraint being tracked.

public abstract IntentFiltergetIntentFilter()

public abstract voidonBroadcastReceive(Context context, Intent intent)

Called when the BroadcastReceiver is receiving an broadcast and should handle the received .

from BroadcastReceiverConstraintTracker<T>startTracking, stopTracking
from ConstraintTracker<T>addListener, removeListener, setState
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public BatteryChargingTracker(Context context, TaskExecutor taskExecutor)

Create an instance of BatteryChargingTracker.

Parameters:

context: The application
taskExecutor: The internal TaskExecutor being used by WorkManager.

Methods

public abstract java.lang.Object getInitialState()

Determines the initial state of the constraint being tracked.

public abstract IntentFilter getIntentFilter()

Returns:

The associated with this tracker.

public abstract void onBroadcastReceive(Context context, Intent intent)

Called when the BroadcastReceiver is receiving an broadcast and should handle the received .

Parameters:

context: The in which the receiver is running.
intent: The being received.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.work.impl.constraints.trackers;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.work.Logger;
import androidx.work.impl.utils.taskexecutor.TaskExecutor;

/**
 * Tracks whether or not the device's battery is charging.
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> {

    private static final String TAG = Logger.tagWithPrefix("BatteryChrgTracker");

    /**
     * Create an instance of {@link BatteryChargingTracker}.
     * @param context The application {@link Context}
     * @param taskExecutor The internal {@link TaskExecutor} being used by WorkManager.
     */
    public BatteryChargingTracker(@NonNull Context context, @NonNull TaskExecutor taskExecutor) {
        super(context, taskExecutor);
    }

    @Override
    public Boolean getInitialState() {
        // {@link ACTION_CHARGING} and {@link ACTION_DISCHARGING} are not sticky broadcasts, so
        // we use {@link ACTION_BATTERY_CHANGED} on all APIs to get the initial state.
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent intent = mAppContext.registerReceiver(null, intentFilter);
        if (intent == null) {
            Logger.get().error(TAG, "getInitialState - null intent received");
            return null;
        }
        return isBatteryChangedIntentCharging(intent);
    }

    @Override
    public IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        if (Build.VERSION.SDK_INT >= 23) {
            intentFilter.addAction(BatteryManager.ACTION_CHARGING);
            intentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
        } else {
            intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
            intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        }
        return intentFilter;
    }

    @Override
    public void onBroadcastReceive(Context context, @NonNull Intent intent) {
        String action = intent.getAction();
        if (action == null) {
            return;
        }

        Logger.get().debug(TAG, String.format("Received %s", action));
        switch (action) {
            case BatteryManager.ACTION_CHARGING:
                setState(true);
                break;

            case BatteryManager.ACTION_DISCHARGING:
                setState(false);
                break;

            case Intent.ACTION_POWER_CONNECTED:
                setState(true);
                break;

            case Intent.ACTION_POWER_DISCONNECTED:
                setState(false);
                break;
        }
    }

    private boolean isBatteryChangedIntentCharging(Intent intent) {
        boolean charging;
        if (Build.VERSION.SDK_INT >= 23) {
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            charging = (status == BatteryManager.BATTERY_STATUS_CHARGING
                    || status == BatteryManager.BATTERY_STATUS_FULL);
        } else {
            int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            charging = (chargePlug != 0);
        }
        return charging;
    }
}