Skip to content

Commit 76eace1

Browse files
Okuro3499dogi
andauthored
all: smoother foreground location (fixes #2006) (#2007)
Co-authored-by: dogi <[email protected]>
1 parent bb7a6cb commit 76eace1

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Diff for: app/src/main/kotlin/io/treehouses/remote/utils/GPSService.kt

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.treehouses.remote.utils
22

33
import android.Manifest
4-
import android.annotation.SuppressLint
54
import android.app.Notification
65
import android.app.NotificationChannel
76
import android.app.NotificationManager
@@ -41,23 +40,21 @@ open class GPSService : Service(), LocationListener {
4140

4241
@RequiresApi(Build.VERSION_CODES.O)
4342
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
44-
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
45-
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
46-
ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
47-
48-
createNotificationChannel()
49-
val notificationIntent = Intent(this, InitialActivity::class.java)
50-
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE)
51-
val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID)
52-
val notification: Notification = notificationBuilder.setOngoing(true)
53-
.setContentTitle("GPS Service")
54-
.setContentText("Treehouses Remote is currently estimating your location")
55-
.setSmallIcon(R.drawable.treehouses2)
56-
.setContentIntent(pendingIntent)
57-
.build()
58-
59-
startForeground(NOTIFICATION_ID, notification)
43+
createNotificationChannel()
44+
val notificationIntent = Intent(this, InitialActivity::class.java)
45+
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE)
46+
val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID)
47+
val notification: Notification = notificationBuilder.setOngoing(true)
48+
.setContentTitle("GPS Service")
49+
.setContentText("Treehouses Remote is currently estimating your location")
50+
.setSmallIcon(R.drawable.treehouses2)
51+
.setContentIntent(pendingIntent)
52+
.build()
6053

54+
startForeground(NOTIFICATION_ID, notification)
55+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
56+
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
57+
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED) {
6158
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
6259
isGPSEnabled = locationManager?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true
6360
getLocation()
@@ -72,13 +69,16 @@ open class GPSService : Service(), LocationListener {
7269
return null
7370
}
7471

75-
@SuppressLint("MissingPermission")
7672
private fun getLocation() {
77-
locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_DISTANCE_CHANGE_FOR_UPDATES, MIN_TIME_BW_UPDATES.toFloat(), this)
78-
if (locationManager != null) {
79-
val location = locationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
80-
if (location != null) {
81-
onLocationChanged(location)
73+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
74+
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
75+
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE) == PackageManager.PERMISSION_GRANTED) {
76+
locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_DISTANCE_CHANGE_FOR_UPDATES, MIN_TIME_BW_UPDATES.toFloat(), this)
77+
if (locationManager != null) {
78+
val location = locationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
79+
if (location != null) {
80+
onLocationChanged(location)
81+
}
8282
}
8383
}
8484
}

0 commit comments

Comments
 (0)