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 {
compile 'com.simplemobiletools:commons:2.30.4'
compile 'com.simplemobiletools:commons:2.30.7'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

View File

@ -1,10 +1,8 @@
package com.simplemobiletools.camera.activities
import android.Manifest
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.hardware.Camera
import android.hardware.SensorManager
@ -13,7 +11,6 @@ import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.view.*
import android.widget.RelativeLayout
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.views.FocusRectView
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 kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSavedListener {
companion object {
private val CAMERA_STORAGE_PERMISSION = 1
private val RECORD_AUDIO_PERMISSION = 2
private val FADE_DELAY = 5000
lateinit var mFocusRectView: FocusRectView
@ -45,7 +42,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private var mPreviewUri: Uri? = null
private var mFlashlightState = FLASH_OFF
private var mIsInPhotoMode = false
private var mIsAskingPermissions = false
private var mIsCameraAvailable = false
private var mIsVideoCaptureIntent = false
private var mIsHardwareShutterHandled = false
@ -71,7 +67,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private fun initVariables() {
mRes = resources
mIsInPhotoMode = false
mIsAskingPermissions = false
mIsCameraAvailable = false
mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false
@ -106,18 +101,21 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
}
private fun tryInitCamera() {
if (hasCameraAndStoragePermission()) {
handlePermission(PERMISSION_CAMERA) {
if (it) {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
initializeCamera()
handleIntent()
} else {
val permissions = ArrayList<String>(2)
if (!hasCameraPermission()) {
permissions.add(Manifest.permission.CAMERA)
toast(R.string.no_permissions)
finish()
}
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() }
}
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() {
if (!checkCameraAvailable()) {
return
@ -321,16 +294,19 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
}
private fun handleTogglePhotoVideo() {
handlePermission(PERMISSION_RECORD_AUDIO) {
if (it) {
togglePhotoVideo()
} else {
toast(R.string.no_audio_permissions)
if (mIsVideoCaptureIntent) {
finish()
}
}
}
}
private fun togglePhotoVideo() {
if (!hasRecordAudioPermission()) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), RECORD_AUDIO_PERMISSION)
mIsAskingPermissions = true
return
}
if (!checkCameraAvailable()) {
return
}
@ -451,20 +427,20 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
override fun onResume() {
super.onResume()
if (hasCameraAndStoragePermission()) {
if (hasStorageAndCameraPermissions()) {
resumeCameraItems()
setupPreviewImage(mIsInPhotoMode)
scheduleFadeOut()
mFocusRectView.setStrokeColor(config.primaryColor)
if (mIsVideoCaptureIntent && mIsInPhotoMode) {
togglePhotoVideo()
handleTogglePhotoVideo()
checkButtons()
}
toggleBottomButtons(false)
}
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (hasCameraAndStoragePermission()) {
if (hasStorageAndCameraPermissions()) {
mOrientationEventListener.enable()
}
}
@ -490,8 +466,9 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
override fun onPause() {
super.onPause()
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (!hasCameraAndStoragePermission() || mIsAskingPermissions)
if (!hasStorageAndCameraPermissions() || isAskingPermissions) {
return
}
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() {
mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
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) {
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 {
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) {
selectionIndex = it as Int
view.change_resolution_video.text = items[selectionIndex].title
if (isBackCamera)
if (isBackCamera) {
config.backVideoResIndex = it
else
} else {
config.frontVideoResIndex = it
}
dialog.dismiss()
}
}