-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PlatformModule.m
479 lines (412 loc) Β· 13.1 KB
/
PlatformModule.m
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_PLATFORM
#import "PlatformModule.h"
#import "TiPlatformDisplayCaps.h"
#import "TiUtils+Addons.h"
#import <TitaniumKit/JSValue+Addons.h>
#import <TitaniumKit/TiApp.h>
#import <mach/mach.h>
#import <sys/sysctl.h>
#import <sys/utsname.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <sys/socket.h>
#import <sys/types.h>
#if defined(USE_TI_PLATFORMIDENTIFIERFORADVERTISING) || defined(USE_TI_PLATFORMGETIDENTIFIERFORADVERTISING)
@import AdSupport;
#endif
NSString *const WIFI_IFACE = @"en0";
NSString *const DATA_IFACE = @"pdp_ip0";
@implementation PlatformModule
@synthesize architecture, availableMemory, model, name, osname, ostype, processorCount, totalMemory, uptime, username, version;
#pragma mark Internal
- (id)init
{
if (self = [super init]) {
UIDevice *theDevice = [UIDevice currentDevice];
name = [[theDevice systemName] retain];
version = [[theDevice systemVersion] retain];
// grab logical CPUs
int cores = 1;
size_t sizeof_cores = sizeof(cores);
sysctlbyname("hw.logicalcpu_max", &cores, &sizeof_cores, NULL, 0);
if (cores <= 0) {
cores = 1;
}
processorCount = [[NSNumber numberWithInt:cores] retain];
username = [[theDevice name] retain];
#ifdef __LP64__
ostype = [@"64bit" retain];
#else
ostype = [@"32bit" retain];
#endif
if ([TiUtils isIPad]) {
// ipad is a constant for Ti.Platform.osname
osname = [@"ipad" retain];
} else {
// iphone is a constant for Ti.Platform.osname
osname = [@"iphone" retain];
}
NSString *themodel = [theDevice model];
// attempt to determine extended phone info
struct utsname u;
uname(&u);
// detect simulator
if (strcmp(u.machine, "i386") == 0 || strcmp(u.machine, "x86_64") == 0) {
model = [[NSString stringWithFormat:@"%s (Simulator)", getenv("SIMULATOR_MODEL_IDENTIFIER")] retain];
} else {
model = [[NSString alloc] initWithUTF8String:u.machine];
}
architecture = [[TiUtils currentArchitecture] retain];
// needed for platform displayCaps orientation to be correct
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
return self;
}
- (void)dealloc
{
RELEASE_TO_NIL(name);
RELEASE_TO_NIL(model);
RELEASE_TO_NIL(version);
RELEASE_TO_NIL(architecture);
RELEASE_TO_NIL(processorCount);
RELEASE_TO_NIL(username);
RELEASE_TO_NIL(ostype);
RELEASE_TO_NIL(availableMemory);
RELEASE_TO_NIL(totalMemory);
RELEASE_TO_NIL(uptime);
[super dealloc];
}
- (NSString *)apiName
{
return @"Ti.Platform";
}
- (void)registerListeners:(id)unused
{
UIDevice *device = [UIDevice currentDevice];
// set a flag to temporarily turn on battery enablement
if (!batteryEnabled && !device.batteryMonitoringEnabled) {
batteryEnabled = YES;
[device setBatteryMonitoringEnabled:YES];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:UIDeviceBatteryStateDidChangeNotification object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:device];
}
- (void)unregisterListeners:(id)unused
{
UIDevice *device = [UIDevice currentDevice];
if (batteryEnabled) {
[device setBatteryMonitoringEnabled:NO];
batteryEnabled = NO;
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceBatteryStateDidChangeNotification object:device];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceBatteryLevelDidChangeNotification object:device];
}
- (void)_listenerAdded:(NSString *)type count:(int)count
{
if (count == 1 && [type isEqualToString:@"battery"]) {
TiThreadPerformOnMainThread(
^{
[self registerListeners:nil];
},
YES);
}
}
- (void)_listenerRemoved:(NSString *)type count:(int)count
{
if (count == 0 && [type isEqualToString:@"battery"]) {
TiThreadPerformOnMainThread(
^{
[self unregisterListeners:nil];
},
YES);
}
}
- (NSString *)getIface:(NSString *)iname mask:(BOOL)mask
{
struct ifaddrs *head = NULL;
struct ifaddrs *ifaddr = NULL;
getifaddrs(&head);
NSString *str = nil;
for (ifaddr = head; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
if (ifaddr->ifa_addr->sa_family == AF_INET && !strcmp(ifaddr->ifa_name, [iname UTF8String])) {
char ipaddr[20];
struct sockaddr_in *addr;
if (mask) {
addr = (struct sockaddr_in *)ifaddr->ifa_netmask;
} else {
addr = (struct sockaddr_in *)ifaddr->ifa_addr;
}
inet_ntop(addr->sin_family, &(addr->sin_addr), ipaddr, 20);
str = [NSString stringWithUTF8String:ipaddr];
break;
}
}
freeifaddrs(head);
return str;
}
#pragma mark Public APIs
- (NSString *)runtime
{
return @"javascriptcore";
}
GETTER_IMPL(NSString *, runtime, Runtime);
- (NSString *)manufacturer
{
return @"apple";
}
GETTER_IMPL(NSString *, manufacturer, Manufacturer);
- (NSString *)locale
{
// this will return the locale that the user has set the phone in
// not the region where the phone is
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defs objectForKey:@"AppleLanguages"];
return [languages count] > 0 ? [languages objectAtIndex:0] : @"en";
}
GETTER_IMPL(NSString *, locale, Locale);
- (NSString *)macaddress
{
return [TiUtils appIdentifier];
}
GETTER_IMPL(NSString *, macaddress, Macaddress);
- (NSNumber *)uptime
{
return [NSNumber numberWithDouble:[[NSProcessInfo processInfo] systemUptime]];
}
- (NSString *)identifierForVendor
{
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
GETTER_IMPL(NSString *, identifierForVendor, IdentifierForVendor);
#if defined(USE_TI_PLATFORMIDENTIFIERFORADVERTISING) || defined(USE_TI_PLATFORMGETIDENTIFIERFORADVERTISING)
- (BOOL)isAdvertisingTrackingEnabled
{
return [[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled];
}
- (NSString *)identifierForAdvertising
{
return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}
#else
- (BOOL)isAdvertisingTrackingEnabled
{
return NO;
}
- (NSString *)identifierForAdvertising
{
return @"";
}
#endif
GETTER_IMPL(BOOL, isAdvertisingTrackingEnabled, IsAdvertisingTrackingEnabled);
GETTER_IMPL(NSString *, identifierForAdvertising, IdentifierForAdvertising);
- (NSString *)id
{
return [TiUtils appIdentifier];
}
GETTER_IMPL(NSString *, id, Id);
- (NSString *)createUUID
{
return [TiUtils createUUID];
}
- (BOOL)is24HourTimeFormat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateInStringForm = [dateFormatter stringFromDate:[NSDate date]];
NSRange amRange = [dateInStringForm rangeOfString:[dateFormatter AMSymbol]];
NSRange pmRange = [dateInStringForm rangeOfString:[dateFormatter PMSymbol]];
[dateFormatter release];
return amRange.location == NSNotFound && pmRange.location == NSNotFound;
}
- (NSNumber *)availableMemory
{
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
if (kernReturn != KERN_SUCCESS) {
return [NSNumber numberWithDouble:-1];
}
return [NSNumber numberWithUnsignedLong:(vm_page_size * vmStats.free_count)];
}
- (NSNumber *)totalMemory
{
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
if (kernReturn != KERN_SUCCESS) {
return [NSNumber numberWithDouble:-1];
}
unsigned long mem_used = (vmStats.active_count + vmStats.inactive_count + vmStats.wire_count) * vm_page_size;
unsigned long mem_free = vmStats.free_count * vm_page_size;
unsigned long mem_total = mem_used + mem_free;
return [NSNumber numberWithUnsignedLong:mem_total];
}
GETTER_IMPL(NSNumber *, availableMemory, AvailableMemory);
- (BOOL)openURL:(NSString *)url withOptions:(JSValue *)options andCallback:(JSValue *)callback
{
NSURL *newUrl = [TiUtils toURL:url proxy:self];
BOOL result = NO;
NSDictionary *optionsDict = @{};
if ([options isFunction]) {
callback = options;
} else if ([options isObject]) {
optionsDict = [options toDictionary];
}
// Ensure callback is actually a function. If not, make it nil so we don't fire it
// Since callback is optional, this may be a JSValue representing 'undefined' here wich is not nil
// So we need this special guard
if (![callback isFunction]) {
callback = nil;
}
if (newUrl != nil) {
[[UIApplication sharedApplication] openURL:newUrl
options:optionsDict
completionHandler:^(BOOL success) {
if (callback != nil) {
[callback callWithArguments:@[ @{@"success" : @(success)} ]];
}
}];
}
return [NSNumber numberWithBool:result];
}
- (BOOL)canOpenURL:(NSString *)arg
{
NSURL *url = [TiUtils toURL:arg proxy:self];
return [[UIApplication sharedApplication] canOpenURL:url];
}
- (TiPlatformDisplayCaps *)displayCaps
{
return [[[TiPlatformDisplayCaps alloc] init] autorelease];
}
GETTER_IMPL(TiPlatformDisplayCaps *, displayCaps, DisplayCaps);
- (TiPlatformDisplayCaps *)DisplayCaps
{
return [self displayCaps];
}
- (void)setBatteryMonitoring:(BOOL)yn
{
if (![NSThread isMainThread]) {
TiThreadPerformOnMainThread(
^{
[self setBatteryMonitoring:yn];
},
YES);
}
[[UIDevice currentDevice] setBatteryMonitoringEnabled:yn];
}
- (BOOL)batteryMonitoring
{
if (![NSThread isMainThread]) {
__block BOOL result = NO;
TiThreadPerformOnMainThread(
^{
result = [self batteryMonitoring];
},
YES);
return result;
}
return [UIDevice currentDevice].batteryMonitoringEnabled;
}
READWRITE_IMPL(BOOL, batteryMonitoring, BatteryMonitoring);
- (NSNumber *)batteryState
{
if (![NSThread isMainThread]) {
__block NSNumber *result = nil;
TiThreadPerformOnMainThread(
^{
result = [[self batteryState] retain];
},
YES);
return [result autorelease];
}
return NUMINT([[UIDevice currentDevice] batteryState]);
}
GETTER_IMPL(NSNumber *, batteryState, BatteryState);
- (NSNumber *)batteryLevel
{
if (![NSThread isMainThread]) {
__block NSNumber *result = nil;
TiThreadPerformOnMainThread(
^{
result = [[self batteryLevel] retain];
},
YES);
return [result autorelease];
}
return NUMFLOAT([[UIDevice currentDevice] batteryLevel]);
}
GETTER_IMPL(NSNumber *, batteryLevel, BatteryLevel);
- (NSString *)address
{
#if TARGET_IPHONE_SIMULATOR
// Assume classical ethernet and wifi interfaces
NSArray *interfaces = [NSArray arrayWithObjects:@"en0", @"en1", nil];
for (NSString *interface in interfaces) {
NSString *iface = [self getIface:interface mask:NO];
if (iface) {
return iface;
}
}
return nil;
#else
return [self getIface:WIFI_IFACE mask:NO];
#endif
}
GETTER_IMPL(NSString *, address, Address);
- (NSString *)dataAddress
{
#if TARGET_IPHONE_SIMULATOR
return nil; // Handy shortcut
#else
return [self getIface:DATA_IFACE mask:NO];
#endif
}
GETTER_IMPL(NSString *, dataAddress, DataAddress);
// Only available for the local wifi; why would you want it for the data network?
- (NSString *)netmask
{
#if TARGET_IPHONE_SIMULATOR
// Assume classical ethernet and wifi interfaces
NSArray *interfaces = [NSArray arrayWithObjects:@"en0", @"en1", nil];
for (NSString *interface in interfaces) {
NSString *iface = [self getIface:interface mask:YES];
if (iface) {
return iface;
}
}
return nil;
#else
return [self getIface:WIFI_IFACE mask:YES];
#endif
}
GETTER_IMPL(NSString *, netmask, Netmask);
// accessors for synthesized properties
GETTER_IMPL(NSString *, architecture, Architecture);
GETTER_IMPL(NSString *, model, Model);
GETTER_IMPL(NSString *, name, Name);
GETTER_IMPL(NSString *, osname, Osname);
GETTER_IMPL(NSString *, ostype, Ostype);
GETTER_IMPL(NSNumber *, processorCount, ProcessorCount);
GETTER_IMPL(NSNumber *, totalMemory, TotalMemory);
GETTER_IMPL(NSNumber *, uptime, Uptime);
GETTER_IMPL(NSString *, username, Username);
GETTER_IMPL(NSString *, version, Version);
MAKE_SYSTEM_PROP(BATTERY_STATE_UNKNOWN, UIDeviceBatteryStateUnknown);
MAKE_SYSTEM_PROP(BATTERY_STATE_UNPLUGGED, UIDeviceBatteryStateUnplugged);
MAKE_SYSTEM_PROP(BATTERY_STATE_CHARGING, UIDeviceBatteryStateCharging);
MAKE_SYSTEM_PROP(BATTERY_STATE_FULL, UIDeviceBatteryStateFull);
#pragma mark Delegates
- (void)batteryStateChanged:(NSNotification *)note
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:[self batteryState], @"state", [self batteryLevel], @"level", nil];
[self fireEvent:@"battery" withDict:event];
}
@end
#endif