This repository was archived by the owner on Oct 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUIDevice.j
More file actions
145 lines (124 loc) · 5.44 KB
/
Copy pathUIDevice.j
File metadata and controls
145 lines (124 loc) · 5.44 KB
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
/*
* UIDevice.j
* UIKit
*
* Created by Amari Robinson.
* Copyright 2011, Amari Robinson.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <Foundation/CPObject.j>
UIDevice = nil;
// UIDeviceBatteryState - The battery power state of the device.
UIDeviceBatteryStateUnknown = @"UIDeviceBatteryStateUnknown";
UIDeviceBatteryStateUnplugged = @"UIDeviceBatteryStateUnplugged";
UIDeviceBatteryStateCharging = @"UIDeviceBatteryStateCharging";
UIDeviceBatteryStateFull = @"UIDeviceBatteryStateFull";
// UIDeviceOrientation - The physical orientation of the device.
UIDeviceOrientationUnknown = @"UIDeviceOrientationUnknown";
UIDeviceOrientationPortrait = @"UIDeviceOrientationPortrait";
UIDeviceOrientationPortraitUpsideDown = @"UIDeviceOrientationPortraitUpsideDown";
UIDeviceOrientationLandscapeLeft = @"UIDeviceOrientationLandscapeLeft";
UIDeviceOrientationLandscapeRight = @"UIDeviceOrientationLandscapeRight";
UIDeviceOrientationFaceUp = @"UIDeviceOrientationFaceUp";
UIDeviceOrientationFaceDown = = @"UIDeviceOrientationFaceDown";
// UIUserInterfaceIdiom – The type of interface that should be used on the current device.
UIUserInterfaceIdiomPhone = @"UIUserInterfaceIdiomPhone";
UIUserInterfaceIdiomPad = @"UIUserInterfaceIdiomPad";
// All UIDevice notifications are posted by the singleton device instance returned by currentDevice.
UIDeviceBatteryLevelDidChangeNotification = @"UIDeviceBatteryLevelDidChangeNotification";
UIDeviceBatteryStateDidChangeNotification = @"UIDeviceBatteryStateDidChangeNotification";
UIDeviceOrientationDidChangeNotification = @"UIDeviceOrientationDidChangeNotification";
UIDeviceProximityStateDidChangeNotification = @"UIDeviceProximityStateDidChangeNotification";
@implementation UIDevice : CPObject {
BOOL _multitaskingSupported @accessors(getter=multitaskingSupported);
CPString _uniqueIdentifier @accessors(getter=uniqueIdentifier);
CPString _name @accessors(getter=name);
CPString _systemName @accessors(getter=systemName);
CPString _systemVersion @accessors(getter=systemVersion);
CPString _model @accessors(getter=model);
CPString _localizedModel @accessors(getter=localizedModel);
UIUserInterfaceIdiom _userInterfaceIdiom @accessors(getter=userInterfaceIdiom);
UIDeviceOrientation _orientation @accessors(getter=orientation);
BOOL _generatesDeviceOrientationNotifications @accessors(getter=isGeneratingDeviceOrientationNotifications);
float _batteryLevel @accessors(getter=batteryLevel);
BOOL _batteryMonitoringEnabled @accessors(getter=isBatteryMonitoringEnabled);
UIDeviceBatteryState _batteryState @accessors(getter=batteryState);
BOOL _proximityMonitoringEnabled @accessors(getter=isProximityMonitoringEnabled);
BOOL _proximityState @accessors(getter=proximityState);
}
+ (UIDevice)currentDevice {
// Detect which device we are
if (!UIDevice)
UIDevice = [[UIDevice alloc] init];
return UIDevice;
}
- (id)init {
if (self = [super init]) {
// Device Information.
_name = @"Generic UIDevice";
_systemName = navigator.platform;
_systemVersion = navigator.appVersion;
_model = @"Generic UIDevice";
_localizedModel = _model;
// Multitasking. Included in all UIDevices.
_multitaskingSupported = YES;
// Orientation.
_orientation = UIDeviceOrientationUnknown;
_generatesDeviceOrientationNotifications = NO;
if (window.orientation) {
window.addEventListener("deviceorientation", function (evt) {[[UIDevice currentDevice] _updateOrientationWithDOMWindowEvent];}, true);
[[UIDevice currentDevice] _updateOrientationWithDOMWindowEvent];
_generatesDeviceOrientationNotifications = YES;
}
// Tablet or Phone.
var width = document.screen.width / document.screen.depth;
var height = document.screen.height / document.screen.depth;
var diagonal = Math.sqrt((width*width)+(height*height));
if (diagonal > 5) // Let's assume that 5 inches is the maximum screen size that a sane person would design a phone.
_userInterfaceIdiom = UIUserInterfaceIdiomPad;
else
_userInterfaceIdiom = UIUserInterfaceIdiomPhone;
// Proximity Monitoring.
_proximityMonitoringEnabled = NO;
_proximityState = NO;
// Battery monitoring.
_batteryLevel = -1.0;
_batteryMotitoringEnabled = NO;
_batteryState = UIDeviceBatteryStateUnknown;
}
return self;
}
- (void)_updateOrientationWithDOMWindowEvent {
switch (window.orientation) {
case 0:
_orientation = UIDeviceOrientationPortrait;
break;
case 90:
_orientation = UIDeviceOrientationLandscapeLeft;
break;
case -90:
_orientation = UIDeviceOrientationLandscapeRight;
break;
case 180:
_orientation = UIDeviceOrientationPortraitUpsideDown;
break;
default:
_orientation = UIDeviceOrientationUnknown;
break;
}
[[[CPNotificationCenter] defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:self userInfo:nil];
}
@end