cake_wallet/lib/entities/background_tasks.dart

656 lines
22 KiB
Dart
Raw Normal View History

2024-07-25 16:38:12 +00:00
import 'dart:async';
import 'dart:io';
2024-08-29 18:29:37 +00:00
import 'dart:ui';
2024-12-18 21:36:39 +00:00
import 'package:battery_plus/battery_plus.dart';
2024-11-08 18:28:43 +00:00
import 'package:cake_wallet/bitcoin/bitcoin.dart';
2024-09-04 16:15:01 +00:00
import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/utils/device_info.dart';
import 'package:cake_wallet/utils/feature_flag.dart';
import 'package:cake_wallet/view_model/settings/sync_mode.dart';
2024-09-04 16:15:01 +00:00
import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
2024-12-18 21:36:39 +00:00
import 'package:connectivity_plus/connectivity_plus.dart';
2024-09-12 04:03:47 +00:00
import 'package:cw_bitcoin/electrum_wallet.dart';
2024-12-30 16:58:51 +00:00
import 'package:cw_core/node.dart';
import 'package:cw_core/sync_status.dart';
import 'package:cw_core/utils/print_verbose.dart';
2024-09-04 16:15:01 +00:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_type.dart';
2024-08-29 18:29:37 +00:00
import 'package:flutter/widgets.dart';
2024-08-28 18:54:47 +00:00
import 'package:flutter_background_service/flutter_background_service.dart';
2024-08-29 18:29:37 +00:00
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:cake_wallet/main.dart';
import 'package:cake_wallet/di.dart';
2025-01-06 21:23:29 +00:00
const initialNotificationTitle = "Cake Background Sync";
const standbyMessage = "On standby - app is in the foreground";
const readyMessage = "Ready to sync - waiting until the app has been in the background for a while";
const startMessage = "Starting sync - app is in the background";
const allWalletsSyncedMessage = "All wallets synced - waiting for next queue refresh";
2024-09-03 21:31:02 +00:00
const notificationId = 888;
2025-01-06 21:23:29 +00:00
const notificationChannelId = "cake_service";
const notificationChannelName = "CAKE BACKGROUND SERVICE";
const notificationChannelDescription = "Cake Wallet Background Service";
2024-09-03 21:50:06 +00:00
const DELAY_SECONDS_BEFORE_SYNC_START = 15;
2024-09-17 19:35:25 +00:00
const spNodeNotificationMessage =
"Currently configured Bitcoin node does not support Silent Payments. skipping wallet";
2024-09-18 18:32:01 +00:00
const SYNC_THRESHOLD = 0.98;
2024-12-18 21:20:13 +00:00
Duration REFRESH_QUEUE_DURATION = Duration(hours: 1);
2024-12-18 21:36:39 +00:00
bool syncOnBattery = false;
bool syncOnData = false;
2024-09-17 19:35:25 +00:00
void setMainNotification(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin, {
required String title,
required String content,
}) async {
2024-09-03 21:31:02 +00:00
flutterLocalNotificationsPlugin.show(
notificationId,
2024-09-17 19:35:25 +00:00
title,
content,
const NotificationDetails(
android: AndroidNotificationDetails(
notificationChannelId,
notificationChannelName,
2025-01-06 21:23:29 +00:00
icon: "ic_bg_service_small",
ongoing: true,
2024-09-17 21:20:14 +00:00
silent: true,
),
),
);
}
2024-09-17 19:35:25 +00:00
void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
flutterLocalNotificationsPlugin.cancelAll();
2024-09-17 19:35:25 +00:00
setMainNotification(
flutterLocalNotificationsPlugin,
title: initialNotificationTitle,
content: standbyMessage,
2024-09-03 21:31:02 +00:00
);
2024-08-29 18:29:37 +00:00
}
2024-09-17 19:35:25 +00:00
void setNotificationReady(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
2024-09-16 20:46:48 +00:00
flutterLocalNotificationsPlugin.cancelAll();
2024-09-17 19:35:25 +00:00
setMainNotification(
flutterLocalNotificationsPlugin,
title: initialNotificationTitle,
content: readyMessage,
);
}
2024-09-24 19:18:38 +00:00
void setNotificationStarting(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
2024-09-18 20:04:56 +00:00
flutterLocalNotificationsPlugin.cancelAll();
setMainNotification(
flutterLocalNotificationsPlugin,
title: initialNotificationTitle,
content: startMessage,
);
}
2024-12-19 18:28:57 +00:00
void setNotificationWalletsSynced(
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
2024-12-19 17:08:05 +00:00
flutterLocalNotificationsPlugin.cancelAll();
setMainNotification(
flutterLocalNotificationsPlugin,
title: initialNotificationTitle,
content: allWalletsSyncedMessage,
);
}
2024-09-18 02:31:11 +00:00
void setWalletNotification(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
{required String title, required String content, required int walletNum}) async {
2024-09-16 20:46:48 +00:00
flutterLocalNotificationsPlugin.show(
2024-09-17 19:35:25 +00:00
notificationId + walletNum,
2024-09-18 02:31:11 +00:00
title,
content,
2024-09-17 19:35:25 +00:00
NotificationDetails(
2024-09-16 20:46:48 +00:00
android: AndroidNotificationDetails(
2024-09-17 19:35:25 +00:00
"${notificationChannelId}_$walletNum",
"${notificationChannelName}_$walletNum",
2025-01-06 21:23:29 +00:00
icon: "ic_bg_service_small",
2024-09-18 02:31:11 +00:00
ongoing: true,
2024-09-17 21:20:14 +00:00
silent: true,
2024-09-16 20:46:48 +00:00
),
),
);
}
2025-01-06 21:23:29 +00:00
@pragma("vm:entry-point")
2024-08-29 18:29:37 +00:00
Future<void> onStart(ServiceInstance service) async {
printV("BACKGROUND SERVICE STARTED");
2024-09-03 21:31:02 +00:00
bool bgSyncStarted = false;
Timer? _syncTimer;
2024-09-24 19:18:38 +00:00
Timer? _stuckSyncTimer;
2024-09-18 18:32:01 +00:00
Timer? _queueTimer;
2024-12-26 21:31:00 +00:00
List<WalletBase> syncingWallets = [];
List<WalletBase> standbyWallets = [];
2025-01-06 21:23:29 +00:00
Timer? _bgTimer;
int fgCount = 0;
2024-08-29 18:29:37 +00:00
2024-08-29 21:53:10 +00:00
// commented because the behavior appears to be bugged:
// DartPluginRegistrant.ensureInitialized();
2024-08-29 18:29:37 +00:00
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2024-12-26 21:31:00 +00:00
service.on("stopService").listen((event) async {
printV("STOPPING BACKGROUND SERVICE");
2024-09-03 21:31:02 +00:00
_syncTimer?.cancel();
2024-12-26 15:43:14 +00:00
_stuckSyncTimer?.cancel();
_queueTimer?.cancel();
2024-12-26 21:31:00 +00:00
try {
// stop all syncing wallets:
for (int i = 0; i < syncingWallets.length; i++) {
final wallet = syncingWallets[i];
await wallet.stopSync(isBackgroundSync: true);
}
// stop all standby wallets (just in case):
for (int i = 0; i < standbyWallets.length; i++) {
final wallet = standbyWallets[i];
await wallet.stopSync(isBackgroundSync: true);
}
} catch (e) {
printV("error stopping sync: $e");
}
// stop the service itself:
2024-09-03 21:31:02 +00:00
await service.stopSelf();
2024-08-29 18:29:37 +00:00
});
2024-12-26 21:31:00 +00:00
service.on("status").listen((event) async {
printV(event);
2024-08-29 18:29:37 +00:00
});
2024-12-26 21:31:00 +00:00
service.on("setForeground").listen((event) async {
2024-08-29 21:53:10 +00:00
bgSyncStarted = false;
2024-09-03 21:31:02 +00:00
_syncTimer?.cancel();
2024-09-03 21:50:06 +00:00
setNotificationStandby(flutterLocalNotificationsPlugin);
2024-08-29 21:53:10 +00:00
});
2025-01-06 21:23:29 +00:00
void setReady() {
setNotificationReady(flutterLocalNotificationsPlugin);
2025-01-06 21:23:29 +00:00
}
service.on("setReady").listen((event) async {
setReady();
});
service.on("foregroundPing").listen((event) async {
fgCount = 0;
});
2024-09-03 21:31:02 +00:00
// we have entered the background, start the sync:
2025-01-06 21:23:29 +00:00
void setBackground() async {
2024-08-29 18:29:37 +00:00
if (bgSyncStarted) {
return;
}
2024-08-29 18:29:37 +00:00
bgSyncStarted = true;
2024-09-03 21:31:02 +00:00
2024-09-03 21:50:06 +00:00
await Future.delayed(const Duration(seconds: DELAY_SECONDS_BEFORE_SYNC_START));
printV("STARTING SYNC FROM BG");
2024-09-18 20:04:56 +00:00
setNotificationStarting(flutterLocalNotificationsPlugin);
2024-08-29 18:29:37 +00:00
2024-09-03 21:31:02 +00:00
try {
2024-09-11 21:25:00 +00:00
await initializeAppConfigs(loadWallet: false);
2024-09-03 21:31:02 +00:00
} catch (_) {
2024-09-03 21:50:06 +00:00
// these errors still show up in logs which doesn't really make sense to me
2024-09-03 21:31:02 +00:00
}
2024-08-29 18:29:37 +00:00
printV("INITIALIZED APP CONFIGS");
2024-08-29 18:29:37 +00:00
2024-09-17 19:35:25 +00:00
// final currentWallet = getIt.get<AppStore>().wallet;
// // don't start syncing immediately:
// await currentWallet?.stopSync();
2024-09-04 16:15:01 +00:00
final walletLoadingService = getIt.get<WalletLoadingService>();
2024-09-11 16:57:45 +00:00
final settingsStore = getIt.get<SettingsStore>();
final walletListViewModel = getIt.get<WalletListViewModel>();
2024-09-04 16:15:01 +00:00
2024-09-18 18:32:01 +00:00
// get all Monero / Wownero wallets and add them
2024-09-17 19:35:25 +00:00
final List<WalletListItem> moneroWallets = walletListViewModel.wallets
.where((element) => [WalletType.monero, WalletType.wownero].contains(element.type))
.toList();
2025-01-06 22:16:20 +00:00
printV("LOADING MONERO WALLETS");
2024-09-17 19:35:25 +00:00
for (int i = 0; i < moneroWallets.length; i++) {
final wallet = await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name);
2025-01-06 21:23:29 +00:00
// stop regular sync process if it's been started
2025-01-06 22:16:20 +00:00
// await wallet.stopSync(isBackgroundSync: false);
await Future.delayed(const Duration(seconds: 10));
2024-09-17 19:35:25 +00:00
syncingWallets.add(wallet);
}
2024-09-04 16:15:01 +00:00
2025-01-06 22:16:20 +00:00
printV("MONERO WALLETS LOADED");
2024-09-18 18:32:01 +00:00
// get all litecoin wallets and add them:
2024-09-17 19:35:25 +00:00
final List<WalletListItem> litecoinWallets = walletListViewModel.wallets
.where((element) => element.type == WalletType.litecoin)
.toList();
// we only need to sync the first litecoin wallet since they share the same collection of blocks
if (litecoinWallets.isNotEmpty) {
try {
final firstWallet = litecoinWallets.first;
final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name);
2024-09-18 18:32:01 +00:00
await wallet.stopSync();
2024-11-08 18:28:43 +00:00
if (bitcoin!.getMwebEnabled(wallet)) {
syncingWallets.add(wallet);
}
2024-09-17 19:35:25 +00:00
} catch (e) {
// couldn't connect to mwebd (most likely)
printV("error syncing litecoin wallet: $e");
2024-09-04 16:15:01 +00:00
}
2024-09-17 19:35:25 +00:00
}
2024-09-04 16:15:01 +00:00
2024-09-18 18:32:01 +00:00
// get all bitcoin wallets and add them:
2024-09-17 19:35:25 +00:00
final List<WalletListItem> bitcoinWallets =
walletListViewModel.wallets.where((element) => element.type == WalletType.bitcoin).toList();
for (int i = 0; i < bitcoinWallets.length; i++) {
try {
final wallet =
await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name);
2024-12-30 16:58:51 +00:00
var node = settingsStore.getCurrentNode(WalletType.bitcoin);
await wallet.connectToNode(node: node);
2024-09-17 19:35:25 +00:00
bool nodeSupportsSP = await (wallet as ElectrumWallet).getNodeSupportsSilentPayments();
if (!nodeSupportsSP) {
2024-12-30 16:58:51 +00:00
// printV("Configured node does not support silent payments, skipping wallet");
// setWalletNotification(
// flutterLocalNotificationsPlugin,
// title: initialNotificationTitle,
// content: spNodeNotificationMessage,
// walletNum: syncingWallets.length + 1,
// );
// spSupported = false;
// continue;
node = Node(uri: "electrs.cakewallet.com:50001");
await wallet.connectToNode(node: node);
}
2024-09-17 21:20:14 +00:00
2024-09-18 02:31:11 +00:00
await wallet.stopSync();
2024-09-17 23:50:30 +00:00
syncingWallets.add(wallet);
2024-09-17 19:35:25 +00:00
} catch (e) {
printV("error syncing bitcoin wallet_$i: $e");
}
2024-09-04 16:15:01 +00:00
}
printV("STARTING SYNC TIMER");
2024-12-19 18:28:57 +00:00
int syncedTicks = 0;
2024-09-03 21:31:02 +00:00
_syncTimer?.cancel();
2024-09-18 02:31:11 +00:00
_syncTimer = Timer.periodic(const Duration(milliseconds: 2000), (timer) async {
for (int i = 0; i < syncingWallets.length; i++) {
final wallet = syncingWallets[i];
2024-09-18 18:32:01 +00:00
final syncStatus = wallet.syncStatus;
final progress = wallet.syncStatus.progress();
final progressPercent = (progress * 100).toStringAsPrecision(5) + "%";
2024-12-18 21:20:13 +00:00
bool shouldSync = i == 0;
2024-09-17 19:35:25 +00:00
2024-09-18 18:32:01 +00:00
String title = "${walletTypeToCryptoCurrency(wallet.type).title} - ${wallet.name}";
late String content;
2024-09-18 18:32:01 +00:00
if (shouldSync) {
if (syncStatus is NotConnectedSyncStatus) {
printV("${wallet.name} NOT CONNECTED");
2024-09-18 18:32:01 +00:00
final node = settingsStore.getCurrentNode(wallet.type);
await wallet.connectToNode(node: node);
2024-12-26 21:31:00 +00:00
wallet.startSync(isBackgroundSync: true);
printV("STARTED SYNC");
2024-09-18 18:32:01 +00:00
}
2024-12-19 15:28:09 +00:00
if (progress > 0.999 || syncStatus is SyncedSyncStatus) {
syncedTicks++;
if (syncedTicks > 5) {
syncedTicks = 0;
printV("WALLET $i SYNCED");
2024-12-19 18:25:44 +00:00
try {
2024-12-26 21:31:00 +00:00
await wallet.stopSync(isBackgroundSync: true);
2024-12-19 18:25:44 +00:00
} catch (e) {
printV("error stopping sync: $e");
}
2024-12-19 15:28:09 +00:00
// pop the first wallet from the list
standbyWallets.add(syncingWallets.removeAt(i));
flutterLocalNotificationsPlugin.cancelAll();
2024-12-19 17:08:05 +00:00
// if all wallets are synced, show a one time notification saying so:
if (syncingWallets.isEmpty) {
setNotificationWalletsSynced(flutterLocalNotificationsPlugin);
}
2024-12-19 15:28:09 +00:00
continue;
}
2024-12-20 20:08:34 +00:00
} else {
syncedTicks = 0;
2024-12-19 15:28:09 +00:00
}
2024-09-18 18:32:01 +00:00
if (syncStatus is SyncingSyncStatus) {
final blocksLeft = syncStatus.blocksLeft;
2024-09-18 19:34:27 +00:00
content = "$blocksLeft Blocks Left";
2024-09-18 18:32:01 +00:00
} else if (syncStatus is SyncedSyncStatus) {
content = "Synced";
2024-09-18 18:32:01 +00:00
} else if (syncStatus is SyncedTipSyncStatus) {
final tip = syncStatus.tip;
content = "Scanned Tip: $tip";
2024-09-18 18:32:01 +00:00
} else if (syncStatus is NotConnectedSyncStatus) {
content = "Still Not Connected";
} else if (syncStatus is AttemptingSyncStatus) {
content = "Attempting Sync";
2024-09-18 18:32:01 +00:00
} else if (syncStatus is StartingScanSyncStatus) {
content = "Starting Scan";
} else if (syncStatus is SyncronizingSyncStatus) {
content = "Syncronizing";
} else if (syncStatus is FailedSyncStatus) {
content = "Failed Sync";
} else if (syncStatus is ConnectingSyncStatus) {
content = "Connecting";
} else {
2024-12-26 17:38:38 +00:00
// throw Exception("sync type not covered");
content = "Unknown Sync Status ${syncStatus.runtimeType}";
}
2024-12-19 18:28:57 +00:00
if (syncedTicks > 0) {
content += " - Finishing up...";
}
2024-09-18 18:32:01 +00:00
} else {
if (syncStatus is! NotConnectedSyncStatus) {
2024-12-26 21:31:00 +00:00
wallet.stopSync(isBackgroundSync: true);
2024-09-18 18:32:01 +00:00
}
if (progress < SYNC_THRESHOLD) {
content = "$progressPercent - Waiting in sync queue";
} else {
content = "$progressPercent - This shouldn't happen, wallet is > SYNC_THRESHOLD";
}
}
2024-09-11 19:33:11 +00:00
2024-12-19 17:08:05 +00:00
// content += " - ${DateFormat("hh:mm:ss").format(DateTime.now())}";
2024-09-18 18:32:01 +00:00
2024-12-19 17:08:05 +00:00
if (i == 0) {
setWalletNotification(
flutterLocalNotificationsPlugin,
title: title,
content: content,
walletNum: i,
);
}
2024-09-18 18:32:01 +00:00
}
2024-12-19 17:08:05 +00:00
// for (int i = 0; i < standbyWallets.length; i++) {
// int notificationIndex = syncingWallets.length + i + 1;
// final wallet = standbyWallets[i];
// final title = "${walletTypeToCryptoCurrency(wallet.type).title} - ${wallet.name}";
// String content = "Synced - on standby until next queue refresh";
// setWalletNotification(
// flutterLocalNotificationsPlugin,
// title: title,
// content: content,
// walletNum: notificationIndex,
// );
// }
2024-08-29 18:29:37 +00:00
});
2024-09-18 18:32:01 +00:00
_queueTimer?.cancel();
// add a timer that checks all wallets and adds them to the queue if they are less than SYNC_THRESHOLD synced:
2024-12-18 21:20:13 +00:00
_queueTimer = Timer.periodic(REFRESH_QUEUE_DURATION, (timer) async {
2024-12-18 21:36:39 +00:00
final batteryState = await Battery().batteryState;
bool onBattery = batteryState == BatteryState.connectedNotCharging ||
batteryState == BatteryState.discharging;
ConnectivityResult connectivityResult = await Connectivity().checkConnectivity();
bool onData = connectivityResult == ConnectivityResult.mobile;
if (onBattery && !syncOnBattery) {
return;
}
if (onData && !syncOnData) {
return;
}
2024-12-19 15:39:23 +00:00
// don't refresh the queue until we've finished syncing all wallets:
if (syncingWallets.isNotEmpty) {
return;
}
2024-09-18 18:32:01 +00:00
for (int i = 0; i < standbyWallets.length; i++) {
final wallet = standbyWallets[i];
final syncStatus = wallet.syncStatus;
// connect to the node if we haven't already:
if (syncStatus is NotConnectedSyncStatus) {
final node = settingsStore.getCurrentNode(wallet.type);
await wallet.connectToNode(node: node);
2024-12-26 21:31:00 +00:00
await wallet.startSync(isBackgroundSync: true);
2024-09-18 18:32:01 +00:00
}
2024-12-18 21:36:39 +00:00
// wait a while before checking progress:
2024-12-18 21:20:13 +00:00
await Future.delayed(const Duration(seconds: 20));
2024-09-18 18:32:01 +00:00
if (syncStatus.progress() < SYNC_THRESHOLD) {
syncingWallets.add(standbyWallets.removeAt(i));
}
}
});
2024-12-18 21:20:13 +00:00
2024-12-18 22:00:23 +00:00
// setup a watch dog to restart the wallet sync process if it appears to get stuck:
2024-09-24 19:18:38 +00:00
List<double> lastFewProgresses = [];
2024-12-19 15:39:23 +00:00
List<String> stuckWallets = [];
2024-09-24 19:18:38 +00:00
_stuckSyncTimer?.cancel();
_stuckSyncTimer = Timer.periodic(const Duration(seconds: 10), (timer) async {
2024-12-18 22:00:23 +00:00
if (syncingWallets.isEmpty) return;
final wallet = syncingWallets.first;
final syncStatus = wallet.syncStatus;
if (syncStatus is! SyncingSyncStatus) return;
lastFewProgresses.add(syncStatus.progress());
if (lastFewProgresses.length < 10) return;
// limit list size to 10:
while (lastFewProgresses.length > 10) {
lastFewProgresses.removeAt(0);
}
// if the progress is the same over the last 100 seconds, restart the sync:
if (lastFewProgresses.every((p) => p == lastFewProgresses.first)) {
printV("syncing appears to be stuck, restarting...");
2024-12-19 15:39:23 +00:00
try {
stuckWallets.add(wallet.name);
2024-12-26 21:31:00 +00:00
await wallet.stopSync(isBackgroundSync: true);
2024-12-19 15:39:23 +00:00
} catch (e) {
printV("error restarting sync: $e");
}
// if this wallet has been stuck more than twice, don't restart it, instead, add it to the standby list and try again on next queue refresh:
// check if stuckWallets contains wallet.name more than 2 times:
if (stuckWallets.where((name) => name == wallet.name).length > 2) {
printV("wallet ${wallet.name} has been stuck more than 2 times, adding to standby list");
standbyWallets.add(syncingWallets.removeAt(0));
stuckWallets = [];
return;
}
2024-12-26 21:31:00 +00:00
wallet.startSync(isBackgroundSync: true);
2024-12-18 22:00:23 +00:00
}
2024-09-24 19:18:38 +00:00
});
2025-01-06 21:23:29 +00:00
}
service.on("setBackground").listen((event) async {
setBackground();
});
// this is a backup timer to trigger in case the user fully closes the app, so that we still
// start the background sync process:
// annoyingly foreground code still runs in the background on android for some time, so we still use the original method
// to detect if we are in the background since it's much faster
_bgTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
fgCount++;
// we haven't been pinged in a while, so we are likely in the background:
if (fgCount == 4) {
setReady();
return;
}
if (fgCount > 10) {
fgCount = 0;
setBackground();
_bgTimer?.cancel();
}
});
}
2024-08-29 18:29:37 +00:00
@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();
return true;
}
2024-08-28 18:54:47 +00:00
2024-09-11 16:57:45 +00:00
Future<void> initializeService(FlutterBackgroundService bgService, bool useNotifications) async {
if (useNotifications) {
2024-09-11 19:33:11 +00:00
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
2024-09-11 16:57:45 +00:00
FlutterLocalNotificationsPlugin();
2024-08-29 18:29:37 +00:00
2024-09-11 16:57:45 +00:00
if (Platform.isIOS || Platform.isAndroid) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings('ic_bg_service_small'),
),
);
}
2024-08-29 18:29:37 +00:00
2024-09-11 19:33:11 +00:00
for (int i = 0; i < 10; i++) {
AndroidNotificationChannel channel = AndroidNotificationChannel(
"${notificationChannelId}_$i",
"${notificationChannelName}_$i",
description: notificationChannelDescription,
2024-09-17 21:20:14 +00:00
importance: Importance.min,
2024-12-26 17:15:18 +00:00
playSound: false,
showBadge: false,
enableVibration: false,
enableLights: false,
2024-09-11 19:33:11 +00:00
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
2024-08-29 18:29:37 +00:00
2024-09-11 16:57:45 +00:00
setNotificationStandby(flutterLocalNotificationsPlugin);
}
2024-08-29 21:53:10 +00:00
2024-09-03 21:31:02 +00:00
// notify the service that we are in the foreground:
bgService.invoke("setForeground");
2024-08-29 21:53:10 +00:00
2024-08-29 21:58:07 +00:00
try {
bool isServiceRunning = await bgService.isRunning();
if (isServiceRunning) {
printV("Service is ALREADY running!");
2024-08-29 21:58:07 +00:00
return;
}
} catch (_) {}
printV("INITIALIZING SERVICE");
2024-09-12 04:03:47 +00:00
2024-08-29 18:29:37 +00:00
await bgService.configure(
2024-08-28 18:54:47 +00:00
androidConfiguration: AndroidConfiguration(
onStart: onStart,
2024-08-29 18:29:37 +00:00
autoStart: true,
isForegroundMode: true,
2024-08-29 21:53:10 +00:00
notificationChannelId: notificationChannelId,
initialNotificationTitle: initialNotificationTitle,
initialNotificationContent: standbyMessage,
2024-08-29 21:53:10 +00:00
foregroundServiceNotificationId: notificationId,
2024-08-29 18:29:37 +00:00
foregroundServiceTypes: [AndroidForegroundType.dataSync],
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
onBackground: onIosBackground,
2024-08-28 18:54:47 +00:00
),
);
}
class BackgroundTasks {
2024-08-29 18:29:37 +00:00
FlutterBackgroundService bgService = FlutterBackgroundService();
2024-09-11 19:33:11 +00:00
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
2025-01-06 21:23:29 +00:00
Timer? _pingTimer;
2024-08-29 18:29:37 +00:00
void serviceBackground() {
bgService.invoke("setBackground");
2025-01-06 21:23:29 +00:00
_pingTimer?.cancel();
}
void foregroundPing() {
bgService.invoke("foregroundPing");
}
Future<void> serviceForeground() async {
final settingsStore = getIt.get<SettingsStore>();
bool showNotifications = settingsStore.showSyncNotification;
2025-01-06 21:23:29 +00:00
bgService.invoke("stopService");
await Future.delayed(const Duration(seconds: 2));
initializeService(bgService, showNotifications);
}
void serviceReady() {
final settingsStore = getIt.get<SettingsStore>();
bool showNotifications = settingsStore.showSyncNotification;
if (showNotifications) {
2025-01-06 21:23:29 +00:00
bgService.invoke("setReady");
2024-09-03 21:31:02 +00:00
}
}
void registerBackgroundService() async {
printV("REGISTER BACKGROUND SERVICE");
try {
final settingsStore = getIt.get<SettingsStore>();
final walletListViewModel = getIt.get<WalletListViewModel>();
bool hasMonero =
walletListViewModel.wallets.any((element) => element.type == WalletType.monero);
bool hasLitecoin =
walletListViewModel.wallets.any((element) => element.type == WalletType.litecoin);
2024-07-24 22:21:52 +00:00
bool hasBitcoin =
walletListViewModel.wallets.any((element) => element.type == WalletType.bitcoin);
2024-09-11 16:57:45 +00:00
if (!settingsStore.silentPaymentsAlwaysScan) {
hasBitcoin = false;
}
/// if its not android nor ios, or the user has no monero wallets; exit
2024-09-11 16:57:45 +00:00
if (!DeviceInfo.instance.isMobile || (!hasMonero && !hasLitecoin && !hasBitcoin)) {
return;
}
final SyncMode syncMode = settingsStore.currentSyncMode;
2024-09-17 19:35:25 +00:00
final bool useNotifications = settingsStore.showSyncNotification;
2024-12-18 21:20:13 +00:00
final bool syncEnabled = settingsStore.backgroundSyncEnabled;
2024-12-18 21:36:39 +00:00
syncOnBattery = settingsStore.backgroundSyncOnBattery;
syncOnData = settingsStore.backgroundSyncOnData;
2024-09-17 19:35:25 +00:00
if (useNotifications) {
2024-09-11 19:33:11 +00:00
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}
2024-09-17 19:35:25 +00:00
bgService.invoke("stopService");
2024-12-18 21:20:13 +00:00
if (!syncEnabled || !FeatureFlag.isBackgroundSyncEnabled) {
2024-09-17 19:35:25 +00:00
return;
}
2024-08-29 18:29:37 +00:00
2024-12-18 21:20:13 +00:00
REFRESH_QUEUE_DURATION = syncMode.frequency;
2024-11-08 18:28:43 +00:00
2025-01-06 21:23:29 +00:00
_pingTimer?.cancel();
_pingTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
getIt.get<BackgroundTasks>().foregroundPing();
});
2024-09-17 19:35:25 +00:00
await initializeService(bgService, useNotifications);
} catch (error, stackTrace) {
printV(error);
printV(stackTrace);
}
}
}