Commit cbbbb159 authored by Lana Petković's avatar Lana Petković Committed by Mehdi Achour

feat: add getFreeDiskStorage() and getTotalDiskCapacity() (#302)

parent 6e529cca
## Release Notes
### Next
* Add `getFreeDiskStorage` and `getTotalDiskCapacity` (https://github.com/rebeccahughes/react-native-device-info/pull/302)
### 0.14.0
* Fix tvOS support (https://github.com/rebeccahughes/react-native-device-info/pull/283)
......
......@@ -195,6 +195,7 @@ var DeviceInfo = require("react-native-device-info");
| [getDeviceLocale()](#getdevicelocale) | `string` | ✅ | ✅ | ✅ | 0.7.0 |
| [getDeviceName()](#getdevicename) | `string` | ✅ | ✅ | ✅ | ? |
| [getFirstInstallTime()](#getfirstinstalltime) | `number` | ❌ | ✅ | ❌ | 0.12.0 |
| [getFreeDiskStorage()](#getFreeDiskStorage) | `number` | ✅ | ✅ | ❌ | 0.15.0 |
| [getIPAddress()](#getipaddress) | `Promise<string>` | ❌ | ✅ | ❌ | 0.12.0 |
| [getInstanceID()](#getinstanceid) | `string` | ❌ | ✅ | ❌ | ? |
| [getLastUpdateTime()](#getlastupdatetime) | `number` | ❌ | ✅ | ❌ | 0.12.0 |
......@@ -208,6 +209,7 @@ var DeviceInfo = require("react-native-device-info");
| [getSystemName()](#getsystemname) | `string` | ✅ | ✅ | ✅ | ? |
| [getSystemVersion()](#getsystemversion) | `string` | ✅ | ✅ | ✅ | ? |
| [getTimezone()](#gettimezone) | `string` | ✅ | ✅ | ✅ | ? |
| [getTotalDiskCapacity()](#getTotalDiskCapacity) | `number` | ✅ | ✅ | ❌ | 0.15.0 |
| [getTotalMemory()](#gettotalmemory) | `number` | ✅ | ✅ | ❌ | 0.14.0 |
| [getUniqueID()](#getuniqueid) | `string` | ✅ | ✅ | ✅ | ? |
| [getUserAgent()](#getuseragent) | `string` | ✅ | ✅ | ❌ | 0.7.0 |
......@@ -389,6 +391,22 @@ const firstInstallTime = DeviceInfo.getFirstInstallTime();
---
### getFreeDiskStorage()
Gets available storage size, in bytes
(Android: Returns only available external storage size, not including internal)
**Examples**
```js
const freeDiskStorage = DeviceInfo.getFreeDiskStorage();
// Android: 17179869184
// iOS: 17179869184
```
---
### getIPAddress()
Gets the device current IP address.
......@@ -597,6 +615,20 @@ Gets the device default timezone.
const timezone = DeviceInfo.getTimezone(); // "Africa/Tunis"
```
---
### getTotalDiskCapacity()
Gets full disk storage size, in bytes
**Examples**
```js
const storageSize = DeviceInfo.getTotalDiskCapacity();
// Android: 17179869184
// iOS: 17179869184
```
---
### getTotalMemory()
......
......@@ -210,10 +210,36 @@ RCT_EXPORT_MODULE(RNDeviceInfo)
return [NSProcessInfo processInfo].physicalMemory;
}
- (NSDictionary *) getStorageDictionary {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: nil];
}
- (uint64_t) totalDiskCapacity {
uint64_t totalSpace = 0;
NSDictionary *storage = [self getStorageDictionary];
if (storage) {
NSNumber *fileSystemSizeInBytes = [storage objectForKey: NSFileSystemSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
}
return totalSpace;
}
- (uint64_t) freeDiskStorage {
uint64_t freeSpace = 0;
NSDictionary *storage = [self getStorageDictionary];
if (storage) {
NSNumber *freeFileSystemSizeInBytes = [storage objectForKey: NSFileSystemFreeSize];
freeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
}
return freeSpace;
}
- (NSDictionary *)constantsToExport
{
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *uniqueId = [DeviceUID uid];
return @{
......@@ -238,7 +264,9 @@ RCT_EXPORT_MODULE(RNDeviceInfo)
@"isEmulator": @(self.isEmulator),
@"isTablet": @(self.isTablet),
@"is24Hour": @(self.is24Hour),
@"totalMemory": @(self.totalMemory)
@"totalMemory": @(self.totalMemory),
@"totalDiskCapacity": @(self.totalDiskCapacity),
@"freeDiskStorage": @(self.freeDiskStorage),
};
}
......
......@@ -11,6 +11,8 @@ import android.content.res.Configuration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings.Secure;
import android.webkit.WebSettings;
import android.telephony.TelephonyManager;
......@@ -120,6 +122,18 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
return telMgr.getNetworkOperatorName();
}
@ReactMethod
public long getTotalDiskCapacity() {
StatFs root = new StatFs(Environment.getRootDirectory().getAbsolutePath());
return root.getBlockCountLong() * root.getBlockSizeLong();
}
@ReactMethod
public long getFreeDiskStorage() {
StatFs external = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
return external.getAvailableBlocksLong() * external.getBlockSizeLong();
}
@Override
public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<String, Object>();
......@@ -201,6 +215,8 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
constants.put("phoneNumber", telMgr.getLine1Number());
}
constants.put("carrier", this.getCarrier());
constants.put("totalDiskCapacity", this.getTotalDiskCapacity());
constants.put("freeDiskStorage", this.getFreeDiskStorage());
Runtime rt = Runtime.getRuntime();
constants.put("maxMemory", rt.maxMemory());
......
......@@ -33,3 +33,5 @@ export function getAPILevel(): number;
export function getCarrier(): string;
export function getTotalMemory(): number;
export function getMaxMemory(): number;
export function getTotalDiskCapacity(): number;
export function getFreeDiskStorage(): number;
\ No newline at end of file
......@@ -101,4 +101,10 @@ module.exports = {
getMaxMemory: function() {
return RNDeviceInfo.maxMemory;
},
getTotalDiskCapacity: function () {
return RNDeviceInfo.totalDiskCapacity;
},
getFreeDiskStorage: function () {
return RNDeviceInfo.freeDiskStorage;
}
};
......@@ -33,4 +33,6 @@ declare module.exports: {
getCarrier: () => string,
getTotalMemory: () => number,
getMaxMemory: () => number,
getTotalDiskCapacity: () => number,
getFreeDiskStorage: () => number,
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment