Skip to content

merail/request-permissions-tool

Repository files navigation

Request Permissions Tool

Make work with Android permissions simpler. The library provides:

  • information about permissions using PermissionsInformer, such as its type, required min SDK, etc.

  • RuntimePermissionRequester for handling requests with single or multiple runtime permissions and responses for them

  • SpecialPermissionRequester to manage some special permissions

    .

  • RoleRequester for requesting roles

Add the library to a project

Groovy

dependencies {

    // other dependencies
    
    implementation 'com.github.merail:request-permissions-tool:1.0.0'
}

Kotlin

dependencies {

    // other dependencies
    
    implementation("com.github.merail:request-permissions-tool:1.0.0")
}

Usage

PermissionsInformer

val permissionsInformer = PermissionsInformer(
    activity = this,
)

if (permissionsInformer.isInstallTime(Manifest.permission.INTERNET)) {
    // do something
} else {
    // do something
}

Note

If you just need fast simple permission's type check, you can go to permissions-lists repository.

RuntimePermissionRequester

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    
    ...
    
    val runtimePermissionRequester = RuntimePermissionRequester(
        activity = this,
        requestedPermissions = arrayOf(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_CONTACTS,
        )
    )
    
    runtimePermissionRequester.requestPermissions { permissionsMap ->
        permissionsMap.entries.forEach { entry ->
            when (entry.value) {
                RuntimePermissionState.GRANTED -> // do something
                RuntimePermissionState.DENIED -> // do something
                RuntimePermissionState.IGNORED -> // do something
                RuntimePermissionState.PERMANENTLY_DENIED -> // do something
            }
        }
    }
}

Note

Since Android 11 you can't manually deny permission forever using checkbox

A second denial will block permission permanently. For first fast decision you can use SettingsSnackbar to handle this usecase

SpecialPermissionRequester

val specialPermissionRequester = SpecialPermissionRequester(
    activity = this,
    requestedPermission = Manifest.permission.MANAGE_EXTERNAL_STORAGE,
)

specialPermissionRequester.requestPermission { (permission, state) ->
    if (state == SpecialPermissionState.GRANTED) {
        // do something
    } else {
        // do something
    }
}

License

Copyright 2022-2024 Rail' Meshcherov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.