-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShareWorker.kt
129 lines (118 loc) · 4.98 KB
/
ShareWorker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package me.zipi.navitotesla.background
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.os.Build
import androidx.concurrent.futures.CallbackToFutureAdapter
import androidx.core.app.NotificationCompat
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ForegroundInfo
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager
import androidx.work.WorkRequest
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.google.common.util.concurrent.ListenableFuture
import me.zipi.navitotesla.R
import me.zipi.navitotesla.service.NaviToTeslaService
import me.zipi.navitotesla.service.poifinder.PoiFinderFactory
import me.zipi.navitotesla.util.AnalysisUtil
class ShareWorker(context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) {
private val naviToTeslaService: NaviToTeslaService
private val channelId = "location_share_channel"
init {
naviToTeslaService = NaviToTeslaService(context)
}
override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
return CallbackToFutureAdapter.getFuture {
it.set(ForegroundInfo(1, createNotification()))
}
}
override fun doWork(): Result {
AnalysisUtil.log("Start share worker")
val inputData = inputData
val packageName = inputData.getString("packageName")!!
val notificationTitle = inputData.getString("notificationTitle")
val notificationText = inputData.getString("notificationText")
naviToTeslaService.share(packageName, notificationTitle, notificationText)
return Result.success()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
}
val notificationManager =
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// create channel
val mChannel =
NotificationChannel(channelId, "Share notification", NotificationManager.IMPORTANCE_LOW)
mChannel.setSound(null, null)
mChannel.setShowBadge(false)
mChannel.description = "차량에 위치를 공유할 때 알림이 나타납니다."
mChannel.vibrationPattern = longArrayOf(0L)
notificationManager.createNotificationChannel(mChannel)
}
private fun createNotification(): Notification {
val context = applicationContext
val packageName = inputData.getString("packageName")
val notificationText = inputData.getString("notificationText")
createNotificationChannel()
var poiName: String? = ""
var address = ""
if (packageName != null) {
val poiFinder = PoiFinderFactory.getPoiFinder(packageName)
address = poiFinder.parseDestination(notificationText ?: "")
if (!naviToTeslaService.isAddress(address)) {
poiName = address
}
}
val contentIntent = PendingIntent.getActivity(
context,
0,
context.packageManager.getLaunchIntentForPackage(context.packageName),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
return NotificationCompat.Builder(context, channelId)
.setContentIntent(contentIntent)
.setContentText(context.getString(R.string.sendingDestination) + "\n" + address)
.setTicker(context.getString(R.string.sendingDestination) + "\n" + poiName)
.setSmallIcon(R.drawable.ic_baseline_share_24)
.setAutoCancel(true)
.setVibrate(longArrayOf(0L))
.setSound(null)
.build();
}
companion object {
fun startShare(
context: Context,
packageName: String,
notificationTitle: String?,
notificationText: String?
) {
AnalysisUtil.log("Register share worker")
val workRequest: WorkRequest =
OneTimeWorkRequestBuilder<ShareWorker>()//(ShareWorker::class.java)
.setInputData(
Data.Builder()
.putString("packageName", packageName)
.putString("notificationTitle", notificationTitle)
.putString("notificationText", notificationText)
.build()
)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.build()
WorkManager.getInstance(context).enqueue(workRequest)
}
}
}