Java Code Examples for android.os.Environment#MEDIA_REMOVED

The following examples show how to use android.os.Environment#MEDIA_REMOVED . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public StorageVolume[] getVolumeList(int uid, String packageName, int flags) {
    final int userId = UserHandle.getUserId(uid);

    final boolean forWrite = (flags & StorageManager.FLAG_FOR_WRITE) != 0;
    final boolean realState = (flags & StorageManager.FLAG_REAL_STATE) != 0;
    final boolean includeInvisible = (flags & StorageManager.FLAG_INCLUDE_INVISIBLE) != 0;

    final boolean userKeyUnlocked;
    final boolean storagePermission;
    final long token = Binder.clearCallingIdentity();
    try {
        userKeyUnlocked = isUserKeyUnlocked(userId);
        storagePermission = mStorageManagerInternal.hasExternalStorage(uid, packageName);
    } finally {
        Binder.restoreCallingIdentity(token);
    }

    boolean foundPrimary = false;

    final ArrayList<StorageVolume> res = new ArrayList<>();
    synchronized (mLock) {
        for (int i = 0; i < mVolumes.size(); i++) {
            final VolumeInfo vol = mVolumes.valueAt(i);
            switch (vol.getType()) {
                case VolumeInfo.TYPE_PUBLIC:
                case VolumeInfo.TYPE_EMULATED:
                    break;
                default:
                    continue;
            }

            boolean match = false;
            if (forWrite) {
                match = vol.isVisibleForWrite(userId);
            } else {
                match = vol.isVisibleForRead(userId)
                        || (includeInvisible && vol.getPath() != null);
            }
            if (!match) continue;

            boolean reportUnmounted = false;
            if ((vol.getType() == VolumeInfo.TYPE_EMULATED) && !userKeyUnlocked) {
                reportUnmounted = true;
            } else if (!storagePermission && !realState) {
                reportUnmounted = true;
            }

            final StorageVolume userVol = vol.buildStorageVolume(mContext, userId,
                    reportUnmounted);
            if (vol.isPrimary()) {
                res.add(0, userVol);
                foundPrimary = true;
            } else {
                res.add(userVol);
            }
        }
    }

    if (!foundPrimary) {
        Log.w(TAG, "No primary storage defined yet; hacking together a stub");

        final boolean primaryPhysical = SystemProperties.getBoolean(
                StorageManager.PROP_PRIMARY_PHYSICAL, false);

        final String id = "stub_primary";
        final File path = Environment.getLegacyExternalStorageDirectory();
        final String description = mContext.getString(android.R.string.unknownName);
        final boolean primary = true;
        final boolean removable = primaryPhysical;
        final boolean emulated = !primaryPhysical;
        final boolean allowMassStorage = false;
        final long maxFileSize = 0L;
        final UserHandle owner = new UserHandle(userId);
        final String uuid = null;
        final String state = Environment.MEDIA_REMOVED;

        res.add(0, new StorageVolume(id, path, path,
                description, primary, removable, emulated,
                allowMassStorage, maxFileSize, owner, uuid, state));
    }

    return res.toArray(new StorageVolume[res.size()]);
}
 
Example 2
private static File prepareTargetDirectory() throws DownloadFailException, IOException {
    File downloadFolder =
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    if (
            downloadFolder
                    != null
                    && (downloadFolder.exists() || downloadFolder.mkdir() || downloadFolder.createNewFile())
                    && downloadFolder.canWrite()
    ) {
        return downloadFolder;
    }

    File externalStorage = Environment.getExternalStorageDirectory();
    String externalStorageState = Environment.getExternalStorageState();
    if (
            externalStorage
                    != null
                    && (externalStorage.exists() || externalStorage.mkdir() || externalStorage.createNewFile())
                    && externalStorage.canWrite()
                    && externalStorageState.equals(Environment.MEDIA_MOUNTED)
    ) {
        return new File(externalStorage, "Download");
    }

    File appExternal = LMvdApp.getInstance().getExternalFilesDir(null);
    if (
            appExternal
                    != null
                    && (appExternal.exists() || appExternal.mkdir() || appExternal.createNewFile())
                    && appExternal.canWrite()
    ) {
        return new File(appExternal, "Download");
    }

    String message;
    switch (externalStorageState) {
        case Environment.MEDIA_UNMOUNTABLE:
            message = "External storage is un-mountable.";
            break;
        case Environment.MEDIA_SHARED:
            message = "USB mass storage is turned on. Can not mount external storage.";
            break;
        case Environment.MEDIA_UNMOUNTED:
            message = "External storage is not mounted.";
            break;
        case Environment.MEDIA_MOUNTED_READ_ONLY:
            message = "External storage is mounted but has no write access.";
            break;
        case Environment.MEDIA_BAD_REMOVAL:
            message = "External storage was removed without being properly ejected.";
            break;
        case Environment.MEDIA_REMOVED:
            message = "External storage does not exist. Probably removed.";
            break;
        case Environment.MEDIA_NOFS:
            message = "External storage is blank or has unsupported filesystem.";
            break;
        case Environment.MEDIA_CHECKING:
            message = "Still checking for external storage.";
            break;
        case Environment.MEDIA_EJECTING:
            message = "External storage is currently being ejected.";
            break;
        case Environment.MEDIA_UNKNOWN:
            message = "External storage is not available for some unknown reason.";
            break;
        case Environment.MEDIA_MOUNTED:
            message = "External storage is mounted but for some unknown reason is not" +
                    " available.";
            break;
        default:
            message = "External storage is not available. No reason.";
    }
    throw new DownloadFailException(message);
}