-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.js
300 lines (234 loc) · 10.3 KB
/
info.js
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
const _ = require('lodash');
const moment = require('moment');
const logger = require('@geek/logger').createLogger('@titanium/essentials', { meta: { filename: __filename } });
const devices = require('./devices');
const manufacturers = require('./manufacturers');
const events = require(`events`).default;
Ti.Platform.batteryMonitoring = true;
const info = {
get uptime() {
return Ti.Platform.uptime;
},
get uptime_formatted() {
return `${moment.duration(Ti.Platform.uptime, 's').humanize()} ago`;
},
get battery_level() {
const battery_level = Ti.Platform.batteryLevel;
if (battery_level === -1) {
return 'Unknown';
}
return battery_level;
},
get battery_level_formatted() {
const battery_level = Ti.Platform.batteryLevel;
if (battery_level === -1) {
return 'Unknown';
}
return `${parseFloat(battery_level * 100).toFixed(0)}%`;
},
get device_available_memory() {
return Ti.Platform.availableMemory;
},
get device_available_memory_formatted() {
return info.humanizeBytes(Ti.Platform.availableMemory);
},
};
module.exports = info;
info.humanizeBytes = (bytes, b = 2) => {
bytes = _.toInteger(parseInt(bytes));
if (bytes === 0) { return '0 Bytes'; } const c = b < 0 ? 0 : b; const d = Math.floor(Math.log(bytes) / Math.log(1024)); return `${parseFloat((bytes / Math.pow(1024, d)).toFixed(c))} ${[ 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ][d]}`;
};
info.getClipboardText = () => {
if (Ti.UI.Clipboard.hasText()) {
return Ti.UI.Clipboard.getText();
}
};
info.setClipboardText = (text = '') => {
Ti.UI.Clipboard.setText(text);
return text;
};
info.percent_height = (relative, plus) => {
relative = relative || 100;
plus = plus || 0;
if (info.isIos) {
return info.platform_height;
} else {
// const pointDp = measurement.pointPXToDP(relative);
const px = info.platform_height * (relative / 100);
const calcDp = Math.round(px / (info.device_dpi / 160));
return (calcDp + parseInt(plus));
}
};
info.percent_width = (relative = 100, plus = 0) => {
if (info.isIos) {
return info.platform_width * (relative / 100);
} else {
const px = info.platform_width * (relative / 100);
const calcDp = Math.round(px / (info.device_dpi / 160));
return (calcDp + parseInt(plus));
}
};
info.recalculate = function () {
const { platformWidth } = Ti.Platform.displayCaps;
const { platformHeight } = Ti.Platform.displayCaps;
// var deviceWidth = OS_IOS ? Ti.Platform.displayCaps.platformWidth : Math.round( Ti.Platform.displayCaps.platformWidth / ( Ti.Platform.displayCaps.dpi / 160 ) );
const { orientation } = Ti.Gesture;
info.isLandscape = orientation === (Ti.UI.LANDSCAPE_LEFT || orientation === Ti.UI.LANDSCAPE_RIGHT);
info.isPortrait = !info.isLandscape;
info.platform_width = info.isPortrait && platformWidth > platformHeight ? platformHeight : platformWidth;
info.platform_height = info.isPortrait && platformWidth > platformHeight ? platformWidth : platformHeight;
info.device_height_actual = info.percent_height();
info.device_height = info.isIos7Plus ? info.device_height_actual - 20 : info.device_height_actual;
info.device_width = info.percent_width();
// console.log(device);
// device.WIDTH_10 = device.width * 0.1;
for (let i = 1; i <= 100; i++) {
info[`WIDTH_${i}`] = info.device_width * (i / 100);
info[`HEIGHT_${i}`] = info.device_height * (i / 100);
}
for (let i = 105; i <= 200; i += 5) {
info[`WIDTH_${i}`] = info.device_width * (i / 100);
info[`HEIGHT_${i}`] = info.device_height * (i / 100);
}
};
info.ip_address = Ti.Platform.address;
info.device_architecture = Ti.Platform.architecture;
info.device_model = Ti.Platform.model;
info.device_density = Ti.Platform.displayCaps.density;
info.device_dpi = Ti.Platform.displayCaps.dpi;
info.session_id = Ti.App.sessionId;
info.device_id = Ti.Platform.id;
info.install_id = Ti.App.installId;
info.app_id = Ti.App.id;
info.app_guid = Ti.App.guid;
info.app_name = Ti.App.name;
info.app_description = Ti.App.description;
info.app_copyright = Ti.App.copyright;
info.app_publisher = Ti.App.publisher;
info.app_url = Ti.App.url;
info.app_deploy_type = Ti.App.deployType;
info.titanium_sdk_version = Ti.version;
info.isProd = (info.app_deploy_type === 'production');
info.isDev = (info.app_deploy_type === 'development');
// module.exports.id = device.id;
info.locale = Ti.Platform.locale;
info.language_code = info.locale.substring(0, 2);
info.country_code = info.locale.substring(3);
info.mac_address = Ti.Platform.macaddress;
// info.manufacturer = _.startCase(_.toLower(Ti.Platform.manufacturer || ''));
info.device_manufacturer = _.get(manufacturers, Ti.Platform.manufacturer, (Ti.Platform.manufacturer || ''));
info.netmask = Ti.Platform.netmask;
info.os = Ti.Platform.osname;
info.os_type = Ti.Platform.ostype;
info.platform = Ti.Platform.name;
info.device_processor_count = Ti.Platform.processorCount;
info.runtime = Ti.Platform.runtime;
info.device_total_memory = Ti.Platform.totalMemory;
info.device_total_memory_formatted = info.humanizeBytes(info.device_total_memory);
info.username = Ti.Platform.username;
info.os_version = Ti.Platform.version;
info.os_version_major = parseInt(info.os_version.split('.')[0], 10) || 0;
info.os_version_minor = parseInt(info.os_version.split('.')[1], 10) || 0;
info.os_version_build = parseInt(info.os_version.split('.')[2], 10) || 0;
info.device_logical_density_factor = Ti.Platform.displayCaps.logicalDensityFactor;
info.app_version = Ti.App.version;
info.app_version_major = parseInt(info.app_version.split('.')[0], 10) || 0;
info.app_version_minor = parseInt(info.app_version.split('.')[1], 10) || 0;
info.app_version_build = parseInt(info.app_version.split('.')[2], 10) || 0;
let app_version_previous = Ti.App.Properties.getString('turbo.app_version_previous');
const app_first_installed_version = Ti.App.Properties.getString('turbo.app_first_installed_version');
const app_version_current = Ti.App.Properties.getString('turbo.app_version_current');
const app_version_history = Ti.App.Properties.getObject('turbo.app_version_history', {});
// logger.debug(`🦠 app_version_previous: ${JSON.stringify(app_version_previous, null, 2)}`);
// logger.debug(`🦠 app_version_current: ${JSON.stringify(app_version_current, null, 2)}`);
info.isFirstLaunchEver = !app_first_installed_version;
info.isFirstLaunchForCurrentVersion = !app_version_history[info.app_version];
// info.isFirstLaunchAfterUpdate = !!app_version_previous && (info.app_version !== app_version_current);
info.isFirstLaunchAfterUpdate = !!app_version_current && (info.app_version !== app_version_current);
if (info.isFirstLaunchEver) {
Ti.App.Properties.setString('turbo.app_first_installed_version', info.app_version);
}
info.isFirstLaunchForMajorVersion = !_.find(_.keys(app_version_history), v => v.startsWith(`${info.app_version_major}.`));
info.isFirstLaunchForMinorVersion = !_.find(_.keys(app_version_history), v => v.startsWith(`${info.app_version_major}.${info.app_version_minor}.`));
if (info.isFirstLaunchForCurrentVersion) {
app_version_history[info.app_version] = new Date().toISOString();
Ti.App.Properties.setObject('turbo.app_version_history', app_version_history);
}
if (info.isFirstLaunchAfterUpdate) {
Ti.App.Properties.setString('turbo.app_version_previous', app_version_current);
app_version_previous = app_version_current;
}
Ti.App.Properties.setString('turbo.app_version_current', info.app_version);
info.app_version_history = app_version_history;
info.app_version_previous = app_version_previous;
info.app_display_name = Ti.App.Properties.getString('app-display-name', info.app_name);
// device.identifierForAdvertising = device.ios ? Ti.Platform.identifierForAdvertising : device.id;
info.advertising_id = Ti.Platform.identifierForAdvertising;
// device.identifierForVendor = device.ios ? Ti.Platform.identifierForVendor : device.id;
info.vendor_id = Ti.Platform.identifierForVendor;
info.isIos = info.platform === 'iPhone OS' || info.platform === 'iOS';
info.isAndroid = info.os === 'android';
info.isIphone = info.os === 'iphone';
info.isIpad = info.os === 'ipad';
// TIBUG: The variable OS_IOS is not working in node_modules
info.os_name = info.isIos ? 'ios' : 'android';
if (info.isIos) {
if (info.isIpad && info.os_version_major >= 13) {
info.os_name_full = 'iPadOS';
} else {
info.os_name_full = 'iOS';
}
} else {
info.os_name_full = 'Android';
}
if (info.device_model.endsWith(' (Simulator)')) {
info.device_model = info.device_model.substring(0, info.device_model.length - 12);
info.isVirtual = true;
} else {
info.isVirtual = info.device_model === 'Simulator' || info.device_model.indexOf('sdk') !== -1;
}
info.device_model_name = _.get(devices, info.device_model, info.device_model);
info.isIos7Plus = info.isIos && info.os_version_major >= 7;
info.isIos8Plus = info.isIos && info.os_version_major >= 8;
info.isIos9Plus = info.isIos && info.os_version_major >= 9;
info.isIos10Plus = info.isIos && info.os_version_major >= 10;
info.isIos11Plus = info.isIos && info.os_version_major >= 11;
info.isIos12Plus = info.isIos && info.os_version_major >= 12;
info.isIos12 = info.isIos && info.os_version_major === 12;
info.isIos13Plus = info.isIos && info.os_version_major >= 13;
info.isIos13 = info.isIos && info.os_version_major === 13;
info.isIos14Plus = info.isIos && info.os_version_major >= 14;
info.isIos14 = info.isIos && info.os_version_major === 14;
info.online = !!Ti.Network.online;
info.network_type = Ti.Network.networkType;
info.network_type_name = Ti.Network.networkTypeName;
info.network_change_reason = 'Initial Network Connection';
Ti.Network.addEventListener('change', e => {
info.network_type_name = e.networkTypeName;
info.network_type = e.networkType;
info.network_change_reason = e.reason;
info.online = !!e.online;
logger.debug(`Ti.Network.onChange: ${JSON.stringify(e, null, 2)}`);
events.fire('network::change');
info.online && events.fire('network::online');
!info.online && events.fire('network::offline');
});
Ti.Platform.addEventListener('battery', e => {
const data = { level: e.level };
switch (e.state) {
case Ti.Platform.BATTERY_STATE_CHARGING:
data.state = 'charging';
break;
case Ti.Platform.BATTERY_STATE_FULL:
data.state = 'full';
break;
case Ti.Platform.BATTERY_STATE_UNPLUGGED:
data.state = 'unplugged';
break;
default:
data.state = 'unknown';
}
logger.verbose(`🦠 battery status change: ${JSON.stringify(data, null, 2)}`);
events.fire('battery::changed', data);
});
info.recalculate();