| 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.Manifest; | 7 import android.Manifest; |
| 8 import android.annotation.SuppressLint; | 8 import android.annotation.SuppressLint; |
| 9 import android.annotation.TargetApi; | 9 import android.annotation.TargetApi; |
| 10 import android.app.Activity; | 10 import android.app.Activity; |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 /** | 165 /** |
| 166 * Called to launch an intent to allow user to select files. | 166 * Called to launch an intent to allow user to select files. |
| 167 */ | 167 */ |
| 168 private void launchSelectFileIntent() { | 168 private void launchSelectFileIntent() { |
| 169 boolean hasCameraPermission = mWindowAndroid.hasPermission(Manifest.perm
ission.CAMERA); | 169 boolean hasCameraPermission = mWindowAndroid.hasPermission(Manifest.perm
ission.CAMERA); |
| 170 if (mSupportsImageCapture && hasCameraPermission) { | 170 if (mSupportsImageCapture && hasCameraPermission) { |
| 171 // GetCameraIntentTask will call LaunchSelectFileWithCameraIntent la
ter. | 171 // GetCameraIntentTask will call LaunchSelectFileWithCameraIntent la
ter. |
| 172 new GetCameraIntentTask().executeOnExecutor(AsyncTask.THREAD_POOL_EX
ECUTOR); | 172 new GetCameraIntentTask(false, mWindowAndroid, this) |
| 173 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 173 } else { | 174 } else { |
| 174 launchSelectFileWithCameraIntent(hasCameraPermission, null); | 175 launchSelectFileWithCameraIntent(hasCameraPermission, null); |
| 175 } | 176 } |
| 176 } | 177 } |
| 177 | 178 |
| 178 /** | 179 /** |
| 179 * Called to launch an intent to allow user to select files. If |camera| is
null, | 180 * Called to launch an intent to allow user to select files. If |camera| is
null, |
| 180 * the select file dialog shouldn't include any files from the camera. Other
wise, user | 181 * the select file dialog shouldn't include any files from the camera. Other
wise, user |
| 181 * is allowed to choose files from the camera. | 182 * is allowed to choose files from the camera. |
| 182 * @param hasCameraPermission Whether accessing camera is allowed. | 183 * @param hasCameraPermission Whether accessing camera is allowed. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 @Override | 266 @Override |
| 266 public void onPickerUserAction(Action action, String[] photos) { | 267 public void onPickerUserAction(Action action, String[] photos) { |
| 267 UiUtils.dismissPhotoPicker(); | 268 UiUtils.dismissPhotoPicker(); |
| 268 | 269 |
| 269 switch (action) { | 270 switch (action) { |
| 270 case CANCEL: | 271 case CANCEL: |
| 271 onFileNotSelected(); | 272 onFileNotSelected(); |
| 272 break; | 273 break; |
| 273 | 274 |
| 274 case PHOTOS_SELECTED: | 275 case PHOTOS_SELECTED: |
| 275 // TODO(finnur): Implement. | 276 if (photos.length == 0) { |
| 276 onFileNotSelected(); | 277 onFileNotSelected(); |
| 278 return; |
| 279 } |
| 280 |
| 281 if (photos.length == 1) { |
| 282 GetDisplayNameTask task = |
| 283 new GetDisplayNameTask(ContextUtils.getApplicationCo
ntext(), false); |
| 284 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Uri.p
arse(photos[0])); |
| 285 return; |
| 286 } else { |
| 287 Uri[] filePathArray = new Uri[photos.length]; |
| 288 for (int i = 0; i < photos.length; ++i) { |
| 289 filePathArray[i] = Uri.parse(photos[i]); |
| 290 } |
| 291 GetDisplayNameTask task = |
| 292 new GetDisplayNameTask(ContextUtils.getApplicationCo
ntext(), true); |
| 293 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, fileP
athArray); |
| 294 } |
| 277 break; | 295 break; |
| 278 | 296 |
| 279 case LAUNCH_GALLERY: | 297 case LAUNCH_GALLERY: |
| 280 // TODO(finnur): Implement. | 298 Intent intent = new Intent(); |
| 281 onFileNotSelected(); | 299 intent.setType("image/*"); |
| 300 if (mAllowMultiple) intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,
true); |
| 301 intent.setAction(Intent.ACTION_GET_CONTENT); |
| 302 Activity activity = mWindowAndroid.getActivity().get(); |
| 303 if (activity != null) { |
| 304 String label = |
| 305 activity.getResources().getString(R.string.photo_pic
ker_select_images); |
| 306 activity.startActivityForResult( |
| 307 Intent.createChooser(intent, label), PhotoPickerList
ener.SHOW_GALLERY); |
| 308 } |
| 282 break; | 309 break; |
| 283 | 310 |
| 284 case LAUNCH_CAMERA: | 311 case LAUNCH_CAMERA: |
| 285 // TODO(finnur): Implement. | 312 new GetCameraIntentTask(true, mWindowAndroid, this) |
| 286 onFileNotSelected(); | 313 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 287 break; | 314 break; |
| 288 } | 315 } |
| 289 } | 316 } |
| 290 | 317 |
| 291 @Override | 318 @Override |
| 292 public Map<String, Long> getFilesForTesting() { | 319 public Map<String, Long> getFilesForTesting() { |
| 293 return null; | 320 return null; |
| 294 } | 321 } |
| 295 | 322 |
| 296 private class GetCameraIntentTask extends AsyncTask<Void, Void, Uri> { | 323 private class GetCameraIntentTask extends AsyncTask<Void, Void, Uri> { |
| 324 private Boolean mDirectToCamera; |
| 325 private WindowAndroid mWindow; |
| 326 private WindowAndroid.IntentCallback mCallback; |
| 327 |
| 328 public GetCameraIntentTask(Boolean directToCamera, WindowAndroid window, |
| 329 WindowAndroid.IntentCallback callback) { |
| 330 mDirectToCamera = directToCamera; |
| 331 mWindow = window; |
| 332 mCallback = callback; |
| 333 } |
| 334 |
| 297 @Override | 335 @Override |
| 298 public Uri doInBackground(Void...voids) { | 336 public Uri doInBackground(Void...voids) { |
| 299 try { | 337 try { |
| 300 Context context = mWindowAndroid.getApplicationContext(); | 338 Context context = mWindowAndroid.getApplicationContext(); |
| 301 return ApiCompatibilityUtils.getUriForImageCaptureFile( | 339 return ApiCompatibilityUtils.getUriForImageCaptureFile( |
| 302 getFileForImageCapture(context)); | 340 getFileForImageCapture(context)); |
| 303 } catch (IOException e) { | 341 } catch (IOException e) { |
| 304 Log.e(TAG, "Cannot retrieve content uri from file", e); | 342 Log.e(TAG, "Cannot retrieve content uri from file", e); |
| 305 return null; | 343 return null; |
| 306 } | 344 } |
| 307 } | 345 } |
| 308 | 346 |
| 309 @Override | 347 @Override |
| 310 protected void onPostExecute(Uri result) { | 348 protected void onPostExecute(Uri result) { |
| 311 mCameraOutputUri = result; | 349 mCameraOutputUri = result; |
| 312 if (mCameraOutputUri == null && captureCamera()) { | 350 if (mCameraOutputUri == null && captureCamera()) { |
| 313 onFileNotSelected(); | 351 onFileNotSelected(); |
| 314 return; | 352 return; |
| 315 } | 353 } |
| 316 | 354 |
| 317 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 355 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 318 camera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | 356 camera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 319 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | 357 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 320 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); | 358 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); |
| 321 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 359 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
| 322 camera.setClipData(ClipData.newUri( | 360 camera.setClipData(ClipData.newUri( |
| 323 mWindowAndroid.getApplicationContext().getContentResolve
r(), | 361 mWindowAndroid.getApplicationContext().getContentResolve
r(), |
| 324 UiUtils.IMAGE_FILE_PATH, mCameraOutputUri)); | 362 UiUtils.IMAGE_FILE_PATH, mCameraOutputUri)); |
| 325 } | 363 } |
| 326 launchSelectFileWithCameraIntent(true, camera); | 364 if (mDirectToCamera) { |
| 365 mWindow.showIntent(camera, mCallback, R.string.low_memory_error)
; |
| 366 } else { |
| 367 launchSelectFileWithCameraIntent(true, camera); |
| 368 } |
| 327 } | 369 } |
| 328 } | 370 } |
| 329 | 371 |
| 330 /** | 372 /** |
| 331 * Get a file for the image capture operation. For devices with JB MR2 or | 373 * Get a file for the image capture operation. For devices with JB MR2 or |
| 332 * latter android versions, the file is put under IMAGE_FILE_PATH directory. | 374 * latter android versions, the file is put under IMAGE_FILE_PATH directory. |
| 333 * For ICS devices, the file is put under CAPTURE_IMAGE_DIRECTORY. | 375 * For ICS devices, the file is put under CAPTURE_IMAGE_DIRECTORY. |
| 334 * | 376 * |
| 335 * @param context The application context. | 377 * @param context The application context. |
| 336 * @return file path for the captured image to be stored. | 378 * @return file path for the captured image to be stored. |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 static SelectFileDialog create(long nativeSelectFileDialog) { | 622 static SelectFileDialog create(long nativeSelectFileDialog) { |
| 581 return new SelectFileDialog(nativeSelectFileDialog); | 623 return new SelectFileDialog(nativeSelectFileDialog); |
| 582 } | 624 } |
| 583 | 625 |
| 584 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, | 626 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, |
| 585 String filePath, String displayName); | 627 String filePath, String displayName); |
| 586 private native void nativeOnMultipleFilesSelected(long nativeSelectFileDialo
gImpl, | 628 private native void nativeOnMultipleFilesSelected(long nativeSelectFileDialo
gImpl, |
| 587 String[] filePathArray, String[] displayNameArray); | 629 String[] filePathArray, String[] displayNameArray); |
| 588 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; | 630 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; |
| 589 } | 631 } |
| OLD | NEW |