| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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.base; | 5 package org.chromium.ui.base; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.ClipData; |
| 8 import android.content.ContentResolver; | 9 import android.content.ContentResolver; |
| 9 import android.content.Intent; | 10 import android.content.Intent; |
| 10 import android.database.Cursor; | 11 import android.database.Cursor; |
| 11 import android.net.Uri; | 12 import android.net.Uri; |
| 13 import android.os.Build; |
| 12 import android.os.Environment; | 14 import android.os.Environment; |
| 13 import android.provider.MediaStore; | 15 import android.provider.MediaStore; |
| 14 import android.text.TextUtils; | 16 import android.text.TextUtils; |
| 15 | 17 |
| 16 import org.chromium.base.CalledByNative; | 18 import org.chromium.base.CalledByNative; |
| 17 import org.chromium.base.JNINamespace; | 19 import org.chromium.base.JNINamespace; |
| 18 import org.chromium.ui.R; | 20 import org.chromium.ui.R; |
| 19 | 21 |
| 20 import java.io.File; | 22 import java.io.File; |
| 21 import java.util.ArrayList; | 23 import java.util.ArrayList; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 43 private Uri mCameraOutputUri; | 45 private Uri mCameraOutputUri; |
| 44 | 46 |
| 45 private SelectFileDialog(long nativeSelectFileDialog) { | 47 private SelectFileDialog(long nativeSelectFileDialog) { |
| 46 mNativeSelectFileDialog = nativeSelectFileDialog; | 48 mNativeSelectFileDialog = nativeSelectFileDialog; |
| 47 } | 49 } |
| 48 | 50 |
| 49 /** | 51 /** |
| 50 * Creates and starts an intent based on the passed fileTypes and capture va
lue. | 52 * Creates and starts an intent based on the passed fileTypes and capture va
lue. |
| 51 * @param fileTypes MIME types requested (i.e. "image/*") | 53 * @param fileTypes MIME types requested (i.e. "image/*") |
| 52 * @param capture The capture value as described in http://www.w3.org/TR/htm
l-media-capture/ | 54 * @param capture The capture value as described in http://www.w3.org/TR/htm
l-media-capture/ |
| 55 * @param multiple Whether it should be possible to select multiple files. |
| 53 * @param window The WindowAndroid that can show intents | 56 * @param window The WindowAndroid that can show intents |
| 54 */ | 57 */ |
| 55 @CalledByNative | 58 @CalledByNative |
| 56 private void selectFile(String[] fileTypes, boolean capture, WindowAndroid w
indow) { | 59 private void selectFile( |
| 60 String[] fileTypes, boolean capture, boolean multiple, WindowAndroid
window) { |
| 57 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); | 61 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); |
| 58 mCapture = capture; | 62 mCapture = capture; |
| 59 | 63 |
| 60 Intent chooser = new Intent(Intent.ACTION_CHOOSER); | 64 Intent chooser = new Intent(Intent.ACTION_CHOOSER); |
| 61 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 65 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 62 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); | 66 mCameraOutputUri = Uri.fromFile(getFileForImageCapture()); |
| 63 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); | 67 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); |
| 64 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | 68 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 65 Intent soundRecorder = new Intent( | 69 Intent soundRecorder = new Intent( |
| 66 MediaStore.Audio.Media.RECORD_SOUND_ACTION); | 70 MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
| 67 | 71 |
| 68 // Quick check - if the |capture| parameter is set and |fileTypes| has t
he appropriate MIME | 72 // Quick check - if the |capture| parameter is set and |fileTypes| has t
he appropriate MIME |
| 69 // type, we should just launch the appropriate intent. Otherwise build u
p a chooser based on | 73 // type, we should just launch the appropriate intent. Otherwise build u
p a chooser based on |
| 70 // the accept type and then display that to the user. | 74 // the accept type and then display that to the user. |
| 71 if (captureCamera()) { | 75 if (captureCamera()) { |
| 72 if (window.showIntent(camera, this, R.string.low_memory_error)) retu
rn; | 76 if (window.showIntent(camera, this, R.string.low_memory_error)) retu
rn; |
| 73 } else if (captureCamcorder()) { | 77 } else if (captureCamcorder()) { |
| 74 if (window.showIntent(camcorder, this, R.string.low_memory_error)) r
eturn; | 78 if (window.showIntent(camcorder, this, R.string.low_memory_error)) r
eturn; |
| 75 } else if (captureMicrophone()) { | 79 } else if (captureMicrophone()) { |
| 76 if (window.showIntent(soundRecorder, this, R.string.low_memory_error
)) return; | 80 if (window.showIntent(soundRecorder, this, R.string.low_memory_error
)) return; |
| 77 } | 81 } |
| 78 | 82 |
| 79 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); | 83 Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 80 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); | 84 getContentIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| 85 |
| 86 if (multiple) |
| 87 getContentIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); |
| 88 |
| 81 ArrayList<Intent> extraIntents = new ArrayList<Intent>(); | 89 ArrayList<Intent> extraIntents = new ArrayList<Intent>(); |
| 82 if (!noSpecificType()) { | 90 if (!noSpecificType()) { |
| 83 // Create a chooser based on the accept type that was specified in t
he webpage. Note | 91 // Create a chooser based on the accept type that was specified in t
he webpage. Note |
| 84 // that if the web page specified multiple accept types, we will hav
e built a generic | 92 // that if the web page specified multiple accept types, we will hav
e built a generic |
| 85 // chooser above. | 93 // chooser above. |
| 86 if (shouldShowImageTypes()) { | 94 if (shouldShowImageTypes()) { |
| 87 extraIntents.add(camera); | 95 extraIntents.add(camera); |
| 88 getContentIntent.setType(ALL_IMAGE_TYPES); | 96 getContentIntent.setType(ALL_IMAGE_TYPES); |
| 89 } else if (shouldShowVideoTypes()) { | 97 } else if (shouldShowVideoTypes()) { |
| 90 extraIntents.add(camcorder); | 98 extraIntents.add(camcorder); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 nativeOnFileSelected(mNativeSelectFileDialog, mCameraOutputUri.getPa
th(), ""); | 184 nativeOnFileSelected(mNativeSelectFileDialog, mCameraOutputUri.getPa
th(), ""); |
| 177 | 185 |
| 178 // Broadcast to the media scanner that there's a new photo on the de
vice so it will | 186 // Broadcast to the media scanner that there's a new photo on the de
vice so it will |
| 179 // show up right away in the gallery (rather than waiting until the
next time the media | 187 // show up right away in the gallery (rather than waiting until the
next time the media |
| 180 // scanner runs). | 188 // scanner runs). |
| 181 window.sendBroadcast(new Intent( | 189 window.sendBroadcast(new Intent( |
| 182 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraOutputUri)); | 190 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraOutputUri)); |
| 183 return; | 191 return; |
| 184 } | 192 } |
| 185 | 193 |
| 194 // Path for when EXTRA_ALLOW_MULTIPLE Intent extra has been defined. Eac
h of the selected |
| 195 // files will be shared as an entry on the Intent's ClipData. This funct
ionality is only |
| 196 // available in Android JellyBean MR2 and higher. |
| 197 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && |
| 198 results.getData() == null && |
| 199 results.getClipData() != null) { |
| 200 ClipData clipData = results.getClipData(); |
| 201 |
| 202 int itemCount = clipData.getItemCount(); |
| 203 if (itemCount == 0) { |
| 204 onFileNotSelected(); |
| 205 return; |
| 206 } |
| 207 |
| 208 String[] filePathArray = new String[itemCount]; |
| 209 String[] displayNameArray = new String[itemCount]; |
| 210 |
| 211 for (int i = 0; i < itemCount; ++i) { |
| 212 final Uri uri = clipData.getItemAt(i).getUri(); |
| 213 |
| 214 filePathArray[i] = uri.toString(); |
| 215 displayNameArray[i] = resolveFileName(uri, contentResolver); |
| 216 } |
| 217 |
| 218 nativeOnMultipleFilesSelected(mNativeSelectFileDialog, |
| 219 filePathArray, displayNameArray); |
| 220 return; |
| 221 } |
| 222 |
| 186 if (ContentResolver.SCHEME_FILE.equals(results.getData().getScheme())) { | 223 if (ContentResolver.SCHEME_FILE.equals(results.getData().getScheme())) { |
| 187 nativeOnFileSelected(mNativeSelectFileDialog, | 224 nativeOnFileSelected(mNativeSelectFileDialog, |
| 188 results.getData().getSchemeSpecificPart(), ""); | 225 results.getData().getSchemeSpecificPart(), ""); |
| 189 return; | 226 return; |
| 190 } | 227 } |
| 191 | 228 |
| 192 if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) { | 229 if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) { |
| 193 nativeOnFileSelected(mNativeSelectFileDialog, | 230 nativeOnFileSelected(mNativeSelectFileDialog, |
| 194 results.getData().toString(), | 231 results.getData().toString(), |
| 195 resolveFileName(results.getData(), | 232 resolveFileName(results.getData(), |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 return false; | 292 return false; |
| 256 } | 293 } |
| 257 | 294 |
| 258 @CalledByNative | 295 @CalledByNative |
| 259 private static SelectFileDialog create(long nativeSelectFileDialog) { | 296 private static SelectFileDialog create(long nativeSelectFileDialog) { |
| 260 return new SelectFileDialog(nativeSelectFileDialog); | 297 return new SelectFileDialog(nativeSelectFileDialog); |
| 261 } | 298 } |
| 262 | 299 |
| 263 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, | 300 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, |
| 264 String filePath, String displayName); | 301 String filePath, String displayName); |
| 302 private native void nativeOnMultipleFilesSelected(long nativeSelectFileDialo
gImpl, |
| 303 String[] filePathArray, String[] displayNameArray); |
| 265 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; | 304 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; |
| 266 } | 305 } |
| OLD | NEW |