Chromium Code Reviews| 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 | |
|
Theresa
2017/04/11 17:01:28
nit: This description should be updated since it c
Finnur
2017/04/12 11:04:43
Correct. Done.
| |
| 17 * supertypes (images/*). | |
| 18 */ | |
| 19 class MimeTypeFileFilter implements FileFilter { | |
| 20 private HashSet<String> mExtensions = new HashSet<>(); | |
| 21 private HashSet<String> mMimeTypes = new HashSet<>(); | |
| 22 private HashSet<String> mMimeSupertypes = new HashSet<>(); | |
| 23 private MimeTypeMap mMimeTypeMap; | |
| 24 | |
| 25 public MimeTypeFileFilter(@NonNull String acceptAttr) { | |
|
Theresa
2017/04/11 17:01:28
JavaDoc
Finnur
2017/04/12 11:04:42
Done.
| |
| 26 for (String field : acceptAttr.toLowerCase(Locale.US).split(",")) { | |
| 27 field = field.trim(); | |
| 28 if (field.startsWith(".")) { | |
| 29 mExtensions.add(field.substring(1)); | |
| 30 } else if (field.endsWith("/*")) { | |
| 31 mMimeSupertypes.add(field.substring(0, field.length() - 2)); | |
| 32 } else if (field.contains("/")) { | |
| 33 mMimeTypes.add(field); | |
| 34 } else { | |
| 35 // Throw exception? | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 mMimeTypeMap = MimeTypeMap.getSingleton(); | |
| 40 } | |
| 41 | |
| 42 @Override | |
| 43 public boolean accept(@NonNull File file) { | |
| 44 if (file.isDirectory()) { | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 String uri = file.toURI().toString(); | |
| 49 String ext = MimeTypeMap.getFileExtensionFromUrl(uri).toLowerCase(Locale .US); | |
| 50 if (mExtensions.contains(ext)) { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 String mimeType = getMimeTypeFromExtension(ext); | |
| 55 if (mimeType != null) { | |
| 56 if (mMimeTypes.contains(mimeType) | |
| 57 || mMimeSupertypes.contains(getMimeSupertype(mimeType))) { | |
| 58 return true; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 private HashSet<String> getAcceptedSupertypes() { | |
| 66 HashSet<String> supertypes = new HashSet<>(); | |
| 67 supertypes.addAll(mMimeSupertypes); | |
| 68 for (String mimeType : mMimeTypes) { | |
| 69 supertypes.add(getMimeSupertype(mimeType)); | |
| 70 } | |
| 71 for (String ext : mExtensions) { | |
| 72 String mimeType = getMimeTypeFromExtension(ext); | |
| 73 if (mimeType != null) { | |
| 74 supertypes.add(getMimeSupertype(mimeType)); | |
| 75 } | |
| 76 } | |
| 77 return supertypes; | |
| 78 } | |
| 79 | |
| 80 private String getMimeTypeFromExtension(@NonNull String ext) { | |
| 81 String mimeType = mMimeTypeMap.getMimeTypeFromExtension(ext); | |
| 82 return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; | |
| 83 } | |
| 84 | |
| 85 @NonNull | |
| 86 private String getMimeSupertype(@NonNull String mimeType) { | |
| 87 return mimeType.split("/", 2)[0]; | |
| 88 } | |
| 89 } | |
| OLD | NEW |