Check permission before trying to reach Camera.

This commit is contained in:
onurays 2020-06-30 10:59:20 +03:00 committed by Benoit Marty
parent 5f788d962e
commit b3d4d20195
2 changed files with 33 additions and 9 deletions

View file

@ -33,6 +33,10 @@ import im.vector.riotx.core.di.ScreenComponent
import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.core.platform.VectorBaseActivity
import im.vector.riotx.core.resources.ColorProvider import im.vector.riotx.core.resources.ColorProvider
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.PERMISSIONS_FOR_TAKING_PHOTO
import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA
import im.vector.riotx.core.utils.allGranted
import im.vector.riotx.core.utils.checkPermissions
import im.vector.riotx.multipicker.MultiPicker import im.vector.riotx.multipicker.MultiPicker
import im.vector.riotx.multipicker.entity.MultiPickerImageType import im.vector.riotx.multipicker.entity.MultiPickerImageType
import kotlinx.android.synthetic.main.activity_big_image_viewer.* import kotlinx.android.synthetic.main.activity_big_image_viewer.*
@ -92,7 +96,6 @@ class BigImageViewerActivity : VectorBaseActivity() {
return uri != null && intent.getBooleanExtra(EXTRA_CAN_EDIT_IMAGE, false) return uri != null && intent.getBooleanExtra(EXTRA_CAN_EDIT_IMAGE, false)
} }
private var avatarCameraUri: Uri? = null
private fun showAvatarSelector() { private fun showAvatarSelector() {
AlertDialog AlertDialog
.Builder(this) .Builder(this)
@ -101,17 +104,21 @@ class BigImageViewerActivity : VectorBaseActivity() {
stringProvider.getString(R.string.attachment_type_gallery) stringProvider.getString(R.string.attachment_type_gallery)
)) { dialog, which -> )) { dialog, which ->
dialog.cancel() dialog.cancel()
when (which) { onAvatarTypeSelected(isCamera = (which == 0))
0 -> {
avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(this)
}
1 -> {
MultiPicker.get(MultiPicker.IMAGE).single().startWith(this)
}
}
}.show() }.show()
} }
private var avatarCameraUri: Uri? = null
private fun onAvatarTypeSelected(isCamera: Boolean) {
if (isCamera) {
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, PERMISSION_REQUEST_CODE_LAUNCH_CAMERA)) {
avatarCameraUri = MultiPicker.get(MultiPicker.CAMERA).startWithExpectingFile(this)
}
} else {
MultiPicker.get(MultiPicker.IMAGE).single().startWith(this)
}
}
private fun onRoomAvatarSelected(image: MultiPickerImageType) { private fun onRoomAvatarSelected(image: MultiPickerImageType) {
val destinationFile = File(cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}") val destinationFile = File(cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}")
val uri = image.contentUri val uri = image.contentUri
@ -148,6 +155,14 @@ class BigImageViewerActivity : VectorBaseActivity() {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (allGranted(grantResults)) {
when (requestCode) {
PERMISSION_REQUEST_CODE_LAUNCH_CAMERA -> onAvatarTypeSelected(true)
}
}
}
private fun onAvatarCropped(uri: Uri?) { private fun onAvatarCropped(uri: Uri?) {
if (uri != null) { if (uri != null) {
setResult(Activity.RESULT_OK, Intent().setData(uri)) setResult(Activity.RESULT_OK, Intent().setData(uri))

View file

@ -49,6 +49,7 @@ import im.vector.riotx.core.platform.VectorBaseFragment
import im.vector.riotx.core.resources.ColorProvider import im.vector.riotx.core.resources.ColorProvider
import im.vector.riotx.core.utils.PERMISSIONS_FOR_TAKING_PHOTO import im.vector.riotx.core.utils.PERMISSIONS_FOR_TAKING_PHOTO
import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA
import im.vector.riotx.core.utils.allGranted
import im.vector.riotx.core.utils.checkPermissions import im.vector.riotx.core.utils.checkPermissions
import im.vector.riotx.core.utils.copyToClipboard import im.vector.riotx.core.utils.copyToClipboard
import im.vector.riotx.core.utils.startSharePlainTextIntent import im.vector.riotx.core.utils.startSharePlainTextIntent
@ -311,6 +312,14 @@ class RoomProfileFragment @Inject constructor(
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (allGranted(grantResults)) {
when (requestCode) {
PERMISSION_REQUEST_CODE_LAUNCH_CAMERA -> onAvatarTypeSelected(true)
}
}
}
private fun onAvatarCropped(uri: Uri?) { private fun onAvatarCropped(uri: Uri?) {
if (uri != null) { if (uri != null) {
roomProfileViewModel.handle(RoomProfileAction.ChangeRoomAvatar(uri, getFilenameFromUri(context, uri))) roomProfileViewModel.handle(RoomProfileAction.ChangeRoomAvatar(uri, getFilenameFromUri(context, uri)))