2024-07-25 16:38:12 +00:00
|
|
|
import 'dart:async';
|
2023-08-04 17:55:56 +00:00
|
|
|
import 'dart:io';
|
2024-08-29 18:29:37 +00:00
|
|
|
import 'dart:ui';
|
2023-08-04 17:55:56 +00:00
|
|
|
|
2024-09-04 16:15:01 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_loading_service.dart';
|
|
|
|
import 'package:cake_wallet/entities/preferences_key.dart';
|
2024-08-29 18:29:37 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2023-08-04 17:55:56 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
|
|
import 'package:cake_wallet/utils/device_info.dart';
|
2024-03-29 18:54:59 +00:00
|
|
|
import 'package:cake_wallet/utils/feature_flag.dart';
|
2023-08-04 17:55:56 +00:00
|
|
|
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';
|
2023-08-04 17:55:56 +00:00
|
|
|
import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
|
2024-09-12 04:03:47 +00:00
|
|
|
import 'package:cw_bitcoin/electrum_wallet.dart';
|
2024-09-04 16:15:01 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2023-08-04 17:55:56 +00:00
|
|
|
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';
|
2023-08-04 17:55:56 +00:00
|
|
|
import 'package:cake_wallet/main.dart';
|
|
|
|
import 'package:cake_wallet/di.dart';
|
2024-09-04 16:15:01 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-08-04 17:55:56 +00:00
|
|
|
|
|
|
|
const moneroSyncTaskKey = "com.fotolockr.cakewallet.monero_sync_task";
|
2024-07-24 22:21:52 +00:00
|
|
|
const mwebSyncTaskKey = "com.fotolockr.cakewallet.mweb_sync_task";
|
2023-08-04 17:55:56 +00:00
|
|
|
|
2024-09-03 21:31:02 +00:00
|
|
|
const initialNotificationTitle = 'Cake Background Sync';
|
|
|
|
const initialNotificationContent = 'On standby - app is in the foreground';
|
|
|
|
const notificationId = 888;
|
|
|
|
const notificationChannelId = 'cake_service';
|
2024-09-03 21:50:06 +00:00
|
|
|
const notificationChannelName = 'CAKE BACKGROUND SERVICE';
|
2024-09-03 21:31:02 +00:00
|
|
|
const notificationChannelDescription = 'Cake Wallet Background Service';
|
2024-09-03 21:50:06 +00:00
|
|
|
const DELAY_SECONDS_BEFORE_SYNC_START = 15;
|
2024-09-03 21:31:02 +00:00
|
|
|
|
|
|
|
void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
|
2024-09-11 19:33:11 +00:00
|
|
|
flutterLocalNotificationsPlugin.cancelAll();
|
2024-09-03 21:31:02 +00:00
|
|
|
flutterLocalNotificationsPlugin.show(
|
|
|
|
notificationId,
|
|
|
|
initialNotificationTitle,
|
|
|
|
initialNotificationContent,
|
|
|
|
const NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
notificationChannelId,
|
|
|
|
notificationChannelName,
|
|
|
|
icon: 'ic_bg_service_small',
|
|
|
|
ongoing: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-08-29 18:29:37 +00:00
|
|
|
}
|
2023-08-04 17:55:56 +00:00
|
|
|
|
2024-08-29 18:29:37 +00:00
|
|
|
@pragma('vm:entry-point')
|
|
|
|
Future<void> onStart(ServiceInstance service) async {
|
|
|
|
print("BACKGROUND SERVICE STARTED!");
|
2024-09-03 21:31:02 +00:00
|
|
|
bool bgSyncStarted = false;
|
|
|
|
Timer? _syncTimer;
|
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-09-03 21:31:02 +00:00
|
|
|
service.on('stopService').listen((event) async {
|
2024-09-03 21:50:06 +00:00
|
|
|
print("STOPPING BACKGROUND SERVICE!");
|
2024-09-03 21:31:02 +00:00
|
|
|
_syncTimer?.cancel();
|
|
|
|
await service.stopSelf();
|
2024-08-29 18:29:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
service.on('status').listen((event) async {
|
|
|
|
print(event);
|
|
|
|
});
|
|
|
|
|
2024-09-03 21:31:02 +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
|
|
|
});
|
|
|
|
|
2024-09-03 21:31:02 +00:00
|
|
|
// we have entered the background, start the sync:
|
|
|
|
service.on('setBackground').listen((event) async {
|
2024-08-29 18:29:37 +00:00
|
|
|
if (bgSyncStarted) {
|
|
|
|
return;
|
2023-08-04 17:55:56 +00:00
|
|
|
}
|
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));
|
2024-08-29 18:29:37 +00:00
|
|
|
print("STARTING SYNC FROM BG!!");
|
|
|
|
|
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
|
|
|
|
|
|
|
print("initialized app configs");
|
|
|
|
|
2024-09-04 16:15:01 +00:00
|
|
|
final currentWallet = getIt.get<AppStore>().wallet;
|
|
|
|
// don't start syncing immediately:
|
|
|
|
await currentWallet?.stopSync();
|
|
|
|
|
|
|
|
final walletLoadingService = getIt.get<WalletLoadingService>();
|
2024-09-11 16:57:45 +00:00
|
|
|
final settingsStore = getIt.get<SettingsStore>();
|
2024-09-04 16:15:01 +00:00
|
|
|
final typeRaw = getIt.get<SharedPreferences>().getInt(PreferencesKey.currentWalletType);
|
|
|
|
|
|
|
|
bool syncAll = true;
|
|
|
|
List<WalletBase?> syncingWallets = [];
|
|
|
|
|
|
|
|
if (syncAll) {
|
|
|
|
/// get all Monero wallets of the user and sync them
|
|
|
|
final List<WalletListItem> moneroWallets = getIt
|
|
|
|
.get<WalletListViewModel>()
|
|
|
|
.wallets
|
|
|
|
.where((element) => [WalletType.monero, WalletType.wownero].contains(element.type))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
for (int i = 0; i < moneroWallets.length; i++) {
|
|
|
|
final wallet =
|
|
|
|
await walletLoadingService.load(moneroWallets[i].type, moneroWallets[i].name);
|
|
|
|
final node = getIt.get<SettingsStore>().getCurrentNode(moneroWallets[i].type);
|
|
|
|
await wallet.connectToNode(node: node);
|
|
|
|
await wallet.startSync();
|
|
|
|
syncingWallets.add(wallet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all litecoin wallets and sync them:
|
|
|
|
final List<WalletListItem> litecoinWallets = getIt
|
|
|
|
.get<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) {
|
2024-09-11 21:25:00 +00:00
|
|
|
final firstWallet = litecoinWallets.first;
|
2024-09-04 16:15:01 +00:00
|
|
|
final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name);
|
|
|
|
final node = getIt.get<SettingsStore>().getCurrentNode(firstWallet.type);
|
|
|
|
await wallet.connectToNode(node: node);
|
|
|
|
await wallet.startSync();
|
|
|
|
syncingWallets.add(wallet);
|
|
|
|
}
|
2024-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
final List<WalletListItem> bitcoinWallets = getIt
|
|
|
|
.get<WalletListViewModel>()
|
|
|
|
.wallets
|
|
|
|
.where((element) => element.type == WalletType.bitcoin)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
for (int i = 0; i < bitcoinWallets.length; i++) {
|
|
|
|
final wallet =
|
|
|
|
await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name);
|
|
|
|
final node = getIt.get<SettingsStore>().getCurrentNode(bitcoinWallets[i].type);
|
|
|
|
await wallet.connectToNode(node: node);
|
|
|
|
await wallet.startSync();
|
2024-09-12 04:03:47 +00:00
|
|
|
// TODO: use proxy layer:
|
|
|
|
await (wallet as ElectrumWallet).setSilentPaymentsScanning(true);
|
2024-09-10 18:48:00 +00:00
|
|
|
syncingWallets.add(wallet);
|
|
|
|
}
|
2024-09-04 16:15:01 +00:00
|
|
|
} else {
|
|
|
|
// /// if the user chose to sync only active wallet
|
|
|
|
// /// if the current wallet is monero; sync it only
|
|
|
|
// if (typeRaw == WalletType.monero.index || typeRaw == WalletType.wownero.index) {
|
|
|
|
// final name = getIt.get<SharedPreferences>().getString(PreferencesKey.currentWalletName);
|
|
|
|
// wallet = await walletLoadingService.load(WalletType.values[typeRaw!], name!);
|
|
|
|
// final node = getIt.get<SettingsStore>().getCurrentNode(WalletType.values[typeRaw]);
|
|
|
|
// await wallet.connectToNode(node: node);
|
|
|
|
// await wallet.startSync();
|
|
|
|
// syncingWallets.add(wallet);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:03:47 +00:00
|
|
|
print("STARTING SYNC TIMER");
|
2024-09-03 21:31:02 +00:00
|
|
|
_syncTimer?.cancel();
|
2024-09-11 21:25:00 +00:00
|
|
|
_syncTimer = Timer.periodic(const Duration(milliseconds: 2000), (timer) {
|
2024-09-09 15:42:06 +00:00
|
|
|
// final wallet = getIt.get<AppStore>().wallet;
|
2024-09-10 18:48:00 +00:00
|
|
|
if (syncingWallets.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-11 16:57:45 +00:00
|
|
|
// final wallet = syncingWallets.first!;
|
|
|
|
// final syncProgress = ((wallet.syncStatus.progress() ?? 0) * 100).toStringAsPrecision(5);
|
2024-09-10 18:48:00 +00:00
|
|
|
// String title = "${wallet!.name} ${syncProgress}% Synced";
|
2024-09-11 21:25:00 +00:00
|
|
|
// flutterLocalNotificationsPlugin.show(
|
|
|
|
// notificationId,
|
|
|
|
// title,
|
|
|
|
// 'Background sync - ${DateTime.now()}',
|
|
|
|
// const NotificationDetails(
|
|
|
|
// android: AndroidNotificationDetails(
|
|
|
|
// notificationChannelId,
|
|
|
|
// notificationChannelName,
|
|
|
|
// icon: 'ic_bg_service_small',
|
|
|
|
// ongoing: true,
|
|
|
|
// ),
|
|
|
|
// ),
|
|
|
|
// );
|
2024-09-10 18:48:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < syncingWallets.length; i++) {
|
|
|
|
final wallet = syncingWallets[i];
|
|
|
|
final syncProgress = ((wallet!.syncStatus.progress()) * 100).toStringAsPrecision(5);
|
2024-09-11 21:25:00 +00:00
|
|
|
String title = "${wallet.name}-${syncProgress}% Synced";
|
2024-09-11 19:33:11 +00:00
|
|
|
|
|
|
|
flutterLocalNotificationsPlugin.show(
|
2024-09-11 19:36:16 +00:00
|
|
|
notificationId + i,
|
2024-09-11 19:33:11 +00:00
|
|
|
title,
|
|
|
|
'Background sync - ${DateTime.now()}',
|
|
|
|
NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
"${notificationChannelId}_$i",
|
|
|
|
"${notificationChannelName}_$i",
|
|
|
|
icon: 'ic_bg_service_small',
|
|
|
|
ongoing: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-09-10 18:48:00 +00:00
|
|
|
}
|
2024-08-29 18:29:37 +00:00
|
|
|
});
|
2023-08-04 17:55:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
importance: Importance.low,
|
|
|
|
);
|
|
|
|
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) {
|
|
|
|
print("Service is ALREADY running!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (_) {}
|
|
|
|
|
2024-09-12 04:03:47 +00:00
|
|
|
print("INITIALIZING SERVICE");
|
|
|
|
|
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: initialNotificationContent,
|
|
|
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:55:56 +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();
|
2024-08-29 18:29:37 +00:00
|
|
|
|
2024-09-11 16:57:45 +00:00
|
|
|
void updateServiceState(bool foreground, bool useNotifications) async {
|
2024-09-03 21:31:02 +00:00
|
|
|
if (foreground) {
|
|
|
|
bgService.invoke('stopService');
|
2024-09-11 19:33:11 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 2));
|
2024-09-11 16:57:45 +00:00
|
|
|
initializeService(bgService, useNotifications);
|
2024-09-03 21:31:02 +00:00
|
|
|
} else {
|
|
|
|
bgService.invoke("setBackground");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerBackgroundService() async {
|
2023-08-04 17:55:56 +00:00
|
|
|
try {
|
|
|
|
bool hasMonero = getIt
|
|
|
|
.get<WalletListViewModel>()
|
|
|
|
.wallets
|
|
|
|
.any((element) => element.type == WalletType.monero);
|
|
|
|
|
2024-07-24 22:21:52 +00:00
|
|
|
bool hasLitecoin = getIt
|
|
|
|
.get<WalletListViewModel>()
|
|
|
|
.wallets
|
|
|
|
.any((element) => element.type == WalletType.litecoin);
|
|
|
|
|
2024-09-11 16:57:45 +00:00
|
|
|
bool hasBitcoin = getIt
|
|
|
|
.get<WalletListViewModel>()
|
|
|
|
.wallets
|
|
|
|
.any((element) => element.type == WalletType.bitcoin);
|
|
|
|
|
|
|
|
final settingsStore = getIt.get<SettingsStore>();
|
|
|
|
if (!settingsStore.silentPaymentsAlwaysScan) {
|
|
|
|
hasBitcoin = false;
|
|
|
|
}
|
|
|
|
if (!settingsStore.mwebAlwaysScan) {
|
|
|
|
hasLitecoin = false;
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:55:56 +00:00
|
|
|
/// 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)) {
|
2023-08-04 17:55:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final SyncMode syncMode = settingsStore.currentSyncMode;
|
|
|
|
final bool syncAll = settingsStore.currentSyncAll;
|
|
|
|
|
2024-09-11 19:33:11 +00:00
|
|
|
if (settingsStore.showSyncNotification) {
|
|
|
|
flutterLocalNotificationsPlugin
|
|
|
|
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
|
|
|
?.requestNotificationsPermission();
|
|
|
|
}
|
|
|
|
|
2024-03-29 18:54:59 +00:00
|
|
|
if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) {
|
2024-08-29 18:29:37 +00:00
|
|
|
bgService.invoke('stopService');
|
2023-08-04 17:55:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-03 21:31:02 +00:00
|
|
|
bgService.invoke('stopService');
|
2024-08-29 18:29:37 +00:00
|
|
|
|
2024-09-11 16:57:45 +00:00
|
|
|
await initializeService(bgService, settingsStore.showSyncNotification);
|
2023-08-04 17:55:56 +00:00
|
|
|
} catch (error, stackTrace) {
|
|
|
|
print(error);
|
|
|
|
print(stackTrace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|