-
Notifications
You must be signed in to change notification settings - Fork 166
/
flutter_background_service_android.dart
225 lines (189 loc) · 5.97 KB
/
flutter_background_service_android.dart
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
library flutter_background_service_android;
import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_background_service_platform_interface/flutter_background_service_platform_interface.dart';
bool _isMainIsolate = true;
@pragma('vm:entry-point')
Future<void> entrypoint(List<String> args) async {
WidgetsFlutterBinding.ensureInitialized();
_isMainIsolate = false;
final service = AndroidServiceInstance._();
final int handle = int.parse(args.first);
final callbackHandle = CallbackHandle.fromRawHandle(handle);
final onStart = PluginUtilities.getCallbackFromHandle(callbackHandle);
if (onStart != null) {
onStart(service);
}
}
class FlutterBackgroundServiceAndroid extends FlutterBackgroundServicePlatform {
/// Registers this class as the default instance of [FlutterBackgroundServicePlatform].
static void registerWith() {
FlutterBackgroundServicePlatform.instance =
FlutterBackgroundServiceAndroid();
}
FlutterBackgroundServiceAndroid._();
static final FlutterBackgroundServiceAndroid _instance =
FlutterBackgroundServiceAndroid._();
factory FlutterBackgroundServiceAndroid() {
if (!_isMainIsolate) {
throw Exception(
"This class should only be used in the main isolate (UI App)",
);
}
return _instance;
}
Future<void> _handleMethodCall(MethodCall call) async {
debugPrint(call.method);
switch (call.method) {
case "onReceiveData":
_controller.sink.add(call.arguments);
break;
default:
}
}
Future<bool> start() async {
final result = await _channel.invokeMethod('start');
return result ?? false;
}
final MethodChannel _channel = MethodChannel(
'id.flutter/background_service/android/method',
JSONMethodCodec(),
);
final EventChannel _eventChannel = EventChannel(
'id.flutter/background_service/android/event',
JSONMethodCodec(),
);
StreamSubscription<dynamic>? _eventChannelListener;
Future<bool> configure({
required IosConfiguration iosConfiguration,
required AndroidConfiguration androidConfiguration,
}) async {
_channel.setMethodCallHandler(_handleMethodCall);
_eventChannelListener?.cancel();
_eventChannelListener =
_eventChannel.receiveBroadcastStream().listen((event) {
_controller.sink.add(event);
});
final CallbackHandle? handle =
PluginUtilities.getCallbackHandle(androidConfiguration.onStart);
if (handle == null) {
throw 'onStart method must be a top-level or static function';
}
final result = await _channel.invokeMethod(
"configure",
{
"background_handle": handle.toRawHandle(),
"is_foreground_mode": androidConfiguration.isForegroundMode,
"auto_start": androidConfiguration.autoStart,
"auto_start_on_boot": androidConfiguration.autoStartOnBoot,
"initial_notification_content":
androidConfiguration.initialNotificationContent,
"initial_notification_title":
androidConfiguration.initialNotificationTitle,
"notification_channel_id": androidConfiguration.notificationChannelId,
"foreground_notification_id":
androidConfiguration.foregroundServiceNotificationId,
},
);
return result ?? false;
}
Future<bool> isServiceRunning() async {
var result = await _channel.invokeMethod("isServiceRunning");
return result ?? false;
}
final _controller = StreamController.broadcast(sync: true);
void dispose() {
_controller.close();
}
@override
void invoke(String method, [Map<String, dynamic>? args]) {
_channel.invokeMethod("sendData", {
'method': method,
'args': args,
});
}
@override
Stream<Map<String, dynamic>?> on(String method) {
return _controller.stream.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
if (data['method'] == method) {
sink.add(data['args']);
}
},
),
);
}
}
class AndroidServiceInstance extends ServiceInstance {
static const MethodChannel _channel = const MethodChannel(
'id.flutter/background_service_android_bg',
JSONMethodCodec(),
);
AndroidServiceInstance._() {
_channel.setMethodCallHandler(_handleMethodCall);
}
final _controller = StreamController.broadcast(sync: true);
Future<void> _handleMethodCall(MethodCall call) async {
switch (call.method) {
case "onReceiveData":
_controller.sink.add(call.arguments);
break;
default:
}
}
@override
void invoke(String method, [Map<String, dynamic>? args]) {
_channel.invokeMethod('sendData', {
'method': method,
'args': args,
});
}
@override
Future<void> stopSelf() async {
await _channel.invokeMethod("stopService");
}
@override
Stream<Map<String, dynamic>?> on(String method) {
return _controller.stream.transform(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
if (data['method'] == method) {
sink.add(data['args']);
}
},
),
);
}
Future<void> setForegroundNotificationInfo({
required String title,
required String content,
}) async {
await _channel.invokeMethod("setNotificationInfo", {
"title": title,
"content": content,
});
}
Future<void> setAsForegroundService() async {
await _channel.invokeMethod("setForegroundMode", {
'value': true,
});
}
Future<void> setAsBackgroundService() async {
await _channel.invokeMethod("setForegroundMode", {
'value': false,
});
}
/// returns true when the current Service instance is in foreground mode.
Future<bool> isForegroundService() async {
final result = await _channel.invokeMethod<bool>('isForegroundMode');
return result ?? false;
}
Future<void> setAutoStartOnBootMode(bool value) async {
await _channel.invokeMethod("setAutoStartOnBootMode", {
"value": value,
});
}
}