| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2024 The Android Open Source Project  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *     https://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +package com.example.snippets.location  | 
 | 18 | + | 
 | 19 | +import android.Manifest  | 
 | 20 | +import android.os.Build  | 
 | 21 | +import androidx.activity.ComponentActivity  | 
 | 22 | +import androidx.activity.result.contract.ActivityResultContracts  | 
 | 23 | +import androidx.annotation.RequiresApi  | 
 | 24 | + | 
 | 25 | +// Assuming this function is inside a ComponentActivity or a subclass of it  | 
 | 26 | +class LocationPermissionsActivity : ComponentActivity() {  | 
 | 27 | + | 
 | 28 | +    // [START android_location_requestpermissions_kotlin]  | 
 | 29 | +    @RequiresApi(Build.VERSION_CODES.N)  | 
 | 30 | +    fun requestPermissions() {  | 
 | 31 | +        val locationPermissionRequest = registerForActivityResult(  | 
 | 32 | +            ActivityResultContracts.RequestMultiplePermissions()  | 
 | 33 | +        ) { permissions ->  | 
 | 34 | +            when {  | 
 | 35 | +                permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {  | 
 | 36 | +                    // Precise location access granted.  | 
 | 37 | +                }  | 
 | 38 | +                permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {  | 
 | 39 | +                    // Only approximate location access granted.  | 
 | 40 | +                }  | 
 | 41 | +                else -> {  | 
 | 42 | +                    // No location access granted.  | 
 | 43 | +                }  | 
 | 44 | +            }  | 
 | 45 | +        }  | 
 | 46 | + | 
 | 47 | +        // Before you perform the actual permission request, check whether your app  | 
 | 48 | +        // already has the permissions, and whether your app needs to show a permission  | 
 | 49 | +        // rationale dialog. For more details, see Request permissions:  | 
 | 50 | +        // https://developer.android.com/training/permissions/requesting#request-permission  | 
 | 51 | +        locationPermissionRequest.launch(  | 
 | 52 | +            arrayOf(  | 
 | 53 | +                Manifest.permission.ACCESS_FINE_LOCATION,  | 
 | 54 | +                Manifest.permission.ACCESS_COARSE_LOCATION  | 
 | 55 | +            )  | 
 | 56 | +        )  | 
 | 57 | +    }  | 
 | 58 | +    // [END android_location_requestpermissions_kotlin]  | 
 | 59 | +}  | 
0 commit comments