am 7d9bfc9b: Merge "Handle external storage errors uniformly." into jb-mr2-dev

* commit '7d9bfc9b8291aa4ec997ecb4002f4c411aa177ac':
  Handle external storage errors uniformly.
This commit is contained in:
Jeff Sharkey
2013-03-26 00:39:54 +00:00
committed by Android Git Automerger

View File

@@ -461,39 +461,63 @@ public class DownloadManager {
} }
/** /**
* Set the local destination for the downloaded file to a path within the application's * Set the local destination for the downloaded file to a path within
* external files directory (as returned by {@link Context#getExternalFilesDir(String)}. * the application's external files directory (as returned by
* {@link Context#getExternalFilesDir(String)}.
* <p> * <p>
* The downloaded file is not scanned by MediaScanner. * The downloaded file is not scanned by MediaScanner. But it can be
* But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. * made scannable by calling {@link #allowScanningByMediaScanner()}.
* *
* @param context the {@link Context} to use in determining the external files directory * @param context the {@link Context} to use in determining the external
* @param dirType the directory type to pass to {@link Context#getExternalFilesDir(String)} * files directory
* @param subPath the path within the external directory, including the destination filename * @param dirType the directory type to pass to
* {@link Context#getExternalFilesDir(String)}
* @param subPath the path within the external directory, including the
* destination filename
* @return this object * @return this object
* @throws IllegalStateException If the external storage directory
* cannot be found or created.
*/ */
public Request setDestinationInExternalFilesDir(Context context, String dirType, public Request setDestinationInExternalFilesDir(Context context, String dirType,
String subPath) { String subPath) {
setDestinationFromBase(context.getExternalFilesDir(dirType), subPath); final File file = context.getExternalFilesDir(dirType);
if (file == null) {
throw new IllegalStateException("Failed to get external storage files directory");
} else if (file.exists()) {
if (!file.isDirectory()) {
throw new IllegalStateException(file.getAbsolutePath() +
" already exists and is not a directory");
}
} else {
if (!file.mkdirs()) {
throw new IllegalStateException("Unable to create directory: "+
file.getAbsolutePath());
}
}
setDestinationFromBase(file, subPath);
return this; return this;
} }
/** /**
* Set the local destination for the downloaded file to a path within the public external * Set the local destination for the downloaded file to a path within
* storage directory (as returned by * the public external storage directory (as returned by
* {@link Environment#getExternalStoragePublicDirectory(String)}. * {@link Environment#getExternalStoragePublicDirectory(String)}).
*<p> * <p>
* The downloaded file is not scanned by MediaScanner. * The downloaded file is not scanned by MediaScanner. But it can be
* But it can be made scannable by calling {@link #allowScanningByMediaScanner()}. * made scannable by calling {@link #allowScanningByMediaScanner()}.
* *
* @param dirType the directory type to pass to * @param dirType the directory type to pass to {@link Environment#getExternalStoragePublicDirectory(String)}
* {@link Environment#getExternalStoragePublicDirectory(String)} * @param subPath the path within the external directory, including the
* @param subPath the path within the external directory, including the destination filename * destination filename
* @return this object * @return this object
* @throws IllegalStateException If the external storage directory
* cannot be found or created.
*/ */
public Request setDestinationInExternalPublicDir(String dirType, String subPath) { public Request setDestinationInExternalPublicDir(String dirType, String subPath) {
File file = Environment.getExternalStoragePublicDirectory(dirType); File file = Environment.getExternalStoragePublicDirectory(dirType);
if (file.exists()) { if (file == null) {
throw new IllegalStateException("Failed to get external storage public directory");
} else if (file.exists()) {
if (!file.isDirectory()) { if (!file.isDirectory()) {
throw new IllegalStateException(file.getAbsolutePath() + throw new IllegalStateException(file.getAbsolutePath() +
" already exists and is not a directory"); " already exists and is not a directory");