-
Notifications
You must be signed in to change notification settings - Fork 42
/
IsolateHolderService.kt
136 lines (117 loc) · 4.46 KB
/
IsolateHolderService.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
130
131
132
133
134
135
136
package de.julianassmann.flutter_background
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import androidx.core.app.NotificationCompat
class IsolateHolderService : Service() {
companion object {
@JvmStatic
val ACTION_SHUTDOWN = "SHUTDOWN"
@JvmStatic
val ACTION_START = "START"
@JvmStatic
val WAKELOCK_TAG = "FlutterBackgroundPlugin:Wakelock"
@JvmStatic
val WIFILOCK_TAG = "FlutterBackgroundPlugin:WifiLock"
@JvmStatic
val CHANNEL_ID = "flutter_background"
@JvmStatic
private val TAG = "IsolateHolderService"
}
private var wakeLock: PowerManager.WakeLock? = null
private var wifiLock: WifiManager.WifiLock? = null
override fun onBind(intent: Intent) : IBinder? {
return null
}
override fun onCreate() {
FlutterBackgroundPlugin.loadNotificationConfiguration(applicationContext)
}
override fun onDestroy() {
cleanupService()
super.onDestroy()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int) : Int {
if (intent?.action == ACTION_SHUTDOWN) {
cleanupService()
stopSelf()
} else if (intent?.action == ACTION_START) {
startService()
}
return START_STICKY
}
private fun cleanupService() {
wakeLock?.apply {
if (isHeld) {
release()
}
}
if (FlutterBackgroundPlugin.enableWifiLock) {
wifiLock?.apply {
if (isHeld) {
release()
}
}
}
stopForeground(true)
}
@SuppressLint("WakelockTimeout")
private fun startService() {
val pm = applicationContext.packageManager
val notificationIntent =
pm.getLaunchIntentForPackage(applicationContext.packageName)
// See https://developer.android.com/guide/components/intents-filters#DeclareMutabilityPendingIntent
var flags = PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT > 23) flags = flags or PendingIntent.FLAG_IMMUTABLE
val pendingIntent = PendingIntent.getActivity(
this, 0,
notificationIntent, flags
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
FlutterBackgroundPlugin.notificationTitle,
FlutterBackgroundPlugin.notificationImportance).apply {
description = FlutterBackgroundPlugin.notificationText
}
channel.setShowBadge(FlutterBackgroundPlugin.showBadge)
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
val imageId = resources.getIdentifier(FlutterBackgroundPlugin.notificationIconName, FlutterBackgroundPlugin.notificationIconDefType, packageName)
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(FlutterBackgroundPlugin.notificationTitle)
.setContentText(FlutterBackgroundPlugin.notificationText)
.setSmallIcon(imageId)
.setContentIntent(pendingIntent)
.setPriority(FlutterBackgroundPlugin.notificationImportance)
.build()
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
wakeLock = newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG).apply {
setReferenceCounted(false)
acquire()
}
}
(applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).run {
wifiLock = createWifiLock(WifiManager.WIFI_MODE_FULL, WIFILOCK_TAG).apply {
setReferenceCounted(false)
acquire()
}
}
startForeground(1, notification)
}
override fun onTaskRemoved(rootIntent: Intent) {
super.onTaskRemoved(rootIntent)
cleanupService()
stopSelf()
}
}