diff --git a/app/src/main/java/cchcc/learn/amu/e04/E04Activity.kt b/app/src/main/java/cchcc/learn/amu/e04/E04Activity.kt index d01aa1c..509e203 100644 --- a/app/src/main/java/cchcc/learn/amu/e04/E04Activity.kt +++ b/app/src/main/java/cchcc/learn/amu/e04/E04Activity.kt @@ -28,27 +28,28 @@ class E04Activity : AppCompatActivity() { it.viewModel = viewModel } - viewModel.pickContactAction.observe(this, Observer { - permissionOf(Manifest.permission.READ_CONTACTS).checkOrRequest permissionIsGranted@{ - - val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI) - startActivityWithResult(intent) { resultCode, data -> - if (resultCode == Activity.RESULT_OK && data?.data != null) { + val actionPermissionGrant: () -> Unit = { + val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI) + startActivityWithResult(intent) { resultCode, data -> + if (resultCode == Activity.RESULT_OK && data?.data != null) { - val cursor = contentResolver.query(data.data!!, null, null, null, null) - val nameAndPhone = cursor?.use { - it.moveToFirst() + val cursor = contentResolver.query(data.data!!, null, null, null, null) + val nameAndPhone = cursor?.use { + it.moveToFirst() - val name = it.getString(it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)) - val phone = it.getString(it.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DATA)) + val name = it.getString(it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)) + val phone = it.getString(it.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DATA)) - name to phone - } ?: "" to "" + name to phone + } ?: "" to "" - viewModel.nameAndPhone.value = nameAndPhone - } + viewModel.nameAndPhone.value = nameAndPhone } } + } + + viewModel.pickContactAction.observe(this, Observer { + permissionOf(Manifest.permission.READ_CONTACTS).checkOrRequest(actionPermissionGrant) {} }) } diff --git a/app/src/main/java/cchcc/learn/amu/util/DialogUtil.kt b/app/src/main/java/cchcc/learn/amu/util/DialogUtil.kt new file mode 100644 index 0000000..36f0b34 --- /dev/null +++ b/app/src/main/java/cchcc/learn/amu/util/DialogUtil.kt @@ -0,0 +1,15 @@ +package cchcc.learn.amu.util + +import android.app.Activity +import android.content.DialogInterface +import androidx.appcompat.app.AlertDialog + + +fun Activity.showAlert() { + val alertDialog = AlertDialog.Builder(this).create() + alertDialog.setTitle("Permission Rationale") + alertDialog.setMessage("Show detail alert message about why this permission is needed.") + alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", + DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() }) + alertDialog.show() +} \ No newline at end of file diff --git a/app/src/main/java/cchcc/learn/amu/util/UserPermission.kt b/app/src/main/java/cchcc/learn/amu/util/UserPermission.kt index 360be17..cbebb10 100644 --- a/app/src/main/java/cchcc/learn/amu/util/UserPermission.kt +++ b/app/src/main/java/cchcc/learn/amu/util/UserPermission.kt @@ -13,10 +13,6 @@ class UserPermission(private val activity: Activity, vararg permissions: String) lateinit var isGrantedCallback: () -> Unit lateinit var notGrantedCallback: () -> Unit - fun isGranted(): Boolean = perms.all { ContextCompat.checkSelfPermission(activity, it) == PackageManager.PERMISSION_GRANTED } - - fun checkOrRequest(isGranted: () -> Unit) = checkOrRequest(isGranted) {} - /** * 1. check if permission is granted * 2. if permission is granted, callback `isGranted`, or request permission @@ -30,11 +26,15 @@ class UserPermission(private val activity: Activity, vararg permissions: String) if (permissionIsAllGranted) { isGranted() } else { - isGrantedCallback = isGranted - notGrantedCallback = notGranted - val requestCode = Math.abs((activity.hashCode() + isGranted.hashCode()).toShort().toInt()) - activity.permissions.put(requestCode, this) - ActivityCompat.requestPermissions(activity, notGrantedPermissions, requestCode) + if(shouldShowRequestRational()) { + activity.showAlert() + } else { + isGrantedCallback = isGranted + notGrantedCallback = notGranted + val requestCode = Math.abs((activity.hashCode() + isGranted.hashCode()).toShort().toInt()) + activity.permissions.put(requestCode, this) + ActivityCompat.requestPermissions(activity, notGrantedPermissions, requestCode) + } } }