178 lines
4.8 KiB
Dart
178 lines
4.8 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:android_id/android_id.dart';
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:novyronst/common/api/api_string.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
@immutable
|
|
class NyDeviceInfo {
|
|
final String deviceId;
|
|
final String systemType;
|
|
final String systemVersion;
|
|
final String brand;
|
|
final String model;
|
|
|
|
final String appName;
|
|
final String appPackageName;
|
|
final String appVersion;
|
|
final String idfv;
|
|
|
|
static NyDeviceInfo? _instance;
|
|
|
|
const NyDeviceInfo._({
|
|
required this.deviceId,
|
|
required this.systemType,
|
|
required this.systemVersion,
|
|
required this.brand,
|
|
required this.model,
|
|
required this.appName,
|
|
required this.appPackageName,
|
|
required this.appVersion,
|
|
required this.idfv,
|
|
});
|
|
|
|
static NyDeviceInfo get instance {
|
|
if (_instance == null) {
|
|
throw StateError('error');
|
|
}
|
|
return _instance!;
|
|
}
|
|
|
|
@visibleForTesting
|
|
static void setMockInstance({
|
|
String deviceId = 'test-device-id',
|
|
String systemType = 'test',
|
|
String systemVersion = '1.0',
|
|
String brand = 'test',
|
|
String model = 'test',
|
|
String appName = 'Glimzo',
|
|
String appPackageName = 'com.example.skywood',
|
|
String appVersion = '1.0.0',
|
|
String idfv = 'test-idfv',
|
|
}) {
|
|
_instance = NyDeviceInfo._(
|
|
deviceId: deviceId,
|
|
systemType: systemType,
|
|
systemVersion: systemVersion,
|
|
brand: brand,
|
|
model: model,
|
|
appName: appName,
|
|
appPackageName: appPackageName,
|
|
appVersion: appVersion,
|
|
idfv: idfv,
|
|
);
|
|
}
|
|
|
|
/// 初始化设备信息
|
|
static Future<void> init() async {
|
|
if (_instance != null) return;
|
|
|
|
String deviceId = '',
|
|
systemType = '',
|
|
systemVersion = '',
|
|
brand = '',
|
|
model = '',
|
|
appName = 'Glimzo',
|
|
appPackageName = '',
|
|
appVersion = '',
|
|
idfv = '';
|
|
|
|
// 获取应用信息
|
|
await _initAppInfo()
|
|
.then((info) {
|
|
appPackageName = info['packageName'] ?? '';
|
|
appVersion = info['version'] ?? '';
|
|
appName = ApiString.appName;
|
|
})
|
|
.catchError((e) {
|
|
debugPrint('[sDeviceInfo] getElPackageName: $e');
|
|
});
|
|
|
|
// 获取设备信息
|
|
if (Platform.isAndroid) {
|
|
await _initAndroidInfo()
|
|
.then((info) {
|
|
deviceId = info['deviceId'] ?? '';
|
|
systemType = 'android';
|
|
systemVersion = info['systemVersion'] ?? '';
|
|
brand = info['brand'] ?? '';
|
|
model = info['model'] ?? '';
|
|
})
|
|
.catchError((e) {
|
|
debugPrint('[GlimzoUtilDeviceInfo] Android: $e');
|
|
});
|
|
} else if (Platform.isIOS) {
|
|
await _initIOSInfo()
|
|
.then((info) {
|
|
deviceId = info['deviceId'] ?? '';
|
|
idfv = info['idfv'] ?? '';
|
|
systemType = 'ios';
|
|
systemVersion = info['systemVersion'] ?? '';
|
|
brand = info['brand'] ?? '';
|
|
model = info['model'] ?? '';
|
|
})
|
|
.catchError((e) {
|
|
debugPrint('[GlimzoUtilDeviceInfo] iOS: $e');
|
|
});
|
|
}
|
|
|
|
_instance = NyDeviceInfo._(
|
|
deviceId: deviceId,
|
|
systemType: systemType,
|
|
systemVersion: systemVersion,
|
|
brand: brand,
|
|
model: model,
|
|
appName: appName,
|
|
appPackageName: appPackageName,
|
|
appVersion: appVersion,
|
|
idfv: idfv,
|
|
);
|
|
}
|
|
|
|
/// 获取应用信息
|
|
static Future<Map<String, String>> _initAppInfo() async {
|
|
final packageInfo = await PackageInfo.fromPlatform();
|
|
return {
|
|
'packageName': packageInfo.packageName,
|
|
'version': packageInfo.version,
|
|
};
|
|
}
|
|
|
|
/// 获取 Android 设备信息
|
|
static Future<Map<String, String?>> _initAndroidInfo() async {
|
|
final plugin = DeviceInfoPlugin();
|
|
final androidInfo = await plugin.androidInfo;
|
|
final androidIdPlugin = AndroidId();
|
|
final androidId = await androidIdPlugin.getId();
|
|
return {
|
|
'deviceId': androidId ?? androidInfo.id,
|
|
'systemVersion': androidInfo.version.release,
|
|
'brand': androidInfo.brand,
|
|
'model': androidInfo.model,
|
|
};
|
|
}
|
|
|
|
/// 获取 iOS 设备信息
|
|
static Future<Map<String, String?>> _initIOSInfo() async {
|
|
final plugin = DeviceInfoPlugin();
|
|
final iosInfo = await plugin.iosInfo;
|
|
final secureStorage = FlutterSecureStorage();
|
|
String? idfvStore = await secureStorage.read(key: 'identifierForVendor');
|
|
idfvStore ??= iosInfo.identifierForVendor;
|
|
|
|
if (idfvStore != null) {
|
|
await secureStorage.write(key: 'identifierForVendor', value: idfvStore);
|
|
}
|
|
|
|
return {
|
|
'deviceId': idfvStore,
|
|
'idfv': iosInfo.identifierForVendor ?? '',
|
|
'systemVersion': iosInfo.systemVersion,
|
|
'brand': iosInfo.model,
|
|
'model': iosInfo.modelName,
|
|
};
|
|
}
|
|
}
|