811a5a4a |
1 | /* \r |
2 | * Copyright (C) 2008 OpenIntents.org\r |
3 | *\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 |
7 | *\r |
8 | * http://www.apache.org/licenses/LICENSE-2.0\r |
9 | *\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 |
15 | */\r |
16 | \r |
17 | package org.openintents.filemanager.util;\r |
18 | \r |
19 | import java.util.HashMap;\r |
20 | import java.util.Map;\r |
21 | \r |
22 | import android.webkit.MimeTypeMap;\r |
23 | \r |
24 | public class MimeTypes {\r |
25 | \r |
26 | private Map<String, String> mMimeTypes;\r |
27 | private Map<String, Integer> mIcons;\r |
28 | \r |
29 | public MimeTypes() {\r |
30 | mMimeTypes = new HashMap<String,String>();\r |
31 | mIcons = new HashMap<String,Integer>();\r |
32 | }\r |
33 | \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 |
36 | */\r |
37 | public void put(String type, String extension, int icon){\r |
38 | put(type, extension);\r |
39 | mIcons.put(extension, icon);\r |
40 | }\r |
41 | \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 |
45 | \r |
46 | mMimeTypes.put(type, extension);\r |
47 | }\r |
48 | \r |
49 | public String getMimeType(String filename) {\r |
50 | \r |
51 | String extension = FileUtils.getExtension(filename);\r |
52 | \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 |
57 | \r |
58 | if (webkitMimeType != null) {\r |
59 | // Found one. Let's take it!\r |
60 | return webkitMimeType;\r |
61 | }\r |
62 | }\r |
63 | \r |
64 | // Convert extensions to lower case letters for easier comparison\r |
65 | extension = extension.toLowerCase();\r |
66 | \r |
67 | String mimetype = mMimeTypes.get(extension);\r |
68 | \r |
69 | if(mimetype==null) mimetype = "*/*";\r |
70 | \r |
71 | return mimetype;\r |
72 | }\r |
73 | \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 |
78 | return iconResId;\r |
79 | }\r |
80 | }\r |