> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/caf-sdk/android/standalone-modules/cafsmartauth/permissions.md).

# Permissions

#### Guide to Implementing and Handling Runtime Permissions for Location in Your Android SDK (CafSmartAuth)

In Android, certain permissions, especially those related to sensitive data like location, require runtime approval from users. This guide explains how to check and request location permissions programmatically in your SDK, CafSmartAuth, using the `checkPermissionStatus` function provided below.

***

#### Why CafSmartAuth Requires Location Permissions

CafSmartAuth requires access to the user’s location as part of its fraud prevention features, ensuring an additional layer of security by verifying location-based information. Android distinguishes between two types of location access, allowing users to control the precision of the location data they share:

* **ACCESS\_FINE\_LOCATION**: Required for precise location information from GPS, which is essential for detecting potentially fraudulent activities with high accuracy.
* **ACCESS\_COARSE\_LOCATION**: Provides approximate location data based on Wi-Fi and cellular networks, suitable for basic location checks.

With these permission levels, users can provide the minimum required access for fraud prevention, while CafSmartAuth ensures compliance with Android's data privacy practices.

***

#### Step-by-Step: Adding and Using `checkPermissionStatus`

1. **Add Permissions to the Manifest**

   In your app's `AndroidManifest.xml`, include both location permissions:

   ```xml
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   ```

   These manifest declarations allow the app to request these permissions but don’t automatically grant them.
2. **Define a Request Code Constant**

   To identify the permission request, use a constant for the request code:

   ```kotlin
    companion object {
        private const val REQUEST_CODE = 1234
    }
   ```
3. **Create and Use `checkPermissionStatus` to Handle Permissions**

   The `checkPermissionStatus` function checks if permissions have been granted and, if not, requests them from the user. This is essential for compliance with Android’s runtime permission model introduced in Android 6.0 (API level 23).

   Here’s the code and an explanation of how it works:

   ```kotlin
    private fun checkPermissionStatus() {
        // Check for ACCESS_FINE_LOCATION permission
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // Request the ACCESS_FINE_LOCATION permission if it hasn't been granted
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                REQUEST_CODE
            )
        }

        // Check for ACCESS_COARSE_LOCATION permission
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // Request the ACCESS_COARSE_LOCATION permission if it hasn't been granted
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
                REQUEST_CODE
            )
        }
    }
   ```

   * `ContextCompat.checkSelfPermission`: This checks if the app has a specific permission. If the permission isn’t granted, it returns `PackageManager.PERMISSION_DENIED`.
   * `ActivityCompat.requestPermissions`: Requests the specified permission(s) from the user, displaying a system dialog for approval.
4. **Handle the Permission Result**

   When requesting permissions, you can handle the result in `onRequestPermissionsResult` to manage user responses appropriately.

   ```kotlin
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                // Permission was granted.
            } else {
                // Permission was denied.
            }
        }
    }
   ```
5. **Call `checkPermissionStatus` at the Right Time**

   You might want to call `checkPermissionStatus` within `onResume` or when location functionality is triggered, ensuring permissions are checked before they’re needed.

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.caf.io/caf-sdk/android/standalone-modules/cafsmartauth/permissions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
