Skip to content

Instantly share code, notes, and snippets.

@mrsasha

mrsasha/Activity.kt

Created Feb 4, 2020
Embed
What would you like to do?
Permission management
override fun onStart() {
super.onStart()
permissionManager.requestLocationPermission(this, object : LocationPermissionListener {
override fun permissionGranted() {
Timber.v("location permission granted!")
}
override fun permissionError(error: DexterError) {
Timber.w("location permission error: $error!")
}
})
}
dependencies {
implementation Libs.dexter
}
object Libs {
const val dexter = "com.karumi:dexter:6.0.1"
}
package com.beans.app.location
import android.Manifest
import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Resources
import android.net.Uri
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import com.beans.app.R
import com.karumi.dexter.Dexter
import com.karumi.dexter.PermissionToken
import com.karumi.dexter.listener.DexterError
import com.karumi.dexter.listener.PermissionDeniedResponse
import com.karumi.dexter.listener.PermissionGrantedResponse
import com.karumi.dexter.listener.PermissionRequest
import com.karumi.dexter.listener.single.PermissionListener
interface LocationPermissionListener {
fun permissionGranted()
fun permissionError(error: DexterError)
}
class LocationPermissionManager(val resources: Resources) {
fun requestLocationPermission(activity: Activity, listener: LocationPermissionListener) {
Dexter.withActivity(activity)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(object : PermissionListener {
override fun onPermissionGranted(response: PermissionGrantedResponse) {
listener.permissionGranted()
}
override fun onPermissionRationaleShouldBeShown(permission: PermissionRequest, token: PermissionToken) {
token.continuePermissionRequest()
}
override fun onPermissionDenied(response: PermissionDeniedResponse) {
openSettingsDialog(activity)
}
})
.withErrorListener { error -> listener.permissionError(error) }
.check()
}
private fun openSettingsDialog(activity: Activity) {
val builder = AlertDialog.Builder(activity)
builder.setTitle(resources.getString(R.string.permissions_location_title))
builder.setMessage(resources.getString(R.string.permissions_location_message))
builder.setPositiveButton(resources.getString(R.string.permissions_location_action_positive), DialogInterface.OnClickListener { dialog, _ ->
dialog.cancel()
openSettingsApp(activity)
})
builder.setNegativeButton(resources.getString(android.R.string.cancel), DialogInterface.OnClickListener { dialog, _ -> dialog.cancel() })
builder.show()
}
private fun openSettingsApp(activity: Activity) {
val intent = Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", activity.packageName, null)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
activity.startActivity(intent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment