Better handling of mime types

pull/14/head
Sam Bosley 13 years ago
parent 5902ac3144
commit a575f41184

@ -13,8 +13,10 @@ public class FileMetadata {
public static final String FILES_DIRECTORY = "attachments"; //$NON-NLS-1$ public static final String FILES_DIRECTORY = "attachments"; //$NON-NLS-1$
/** Constants for file types */ /** Constants for file types */
public static final String FILE_TYPE_AUDIO = "audio/m4a"; //$NON-NLS-1$ public static final String FILE_TYPE_AUDIO = "audio/"; //$NON-NLS-1$
public static final String FILE_TYPE_IMG = "image/png"; //$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$ public static final String FILE_TYPE_OTHER = "application/octet-stream"; //$NON-NLS-1$

@ -143,7 +143,7 @@ public class FilesControlSet extends PopupControlSet {
} }
private void setupFileClickListener(View view, final Metadata m) { 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; final String filePath = m.containsNonNullValue(FileMetadata.FILE_PATH) ? m.getValue(FileMetadata.FILE_PATH) : null;
if (TextUtils.isEmpty(filePath)) { if (TextUtils.isEmpty(filePath)) {
view.setOnClickListener(new OnClickListener() { view.setOnClickListener(new OnClickListener() {
@ -158,14 +158,14 @@ public class FilesControlSet extends PopupControlSet {
}, null); }, null);
} }
}); });
} else if (FileMetadata.FILE_TYPE_AUDIO.equals(fileType)) { } else if (fileType.startsWith(FileMetadata.FILE_TYPE_AUDIO)) {
view.setOnClickListener(new OnClickListener() { view.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
RecognizerApi.play(m.getValue(FileMetadata.FILE_PATH)); 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() { view.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {

@ -54,6 +54,7 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent; import android.view.ViewParent;
import android.webkit.MimeTypeMap;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
@ -1038,6 +1039,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
startActivityForResult(recordAudio, REQUEST_CODE_RECORD); startActivityForResult(recordAudio, REQUEST_CODE_RECORD);
} }
@SuppressWarnings("nls")
private void attachFile(String file) { private void attachFile(String file) {
File src = new File(file); File src = new File(file);
if (!src.exists()) { if (!src.exists()) {
@ -1053,7 +1055,22 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
return; 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") @SuppressWarnings("nls")
@ -1073,7 +1090,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
fos.flush(); fos.flush();
fos.close(); fos.close();
createNewFileAttachment(path, FileMetadata.FILE_TYPE_IMG); createNewFileAttachment(path, FileMetadata.FILE_TYPE_IMAGE + "png");
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(getActivity(), R.string.file_err_copy, Toast.LENGTH_LONG); Toast.makeText(getActivity(), R.string.file_err_copy, Toast.LENGTH_LONG);
} }
@ -1197,7 +1214,7 @@ ViewPager.OnPageChangeListener, EditNoteActivity.UpdatesChangedListener {
notesControlSet.writeToModel(model); notesControlSet.writeToModel(model);
} else if (requestCode == REQUEST_CODE_RECORD && resultCode == Activity.RESULT_OK) { } else if (requestCode == REQUEST_CODE_RECORD && resultCode == Activity.RESULT_OK) {
String recordedAudio = data.getStringExtra(AACRecordingActivity.RESULT_OUTFILE); 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) { } else if (requestCode == REQUEST_CODE_ATTACH_FILE && resultCode == Activity.RESULT_OK) {
attachFile(data.getStringExtra(FileExplore.EXTRA_FILE_SELECTED)); attachFile(data.getStringExtra(FileExplore.EXTRA_FILE_SELECTED));
} }

@ -344,7 +344,7 @@ public class QuickAddBar extends LinearLayout implements RecognizerApiListener {
voiceRecognizer.convert(filePath); voiceRecognizer.convert(filePath);
currentVoiceFile = null; 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); metadataService.save(fileMetadata);
} }

Loading…
Cancel
Save