/EPermission

一个基于Kotlin实现的简单的Android权限申请框架

Primary LanguageKotlinApache License 2.0Apache-2.0

EPermission

A simple Android permission application framework based on Kotlin.

English | 中文

Guide

Project build.gradle

allprojects {
    repositories {
        ...
        maven { url 'https://www.jitpack.io' }
    }
}

Module build.gradle

dependencies {
    implementation 'com.github.RickyHal:EPermission:$latest_version'
}

Call directly in activity or fragment

// Request Storage permission
runWithPermissions(
    *EPermissions.STORAGE,
    onDenied = {
        Toast.makeText(this, "STORAGE permission denied", Toast.LENGTH_SHORT).show()
    },
    onDeniedForever = {
        Toast.makeText(this, "STORAGE permission denied forever", Toast.LENGTH_SHORT).show()
    },
    onAllGranted = {
        Toast.makeText(this, "STORAGE permission denied", Toast.LENGTH_SHORT).show()
    }
)

or

runWithStoragePermission(onFailed = {
    Toast.makeText(this, "STORAGE permission denied", Toast.LENGTH_SHORT).show()
}) {
    Toast.makeText(this, "STORAGE permission granted", Toast.LENGTH_SHORT).show()
}

Request multiple permissions

runWithPermissions(*EPermissions.CAMERA, *EPermissions.STORAGE,
    onDenied = { deniedList ->
        Toast.makeText(this, "permission denied $deniedList", Toast.LENGTH_SHORT).show()
    },
    onDeniedForever = { deniedForeverList ->
        Toast.makeText(this, "permission denied forever $deniedForeverList", Toast.LENGTH_SHORT).show()
    },
    onAllGranted = {
        Toast.makeText(this, "Permission all granted", Toast.LENGTH_SHORT).show()
    })

If you do not need to deal with the unsuccessful condition

runWithStoragePermission {
    Toast.makeText(this, "STORAGE permission granted", Toast.LENGTH_SHORT).show()
}

If you only have permission to perform some operations, you can use the following methods

doWhenPermissionGranted(*EPermissions.CAMERA) {
    Toast.makeText(this, "Do this when camera Permission is granted", Toast.LENGTH_SHORT).show()
}

Check permissions

if (checkPermissions(*EPermissions.CAMERA)) {
    Toast.makeText(this, "Camera Permission is granted", Toast.LENGTH_SHORT).show()
} else {
    Toast.makeText(this, "Camera Permission is not granted", Toast.LENGTH_SHORT).show()
}

License

Copyright 2021 RickyHal

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.