summaryrefslogtreecommitdiffstats
path: root/packages/ExternalStorageProvider
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2013-09-22 12:59:40 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-09-22 12:59:40 -0700
commita440a0dd64c596991af6c860c1083c38ed4dc443 (patch)
tree172a673311c10ab57d641f19408f65f1144c1c5e /packages/ExternalStorageProvider
parentf17285c4659e859b1252f6c529018b8e79666a6d (diff)
parentb8a48da2a707720a105584f1b8dc087a6dd9c887 (diff)
downloadframeworks_base-a440a0dd64c596991af6c860c1083c38ed4dc443.zip
frameworks_base-a440a0dd64c596991af6c860c1083c38ed4dc443.tar.gz
frameworks_base-a440a0dd64c596991af6c860c1083c38ed4dc443.tar.bz2
am b8a48da2: am 5e1a5e5b: Merge "Create unique files, root ordering, UI bugs." into klp-dev
* commit 'b8a48da2a707720a105584f1b8dc087a6dd9c887': Create unique files, root ordering, UI bugs.
Diffstat (limited to 'packages/ExternalStorageProvider')
-rw-r--r--packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java67
1 files changed, 34 insertions, 33 deletions
diff --git a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
index f468abc..0ef5f56 100644
--- a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
+++ b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
@@ -96,25 +96,6 @@ public class ExternalStorageProvider extends DocumentsProvider {
throw new IllegalStateException(e);
}
- try {
- final String rootId = "documents";
- final File path = Environment.getExternalStoragePublicDirectory(
- Environment.DIRECTORY_DOCUMENTS);
- mIdToPath.put(rootId, path);
-
- final RootInfo root = new RootInfo();
- root.rootId = rootId;
- root.rootType = Root.ROOT_TYPE_SHORTCUT;
- root.flags = Root.FLAG_SUPPORTS_CREATE | Root.FLAG_LOCAL_ONLY
- | Root.FLAG_SUPPORTS_SEARCH;
- root.title = getContext().getString(R.string.root_documents);
- root.docId = getDocIdForFile(path);
- mRoots.add(root);
- mIdToRoot.put(rootId, root);
- } catch (FileNotFoundException e) {
- throw new IllegalStateException(e);
- }
-
return true;
}
@@ -230,14 +211,23 @@ public class ExternalStorageProvider extends DocumentsProvider {
public String createDocument(String docId, String mimeType, String displayName)
throws FileNotFoundException {
final File parent = getFileForDocId(docId);
- displayName = validateDisplayName(mimeType, displayName);
+ File file;
- final File file = new File(parent, displayName);
if (Document.MIME_TYPE_DIR.equals(mimeType)) {
+ file = new File(parent, displayName);
if (!file.mkdir()) {
throw new IllegalStateException("Failed to mkdir " + file);
}
} else {
+ displayName = removeExtension(mimeType, displayName);
+ file = new File(parent, addExtension(mimeType, displayName));
+
+ // If conflicting file, try adding counter suffix
+ int n = 0;
+ while (file.exists() && n++ < 32) {
+ file = new File(parent, addExtension(mimeType, displayName + " (" + n + ")"));
+ }
+
try {
if (!file.createNewFile()) {
throw new IllegalStateException("Failed to touch " + file);
@@ -354,20 +344,31 @@ public class ExternalStorageProvider extends DocumentsProvider {
return "application/octet-stream";
}
- private static String validateDisplayName(String mimeType, String displayName) {
- if (Document.MIME_TYPE_DIR.equals(mimeType)) {
- return displayName;
- } else {
- // Try appending meaningful extension if needed
- if (!mimeType.equals(getTypeForName(displayName))) {
- final String extension = MimeTypeMap.getSingleton()
- .getExtensionFromMimeType(mimeType);
- if (extension != null) {
- displayName += "." + extension;
- }
+ /**
+ * Remove file extension from name, but only if exact MIME type mapping
+ * exists. This means we can reapply the extension later.
+ */
+ private static String removeExtension(String mimeType, String name) {
+ final int lastDot = name.lastIndexOf('.');
+ if (lastDot >= 0) {
+ final String extension = name.substring(lastDot + 1);
+ final String nameMime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
+ if (mimeType.equals(nameMime)) {
+ return name.substring(0, lastDot);
}
+ }
+ return name;
+ }
- return displayName;
+ /**
+ * Add file extension to name, but only if exact MIME type mapping exists.
+ */
+ private static String addExtension(String mimeType, String name) {
+ final String extension = MimeTypeMap.getSingleton()
+ .getExtensionFromMimeType(mimeType);
+ if (extension != null) {
+ return name + "." + extension;
}
+ return name;
}
}