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.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.app.Activity; | 8 import android.app.Activity; |
9 import android.content.ClipData; | 9 import android.content.ClipData; |
10 import android.content.ContentResolver; | 10 import android.content.ContentResolver; |
11 import android.content.Context; | 11 import android.content.Context; |
12 import android.content.Intent; | 12 import android.content.Intent; |
13 import android.net.Uri; | 13 import android.net.Uri; |
14 import android.os.AsyncTask; | 14 import android.os.AsyncTask; |
15 import android.os.Build; | 15 import android.os.Build; |
16 import android.os.Environment; | |
17 import android.provider.MediaStore; | 16 import android.provider.MediaStore; |
18 import android.text.TextUtils; | 17 import android.text.TextUtils; |
19 import android.util.Log; | 18 import android.util.Log; |
20 | 19 |
21 import org.chromium.base.CalledByNative; | 20 import org.chromium.base.CalledByNative; |
22 import org.chromium.base.ContentUriUtils; | 21 import org.chromium.base.ContentUriUtils; |
23 import org.chromium.base.JNINamespace; | 22 import org.chromium.base.JNINamespace; |
24 import org.chromium.ui.R; | 23 import org.chromium.ui.R; |
| 24 import org.chromium.ui.UiUtils; |
25 | 25 |
26 import java.io.File; | 26 import java.io.File; |
27 import java.io.IOException; | 27 import java.io.IOException; |
28 import java.util.ArrayList; | 28 import java.util.ArrayList; |
29 import java.util.Arrays; | 29 import java.util.Arrays; |
30 import java.util.List; | 30 import java.util.List; |
31 | 31 |
32 /** | 32 /** |
33 * A dialog that is triggered from a file input field that allows a user to sele
ct a file based on | 33 * A dialog that is triggered from a file input field that allows a user to sele
ct a file based on |
34 * a set of accepted file types. The path of the selected file is passed to the
native dialog. | 34 * a set of accepted file types. The path of the selected file is passed to the
native dialog. |
35 */ | 35 */ |
36 @JNINamespace("ui") | 36 @JNINamespace("ui") |
37 class SelectFileDialog implements WindowAndroid.IntentCallback { | 37 class SelectFileDialog implements WindowAndroid.IntentCallback { |
38 private static final String TAG = "SelectFileDialog"; | 38 private static final String TAG = "SelectFileDialog"; |
39 private static final String IMAGE_TYPE = "image/"; | 39 private static final String IMAGE_TYPE = "image/"; |
40 private static final String VIDEO_TYPE = "video/"; | 40 private static final String VIDEO_TYPE = "video/"; |
41 private static final String AUDIO_TYPE = "audio/"; | 41 private static final String AUDIO_TYPE = "audio/"; |
42 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; | 42 private static final String ALL_IMAGE_TYPES = IMAGE_TYPE + "*"; |
43 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; | 43 private static final String ALL_VIDEO_TYPES = VIDEO_TYPE + "*"; |
44 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; | 44 private static final String ALL_AUDIO_TYPES = AUDIO_TYPE + "*"; |
45 private static final String ANY_TYPES = "*/*"; | 45 private static final String ANY_TYPES = "*/*"; |
46 private static final String CAPTURE_IMAGE_DIRECTORY = "browser-photos"; | |
47 // Keep this variable in sync with the value defined in file_paths.xml. | |
48 private static final String IMAGE_FILE_PATH = "images"; | |
49 | 46 |
50 private final long mNativeSelectFileDialog; | 47 private final long mNativeSelectFileDialog; |
51 private List<String> mFileTypes; | 48 private List<String> mFileTypes; |
52 private boolean mCapture; | 49 private boolean mCapture; |
53 private Uri mCameraOutputUri; | 50 private Uri mCameraOutputUri; |
54 | 51 |
55 private SelectFileDialog(long nativeSelectFileDialog) { | 52 private SelectFileDialog(long nativeSelectFileDialog) { |
56 mNativeSelectFileDialog = nativeSelectFileDialog; | 53 mNativeSelectFileDialog = nativeSelectFileDialog; |
57 } | 54 } |
58 | 55 |
(...skipping 10 matching lines...) Expand all Loading... |
69 String[] fileTypes, boolean capture, boolean multiple, WindowAndroid
window) { | 66 String[] fileTypes, boolean capture, boolean multiple, WindowAndroid
window) { |
70 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); | 67 mFileTypes = new ArrayList<String>(Arrays.asList(fileTypes)); |
71 mCapture = capture; | 68 mCapture = capture; |
72 | 69 |
73 Intent chooser = new Intent(Intent.ACTION_CHOOSER); | 70 Intent chooser = new Intent(Intent.ACTION_CHOOSER); |
74 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 71 Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
75 Context context = window.getApplicationContext(); | 72 Context context = window.getApplicationContext(); |
76 camera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | 73 camera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
77 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | 74 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
78 try { | 75 try { |
79 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 76 mCameraOutputUri = |
80 mCameraOutputUri = ContentUriUtils.getContentUriFromFile( | 77 UiUtils.getUriForImageCaptureFile(context, getFileForImageCa
pture(context)); |
81 context, getFileForImageCapture(context)); | |
82 } else { | |
83 mCameraOutputUri = Uri.fromFile(getFileForImageCapture(context))
; | |
84 } | |
85 } catch (IOException e) { | 78 } catch (IOException e) { |
86 Log.e(TAG, "Cannot retrieve content uri from file", e); | 79 Log.e(TAG, "Cannot retrieve content uri from file", e); |
87 } | 80 } |
88 | 81 |
89 if (mCameraOutputUri == null) { | 82 if (mCameraOutputUri == null) { |
90 onFileNotSelected(); | 83 onFileNotSelected(); |
91 return; | 84 return; |
92 } | 85 } |
93 | 86 |
94 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); | 87 camera.putExtra(MediaStore.EXTRA_OUTPUT, mCameraOutputUri); |
95 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 88 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { |
96 camera.setClipData( | 89 camera.setClipData(ClipData.newUri( |
97 ClipData.newUri(context.getContentResolver(), | 90 context.getContentResolver(), UiUtils.IMAGE_FILE_PATH, mCame
raOutputUri)); |
98 IMAGE_FILE_PATH, mCameraOutputUri)); | |
99 } | 91 } |
100 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); | 92 Intent camcorder = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
101 Intent soundRecorder = new Intent( | 93 Intent soundRecorder = new Intent( |
102 MediaStore.Audio.Media.RECORD_SOUND_ACTION); | 94 MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
103 | 95 |
104 // Quick check - if the |capture| parameter is set and |fileTypes| has t
he appropriate MIME | 96 // Quick check - if the |capture| parameter is set and |fileTypes| has t
he appropriate MIME |
105 // type, we should just launch the appropriate intent. Otherwise build u
p a chooser based on | 97 // type, we should just launch the appropriate intent. Otherwise build u
p a chooser based on |
106 // the accept type and then display that to the user. | 98 // the accept type and then display that to the user. |
107 if (captureCamera()) { | 99 if (captureCamera()) { |
108 if (window.showIntent(camera, this, R.string.low_memory_error)) retu
rn; | 100 if (window.showIntent(camera, this, R.string.low_memory_error)) retu
rn; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 | 147 |
156 /** | 148 /** |
157 * Get a file for the image capture operation. For devices with JB MR2 or | 149 * Get a file for the image capture operation. For devices with JB MR2 or |
158 * latter android versions, the file is put under IMAGE_FILE_PATH directory. | 150 * latter android versions, the file is put under IMAGE_FILE_PATH directory. |
159 * For ICS devices, the file is put under CAPTURE_IMAGE_DIRECTORY. | 151 * For ICS devices, the file is put under CAPTURE_IMAGE_DIRECTORY. |
160 * | 152 * |
161 * @param context The application context. | 153 * @param context The application context. |
162 * @return file path for the captured image to be stored. | 154 * @return file path for the captured image to be stored. |
163 */ | 155 */ |
164 private File getFileForImageCapture(Context context) throws IOException { | 156 private File getFileForImageCapture(Context context) throws IOException { |
165 File path; | 157 File photoFile = File.createTempFile(String.valueOf(System.currentTimeMi
llis()), ".jpg", |
166 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | 158 UiUtils.getDirectoryForImageCapture(context)); |
167 path = new File(context.getFilesDir(), IMAGE_FILE_PATH); | |
168 if (!path.exists() && !path.mkdir()) { | |
169 throw new IOException("Folder cannot be created."); | |
170 } | |
171 } else { | |
172 File externalDataDir = Environment.getExternalStoragePublicDirectory
( | |
173 Environment.DIRECTORY_DCIM); | |
174 path = new File(externalDataDir.getAbsolutePath() | |
175 + File.separator + CAPTURE_IMAGE_DIRECTORY); | |
176 if (!path.exists() && !path.mkdirs()) { | |
177 path = externalDataDir; | |
178 } | |
179 } | |
180 File photoFile = File.createTempFile( | |
181 String.valueOf(System.currentTimeMillis()), ".jpg", path); | |
182 return photoFile; | 159 return photoFile; |
183 } | 160 } |
184 | 161 |
185 /** | 162 /** |
186 * Callback method to handle the intent results and pass on the path to the
native | 163 * Callback method to handle the intent results and pass on the path to the
native |
187 * SelectFileDialog. | 164 * SelectFileDialog. |
188 * @param window The window that has access to the application activity. | 165 * @param window The window that has access to the application activity. |
189 * @param resultCode The result code whether the intent returned successfull
y. | 166 * @param resultCode The result code whether the intent returned successfull
y. |
190 * @param contentResolver The content resolver used to extract the path of t
he selected file. | 167 * @param contentResolver The content resolver used to extract the path of t
he selected file. |
191 * @param results The results of the requested intent. | 168 * @param results The results of the requested intent. |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 private static SelectFileDialog create(long nativeSelectFileDialog) { | 338 private static SelectFileDialog create(long nativeSelectFileDialog) { |
362 return new SelectFileDialog(nativeSelectFileDialog); | 339 return new SelectFileDialog(nativeSelectFileDialog); |
363 } | 340 } |
364 | 341 |
365 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, | 342 private native void nativeOnFileSelected(long nativeSelectFileDialogImpl, |
366 String filePath, String displayName); | 343 String filePath, String displayName); |
367 private native void nativeOnMultipleFilesSelected(long nativeSelectFileDialo
gImpl, | 344 private native void nativeOnMultipleFilesSelected(long nativeSelectFileDialo
gImpl, |
368 String[] filePathArray, String[] displayNameArray); | 345 String[] filePathArray, String[] displayNameArray); |
369 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; | 346 private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl)
; |
370 } | 347 } |
OLD | NEW |