| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.ui; | 5 package org.chromium.ui; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.ContentResolver; | 8 import android.content.ContentResolver; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.database.Cursor; | 10 import android.database.Cursor; |
| 11 import android.net.Uri; | 11 import android.net.Uri; |
| 12 import android.os.Environment; | 12 import android.os.Environment; |
| 13 import android.provider.MediaStore; | 13 import android.provider.MediaStore; |
| 14 import android.text.TextUtils; |
| 14 | 15 |
| 15 import java.io.File; | 16 import java.io.File; |
| 16 import java.util.ArrayList; | 17 import java.util.ArrayList; |
| 17 import java.util.Arrays; | 18 import java.util.Arrays; |
| 18 import java.util.List; | 19 import java.util.List; |
| 19 | 20 |
| 20 import org.chromium.base.CalledByNative; | 21 import org.chromium.base.CalledByNative; |
| 21 import org.chromium.base.JNINamespace; | 22 import org.chromium.base.JNINamespace; |
| 22 import org.chromium.ui.WindowAndroid; | 23 import org.chromium.ui.WindowAndroid; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * A dialog that is triggered from a file input field that allows a user to sele
ct a file based on | 26 * A dialog that is triggered from a file input field that allows a user to sele
ct a file based on |
| 26 * a set of accepted file types. The path of the selected file is passed to the
native dialog. | 27 * a set of accepted file types. The path of the selected file is passed to the
native dialog. |
| 27 */ | 28 */ |
| 28 @JNINamespace("ui") | 29 @JNINamespace("ui") |
| 29 class SelectFileDialog implements WindowAndroid.IntentCallback{ | 30 class SelectFileDialog implements WindowAndroid.IntentCallback{ |
| 30 private static final String IMAGE_TYPE = "image/"; | 31 private static final String IMAGE_TYPE = "image/"; |
| 31 private static final String VIDEO_TYPE = "video/"; | 32 private static final String VIDEO_TYPE = "video/"; |
| 32 private static final String AUDIO_TYPE = "audio/"; | 33 private static final String AUDIO_TYPE = "audio/"; |
| 33 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; | 34 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; |
| 34 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; | 35 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; |
| 35 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; | 36 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; |
| 36 private static final String ANY_TYPES = "*/*"; | 37 private static final String ANY_TYPES = "*/*"; |
| 37 private static final String CAPTURE_CAMERA = "camera"; | |
| 38 private static final String CAPTURE_CAMCORDER = "camcorder"; | |
| 39 private static final String CAPTURE_MICROPHONE = "microphone"; | |
| 40 private static final String CAPTURE_FILESYSTEM = "filesystem"; | |
| 41 private static final String CAPTURE_IMAGE_DIRECTORY = "browser-photos"; | 38 private static final String CAPTURE_IMAGE_DIRECTORY = "browser-photos"; |
| 42 | 39 |
| 43 private final int mNativeSelectFileDialog; | 40 private final int mNativeSelectFileDialog; |
| 44 private List<String> mFileTypes; | 41 private List<String> mFileTypes; |
| 45 private String mCapture; // May be null if no capture parameter was set. | 42 private boolean mCapture; |
| 46 private Uri mCameraOutputUri; | 43 private Uri mCameraOutputUri; |
| 47 | 44 |
| 48 private SelectFileDialog(int nativeSelectFileDialog) { | 45 private SelectFileDialog(int nativeSelectFileDialog) { |
| 49 mNativeSelectFileDialog = nativeSelectFileDialog; | 46 mNativeSelectFileDialog = nativeSelectFileDialog; |
| 50 } | 47 } |
| 51 | 48 |
| 52 /** | 49 /** |
| 53 * Creates and starts an intent based on the passed fileTypes and capture va
lue. | 50 * Creates and starts an intent based on the passed fileTypes and capture va
lue. |
| 54 * @param fileTypes MIME types requested (i.e. "image/*") | 51 * @param fileTypes MIME types requested (i.e. "image/*") |
| 55 * @param capture The capture value as described in http://www.w3.org/TR/htm
l-media-capture/ | 52 * @param capture The capture value as described in http://www.w3.org/TR/htm
l-media-capture/ |
| 56 * @param window The WindowAndroid that can show intents | 53 * @param window The WindowAndroid that can show intents |
| 57 */ | 54 */ |
| 58 @CalledByNative | 55 @CalledByNative |
| 59 private void selectFile(String[] fileTypes, String capture, WindowAndroid wi
ndow) { | 56 private void selectFile(String[] fileTypes, boolean capture, WindowAndroid w
indow) { |
| 60 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); | 57 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); |
| 61 mCapture = capture; | 58 mCapture = capture; |
| 62 | 59 |
| 63 Intent chooser = new Intent(Intent.ACTION_CHOOSER); | 60 Intent chooser = new Intent(Intent.ACTION_CHOOSER); |
| 64 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 61 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 65 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); | 62 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); |
| 66 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); | 63 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); |
| 67 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | 64 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 68 Intent soundRecorder = new Intent( | 65 Intent soundRecorder = new Intent( |
| 69 MediaStore.Audio.Media.RECORD_SOUND_ACTION); | 66 MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
| 70 String lowMemoryError = window.getContext().getString(R.string.low_memor
y_error); | 67 String lowMemoryError = window.getContext().getString(R.string.low_memor
y_error); |
| 71 | 68 |
| 72 // Quick check - if a capture parameter other than filesystem (the defau
lt) is specified we | 69 // Quick check - if the |capture| parameter is set and |fileTypes| has t
he appropriate MIME |
| 73 // should just launch the appropriate intent. Otherwise build up a choos
er based on the | 70 // type, we should just launch the appropriate intent. Otherwise build u
p a chooser based on |
| 74 // accept type and then display that to the user. | 71 // the accept type and then display that to the user. |
| 75 if (captureCamera()) { | 72 if (captureCamera()) { |
| 76 if (window.showIntent(camera, this, lowMemoryError)) return; | 73 if (window.showIntent(camera, this, lowMemoryError)) return; |
| 77 } else if (captureCamcorder()) { | 74 } else if (captureCamcorder()) { |
| 78 if (window.showIntent(camcorder, this, lowMemoryError)) return; | 75 if (window.showIntent(camcorder, this, lowMemoryError)) return; |
| 79 } else if (captureMicrophone()) { | 76 } else if (captureMicrophone()) { |
| 80 if (window.showIntent(soundRecorder, this, lowMemoryError)) return; | 77 if (window.showIntent(soundRecorder, this, lowMemoryError)) return; |
| 81 } | 78 } |
| 82 | 79 |
| 83 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); | 80 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 84 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); | 81 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 } | 205 } |
| 209 | 206 |
| 210 private boolean shouldShowVideoTypes() { | 207 private boolean shouldShowVideoTypes() { |
| 211 return shouldShowTypes(ALL_VIDEO_TYPES, VIDEO_TYPE); | 208 return shouldShowTypes(ALL_VIDEO_TYPES, VIDEO_TYPE); |
| 212 } | 209 } |
| 213 | 210 |
| 214 private boolean shouldShowAudioTypes() { | 211 private boolean shouldShowAudioTypes() { |
| 215 return shouldShowTypes(ALL_AUDIO_TYPES, AUDIO_TYPE); | 212 return shouldShowTypes(ALL_AUDIO_TYPES, AUDIO_TYPE); |
| 216 } | 213 } |
| 217 | 214 |
| 215 private boolean acceptsSpecificType(String type) { |
| 216 return mFileTypes.size() == 1 && TextUtils.equals(mFileTypes.get(0), typ
e); |
| 217 } |
| 218 |
| 218 private boolean captureCamera() { | 219 private boolean captureCamera() { |
| 219 return shouldShowImageTypes() && mCapture != null && mCapture.startsWith
(CAPTURE_CAMERA); | 220 return mCapture && acceptsSpecificType(ALL_IMAGE_TYPES); |
| 220 } | 221 } |
| 221 | 222 |
| 222 private boolean captureCamcorder() { | 223 private boolean captureCamcorder() { |
| 223 return shouldShowVideoTypes() && mCapture != null && | 224 return mCapture && acceptsSpecificType(ALL_VIDEO_TYPES); |
| 224 mCapture.startsWith(CAPTURE_CAMCORDER); | |
| 225 } | 225 } |
| 226 | 226 |
| 227 private boolean captureMicrophone() { | 227 private boolean captureMicrophone() { |
| 228 return shouldShowAudioTypes() && mCapture != null && | 228 return mCapture && acceptsSpecificType(ALL_AUDIO_TYPES); |
| 229 mCapture.startsWith(CAPTURE_MICROPHONE); | |
| 230 } | |
| 231 | |
| 232 private boolean captureFilesystem() { | |
| 233 return mCapture != null && mCapture.startsWith(CAPTURE_FILESYSTEM); | |
| 234 } | 229 } |
| 235 | 230 |
| 236 private boolean acceptSpecificType(String accept) { | 231 private boolean acceptSpecificType(String accept) { |
| 237 for (String type : mFileTypes) { | 232 for (String type : mFileTypes) { |
| 238 if (type.startsWith(accept)) { | 233 if (type.startsWith(accept)) { |
| 239 return true; | 234 return true; |
| 240 } | 235 } |
| 241 } | 236 } |
| 242 return false; | 237 return false; |
| 243 } | 238 } |
| 244 | 239 |
| 245 @CalledByNative | 240 @CalledByNative |
| 246 private static SelectFileDialog create(int nativeSelectFileDialog) { | 241 private static SelectFileDialog create(int nativeSelectFileDialog) { |
| 247 return new SelectFileDialog(nativeSelectFileDialog); | 242 return new SelectFileDialog(nativeSelectFileDialog); |
| 248 } | 243 } |
| 249 | 244 |
| 250 private native void nativeOnFileSelected(int nativeSelectFileDialogImpl, | 245 private native void nativeOnFileSelected(int nativeSelectFileDialogImpl, |
| 251 String filePath); | 246 String filePath); |
| 252 private native void nativeOnFileNotSelected(int nativeSelectFileDialogImpl); | 247 private native void nativeOnFileNotSelected(int nativeSelectFileDialogImpl); |
| 253 } | 248 } |
| OLD | NEW |