Chromium Code Reviews| 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; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 */ | 27 */ |
| 28 @JNINamespace("ui") | 28 @JNINamespace("ui") |
| 29 class SelectFileDialog implements WindowAndroid.IntentCallback{ | 29 class SelectFileDialog implements WindowAndroid.IntentCallback{ |
| 30 private static final String IMAGE_TYPE = "image/"; | 30 private static final String IMAGE_TYPE = "image/"; |
| 31 private static final String VIDEO_TYPE = "video/"; | 31 private static final String VIDEO_TYPE = "video/"; |
| 32 private static final String AUDIO_TYPE = "audio/"; | 32 private static final String AUDIO_TYPE = "audio/"; |
| 33 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; | 33 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; |
| 34 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; | 34 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; |
| 35 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; | 35 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; |
| 36 private static final String ANY_TYPES = "*/*"; | 36 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"; | 37 private static final String CAPTURE_IMAGE_DIRECTORY = "browser-photos"; |
| 42 | 38 |
| 43 private final int mNativeSelectFileDialog; | 39 private final int mNativeSelectFileDialog; |
| 44 private List<String> mFileTypes; | 40 private List<String> mFileTypes; |
| 45 private String mCapture; // May be null if no capture parameter was set. | 41 private boolean mCapture; |
| 46 private Uri mCameraOutputUri; | 42 private Uri mCameraOutputUri; |
| 47 | 43 |
| 48 private SelectFileDialog(int nativeSelectFileDialog) { | 44 private SelectFileDialog(int nativeSelectFileDialog) { |
| 49 mNativeSelectFileDialog = nativeSelectFileDialog; | 45 mNativeSelectFileDialog = nativeSelectFileDialog; |
| 50 } | 46 } |
| 51 | 47 |
| 52 /** | 48 /** |
| 53 * Creates and starts an intent based on the passed fileTypes and capture va lue. | 49 * Creates and starts an intent based on the passed fileTypes and capture va lue. |
| 54 * @param fileTypes MIME types requested (i.e. "image/*") | 50 * @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/ | 51 * @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 | 52 * @param window The WindowAndroid that can show intents |
| 57 */ | 53 */ |
| 58 @CalledByNative | 54 @CalledByNative |
| 59 private void selectFile(String[] fileTypes, String capture, WindowAndroid wi ndow) { | 55 private void selectFile(String[] fileTypes, boolean capture, WindowAndroid w indow) { |
| 60 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); | 56 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); |
| 61 mCapture = capture; | 57 mCapture = capture; |
| 62 | 58 |
| 63 Intent chooser = new Intent(Intent.ACTION_CHOOSER); | 59 Intent chooser = new Intent(Intent.ACTION_CHOOSER); |
| 64 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 60 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 65 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); | 61 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); |
| 66 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); | 62 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); |
| 67 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | 63 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 68 Intent soundRecorder = new Intent( | 64 Intent soundRecorder = new Intent( |
| 69 MediaStore.Audio.Media.RECORD_SOUND_ACTION); | 65 MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
| 70 String lowMemoryError = window.getContext().getString(R.string.low_memor y_error); | 66 String lowMemoryError = window.getContext().getString(R.string.low_memor y_error); |
| 71 | 67 |
| 72 // Quick check - if a capture parameter other than filesystem (the defau lt) is specified we | 68 // 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 | 69 // 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. | 70 // the accept type and then display that to the user. |
| 75 if (captureCamera()) { | 71 if (captureCamera()) { |
| 76 if (window.showIntent(camera, this, lowMemoryError)) return; | 72 if (window.showIntent(camera, this, lowMemoryError)) return; |
| 77 } else if (captureCamcorder()) { | 73 } else if (captureCamcorder()) { |
| 78 if (window.showIntent(camcorder, this, lowMemoryError)) return; | 74 if (window.showIntent(camcorder, this, lowMemoryError)) return; |
| 79 } else if (captureMicrophone()) { | 75 } else if (captureMicrophone()) { |
| 80 if (window.showIntent(soundRecorder, this, lowMemoryError)) return; | 76 if (window.showIntent(soundRecorder, this, lowMemoryError)) return; |
| 81 } | 77 } |
| 82 | 78 |
| 83 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); | 79 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 84 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); | 80 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| 85 ArrayList<Intent> extraIntents = new ArrayList<Intent>(); | 81 ArrayList<Intent> extraIntents = new ArrayList<Intent>(); |
| 86 if (!noSpecificType()) { | 82 if (!noSpecificType()) { |
| 87 // Create a chooser based on the accept type that was specified in t he webpage. Note | 83 // Create a chooser based on the accept type that was specified in t he webpage. Note |
| 88 // that if the web page specified multiple accept types, we will hav e built a generic | 84 // that if the web page specified multiple accept types, we will hav e built a generic |
| 89 // chooser above. | 85 // chooser above. |
| 90 if (shouldShowImageTypes()) { | 86 if (shouldShowImageTypes()) { |
| 91 extraIntents.add(camera); | 87 extraIntents.add(camera); |
| 92 getContentIntent.setType("image/*"); | 88 getContentIntent.setType("image/*"); |
|
joth
2013/07/04 16:55:22
(patch creep) this could be ALL_IMAGE_TYPES etc he
| |
| 93 } else if (shouldShowVideoTypes()) { | 89 } else if (shouldShowVideoTypes()) { |
| 94 extraIntents.add(camcorder); | 90 extraIntents.add(camcorder); |
| 95 getContentIntent.setType("video/*"); | 91 getContentIntent.setType("video/*"); |
| 96 } else if (shouldShowAudioTypes()) { | 92 } else if (shouldShowAudioTypes()) { |
| 97 extraIntents.add(soundRecorder); | 93 extraIntents.add(soundRecorder); |
| 98 getContentIntent.setType("audio/*"); | 94 getContentIntent.setType("audio/*"); |
| 99 } | 95 } |
| 100 } | 96 } |
| 101 | 97 |
| 102 if (extraIntents.isEmpty()) { | 98 if (extraIntents.isEmpty()) { |
| 103 // We couldn't resolve an accept type, so fallback to a generic choo ser. | 99 // We couldn't resolve an accept type, so fallback to a generic choo ser. |
| 104 getContentIntent.setType("*/*"); | 100 getContentIntent.setType("*/*"); |
|
joth
2013/07/04 16:55:22
ALL_TYPES
| |
| 105 extraIntents.add(camera); | 101 extraIntents.add(camera); |
| 106 extraIntents.add(camcorder); | 102 extraIntents.add(camcorder); |
| 107 extraIntents.add(soundRecorder); | 103 extraIntents.add(soundRecorder); |
| 108 } | 104 } |
| 109 | 105 |
| 110 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, | 106 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, |
| 111 extraIntents.toArray(new Intent[] { })); | 107 extraIntents.toArray(new Intent[] { })); |
| 112 | 108 |
| 113 chooser.putExtra(Intent.EXTRA_INTENT, getContentIntent); | 109 chooser.putExtra(Intent.EXTRA_INTENT, getContentIntent); |
| 114 | 110 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 } | 204 } |
| 209 | 205 |
| 210 private boolean shouldShowVideoTypes() { | 206 private boolean shouldShowVideoTypes() { |
| 211 return shouldShowTypes(ALL_VIDEO_TYPES, VIDEO_TYPE); | 207 return shouldShowTypes(ALL_VIDEO_TYPES, VIDEO_TYPE); |
| 212 } | 208 } |
| 213 | 209 |
| 214 private boolean shouldShowAudioTypes() { | 210 private boolean shouldShowAudioTypes() { |
| 215 return shouldShowTypes(ALL_AUDIO_TYPES, AUDIO_TYPE); | 211 return shouldShowTypes(ALL_AUDIO_TYPES, AUDIO_TYPE); |
| 216 } | 212 } |
| 217 | 213 |
| 214 private boolean acceptsSpecificType(String type) { | |
| 215 return mFileTypes.size() == 1 && mFileTypes.at(0).equals(type); | |
| 216 } | |
| 217 | |
| 218 private boolean captureCamera() { | 218 private boolean captureCamera() { |
| 219 return shouldShowImageTypes() && mCapture != null && mCapture.startsWith (CAPTURE_CAMERA); | 219 return mCapture && acceptsSpecificType(ALL_IMAGE_TYPES); |
| 220 } | 220 } |
| 221 | 221 |
| 222 private boolean captureCamcorder() { | 222 private boolean captureCamcorder() { |
| 223 return shouldShowVideoTypes() && mCapture != null && | 223 return mCapture && acceptsSpecificType(ALL_VIDEO_TYPES); |
| 224 mCapture.startsWith(CAPTURE_CAMCORDER); | |
| 225 } | 224 } |
| 226 | 225 |
| 227 private boolean captureMicrophone() { | 226 private boolean captureMicrophone() { |
| 228 return shouldShowAudioTypes() && mCapture != null && | 227 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 } | 228 } |
| 235 | 229 |
| 236 private boolean acceptSpecificType(String accept) { | 230 private boolean acceptSpecificType(String accept) { |
| 237 for (String type : mFileTypes) { | 231 for (String type : mFileTypes) { |
| 238 if (type.startsWith(accept)) { | 232 if (type.startsWith(accept)) { |
| 239 return true; | 233 return true; |
| 240 } | 234 } |
| 241 } | 235 } |
| 242 return false; | 236 return false; |
| 243 } | 237 } |
| 244 | 238 |
| 245 @CalledByNative | 239 @CalledByNative |
| 246 private static SelectFileDialog create(int nativeSelectFileDialog) { | 240 private static SelectFileDialog create(int nativeSelectFileDialog) { |
| 247 return new SelectFileDialog(nativeSelectFileDialog); | 241 return new SelectFileDialog(nativeSelectFileDialog); |
| 248 } | 242 } |
| 249 | 243 |
| 250 private native void nativeOnFileSelected(int nativeSelectFileDialogImpl, | 244 private native void nativeOnFileSelected(int nativeSelectFileDialogImpl, |
| 251 String filePath); | 245 String filePath); |
| 252 private native void nativeOnFileNotSelected(int nativeSelectFileDialogImpl); | 246 private native void nativeOnFileNotSelected(int nativeSelectFileDialogImpl); |
| 253 } | 247 } |
| OLD | NEW |