1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.mail.utils; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.IntentFilter; 21 22 /** 23 * Holds the storage state of the system. Allows for registering a single handler which can 24 * adjust application state to deal with STORAGE_LOW and STORAGE_OK state: 25 * Reference: {@link Intent#ACTION_DEVICE_STORAGE_LOW}, {@link Intent#ACTION_DEVICE_STORAGE_OK} 26 */ 27 public final class StorageLowState { 28 /** 29 * Methods that are called when a device enters/leaves storage low mode. 30 */ 31 public interface LowStorageHandler { 32 /** 33 * Method to be called when the device enters storage low mode. 34 */ onStorageLow()35 void onStorageLow(); 36 37 /** 38 * Method to be run when the device recovers from storage low mode. 39 */ onStorageOk()40 void onStorageOk(); 41 } 42 /** True if the system has entered STORAGE_LOW state. */ 43 private static boolean sIsStorageLow = false; 44 /** If non-null, this represents a handler that is notified of changes to state. */ 45 private static LowStorageHandler sHandler = null; 46 47 /** Private constructor to avoid class instantiation. */ StorageLowState()48 private StorageLowState() { 49 // Do nothing. 50 } 51 52 /** 53 * Checks if the device is in storage low state. If the state changes, the handler is notified 54 * of it. The handler is not notified if the state remains the same as before. 55 */ checkStorageLowMode(Context context)56 public static void checkStorageLowMode(Context context) { 57 // Identify if we are in low storage mode. This works because storage low is a sticky 58 // intent, so we are guaranteed a non-null intent if that broadcast was sent and not 59 // cleared subsequently. 60 final IntentFilter filter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); 61 final Intent result = context.registerReceiver(null, filter); 62 setIsStorageLow(result != null); 63 } 64 65 /** 66 * Notifies {@link StorageLowState} that the device has entered storage low state. 67 */ setIsStorageLow(boolean newValue)68 public static void setIsStorageLow(boolean newValue) { 69 if (sIsStorageLow == newValue) { 70 // The state is unchanged, nothing to do. 71 return; 72 } 73 sIsStorageLow = newValue; 74 if (sHandler == null) { 75 return; 76 } 77 if (newValue) { 78 sHandler.onStorageLow(); 79 } else { 80 sHandler.onStorageOk(); 81 } 82 } 83 84 /** 85 * Sets the handler that can adjust application state to deal with storage low and 86 * storage ok intents. 87 * Reference: {@link Intent#ACTION_DEVICE_STORAGE_LOW}, {@link Intent#ACTION_DEVICE_STORAGE_OK} 88 * @param in a handler that can deal with changes to the storage state. 89 */ registerHandler(LowStorageHandler in)90 public static void registerHandler(LowStorageHandler in) { 91 sHandler = in; 92 // If we are currently in low storage mode, let the handler deal with it immediately. 93 if (sIsStorageLow) { 94 sHandler.onStorageLow(); 95 } 96 } 97 } 98