update Commons for easy runtime permissions handling

This commit is contained in:
tibbi 2017-10-21 20:33:03 +02:00
parent 4b9eb1bfd9
commit 15e88a8510
3 changed files with 39 additions and 59 deletions

View File

@ -37,7 +37,7 @@ android {
} }
dependencies { dependencies {
compile 'com.simplemobiletools:commons:2.30.4' compile 'com.simplemobiletools:commons:2.30.7'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }

View File

@ -1,10 +1,8 @@
package com.simplemobiletools.camera.activities package com.simplemobiletools.camera.activities
import android.Manifest
import android.app.Activity import android.app.Activity
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources import android.content.res.Resources
import android.hardware.Camera import android.hardware.Camera
import android.hardware.SensorManager import android.hardware.SensorManager
@ -13,7 +11,6 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.MediaStore import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.view.* import android.view.*
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -26,14 +23,14 @@ import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.navBarHeight import com.simplemobiletools.camera.extensions.navBarHeight
import com.simplemobiletools.camera.views.FocusRectView import com.simplemobiletools.camera.views.FocusRectView
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSavedListener { class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSavedListener {
companion object { companion object {
private val CAMERA_STORAGE_PERMISSION = 1
private val RECORD_AUDIO_PERMISSION = 2
private val FADE_DELAY = 5000 private val FADE_DELAY = 5000
lateinit var mFocusRectView: FocusRectView lateinit var mFocusRectView: FocusRectView
@ -45,7 +42,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private var mPreviewUri: Uri? = null private var mPreviewUri: Uri? = null
private var mFlashlightState = FLASH_OFF private var mFlashlightState = FLASH_OFF
private var mIsInPhotoMode = false private var mIsInPhotoMode = false
private var mIsAskingPermissions = false
private var mIsCameraAvailable = false private var mIsCameraAvailable = false
private var mIsVideoCaptureIntent = false private var mIsVideoCaptureIntent = false
private var mIsHardwareShutterHandled = false private var mIsHardwareShutterHandled = false
@ -71,7 +67,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private fun initVariables() { private fun initVariables() {
mRes = resources mRes = resources
mIsInPhotoMode = false mIsInPhotoMode = false
mIsAskingPermissions = false
mIsCameraAvailable = false mIsCameraAvailable = false
mIsVideoCaptureIntent = false mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false mIsHardwareShutterHandled = false
@ -106,18 +101,21 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
} }
private fun tryInitCamera() { private fun tryInitCamera() {
if (hasCameraAndStoragePermission()) { handlePermission(PERMISSION_CAMERA) {
if (it) {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
initializeCamera() initializeCamera()
handleIntent() handleIntent()
} else { } else {
val permissions = ArrayList<String>(2) toast(R.string.no_permissions)
if (!hasCameraPermission()) { finish()
permissions.add(Manifest.permission.CAMERA)
} }
if (!hasWriteStoragePermission()) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
} }
ActivityCompat.requestPermissions(this, permissions.toTypedArray(), CAMERA_STORAGE_PERMISSION) } else {
toast(R.string.no_permissions)
finish()
}
} }
} }
@ -170,31 +168,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
change_resolution.setOnClickListener { mPreview?.showChangeResolutionDialog() } change_resolution.setOnClickListener { mPreview?.showChangeResolutionDialog() }
} }
private fun hasCameraAndStoragePermission() = hasCameraPermission() && hasWriteStoragePermission()
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
mIsAskingPermissions = false
if (requestCode == CAMERA_STORAGE_PERMISSION) {
if (hasCameraAndStoragePermission()) {
initializeCamera()
handleIntent()
} else {
toast(R.string.no_permissions)
finish()
}
} else if (requestCode == RECORD_AUDIO_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
togglePhotoVideo()
} else {
toast(R.string.no_audio_permissions)
if (mIsVideoCaptureIntent)
finish()
}
}
}
private fun toggleCamera() { private fun toggleCamera() {
if (!checkCameraAvailable()) { if (!checkCameraAvailable()) {
return return
@ -321,16 +294,19 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
} }
private fun handleTogglePhotoVideo() { private fun handleTogglePhotoVideo() {
handlePermission(PERMISSION_RECORD_AUDIO) {
if (it) {
togglePhotoVideo() togglePhotoVideo()
} else {
toast(R.string.no_audio_permissions)
if (mIsVideoCaptureIntent) {
finish()
}
}
}
} }
private fun togglePhotoVideo() { private fun togglePhotoVideo() {
if (!hasRecordAudioPermission()) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_AUDIO_PERMISSION)
mIsAskingPermissions = true
return
}
if (!checkCameraAvailable()) { if (!checkCameraAvailable()) {
return return
} }
@ -451,20 +427,20 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (hasCameraAndStoragePermission()) { if (hasStorageAndCameraPermissions()) {
resumeCameraItems() resumeCameraItems()
setupPreviewImage(mIsInPhotoMode) setupPreviewImage(mIsInPhotoMode)
scheduleFadeOut() scheduleFadeOut()
mFocusRectView.setStrokeColor(config.primaryColor) mFocusRectView.setStrokeColor(config.primaryColor)
if (mIsVideoCaptureIntent && mIsInPhotoMode) { if (mIsVideoCaptureIntent && mIsInPhotoMode) {
togglePhotoVideo() handleTogglePhotoVideo()
checkButtons() checkButtons()
} }
toggleBottomButtons(false) toggleBottomButtons(false)
} }
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (hasCameraAndStoragePermission()) { if (hasStorageAndCameraPermissions()) {
mOrientationEventListener.enable() mOrientationEventListener.enable()
} }
} }
@ -490,8 +466,9 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (!hasCameraAndStoragePermission() || mIsAskingPermissions) if (!hasStorageAndCameraPermissions() || isAskingPermissions) {
return return
}
mFadeHandler.removeCallbacksAndMessages(null) mFadeHandler.removeCallbacksAndMessages(null)
@ -504,6 +481,8 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
} }
} }
private fun hasStorageAndCameraPermissions() = hasPermission(PERMISSION_WRITE_STORAGE) && hasPermission(PERMISSION_CAMERA)
private fun setupOrientationEventListener() { private fun setupOrientationEventListener() {
mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) { override fun onOrientationChanged(orientation: Int) {

View File

@ -16,7 +16,7 @@ import kotlinx.android.synthetic.main.dialog_change_resolution.view.*
class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) { class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) {
var dialog: AlertDialog var dialog: AlertDialog
val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK private val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
init { init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply {
@ -59,10 +59,11 @@ class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, v
RadioGroupDialog(activity, items, selectionIndex) { RadioGroupDialog(activity, items, selectionIndex) {
selectionIndex = it as Int selectionIndex = it as Int
view.change_resolution_video.text = items[selectionIndex].title view.change_resolution_video.text = items[selectionIndex].title
if (isBackCamera) if (isBackCamera) {
config.backVideoResIndex = it config.backVideoResIndex = it
else } else {
config.frontVideoResIndex = it config.frontVideoResIndex = it
}
dialog.dismiss() dialog.dismiss()
} }
} }