From d0c5a7169ba60b65fd91158a1310a8443118227c Mon Sep 17 00:00:00 2001 From: Adnan Date: Wed, 27 Aug 2014 17:13:28 -0700 Subject: DownloadProvider: Check if the device actually supports second storage. Change-Id: I78297ca235e058aecfd0ff5a0e990fc3004b4c77 --- .../providers/downloads/DownloadProvider.java | 2 +- .../providers/downloads/DownloadThread.java | 5 +-- src/com/android/providers/downloads/Helpers.java | 6 ++-- .../providers/downloads/StorageManager.java | 37 ++++++++++++++-------- 4 files changed, 30 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java index 132deb9a..ed793aa6 100644 --- a/src/com/android/providers/downloads/DownloadProvider.java +++ b/src/com/android/providers/downloads/DownloadProvider.java @@ -713,7 +713,7 @@ public final class DownloadProvider extends ContentProvider { final String phoneStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath(); String sdCardStoragePath = null; - if (StorageManager.isSecondStorageSupported()) { + if (StorageManager.isSecondStorageSupported(getContext())) { sdCardStoragePath = StorageManager.getExternalStorageDirectory(getContext()); } if (!path.startsWith(phoneStoragePath) diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index 88cd9162..517d8f8f 100755 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -583,7 +583,7 @@ public class DownloadThread implements Runnable { */ private void writeDataToDestination(State state, byte[] data, int bytesRead, OutputStream out) throws StopRequestException { - mStorageManager.verifySpaceBeforeWritingToFile( + mStorageManager.verifySpaceBeforeWritingToFile(mContext, mInfo.mDestination, state.mFilename, bytesRead); boolean forceVerified = false; @@ -595,7 +595,8 @@ public class DownloadThread implements Runnable { // TODO: better differentiate between DRM and disk failures if (!forceVerified) { // couldn't write to file. are we out of space? check. - mStorageManager.verifySpace(mInfo.mDestination, state.mFilename, bytesRead); + mStorageManager.verifySpace(mContext, mInfo.mDestination, + state.mFilename, bytesRead); forceVerified = true; } else { throw new StopRequestException(Downloads.Impl.STATUS_FILE_ERROR, diff --git a/src/com/android/providers/downloads/Helpers.java b/src/com/android/providers/downloads/Helpers.java index d9b38b50..0739eea2 100644 --- a/src/com/android/providers/downloads/Helpers.java +++ b/src/com/android/providers/downloads/Helpers.java @@ -92,7 +92,7 @@ public class Helpers { path = chooseFilename(url, hint, contentDisposition, contentLocation, destination); } - storageManager.verifySpace(destination, path, contentLength); + storageManager.verifySpace(context, destination, path, contentLength); if (DownloadDrmHelper.isDrmConvertNeeded(mimeType)) { path = DownloadDrmHelper.modifyDrmFwLockFileExtension(path); } @@ -382,8 +382,8 @@ public class Helpers { return filename.startsWith(Environment.getDownloadCacheDirectory().toString()) || filename.startsWith(downloadsDataDir.toString()) || filename.startsWith(Environment.getExternalStorageDirectory().toString()) - || (StorageManager.isSecondStorageSupported() && filename.startsWith(StorageManager - .getExternalStorageDirectory(context))); + || (StorageManager.isSecondStorageSupported(context) + && filename.startsWith(StorageManager.getExternalStorageDirectory(context))); } /** diff --git a/src/com/android/providers/downloads/StorageManager.java b/src/com/android/providers/downloads/StorageManager.java index a0ceae6a..4194ec8b 100644 --- a/src/com/android/providers/downloads/StorageManager.java +++ b/src/com/android/providers/downloads/StorageManager.java @@ -88,7 +88,7 @@ class StorageManager { mExternalStorageDir = Environment.getExternalStorageDirectory(); mSystemCacheDir = Environment.getDownloadCacheDirectory(); startThreadToCleanupDatabaseAndPurgeFileSystem(); - if (isSecondStorageSupported()) { + if (isSecondStorageSupported(context)) { sdCardStorageDir = getExternalStorageDirectory(context); } else { sdCardStorageDir = null; @@ -125,17 +125,18 @@ class StorageManager { mCleanupThread.start(); } - void verifySpaceBeforeWritingToFile(int destination, String path, long length) + void verifySpaceBeforeWritingToFile(Context context, 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(destination, path, length); + verifySpace(context, destination, path, length); } - void verifySpace(int destination, String path, long length) throws StopRequestException { + void verifySpace(Context context, int destination, String path, + long length) throws StopRequestException { resetBytesDownloadedSinceLastCheckOnSpace(); File dir = null; if (Constants.LOGV) { @@ -158,7 +159,7 @@ class StorageManager { dir = mSystemCacheDir; break; case Downloads.Impl.DESTINATION_FILE_URI: - if (isSecondStorageSupported() && path.startsWith(sdCardStorageDir)) { + if (isSecondStorageSupported(context) && path.startsWith(sdCardStorageDir)) { dir = new File(sdCardStorageDir); } else if (path.startsWith(mExternalStorageDir.getPath())) { dir = mExternalStorageDir; @@ -173,7 +174,7 @@ class StorageManager { throw new IllegalStateException("invalid combination of destination: " + destination + ", path: " + path); } - findSpace(dir, length, destination); + findSpace(context, dir, length, destination); } /** @@ -181,12 +182,12 @@ class StorageManager { * specified by the input param(targetBytes). * returns true if found. false otherwise. */ - private synchronized void findSpace(File root, long targetBytes, int destination) - throws StopRequestException { + private synchronized void findSpace(Context context, File root, long targetBytes, + int destination) throws StopRequestException { if (targetBytes == 0) { return; } - if (!(isSecondStorageSupported() && root.getPath().startsWith(sdCardStorageDir))) { + 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)) { @@ -484,12 +485,20 @@ class StorageManager { } /** - * Whether support Second Storage - * - * @return boolean true support Second Storage, false will be not + * Whether the device supports second storage + * @return boolean true support second storage, false does not */ - public static boolean isSecondStorageSupported() { - return true; + public static boolean isSecondStorageSupported(Context context) { + 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; + } + } + return false; } public static String getExternalStorageDirectory(Context context) { -- cgit v1.2.3