Skip to content

Commit

Permalink
chore: Update Foreground Task library, fix permissions for Android 34
Browse files Browse the repository at this point in the history
Fixes #144, #146
  • Loading branch information
qdot committed Sep 2, 2024
1 parent 5c93301 commit 9bd8295
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
9 changes: 7 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Expand All @@ -41,7 +42,11 @@
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data android:name="flutterEmbedding" android:value="2" />
<!-- Foreground service handling -->
<service android:name="com.pravera.flutter_foreground_task.service.ForegroundService" />
<!-- Warning: Do not change service name. -->
<!-- Add android:stopWithTask option only when necessary. -->
<service
android:name="com.pravera.flutter_foreground_task.service.ForegroundService"
android:foregroundServiceType="connectedDevice"
android:exported="false" />
</application>
</manifest>
60 changes: 29 additions & 31 deletions lib/bloc/engine/foreground_task_library_engine_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void startCallback() {
}

class IntifaceEngineTaskHandler extends TaskHandler {
SendPort? _sendPort;
final ReceivePort _serverMessageReceivePort;
final ReceivePort _backdoorMessageReceivePort;
final ReceivePort _shutdownReceivePort;
Expand All @@ -36,7 +35,7 @@ class IntifaceEngineTaskHandler extends TaskHandler {
message.message = outgoingMessage;
var engineMessage = EngineMessage();
engineMessage.engineProviderLog = message;
_sendPort!.send(jsonEncode(engineMessage));
FlutterForegroundTask.sendDataToMain(jsonEncode(engineMessage));
}

IntifaceEngineTaskHandler()
Expand All @@ -54,8 +53,7 @@ class IntifaceEngineTaskHandler extends TaskHandler {
}

@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
_sendPort = sendPort;
Future<void> onStart(DateTime timestamp) async {
_sendProviderLog("Info", "Trying to start engine in foreground service.");

// Due to the way the foregrounding package we're using works, we can't store the options across the foregrounding
Expand Down Expand Up @@ -87,7 +85,7 @@ class IntifaceEngineTaskHandler extends TaskHandler {
_stream!.listen((element) {
try {
// Send first
_sendPort!.send(element);
FlutterForegroundTask.sendDataToMain(element);
// Then check to see if this is a EngineStopped message.
// Try parsing the JSON first to make sure it's even valid JSON.
var jsonElement = jsonDecode(element);
Expand All @@ -111,18 +109,18 @@ class IntifaceEngineTaskHandler extends TaskHandler {
await _serverExited.future;
_sendProviderLog("INFO", "Engine shutdown successful");
// We'll never send a bool type over this port otherwise, so we can use that as a trigger to say we're done.
_sendPort!.send(false);
FlutterForegroundTask.sendDataToMain(false);
});
}

@override
Future<void> onRepeatEvent(DateTime timestamp, SendPort? sendPort) async {
Future<void> onRepeatEvent(DateTime timestamp) async {
// Send data to the main isolate.
//sendPort?.send(timestamp);
}

@override
Future<void> onDestroy(DateTime timestamp, SendPort? sendPort) async {
Future<void> onDestroy(DateTime timestamp) async {
_sendProviderLog("INFO", "Shutting down foreground task");
IsolateNameServer.removePortNameMapping(_kMainServerPortName);
IsolateNameServer.removePortNameMapping(_kMainBackdoorPortName);
Expand Down Expand Up @@ -196,49 +194,49 @@ class ForegroundTaskLibraryEngineProvider implements EngineProvider {
_backdoorSendPort!.send(msg);
}

Future<bool> _startForegroundTask() async {
bool reqResult;
Future<ServiceRequestResult> _startForegroundTask() async {
ServiceRequestResult reqResult;
if (await FlutterForegroundTask.isRunningService) {
reqResult = await FlutterForegroundTask.restartService();
} else {
reqResult = await FlutterForegroundTask.startService(
notificationTitle: 'Intiface Engine is running',
notificationText: 'Tap to return to the app',
notificationIcon: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
notificationButtons: [
const NotificationButton(id: 'stopServerButton', text: 'Stop Server'),
],
callback: startCallback,
);
}

ReceivePort? receivePort;
if (reqResult) {
receivePort = FlutterForegroundTask.receivePort;
}
_registerReceivePort();

return _registerReceivePort(receivePort);
return reqResult;
}

void _closeReceivePort() {
_receivePort?.close();
_receivePort = null;
}

bool _registerReceivePort(ReceivePort? receivePort) {
_closeReceivePort();

if (receivePort != null) {
_receivePort = receivePort;
_receivePort?.listen((message) {
if (message is String) {
_processMessageStream.add(message);
} else if (message is bool) {
logInfo("Shutdown complete message received, stopping foreground task.");
FlutterForegroundTask.stopService().then((_) => logInfo("Foreground task shutdown complete."));
}
});

return true;
void _onReceiveTaskData(message) {
if (message is String) {
_processMessageStream.add(message);
} else if (message is bool) {
logInfo("Shutdown complete message received, stopping foreground task.");
FlutterForegroundTask.stopService().then((_) => logInfo("Foreground task shutdown complete."));
}
}

return false;
void _registerReceivePort() {
// Remove the task if it already exists, just to make sure to clear things out.
FlutterForegroundTask.removeTaskDataCallback(_onReceiveTaskData);
FlutterForegroundTask.addTaskDataCallback(_onReceiveTaskData);
}

@override
Expand Down
13 changes: 3 additions & 10 deletions lib/intiface_central_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,14 @@ class IntifaceCentralApp extends StatelessWidget with WindowListener {
channelDescription: 'This notification appears when the Intiface Engine foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'stopServerButton', text: 'Stop Server'),
],
),
iosNotificationOptions: const IOSNotificationOptions(),
foregroundTaskOptions: const ForegroundTaskOptions(
interval: 1000,
foregroundTaskOptions: ForegroundTaskOptions(
eventAction: ForegroundTaskEventAction.repeat(1000),
allowWifiLock: true,
),
);
FlutterForegroundTask.initCommunicationPort();
}
}
var errorNotifierCubit = ErrorNotifierCubit();
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ packages:
dependency: "direct main"
description:
name: flutter_foreground_task
sha256: "6cf10a27f5e344cd2ecad0752d3a5f4ec32846d82fda8753b3fe2480ebb832a3"
sha256: "4962ffefe4352435900eb25734925f1ad002abf48e13c1ca22c9c63391be4f94"
url: "https://pub.dev"
source: hosted
version: "6.5.0"
version: "8.7.0"
flutter_launcher_icons:
dependency: "direct dev"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies:
# Needed for rust ffigen
ffi: ^2.1.0
flutter_bloc: ^8.1.6
flutter_foreground_task: ^6.1.2
flutter_foreground_task: ^8.7.0
flutter_local_notifications: ^17.2.2
flutter_loggy: ^2.0.2
flutter_markdown: ^0.6.18
Expand Down

0 comments on commit 9bd8295

Please sign in to comment.