Created
October 24, 2017 07:29
-
-
Save m7mdra/24db450785b614c82a0ce6515e0d853e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comtas.com.zoolvibs.helper; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.util.Log; | |
/** | |
* Holds the storage state of the system. Allows for registering a single handler which can | |
* adjust application state to deal with STORAGE_LOW and STORAGE_OK state: | |
* Reference: {@link Intent#ACTION_DEVICE_STORAGE_LOW}, {@link Intent#ACTION_DEVICE_STORAGE_OK} | |
*/ | |
public final class StorageLowState { | |
/** | |
* Methods that are called when a device enters/leaves storage low mode. | |
*/ | |
public interface LowStorageHandler { | |
/** | |
* Method to be called when the device enters storage low mode. | |
*/ | |
void onStorageLow(); | |
/** | |
* Method to be run when the device recovers from storage low mode. | |
*/ | |
void onStorageOk(); | |
} | |
/** | |
* True if the system has entered STORAGE_LOW state. | |
*/ | |
// private static boolean sIsStorageLow = false; | |
/** | |
* If non-null, this represents a handler that is notified of changes to state. | |
*/ | |
private static LowStorageHandler sHandler = null; | |
/** | |
* Private constructor to avoid class instantiation. | |
*/ | |
private StorageLowState() { | |
// Do nothing. | |
} | |
/** | |
* Checks if the device is in storage low state. If the state changes, the handler is notified | |
* of it. The handler is not notified if the state remains the same as before. | |
*/ | |
public static void checkStorageLowMode(Context context) { | |
// Identify if we are in low storage mode. This works because storage low is a sticky | |
// intent, so we are guaranteed a non-null intent if that broadcast was sent and not | |
// cleared subsequently. | |
final IntentFilter filter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); | |
context.registerReceiver(new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
setIsStorageLow(intent != null); | |
} | |
}, filter); | |
final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_OK); | |
context.registerReceiver(new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
setIsStorageLow(intent == null); | |
} | |
}, intentFilter); | |
} | |
private static final String TAG = "StorageLowState"; | |
/** | |
* Notifies {@link StorageLowState} that the device has entered storage low state. | |
*/ | |
public static void setIsStorageLow(boolean newValue) { | |
// sIsStorageLow = newValue; | |
if (sHandler == null) { | |
return; | |
} | |
if (newValue) { | |
sHandler.onStorageLow(); | |
} else { | |
sHandler.onStorageOk(); | |
} | |
} | |
/** | |
* Sets the handler that can adjust application state to deal with storage low and | |
* storage ok intents. | |
* Reference: {@link Intent#ACTION_DEVICE_STORAGE_LOW}, {@link Intent#ACTION_DEVICE_STORAGE_OK} | |
* | |
* @param in a handler that can deal with changes to the storage state. | |
*/ | |
public static void registerHandler(LowStorageHandler in) { | |
sHandler = in; | |
// If we are currently in low storage mode, let the handler deal with it immediately. | |
/* if (sIsStorageLow) | |
sHandler.onStorageLow(); | |
else sHandler.onStorageOk();*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment