Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/AttrAcceptFileFilter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/AttrAcceptFileFilter.java b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/AttrAcceptFileFilter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d2a34e2f269ca7d9fbc9afb54dcb5dc39a1ba3e8 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/AttrAcceptFileFilter.java |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.photo_picker; |
| + |
| +import android.support.annotation.NonNull; |
| +import android.webkit.MimeTypeMap; |
| + |
| +import java.io.File; |
| +import java.io.FileFilter; |
| +import java.util.HashSet; |
| +import java.util.Locale; |
| + |
| +/** |
| + * A file filter for handling extensions and MIME types (images/jpeg) and |
| + * supertypes (images/*). |
| + */ |
| +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.
|
| + private static final String IMAGE_SUPERTYPE = "image"; |
| + private static final String VIDEO_SUPERTYPE = "video"; |
| + |
| + private HashSet<String> mExtensions = new HashSet<>(); |
| + private HashSet<String> mMimeTypes = new HashSet<>(); |
| + private HashSet<String> mMimeSupertypes = new HashSet<>(); |
| + private MimeTypeMap mMimeTypeMap; |
| + |
| + public AttrAcceptFileFilter(@NonNull String acceptAttr) { |
| + for (String field : acceptAttr.toLowerCase(Locale.US).split(",")) { |
| + field = field.trim(); |
| + if (field.startsWith(".")) { |
| + mExtensions.add(field.substring(1)); |
| + } else if (field.endsWith("/*")) { |
| + mMimeSupertypes.add(field.substring(0, field.length() - 2)); |
| + } else if (field.contains("/")) { |
| + mMimeTypes.add(field); |
| + } else { |
| + // Throw exception? |
| + } |
| + } |
| + |
| + mMimeTypeMap = MimeTypeMap.getSingleton(); |
| + } |
| + |
| + @Override |
| + public boolean accept(@NonNull File file) { |
| + if (file.isDirectory()) { |
| + return true; |
| + } |
| + |
| + String uri = file.toURI().toString(); |
| + String ext = MimeTypeMap.getFileExtensionFromUrl(uri).toLowerCase(Locale.US); |
| + if (mExtensions.contains(ext)) { |
| + return true; |
| + } |
| + |
| + String mimeType = getMimeTypeFromExtension(ext); |
| + if (mimeType != null) { |
| + if (mMimeTypes.contains(mimeType)) { |
| + return true; |
| + } |
| + if (mMimeSupertypes.contains(getMimeSupertype(mimeType))) { |
| + return true; |
| + } |
| + } |
| + |
| + return false; |
| + } |
| + |
| + 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.
|
| + return getAcceptedSupertypes().contains(IMAGE_SUPERTYPE); |
| + } |
| + |
| + public boolean acceptsVideos() { |
| + return getAcceptedSupertypes().contains(VIDEO_SUPERTYPE); |
| + } |
| + |
| + public boolean acceptsOther() { |
| + HashSet<String> supertypes = getAcceptedSupertypes(); |
| + supertypes.remove(IMAGE_SUPERTYPE); |
| + supertypes.remove(VIDEO_SUPERTYPE); |
| + return !supertypes.isEmpty(); |
| + } |
| + |
| + private HashSet<String> getAcceptedSupertypes() { |
| + HashSet<String> supertypes = new HashSet<>(); |
| + supertypes.addAll(mMimeSupertypes); |
| + for (String mimeType : mMimeTypes) { |
| + supertypes.add(getMimeSupertype(mimeType)); |
| + } |
| + for (String ext : mExtensions) { |
| + String mimeType = getMimeTypeFromExtension(ext); |
| + if (mimeType != null) { |
| + supertypes.add(getMimeSupertype(mimeType)); |
| + } |
| + } |
| + return supertypes; |
| + } |
| + |
| + private String getMimeTypeFromExtension(@NonNull String ext) { |
| + 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
|
| + return (mimeType != null) ? mimeType.toLowerCase(Locale.US) : null; |
| + } |
| + |
| + @NonNull |
| + private String getMimeSupertype(@NonNull String mimeType) { |
| + return mimeType.split("/", 2)[0]; |
| + } |
| +} |