3. Kotlin User Group Serbia
https://www.meetup.com/Serbia-Kotlin-User-Group
https://www.facebook.com/kotlinserbia/
https://twitter.com/kotlin_serbia
4. Android permissions definition
● part of Android security system
● used to control access to system features, data, etc.
● apps are in sandbox
● needed permission needs to be annotated in
AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
5. Pre-Marshmallow
● targetSdkVersion < 23
● all permissions are granted in
install time
● less control for user
● no explanation for permission
usage
● when app is updated we need to
check additional permissions
6. After Marshmallow
● targetSdkVersion >= 23
● permissions are requested in
runtime
● all edge cases need to be
covered
● User can revoke permission from
settings screen
● Process is revoked
7. After Marshmallow
● targetSdkVersion >= 23
● permissions are requested in
runtime
● all edge cases need to be
covered
● User can revoke permission from
settings screen
● Process is revoked
8. Permission protection level
● Normal permissions
● Dangerous permissions
● Signature permissions
● Signature and system permissions
9. Permission protection level
● Normal permissions
● Dangerous permissions
● Signature permissions
● Signature and system permissions
10. Normal permissions
● granted when app is installed
● they are not risk for users data privacy and can’t affect other apps
Permission list:
● ACCESS_NETWORK_STATE
● BLUETOOTH
● INTERNET
● NFC
● VIBRATE
● WAKE_LOCK
● etc...
11. Permission protection level
● Normal permissions
● Dangerous permissions
● Signature permissions
● Signature and system permissions
12. Permission protection level
● Normal permissions
● Dangerous permissions
● Signature permissions
● Signature and system permissions
13. Dangerous permissions
● requested in runtime
● when permission from one group
is granted, all permissions in
group are granted
● they are risk for data privacy and
can affect other apps
47. Overview - before and now
class CameraActivity : AppCompatActivity() {
private val REQUEST_CAMERA_PERMISSION: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
setSupportActionBar(toolbar)
//Start camera preview
if (PermissionChecker.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
startCameraPreviewScreen()
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA)) {
showSnackBarWithExplanation()
} else {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA),
REQUEST_CAMERA_PERMISSION)
}
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults:
IntArray) {
when (requestCode) {
REQUEST_CAMERA_PERMISSION -> {
if (grantResults.filterNot { it == PackageManager.PERMISSION_GRANTED }.isEmpty()) {
showCameraPreviewScreen()
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.CAMERA)){
showSnackBarWithExplanation()
} else {
startGoToSettingsScreenIntent()
}
}
}
}
}
}
class CameraActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
setSupportActionBar(toolbar)
handlePermission(AppPermission.CAMERA,
onGranted = {
startCameraPreviewScreen()
},
onExplanationNeeded = {
showSnackbarExplanation
},
onDenied = {
requestPermission(it)
}
)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults:
IntArray) {
onPermissionRequestResultReceived(requestCode, grantResults,
onGranted = {
startCameraPreviewScreen()
},
onExplanationNeeded = {
showSnackbarExplanation()
},
onDenied = {
startGoToSettingsScreenIntent()
}
)
}
}
Before Now
48. Summary
● use PermissionChecker
● extension functions
● higher order functions
● use combination of two concepts to reduce amount of
boilerplate code
● use inline modifier
49. Resources
● RuntimePermissionsExtended
● Official Kotlin documentation
● Official Kotlin Github
● Kotlin koans
● Awesome Kotlin – collection of materials
● Slack kanal
● #droidconpl2015 - Shintaro Katafuchi 'Managing Runtime Permissions'
● Sonja Kesić from Endava: May I? - droidcon Zagreb 2016
● Runtime Permissions - official Android documentation
● PermissionsDispatcher library
53. Nebojša Vukšić
Android developer @ codecentric
Founder of Kotlin User Group Serbia
nebojsa92vuksic@gmail.com
Kotlin User Group Serbia
https://www.meetup.com/Serbia-Kotlin-User-Group
https://www.facebook.com/kotlinserbia/
https://twitter.com/kotlin_serbia
TheTechW0lf