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 class AttrAcceptFileFilter implements FileFilter { | |
Theresa
2017/03/28 20:40:27
Please document this class
Finnur
2017/03/31 14:26:49
Done.
| |
16 private static final String IMAGE_SUPERTYPE = "image"; | |
17 private static final String VIDEO_SUPERTYPE = "video"; | |
18 | |
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 public AttrAcceptFileFilter(@NonNull String acceptAttr) { | |
25 for (String field : acceptAttr.toLowerCase(Locale.US).split(",")) { | |
26 field = field.trim(); | |
27 if (field.startsWith(".")) { | |
28 mExtensions.add(field.substring(1)); | |
29 } else if (field.endsWith("/*")) { | |
30 mMimeSupertypes.add(field.substring(0, field.length() - 2)); | |
31 } else if (field.contains("/")) { | |
32 mMimeTypes.add(field); | |
33 } else { | |
34 // Throw exception? | |
35 } | |
36 } | |
37 | |
38 mMimeTypeMap = MimeTypeMap.getSingleton(); | |
39 } | |
40 | |
41 @Override | |
42 public boolean accept(@NonNull File file) { | |
43 if (file.isDirectory()) { | |
44 return true; | |
45 } | |
46 | |
47 String uri = file.toURI().toString(); | |
48 String ext = MimeTypeMap.getFileExtensionFromUrl(uri).toLowerCase(Locale .US); | |
49 if (mExtensions.contains(ext)) { | |
50 return true; | |
51 } | |
52 | |
53 String mimeType = getMimeTypeFromExtension(ext); | |
54 if (mimeType != null) { | |
55 if (mMimeTypes.contains(mimeType)) { | |
56 return true; | |
57 } | |
58 if (mMimeSupertypes.contains(getMimeSupertype(mimeType))) { | |
59 return true; | |
60 } | |
61 } | |
62 | |
63 return false; | |
64 } | |
65 | |
66 public boolean acceptsImages() { | |
67 return getAcceptedSupertypes().contains(IMAGE_SUPERTYPE); | |
68 } | |
69 | |
70 public boolean acceptsVideos() { | |
71 return getAcceptedSupertypes().contains(VIDEO_SUPERTYPE); | |
72 } | |
73 | |
74 public boolean acceptsOther() { | |
75 HashSet<String> supertypes = getAcceptedSupertypes(); | |
76 supertypes.remove(IMAGE_SUPERTYPE); | |
77 supertypes.remove(VIDEO_SUPERTYPE); | |
78 return !supertypes.isEmpty(); | |
79 } | |
80 | |
81 private HashSet<String> getAcceptedSupertypes() { | |
82 HashSet<String> supertypes = new HashSet<>(); | |
83 supertypes.addAll(mMimeSupertypes); | |
84 for (String mimeType : mMimeTypes) { | |
85 supertypes.add(getMimeSupertype(mimeType)); | |
86 } | |
87 for (String ext : mExtensions) { | |
88 String mimeType = getMimeTypeFromExtension(ext); | |
89 if (mimeType != null) { | |
90 supertypes.add(getMimeSupertype(mimeType)); | |
91 } | |
92 } | |
93 return supertypes; | |
94 } | |
95 | |
96 private String getMimeTypeFromExtension(@NonNull String ext) { | |
97 String mimeType = mMimeTypeMap.getMimeTypeFromExtension(ext); | |
98 return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; | |
99 } | |
100 | |
101 @NonNull | |
102 private String getMimeSupertype(@NonNull String mimeType) { | |
103 return mimeType.split("/", 2)[0]; | |
104 } | |
105 } | |
OLD | NEW |