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