diff options
author | Tao Bai <michaelbai@google.com> | 2014-09-10 21:54:52 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-09-10 21:54:52 +0000 |
commit | c232a00c54d59947bd2dcd32767fbc0615ea33a3 (patch) | |
tree | 0c6decd1f376d8621ba9a2070e7e7c189f023dba | |
parent | cc5e753b9b3486222f87a2da4f8a8d34cfaff316 (diff) | |
parent | 2979e7861f4fd053dce21ef50ed65605537479e7 (diff) | |
download | Browser-c232a00c54d59947bd2dcd32767fbc0615ea33a3.tar.gz |
am 2979e786: am 126b5b3a: Adapt AOSP browser to use the revised createIntent() API
* commit '2979e7861f4fd053dce21ef50ed65605537479e7':
Adapt AOSP browser to use the revised createIntent() API
-rw-r--r-- | AndroidManifest.xml | 8 | ||||
-rw-r--r-- | res/xml/file_paths.xml | 21 | ||||
-rw-r--r-- | src/com/android/browser/UploadHandler.java | 117 |
3 files changed, 139 insertions, 7 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 6f8d80d6..6d1c822c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -271,6 +271,14 @@ android:authorities="com.android.browser.snapshots" android:exported="false" /> + <provider android:name="android.support.v4.content.FileProvider" + android:authorities="com.android.browser-classic.file" + android:exported="false" + android:grantUriPermissions="true"> + <meta-data + android:name="android.support.FILE_PROVIDER_PATHS" + android:resource="@xml/file_paths" /> + </provider> </application> </manifest> diff --git a/res/xml/file_paths.xml b/res/xml/file_paths.xml new file mode 100644 index 00000000..20369ba5 --- /dev/null +++ b/res/xml/file_paths.xml @@ -0,0 +1,21 @@ +<!-- +/* + * Copyright 2014, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +--> + +<paths xmlns:android="http://schemas.android.com/apk/res/android"> + <files-path name="captured_media" path="captured_media/"/> +</paths> diff --git a/src/com/android/browser/UploadHandler.java b/src/com/android/browser/UploadHandler.java index beaaf5ff..c20fbf6f 100644 --- a/src/com/android/browser/UploadHandler.java +++ b/src/com/android/browser/UploadHandler.java @@ -20,29 +20,31 @@ import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; -import android.os.Environment; import android.provider.MediaStore; +import android.support.v4.content.FileProvider; import android.webkit.WebChromeClient.FileChooserParams; -import android.webkit.WebChromeClient.UploadHelper; import android.webkit.ValueCallback; import android.widget.Toast; import java.io.File; -import java.util.Vector; /** * Handle the file upload. This does not support selecting multiple files yet. */ public class UploadHandler { + private final static String IMAGE_MIME_TYPE = "image/*"; + private final static String VIDEO_MIME_TYPE = "video/*"; + private final static String AUDIO_MIME_TYPE = "audio/*"; /* * The Object used to inform the WebView of the file to upload. */ private ValueCallback<Uri[]> mUploadMessage; - private UploadHelper mUploadHelper; private boolean mHandled; private Controller mController; + private FileChooserParams mParams; + private Uri mCapturedMedia; public UploadHandler(Controller controller) { mController = controller; @@ -53,7 +55,11 @@ public class UploadHandler { } void onResult(int resultCode, Intent intent) { - mUploadMessage.onReceiveValue(mUploadHelper.parseResult(resultCode, intent)); + Uri[] uris; + // As the media capture is always supported, we can't use + // FileChooserParams.parseResult(). + uris = parseResult(resultCode, intent); + mUploadMessage.onReceiveValue(uris); mHandled = true; } @@ -65,8 +71,46 @@ public class UploadHandler { } mUploadMessage = callback; - mUploadHelper = fileChooserParams.getUploadHelper(); - startActivity(mUploadHelper.buildIntent()); + mParams = fileChooserParams; + Intent[] captureIntents = createCaptureIntent(); + assert(captureIntents != null && captureIntents.length > 0); + Intent intent = null; + // Go to the media capture directly if capture is specified, this is the + // preferred way. + if (fileChooserParams.isCaptureEnabled() && captureIntents.length == 1) { + intent = captureIntents[0]; + } else { + intent = new Intent(Intent.ACTION_CHOOSER); + intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, captureIntents); + intent.putExtra(Intent.EXTRA_INTENT, fileChooserParams.createIntent()); + } + startActivity(intent); + } + + private Uri[] parseResult(int resultCode, Intent intent) { + if (resultCode == Activity.RESULT_CANCELED) { + return null; + } + Uri result = intent == null || resultCode != Activity.RESULT_OK ? null + : intent.getData(); + + // As we ask the camera to save the result of the user taking + // a picture, the camera application does not return anything other + // than RESULT_OK. So we need to check whether the file we expected + // was written to disk in the in the case that we + // did not get an intent returned but did get a RESULT_OK. If it was, + // we assume that this result has came back from the camera. + if (result == null && intent == null && resultCode == Activity.RESULT_OK + && mCapturedMedia != null) { + result = mCapturedMedia; + } + + Uri[] uris = null; + if (result != null) { + uris = new Uri[1]; + uris[0] = result; + } + return uris; } private void startActivity(Intent intent) { @@ -79,4 +123,63 @@ public class UploadHandler { Toast.LENGTH_LONG).show(); } } + + private Intent[] createCaptureIntent() { + try { + File mediaPath = new File(mController.getActivity().getFilesDir(), "captured_media"); + if (!mediaPath.exists() && !mediaPath.mkdir()) { + throw new RuntimeException("Folder cannot be created."); + } + File mediaFile = File.createTempFile( + String.valueOf(System.currentTimeMillis()), null, mediaPath); + mCapturedMedia = FileProvider.getUriForFile(mController.getActivity(), + "com.android.browser-classic.file", mediaFile); + } catch (java.io.IOException e) { + throw new RuntimeException(e); + } + String mimeType = "*/*"; + String[] acceptTypes = mParams.getAcceptTypes(); + if ( acceptTypes != null && acceptTypes.length > 0) { + mimeType = acceptTypes[0]; + } + Intent[] intents; + if (mimeType.equals(IMAGE_MIME_TYPE)) { + intents = new Intent[1]; + intents[0] = createCameraIntent(); + } else if (mimeType.equals(VIDEO_MIME_TYPE)) { + intents = new Intent[1]; + intents[0] = createCamcorderIntent(); + } else if (mimeType.equals(AUDIO_MIME_TYPE)) { + intents = new Intent[1]; + intents[0] = createSoundRecorderIntent(); + } else { + intents = new Intent[3]; + intents[0] = createCameraIntent(); + intents[1] = createCamcorderIntent(); + intents[2] = createSoundRecorderIntent(); + } + return intents; + } + + private Intent createCameraIntent() { + if (mCapturedMedia == null) throw new IllegalArgumentException(); + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | + Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedMedia); + return intent; + } + + private Intent createCamcorderIntent() { + if (mCapturedMedia == null) throw new IllegalArgumentException(); + Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | + Intent.FLAG_GRANT_WRITE_URI_PERMISSION); + intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedMedia); + return intent; + } + + private Intent createSoundRecorderIntent() { + return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); + } } |