Git Product home page Git Product logo

xxpermissions-ktx's Introduction

XXPermissions-ktx

English简体中文 (Simplified Chinese)

Library for Android runtime permissions

  • XXPermissions-ktx is the Kotlin extension library for XXPermissions, Easily request permissions using Kotlin DSL.
  • Provide Java compatible chain calling, which can also be easily used in Java.

Table of Contents

Integration steps

Add JitPack package repository

  • If your Gradle version is below 7.0, add JitPack in your root build.gradle.kts or build.gradle at the end of repositories:
// build.gradle.kts
allprojects {
    repositories {
        maven("https://jitpack.io")
    }
}
// build.gradle
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  • If your Gradle version is 7.0 or higher,add JitPack in your root settings.gradle.kts or settings.gradle at the end of repositories:
// build.gradle.kts
dependencyResolutionManagement {
    repositories {
        maven("https://jitpack.io")
    }
}
// build.gradle
dependencyResolutionManagement {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency

  • Add the dependency in your module build.gradle.kts or build.gradle
// build.gradle.kts
dependencies {
    implementation("com.github.Zhao-YinGang:XXPermissions-ktx:V1.6.0")
}
// build.gradle
dependencies {
    implementation 'com.github.Zhao-YinGang:XXPermissions-ktx:V1.6.0'
}

Migrate to AndroidX

XXPermissions-ktx is for AndroidX. Add the AndroidX migration configuration in your gradle.properties:

# Migrate third-party libraries to Android X
android.enableJetifier = true

Usage

  • Kotlin Basic usage
xxPermissions {
    // add location permissions
    permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
    // request result
    onResult { allGranted, grantedList, deniedList ->
        toast(
            "allGranted: " + allGranted +
                "\ngrantedList: " + grantedList +
                "\ndeniedList: " + deniedList
        )
    }
}
  • Kotlin Complete Usage
xxPermissions {
    // add bluetooth connect permission
    permissions(Permission.BLUETOOTH_CONNECT)
    // add camera permission
    permissions(Permission.CAMERA)
    // add location permissions
    permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
    // If you should show rationale before requesting permissions, this callback will be called
    onShouldShowRationale { shouldShowRationaleList, onUserResult ->
        // The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
        AlertDialog.Builder(this@requestDemo)
            .setTitle("Request Permissions")
            // Provide appropriate prompts based on rationale parameter
            .setMessage(
                "The application requires the following permissions: " +
                    permissionsNames(shouldShowRationaleList) +
                    "If these permissions are denied, some functions will be restricted."
            )
            .setNegativeButton("Cancel") { dialog, _ ->
                dialog.cancel()
            }
            .setPositiveButton("OK") { dialog, _ ->
                dialog.dismiss()
                // User clicked positive button, call onConsent callback to requesting permissions
                onUserResult.onResult(true)
            }
            .setOnCancelListener {
                onUserResult.onResult(false)
            }
            .show()
    }
    // permissions denied permanently
    onDoNotAskAgain { doNotAskAgainList, onUserResult ->
        // The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
        AlertDialog.Builder(this@requestDemo)
            .setTitle("Permissions permanently denied")
            // Provide appropriate prompts based on denied parameter
            .setMessage(
                "The following permissions have been permanently denied: " +
                    permissionsNames(denied.doNotAskAgainList) +
                    "Please enter the settings screen to grant permissions."
            )
            .setNegativeButton("Cancel") { dialog, _ ->
                dialog.cancel()
            }
            .setPositiveButton("Enter Settings") { dialog, _ ->
                dialog.dismiss()
                // User clicked positive button, call onConsent callback to entering settings screen
                onUserResult.onResult(true)
            }
            .setOnCancelListener {
                onUserResult.onResult(false)
            }
            .show()
    }
    // request result
    onResult { allGranted, grantedList, deniedList ->
        toast(
            "allGranted: " + allGranted +
                "\ngrantedList: " + grantedList +
                "\ndeniedList: " + deniedList
        )
    }
}
  • Java Complete Usage
public class RequestDemo4j {
    public static void run(Activity activity) {
        XXPermissions4j.with(activity)
            // add bluetooth connect permission
            .permissions(Permission.BLUETOOTH_CONNECT)
            // add camera permission
            .permissions(Permission.CAMERA)
            // add location permissions
            .permissions(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
            // If you should show rationale before requesting permissions, this callback will be called
            .onShouldShowRationale((shouldShowRationaleList, onUserResult) -> {
                // The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
                new AlertDialog.Builder(activity)
                    .setTitle("Request Permissions")
                    // Provide appropriate prompts based on rationale parameter
                    .setMessage(
                        "The application requires the following permissions: " +
                            RequestDemoKt.permissionsNames(rationale.getRationaleList()) +
                            "If these permissions are denied, some functions will be restricted."
                    )
                    .setNegativeButton("Cancel", (dialog, which) -> dialog.cancel())
                    .setPositiveButton("OK", (dialog, which) -> {
                        dialog.dismiss();
                        // User clicked positive button, call onConsent callback to requesting permissions
                        onUserResult.onResult(true);
                    })
                    .setOnCancelListener(dialog -> onUserResult.onResult(false))
                    .show();
            })
            .onDoNotAskAgain((doNotAskAgainList, onUserResult) -> {
                // The dialog here is just an example and does not use DialogFragment to handle the dialog lifecycle
                new AlertDialog.Builder(activity)
                    .setTitle("Permissions permanently denied")
                    // Provide appropriate prompts based on denied parameter
                    .setMessage(
                        "The following permissions have been permanently denied: " +
                            RequestDemoKt.permissionsNames(denied.getDoNotAskAgainList()) +
                            "Please enter the settings screen to grant permissions."
                    )
                    .setNegativeButton("Cancel", (dialog, which) -> dialog.cancel())
                    .setPositiveButton("OK", (dialog, which) -> {
                        dialog.dismiss();
                        // User clicked positive button, call onConsent callback to entering settings screen
                        onUserResult.onResult(true);
                    })
                    .setOnCancelListener(dialog -> onUserResult.onResult(false))
                    .show();
            })
            // request result
            .onResult((allGranted, grantedList, deniedList) -> ToastHelper.toast(activity,
                "allGranted: " + allGranted +
                    "\ngrantedList: " + grantedList +
                    "\ndeniedList: " + deniedList
            ))
            // request permissions
            .request();
    }
}

Example

To learn how to use XXPermissions-ktx, please refer to demo

Related Efforts

  • XXPermissions - Android permission request framework, adapted to Android 13

Maintainers

@Zhao-YinGang

Contributing

Feel free to dive in! Open an issue or submit PRs.

XXPermissions-ktx follows the Contributor Covenant Code of Conduct.

License

Apache-2.0 © Zhao-YinGang

xxpermissions-ktx's People

Contributors

zhao-yingang avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.