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 ImageFileFilter implements FileFilter { | |
20 private static final String IMAGE_SUPERTYPE = "image"; | |
21 private static final String VIDEO_SUPERTYPE = "video"; | |
Theresa
2017/04/11 15:44:49
This isn't used anymore.
| |
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 ImageFileFilter(@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 } | |
Theresa
2017/04/11 15:44:49
nit: this can be simplified to:
if (mimeType != n
Finnur
2017/04/11 16:25:23
Done.
| |
65 } | |
66 | |
67 return false; | |
68 } | |
69 | |
70 private HashSet<String> getAcceptedSupertypes() { | |
71 HashSet<String> supertypes = new HashSet<>(); | |
72 supertypes.addAll(mMimeSupertypes); | |
73 for (String mimeType : mMimeTypes) { | |
74 supertypes.add(getMimeSupertype(mimeType)); | |
75 } | |
76 for (String ext : mExtensions) { | |
77 String mimeType = getMimeTypeFromExtension(ext); | |
78 if (mimeType != null) { | |
79 supertypes.add(getMimeSupertype(mimeType)); | |
80 } | |
81 } | |
82 return supertypes; | |
83 } | |
84 | |
85 private String getMimeTypeFromExtension(@NonNull String ext) { | |
86 String mimeType = mMimeTypeMap.getMimeTypeFromExtension(ext); | |
87 return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; | |
88 } | |
89 | |
90 @NonNull | |
91 private String getMimeSupertype(@NonNull String mimeType) { | |
92 return mimeType.split("/", 2)[0]; | |
93 } | |
94 } | |
OLD | NEW |