From b99454d07d558f310eefd306da13620dfa711f91 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 4 Jun 2012 19:09:35 -0700 Subject: [PATCH] Special logic to try and guess mime types from things normally marked as octet streams --- .../andlib/utility/AndroidUtilities.java | 17 +++++++++++++++++ .../todoroo/astrid/files/FilesControlSet.java | 17 +++++++++++++++-- .../astrid/activity/TaskEditFragment.java | 5 +---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java index f91a0de11..deb26dfc1 100644 --- a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java @@ -863,4 +863,21 @@ public class AndroidUtilities { } } + /** + * Returns the final word characters after the last '.' + * @param file + * @return + */ + @SuppressWarnings("nls") + public static String getFileExtension(String file) { + int index = file.lastIndexOf('.'); + String extension = ""; + if (index > 0) { + extension = file.substring(index + 1); + if (!extension.matches("\\w+")) + extension = ""; + } + return extension; + } + } diff --git a/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java b/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java index bc1022d29..1307c0dca 100644 --- a/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java @@ -20,6 +20,7 @@ import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; +import android.webkit.MimeTypeMap; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; @@ -245,7 +246,20 @@ public class FilesControlSet extends PopupControlSet { }); image.show(); } else { - showFromIntent(filePath, fileType); + String useType = fileType; + if (fileType.equals(FileMetadata.FILE_TYPE_OTHER)) { + String extension = AndroidUtilities.getFileExtension(filePath); + + MimeTypeMap map = MimeTypeMap.getSingleton(); + String guessedType = map.getMimeTypeFromExtension(extension); + if (!TextUtils.isEmpty(guessedType)) + useType = guessedType; + if (useType != guessedType) { + m.setValue(FileMetadata.FILE_TYPE, useType); + metadataService.save(m); + } + } + showFromIntent(filePath, useType); } } @@ -260,7 +274,6 @@ public class FilesControlSet extends PopupControlSet { } private void handleActivityNotFound(String fileType) { - System.err.println("HANDLING FILE OF TYPE: " + fileType); //$NON-NLS-1$ if (fileType.startsWith(FileMetadata.FILE_TYPE_AUDIO)) { searchMarket("com.clov4r.android.nil", R.string.search_market_audio_title, R.string.search_market_audio); //$NON-NLS-1$ } else if (fileType.equals(FileMetadata.FILE_TYPE_PDF)) { diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index c8cee3941..0e5477cb0 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -1059,10 +1059,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { String path = dst.getAbsolutePath(); String name = dst.getName(); - String extension = ""; - if (name.matches("\\w+.\\w+")) { - extension = name.substring(name.lastIndexOf('.') + 1); - } + String extension = AndroidUtilities.getFileExtension(name); String type = FileMetadata.FILE_TYPE_OTHER; if (!TextUtils.isEmpty(extension)) {