Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 186b071

Browse files
Inform user if Location Services is disabled
1 parent 62db354 commit 186b071

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

LocationUpdatesBackgroundKotlin/app/src/main/java/com/google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ class LocationRepository private constructor(
8383
*/
8484
val receivingLocationUpdates: LiveData<Boolean> = myLocationManager.receivingLocationUpdates
8585

86+
/**
87+
* Status of whether the Location Services are available.
88+
*/
89+
val locationServicesAvailable: LiveData<Boolean> = myLocationManager.locationServicesAvailable
90+
8691
/**
8792
* Subscribes to location updates.
8893
*/

LocationUpdatesBackgroundKotlin/app/src/main/java/com/google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ private const val TAG = "MyLocationManager"
3838
*/
3939
class MyLocationManager private constructor(private val context: Context) {
4040

41-
private val _receivingLocationUpdates: MutableLiveData<Boolean> = MutableLiveData<Boolean>(false)
41+
private val _receivingLocationUpdates: MutableLiveData<Boolean> =
42+
MutableLiveData<Boolean>(false)
43+
private val _locationServicesAvailable: MutableLiveData<Boolean> =
44+
MutableLiveData<Boolean>(true)
4245

4346
/**
4447
* Status of location updates, i.e., whether the app is actively subscribed to location changes.
4548
*/
4649
val receivingLocationUpdates: LiveData<Boolean>
4750
get() = _receivingLocationUpdates
4851

52+
/**
53+
* Status of Location Services, i.e., whether the user has enabled Location Services.
54+
*/
55+
val locationServicesAvailable: LiveData<Boolean>
56+
get() = _locationServicesAvailable
57+
4958
// The Fused Location Provider provides access to location APIs.
5059
private val fusedLocationClient: FusedLocationProviderClient =
5160
LocationServices.getFusedLocationProviderClient(context)
@@ -83,7 +92,12 @@ class MyLocationManager private constructor(private val context: Context) {
8392
val intent = Intent(context, LocationUpdatesBroadcastReceiver::class.java)
8493
intent.action = LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES
8594
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
86-
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
95+
PendingIntent.getBroadcast(
96+
context,
97+
0,
98+
intent,
99+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
100+
)
87101
} else {
88102
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
89103
}
@@ -103,19 +117,33 @@ class MyLocationManager private constructor(private val context: Context) {
103117

104118
if (!context.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) return
105119

106-
try {
107-
_receivingLocationUpdates.value = true
108-
// If the PendingIntent is the same as the last request (which it always is), this
109-
// request will replace any requestLocationUpdates() called before.
110-
fusedLocationClient.requestLocationUpdates(locationRequest, locationUpdatePendingIntent)
111-
} catch (permissionRevoked: SecurityException) {
112-
_receivingLocationUpdates.value = false
113-
114-
// Exception only occurs if the user revokes the FINE location permission before
115-
// requestLocationUpdates() is finished executing (very rare).
116-
Log.d(TAG, "Location permission revoked; details: $permissionRevoked")
117-
throw permissionRevoked
118-
}
120+
fusedLocationClient.locationAvailability
121+
.addOnSuccessListener {
122+
if (!it.isLocationAvailable) {
123+
_receivingLocationUpdates.value = false
124+
_locationServicesAvailable.value = false
125+
// User has turned off Location Services
126+
Log.d(TAG, "Location services are no longer available!")
127+
} else {
128+
try {
129+
_receivingLocationUpdates.value = true
130+
_locationServicesAvailable.value = true
131+
// If the PendingIntent is the same as the last request (which it always is), this
132+
// request will replace any requestLocationUpdates() called before.
133+
fusedLocationClient.requestLocationUpdates(
134+
locationRequest,
135+
locationUpdatePendingIntent
136+
)
137+
} catch (permissionRevoked: SecurityException) {
138+
_receivingLocationUpdates.value = false
139+
140+
// Exception only occurs if the user revokes the FINE location permission before
141+
// requestLocationUpdates() is finished executing (very rare).
142+
Log.d(TAG, "Location permission revoked; details: $permissionRevoked")
143+
throw permissionRevoked
144+
}
145+
}
146+
}
119147
}
120148

121149
@MainThread
@@ -126,7 +154,8 @@ class MyLocationManager private constructor(private val context: Context) {
126154
}
127155

128156
companion object {
129-
@Volatile private var INSTANCE: MyLocationManager? = null
157+
@Volatile
158+
private var INSTANCE: MyLocationManager? = null
130159

131160
fun getInstance(context: Context): MyLocationManager {
132161
return INSTANCE ?: synchronized(this) {

LocationUpdatesBackgroundKotlin/app/src/main/java/com/google/android/gms/location/sample/locationupdatesbackgroundkotlin/ui/LocationUpdateFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.google.android.gms.location.sample.locationupdatesbackgroundkotlin.R
2828
import com.google.android.gms.location.sample.locationupdatesbackgroundkotlin.databinding.FragmentLocationUpdateBinding
2929
import com.google.android.gms.location.sample.locationupdatesbackgroundkotlin.hasPermission
3030
import com.google.android.gms.location.sample.locationupdatesbackgroundkotlin.viewmodels.LocationUpdateViewModel
31+
import com.google.android.material.snackbar.Snackbar
3132
import java.lang.StringBuilder
3233

3334
private const val TAG = "LocationUpdateFragment"
@@ -108,6 +109,13 @@ class LocationUpdateFragment : Fragment() {
108109
}
109110
}
110111
)
112+
113+
locationUpdateViewModel.locationServicesAvailable.observe(viewLifecycleOwner) {
114+
available ->
115+
if (!available) {
116+
Snackbar.make(view, R.string.enable_location_services_text, Snackbar.LENGTH_LONG).show()
117+
}
118+
}
111119
}
112120

113121
override fun onResume() {

LocationUpdatesBackgroundKotlin/app/src/main/java/com/google/android/gms/location/sample/locationupdatesbackgroundkotlin/viewmodels/LocationUpdateViewModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class LocationUpdateViewModel(application: Application) : AndroidViewModel(appli
3434

3535
val receivingLocationUpdates: LiveData<Boolean> = locationRepository.receivingLocationUpdates
3636

37+
val locationServicesAvailable: LiveData<Boolean> = locationRepository.locationServicesAvailable
38+
3739
val locationListLiveData = locationRepository.getLocations()
3840

3941
fun startLocationUpdates() = locationRepository.startLocationUpdates()

LocationUpdatesBackgroundKotlin/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@
5959

6060
<string name="batched_location_updates">Batched location updates</string>
6161
<string name="emptyLocationDatabaseMessage">To start receiving location updates, please click the button below!</string>
62+
<string name="enable_location_services_text">Please enable Location Services to receive location updates!</string>
6263
</resources>

0 commit comments

Comments
 (0)