diff options
Diffstat (limited to 'src/com/android/providers/downloads/StorageManager.java')
-rw-r--r-- | src/com/android/providers/downloads/StorageManager.java | 94 |
1 files changed, 43 insertions, 51 deletions
diff --git a/src/com/android/providers/downloads/StorageManager.java b/src/com/android/providers/downloads/StorageManager.java index 4194ec8b..41592f4a 100644 --- a/src/com/android/providers/downloads/StorageManager.java +++ b/src/com/android/providers/downloads/StorageManager.java @@ -19,7 +19,6 @@ package com.android.providers.downloads; import static com.android.providers.downloads.Constants.LOGV; import static com.android.providers.downloads.Constants.TAG; -import android.app.Activity; import android.content.ContentUris; import android.content.Context; import android.content.res.Resources; @@ -65,6 +64,9 @@ class StorageManager { /** see {@link Environment#getExternalStorageDirectory()} */ private final File mExternalStorageDir; + /** list of secondary external storage paths */ + private final List<String> mSecondaryStoragePaths; + /** see {@link Environment#getDownloadCacheDirectory()} */ private final File mSystemCacheDir; @@ -79,20 +81,14 @@ class StorageManager { /** misc members */ private final Context mContext; - private static String sdCardStorageDir; - private static final String LOGTAG = "StorageManager"; public StorageManager(Context context) { mContext = context; mDownloadDataDir = getDownloadDataDirectory(context); mExternalStorageDir = Environment.getExternalStorageDirectory(); mSystemCacheDir = Environment.getDownloadCacheDirectory(); + mSecondaryStoragePaths = getSecondaryStoragePaths(context); startThreadToCleanupDatabaseAndPurgeFileSystem(); - if (isSecondStorageSupported(context)) { - sdCardStorageDir = getExternalStorageDirectory(context); - } else { - sdCardStorageDir = null; - } } /** How often should database and filesystem be cleaned up to remove spurious files @@ -125,20 +121,20 @@ class StorageManager { mCleanupThread.start(); } - void verifySpaceBeforeWritingToFile(Context context, int destination, String path, long length) + void verifySpaceBeforeWritingToFile(int destination, String path, long length) throws StopRequestException { // do this check only once for every 1MB of downloaded data if (incrementBytesDownloadedSinceLastCheckOnSpace(length) < FREQUENCY_OF_CHECKS_ON_SPACE_AVAILABILITY) { return; } - verifySpace(context, destination, path, length); + verifySpace(destination, path, length); } - void verifySpace(Context context, int destination, String path, - long length) throws StopRequestException { + void verifySpace(int destination, String path, long length) throws StopRequestException { resetBytesDownloadedSinceLastCheckOnSpace(); File dir = null; + boolean isSecondaryStorage = false; if (Constants.LOGV) { Log.i(Constants.TAG, "in verifySpace, destination: " + destination + ", path: " + path + ", length: " + length); @@ -159,14 +155,25 @@ class StorageManager { dir = mSystemCacheDir; break; case Downloads.Impl.DESTINATION_FILE_URI: - if (isSecondStorageSupported(context) && path.startsWith(sdCardStorageDir)) { - dir = new File(sdCardStorageDir); - } else if (path.startsWith(mExternalStorageDir.getPath())) { + if (path.startsWith(mExternalStorageDir.getPath())) { dir = mExternalStorageDir; - } else if (path.startsWith(mDownloadDataDir.getPath())) { - dir = mDownloadDataDir; - } else if (path.startsWith(mSystemCacheDir.getPath())) { - dir = mSystemCacheDir; + } else { + for (String secondaryPath : mSecondaryStoragePaths) { + if (path.startsWith(secondaryPath)) { + isSecondaryStorage = true; + dir = new File(secondaryPath); + break; + } + } + } + // TODO - should only check external storage? + if (dir == null) { + if (path.startsWith(mDownloadDataDir.getPath())) { + dir = mDownloadDataDir; + } + else if (path.startsWith(mSystemCacheDir.getPath())) { + dir = mSystemCacheDir; + } } break; } @@ -174,7 +181,7 @@ class StorageManager { throw new IllegalStateException("invalid combination of destination: " + destination + ", path: " + path); } - findSpace(context, dir, length, destination); + findSpace(dir, length, destination, isSecondaryStorage); } /** @@ -182,18 +189,20 @@ class StorageManager { * specified by the input param(targetBytes). * returns true if found. false otherwise. */ - private synchronized void findSpace(Context context, File root, long targetBytes, - int destination) throws StopRequestException { + private synchronized void findSpace(File root, long targetBytes, int destination, + boolean isSecondaryStorage) throws StopRequestException { if (targetBytes == 0) { return; } - if (!(isSecondStorageSupported(context) && root.getPath().startsWith(sdCardStorageDir))) { - if (destination == Downloads.Impl.DESTINATION_FILE_URI || - destination == Downloads.Impl.DESTINATION_EXTERNAL) { - if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { - throw new StopRequestException(Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR, - "external media not mounted"); - } + if (root == mExternalStorageDir) { + if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + throw new StopRequestException(Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR, + "external media not mounted"); + } + } else if (isSecondaryStorage) { + if (!Environment.getStorageState(root).equals(Environment.MEDIA_MOUNTED)) { + throw new StopRequestException(Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR, + "external media not mounted"); } } // is there enough space in the file system of the given param 'root'. @@ -484,35 +493,18 @@ class StorageManager { mBytesDownloadedSinceLastCheckOnSpace = 0; } - /** - * Whether the device supports second storage - * @return boolean true support second storage, false does not - */ - public static boolean isSecondStorageSupported(Context context) { + public static List<String> getSecondaryStoragePaths(Context context) { + List<String> secondaryPaths = new ArrayList<String>(); android.os.storage.StorageManager storageManager = (android.os.storage.StorageManager) context .getSystemService(Context.STORAGE_SERVICE); StorageVolume[] volumes = storageManager.getVolumeList(); for (int i = 0; i < volumes.length; i++) { - if (!volumes[i].isPrimary() && volumes[i].allowMassStorage()) { - return true; + if (!volumes[i].isPrimary()) { + secondaryPaths.add(volumes[i].getPath()); } } - return false; + return secondaryPaths; } - public static String getExternalStorageDirectory(Context context) { - String sdCardDir = null; - android.os.storage.StorageManager storageManager = - (android.os.storage.StorageManager) context - .getSystemService(Context.STORAGE_SERVICE); - StorageVolume[] volumes = storageManager.getVolumeList(); - for (int i = 0; i < volumes.length; i++) { - if (volumes[i].isRemovable() && volumes[i].allowMassStorage()) { - sdCardDir = volumes[i].getPath(); - break; - } - } - return sdCardDir; - } } |