public class

StorageNotLowTracker

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.StorageNotLowTracker

Overview

Tracks whether or not the device's storage is low.

Summary

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

Create an instance of StorageNotLowTracker.

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 StorageNotLowTracker(Context context, TaskExecutor taskExecutor)

Create an instance of StorageNotLowTracker.

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 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 storage is low.
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class StorageNotLowTracker extends BroadcastReceiverConstraintTracker<Boolean> {

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

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

    @Override
    public Boolean getInitialState() {
        Intent intent = mAppContext.registerReceiver(null, getIntentFilter());
        if (intent == null || intent.getAction() == null) {
            // ACTION_DEVICE_STORAGE_LOW is a sticky broadcast that is removed when sufficient
            // storage is available again.  ACTION_DEVICE_STORAGE_OK is not sticky.  So if we
            // don't receive anything here, we can assume that the storage state is okay.
            return true;
        } else {
            switch (intent.getAction()) {
                case Intent.ACTION_DEVICE_STORAGE_OK:
                    return true;

                case Intent.ACTION_DEVICE_STORAGE_LOW:
                    return false;

                default:
                    // This should never happen because the intent filter is configured
                    // correctly.
                    return null;
            }
        }
    }

    @Override
    public IntentFilter getIntentFilter() {
        // In API 26+, DEVICE_STORAGE_OK/LOW are deprecated and are no longer sent to
        // manifest-defined BroadcastReceivers. Since we are registering our receiver manually, this
        // is currently okay. This may change in future versions, so this will need to be monitored.
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
        intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
        return intentFilter;
    }

    @Override
    public void onBroadcastReceive(Context context, @NonNull Intent intent) {
        if (intent.getAction() == null) {
            return; // Should never happen since the IntentFilter was configured.
        }

        Logger.get().debug(TAG, String.format("Received %s", intent.getAction()));

        switch (intent.getAction()) {
            case Intent.ACTION_DEVICE_STORAGE_OK:
                setState(true);
                break;

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