From a575f411843d6b107abcc137c289c63a692b13f1 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Wed, 30 May 2012 17:54:06 -0700 Subject: [PATCH] Better handling of mime types --- .../todoroo/astrid/files/FileMetadata.java | 6 +++-- .../todoroo/astrid/files/FilesControlSet.java | 6 ++--- .../astrid/activity/TaskEditFragment.java | 23 ++++++++++++++++--- .../com/todoroo/astrid/ui/QuickAddBar.java | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/files/FileMetadata.java b/astrid/plugin-src/com/todoroo/astrid/files/FileMetadata.java index dbe841bc0..71c916069 100644 --- a/astrid/plugin-src/com/todoroo/astrid/files/FileMetadata.java +++ b/astrid/plugin-src/com/todoroo/astrid/files/FileMetadata.java @@ -13,8 +13,10 @@ public class FileMetadata { public static final String FILES_DIRECTORY = "attachments"; //$NON-NLS-1$ /** Constants for file types */ - public static final String FILE_TYPE_AUDIO = "audio/m4a"; //$NON-NLS-1$ - public static final String FILE_TYPE_IMG = "image/png"; //$NON-NLS-1$ + public static final String FILE_TYPE_AUDIO = "audio/"; //$NON-NLS-1$ + public static final String FILE_TYPE_IMAGE = "image/"; //$NON-NLS-1$ + + public static final String FILE_TYPE_OTHER = "application/octet-stream"; //$NON-NLS-1$ diff --git a/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java b/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java index 613d9885d..1e1ecf3e8 100644 --- a/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java +++ b/astrid/plugin-src/com/todoroo/astrid/files/FilesControlSet.java @@ -143,7 +143,7 @@ public class FilesControlSet extends PopupControlSet { } private void setupFileClickListener(View view, final Metadata m) { - String fileType = m.getValue(FileMetadata.FILE_TYPE); + String fileType = m.containsNonNullValue(FileMetadata.FILE_TYPE) ? m.getValue(FileMetadata.FILE_TYPE) : FileMetadata.FILE_TYPE_OTHER; final String filePath = m.containsNonNullValue(FileMetadata.FILE_PATH) ? m.getValue(FileMetadata.FILE_PATH) : null; if (TextUtils.isEmpty(filePath)) { view.setOnClickListener(new OnClickListener() { @@ -158,14 +158,14 @@ public class FilesControlSet extends PopupControlSet { }, null); } }); - } else if (FileMetadata.FILE_TYPE_AUDIO.equals(fileType)) { + } else if (fileType.startsWith(FileMetadata.FILE_TYPE_AUDIO)) { view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { RecognizerApi.play(m.getValue(FileMetadata.FILE_PATH)); } }); - } else if (FileMetadata.FILE_TYPE_IMG.equals(fileType)) { + } else if (fileType.startsWith(FileMetadata.FILE_TYPE_IMAGE)) { view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index b118b4d31..240e73c3f 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -54,6 +54,7 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewParent; +import android.webkit.MimeTypeMap; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; @@ -1038,6 +1039,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { startActivityForResult(recordAudio, REQUEST_CODE_RECORD); } + @SuppressWarnings("nls") private void attachFile(String file) { File src = new File(file); if (!src.exists()) { @@ -1053,7 +1055,22 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { return; } - createNewFileAttachment(dst.getAbsolutePath(), FileMetadata.FILE_TYPE_OTHER); + String path = dst.getAbsolutePath(); + String name = dst.getName(); + String extension = ""; + if (name.matches("\\w+.\\w+")) { + extension = name.substring(name.lastIndexOf('.') + 1); + } + + String type = FileMetadata.FILE_TYPE_OTHER; + if (!TextUtils.isEmpty(extension)) { + MimeTypeMap map = MimeTypeMap.getSingleton(); + String guessedType = map.getMimeTypeFromExtension(extension); + if (!TextUtils.isEmpty(guessedType)) + type = guessedType; + } + + createNewFileAttachment(path, type); } @SuppressWarnings("nls") @@ -1073,7 +1090,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { fos.flush(); fos.close(); - createNewFileAttachment(path, FileMetadata.FILE_TYPE_IMG); + createNewFileAttachment(path, FileMetadata.FILE_TYPE_IMAGE + "png"); } catch (Exception e) { Toast.makeText(getActivity(), R.string.file_err_copy, Toast.LENGTH_LONG); } @@ -1197,7 +1214,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener { notesControlSet.writeToModel(model); } else if (requestCode == REQUEST_CODE_RECORD && resultCode == Activity.RESULT_OK) { String recordedAudio = data.getStringExtra(AACRecordingActivity.RESULT_OUTFILE); - createNewFileAttachment(recordedAudio, FileMetadata.FILE_TYPE_AUDIO); + createNewFileAttachment(recordedAudio, FileMetadata.FILE_TYPE_AUDIO + "m4a"); //$NON-NLS-1$ } else if (requestCode == REQUEST_CODE_ATTACH_FILE && resultCode == Activity.RESULT_OK) { attachFile(data.getStringExtra(FileExplore.EXTRA_FILE_SELECTED)); } diff --git a/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java b/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java index 5e98b759d..b0d0b105a 100644 --- a/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java +++ b/astrid/src/com/todoroo/astrid/ui/QuickAddBar.java @@ -344,7 +344,7 @@ public class QuickAddBar extends LinearLayout implements RecognizerApiListener { voiceRecognizer.convert(filePath); currentVoiceFile = null; - Metadata fileMetadata = FileMetadata.createNewFileMetadata(task.getId(), filePath, FileMetadata.FILE_TYPE_AUDIO); + Metadata fileMetadata = FileMetadata.createNewFileMetadata(task.getId(), filePath, FileMetadata.FILE_TYPE_AUDIO + "m4a"); metadataService.save(fileMetadata); }