mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 18:35:40 +03:00
Check permission before trying to reach Camera.
This commit is contained in:
parent
5f788d962e
commit
b3d4d20195
2 changed files with 33 additions and 9 deletions
|
@ -33,6 +33,10 @@ import im.vector.riotx.core.di.ScreenComponent
|
|||
import im.vector.riotx.core.platform.VectorBaseActivity
|
||||
import im.vector.riotx.core.resources.ColorProvider
|
||||
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.entity.MultiPickerImageType
|
||||
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)
|
||||
}
|
||||
|
||||
private var avatarCameraUri: Uri? = null
|
||||
private fun showAvatarSelector() {
|
||||
AlertDialog
|
||||
.Builder(this)
|
||||
|
@ -101,16 +104,20 @@ class BigImageViewerActivity : VectorBaseActivity() {
|
|||
stringProvider.getString(R.string.attachment_type_gallery)
|
||||
)) { dialog, which ->
|
||||
dialog.cancel()
|
||||
when (which) {
|
||||
0 -> {
|
||||
onAvatarTypeSelected(isCamera = (which == 0))
|
||||
}.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)
|
||||
}
|
||||
1 -> {
|
||||
} else {
|
||||
MultiPicker.get(MultiPicker.IMAGE).single().startWith(this)
|
||||
}
|
||||
}
|
||||
}.show()
|
||||
}
|
||||
|
||||
private fun onRoomAvatarSelected(image: MultiPickerImageType) {
|
||||
val destinationFile = File(cacheDir, "${image.displayName}_edited_image_${System.currentTimeMillis()}")
|
||||
|
@ -148,6 +155,14 @@ class BigImageViewerActivity : VectorBaseActivity() {
|
|||
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?) {
|
||||
if (uri != null) {
|
||||
setResult(Activity.RESULT_OK, Intent().setData(uri))
|
||||
|
|
|
@ -49,6 +49,7 @@ import im.vector.riotx.core.platform.VectorBaseFragment
|
|||
import im.vector.riotx.core.resources.ColorProvider
|
||||
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.core.utils.copyToClipboard
|
||||
import im.vector.riotx.core.utils.startSharePlainTextIntent
|
||||
|
@ -311,6 +312,14 @@ class RoomProfileFragment @Inject constructor(
|
|||
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?) {
|
||||
if (uri != null) {
|
||||
roomProfileViewModel.handle(RoomProfileAction.ChangeRoomAvatar(uri, getFilenameFromUri(context, uri)))
|
||||
|
|
Loading…
Reference in a new issue