OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.photo_picker; | |
6 | |
7 import android.support.annotation.NonNull; | |
8 import android.webkit.MimeTypeMap; | |
9 | |
10 import java.io.File; | |
11 import java.io.FileFilter; | |
12 import java.util.HashSet; | |
13 import java.util.Locale; | |
14 | |
15 /** | |
16 * A file filter for handling extensions mapping to MIME types (such as images/j peg and images/*). | |
17 */ | |
18 class MimeTypeFileFilter implements FileFilter { | |
19 private HashSet<String> mExtensions = new HashSet<>(); | |
20 private HashSet<String> mMimeTypes = new HashSet<>(); | |
21 private HashSet<String> mMimeSupertypes = new HashSet<>(); | |
22 private MimeTypeMap mMimeTypeMap; | |
23 | |
24 /** | |
25 * Contructs a MimeTypeFileFilter object. | |
26 * @param acceptAttr A comma seperated list of MIME types this filter accept s. | |
27 * For example: images/gif, video/*. | |
28 */ | |
29 public MimeTypeFileFilter(@NonNull String acceptAttr) { | |
Theresa
2017/04/12 14:56:18
Instead of a comma separate string, could this be
| |
30 for (String field : acceptAttr.toLowerCase(Locale.US).split(",")) { | |
31 field = field.trim(); | |
32 if (field.startsWith(".")) { | |
33 mExtensions.add(field.substring(1)); | |
34 } else if (field.endsWith("/*")) { | |
35 mMimeSupertypes.add(field.substring(0, field.length() - 2)); | |
36 } else if (field.contains("/")) { | |
37 mMimeTypes.add(field); | |
38 } else { | |
39 // Throw exception? | |
40 } | |
41 } | |
42 | |
43 mMimeTypeMap = MimeTypeMap.getSingleton(); | |
44 } | |
45 | |
46 @Override | |
47 public boolean accept(@NonNull File file) { | |
48 if (file.isDirectory()) { | |
49 return true; | |
50 } | |
51 | |
52 String uri = file.toURI().toString(); | |
53 String ext = MimeTypeMap.getFileExtensionFromUrl(uri).toLowerCase(Locale .US); | |
54 if (mExtensions.contains(ext)) { | |
55 return true; | |
56 } | |
57 | |
58 String mimeType = getMimeTypeFromExtension(ext); | |
59 if (mimeType != null) { | |
60 if (mMimeTypes.contains(mimeType) | |
61 || mMimeSupertypes.contains(getMimeSupertype(mimeType))) { | |
62 return true; | |
63 } | |
64 } | |
65 | |
66 return false; | |
67 } | |
68 | |
69 private HashSet<String> getAcceptedSupertypes() { | |
70 HashSet<String> supertypes = new HashSet<>(); | |
71 supertypes.addAll(mMimeSupertypes); | |
72 for (String mimeType : mMimeTypes) { | |
73 supertypes.add(getMimeSupertype(mimeType)); | |
74 } | |
75 for (String ext : mExtensions) { | |
76 String mimeType = getMimeTypeFromExtension(ext); | |
77 if (mimeType != null) { | |
78 supertypes.add(getMimeSupertype(mimeType)); | |
79 } | |
80 } | |
81 return supertypes; | |
82 } | |
83 | |
84 private String getMimeTypeFromExtension(@NonNull String ext) { | |
85 String mimeType = mMimeTypeMap.getMimeTypeFromExtension(ext); | |
86 return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; | |
87 } | |
88 | |
89 @NonNull | |
90 private String getMimeSupertype(@NonNull String mimeType) { | |
91 return mimeType.split("/", 2)[0]; | |
92 } | |
93 } | |
OLD | NEW |