Skip to content

Commit 5359380

Browse files
bngshyonghanJu
andcommitted
✨ locationListener -> fusedLocationProviderClient로 변경
Co-authored-by: yonghanJu <[email protected]>
1 parent 9fa4294 commit 5359380

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

presentation/src/main/java/com/whyranoid/presentation/running/RunningWorker.kt

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import android.app.PendingIntent
66
import android.content.Context
77
import android.content.Context.NOTIFICATION_SERVICE
88
import android.content.Intent
9-
import android.location.Location
10-
import android.location.LocationListener
11-
import android.location.LocationManager
129
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
1310
import android.os.Build
1411
import android.os.Looper
@@ -18,6 +15,11 @@ import androidx.hilt.work.HiltWorker
1815
import androidx.work.CoroutineWorker
1916
import androidx.work.ForegroundInfo
2017
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
2123
import com.whyranoid.presentation.R
2224
import dagger.assisted.Assisted
2325
import dagger.assisted.AssistedInject
@@ -30,9 +32,10 @@ class RunningWorker @AssistedInject constructor(
3032
private val runningRepository: RunningRepository
3133
) : CoroutineWorker(context, params) {
3234

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
3639

3740
override suspend fun doWork(): Result {
3841
if (startTracking().not()) {
@@ -43,14 +46,14 @@ class RunningWorker @AssistedInject constructor(
4346
setForeground(createForegroundInfo(context.getString(R.string.running_notification_content)))
4447

4548
while ((runningRepository.runningState.value is RunningState.NotRunning).not()) {
46-
delay(1000)
49+
delay(UPDATE_INTERVAL_MS)
4750
when (runningRepository.runningState.value) {
4851
is RunningState.NotRunning -> break
4952
is RunningState.Paused -> continue
5053
is RunningState.Running -> runningRepository.tick()
5154
}
5255
}
53-
locationManager.removeUpdates(locationListener)
56+
fusedLocationClient.removeLocationUpdates(locationCallback)
5457
return Result.success()
5558
}
5659

@@ -94,34 +97,20 @@ class RunningWorker @AssistedInject constructor(
9497
}
9598

9699
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+
}
109107
}
110108
}
111109

112110
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,
125114
Looper.getMainLooper()
126115
)
127116
runningRepository.startRunning()
@@ -138,5 +127,6 @@ class RunningWorker @AssistedInject constructor(
138127
const val WORKER_NAME = "runningWorker"
139128
const val NOTIFICATION_ID = 1000
140129
const val CHANNEL_ID = "모각런"
130+
const val UPDATE_INTERVAL_MS = 1000L
141131
}
142132
}

0 commit comments

Comments
 (0)