-
Notifications
You must be signed in to change notification settings - Fork 92
/
ForegroundService.kt
262 lines (229 loc) · 9.42 KB
/
ForegroundService.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.pravera.flutter_foreground_task.service
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.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import androidx.core.app.NotificationCompat
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.embedding.engine.loader.FlutterLoader
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.view.FlutterCallbackInformation
import kotlinx.coroutines.*
/**
* Service class for implementing foreground service.
*
* @author Dev-hwang
* @version 1.0
*/
class ForegroundService: Service(), MethodChannel.MethodCallHandler {
companion object {
const val TAG = "ForegroundService"
var isRunningService = false
private set
}
private var flutterLoader: FlutterLoader? = null
private var prevFlutterEngine: FlutterEngine? = null
private var currFlutterEngine: FlutterEngine? = null
private var backgroundChannel: MethodChannel? = null
private var backgroundJob: Job? = null
private var serviceId: Int = 1000
private var notificationChannelId: String = ""
private var notificationChannelName: String = ""
private var notificationChannelDesc: String? = null
private var notificationChannelImportance: Int = 3
private var notificationPriority: Int = 0
private var notificationContentTitle: String = ""
private var notificationContentText: String = ""
private var enableVibration: Boolean = false
private var playSound: Boolean = true
private var showWhen: Boolean = false
private var visibility: Int = 1
private var iconResType: String? = null
private var iconResPrefix: String? = null
private var iconName: String? = null
private var taskInterval: Long = 5000L
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val prefs = applicationContext.getSharedPreferences(
ForegroundServicePrefsKey.PREFS_NAME, Context.MODE_PRIVATE)
notificationChannelId = prefs.getString(ForegroundServicePrefsKey.NOTIFICATION_CHANNEL_ID, notificationChannelId) ?: ""
notificationChannelName = prefs.getString(ForegroundServicePrefsKey.NOTIFICATION_CHANNEL_NAME, notificationChannelName) ?: ""
notificationChannelDesc = prefs.getString(ForegroundServicePrefsKey.NOTIFICATION_CHANNEL_DESC, notificationChannelDesc)
notificationChannelImportance = prefs.getInt(ForegroundServicePrefsKey.NOTIFICATION_CHANNEL_IMPORTANCE, notificationChannelImportance)
notificationPriority = prefs.getInt(ForegroundServicePrefsKey.NOTIFICATION_PRIORITY, notificationPriority)
notificationContentTitle = prefs.getString(ForegroundServicePrefsKey.NOTIFICATION_CONTENT_TITLE, notificationContentTitle) ?: ""
notificationContentText = prefs.getString(ForegroundServicePrefsKey.NOTIFICATION_CONTENT_TEXT, notificationContentText) ?: ""
enableVibration = prefs.getBoolean(ForegroundServicePrefsKey.ENABLE_VIBRATION, enableVibration)
playSound = prefs.getBoolean(ForegroundServicePrefsKey.PLAY_SOUND, playSound)
showWhen = prefs.getBoolean(ForegroundServicePrefsKey.SHOW_WHEN, showWhen)
visibility = prefs.getInt(ForegroundServicePrefsKey.VISIBILITY, visibility)
iconResType = prefs.getString(ForegroundServicePrefsKey.ICON_RES_TYPE, iconResType)
iconResPrefix = prefs.getString(ForegroundServicePrefsKey.ICON_RES_PREFIX, iconResPrefix)
iconName = prefs.getString(ForegroundServicePrefsKey.ICON_NAME, iconName)
taskInterval = prefs.getLong(ForegroundServicePrefsKey.TASK_INTERVAL, taskInterval)
when (intent?.action) {
ForegroundServiceAction.START,
ForegroundServiceAction.UPDATE -> {
startForegroundService()
val callbackHandle = if (prefs.contains(ForegroundServicePrefsKey.CALLBACK_HANDLE))
prefs.getLong(ForegroundServicePrefsKey.CALLBACK_HANDLE, 0L)
else
null
executeDartCallback(callbackHandle)
}
ForegroundServiceAction.REBOOT -> {
startForegroundService()
val callbackHandle = if (prefs.contains(ForegroundServicePrefsKey.CALLBACK_HANDLE_ON_BOOT))
prefs.getLong(ForegroundServicePrefsKey.CALLBACK_HANDLE_ON_BOOT, 0L)
else
null
executeDartCallback(callbackHandle)
}
ForegroundServiceAction.STOP -> stopForegroundService()
}
return super.onStartCommand(intent, flags, startId)
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onDestroy() {
super.onDestroy()
destroyForegroundTask()
}
@SuppressLint("WrongConstant")
private fun startForegroundService() {
val pm = applicationContext.packageManager
val iconResId = if (iconResType.isNullOrEmpty()
|| iconResPrefix.isNullOrEmpty()
|| iconName.isNullOrEmpty())
getAppIconResourceId(pm)
else
getDrawableResourceId(iconResType!!, iconResPrefix!!, iconName!!)
val pendingIntent = getPendingIntent(pm)
val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
notificationBuilder.setOngoing(true)
notificationBuilder.setShowWhen(showWhen)
notificationBuilder.setSmallIcon(iconResId)
notificationBuilder.setContentIntent(pendingIntent)
notificationBuilder.setContentTitle(notificationContentTitle)
notificationBuilder.setContentText(notificationContentText)
notificationBuilder.setVisibility(visibility)
if (!enableVibration) notificationBuilder.setVibrate(longArrayOf(0L))
if (!playSound) notificationBuilder.setSound(null)
notificationBuilder.priority = notificationPriority
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
notificationChannelId, notificationChannelName, notificationChannelImportance)
channel.description = notificationChannelDesc
channel.enableVibration(enableVibration)
if (!playSound) channel.setSound(null, null)
val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nm.createNotificationChannel(channel)
}
startForeground(serviceId, notificationBuilder.build())
isRunningService = true
}
private fun stopForegroundService() {
stopForeground(true)
stopSelf()
isRunningService = false
}
private fun executeDartCallback(callbackHandle: Long?) {
// If there is no callback handle, the code below will not be executed.
if (callbackHandle == null) return
// If there is an already initialized foreground task, destroy it and perform initialization.
if (currFlutterEngine != null) destroyForegroundTask()
currFlutterEngine = FlutterEngine(this)
flutterLoader = FlutterInjector.instance().flutterLoader()
flutterLoader!!.startInitialization(this)
flutterLoader!!.ensureInitializationComplete(this, null)
val messenger = currFlutterEngine!!.dartExecutor.binaryMessenger
backgroundChannel = MethodChannel(messenger, "flutter_foreground_task/background")
backgroundChannel!!.setMethodCallHandler(this)
val callbackInfo = FlutterCallbackInformation.lookupCallbackInformation(callbackHandle)
val appBundlePath = flutterLoader!!.findAppBundlePath()
val dartCallback = DartExecutor.DartCallback(assets, appBundlePath, callbackInfo)
currFlutterEngine!!.dartExecutor.executeDartCallback(dartCallback)
}
private fun startForegroundTask() {
if (backgroundJob != null) stopForegroundTask()
val handler = Handler(Looper.getMainLooper())
backgroundJob = GlobalScope.launch {
while (isActive) {
handler.post {
try {
backgroundChannel?.invokeMethod("event", null)
} catch (e: Exception) {
Log.e(TAG, "invokeMethod", e)
}
}
delay(taskInterval)
}
}
}
private fun stopForegroundTask() {
backgroundJob?.cancel()
backgroundJob = null
}
private fun destroyForegroundTask() {
stopForegroundTask()
flutterLoader = null
prevFlutterEngine = currFlutterEngine
currFlutterEngine = null
backgroundChannel?.invokeMethod("destroy", null, object : MethodChannel.Result {
override fun success(result: Any?) {
prevFlutterEngine?.destroy()
prevFlutterEngine = null
}
override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
prevFlutterEngine?.destroy()
prevFlutterEngine = null
}
override fun notImplemented() {
prevFlutterEngine?.destroy()
prevFlutterEngine = null
}
})
backgroundChannel?.setMethodCallHandler(null)
backgroundChannel = null
}
private fun getDrawableResourceId(resType: String, resPrefix: String, name: String): Int {
val resName = if (resPrefix.contains("ic"))
String.format("ic_%s", name)
else
String.format("img_%s", name)
return applicationContext.resources.getIdentifier(
resName, resType, applicationContext.packageName)
}
private fun getAppIconResourceId(pm: PackageManager): Int {
return try {
val appInfo = pm.getApplicationInfo(
applicationContext.packageName, PackageManager.GET_META_DATA)
appInfo.icon
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "getAppIconResourceId", e)
0
}
}
private fun getPendingIntent(pm: PackageManager): PendingIntent {
val launchIntent = pm.getLaunchIntentForPackage(applicationContext.packageName)
return PendingIntent.getActivity(this, 0, launchIntent, 0)
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
when (call.method) {
"initialize" -> startForegroundTask()
else -> result.notImplemented()
}
}
}