@@ -6,9 +6,6 @@ import android.app.PendingIntent
6
6
import android.content.Context
7
7
import android.content.Context.NOTIFICATION_SERVICE
8
8
import android.content.Intent
9
- import android.location.Location
10
- import android.location.LocationListener
11
- import android.location.LocationManager
12
9
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
13
10
import android.os.Build
14
11
import android.os.Looper
@@ -18,6 +15,11 @@ import androidx.hilt.work.HiltWorker
18
15
import androidx.work.CoroutineWorker
19
16
import androidx.work.ForegroundInfo
20
17
import androidx.work.WorkerParameters
18
+ import com.google.android.gms.location.LocationCallback
19
+ import com.google.android.gms.location.LocationRequest
20
+ import com.google.android.gms.location.LocationResult
21
+ import com.google.android.gms.location.LocationServices
22
+ import com.google.android.gms.location.Priority
21
23
import com.whyranoid.presentation.R
22
24
import dagger.assisted.Assisted
23
25
import dagger.assisted.AssistedInject
@@ -30,9 +32,10 @@ class RunningWorker @AssistedInject constructor(
30
32
private val runningRepository : RunningRepository
31
33
) : CoroutineWorker(context, params) {
32
34
33
- private val locationManager =
34
- context.getSystemService(Context .LOCATION_SERVICE ) as LocationManager
35
- private lateinit var locationListener: LocationListener
35
+ private val fusedLocationClient = LocationServices .getFusedLocationProviderClient(context)
36
+ private val locationRequest =
37
+ LocationRequest .Builder (Priority .PRIORITY_HIGH_ACCURACY , UPDATE_INTERVAL_MS ).build()
38
+ private lateinit var locationCallback: LocationCallback
36
39
37
40
override suspend fun doWork (): Result {
38
41
if (startTracking().not ()) {
@@ -43,14 +46,14 @@ class RunningWorker @AssistedInject constructor(
43
46
setForeground(createForegroundInfo(context.getString(R .string.running_notification_content)))
44
47
45
48
while ((runningRepository.runningState.value is RunningState .NotRunning ).not ()) {
46
- delay(1000 )
49
+ delay(UPDATE_INTERVAL_MS )
47
50
when (runningRepository.runningState.value) {
48
51
is RunningState .NotRunning -> break
49
52
is RunningState .Paused -> continue
50
53
is RunningState .Running -> runningRepository.tick()
51
54
}
52
55
}
53
- locationManager.removeUpdates(locationListener )
56
+ fusedLocationClient.removeLocationUpdates(locationCallback )
54
57
return Result .success()
55
58
}
56
59
@@ -94,34 +97,20 @@ class RunningWorker @AssistedInject constructor(
94
97
}
95
98
96
99
private fun startTracking (): Boolean {
97
- locationListener = object : LocationListener {
98
- override fun onLocationChanged (location : Location ) {
99
- val lastLatitude = location.latitude
100
- val lastLongitude = location.longitude
101
-
102
- runningRepository.setRunningState(
103
- RunningPosition (lastLatitude, lastLongitude)
104
- )
105
- }
106
-
107
- override fun onProviderDisabled (provider : String ) {
108
- runningRepository.pauseRunning()
100
+ locationCallback = object : LocationCallback () {
101
+ override fun onLocationResult (locationResult : LocationResult ) {
102
+ locationResult.lastLocation?.let { location ->
103
+ runningRepository.setRunningState(location)
104
+ } ? : run {
105
+ runningRepository.pauseRunning()
106
+ }
109
107
}
110
108
}
111
109
112
110
try {
113
- locationManager.requestLocationUpdates(
114
- LocationManager .GPS_PROVIDER ,
115
- 1000 ,
116
- 0f ,
117
- locationListener,
118
- Looper .getMainLooper()
119
- )
120
- locationManager.requestLocationUpdates(
121
- LocationManager .NETWORK_PROVIDER ,
122
- 1000 ,
123
- 0f ,
124
- locationListener,
111
+ fusedLocationClient.requestLocationUpdates(
112
+ locationRequest,
113
+ locationCallback,
125
114
Looper .getMainLooper()
126
115
)
127
116
runningRepository.startRunning()
@@ -138,5 +127,6 @@ class RunningWorker @AssistedInject constructor(
138
127
const val WORKER_NAME = " runningWorker"
139
128
const val NOTIFICATION_ID = 1000
140
129
const val CHANNEL_ID = " 모각런"
130
+ const val UPDATE_INTERVAL_MS = 1000L
141
131
}
142
132
}
0 commit comments