Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

iOS ram and disk space #20

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 88 additions & 51 deletions ALSystemUtilities/ALSystemUtilities/ALDisk/ALDisk.m
Original file line number Diff line number Diff line change
@@ -1,70 +1,107 @@
//
// ALDisk.m
// ALSystem
//
// Created by Andrea Mario Lufino on 19/07/13.
// Copyright (c) 2013 Andrea Mario Lufino. All rights reserved.
//
/********* HardwareInfo.m Cordova Plugin Implementation *******/

#import "ALDisk.h"
#import <Cordova/CDV.h>
#import <mach/mach.h>
#import <mach/mach_host.h>

#define MB (1024*1024)
#define GB (MB*1024)
@interface HardwareInfo : CDVPlugin {
// Member variables go here.
}

@implementation ALDisk
- (void)coolMethod:(CDVInvokedUrlCommand*)command;
- (void)CPUInfo:(CDVInvokedUrlCommand*)command;
- (void)RAMInfo:(CDVInvokedUrlCommand*)command;
- (void)DeviceInfo:(CDVInvokedUrlCommand*)command;
@end

#pragma mark - Formatter
@implementation HardwareInfo

+ (NSString *)memoryFormatter:(long long)diskSpace {
NSString *formatted;
double bytes = 1.0 * diskSpace;
double megabytes = bytes / MB;
double gigabytes = bytes / GB;
if (gigabytes >= 1.0)
formatted = [NSString stringWithFormat:@"%.2f GB", gigabytes];
else if (megabytes >= 1.0)
formatted = [NSString stringWithFormat:@"%.2f MB", megabytes];
else
formatted = [NSString stringWithFormat:@"%.2f bytes", bytes];

return formatted;
}
- (void)coolMethod:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];


#pragma mark - Methods
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

+ (NSString *)totalDiskSpace {
long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
return [self memoryFormatter:space];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

+ (NSString *)freeDiskSpace {
long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
return [self memoryFormatter:freeSpace];
}
- (void)CPUInfo:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];

+ (NSString *)usedDiskSpace {
return [self memoryFormatter:[self usedDiskSpaceInBytes]];
}
if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

+ (CGFloat)totalDiskSpaceInBytes {
long long space = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemSize] longLongValue];
return space;
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

+ (CGFloat)freeDiskSpaceInBytes {
long long freeSpace = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemFreeSize] longLongValue];
return freeSpace;
- (void)RAMInfo:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
long physicalMemoryvalue = [[NSProcessInfo processInfo] physicalMemory];
NSString *rammemory = [NSString stringWithFormat:@"%ld", physicalMemoryvalue];
__autoreleasing NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];

NSString *totalstorage;
NSString *totalfreestorage;

if (dictionary) {
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
totalstorage = [NSString stringWithFormat:@"%llu", ((totalSpace/1024ll)/1024ll)];
totalfreestorage = [NSString stringWithFormat:@"%llu", ((totalFreeSpace/1024ll)/1024ll)];

}
else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %ld", [error domain], (long)[error code]);
}


NSDictionary *firstJsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
totalfreestorage, @"totalfreestorage",
totalstorage, @"totalstorage",
rammemory, @"ramtotal",
nil];

NSMutableArray * arr = [[NSMutableArray alloc] init];

[arr addObject:firstJsonDictionary];

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:jsonString];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

+ (CGFloat)usedDiskSpaceInBytes {
long long usedSpace = [self totalDiskSpaceInBytes] - [self freeDiskSpaceInBytes];
return usedSpace;
}
- (void)DeviceInfo:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;
NSString* echo = [command.arguments objectAtIndex:0];

if (echo != nil && [echo length] > 0) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

/*! WORK IN PROGRESS */
+ (CGFloat)numberOfNodes {
long long numberOfNodes = [[[[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil] objectForKey:NSFileSystemNodes] longLongValue];
return numberOfNodes;
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end