Skip to content

feat(device_info_plus): add storage information #3536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import kotlin.collections.HashMap
import android.provider.Settings
import android.os.StatFs
import android.os.Environment

/**
* The implementation of [MethodChannel.MethodCallHandler] for the plugin. Responsible for
Expand Down Expand Up @@ -57,6 +59,10 @@ internal class MethodCallHandlerImpl(
build["isPhysicalDevice"] = !isEmulator
build["systemFeatures"] = getSystemFeatures()

val statFs = StatFs(Environment.getDataDirectory().getPath())
build["freeDiskSpace"] = statFs.getFreeBytes()
build["totalDiskSpace"] = statFs.getTotalBytes()

val version: MutableMap<String, Any> = HashMap()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
version["baseOS"] = Build.VERSION.BASE_OS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call
if (@available(iOS 14.0, *)) {
isiOSAppOnMac = [NSNumber numberWithBool:[info isiOSAppOnMac]];
}
NSError *error = nil;
NSDictionary *fsAttributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
NSNumber *freeSize = [NSNumber numberWithInt:-1];
NSNumber *totalSize = [NSNumber numberWithInt:-1];
if(fsAttributes) {
freeSize = fsAttributes[NSFileSystemFreeSize];
totalSize = fsAttributes[NSFileSystemSize];
}

NSString *machine;
NSString *deviceName;
if ([self isDevicePhysical]) {
Expand All @@ -47,6 +56,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call
@"modelName" : deviceName,
@"identifierForVendor" : [[device identifierForVendor] UUIDString]
?: [NSNull null],
@"freeDiskSize": freeSize,
@"totalDiskSize": totalSize,
@"isPhysicalDevice" : isPhysicalNumber,
@"isiOSAppOnMac" : isiOSAppOnMac,
@"utsname" : @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
required this.tags,
required this.type,
required this.isPhysicalDevice,
required this.freeDiskSpace,
required this.totalDiskSpace,
required List<String> systemFeatures,
required this.serialNumber,
required this.isLowRamDevice,
Expand Down Expand Up @@ -121,6 +123,16 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
/// `false` if the application is running in an emulator, `true` otherwise.
final bool isPhysicalDevice;

/// Available disk space in bytes
///
/// https://developer.android.com/reference/android/os/StatFs#getFreeBytes()
final int freeDiskSpace;

/// Total disk space in bytes
///
/// https://developer.android.com/reference/android/os/StatFs#getTotalBytes()
final int totalDiskSpace;

/// Describes what features are available on the current device.
///
/// This can be used to check if the device has, for example, a front-facing
Expand Down Expand Up @@ -171,6 +183,8 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
tags: map['tags'],
type: map['type'],
isPhysicalDevice: map['isPhysicalDevice'],
freeDiskSpace: map['freeDiskSpace'],
totalDiskSpace: map['totalDiskSpace'],
systemFeatures: _fromList(map['systemFeatures'] ?? []),
serialNumber: map['serialNumber'],
isLowRamDevice: map['isLowRamDevice'],
Expand Down Expand Up @@ -200,6 +214,8 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
required String tags,
required String type,
required bool isPhysicalDevice,
required int freeDiskSpace,
required int totalDiskSpace,
required List<String> systemFeatures,
required String serialNumber,
required bool isLowRamDevice,
Expand Down Expand Up @@ -233,6 +249,8 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
'tags': tags,
'type': type,
'isPhysicalDevice': isPhysicalDevice,
'freeDiskSpace': freeDiskSpace,
'totalDiskSpace': totalDiskSpace,
'systemFeatures': systemFeatures,
'serialNumber': serialNumber,
'isLowRamDevice': isLowRamDevice,
Expand Down Expand Up @@ -260,6 +278,8 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
tags: tags,
type: type,
isPhysicalDevice: isPhysicalDevice,
freeDiskSpace: freeDiskSpace,
totalDiskSpace: totalDiskSpace,
systemFeatures: _fromList(systemFeatures),
serialNumber: serialNumber,
isLowRamDevice: isLowRamDevice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class IosDeviceInfo extends BaseDeviceInfo {
required this.model,
required this.modelName,
required this.localizedModel,
required this.freeDiskSize,
required this.totalDiskSize,
this.identifierForVendor,
required this.isPhysicalDevice,
required this.isiOSAppOnMac,
Expand Down Expand Up @@ -66,6 +68,12 @@ class IosDeviceInfo extends BaseDeviceInfo {
/// Operating system information derived from `sys/utsname.h`.
final IosUtsname utsname;

/// Free disk size in bytes
final int freeDiskSize;

/// Total disk size in bytes
final int totalDiskSize;

/// Deserializes from the map message received from [_kChannel].
static IosDeviceInfo fromMap(Map<String, dynamic> map) {
return IosDeviceInfo._(
Expand All @@ -77,6 +85,8 @@ class IosDeviceInfo extends BaseDeviceInfo {
modelName: map['modelName'],
localizedModel: map['localizedModel'],
identifierForVendor: map['identifierForVendor'],
freeDiskSize: map['freeDiskSize'],
totalDiskSize: map['totalDiskSize'],
isPhysicalDevice: map['isPhysicalDevice'],
isiOSAppOnMac: map['isiOSAppOnMac'],
utsname:
Expand All @@ -93,6 +103,8 @@ class IosDeviceInfo extends BaseDeviceInfo {
required String model,
required String modelName,
required String localizedModel,
required int freeDiskSize,
required int totalDiskSize,
String? identifierForVendor,
required bool isPhysicalDevice,
required bool isiOSAppOnMac,
Expand All @@ -106,6 +118,8 @@ class IosDeviceInfo extends BaseDeviceInfo {
'modelName': modelName,
'localizedModel': localizedModel,
'identifierForVendor': identifierForVendor,
'freeDiskSize': freeDiskSize,
'totalDiskSize': totalDiskSize,
'isPhysicalDevice': isPhysicalDevice,
'isiOSAppOnMac': isiOSAppOnMac,
'utsname': {
Expand All @@ -125,6 +139,8 @@ class IosDeviceInfo extends BaseDeviceInfo {
modelName: modelName,
localizedModel: localizedModel,
identifierForVendor: identifierForVendor,
freeDiskSize: freeDiskSize,
totalDiskSize: totalDiskSize,
isPhysicalDevice: isPhysicalDevice,
isiOSAppOnMac: isiOSAppOnMac,
utsname: utsname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const _fakeAndroidDeviceInfo = <String, dynamic>{
'display': 'display',
'hardware': 'hardware',
'isPhysicalDevice': true,
'freeDiskSpace': 70729949184,
'totalDiskSpace': 113281839104,
'bootloader': 'bootloader',
'fingerprint': 'fingerprint',
'manufacturer': 'manufacturer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void main() {
expect(androidDeviceInfo.hardware, 'hardware');
expect(androidDeviceInfo.bootloader, 'bootloader');
expect(androidDeviceInfo.isPhysicalDevice, isTrue);
expect(androidDeviceInfo.freeDiskSpace, 70729949184);
expect(androidDeviceInfo.totalDiskSpace, 113281839104);
expect(androidDeviceInfo.fingerprint, 'fingerprint');
expect(androidDeviceInfo.manufacturer, 'manufacturer');
expect(androidDeviceInfo.supportedAbis, _fakeSupportedAbis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ void main() {
'systemVersion': 'systemVersion',
'localizedModel': 'localizedModel',
'identifierForVendor': 'identifierForVendor',
'freeDiskSize': 4096,
'totalDiskSize': 8192,
};

iosDeviceInfo = IosDeviceInfo.fromMap(iosDeviceInfoMap);
Expand All @@ -39,6 +41,8 @@ void main() {
expect(iosDeviceInfo.systemName, 'systemName');
expect(iosDeviceInfo.systemVersion, 'systemVersion');
expect(iosDeviceInfo.localizedModel, 'localizedModel');
expect(iosDeviceInfo.freeDiskSize, 4096);
expect(iosDeviceInfo.totalDiskSize, 8192);
expect(iosDeviceInfo.utsname.release, 'release');
expect(iosDeviceInfo.utsname.version, 'version');
expect(iosDeviceInfo.utsname.machine, 'machine');
Expand Down
Loading