EPermission

GitHub

A simple Android permission application framework based on Kotlin implementation

The principle of

Encapsulate the permission request process by adding an invisible Fragment to the current Activity.

implementation

Invisible Fragment

internal class EPermissionFragment : Fragment() {
    private var mCallback: EPermissionCallback? = null

    fun requestPermission(callback: EPermissionCallback.vararg permissions: String) {
        mCallback = callback
        // Request permission
        requestPermissions(permissions, CODE_REQUEST_PERMISSION)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if (requestCode == CODE_REQUEST_PERMISSION) {
            val deniedList = ArrayList<String>()
            val deniedForeverList = ArrayList<String>()
            grantResults.forEachIndexed { index, result ->
                // Retrieve permission application results
                if(result ! = PackageManager.PERMISSION_GRANTED) {val permission = permissions[index]
                    deniedList.add(permission)
                    // Whether to reject and no longer display
                    if(! shouldShowRequestPermissionRationale(permission)) { deniedForeverList.add(permission) } } }if(deniedList.isEmpty()) mCallback? .onAllGranted()if(deniedList.isNotEmpty()) mCallback? .onDenied(deniedList)if(deniedForeverList.isNotEmpty()) mCallback? .onDeniedForever(deniedForeverList) } }override fun onDestroy(a) {
        mCallback = null
        super.onDestroy()
    }
}
Copy the code

Encapsulation Permission Application

/ / extension FragmentActivity
fun FragmentActivity.runWithPermissions(
    vararg permissions: String,
    onDenied: (ArrayList<String- > >)Unit = { _ -> },
    onDeniedForever: (ArrayList<String- > >)Unit = { _ -> },
    onAllGranted: () -> Unit= {}) {
    if (checkPermissions(*permissions)) {
        onAllGranted()
        return
    }
    // Add an invisible Fragment
    val isFragmentExist = supportFragmentManager.findFragmentByTag(EPermissionFragment.TAG)
    val fragment = if(isFragmentExist ! =null) {
        isFragmentExist as EPermissionFragment
    } else {
        val invisibleFragment = EPermissionFragment()
        supportFragmentManager.beginTransaction().add(invisibleFragment, EPermissionFragment.TAG).commitNowAllowingStateLoss()
        invisibleFragment
    }
    val callback = object : EPermissionCallback {
        override fun onAllGranted(a) {
            onAllGranted()
        }

        override fun onDenied(deniedList: ArrayList<String>) {
            onDenied(deniedList)
        }

        override fun onDeniedForever(deniedForeverList: ArrayList<String>) {
            onDeniedForever(deniedForeverList)
        }
    }
    // Request permission
    fragment.requestPermission(callback, *permissions)
}
Copy the code

Method of use

Add project build.gradle

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

Module build.gradle added

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

Called directly in an Activity or Fragment

// Apply for 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 granted", Toast.LENGTH_SHORT).show()
    }
)
Copy the code

You can also use this simple method

runWithStoragePermission(onFailed = {
    Toast.makeText(this."SMS permission denied", Toast.LENGTH_SHORT).show()
}) {
    Toast.makeText(this."SMS permission granted", Toast.LENGTH_SHORT).show()
}
Copy the code

Apply for multiple permissions at a time

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()
    })
Copy the code

If you do not need to handle the failure of applying for permission, you can directly write this parameter

runWithStoragePermission {
    Toast.makeText(this."SMS permission granted", Toast.LENGTH_SHORT).show()
}
Copy the code

You can use the following methods if you only have permission to perform certain operations

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

Check the 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()
}
Copy the code