/*
 * 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 android.arch.background.workmanager.constraints.trackers;

import android.arch.background.workmanager.constraints.listeners.StorageNotLowListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

/**
 * A {@link BroadcastReceiver} for storage level.
 */

public class StorageNotLowTracker extends ConstraintTracker<StorageNotLowListener> {

    private static final String TAG = "StorageNotLowTracker";

    @VisibleForTesting
    Boolean mIsStorageNotLow;

    public StorageNotLowTracker(Context context) {
        super(context);
    }

    @Override
    public void setUpInitialState(StorageNotLowListener listener) {
        if (mIsStorageNotLow == null) {
            Intent intent = mAppContext.registerReceiver(null, getIntentFilter());
            if (intent == null || intent.getAction() == null) {
                return;
            }

            switch (intent.getAction()) {
                case Intent.ACTION_DEVICE_STORAGE_OK:
                    mIsStorageNotLow = true;
                    break;
                case Intent.ACTION_DEVICE_STORAGE_LOW:
                    mIsStorageNotLow = false;
                    break;
            }

            Log.d(TAG, "Setting initial mIsStorageNotLow to " + mIsStorageNotLow);
        }

        if (mIsStorageNotLow != null) {
            listener.setStorageNotLow(mIsStorageNotLow);
        }
    }

    @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 onReceive(Context context, Intent intent) {
        if (intent == null || intent.getAction() == null) {
            return; // Should never happen since the IntentFilter was configured.
        }

        Log.d(TAG, "Received " + intent.getAction());

        switch (intent.getAction()) {
            case Intent.ACTION_DEVICE_STORAGE_OK:
                setIsStorageNotLowAndNotify(true);
                break;
            case Intent.ACTION_DEVICE_STORAGE_LOW:
                setIsStorageNotLowAndNotify(false);
                break;
        }
    }

    private void setIsStorageNotLowAndNotify(boolean isStorageNotLow) {
        if (mIsStorageNotLow == null || mIsStorageNotLow != isStorageNotLow) {
            Log.d(TAG, "Setting mIsStorageNotLow to " + isStorageNotLow);
            mIsStorageNotLow = isStorageNotLow;
            for (StorageNotLowListener listener : mListeners) {
                listener.setStorageNotLow(mIsStorageNotLow);
            }
        }
    }
}

@@ -1447,32 +1447,33 @@
 tLow == null) %7B%0A
+%0A
             Inte
@@ -1619,22 +1619,330 @@
         
-return
+// ACTION_DEVICE_STORAGE_LOW is a sticky broadcast that is removed when sufficient%0A                // storage is available again.  ACTION_DEVICE_STORAGE_OK is not sticky.  So if we%0A                // don't receive anything here, we can assume that the storage state is okay.%0A                mIsStorageNotLow = true
 ;%0A      
@@ -1940,34 +1940,44 @@
 e;%0A            %7D
-%0A%0A
+ else %7B%0A    
             swit
@@ -2010,32 +2010,36 @@
                 
+   
+ 
 case Intent.ACTI
@@ -2072,32 +2072,36 @@
                 
+    
 mIsStorageNotLow
@@ -2121,39 +2121,48 @@
                 
+ 
+   
 break;%0A
+%0A    
                 
@@ -2212,32 +2212,36 @@
                 
+   
+ 
 mIsStorageNotLow
@@ -2262,32 +2262,36 @@
                 
+    
 break;%0A         
@@ -2285,34 +2285,51 @@
 ak;%0A            
+    %7D%0A            
 %7D
-%0A
 %0A            Log
@@ -3526,32 +3526,33 @@
          break;%0A
+%0A
             case

Fix logic for StorageNotLowTracker.