1
1
package com.stop.ui.mission
2
2
3
+ import android.Manifest
3
4
import android.app.NotificationChannel
4
5
import android.app.NotificationManager
5
6
import android.content.Context
7
+ import android.content.pm.PackageManager
8
+ import android.os.Looper
9
+ import android.util.Log
10
+ import androidx.core.app.ActivityCompat
6
11
import androidx.core.app.NotificationCompat
7
12
import androidx.hilt.work.HiltWorker
8
13
import androidx.work.CoroutineWorker
9
14
import androidx.work.ForegroundInfo
10
15
import androidx.work.WorkManager
11
16
import androidx.work.WorkerParameters
17
+ import com.google.android.gms.location.*
12
18
import com.stop.R
13
- import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
14
19
import com.stop.isMoreThanOreo
15
- import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_TIME
16
- import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.LAST_TIME
20
+ import com.stop.model.Location
17
21
import dagger.assisted.Assisted
18
22
import dagger.assisted.AssistedInject
19
23
import kotlinx.coroutines.delay
20
24
21
25
@HiltWorker
22
26
class MissionWorker @AssistedInject constructor(
23
27
@Assisted context : Context ,
24
- @Assisted workerParameters : WorkerParameters
28
+ @Assisted workerParameters : WorkerParameters ,
29
+ private val missionManager : MissionManager
25
30
) : CoroutineWorker(context, workerParameters) {
26
31
27
32
private val notificationManager =
28
33
context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
29
34
35
+ private val fusedLocationClient = LocationServices .getFusedLocationProviderClient(context)
36
+ private val locationRequest = LocationRequest .Builder (Priority .PRIORITY_HIGH_ACCURACY , INTERVAL_UNIT ).build()
37
+ private lateinit var locationCallback: LocationCallback
38
+
30
39
override suspend fun doWork (): Result {
31
40
setForeground(createForegroundInfo())
32
-
41
+ initLocation()
42
+ test()
43
+ Log .d(" MissionWorker" ," personMovements ${missionManager.personMovements} " )
33
44
return Result .success()
34
45
}
35
46
47
+ private fun initLocation () {
48
+ if (ActivityCompat .checkSelfPermission(
49
+ applicationContext,
50
+ Manifest .permission.ACCESS_FINE_LOCATION
51
+ ) != PackageManager .PERMISSION_GRANTED && ActivityCompat .checkSelfPermission(
52
+ applicationContext,
53
+ Manifest .permission.ACCESS_COARSE_LOCATION
54
+ ) != PackageManager .PERMISSION_GRANTED
55
+ ) {
56
+ return
57
+ }
58
+ fusedLocationClient.lastLocation
59
+ .addOnSuccessListener { location ->
60
+ if (location == null ) {
61
+ Log .d(" MissionWorker" , " location get fail" )
62
+ } else {
63
+ Log .d(" MissionWorker" , " initLocation ${location.latitude} , ${location.longitude} " )
64
+ }
65
+ }
66
+ .addOnFailureListener {
67
+ Log .d(" MissionWorker" , " location error is ${it.message} " )
68
+ it.printStackTrace()
69
+ }
70
+
71
+ locationCallback = object : LocationCallback () {
72
+ override fun onLocationResult (locationResult : LocationResult ) {
73
+ for (location in locationResult.locations) {
74
+ if (location != null ) {
75
+ missionManager.personMovements.add(Location (location.latitude, location.longitude))
76
+ }
77
+ }
78
+ }
79
+ }
80
+ fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper .getMainLooper())
81
+
82
+ }
83
+
84
+ private fun getPersonLocation () {
85
+ locationCallback = object : LocationCallback () {
86
+ override fun onLocationResult (locationResult : LocationResult ) {
87
+ Log .d(" MissionWorker" , " entire locationResult ${locationResult.locations} " )
88
+ for (location in locationResult.locations) {
89
+ if (location != null ) {
90
+ val latitude = location.latitude
91
+ val longitude = location.longitude
92
+ Log .d(" MissionWorker" , " lat ${latitude} long ${longitude} " )
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+
100
+ private suspend fun test () {
101
+ while (NUM < 60 ) {
102
+ Log .d(" MissionWorker" , " 찍히나 테스트 ${NUM } " )
103
+ NUM + = 1
104
+ delay(1000 )
105
+ }
106
+ }
107
+
36
108
private fun createForegroundInfo (): ForegroundInfo {
37
109
val id = applicationContext.getString(R .string.mission_notification_channel_id)
38
110
val title = applicationContext.getString(R .string.mission_notification_title)
@@ -57,19 +129,21 @@ class MissionWorker @AssistedInject constructor(
57
129
58
130
private fun createChannel (id : String ) {
59
131
if (isMoreThanOreo()) {
132
+ Log .d(" MissionWorker" , " createChannel ${notificationManager.getNotificationChannel(id)} " )
60
133
if (notificationManager.getNotificationChannel(id) == null ) {
61
- val name = applicationContext.getString(R .string.notification_channel_name )
134
+ val name = applicationContext.getString(R .string.mission_notification_channel_name )
62
135
NotificationChannel (id, name, NotificationManager .IMPORTANCE_DEFAULT ).apply {
63
136
notificationManager.createNotificationChannel(this )
64
137
}
65
138
}
66
139
}
67
140
}
68
141
69
-
70
-
71
142
companion object {
72
- const val NOTIFICATION_ID = 12
143
+ const val NOTIFICATION_ID = 82
73
144
private const val NOTIFICATION_CONTENT = " 사용자의 위치를 추적중입니다."
145
+ private var NUM = 0
146
+ private const val INTERVAL_UNIT = 1000L
74
147
}
148
+
75
149
}
0 commit comments