Line data Source code
1 : // Copyright 2026 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:flutter/foundation.dart';
5 : import 'package:flutter/services.dart';
6 :
7 : class NotificationRecord {
8 : final String id;
9 : final String package;
10 : final String title;
11 : final String text;
12 : final DateTime time;
13 :
14 1 : const NotificationRecord({
15 : required this.id,
16 : required this.package,
17 : required this.title,
18 : required this.text,
19 : required this.time,
20 : });
21 :
22 0 : factory NotificationRecord.fromMap(Map<dynamic, dynamic> map) => NotificationRecord(
23 0 : id: map['id'].toString(),
24 0 : package: map['package'].toString(),
25 0 : title: map['title']?.toString() ?? '',
26 0 : text: map['text']?.toString() ?? '',
27 0 : time: DateTime.fromMillisecondsSinceEpoch((map['time'] as num?)?.toInt() ?? 0),
28 : );
29 : }
30 :
31 : // Bridge to the Android notification listener (`NotificationParserService`)
32 : class NotificationChannel {
33 : static const channel = MethodChannel('com.tercad.fingrom/notifications');
34 :
35 0 : static bool get isSupported => !kIsWeb && defaultTargetPlatform == TargetPlatform.android;
36 :
37 0 : const NotificationChannel();
38 :
39 0 : Future<bool> isGranted() async => await channel.invokeMethod<bool>('isGranted') ?? false;
40 :
41 0 : Future<void> openSettings() => channel.invokeMethod<void>('openSettings');
42 :
43 0 : Future<Map<String, String>> getApps() async {
44 0 : final result = await channel.invokeMapMethod<String, dynamic>('getApps') ?? {};
45 0 : return result.map((key, value) => MapEntry(key, value.toString()));
46 : }
47 :
48 0 : Future<Set<String>> getPackages() async =>
49 0 : (await channel.invokeListMethod<String>('getPackages') ?? const <String>[]).toSet();
50 :
51 0 : Future<void> setPackages(Iterable<String> packages) =>
52 0 : channel.invokeMethod<void>('setPackages', {'packages': packages.toList()});
53 :
54 0 : Future<void> setAutoPackages(Iterable<String> packages) =>
55 0 : channel.invokeMethod<void>('setAutoPackages', {'packages': packages.toList()});
56 :
57 0 : Future<bool> isBusy() async => await channel.invokeMethod<bool>('isBusy') ?? false;
58 :
59 0 : Future<void> done(int added) => channel.invokeMethod<void>('done', {'added': added});
60 :
61 0 : Future<List<NotificationRecord>> getNotifications() async {
62 0 : final result = await channel.invokeListMethod<Map<dynamic, dynamic>>('getNotifications') ?? const [];
63 0 : return result.map(NotificationRecord.fromMap).toList();
64 : }
65 :
66 0 : Future<void> removeNotifications(Iterable<String> ids) =>
67 0 : channel.invokeMethod<void>('removeNotifications', {'ids': ids.toList()});
68 : }
|