Skip to content

Commit

Permalink
fix: more aggressively keep app in foreground (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 authored Jul 15, 2022
1 parent e02605a commit d96bf23
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rtirl.chat">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<queries>
Expand Down
19 changes: 19 additions & 0 deletions lib/models/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_background/flutter_background.dart';
import 'package:rtchat/audio_channel.dart';
import 'package:rtchat/models/adapters/profiles.dart';
import 'package:rtchat/models/channels.dart';
Expand Down Expand Up @@ -67,6 +68,7 @@ class AudioModel extends ChangeNotifier {
_hostChannelStateSubscription = null;
_isOnline = false;
_syncWebViews();
_toggleBackground(false);
notifyListeners();
return;
}
Expand All @@ -75,10 +77,27 @@ class AudioModel extends ChangeNotifier {
.listen((isOnline) {
_isOnline = isOnline;
_syncWebViews();
_toggleBackground(isOnline);
notifyListeners();
});
}

// this isn't really the best place to put this function since it applies
// outside audio, but it's a convenient place since we already have the listener.
void _toggleBackground(bool enable) async {
const androidConfig = FlutterBackgroundAndroidConfig(
notificationTitle: "RealtimeChat",
notificationText: "RealtimeChat is running in the background",
);
if (enable) {
if (await FlutterBackground.initialize(androidConfig: androidConfig)) {
await FlutterBackground.enableBackgroundExecution();
}
} else if (FlutterBackground.isBackgroundExecutionEnabled) {
await FlutterBackground.disableBackgroundExecution();
}
}

bool get isSettingsVisible => _isSettingsVisible;

set isSettingsVisible(bool value) {
Expand Down
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_background:
dependency: "direct main"
description:
name: flutter_background
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
flutter_inappwebview:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies:
path_provider: ^2.0.10
in_app_purchase: ^3.0.4
firebase_database: ^9.0.18
flutter_background: ^1.1.0

dev_dependencies:
flutter_lints: ^2.0.1
Expand Down

0 comments on commit d96bf23

Please sign in to comment.