Skip to content

hendraanggrian/dangerous

Repository files navigation

Travis CI Codecov Maven Central Nexus Snapshot Android SDK

Dangerous

Lightweight library to simplify the process of requesting runtime permissions. Powered by the newer Activity Result APIs, so we no longer have to override onActivityResult().

  • Blocking call with requirePermission, or use Kotlin DSL with withPermission.
  • Force user to open settings when the permission is always declined.
requirePermission(Manifest.permission.CAMERA)
camera.start()

// or

withPermission(Manifest.permission.CAMERA) { isGranted ->
    if (isGranted) {
        camera.start()
    }
}

Download

repositories {
    google()
    mavenCentral()
}
dependencies {
    compile 'com.hendraanggrian.appcompat:dangerous:0.1'
}

Usage

To force user open Settings app, provide the second DSL.

withPermission(Manifest.permission.CAMERA, { settingsIntent ->
    AlertDialog.Builder(this)
        .setTitle("Permission Denied")
        .setMessage("Need to be enabled manually.")
        .setPositiveButton("Go to Settings") { _, _ ->
            startActivity(settingsIntent)
        }
}) { isGranted ->
    if (isGranted) {
        camera.start()
    }
}