1 | import { useCallback, useEffect, useState } from 'react';
|
2 | import { Dimensions, NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
3 | import { useOnEvent, useOnMount } from './internal/asyncHookWrappers';
|
4 | import devicesWithNotch from './internal/devicesWithNotch';
|
5 | import RNDeviceInfo from './internal/nativeInterface';
|
6 | import {
|
7 | getSupportedPlatformInfoAsync,
|
8 | getSupportedPlatformInfoFunctions,
|
9 | getSupportedPlatformInfoSync,
|
10 | } from './internal/supported-platform-info';
|
11 | import { DeviceInfoModule } from './internal/privateTypes';
|
12 | import type {
|
13 | AsyncHookResult,
|
14 | DeviceType,
|
15 | LocationProviderInfo,
|
16 | PowerState,
|
17 | } from './internal/types';
|
18 |
|
19 | export const [getUniqueId, getUniqueIdSync] = getSupportedPlatformInfoFunctions({
|
20 | memoKey: 'uniqueId',
|
21 | supportedPlatforms: ['android', 'ios', 'windows'],
|
22 | getter: () => RNDeviceInfo.getUniqueId(),
|
23 | syncGetter: () => RNDeviceInfo.getUniqueIdSync(),
|
24 | defaultValue: 'unknown',
|
25 | });
|
26 |
|
27 | let uniqueId: string;
|
28 | export async function syncUniqueId() {
|
29 | if (Platform.OS === 'ios') {
|
30 | uniqueId = await RNDeviceInfo.syncUniqueId();
|
31 | } else {
|
32 | uniqueId = await getUniqueId();
|
33 | }
|
34 | return uniqueId;
|
35 | }
|
36 |
|
37 | export const [getInstanceId, getInstanceIdSync] = getSupportedPlatformInfoFunctions({
|
38 | memoKey: 'instanceId',
|
39 | supportedPlatforms: ['android'],
|
40 | getter: () => RNDeviceInfo.getInstanceId(),
|
41 | syncGetter: () => RNDeviceInfo.getInstanceIdSync(),
|
42 | defaultValue: 'unknown',
|
43 | });
|
44 |
|
45 | export const [getSerialNumber, getSerialNumberSync] = getSupportedPlatformInfoFunctions({
|
46 | memoKey: 'serialNumber',
|
47 | supportedPlatforms: ['android', 'windows'],
|
48 | getter: () => RNDeviceInfo.getSerialNumber(),
|
49 | syncGetter: () => RNDeviceInfo.getSerialNumberSync(),
|
50 | defaultValue: 'unknown',
|
51 | });
|
52 |
|
53 | export const [getAndroidId, getAndroidIdSync] = getSupportedPlatformInfoFunctions({
|
54 | memoKey: 'androidId',
|
55 | supportedPlatforms: ['android'],
|
56 | getter: () => RNDeviceInfo.getAndroidId(),
|
57 | syncGetter: () => RNDeviceInfo.getAndroidIdSync(),
|
58 | defaultValue: 'unknown',
|
59 | });
|
60 |
|
61 | export const [getIpAddress, getIpAddressSync] = getSupportedPlatformInfoFunctions({
|
62 | supportedPlatforms: ['android', 'ios', 'windows'],
|
63 | getter: () => RNDeviceInfo.getIpAddress(),
|
64 | syncGetter: () => RNDeviceInfo.getIpAddressSync(),
|
65 | defaultValue: 'unknown',
|
66 | });
|
67 |
|
68 | export const [isCameraPresent, isCameraPresentSync] = getSupportedPlatformInfoFunctions({
|
69 | supportedPlatforms: ['android', 'windows', 'web'],
|
70 | getter: () => RNDeviceInfo.isCameraPresent(),
|
71 | syncGetter: () => RNDeviceInfo.isCameraPresentSync(),
|
72 | defaultValue: false,
|
73 | });
|
74 |
|
75 | export async function getMacAddress() {
|
76 | if (Platform.OS === 'android') {
|
77 | return RNDeviceInfo.getMacAddress();
|
78 | } else if (Platform.OS === 'ios') {
|
79 | return '02:00:00:00:00:00';
|
80 | }
|
81 | return 'unknown';
|
82 | }
|
83 |
|
84 | export function getMacAddressSync() {
|
85 | if (Platform.OS === 'android') {
|
86 | return RNDeviceInfo.getMacAddressSync();
|
87 | } else if (Platform.OS === 'ios') {
|
88 | return '02:00:00:00:00:00';
|
89 | }
|
90 | return 'unknown';
|
91 | }
|
92 |
|
93 | export const getDeviceId = () =>
|
94 | getSupportedPlatformInfoSync({
|
95 | defaultValue: 'unknown',
|
96 | memoKey: 'deviceId',
|
97 | getter: () => RNDeviceInfo.deviceId,
|
98 | supportedPlatforms: ['android', 'ios', 'windows'],
|
99 | });
|
100 |
|
101 | export const [getManufacturer, getManufacturerSync] = getSupportedPlatformInfoFunctions({
|
102 | memoKey: 'manufacturer',
|
103 | supportedPlatforms: ['android', 'ios', 'windows'],
|
104 | getter: () =>
|
105 | Platform.OS == 'ios' ? Promise.resolve('Apple') : RNDeviceInfo.getSystemManufacturer(),
|
106 | syncGetter: () => (Platform.OS == 'ios' ? 'Apple' : RNDeviceInfo.getSystemManufacturerSync()),
|
107 | defaultValue: 'unknown',
|
108 | });
|
109 |
|
110 | export const getModel = () =>
|
111 | getSupportedPlatformInfoSync({
|
112 | memoKey: 'model',
|
113 | defaultValue: 'unknown',
|
114 | supportedPlatforms: ['ios', 'android', 'windows'],
|
115 | getter: () => RNDeviceInfo.model,
|
116 | });
|
117 |
|
118 | export const getBrand = () =>
|
119 | getSupportedPlatformInfoSync({
|
120 | memoKey: 'brand',
|
121 | supportedPlatforms: ['android', 'ios', 'windows'],
|
122 | defaultValue: 'unknown',
|
123 | getter: () => RNDeviceInfo.brand,
|
124 | });
|
125 |
|
126 | export const getSystemName = () =>
|
127 | getSupportedPlatformInfoSync({
|
128 | defaultValue: 'unknown',
|
129 | supportedPlatforms: ['ios', 'android', 'windows'],
|
130 | memoKey: 'systemName',
|
131 | getter: () =>
|
132 | Platform.select({
|
133 | ios: RNDeviceInfo.systemName,
|
134 | android: 'Android',
|
135 | windows: 'Windows',
|
136 | default: 'unknown',
|
137 | }),
|
138 | });
|
139 |
|
140 | export const getSystemVersion = () =>
|
141 | getSupportedPlatformInfoSync({
|
142 | defaultValue: 'unknown',
|
143 | getter: () => RNDeviceInfo.systemVersion,
|
144 | supportedPlatforms: ['android', 'ios', 'windows'],
|
145 | memoKey: 'systemVersion',
|
146 | });
|
147 |
|
148 | export const [getBuildId, getBuildIdSync] = getSupportedPlatformInfoFunctions({
|
149 | memoKey: 'buildId',
|
150 | supportedPlatforms: ['android', 'ios', 'windows'],
|
151 | getter: () => RNDeviceInfo.getBuildId(),
|
152 | syncGetter: () => RNDeviceInfo.getBuildIdSync(),
|
153 | defaultValue: 'unknown',
|
154 | });
|
155 |
|
156 | export const [getApiLevel, getApiLevelSync] = getSupportedPlatformInfoFunctions({
|
157 | memoKey: 'apiLevel',
|
158 | supportedPlatforms: ['android'],
|
159 | getter: () => RNDeviceInfo.getApiLevel(),
|
160 | syncGetter: () => RNDeviceInfo.getApiLevelSync(),
|
161 | defaultValue: -1,
|
162 | });
|
163 |
|
164 | export const getBundleId = () =>
|
165 | getSupportedPlatformInfoSync({
|
166 | memoKey: 'bundleId',
|
167 | supportedPlatforms: ['android', 'ios', 'windows'],
|
168 | defaultValue: 'unknown',
|
169 | getter: () => RNDeviceInfo.bundleId,
|
170 | });
|
171 |
|
172 | export const [
|
173 | getInstallerPackageName,
|
174 | getInstallerPackageNameSync,
|
175 | ] = getSupportedPlatformInfoFunctions({
|
176 | memoKey: 'installerPackageName',
|
177 | supportedPlatforms: ['android', 'windows', 'ios'],
|
178 | getter: () => RNDeviceInfo.getInstallerPackageName(),
|
179 | syncGetter: () => RNDeviceInfo.getInstallerPackageNameSync(),
|
180 | defaultValue: 'unknown',
|
181 | });
|
182 |
|
183 | export const getApplicationName = () =>
|
184 | getSupportedPlatformInfoSync({
|
185 | memoKey: 'appName',
|
186 | defaultValue: 'unknown',
|
187 | getter: () => RNDeviceInfo.appName,
|
188 | supportedPlatforms: ['android', 'ios', 'windows'],
|
189 | });
|
190 |
|
191 | export const getBuildNumber = () =>
|
192 | getSupportedPlatformInfoSync({
|
193 | memoKey: 'buildNumber',
|
194 | supportedPlatforms: ['android', 'ios', 'windows'],
|
195 | getter: () => RNDeviceInfo.buildNumber,
|
196 | defaultValue: 'unknown',
|
197 | });
|
198 |
|
199 | export const getVersion = () =>
|
200 | getSupportedPlatformInfoSync({
|
201 | memoKey: 'version',
|
202 | defaultValue: 'unknown',
|
203 | supportedPlatforms: ['android', 'ios', 'windows'],
|
204 | getter: () => RNDeviceInfo.appVersion,
|
205 | });
|
206 |
|
207 | export function getReadableVersion() {
|
208 | return getVersion() + '.' + getBuildNumber();
|
209 | }
|
210 |
|
211 | export const [getDeviceName, getDeviceNameSync] = getSupportedPlatformInfoFunctions({
|
212 | supportedPlatforms: ['android', 'ios', 'windows'],
|
213 | getter: () => RNDeviceInfo.getDeviceName(),
|
214 | syncGetter: () => RNDeviceInfo.getDeviceNameSync(),
|
215 | defaultValue: 'unknown',
|
216 | });
|
217 |
|
218 | export const [getUsedMemory, getUsedMemorySync] = getSupportedPlatformInfoFunctions({
|
219 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
220 | getter: () => RNDeviceInfo.getUsedMemory(),
|
221 | syncGetter: () => RNDeviceInfo.getUsedMemorySync(),
|
222 | defaultValue: -1,
|
223 | });
|
224 |
|
225 | export const getUserAgent = () =>
|
226 | getSupportedPlatformInfoAsync({
|
227 | memoKey: 'userAgent',
|
228 | defaultValue: 'unknown',
|
229 | supportedPlatforms: ['android', 'ios', 'web'],
|
230 | getter: () => RNDeviceInfo.getUserAgent(),
|
231 | });
|
232 |
|
233 | export const getUserAgentSync = () =>
|
234 | getSupportedPlatformInfoSync({
|
235 | memoKey: 'userAgent',
|
236 | defaultValue: 'unknown',
|
237 | supportedPlatforms: ['android', 'web'],
|
238 | getter: () => RNDeviceInfo.getUserAgentSync(),
|
239 | });
|
240 |
|
241 | export const [getFontScale, getFontScaleSync] = getSupportedPlatformInfoFunctions({
|
242 | supportedPlatforms: ['android', 'ios', 'windows'],
|
243 | getter: () => RNDeviceInfo.getFontScale(),
|
244 | syncGetter: () => RNDeviceInfo.getFontScaleSync(),
|
245 | defaultValue: -1,
|
246 | });
|
247 |
|
248 | export const [getBootloader, getBootloaderSync] = getSupportedPlatformInfoFunctions({
|
249 | memoKey: 'bootloader',
|
250 | supportedPlatforms: ['android'],
|
251 | getter: () => RNDeviceInfo.getBootloader(),
|
252 | syncGetter: () => RNDeviceInfo.getBootloaderSync(),
|
253 | defaultValue: 'unknown',
|
254 | });
|
255 |
|
256 | export const [getDevice, getDeviceSync] = getSupportedPlatformInfoFunctions({
|
257 | getter: () => RNDeviceInfo.getDevice(),
|
258 | syncGetter: () => RNDeviceInfo.getDeviceSync(),
|
259 | defaultValue: 'unknown',
|
260 | memoKey: 'device',
|
261 | supportedPlatforms: ['android'],
|
262 | });
|
263 |
|
264 | export const [getDisplay, getDisplaySync] = getSupportedPlatformInfoFunctions({
|
265 | memoKey: 'display',
|
266 | supportedPlatforms: ['android'],
|
267 | getter: () => RNDeviceInfo.getDisplay(),
|
268 | syncGetter: () => RNDeviceInfo.getDisplaySync(),
|
269 | defaultValue: 'unknown',
|
270 | });
|
271 |
|
272 | export const [getFingerprint, getFingerprintSync] = getSupportedPlatformInfoFunctions({
|
273 | memoKey: 'fingerprint',
|
274 | supportedPlatforms: ['android'],
|
275 | getter: () => RNDeviceInfo.getFingerprint(),
|
276 | syncGetter: () => RNDeviceInfo.getFingerprintSync(),
|
277 | defaultValue: 'unknown',
|
278 | });
|
279 |
|
280 | export const [getHardware, getHardwareSync] = getSupportedPlatformInfoFunctions({
|
281 | memoKey: 'hardware',
|
282 | supportedPlatforms: ['android'],
|
283 | getter: () => RNDeviceInfo.getHardware(),
|
284 | syncGetter: () => RNDeviceInfo.getHardwareSync(),
|
285 | defaultValue: 'unknown',
|
286 | });
|
287 |
|
288 | export const [getHost, getHostSync] = getSupportedPlatformInfoFunctions({
|
289 | memoKey: 'host',
|
290 | supportedPlatforms: ['android'],
|
291 | getter: () => RNDeviceInfo.getHost(),
|
292 | syncGetter: () => RNDeviceInfo.getHostSync(),
|
293 | defaultValue: 'unknown',
|
294 | });
|
295 |
|
296 | export const [getProduct, getProductSync] = getSupportedPlatformInfoFunctions({
|
297 | memoKey: 'product',
|
298 | supportedPlatforms: ['android'],
|
299 | getter: () => RNDeviceInfo.getProduct(),
|
300 | syncGetter: () => RNDeviceInfo.getProductSync(),
|
301 | defaultValue: 'unknown',
|
302 | });
|
303 |
|
304 | export const [getTags, getTagsSync] = getSupportedPlatformInfoFunctions({
|
305 | memoKey: 'tags',
|
306 | supportedPlatforms: ['android'],
|
307 | getter: () => RNDeviceInfo.getTags(),
|
308 | syncGetter: () => RNDeviceInfo.getTagsSync(),
|
309 | defaultValue: 'unknown',
|
310 | });
|
311 |
|
312 | export const [getType, getTypeSync] = getSupportedPlatformInfoFunctions({
|
313 | memoKey: 'type',
|
314 | supportedPlatforms: ['android'],
|
315 | getter: () => RNDeviceInfo.getType(),
|
316 | syncGetter: () => RNDeviceInfo.getTypeSync(),
|
317 | defaultValue: 'unknown',
|
318 | });
|
319 |
|
320 | export const [getBaseOs, getBaseOsSync] = getSupportedPlatformInfoFunctions({
|
321 | memoKey: 'baseOs',
|
322 | supportedPlatforms: ['android', 'web', 'windows'],
|
323 | getter: () => RNDeviceInfo.getBaseOs(),
|
324 | syncGetter: () => RNDeviceInfo.getBaseOsSync(),
|
325 | defaultValue: 'unknown',
|
326 | });
|
327 |
|
328 | export const [getPreviewSdkInt, getPreviewSdkIntSync] = getSupportedPlatformInfoFunctions({
|
329 | memoKey: 'previewSdkInt',
|
330 | supportedPlatforms: ['android'],
|
331 | getter: () => RNDeviceInfo.getPreviewSdkInt(),
|
332 | syncGetter: () => RNDeviceInfo.getPreviewSdkIntSync(),
|
333 | defaultValue: -1,
|
334 | });
|
335 |
|
336 | export const [getSecurityPatch, getSecurityPatchSync] = getSupportedPlatformInfoFunctions({
|
337 | memoKey: 'securityPatch',
|
338 | supportedPlatforms: ['android'],
|
339 | getter: () => RNDeviceInfo.getSecurityPatch(),
|
340 | syncGetter: () => RNDeviceInfo.getSecurityPatchSync(),
|
341 | defaultValue: 'unknown',
|
342 | });
|
343 |
|
344 | export const [getCodename, getCodenameSync] = getSupportedPlatformInfoFunctions({
|
345 | memoKey: 'codeName',
|
346 | supportedPlatforms: ['android'],
|
347 | getter: () => RNDeviceInfo.getCodename(),
|
348 | syncGetter: () => RNDeviceInfo.getCodenameSync(),
|
349 | defaultValue: 'unknown',
|
350 | });
|
351 |
|
352 | export const [getIncremental, getIncrementalSync] = getSupportedPlatformInfoFunctions({
|
353 | memoKey: 'incremental',
|
354 | supportedPlatforms: ['android'],
|
355 | getter: () => RNDeviceInfo.getIncremental(),
|
356 | syncGetter: () => RNDeviceInfo.getIncrementalSync(),
|
357 | defaultValue: 'unknown',
|
358 | });
|
359 |
|
360 | export const [isEmulator, isEmulatorSync] = getSupportedPlatformInfoFunctions({
|
361 | memoKey: 'emulator',
|
362 | supportedPlatforms: ['android', 'ios', 'windows'],
|
363 | getter: () => RNDeviceInfo.isEmulator(),
|
364 | syncGetter: () => RNDeviceInfo.isEmulatorSync(),
|
365 | defaultValue: false,
|
366 | });
|
367 |
|
368 | export const isTablet = () =>
|
369 | getSupportedPlatformInfoSync({
|
370 | defaultValue: false,
|
371 | supportedPlatforms: ['android', 'ios', 'windows'],
|
372 | memoKey: 'tablet',
|
373 | getter: () => RNDeviceInfo.isTablet,
|
374 | });
|
375 |
|
376 | export const [isPinOrFingerprintSet, isPinOrFingerprintSetSync] = getSupportedPlatformInfoFunctions(
|
377 | {
|
378 | supportedPlatforms: ['android', 'ios', 'windows'],
|
379 | getter: () => RNDeviceInfo.isPinOrFingerprintSet(),
|
380 | syncGetter: () => RNDeviceInfo.isPinOrFingerprintSetSync(),
|
381 | defaultValue: false,
|
382 | }
|
383 | );
|
384 |
|
385 | let notch: boolean;
|
386 | export function hasNotch() {
|
387 | if (notch === undefined) {
|
388 | let _brand = getBrand();
|
389 | let _model = getModel();
|
390 | notch =
|
391 | devicesWithNotch.findIndex(
|
392 | (item) =>
|
393 | item.brand.toLowerCase() === _brand.toLowerCase() &&
|
394 | item.model.toLowerCase() === _model.toLowerCase()
|
395 | ) !== -1;
|
396 | }
|
397 | return notch;
|
398 | }
|
399 |
|
400 | export const [hasGms, hasGmsSync] = getSupportedPlatformInfoFunctions({
|
401 | supportedPlatforms: ['android'],
|
402 | getter: () => RNDeviceInfo.hasGms(),
|
403 | syncGetter: () => RNDeviceInfo.hasGmsSync(),
|
404 | defaultValue: false,
|
405 | });
|
406 |
|
407 | export const [hasHms, hasHmsSync] = getSupportedPlatformInfoFunctions({
|
408 | supportedPlatforms: ['android'],
|
409 | getter: () => RNDeviceInfo.hasHms(),
|
410 | syncGetter: () => RNDeviceInfo.hasHmsSync(),
|
411 | defaultValue: false,
|
412 | });
|
413 |
|
414 | export const [getFirstInstallTime, getFirstInstallTimeSync] = getSupportedPlatformInfoFunctions({
|
415 | memoKey: 'firstInstallTime',
|
416 | supportedPlatforms: ['android', 'windows'],
|
417 | getter: () => RNDeviceInfo.getFirstInstallTime(),
|
418 | syncGetter: () => RNDeviceInfo.getFirstInstallTimeSync(),
|
419 | defaultValue: -1,
|
420 | });
|
421 |
|
422 | export const [getInstallReferrer, getInstallReferrerSync] = getSupportedPlatformInfoFunctions({
|
423 | memoKey: 'installReferrer',
|
424 | supportedPlatforms: ['android', 'windows', 'web'],
|
425 | getter: () => RNDeviceInfo.getInstallReferrer(),
|
426 | syncGetter: () => RNDeviceInfo.getInstallReferrerSync(),
|
427 | defaultValue: 'unknown',
|
428 | });
|
429 |
|
430 | export const [getLastUpdateTime, getLastUpdateTimeSync] = getSupportedPlatformInfoFunctions({
|
431 | memoKey: 'lastUpdateTime',
|
432 | supportedPlatforms: ['android'],
|
433 | getter: () => RNDeviceInfo.getLastUpdateTime(),
|
434 | syncGetter: () => RNDeviceInfo.getLastUpdateTimeSync(),
|
435 | defaultValue: -1,
|
436 | });
|
437 |
|
438 | export const [getPhoneNumber, getPhoneNumberSync] = getSupportedPlatformInfoFunctions({
|
439 | supportedPlatforms: ['android'],
|
440 | getter: () => RNDeviceInfo.getPhoneNumber(),
|
441 | syncGetter: () => RNDeviceInfo.getPhoneNumberSync(),
|
442 | defaultValue: 'unknown',
|
443 | });
|
444 |
|
445 | export const [getCarrier, getCarrierSync] = getSupportedPlatformInfoFunctions({
|
446 | supportedPlatforms: ['android', 'ios'],
|
447 | getter: () => RNDeviceInfo.getCarrier(),
|
448 | syncGetter: () => RNDeviceInfo.getCarrierSync(),
|
449 | defaultValue: 'unknown',
|
450 | });
|
451 |
|
452 | export const [getTotalMemory, getTotalMemorySync] = getSupportedPlatformInfoFunctions({
|
453 | memoKey: 'totalMemory',
|
454 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
455 | getter: () => RNDeviceInfo.getTotalMemory(),
|
456 | syncGetter: () => RNDeviceInfo.getTotalMemorySync(),
|
457 | defaultValue: -1,
|
458 | });
|
459 |
|
460 | export const [getMaxMemory, getMaxMemorySync] = getSupportedPlatformInfoFunctions({
|
461 | memoKey: 'maxMemory',
|
462 | supportedPlatforms: ['android', 'windows', 'web'],
|
463 | getter: () => RNDeviceInfo.getMaxMemory(),
|
464 | syncGetter: () => RNDeviceInfo.getMaxMemorySync(),
|
465 | defaultValue: -1,
|
466 | });
|
467 |
|
468 | export const [getTotalDiskCapacity, getTotalDiskCapacitySync] = getSupportedPlatformInfoFunctions({
|
469 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
470 | getter: () => RNDeviceInfo.getTotalDiskCapacity(),
|
471 | syncGetter: () => RNDeviceInfo.getTotalDiskCapacitySync(),
|
472 | defaultValue: -1,
|
473 | });
|
474 |
|
475 | export async function getTotalDiskCapacityOld() {
|
476 | if (Platform.OS === 'android') {
|
477 | return RNDeviceInfo.getTotalDiskCapacityOld();
|
478 | }
|
479 | if (Platform.OS === 'ios' || Platform.OS === 'windows' || Platform.OS === 'web') {
|
480 | return getTotalDiskCapacity();
|
481 | }
|
482 |
|
483 | return -1;
|
484 | }
|
485 |
|
486 | export function getTotalDiskCapacityOldSync() {
|
487 | if (Platform.OS === 'android') {
|
488 | return RNDeviceInfo.getTotalDiskCapacityOldSync();
|
489 | }
|
490 | if (Platform.OS === 'ios' || Platform.OS === 'windows' || Platform.OS === 'web') {
|
491 | return getTotalDiskCapacitySync();
|
492 | }
|
493 |
|
494 | return -1;
|
495 | }
|
496 |
|
497 | export const [getFreeDiskStorage, getFreeDiskStorageSync] = getSupportedPlatformInfoFunctions({
|
498 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
499 | getter: () => RNDeviceInfo.getFreeDiskStorage(),
|
500 | syncGetter: () => RNDeviceInfo.getFreeDiskStorageSync(),
|
501 | defaultValue: -1,
|
502 | });
|
503 |
|
504 | export async function getFreeDiskStorageOld() {
|
505 | if (Platform.OS === 'android') {
|
506 | return RNDeviceInfo.getFreeDiskStorageOld();
|
507 | }
|
508 | if (Platform.OS === 'ios' || Platform.OS === 'windows' || Platform.OS === 'web') {
|
509 | return getFreeDiskStorage();
|
510 | }
|
511 |
|
512 | return -1;
|
513 | }
|
514 |
|
515 | export function getFreeDiskStorageOldSync() {
|
516 | if (Platform.OS === 'android') {
|
517 | return RNDeviceInfo.getFreeDiskStorageOldSync();
|
518 | }
|
519 | if (Platform.OS === 'ios' || Platform.OS === 'windows' || Platform.OS === 'web') {
|
520 | return getFreeDiskStorageSync();
|
521 | }
|
522 |
|
523 | return -1;
|
524 | }
|
525 |
|
526 | export const [getBatteryLevel, getBatteryLevelSync] = getSupportedPlatformInfoFunctions({
|
527 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
528 | getter: () => RNDeviceInfo.getBatteryLevel(),
|
529 | syncGetter: () => RNDeviceInfo.getBatteryLevelSync(),
|
530 | defaultValue: -1,
|
531 | });
|
532 |
|
533 | export const [getPowerState, getPowerStateSync] = getSupportedPlatformInfoFunctions<
|
534 | Partial<PowerState>
|
535 | >({
|
536 | supportedPlatforms: ['ios', 'android', 'windows', 'web'],
|
537 | getter: () => RNDeviceInfo.getPowerState(),
|
538 | syncGetter: () => RNDeviceInfo.getPowerStateSync(),
|
539 | defaultValue: {},
|
540 | });
|
541 |
|
542 | export const [isBatteryCharging, isBatteryChargingSync] = getSupportedPlatformInfoFunctions({
|
543 | supportedPlatforms: ['android', 'ios', 'windows', 'web'],
|
544 | getter: () => RNDeviceInfo.isBatteryCharging(),
|
545 | syncGetter: () => RNDeviceInfo.isBatteryChargingSync(),
|
546 | defaultValue: false,
|
547 | });
|
548 |
|
549 | export async function isLandscape() {
|
550 | return Promise.resolve(isLandscapeSync());
|
551 | }
|
552 |
|
553 | export function isLandscapeSync() {
|
554 | const { height, width } = Dimensions.get('window');
|
555 | return width >= height;
|
556 | }
|
557 |
|
558 | export const [isAirplaneMode, isAirplaneModeSync] = getSupportedPlatformInfoFunctions({
|
559 | supportedPlatforms: ['android', 'web'],
|
560 | getter: () => RNDeviceInfo.isAirplaneMode(),
|
561 | syncGetter: () => RNDeviceInfo.isAirplaneModeSync(),
|
562 | defaultValue: false,
|
563 | });
|
564 |
|
565 | export const getDeviceType = () => {
|
566 | return getSupportedPlatformInfoSync({
|
567 | memoKey: 'deviceType',
|
568 | supportedPlatforms: ['android', 'ios', 'windows'],
|
569 | defaultValue: 'unknown',
|
570 | getter: () => RNDeviceInfo.deviceType,
|
571 | });
|
572 | };
|
573 |
|
574 | export const getDeviceTypeSync = () => {
|
575 | return getSupportedPlatformInfoSync({
|
576 | memoKey: 'deviceType',
|
577 | supportedPlatforms: ['android', 'ios', 'windows'],
|
578 | defaultValue: 'unknown',
|
579 | getter: () => RNDeviceInfo.deviceType,
|
580 | });
|
581 | };
|
582 |
|
583 | export const [supportedAbis, supportedAbisSync] = getSupportedPlatformInfoFunctions({
|
584 | memoKey: '_supportedAbis',
|
585 | supportedPlatforms: ['android', 'ios', 'windows'],
|
586 | getter: () => RNDeviceInfo.getSupportedAbis(),
|
587 | syncGetter: () => RNDeviceInfo.getSupportedAbisSync(),
|
588 | defaultValue: [] as string[],
|
589 | });
|
590 |
|
591 | export const [supported32BitAbis, supported32BitAbisSync] = getSupportedPlatformInfoFunctions({
|
592 | memoKey: '_supported32BitAbis',
|
593 | supportedPlatforms: ['android'],
|
594 | getter: () => RNDeviceInfo.getSupported32BitAbis(),
|
595 | syncGetter: () => RNDeviceInfo.getSupported32BitAbisSync(),
|
596 | defaultValue: [] as string[],
|
597 | });
|
598 |
|
599 | export const [supported64BitAbis, supported64BitAbisSync] = getSupportedPlatformInfoFunctions({
|
600 | memoKey: '_supported64BitAbis',
|
601 | supportedPlatforms: ['android'],
|
602 | getter: () => RNDeviceInfo.getSupported64BitAbis(),
|
603 | syncGetter: () => RNDeviceInfo.getSupported64BitAbisSync(),
|
604 | defaultValue: [],
|
605 | });
|
606 |
|
607 | export async function hasSystemFeature(feature: string) {
|
608 | if (Platform.OS === 'android') {
|
609 | return RNDeviceInfo.hasSystemFeature(feature);
|
610 | }
|
611 | return false;
|
612 | }
|
613 |
|
614 | export function hasSystemFeatureSync(feature: string) {
|
615 | if (Platform.OS === 'android') {
|
616 | return RNDeviceInfo.hasSystemFeatureSync(feature);
|
617 | }
|
618 | return false;
|
619 | }
|
620 |
|
621 | export function isLowBatteryLevel(level: number): boolean {
|
622 | if (Platform.OS === 'android') {
|
623 | return level < 0.15;
|
624 | }
|
625 | return level < 0.2;
|
626 | }
|
627 |
|
628 | export const [
|
629 | getSystemAvailableFeatures,
|
630 | getSystemAvailableFeaturesSync,
|
631 | ] = getSupportedPlatformInfoFunctions({
|
632 | supportedPlatforms: ['android'],
|
633 | getter: () => RNDeviceInfo.getSystemAvailableFeatures(),
|
634 | syncGetter: () => RNDeviceInfo.getSystemAvailableFeaturesSync(),
|
635 | defaultValue: [] as string[],
|
636 | });
|
637 |
|
638 | export const [isLocationEnabled, isLocationEnabledSync] = getSupportedPlatformInfoFunctions({
|
639 | supportedPlatforms: ['android', 'ios', 'web'],
|
640 | getter: () => RNDeviceInfo.isLocationEnabled(),
|
641 | syncGetter: () => RNDeviceInfo.isLocationEnabledSync(),
|
642 | defaultValue: false,
|
643 | });
|
644 |
|
645 | export const [isHeadphonesConnected, isHeadphonesConnectedSync] = getSupportedPlatformInfoFunctions(
|
646 | {
|
647 | supportedPlatforms: ['android', 'ios'],
|
648 | getter: () => RNDeviceInfo.isHeadphonesConnected(),
|
649 | syncGetter: () => RNDeviceInfo.isHeadphonesConnectedSync(),
|
650 | defaultValue: false,
|
651 | }
|
652 | );
|
653 |
|
654 | export const [isMouseConnected, isMouseConnectedSync] = getSupportedPlatformInfoFunctions({
|
655 | supportedPlatforms: ['windows'],
|
656 | getter: () => RNDeviceInfo.isMouseConnected(),
|
657 | syncGetter: () => RNDeviceInfo.isMouseConnectedSync(),
|
658 | defaultValue: false,
|
659 | });
|
660 |
|
661 | export const [isKeyboardConnected, isKeyboardConnectedSync] = getSupportedPlatformInfoFunctions({
|
662 | supportedPlatforms: ['windows'],
|
663 | getter: () => RNDeviceInfo.isKeyboardConnected(),
|
664 | syncGetter: () => RNDeviceInfo.isKeyboardConnectedSync(),
|
665 | defaultValue: false,
|
666 | });
|
667 |
|
668 | export const isTabletMode = () =>
|
669 | getSupportedPlatformInfoAsync({
|
670 | supportedPlatforms: ['windows'],
|
671 | getter: () => RNDeviceInfo.isTabletMode(),
|
672 | defaultValue: false,
|
673 | });
|
674 |
|
675 | export const [
|
676 | getAvailableLocationProviders,
|
677 | getAvailableLocationProvidersSync,
|
678 | ] = getSupportedPlatformInfoFunctions({
|
679 | supportedPlatforms: ['android', 'ios'],
|
680 | getter: () => RNDeviceInfo.getAvailableLocationProviders(),
|
681 | syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
|
682 | defaultValue: {},
|
683 | });
|
684 |
|
685 | export const [getBrightness, getBrightnessSync] = getSupportedPlatformInfoFunctions({
|
686 | supportedPlatforms: ['ios'],
|
687 | getter: () => RNDeviceInfo.getBrightness(),
|
688 | syncGetter: () => RNDeviceInfo.getBrightnessSync(),
|
689 | defaultValue: -1,
|
690 | });
|
691 |
|
692 | export async function getDeviceToken() {
|
693 | if (Platform.OS === 'ios') {
|
694 | return RNDeviceInfo.getDeviceToken();
|
695 | }
|
696 | return 'unknown';
|
697 | }
|
698 |
|
699 | const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
|
700 | export function useBatteryLevel(): number | null {
|
701 | const [batteryLevel, setBatteryLevel] = useState<number | null>(null);
|
702 |
|
703 | useEffect(() => {
|
704 | const setInitialValue = async () => {
|
705 | const initialValue: number = await getBatteryLevel();
|
706 | setBatteryLevel(initialValue);
|
707 | };
|
708 |
|
709 | const onChange = (level: number) => {
|
710 | setBatteryLevel(level);
|
711 | };
|
712 |
|
713 | setInitialValue();
|
714 |
|
715 | const subscription = deviceInfoEmitter.addListener(
|
716 | 'RNDeviceInfo_batteryLevelDidChange',
|
717 | onChange
|
718 | );
|
719 |
|
720 | return () => subscription.remove();
|
721 | }, []);
|
722 |
|
723 | return batteryLevel;
|
724 | }
|
725 |
|
726 | export function useBatteryLevelIsLow(): number | null {
|
727 | const [batteryLevelIsLow, setBatteryLevelIsLow] = useState<number | null>(null);
|
728 |
|
729 | useEffect(() => {
|
730 | const setInitialValue = async () => {
|
731 | const initialValue: number = await getBatteryLevel();
|
732 | isLowBatteryLevel(initialValue) && setBatteryLevelIsLow(initialValue);
|
733 | };
|
734 |
|
735 | setInitialValue();
|
736 |
|
737 | const onChange = (level: number) => {
|
738 | setBatteryLevelIsLow(level);
|
739 | };
|
740 |
|
741 | const subscription = deviceInfoEmitter.addListener('RNDeviceInfo_batteryLevelIsLow', onChange);
|
742 |
|
743 | return () => subscription.remove();
|
744 | }, []);
|
745 |
|
746 | return batteryLevelIsLow;
|
747 | }
|
748 |
|
749 | export function usePowerState(): Partial<PowerState> {
|
750 | const [powerState, setPowerState] = useState<Partial<PowerState>>({});
|
751 |
|
752 | useEffect(() => {
|
753 | const setInitialValue = async () => {
|
754 | const initialValue: Partial<PowerState> = await getPowerState();
|
755 | setPowerState(initialValue);
|
756 | };
|
757 |
|
758 | const onChange = (state: PowerState) => {
|
759 | setPowerState(state);
|
760 | };
|
761 |
|
762 | setInitialValue();
|
763 |
|
764 | const subscription = deviceInfoEmitter.addListener(
|
765 | 'RNDeviceInfo_powerStateDidChange',
|
766 | onChange
|
767 | );
|
768 |
|
769 | return () => subscription.remove();
|
770 | }, []);
|
771 |
|
772 | return powerState;
|
773 | }
|
774 |
|
775 | export function useIsHeadphonesConnected(): AsyncHookResult<boolean> {
|
776 | return useOnEvent('RNDeviceInfo_headphoneConnectionDidChange', isHeadphonesConnected, false);
|
777 | }
|
778 |
|
779 | export function useFirstInstallTime(): AsyncHookResult<number> {
|
780 | return useOnMount(getFirstInstallTime, -1);
|
781 | }
|
782 |
|
783 | export function useDeviceName(): AsyncHookResult<string> {
|
784 | return useOnMount(getDeviceName, 'unknown');
|
785 | }
|
786 |
|
787 | export function useHasSystemFeature(feature: string): AsyncHookResult<boolean> {
|
788 | const asyncGetter = useCallback(() => hasSystemFeature(feature), [feature]);
|
789 | return useOnMount(asyncGetter, false);
|
790 | }
|
791 |
|
792 | export function useIsEmulator(): AsyncHookResult<boolean> {
|
793 | return useOnMount(isEmulator, false);
|
794 | }
|
795 |
|
796 | export function useManufacturer(): AsyncHookResult<string> {
|
797 | return useOnMount(getManufacturer, 'unknown');
|
798 | }
|
799 |
|
800 | export function useBrightness(): number | null {
|
801 | const [brightness, setBrightness] = useState<number | null>(null);
|
802 |
|
803 | useEffect(() => {
|
804 | const setInitialValue = async () => {
|
805 | const initialValue: number = await getBrightness();
|
806 | setBrightness(initialValue);
|
807 | };
|
808 |
|
809 | const onChange = (value: number) => {
|
810 | setBrightness(value);
|
811 | };
|
812 |
|
813 | setInitialValue();
|
814 |
|
815 | const subscription = deviceInfoEmitter.addListener(
|
816 | 'RNDeviceInfo_brightnessDidChange',
|
817 | onChange
|
818 | );
|
819 |
|
820 | return () => subscription.remove();
|
821 | }, []);
|
822 |
|
823 | return brightness;
|
824 | }
|
825 |
|
826 | export type { AsyncHookResult, DeviceType, LocationProviderInfo, PowerState };
|
827 |
|
828 | const deviceInfoModule: DeviceInfoModule = {
|
829 | getAndroidId,
|
830 | getAndroidIdSync,
|
831 | getApiLevel,
|
832 | getApiLevelSync,
|
833 | getApplicationName,
|
834 | getAvailableLocationProviders,
|
835 | getAvailableLocationProvidersSync,
|
836 | getBaseOs,
|
837 | getBaseOsSync,
|
838 | getBatteryLevel,
|
839 | getBatteryLevelSync,
|
840 | getBootloader,
|
841 | getBootloaderSync,
|
842 | getBrand,
|
843 | getBuildId,
|
844 | getBuildIdSync,
|
845 | getBuildNumber,
|
846 | getBundleId,
|
847 | getCarrier,
|
848 | getCarrierSync,
|
849 | getCodename,
|
850 | getCodenameSync,
|
851 | getDevice,
|
852 | getDeviceId,
|
853 | getDeviceName,
|
854 | getDeviceNameSync,
|
855 | getDeviceSync,
|
856 | getDeviceToken,
|
857 | getDeviceType,
|
858 | getDisplay,
|
859 | getDisplaySync,
|
860 | getFingerprint,
|
861 | getFingerprintSync,
|
862 | getFirstInstallTime,
|
863 | getFirstInstallTimeSync,
|
864 | getFontScale,
|
865 | getFontScaleSync,
|
866 | getFreeDiskStorage,
|
867 | getFreeDiskStorageOld,
|
868 | getFreeDiskStorageSync,
|
869 | getFreeDiskStorageOldSync,
|
870 | getHardware,
|
871 | getHardwareSync,
|
872 | getHost,
|
873 | getHostSync,
|
874 | getIncremental,
|
875 | getIncrementalSync,
|
876 | getInstallerPackageName,
|
877 | getInstallerPackageNameSync,
|
878 | getInstallReferrer,
|
879 | getInstallReferrerSync,
|
880 | getInstanceId,
|
881 | getInstanceIdSync,
|
882 | getIpAddress,
|
883 | getIpAddressSync,
|
884 | getLastUpdateTime,
|
885 | getLastUpdateTimeSync,
|
886 | getMacAddress,
|
887 | getMacAddressSync,
|
888 | getManufacturer,
|
889 | getManufacturerSync,
|
890 | getMaxMemory,
|
891 | getMaxMemorySync,
|
892 | getModel,
|
893 | getPhoneNumber,
|
894 | getPhoneNumberSync,
|
895 | getPowerState,
|
896 | getPowerStateSync,
|
897 | getPreviewSdkInt,
|
898 | getPreviewSdkIntSync,
|
899 | getProduct,
|
900 | getProductSync,
|
901 | getReadableVersion,
|
902 | getSecurityPatch,
|
903 | getSecurityPatchSync,
|
904 | getSerialNumber,
|
905 | getSerialNumberSync,
|
906 | getSystemAvailableFeatures,
|
907 | getSystemAvailableFeaturesSync,
|
908 | getSystemName,
|
909 | getSystemVersion,
|
910 | getTags,
|
911 | getTagsSync,
|
912 | getTotalDiskCapacity,
|
913 | getTotalDiskCapacityOld,
|
914 | getTotalDiskCapacitySync,
|
915 | getTotalDiskCapacityOldSync,
|
916 | getTotalMemory,
|
917 | getTotalMemorySync,
|
918 | getType,
|
919 | getTypeSync,
|
920 | getUniqueId,
|
921 | getUniqueIdSync,
|
922 | getUsedMemory,
|
923 | getUsedMemorySync,
|
924 | getUserAgent,
|
925 | getUserAgentSync,
|
926 | getVersion,
|
927 | getBrightness,
|
928 | getBrightnessSync,
|
929 | hasGms,
|
930 | hasGmsSync,
|
931 | hasHms,
|
932 | hasHmsSync,
|
933 | hasNotch,
|
934 | hasSystemFeature,
|
935 | hasSystemFeatureSync,
|
936 | isAirplaneMode,
|
937 | isAirplaneModeSync,
|
938 | isBatteryCharging,
|
939 | isBatteryChargingSync,
|
940 | isCameraPresent,
|
941 | isCameraPresentSync,
|
942 | isEmulator,
|
943 | isEmulatorSync,
|
944 | isHeadphonesConnected,
|
945 | isHeadphonesConnectedSync,
|
946 | isLandscape,
|
947 | isLandscapeSync,
|
948 | isLocationEnabled,
|
949 | isLocationEnabledSync,
|
950 | isPinOrFingerprintSet,
|
951 | isPinOrFingerprintSetSync,
|
952 | isMouseConnected,
|
953 | isMouseConnectedSync,
|
954 | isKeyboardConnected,
|
955 | isKeyboardConnectedSync,
|
956 | isTabletMode,
|
957 | isTablet,
|
958 | supported32BitAbis,
|
959 | supported32BitAbisSync,
|
960 | supported64BitAbis,
|
961 | supported64BitAbisSync,
|
962 | supportedAbis,
|
963 | supportedAbisSync,
|
964 | syncUniqueId,
|
965 | useBatteryLevel,
|
966 | useBatteryLevelIsLow,
|
967 | useDeviceName,
|
968 | useFirstInstallTime,
|
969 | useHasSystemFeature,
|
970 | useIsEmulator,
|
971 | usePowerState,
|
972 | useManufacturer,
|
973 | useIsHeadphonesConnected,
|
974 | useBrightness,
|
975 | };
|
976 |
|
977 | export default deviceInfoModule;
|