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 and MIME types (images/jpeg) and | |
17 * supertypes (images/*). | |
18 */ | |
19 class AttrAcceptFileFilter implements FileFilter { | |
Theresa
2017/04/10 18:01:14
nit: I think ImageFileFilter is a better name sinc
Finnur
2017/04/11 11:30:57
Agreed.
| |
20 private static final String IMAGE_SUPERTYPE = "image"; | |
21 private static final String VIDEO_SUPERTYPE = "video"; | |
22 | |
23 private HashSet<String> mExtensions = new HashSet<>(); | |
24 private HashSet<String> mMimeTypes = new HashSet<>(); | |
25 private HashSet<String> mMimeSupertypes = new HashSet<>(); | |
26 private MimeTypeMap mMimeTypeMap; | |
27 | |
28 public AttrAcceptFileFilter(@NonNull String acceptAttr) { | |
29 for (String field : acceptAttr.toLowerCase(Locale.US).split(",")) { | |
30 field = field.trim(); | |
31 if (field.startsWith(".")) { | |
32 mExtensions.add(field.substring(1)); | |
33 } else if (field.endsWith("/*")) { | |
34 mMimeSupertypes.add(field.substring(0, field.length() - 2)); | |
35 } else if (field.contains("/")) { | |
36 mMimeTypes.add(field); | |
37 } else { | |
38 // Throw exception? | |
39 } | |
40 } | |
41 | |
42 mMimeTypeMap = MimeTypeMap.getSingleton(); | |
43 } | |
44 | |
45 @Override | |
46 public boolean accept(@NonNull File file) { | |
47 if (file.isDirectory()) { | |
48 return true; | |
49 } | |
50 | |
51 String uri = file.toURI().toString(); | |
52 String ext = MimeTypeMap.getFileExtensionFromUrl(uri).toLowerCase(Locale .US); | |
53 if (mExtensions.contains(ext)) { | |
54 return true; | |
55 } | |
56 | |
57 String mimeType = getMimeTypeFromExtension(ext); | |
58 if (mimeType != null) { | |
59 if (mMimeTypes.contains(mimeType)) { | |
60 return true; | |
61 } | |
62 if (mMimeSupertypes.contains(getMimeSupertype(mimeType))) { | |
63 return true; | |
64 } | |
65 } | |
66 | |
67 return false; | |
68 } | |
69 | |
70 public boolean acceptsImages() { | |
Theresa
2017/04/10 18:01:13
nit: JavaDoc on this and other two public methods.
Finnur
2017/04/11 11:30:57
Huh... Nothing apparently. Good catch.
| |
71 return getAcceptedSupertypes().contains(IMAGE_SUPERTYPE); | |
72 } | |
73 | |
74 public boolean acceptsVideos() { | |
75 return getAcceptedSupertypes().contains(VIDEO_SUPERTYPE); | |
76 } | |
77 | |
78 public boolean acceptsOther() { | |
79 HashSet<String> supertypes = getAcceptedSupertypes(); | |
80 supertypes.remove(IMAGE_SUPERTYPE); | |
81 supertypes.remove(VIDEO_SUPERTYPE); | |
82 return !supertypes.isEmpty(); | |
83 } | |
84 | |
85 private HashSet<String> getAcceptedSupertypes() { | |
86 HashSet<String> supertypes = new HashSet<>(); | |
87 supertypes.addAll(mMimeSupertypes); | |
88 for (String mimeType : mMimeTypes) { | |
89 supertypes.add(getMimeSupertype(mimeType)); | |
90 } | |
91 for (String ext : mExtensions) { | |
92 String mimeType = getMimeTypeFromExtension(ext); | |
93 if (mimeType != null) { | |
94 supertypes.add(getMimeSupertype(mimeType)); | |
95 } | |
96 } | |
97 return supertypes; | |
98 } | |
99 | |
100 private String getMimeTypeFromExtension(@NonNull String ext) { | |
101 String mimeType = mMimeTypeMap.getMimeTypeFromExtension(ext); | |
Theresa
2017/04/10 18:01:14
This requires the file to have an extension type.
Finnur
2017/04/11 11:30:57
Yes, that is consistent. I used adb shell to renam
| |
102 return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; | |
103 } | |
104 | |
105 @NonNull | |
106 private String getMimeSupertype(@NonNull String mimeType) { | |
107 return mimeType.split("/", 2)[0]; | |
108 } | |
109 } | |
OLD | NEW |