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 2 : factory NotificationRecord.fromMap(Map<dynamic, dynamic> map) => NotificationRecord(
23 2 : id: map['id'].toString(),
24 2 : package: map['package'].toString(),
25 2 : title: map['title']?.toString() ?? '',
26 2 : text: map['text']?.toString() ?? '',
27 3 : 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 18 : static bool get isSupported => !kIsWeb && defaultTargetPlatform == TargetPlatform.android;
36 :
37 2 : const NotificationChannel();
38 :
39 2 : Future<bool> isGranted() async => await channel.invokeMethod<bool>('isGranted') ?? false;
40 :
41 2 : Future<void> openSettings() => channel.invokeMethod<void>('openSettings');
42 :
43 1 : Future<Map<String, String>> getApps() async {
44 1 : final result = await channel.invokeMapMethod<String, dynamic>('getApps') ?? {};
45 4 : return result.map((key, value) => MapEntry(key, value.toString()));
46 : }
47 :
48 1 : Future<Set<String>> getPackages() async =>
49 2 : (await channel.invokeListMethod<String>('getPackages') ?? const <String>[]).toSet();
50 :
51 1 : Future<void> setPackages(Iterable<String> packages) =>
52 3 : channel.invokeMethod<void>('setPackages', {'packages': packages.toList()});
53 :
54 6 : Future<void> setAutoPackages(Iterable<String> packages) =>
55 18 : channel.invokeMethod<void>('setAutoPackages', {'packages': packages.toList()});
56 :
57 2 : Future<bool> isBusy() async => await channel.invokeMethod<bool>('isBusy') ?? false;
58 :
59 3 : Future<void> done(int added) => channel.invokeMethod<void>('done', {'added': added});
60 :
61 1 : Future<List<NotificationRecord>> getNotifications() async {
62 1 : final result = await channel.invokeListMethod<Map<dynamic, dynamic>>('getNotifications') ?? const [];
63 2 : return result.map(NotificationRecord.fromMap).toList();
64 : }
65 :
66 1 : Future<void> removeNotifications(Iterable<String> ids) =>
67 3 : channel.invokeMethod<void>('removeNotifications', {'ids': ids.toList()});
68 : }
|