1
+ package com.stop
2
+
3
+ import android.app.NotificationChannel
4
+ import android.app.NotificationManager
5
+ import android.content.Context
6
+ import androidx.core.app.NotificationCompat
7
+ import androidx.hilt.work.HiltWorker
8
+ import androidx.work.CoroutineWorker
9
+ import androidx.work.ForegroundInfo
10
+ import androidx.work.WorkManager
11
+ import androidx.work.WorkerParameters
12
+ import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
13
+ import dagger.assisted.Assisted
14
+ import dagger.assisted.AssistedInject
15
+ import kotlinx.coroutines.delay
16
+
17
+ @HiltWorker
18
+ class LastTimeCheckWorker @AssistedInject constructor(
19
+ @Assisted context : Context ,
20
+ @Assisted workerParameters : WorkerParameters ,
21
+ private val getNearPlacesUseCase : GetNearPlacesUseCase
22
+ ) : CoroutineWorker(context, workerParameters) {
23
+
24
+ private val notificationManager =
25
+ context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
26
+
27
+ override suspend fun doWork (): Result {
28
+
29
+ setForeground(createForegroundInfo())
30
+ checkLastTransportTime()
31
+
32
+ return Result .success()
33
+ }
34
+
35
+ private fun createForegroundInfo (): ForegroundInfo {
36
+ val id = applicationContext.getString(R .string.notification_channel_id)
37
+ val title = applicationContext.getString(R .string.notification_title)
38
+ val cancel = applicationContext.getString(R .string.alarm_cancel_text)
39
+
40
+ val intent = WorkManager .getInstance(applicationContext)
41
+ .createCancelPendingIntent(getId())
42
+
43
+ createChannel(id)
44
+
45
+ val notification = NotificationCompat .Builder (applicationContext, id)
46
+ .setContentTitle(title)
47
+ .setTicker(title)
48
+ .setContentText(NOTIFICATION_CONTENT )
49
+ .setSmallIcon(R .mipmap.ic_bus)
50
+ .setOngoing(true ) // 사용자가 지우지 못하도록 막음
51
+ .addAction(android.R .drawable.ic_delete, cancel, intent)
52
+ .build()
53
+
54
+ return ForegroundInfo (NOTIFICATION_ID , notification)
55
+ }
56
+
57
+ private fun createChannel (id : String ) {
58
+ if (isMoreThanOreo()) {
59
+ if (notificationManager.getNotificationChannel(id) == null ) {
60
+ val name = applicationContext.getString(R .string.notification_channel_name)
61
+ NotificationChannel (id, name, NotificationManager .IMPORTANCE_DEFAULT ).apply {
62
+ notificationManager.createNotificationChannel(this )
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ private suspend fun checkLastTransportTime () {
69
+ while (isStopped.not ()) {
70
+ getNearPlacesUseCase.getNearPlaces(
71
+ " 아남타워" ,
72
+ 126.969652 ,
73
+ 37.553836
74
+ )
75
+
76
+ val delayTime = getDelayTime(" 18:18:00" )
77
+ if (delayTime == 0L ) {
78
+ this .onStopped()
79
+ } else {
80
+ delay(delayTime)
81
+ }
82
+ }
83
+ }
84
+
85
+ private fun getDelayTime (lastTime : String ): Long {
86
+ val fullLastTimeMillis = makeFullTime(lastTime).timeInMillis
87
+ val currentTimeMillis = System .currentTimeMillis()
88
+
89
+ return when (val diffTimeMillis =
90
+ if (fullLastTimeMillis > currentTimeMillis) fullLastTimeMillis - currentTimeMillis
91
+ else 0L
92
+ ) {
93
+ in THIRTY_MINUTES until Long .MAX_VALUE -> diffTimeMillis - THIRTY_MINUTES
94
+ in TWENTY_MINUTES until THIRTY_MINUTES -> FIVE_MINUTES
95
+ in TEN_MINUTES until TWENTY_MINUTES -> TWO_MINUTES
96
+ in 0 until TEN_MINUTES -> ONE_MINUTES
97
+ else -> 0
98
+ }
99
+ }
100
+
101
+ companion object {
102
+ const val NOTIFICATION_ID = 12
103
+ private const val NOTIFICATION_CONTENT = " 막차 시간이 변경되는지 계속 추적중입니다."
104
+ private const val THIRTY_MINUTES = 1_800_000L
105
+ private const val TWENTY_MINUTES = 1_200_000L
106
+ private const val TEN_MINUTES = 600_000L
107
+ private const val FIVE_MINUTES = 300_000L
108
+ private const val TWO_MINUTES = 120_000L
109
+ private const val ONE_MINUTES = 60_000L
110
+ }
111
+ }
0 commit comments