Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E04 : Add alert dialog for shouldShowRequestRational and refactor unu… #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions app/src/main/java/cchcc/learn/amu/e04/E04Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
})
}

Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/cchcc/learn/amu/util/DialogUtil.kt
Original file line number Diff line number Diff line change
@@ -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()
}
18 changes: 9 additions & 9 deletions app/src/main/java/cchcc/learn/amu/util/UserPermission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}

Expand Down