This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathLocationRequiredFragment.kt
More file actions
91 lines (80 loc) · 3.32 KB
/
LocationRequiredFragment.kt
File metadata and controls
91 lines (80 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (C) 2020 The Android Open Source Project
*
* 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.
*/
package com.example.bluetoothlechat.bluetooth
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.bluetoothlechat.R
import com.example.bluetoothlechat.databinding.FragmentLocationRequiredBinding
// Fragment responsible for checking if the app has the ACCESS_FINE_LOCATION permission.
// This permission is required when using the BLE APIs so the user must grant permission
// to the app before viewing the BluetoothChatFragment or DeviceListFragment
class LocationRequiredFragment : Fragment() {
private var _binding: FragmentLocationRequiredBinding? = null
private val binding: FragmentLocationRequiredBinding
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentLocationRequiredBinding.inflate(inflater, container, false)
// hide the error messages while checking the permissions
binding.locationErrorMessage.visibility = View.GONE
binding.grantPermissionButton.visibility = View.GONE
// setup click listener on grant permission button
binding.grantPermissionButton.setOnClickListener {
checkLocationPermission()
}
return binding.root
}
override fun onStart() {
super.onStart()
// check location permission when the Fragment becomes visible on screen
checkLocationPermission()
}
private val startForResultRequestPermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
findNavController().navigate(R.id.action_start_chat)
} else {
showError()
}
}
private fun showError() {
binding.locationErrorMessage.visibility = View.VISIBLE
binding.grantPermissionButton.visibility = View.VISIBLE
}
private fun checkLocationPermission() {
val hasLocationPermission = ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
if (hasLocationPermission) {
// Navigate to the chat fragment
findNavController().navigate(R.id.action_start_chat)
} else {
startForResultRequestPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
}