forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_device_service.dart
More file actions
93 lines (78 loc) · 3.13 KB
/
Copy pathclient_device_service.dart
File metadata and controls
93 lines (78 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:omi/backend/preferences.dart';
/// Stable device-type labels returned by [ClientDeviceService.deviceProvenanceType].
///
/// The UI layer resolves these to localized strings via `context.l10n`.
enum DeviceProvenanceType { thisDevice, thisIphone, thisPhone, mac, iphone, android, other }
/// Shared stable per-installation device identity for provenance (not notifications-only).
class ClientDeviceService {
ClientDeviceService._();
static final ClientDeviceService instance = ClientDeviceService._();
String? _deviceIdHash;
Future<void> initialize() async {
_deviceIdHash = await _loadOrCreateDeviceIdHash();
}
String get deviceIdHash => _deviceIdHash ?? '';
String get platform => Platform.operatingSystem;
/// Contract: `{platform}_{hash}` — same shape as backend FCM `device_key`.
String get clientDeviceId {
final hash = deviceIdHash;
if (hash.isEmpty) return '';
return '${platform}_$hash';
}
/// Returns a semantic [DeviceProvenanceType] for the capture device, or null if
/// [primaryCaptureDevice] is absent. The UI layer resolves the enum to a localized
/// label — the service never hardcodes user-facing strings.
DeviceProvenanceType? deviceProvenanceType({String? primaryCaptureDevice}) {
if (primaryCaptureDevice == null || primaryCaptureDevice.isEmpty) {
return null;
}
if (primaryCaptureDevice == clientDeviceId) {
if (Platform.isIOS) return DeviceProvenanceType.thisIphone;
if (Platform.isAndroid) return DeviceProvenanceType.thisPhone;
return DeviceProvenanceType.thisDevice;
}
final platformPrefix = primaryCaptureDevice.split('_').first;
switch (platformPrefix) {
case 'macos':
return DeviceProvenanceType.mac;
case 'ios':
return DeviceProvenanceType.iphone;
case 'android':
return DeviceProvenanceType.android;
default:
return DeviceProvenanceType.other;
}
}
bool memoryMatchesThisDevice({String? primaryCaptureDevice, List<String> captureDeviceIds = const []}) {
final localId = clientDeviceId;
if (localId.isEmpty) return false;
if (primaryCaptureDevice == localId) return true;
return captureDeviceIds.contains(localId);
}
Future<String> _loadOrCreateDeviceIdHash() async {
final storedHash = SharedPreferencesUtil().deviceIdHash;
if (storedHash.isNotEmpty) {
return storedHash;
}
final deviceInfo = DeviceInfoPlugin();
String deviceIdentifier = '';
try {
if (Platform.isIOS) {
final iosInfo = await deviceInfo.iosInfo;
deviceIdentifier = iosInfo.identifierForVendor ?? '';
} else if (Platform.isAndroid) {
final androidInfo = await deviceInfo.androidInfo;
deviceIdentifier = androidInfo.id;
}
} catch (_) {
deviceIdentifier = DateTime.now().millisecondsSinceEpoch.toString();
}
final hash = sha256.convert(utf8.encode(deviceIdentifier)).toString().substring(0, 8);
SharedPreferencesUtil().deviceIdHash = hash;
return hash;
}
}