2 * Copyright (C) 2008 OpenIntents.org
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openintents.filemanager.util;
\r
19 import java.util.HashMap;
\r
20 import java.util.Map;
\r
22 import android.webkit.MimeTypeMap;
\r
24 public class MimeTypes {
\r
26 private Map<String, String> mMimeTypes;
\r
27 private Map<String, Integer> mIcons;
\r
29 public MimeTypes() {
\r
30 mMimeTypes = new HashMap<String,String>();
\r
31 mIcons = new HashMap<String,Integer>();
\r
34 /* I think the type and extension names are switched (type contains .png, extension contains x/y),
\r
35 * but maybe it's on purpouse, so I won't change it.
\r
37 public void put(String type, String extension, int icon){
\r
38 put(type, extension);
\r
39 mIcons.put(extension, icon);
\r
42 public void put(String type, String extension) {
\r
43 // Convert extensions to lower case letters for easier comparison
\r
44 extension = extension.toLowerCase();
\r
46 mMimeTypes.put(type, extension);
\r
49 public String getMimeType(String filename) {
\r
51 String extension = FileUtils.getExtension(filename);
\r
53 // Let's check the official map first. Webkit has a nice extension-to-MIME map.
\r
54 // Be sure to remove the first character from the extension, which is the "." character.
\r
55 if (extension.length() > 0) {
\r
56 String webkitMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1));
\r
58 if (webkitMimeType != null) {
\r
59 // Found one. Let's take it!
\r
60 return webkitMimeType;
\r
64 // Convert extensions to lower case letters for easier comparison
\r
65 extension = extension.toLowerCase();
\r
67 String mimetype = mMimeTypes.get(extension);
\r
69 if(mimetype==null) mimetype = "*/*";
\r
74 public int getIcon(String mimetype){
\r
75 Integer iconResId = mIcons.get(mimetype);
\r
76 if(iconResId == null)
\r
77 return 0; // Invalid identifier
\r