-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to record a video from the camera
Replace #2411
- Loading branch information
Showing
11 changed files
with
404 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
multipicker/src/main/java/im/vector/lib/multipicker/CameraVideoPicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright (c) 2021 New Vector Ltd | ||
* | ||
* 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. | ||
*/ | ||
|
||
package im.vector.lib.multipicker | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.media.MediaMetadataRetriever | ||
import android.net.Uri | ||
import android.provider.MediaStore | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.core.content.FileProvider | ||
import im.vector.lib.multipicker.entity.MultiPickerVideoType | ||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
/** | ||
* Implementation of taking a video with Camera | ||
*/ | ||
class CameraVideoPicker { | ||
|
||
/** | ||
* Start camera by using a ActivityResultLauncher | ||
* @return Uri of taken photo or null if the operation is cancelled. | ||
*/ | ||
fun startWithExpectingFile(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>): Uri? { | ||
val videoUri = createVideoUri(context) | ||
val intent = createIntent().apply { | ||
putExtra(MediaStore.EXTRA_OUTPUT, videoUri) | ||
} | ||
activityResultLauncher.launch(intent) | ||
return videoUri | ||
} | ||
|
||
/** | ||
* Call this function from onActivityResult(int, int, Intent). | ||
* @return Taken photo or null if request code is wrong | ||
* or result code is not Activity.RESULT_OK | ||
* or user cancelled the operation. | ||
*/ | ||
fun getTakenVideo(context: Context, videoUri: Uri): MultiPickerVideoType? { | ||
val projection = arrayOf( | ||
MediaStore.Images.Media.DISPLAY_NAME, | ||
MediaStore.Images.Media.SIZE, | ||
MediaStore.Images.Media.MIME_TYPE | ||
) | ||
|
||
context.contentResolver.query( | ||
videoUri, | ||
projection, | ||
null, | ||
null, | ||
null | ||
)?.use { cursor -> | ||
val nameColumn = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME) | ||
val sizeColumn = cursor.getColumnIndex(MediaStore.Images.Media.SIZE) | ||
|
||
if (cursor.moveToNext()) { | ||
val name = cursor.getString(nameColumn) | ||
val size = cursor.getLong(sizeColumn) | ||
var duration = 0L | ||
var width = 0 | ||
var height = 0 | ||
var orientation = 0 | ||
|
||
context.contentResolver.openFileDescriptor(videoUri, "r")?.use { pfd -> | ||
val mediaMetadataRetriever = MediaMetadataRetriever() | ||
mediaMetadataRetriever.setDataSource(pfd.fileDescriptor) | ||
duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?: 0L | ||
width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0 | ||
height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: 0 | ||
orientation = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)?.toInt() ?: 0 | ||
} | ||
|
||
return MultiPickerVideoType( | ||
name, | ||
size, | ||
context.contentResolver.getType(videoUri), | ||
videoUri, | ||
width, | ||
height, | ||
orientation, | ||
duration | ||
) | ||
} | ||
} | ||
return null | ||
} | ||
|
||
private fun createIntent(): Intent { | ||
return Intent(MediaStore.ACTION_VIDEO_CAPTURE) | ||
} | ||
|
||
companion object { | ||
fun createVideoUri(context: Context): Uri { | ||
val file = createVideoFile(context) | ||
val authority = context.packageName + ".multipicker.fileprovider" | ||
return FileProvider.getUriForFile(context, authority, file) | ||
} | ||
|
||
private fun createVideoFile(context: Context): File { | ||
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) | ||
val storageDir: File = context.filesDir | ||
return File.createTempFile( | ||
"${timeStamp}_", /* prefix */ | ||
".mp4", /* suffix */ | ||
storageDir /* directory */ | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
vector/src/main/java/im/vector/app/core/dialogs/PhotoOrVideoDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright (c) 2021 New Vector Ltd | ||
* | ||
* 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. | ||
*/ | ||
|
||
package im.vector.app.core.dialogs | ||
|
||
import android.app.Activity | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.core.view.isVisible | ||
import im.vector.app.R | ||
import im.vector.app.databinding.DialogPhotoOrVideoBinding | ||
import im.vector.app.features.settings.VectorPreferences | ||
|
||
class PhotoOrVideoDialog( | ||
private val activity: Activity, | ||
private val vectorPreferences: VectorPreferences | ||
) { | ||
|
||
interface PhotoOrVideoDialogListener { | ||
fun takePhoto() | ||
fun takeVideo() | ||
} | ||
|
||
interface PhotoOrVideoDialogSettingsListener { | ||
fun onUpdated() | ||
} | ||
|
||
fun show(listener: PhotoOrVideoDialogListener) { | ||
when (vectorPreferences.getTakePhotoVideoMode()) { | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_PHOTO -> listener.takePhoto() | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_VIDEO -> listener.takeVideo() | ||
/* VectorPreferences.TAKE_PHOTO_VIDEO_MODE_ALWAYS_ASK */ | ||
else -> { | ||
val dialogLayout = activity.layoutInflater.inflate(R.layout.dialog_photo_or_video, null) | ||
val views = DialogPhotoOrVideoBinding.bind(dialogLayout) | ||
|
||
// Show option to set as default in this case | ||
views.dialogPhotoOrVideoAsDefault.isVisible = true | ||
// Always default to photo | ||
views.dialogPhotoOrVideoPhoto.isChecked = true | ||
|
||
AlertDialog.Builder(activity) | ||
.setTitle(R.string.option_take_photo_video) | ||
.setView(dialogLayout) | ||
.setPositiveButton(R.string._continue) { _, _ -> | ||
submit(views, vectorPreferences, listener) | ||
} | ||
.setNegativeButton(R.string.cancel, null) | ||
.show() | ||
} | ||
} | ||
} | ||
|
||
private fun submit(views: DialogPhotoOrVideoBinding, | ||
vectorPreferences: VectorPreferences, | ||
listener: PhotoOrVideoDialogListener) { | ||
val mode = if (views.dialogPhotoOrVideoPhoto.isChecked) { | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_PHOTO | ||
} else { | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_VIDEO | ||
} | ||
|
||
if (views.dialogPhotoOrVideoAsDefault.isChecked) { | ||
vectorPreferences.setTakePhotoVideoMode(mode) | ||
} | ||
|
||
when (mode) { | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_PHOTO -> listener.takePhoto() | ||
VectorPreferences.TAKE_PHOTO_VIDEO_MODE_VIDEO -> listener.takeVideo() | ||
} | ||
} | ||
|
||
fun showForSettings(listener: PhotoOrVideoDialogSettingsListener) { | ||
val currentMode = vectorPreferences.getTakePhotoVideoMode() | ||
|
||
val dialogLayout = activity.layoutInflater.inflate(R.layout.dialog_photo_or_video, null) | ||
val views = DialogPhotoOrVideoBinding.bind(dialogLayout) | ||
|
||
// Show option for always ask in this case | ||
views.dialogPhotoOrVideoAlwaysAsk.isVisible = true | ||
// Always default to photo | ||
views.dialogPhotoOrVideoPhoto.isChecked = currentMode == VectorPreferences.TAKE_PHOTO_VIDEO_MODE_PHOTO | ||
views.dialogPhotoOrVideoVideo.isChecked = currentMode == VectorPreferences.TAKE_PHOTO_VIDEO_MODE_VIDEO | ||
views.dialogPhotoOrVideoAlwaysAsk.isChecked = currentMode == VectorPreferences.TAKE_PHOTO_VIDEO_MODE_ALWAYS_ASK | ||
|
||
AlertDialog.Builder(activity) | ||
.setTitle(R.string.option_take_photo_video) | ||
.setView(dialogLayout) | ||
.setPositiveButton(R.string.save) { _, _ -> | ||
submitSettings(views) | ||
listener.onUpdated() | ||
} | ||
.setNegativeButton(R.string.cancel, null) | ||
.show() | ||
} | ||
|
||
private fun submitSettings(views: DialogPhotoOrVideoBinding) { | ||
vectorPreferences.setTakePhotoVideoMode( | ||
when { | ||
views.dialogPhotoOrVideoPhoto.isChecked -> VectorPreferences.TAKE_PHOTO_VIDEO_MODE_PHOTO | ||
views.dialogPhotoOrVideoVideo.isChecked -> VectorPreferences.TAKE_PHOTO_VIDEO_MODE_VIDEO | ||
else -> VectorPreferences.TAKE_PHOTO_VIDEO_MODE_ALWAYS_ASK | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.