--- /dev/null
+/* \r
+ * Copyright (C) 2008 OpenIntents.org\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openintents.filemanager.util;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import android.webkit.MimeTypeMap;\r
+\r
+public class MimeTypes {\r
+\r
+ private Map<String, String> mMimeTypes;\r
+ private Map<String, Integer> mIcons;\r
+\r
+ public MimeTypes() {\r
+ mMimeTypes = new HashMap<String,String>();\r
+ mIcons = new HashMap<String,Integer>();\r
+ }\r
+ \r
+ /* I think the type and extension names are switched (type contains .png, extension contains x/y),\r
+ * but maybe it's on purpouse, so I won't change it.\r
+ */\r
+ public void put(String type, String extension, int icon){\r
+ put(type, extension);\r
+ mIcons.put(extension, icon);\r
+ }\r
+ \r
+ public void put(String type, String extension) {\r
+ // Convert extensions to lower case letters for easier comparison\r
+ extension = extension.toLowerCase();\r
+ \r
+ mMimeTypes.put(type, extension);\r
+ }\r
+ \r
+ public String getMimeType(String filename) {\r
+ \r
+ String extension = FileUtils.getExtension(filename);\r
+ \r
+ // Let's check the official map first. Webkit has a nice extension-to-MIME map.\r
+ // Be sure to remove the first character from the extension, which is the "." character.\r
+ if (extension.length() > 0) {\r
+ String webkitMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1));\r
+ \r
+ if (webkitMimeType != null) {\r
+ // Found one. Let's take it!\r
+ return webkitMimeType;\r
+ }\r
+ }\r
+ \r
+ // Convert extensions to lower case letters for easier comparison\r
+ extension = extension.toLowerCase();\r
+ \r
+ String mimetype = mMimeTypes.get(extension);\r
+ \r
+ if(mimetype==null) mimetype = "*/*";\r
+ \r
+ return mimetype;\r
+ }\r
+ \r
+ public int getIcon(String mimetype){\r
+ Integer iconResId = mIcons.get(mimetype);\r
+ if(iconResId == null)\r
+ return 0; // Invalid identifier\r
+ return iconResId;\r
+ }\r
+}\r