From bba2301b900d64c42e978ba1bc45a2e7324dd312 Mon Sep 17 00:00:00 2001 From: Areeb Jamal Date: Sat, 23 Feb 2019 17:36:41 +0530 Subject: [PATCH] fix: Check for null permission in onPermissionsResult (#1978) --- .../org/fossasia/susi/ai/chat/ChatActivity.kt | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt b/app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt index c8c75a4ba..774fdb902 100644 --- a/app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt +++ b/app/src/main/java/org/fossasia/susi/ai/chat/ChatActivity.kt @@ -389,26 +389,25 @@ class ChatActivity : AppCompatActivity(), IChatView { ActivityCompat.requestPermissions(this, permissions, PERM_REQ_CODE) } - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { when (requestCode) { PERM_REQ_CODE -> run { var audioPermissionGiven = false - for (i in permissions.indices) { - when (permissions[i]) { - Manifest.permission.ACCESS_FINE_LOCATION -> if (grantResults.isNotEmpty() && grantResults[i] == PackageManager.PERMISSION_GRANTED) { + for ((index, permission) in permissions.withIndex()) { + if (permission == null) + continue + val granted = grantResults.isNotEmpty() && grantResults[index] == PackageManager.PERMISSION_GRANTED + when (permission) { + Manifest.permission.ACCESS_FINE_LOCATION -> if (granted) { chatPresenter.getLocationFromLocationService() } Manifest.permission.RECORD_AUDIO -> { - if (grantResults.isNotEmpty() && grantResults[i] == PackageManager.PERMISSION_GRANTED) { - chatPresenter.disableMicInput(false) - } else { - chatPresenter.disableMicInput(true) - } - audioPermissionGiven = true + chatPresenter.disableMicInput(!granted) + audioPermissionGiven = granted } - Manifest.permission.WRITE_EXTERNAL_STORAGE -> if (grantResults.size >= 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED && audioPermissionGiven) { + Manifest.permission.WRITE_EXTERNAL_STORAGE -> if (granted && audioPermissionGiven) { chatPresenter.initiateHotwordDetection() } }