Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 20 | import android.content.ActivityNotFoundException; |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 21 | import android.content.ComponentName; |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 23 | import android.content.ContentUris; |
| 24 | import android.content.Context; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.pm.ActivityInfo; |
kaiyiz | 4b59d26 | 2013-07-30 10:41:38 +0800 | [diff] [blame] | 29 | import android.database.Cursor; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
| 31 | import android.os.Environment; |
| 32 | import android.provider.MediaStore; |
| 33 | import android.webkit.ValueCallback; |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 35 | import android.widget.Toast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 36 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 37 | import com.android.browser.R; |
| 38 | import com.android.browser.reflect.ReflectHelper; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 39 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 40 | import java.io.File; |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 41 | import java.util.ArrayList; |
| 42 | import java.util.List; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 43 | import java.util.Vector; |
| 44 | |
| 45 | /** |
| 46 | * Handle the file upload callbacks from WebView here |
| 47 | */ |
| 48 | public class UploadHandler { |
| 49 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 50 | private static final String TAG = "UploadHandler"; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 51 | /* |
| 52 | * The Object used to inform the WebView of the file to upload. |
| 53 | */ |
| 54 | private ValueCallback<Uri> mUploadMessage; |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 55 | private ValueCallback<String[]> mUploadFilePaths; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 56 | private String mCameraFilePath; |
| 57 | |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 58 | private boolean mHandled; |
| 59 | private boolean mCaughtActivityNotFoundException; |
| 60 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 61 | private Controller mController; |
| 62 | |
| 63 | public UploadHandler(Controller controller) { |
| 64 | mController = controller; |
| 65 | } |
| 66 | |
| 67 | String getFilePath() { |
| 68 | return mCameraFilePath; |
| 69 | } |
| 70 | |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 71 | protected boolean handled() { |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 72 | return mHandled; |
| 73 | } |
| 74 | |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 75 | protected void setHandled(boolean handled) { |
| 76 | mHandled = handled; |
| 77 | mCaughtActivityNotFoundException = false; |
| 78 | if (!mHandled) |
| 79 | mUploadFilePaths.onReceiveValue(null); |
| 80 | } |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 81 | |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 82 | void onResult(int resultCode, Intent intent) { |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 83 | if (resultCode == Activity.RESULT_CANCELED && mCaughtActivityNotFoundException) { |
| 84 | // Couldn't resolve an activity, we are going to try again so skip |
| 85 | // this result. |
| 86 | mCaughtActivityNotFoundException = false; |
| 87 | return; |
| 88 | } |
| 89 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 90 | Uri result = intent == null || resultCode != Activity.RESULT_OK ? null |
| 91 | : intent.getData(); |
| 92 | |
| 93 | // As we ask the camera to save the result of the user taking |
| 94 | // a picture, the camera application does not return anything other |
| 95 | // than RESULT_OK. So we need to check whether the file we expected |
| 96 | // was written to disk in the in the case that we |
| 97 | // did not get an intent returned but did get a RESULT_OK. If it was, |
| 98 | // we assume that this result has came back from the camera. |
| 99 | if (result == null && intent == null && resultCode == Activity.RESULT_OK) { |
| 100 | File cameraFile = new File(mCameraFilePath); |
| 101 | if (cameraFile.exists()) { |
| 102 | result = Uri.fromFile(cameraFile); |
| 103 | // Broadcast to the media scanner that we have a new photo |
| 104 | // so it will be added into the gallery for the user. |
| 105 | mController.getActivity().sendBroadcast( |
| 106 | new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result)); |
| 107 | } |
| 108 | } |
| 109 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 110 | // try to get local file path from uri |
| 111 | boolean hasGoodFilePath = false; |
| 112 | String filePath = null; |
| 113 | if (result != null) { |
| 114 | String scheme = result.getScheme(); |
| 115 | if ("file".equals(scheme)) { |
| 116 | filePath = result.getPath(); |
| 117 | hasGoodFilePath = filePath != null && !filePath.isEmpty(); |
| 118 | } else if ("content".equals(scheme)) { |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 119 | filePath = getFilePath(mController.getContext(), result); |
| 120 | hasGoodFilePath = filePath != null && !filePath.isEmpty(); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | // Add for carrier feature - prevent uploading DRM type files. |
Bijan Amirzada | e75909d | 2014-05-06 14:18:54 -0700 | [diff] [blame] | 125 | boolean drmUploadEnabled = mController.getContext().getResources().getBoolean( |
| 126 | R.bool.drm_uploads); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 127 | boolean isDRMFileType = false; |
Bijan Amirzada | e75909d | 2014-05-06 14:18:54 -0700 | [diff] [blame] | 128 | if (drmUploadEnabled && filePath != null |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 129 | && (filePath.endsWith(".fl") || filePath.endsWith(".dm") |
| 130 | || filePath.endsWith(".dcf") || filePath.endsWith(".dr") |
| 131 | || filePath.endsWith(".dd"))) { |
| 132 | isDRMFileType = true; |
| 133 | Toast.makeText(mController.getContext(), R.string.drm_file_unsupported, |
| 134 | Toast.LENGTH_LONG).show(); |
| 135 | } |
| 136 | |
| 137 | if (mUploadMessage != null) { |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 138 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 139 | if (!isDRMFileType) { |
| 140 | mUploadMessage.onReceiveValue(result); |
| 141 | } else { |
| 142 | mUploadMessage.onReceiveValue(null); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (mUploadFilePaths != null) { |
| 147 | if (hasGoodFilePath && !isDRMFileType) { |
| 148 | Log.d(TAG, "upload file path:" + filePath); |
| 149 | mUploadFilePaths.onReceiveValue(new String[]{filePath}); |
| 150 | } else { |
| 151 | mUploadFilePaths.onReceiveValue(null); |
| 152 | } |
kaiyiz | 4b59d26 | 2013-07-30 10:41:38 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 155 | mHandled = true; |
| 156 | mCaughtActivityNotFoundException = false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 159 | |
| 160 | public String getDocumentId(final Uri uri) { |
| 161 | String id = null; |
| 162 | try { |
| 163 | Object[] params = {(android.net.Uri)uri}; |
| 164 | Class[] type = new Class[] {Class.forName("android.net.Uri") }; |
| 165 | id = (String) ReflectHelper.invokeMethod( |
| 166 | "android.provider.DocumentsContract","getDocumentId", |
| 167 | type, params); |
| 168 | |
| 169 | } catch(java.lang.ClassNotFoundException e) { |
| 170 | |
| 171 | } |
| 172 | return id; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public String getFilePath(final Context context, final Uri uri) { |
| 177 | String id = getDocumentId(uri); |
| 178 | |
| 179 | // DocumentProvider is new API exposed in Kitkat |
| 180 | // Its a way of exposing unified file explorer |
| 181 | if (id != null) { |
| 182 | // ExternalStorageProvider |
| 183 | if (isExternalStorageDocument(uri)) { |
| 184 | final String docId = id; |
| 185 | final String[] split = docId.split(":"); |
| 186 | final String type = split[0]; |
| 187 | |
| 188 | if ("primary".equalsIgnoreCase(type)) { |
| 189 | return Environment.getExternalStorageDirectory() + "/" + split[1]; |
| 190 | } |
| 191 | } |
| 192 | // DownloadsProvider |
| 193 | else if (isDownloadsDocument(uri)) { |
| 194 | final Uri contentUri = ContentUris.withAppendedId( |
| 195 | Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); |
| 196 | return getDataColumn(context, contentUri, null, null); |
| 197 | } |
| 198 | // MediaProvider |
| 199 | else if (isMediaDocument(uri)) { |
| 200 | final String docId = id; |
| 201 | final String[] split = docId.split(":"); |
| 202 | final String type = split[0]; |
| 203 | |
| 204 | Uri contentUri = null; |
| 205 | if ("image".equals(type)) { |
| 206 | contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 207 | } else if ("video".equals(type)) { |
| 208 | contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 209 | } else if ("audio".equals(type)) { |
| 210 | contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 211 | } |
| 212 | |
| 213 | final String selection = "_id=?"; |
| 214 | final String[] selectionArgs = new String[] { |
| 215 | split[1] |
| 216 | }; |
| 217 | |
| 218 | return getDataColumn(context, contentUri, selection, selectionArgs); |
| 219 | } |
| 220 | } |
| 221 | // MediaStore (and general) |
| 222 | else if ("content".equalsIgnoreCase(uri.getScheme())) { |
| 223 | return getDataColumn(context, uri, null, null); |
| 224 | } |
| 225 | |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get the value of the data column for this Uri. This is useful for |
| 231 | * MediaStore Uris, and other file-based ContentProviders. |
| 232 | * @return The value of the _data column, which is typically a file path. |
| 233 | */ |
| 234 | private String getDataColumn(Context context, Uri uri, String selection, |
| 235 | String[] selectionArgs) { |
| 236 | |
| 237 | Cursor cursor = null; |
| 238 | final String column = "_data"; |
| 239 | final String[] projection = { column }; |
| 240 | |
| 241 | try { |
| 242 | cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, |
| 243 | null); |
| 244 | if (cursor != null && cursor.moveToFirst()) { |
| 245 | final int column_index = cursor.getColumnIndexOrThrow(column); |
| 246 | return cursor.getString(column_index); |
| 247 | } |
| 248 | } finally { |
| 249 | if (cursor != null) |
| 250 | cursor.close(); |
| 251 | } |
| 252 | return null; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @return Whether the Uri authority is ExternalStorageProvider. |
| 257 | */ |
| 258 | private boolean isExternalStorageDocument(Uri uri) { |
| 259 | return "com.android.externalstorage.documents".equals(uri.getAuthority()); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @return Whether the Uri authority is DownloadsProvider. |
| 264 | */ |
| 265 | private boolean isDownloadsDocument(Uri uri) { |
| 266 | return "com.android.providers.downloads.documents".equals(uri.getAuthority()); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * @return Whether the Uri authority is MediaProvider. |
| 271 | */ |
| 272 | public static boolean isMediaDocument(Uri uri) { |
| 273 | return "com.android.providers.media.documents".equals(uri.getAuthority()); |
| 274 | } |
| 275 | |
| 276 | |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 277 | void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 278 | |
| 279 | final String imageMimeType = "image/*"; |
| 280 | final String videoMimeType = "video/*"; |
| 281 | final String audioMimeType = "audio/*"; |
| 282 | final String mediaSourceKey = "capture"; |
| 283 | final String mediaSourceValueCamera = "camera"; |
| 284 | final String mediaSourceValueFileSystem = "filesystem"; |
| 285 | final String mediaSourceValueCamcorder = "camcorder"; |
| 286 | final String mediaSourceValueMicrophone = "microphone"; |
| 287 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 288 | // According to the spec, media source can be 'filesystem' or 'camera' or 'camcorder' |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 289 | // or 'microphone' and the default value should be 'filesystem'. |
| 290 | String mediaSource = mediaSourceValueFileSystem; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 291 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 292 | if (mUploadMessage != null) { |
| 293 | // Already a file picker operation in progress. |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | mUploadMessage = uploadMsg; |
| 298 | |
| 299 | // Parse the accept type. |
| 300 | String params[] = acceptType.split(";"); |
| 301 | String mimeType = params[0]; |
| 302 | |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 303 | if (capture.length() > 0) { |
| 304 | mediaSource = capture; |
| 305 | } |
| 306 | |
| 307 | if (capture.equals(mediaSourceValueFileSystem)) { |
| 308 | // To maintain backwards compatibility with the previous implementation |
| 309 | // of the media capture API, if the value of the 'capture' attribute is |
| 310 | // "filesystem", we should examine the accept-type for a MIME type that |
| 311 | // may specify a different capture value. |
| 312 | for (String p : params) { |
| 313 | String[] keyValue = p.split("="); |
| 314 | if (keyValue.length == 2) { |
| 315 | // Process key=value parameters. |
| 316 | if (mediaSourceKey.equals(keyValue[0])) { |
| 317 | mediaSource = keyValue[1]; |
| 318 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 323 | //Ensure it is not still set from a previous upload. |
| 324 | mCameraFilePath = null; |
| 325 | |
| 326 | if (mimeType.equals(imageMimeType)) { |
| 327 | if (mediaSource.equals(mediaSourceValueCamera)) { |
| 328 | // Specified 'image/*' and requested the camera, so go ahead and launch the |
| 329 | // camera directly. |
| 330 | startActivity(createCameraIntent()); |
| 331 | return; |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 332 | } else { |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 333 | // Specified just 'image/*', capture=filesystem, or an invalid capture parameter. |
| 334 | // In all these cases we show a traditional picker filetered on accept type |
| 335 | // so launch an intent for both the Camera and image/* OPENABLE. |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 336 | Intent chooser = createChooserIntent(createCameraIntent()); |
| 337 | chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(imageMimeType)); |
| 338 | startActivity(chooser); |
| 339 | return; |
| 340 | } |
| 341 | } else if (mimeType.equals(videoMimeType)) { |
| 342 | if (mediaSource.equals(mediaSourceValueCamcorder)) { |
| 343 | // Specified 'video/*' and requested the camcorder, so go ahead and launch the |
| 344 | // camcorder directly. |
| 345 | startActivity(createCamcorderIntent()); |
| 346 | return; |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 347 | } else { |
| 348 | // Specified just 'video/*', capture=filesystem or an invalid capture parameter. |
| 349 | // In all these cases we show an intent for the traditional file picker, filtered |
| 350 | // on accept type so launch an intent for both camcorder and video/* OPENABLE. |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 351 | Intent chooser = createChooserIntent(createCamcorderIntent()); |
| 352 | chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(videoMimeType)); |
| 353 | startActivity(chooser); |
| 354 | return; |
| 355 | } |
| 356 | } else if (mimeType.equals(audioMimeType)) { |
| 357 | if (mediaSource.equals(mediaSourceValueMicrophone)) { |
| 358 | // Specified 'audio/*' and requested microphone, so go ahead and launch the sound |
| 359 | // recorder. |
| 360 | startActivity(createSoundRecorderIntent()); |
| 361 | return; |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 362 | } else { |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 363 | // Specified just 'audio/*', capture=filesystem of an invalid capture parameter. |
| 364 | // In all these cases so go ahead and launch an intent for both the sound |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 365 | // recorder and audio/* OPENABLE. |
| 366 | Intent chooser = createChooserIntent(createSoundRecorderIntent()); |
| 367 | chooser.putExtra(Intent.EXTRA_INTENT, createOpenableIntent(audioMimeType)); |
| 368 | startActivity(chooser); |
| 369 | return; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // No special handling based on the accept type was necessary, so trigger the default |
| 374 | // file upload chooser. |
| 375 | startActivity(createDefaultOpenableIntent()); |
| 376 | } |
| 377 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 378 | void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes, |
| 379 | boolean capture) { |
| 380 | |
| 381 | final String imageMimeType = "image/*"; |
| 382 | final String videoMimeType = "video/*"; |
| 383 | final String audioMimeType = "audio/*"; |
| 384 | |
| 385 | if (mUploadFilePaths != null) { |
| 386 | // Already a file picker operation in progress. |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | mUploadFilePaths = uploadFilePaths; |
| 391 | |
| 392 | // Parse the accept type. |
| 393 | String params[] = acceptTypes.split(";"); |
| 394 | String mimeType = params[0]; |
| 395 | |
| 396 | // Ensure it is not still set from a previous upload. |
| 397 | mCameraFilePath = null; |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 398 | List<Intent> intentList = new ArrayList<Intent>(); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 399 | |
| 400 | if (mimeType.equals(imageMimeType)) { |
| 401 | if (capture) { |
| 402 | // Specified 'image/*' and capture=true, so go ahead and launch the |
| 403 | // camera directly. |
| 404 | startActivity(createCameraIntent()); |
| 405 | return; |
| 406 | } else { |
| 407 | // Specified just 'image/*', capture=false, or no capture value. |
| 408 | // In all these cases we show a traditional picker filetered on accept type |
| 409 | // so launch an intent for both the Camera and image/* OPENABLE. |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 410 | intentList.add(createCameraIntent()); |
| 411 | createUploadDialog(imageMimeType, intentList); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | } else if (mimeType.equals(videoMimeType)) { |
| 415 | if (capture) { |
| 416 | // Specified 'video/*' and capture=true, so go ahead and launch the |
| 417 | // camcorder directly. |
| 418 | startActivity(createCamcorderIntent()); |
| 419 | return; |
| 420 | } else { |
| 421 | // Specified just 'video/*', capture=false, or no capture value. |
| 422 | // In all these cases we show an intent for the traditional file picker, filtered |
| 423 | // on accept type so launch an intent for both camcorder and video/* OPENABLE. |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 424 | intentList.add(createCamcorderIntent()); |
| 425 | createUploadDialog(videoMimeType, intentList); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | } else if (mimeType.equals(audioMimeType)) { |
| 429 | if (capture) { |
| 430 | // Specified 'audio/*' and capture=true, so go ahead and launch the sound |
| 431 | // recorder. |
| 432 | startActivity(createSoundRecorderIntent()); |
| 433 | return; |
| 434 | } else { |
| 435 | // Specified just 'audio/*', capture=false, or no capture value. |
| 436 | // In all these cases so go ahead and launch an intent for both the sound |
| 437 | // recorder and audio/* OPENABLE. |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 438 | intentList.add(createSoundRecorderIntent()); |
| 439 | createUploadDialog(audioMimeType, intentList); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // No special handling based on the accept type was necessary, so trigger the default |
| 445 | // file upload chooser. |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 446 | createUploadDialog("*/*", null); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 450 | private void startActivity(Intent intent) { |
| 451 | try { |
| 452 | mController.getActivity().startActivityForResult(intent, Controller.FILE_SELECTED); |
| 453 | } catch (ActivityNotFoundException e) { |
| 454 | // No installed app was able to handle the intent that |
| 455 | // we sent, so fallback to the default file upload control. |
| 456 | try { |
Ben Murdoch | 51f6a2f | 2011-02-21 12:27:07 +0000 | [diff] [blame] | 457 | mCaughtActivityNotFoundException = true; |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 458 | mController.getActivity().startActivityForResult(createDefaultOpenableIntent(), |
| 459 | Controller.FILE_SELECTED); |
| 460 | } catch (ActivityNotFoundException e2) { |
| 461 | // Nothing can return us a file, so file upload is effectively disabled. |
| 462 | Toast.makeText(mController.getActivity(), R.string.uploads_disabled, |
| 463 | Toast.LENGTH_LONG).show(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | private Intent createDefaultOpenableIntent() { |
| 469 | // Create and return a chooser with the default OPENABLE |
| 470 | // actions including the camera, camcorder and sound |
| 471 | // recorder where available. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 472 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 473 | i.addCategory(Intent.CATEGORY_OPENABLE); |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 474 | i.setType("*/*"); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 475 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 476 | Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(), |
| 477 | createSoundRecorderIntent()); |
| 478 | chooser.putExtra(Intent.EXTRA_INTENT, i); |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 479 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 480 | return chooser; |
| 481 | } |
| 482 | |
Axesh R. Ajmera | 3aae101 | 2014-07-03 16:37:53 -0700 | [diff] [blame^] | 483 | |
| 484 | private void createUploadDialog(String openableMimeType, List<Intent> intentList) { |
| 485 | |
| 486 | Intent openable = new Intent(Intent.ACTION_GET_CONTENT); |
| 487 | openable.addCategory(Intent.CATEGORY_OPENABLE); |
| 488 | openable.setType(openableMimeType); |
| 489 | |
| 490 | if (openableMimeType.equals("*/*") && intentList == null) { |
| 491 | intentList = new ArrayList<Intent>(); |
| 492 | intentList.add(createCameraIntent()); |
| 493 | intentList.add(createCamcorderIntent()); |
| 494 | intentList.add(createSoundRecorderIntent()); |
| 495 | } |
| 496 | |
| 497 | // get all openable apps list and create corresponading intents |
| 498 | PackageManager pm = mController.getActivity().getPackageManager(); |
| 499 | List<ResolveInfo> openableAppsList = pm.queryIntentActivities(openable, 0); |
| 500 | for (int j = 0, n = openableAppsList.size(); j < n; j++ ) { |
| 501 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 502 | i.setType(openableMimeType); |
| 503 | ActivityInfo activityInfo = openableAppsList.get(j).activityInfo; |
| 504 | ComponentName name = new ComponentName(activityInfo.applicationInfo.packageName, |
| 505 | activityInfo.name); |
| 506 | i.setComponent(name); |
| 507 | intentList.add(i); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | UploadDialog upDialog = new UploadDialog(mController.getActivity()); |
| 512 | upDialog.getUploadableApps(intentList); |
| 513 | upDialog.loadView(this); |
| 514 | } |
| 515 | |
| 516 | public void initiateActivity(Intent intent) { |
| 517 | startActivity(intent); |
| 518 | } |
| 519 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 520 | private Intent createChooserIntent(Intent... intents) { |
| 521 | Intent chooser = new Intent(Intent.ACTION_CHOOSER); |
| 522 | chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents); |
| 523 | chooser.putExtra(Intent.EXTRA_TITLE, |
| 524 | mController.getActivity().getResources() |
| 525 | .getString(R.string.choose_upload)); |
| 526 | return chooser; |
| 527 | } |
| 528 | |
| 529 | private Intent createOpenableIntent(String type) { |
| 530 | Intent i = new Intent(Intent.ACTION_GET_CONTENT); |
| 531 | i.addCategory(Intent.CATEGORY_OPENABLE); |
| 532 | i.setType(type); |
| 533 | return i; |
| 534 | } |
| 535 | |
| 536 | private Intent createCameraIntent() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 537 | Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 538 | File externalDataDir = Environment.getExternalStoragePublicDirectory( |
| 539 | Environment.DIRECTORY_DCIM); |
| 540 | File cameraDataDir = new File(externalDataDir.getAbsolutePath() + |
| 541 | File.separator + "browser-photos"); |
| 542 | cameraDataDir.mkdirs(); |
| 543 | mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator + |
| 544 | System.currentTimeMillis() + ".jpg"; |
| 545 | cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath))); |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 546 | return cameraIntent; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Ben Murdoch | e4c0cae | 2011-02-18 11:25:38 +0000 | [diff] [blame] | 549 | private Intent createCamcorderIntent() { |
| 550 | return new Intent(MediaStore.ACTION_VIDEO_CAPTURE); |
| 551 | } |
| 552 | |
| 553 | private Intent createSoundRecorderIntent() { |
| 554 | return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | } |