Created
January 14, 2020 10:51
-
-
Save Chrispassold/535d46d66b759d1b505ed10c981b978c to your computer and use it in GitHub Desktop.
Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.net.Uri | |
import android.provider.Settings | |
import androidx.core.app.ActivityCompat | |
import androidx.core.content.ContextCompat | |
class PermissionHelper private constructor( | |
val requestCode: Int, | |
val permissions: Set<String>, | |
val listener: IPermissionRequest | |
) { | |
companion object { | |
fun openPermissionSettings(context: Context) { | |
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) | |
val uri: Uri = Uri.fromParts("package", context.packageName, null) | |
intent.data = uri | |
context.startActivity(intent) | |
} | |
} | |
fun requestPermission(activity: Activity) { | |
val permissionsNotGranted = permissions.filter { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED } | |
if (permissionsNotGranted.isNotEmpty()) { | |
val isAnyNeededExplainWhyNeedsPermission = permissionsNotGranted.any { | |
ActivityCompat.shouldShowRequestPermissionRationale( | |
activity, | |
it | |
) | |
} | |
if (isAnyNeededExplainWhyNeedsPermission) { | |
listener.expainToUserWhyWeNeedPermission(requestCode) | |
} else { | |
ActivityCompat.requestPermissions( | |
activity, | |
permissions.toTypedArray(), | |
requestCode | |
) | |
} | |
} | |
} | |
fun onRequestPermissionsResult( | |
requestCode: Int, | |
grantResults: IntArray | |
) { | |
if (requestCode != this.requestCode) | |
return | |
val isAllPermissionsGranted = grantResults.all { it == PackageManager.PERMISSION_GRANTED } | |
if (isAllPermissionsGranted) | |
listener.userGrantedPermission(requestCode) | |
else | |
listener.userDeniedPermission(requestCode) | |
} | |
interface IPermissionRequest { | |
/** | |
* Gets called when user grant permission. | |
* | |
* @param requestCode Request COde. | |
*/ | |
fun userGrantedPermission(requestCode: Int) | |
/** | |
* Gets called when user denied permission. | |
* | |
* @param requestCode Request Code. | |
*/ | |
fun userDeniedPermission(requestCode: Int) | |
/** | |
* Get called if we need to show expaination to user why we need this permission. | |
* todo: need to implement when should call this | |
* | |
* @param requestCode Request Code. | |
*/ | |
fun expainToUserWhyWeNeedPermission(requestCode: Int) | |
} | |
class Builder { | |
private val permissions: MutableSet<String> = mutableSetOf() | |
private var listener: IPermissionRequest? = null | |
fun addPermission(permission: String): Builder { | |
permissions.add(permission) | |
return this | |
} | |
fun addListener(listener: IPermissionRequest): Builder { | |
this.listener = listener | |
return this | |
} | |
fun build(requestCode: Int): PermissionHelper { | |
return PermissionHelper(requestCode, permissions, listener!!) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment