From 9463583a29225fa9cbdc5150a9676ac5b2c1ee23 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Sun, 1 Sep 2019 15:45:05 -0500 Subject: [PATCH] BREAKING CHANGE: API capitalization changes, empty return value standardization --- CHANGELOG.md | 33 +- README.md | 204 +++++----- .../learnium/RNDeviceInfo/RNDeviceModule.java | 84 +++-- default/index.js | 48 --- deviceinfo.d.ts | 15 +- deviceinfo.js | 353 +++++++++++++----- deviceinfo.js.flow | 15 +- example/App.js | 77 ++-- package.json | 4 +- web/index.js | 10 +- windows/RNDeviceInfo/RNDeviceInfoModule.cs | 31 -- yarn.lock | 22 +- 12 files changed, 537 insertions(+), 359 deletions(-) delete mode 100644 default/index.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e83737a..814751899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Release Notes +## 3.0.0-rc.3 + +* fix some real android device v2->v3 discrepancies +* BREAKING CHANGE: more capitalization changes for API calls to standardize (see below) +* BREAKING CHANGE: more return value changes to standardize return values (see below) + ## 3.0.0-rc.2 * fix all emulator/simulator v2->v3 discrepancies @@ -10,14 +16,35 @@ Each BREAKING CHANGE contains the required information to migrate. The example App.js shows sample usage. * BREAKING CHANGE: Every API call returns a Promise now (and thus no more Android constructor with async boolean argument) -* BREAKING CHANGE: Renamed getSupportedABIs to getSupportedAbis (note lower case!) for consistency with other getXxxAbis methods -* BREAKING CHANGE: all events prefixed with 'RNDeviceInfo_' to future-proof against collisions (https://github.com/react-native-community/react-native-device-info/issues/620) + * This was required to improve module load speed, handle dynamic values, and release the main queue for iOS +* BREAKING CHANGE: Every API call with acronyms ('getIP', 'getABI' etc follows pure camel-case now, e.g. 'getIp', 'getAbi') + * This naming style is a consensus standard. Previously APIs here were half one way half the other. Now they are consistent + * isAirPlaneMode -> isAirplaneMode + * getIPAddress -> getIpAddress + * getMACAddress -> getMACAddress + * getAPILevel -> GetApiLevel + * getBaseOS -> getBaseOs + * getInstanceID -> getInstanceId + * getUniqueID -> getUniqueId + * supportedABIs -> supportedAbis +* BREAKING CHANGE: all events prefixed with 'RNDeviceInfo_' (https://github.com/react-native-community/react-native-device-info/issues/620) + * This is required as event names are a global namespace and collisions are inevitable otherwise + * powerStateDidChange -> RNDeviceInfo_powerStateDidChange + * batteryLevelDidChange -> RNDeviceInfo_batteryLevelDidChange + * batteryLevelIsLow -> RNDeviceInfo_batteryLevelIsLow * BREAKING CHANGE: Android `getBuildNumber` returns string like iOS (https://github.com/react-native-community/react-native-device-info/pull/648) -* BREAKING CHANGE: remove is24Hour, getTimezone, isAutoTimeZone and isAutoDateAndTime, getDeviceLocale, getDeviceCountry, getPreferredLocales. react-native-localize is superior +* BREAKING CHANGE: remove is24Hour, getTimezone, isAutoTimeZone and isAutoDateAndTime, getDeviceLocale, getDeviceCountry, getPreferredLocales + * This was the result of a survey. It removes API duplication in the react-native-community modules * Related PR: https://github.com/react-native-community/react-native-localize/pull/65 * Use `yarn add https://github.com/mikehardy/react-native-localize.git#e062f0d2dc3171dc18fdb7b7139d347ad03933dc` to maintain isAutoTimeZone + isAutoDateAndTime until merged * BREAKING CHANGE: iOS switch deprecated WebView for WebKit / getUserAgent returns Promise (https://github.com/react-native-community/react-native-device-info/pull/757) + * The change from WebView to WebKit was required as the API is being removed from the iOS platform +* BREAKING CHANGE: if an API is platform-specific, all non-implementing platforms will return standard values of -1, false, or 'unknown' depending on return type + * This was how most APIs behaved before but it was not 100% - some returned null or empty string before + * getPhoneNumber sometimes returned null, now it will be 'unknown' if not known * deprecated: IP-address-related methods deprecated - use @react-native-community/netinfo or react-native-network-info or react-native-carrier-info +* feat: all APIs are now restricted in Javascirpt to the platforms they have full implementations on so the web polyfill is up to date +* feat: 'getAndroidId' on Android returns android.provider.Settings.Secure.ANDROID_ID, read platform docs for usage * feat: `getUsedMemory` (https://github.com/rebeccahughes/react-native-device-info/pull/356) * feat: getDeviceName() without Bluetooth permission on Android (https://github.com/react-native-community/react-native-device-info/issues/735) * feat: TurboModule support (https://github.com/react-native-community/react-native-device-info/pull/745) for these purposes (https://github.com/react-native-community/react-native-localize/pull/65) diff --git a/README.md b/README.md index da7f46ccc..47f12a2de 100644 --- a/README.md +++ b/README.md @@ -257,87 +257,90 @@ import DeviceInfo from 'react-native-device-info'; // or ES6+ destructured imports -import { getUniqueID, getManufacturer } from 'react-native-device-info'; +import { getUniqueId, getManufacturer } from 'react-native-device-info'; ``` ## API -| Method | Return Type |  iOS | Android | Windows | Since | -| ----------------------------------------------------------------- | ------------------- | :--: | :-----: | :-----: | ------ | -| [getAPILevel()](#getapilevel) | `Promise` | ❌ | ✅ | ❌ | 0.12.0 | -| [getApplicationName()](#getapplicationname) | `Promise` | ✅ | ✅ | ✅ | 0.14.0 | -| [getBaseOS()](#getbaseOS) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getBatteryLevel()](#getbatterylevel) | `Promise` | ✅ | ✅ | ✅ | 0.18.0 | -| [getBootloader()](#getbootloader) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getBrand()](#getbrand) | `Promise` | ✅ | ✅ | ✅ | 0.9.3 | -| [getBuildNumber()](#getbuildnumber) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getBundleId()](#getbundleid) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getCameraPresence()](#getcamerapresence) | `Promise` | ❌ | ✅ | ✅ | ? | -| [getCarrier()](#getcarrier) | `Promise` | ✅ | ✅ | ❌ | 0.13.0 | -| [getCodename()](#getcodename) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getDevice()](#getdevice) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getDeviceId()](#getdeviceid) | `Promise` | ✅ | ✅ | ✅ | 0.5.0 | -| [getDisplay()](#getdisplay) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getDeviceName()](#getdevicename) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getFirstInstallTime()](#getfirstinstalltime) | `Promise` | ❌ | ✅ | ✅ | 0.12.0 | -| [getFingerprint()](#getfingerprint) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getFontScale()](#getfontscale) | `Promise` | ✅ | ✅ | ❌ | 0.15.0 | -| [getFreeDiskStorage()](#getfreediskstorage) | `Promise` | ✅ | ✅ | ❌ | 0.15.0 | -| [getHardware()](#gethardware) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getHost()](#gethost) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getIPAddress()](#getipaddress) | `Promise` | ✅ | ✅ | ✅ | 0.12.0 | -| [getIncremental()](#getincremental) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getInstallReferrer()](#getinstallreferrer) | `Promise` | ❌ | ✅ | ❌ | 0.19.0 | -| [getInstanceID()](#getinstanceid) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getLastUpdateTime()](#getlastupdatetime) | `Promise` | ❌ | ✅ | ❌ | 0.12.0 | -| [getMACAddress()](#getmacaddress) | `Promise` | ✅ | ✅ | ❌ | 0.12.0 | -| [getManufacturer()](#getmanufacturer) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getMaxMemory()](#getmaxmemory) | `Promise` | ❌ | ✅ | ✅ | 0.14.0 | -| [getModel()](#getmodel) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getPhoneNumber()](#getphonenumber) | `Promise` | ❌ | ✅ | ❌ | 0.12.0 | -| [getPowerState()](#getpowerstate) | `Promise` | ✅ | ❌ | ❌ | | -| [getProduct()](#getproduct) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getPreviewSdkInt()](#getPreviewSdkInt) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getReadableVersion()](#getreadableversion) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getSerialNumber()](#getserialnumber) | `Promise` | ❌ | ✅ | ❌ | 0.12.0 | -| [getSecurityPatch()](#getsecuritypatch) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getSystemName()](#getsystemname) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getSystemVersion()](#getsystemversion) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getBuildId()](#getbuildid) | `Promise` | ✅ | ✅ | ❌ | ? | -| [getTags()](#gettags) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getType()](#gettype) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getTotalDiskCapacity()](#gettotaldiskcapacity) | `Promise` | ✅ | ✅ | ❌ | 0.15.0 | -| [getTotalMemory()](#gettotalmemory) | `Promise` | ✅ | ✅ | ❌ | 0.14.0 | -| [getUniqueID()](#getuniqueid) | `Promise` | ✅ | ✅ | ✅ | ? | -| [getUsedMemory()](#getusedmemory) | `Promise` | ✅ | ✅ | ❌ | 3.0.0 | -| [getUserAgent()](#getuseragent) | `Promise` | ✅ | ✅ | ❌ | 0.7.0 | -| [getVersion()](#getversion) | `Promise` | ✅ | ✅ | ✅ | ? | -| [isAirPlaneMode()](#isairplanemode) | `Promise` | ❌ | ✅ | ❌ | 0.25.0 | -| [isBatteryCharging()](#isbatterycharging) | `Promise` | ✅ | ✅ | ❌ | 0.27.0 | -| [isEmulator()](#isemulator) | `Promise` | ✅ | ✅ | ✅ | ? | -| [isPinOrFingerprintSet()](#ispinorfingerprintset) | `Promise` | ✅ | ✅ | ✅ | 0.10.1 | -| [isTablet()](#istablet) | `Promise` | ✅ | ✅ | ✅ | ? | -| [hasNotch()](#hasNotch) | `Promise` | ✅ | ✅ | ✅ | 0.23.0 | -| [isLandscape()](#isLandscape) | `Promise` | ✅ | ✅ | ✅ | 0.24.0 | -| [getDeviceType()](#getDeviceType) | `Promise` | ✅ | ✅ | ❌ | ? | -| [supported32BitAbis()](#supported32BitAbis) | `Promise` | ❌ | ✅ | ❌ | ? | -| [supported64BitAbis()](#supported64BitAbis) | `Promise` | ❌ | ✅ | ❌ | ? | -| [supportedAbis()](#supportedAbis) | `Promise` | ✅ | ✅ | ❌ | 1.1.0 | -| [hasSystemFeature()](#hassystemfeaturefeature) | `Promise` | ❌ | ✅ | ❌ | ? | -| [getSystemAvailableFeatures()](#getSystemAvailableFeatures) | `Promise` | ❌ | ✅ | ❌ | ? | -| [isLocationEnabled()](#isLocationEnabled) | `Promise` | ✅ | ✅ | ❌ | ? | -| [getAvailableLocationProviders()](#getAvailableLocationProviders) | `Promise` | ✅ | ✅ | ❌ | ? | - ---- - -### getAPILevel() +Note that many APIs are platform-specific. If there is no implementation for a platform, then the "default" return values you will receive are 'unknown' for string, '-1' for number, and 'false' for boolean. Arrays and Objects will be empty ('[]' and '{}' respectively). + +| Method | Return Type | iOS | Android | Windows | +| ----------------------------------------------------------------- | ------------------- | :-: | :-----: | :-----: | +| [getAndroidId()](#getandroidid) | `Promise` | ❌ | ✅ | ❌ | +| [getApiLevel()](#getapilevel) | `Promise` | ❌ | ✅ | ❌ | +| [getApplicationName()](#getapplicationname) | `Promise` | ✅ | ✅ | ✅ | +| [getAvailableLocationProviders()](#getAvailableLocationProviders) | `Promise` | ✅ | ✅ | ❌ | +| [getBaseOs()](#getbaseOs) | `Promise` | ❌ | ✅ | ❌ | +| [getBuildId()](#getbuildid) | `Promise` | ✅ | ✅ | ❌ | +| [getBatteryLevel()](#getbatterylevel) | `Promise` | ✅ | ✅ | ✅ | +| [getBootloader()](#getbootloader) | `Promise` | ❌ | ✅ | ❌ | +| [getBrand()](#getbrand) | `Promise` | ✅ | ✅ | ✅ | +| [getBuildNumber()](#getbuildnumber) | `Promise` | ✅ | ✅ | ✅ | +| [getBundleId()](#getbundleid) | `Promise` | ✅ | ✅ | ✅ | +| [getCameraPresence()](#getcamerapresence) | `Promise` | ❌ | ✅ | ✅ | +| [getCarrier()](#getcarrier) | `Promise` | ✅ | ✅ | ❌ | +| [getCodename()](#getcodename) | `Promise` | ❌ | ✅ | ❌ | +| [getDevice()](#getdevice) | `Promise` | ❌ | ✅ | ❌ | +| [getDeviceId()](#getdeviceid) | `Promise` | ✅ | ✅ | ✅ | +| [getDeviceType()](#getDeviceType) | `Promise` | ✅ | ✅ | ❌ | +| [getDisplay()](#getdisplay) | `Promise` | ❌ | ✅ | ❌ | +| [getDeviceName()](#getdevicename) | `Promise` | ✅ | ✅ | ✅ | +| [getFirstInstallTime()](#getfirstinstalltime) | `Promise` | ❌ | ✅ | ✅ | +| [getFingerprint()](#getfingerprint) | `Promise` | ❌ | ✅ | ❌ | +| [getFontScale()](#getfontscale) | `Promise` | ✅ | ✅ | ❌ | +| [getFreeDiskStorage()](#getfreediskstorage) | `Promise` | ✅ | ✅ | ❌ | +| [getHardware()](#gethardware) | `Promise` | ❌ | ✅ | ❌ | +| [getHost()](#gethost) | `Promise` | ❌ | ✅ | ❌ | +| [getIpAddress()](#getipaddress) | `Promise` | ✅ | ✅ | ✅ | +| [getIncremental()](#getincremental) | `Promise` | ❌ | ✅ | ❌ | +| [getInstallReferrer()](#getinstallreferrer) | `Promise` | ❌ | ✅ | ❌ | +| [getInstanceId()](#getinstanceid) | `Promise` | ❌ | ✅ | ❌ | +| [getLastUpdateTime()](#getlastupdatetime) | `Promise` | ❌ | ✅ | ❌ | +| [getMacAddress()](#getmacaddress) | `Promise` | ✅ | ✅ | ❌ | +| [getManufacturer()](#getmanufacturer) | `Promise` | ✅ | ✅ | ✅ | +| [getMaxMemory()](#getmaxmemory) | `Promise` | ❌ | ✅ | ✅ | +| [getModel()](#getmodel) | `Promise` | ✅ | ✅ | ✅ | +| [getPhoneNumber()](#getphonenumber) | `Promise` | ❌ | ✅ | ❌ | +| [getPowerState()](#getpowerstate) | `Promise` | ✅ | ❌ | ❌ | +| [getProduct()](#getproduct) | `Promise` | ❌ | ✅ | ❌ | +| [getPreviewSdkInt()](#getPreviewSdkInt) | `Promise` | ❌ | ✅ | ❌ | +| [getReadableVersion()](#getreadableversion) | `Promise` | ✅ | ✅ | ✅ | +| [getSerialNumber()](#getserialnumber) | `Promise` | ❌ | ✅ | ❌ | +| [getSecurityPatch()](#getsecuritypatch) | `Promise` | ❌ | ✅ | ❌ | +| [getSystemAvailableFeatures()](#getSystemAvailableFeatures) | `Promise` | ❌ | ✅ | ❌ | +| [getSystemName()](#getsystemname) | `Promise` | ✅ | ✅ | ✅ | +| [getSystemVersion()](#getsystemversion) | `Promise` | ✅ | ✅ | ✅ | +| [getTags()](#gettags) | `Promise` | ❌ | ✅ | ❌ | +| [getType()](#gettype) | `Promise` | ❌ | ✅ | ❌ | +| [getTotalDiskCapacity()](#gettotaldiskcapacity) | `Promise` | ✅ | ✅ | ❌ | +| [getTotalMemory()](#gettotalmemory) | `Promise` | ✅ | ✅ | ❌ | +| [getUniqueId()](#getuniqueid) | `Promise` | ✅ | ✅ | ✅ | +| [getUsedMemory()](#getusedmemory) | `Promise` | ✅ | ✅ | ❌ | +| [getUserAgent()](#getuseragent) | `Promise` | ✅ | ✅ | ❌ | +| [getVersion()](#getversion) | `Promise` | ✅ | ✅ | ✅ | +| [hasNotch()](#hasNotch) | `Promise` | ✅ | ✅ | ✅ | +| [hasSystemFeature()](#hassystemfeaturefeature) | `Promise` | ❌ | ✅ | ❌ | +| [isAirPlaneMode()](#isairplanemode) | `Promise` | ❌ | ✅ | ❌ | +| [isBatteryCharging()](#isbatterycharging) | `Promise` | ✅ | ✅ | ❌ | +| [isEmulator()](#isemulator) | `Promise` | ✅ | ✅ | ✅ | +| [isLandscape()](#isLandscape) | `Promise` | ✅ | ✅ | ✅ | +| [isLocationEnabled()](#isLocationEnabled) | `Promise` | ✅ | ✅ | ❌ | +| [isPinOrFingerprintSet()](#ispinorfingerprintset) | `Promise` | ✅ | ✅ | ✅ | +| [isTablet()](#istablet) | `Promise` | ✅ | ✅ | ✅ | +| [supported32BitAbis()](#supported32BitAbis) | `Promise` | ❌ | ✅ | ❌ | +| [supported64BitAbis()](#supported64BitAbis) | `Promise` | ❌ | ✅ | ❌ | +| [supportedAbis()](#supportedAbis) | `Promise` | ✅ | ✅ | ❌ | + +--- + +### getApiLevel() Gets the API level. #### Examples ```js -DeviceInfo.getAPILevel().then(apiLevel => { +DeviceInfo.getApiLevel().then(apiLevel => { // iOS: ? // Android: 25 // Windows: ? @@ -350,6 +353,20 @@ DeviceInfo.getAPILevel().then(apiLevel => { --- +### getAndroidId() + +Gets the ANDROID_ID. See [API documentation](https://developer.android.com/reference/android/provider/Settings.Secure#ANDROID_ID) for appropriate use. + +#### Examples + +```js +DeviceInfo.getAndroidId().then(androidId => { + // androidId here +}); +``` + +--- + ### getApplicationName() Gets the application name. @@ -364,14 +381,14 @@ DeviceInfo.getApplicationName().then(appName => { --- -### getBaseOS() +### getBaseOs() The base OS build the product is based on. #### Examples ```js -DeviceInfo.getBaseOS().then(baseOS => { +DeviceInfo.getBaseOs().then(baseOs => { // "Windows", "Android" etc }); ``` @@ -679,7 +696,7 @@ DeviceInfo.getHost().then(host => { --- -### getIPAddress() +### getIpAddress() **Deprecated** Gets the device current IP address. (of wifi only) Switch to @react-native-community/netinfo or react-native-network-info @@ -687,7 +704,7 @@ Switch to @react-native-community/netinfo or react-native-network-info #### Examples ```js -DeviceInfo.getIPAddress().then(ip => { +DeviceInfo.getIpAddress().then(ip => { // "92.168.32.44" }); ``` @@ -731,14 +748,14 @@ DeviceInfo.getInstallReferrer().then(installReferrer => { --- -### getInstanceID() +### getInstanceId() Gets the application instance ID. #### Examples ```js -DeviceInfo.getInstanceID().then(instanceId => { +DeviceInfo.getInstanceId().then(instanceId => { // Android: ? }); ``` @@ -763,14 +780,14 @@ DeviceInfo.getLastUpdateTime().then(lastUpdateTime => { --- -### getMACAddress() +### getMacAddress() Gets the network adapter MAC address. #### Examples ```js -DeviceInfo.getMACAddress().then(mac => { +DeviceInfo.getMacAddress().then(mac => { // "E5:12:D8:E5:69:97" }); ``` @@ -920,15 +937,15 @@ DeviceInfo.getReadableVersion().then(readableVersion => { ### getSerialNumber() -Gets the device serial number. +Gets the device serial number. Will be 'unknown' in almost all cases [unless you have a privileged app and you know what you're doing](https://developer.android.com/reference/android/os/Build.html#getSerial()). #### Examples ```js DeviceInfo.getSerialNumber().then(serialNumber => { - // iOS: undefined - // Android: ? - // Windows: ? + // iOS: unknown + // Android: ? (maybe a serial number, if your app is privileged) + // Windows: unknown }); ``` @@ -1054,14 +1071,17 @@ DeviceInfo.getTotalMemory().then(totalMemory => { --- -### getUniqueID() +### getUniqueId() -Gets the device unique ID. +Gets the device unique ID. +On Android it is currently identical to getAndroidId() in this module +On iOS it uses the DeviceUID uid identifier +On Windows it uses Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation.id #### Examples ```js -DeviceInfo.getUniqueID().then(uniqueId => { +DeviceInfo.getUniqueId().then(uniqueId => { // iOS: "FCDBD8EF-62FC-4ECB-B2F5-92C9E79AC7F9" // Android: "dd96dec43fb81c97" // Windows: ? @@ -1123,14 +1143,14 @@ DeviceInfo.getVersion().then(version => { --- -### isAirPlaneMode() +### isAirplaneMode() -Tells if the device is in AirPlaneMode. +Tells if the device is in Airplane Mode. #### Examples ```js -DeviceInfo.isAirPlaneMode().then(airPlaneModeOn => { +DeviceInfo.isAirplaneMode().then(airplaneModeOn => { // false }); ``` @@ -1361,7 +1381,7 @@ DeviceInfo.getAvailableLocationProviders().then(providers => { Currently iOS-only. -### batteryLevelDidChange +### RNDeviceInfo_batteryLevelDidChange Fired when the battery level changes; sent no more frequently than once per minute. @@ -1378,7 +1398,7 @@ deviceInfoEmitter.addListener('RNDeviceInfo_batteryLevelDidChange', level => { --- -### batteryLevelIsLow +### RNDeviceInfo_batteryLevelIsLow Fired when the battery drops below 20%. @@ -1395,7 +1415,7 @@ deviceInfoEmitter.addListener('RNDeviceInfo_batteryLevelIsLow', level => { --- -### powerStateDidChange +### RNDeviceInfo_powerStateDidChange Fired when the battery state changes, for example when the device enters charging mode or is unplugged. diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index 49984150f..48889b8a4 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -52,7 +52,6 @@ import static android.provider.Settings.Secure.getString; -@SuppressWarnings("WeakerAccess") @ReactModule(name = RNDeviceModule.NAME) public class RNDeviceModule extends ReactContextBaseJavaModule { public static final String NAME = "RNDeviceInfo"; @@ -186,6 +185,7 @@ public void isPinOrFingerprintSet(Promise p) { KeyguardManager keyguardManager = (KeyguardManager) getReactApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); if (keyguardManager != null) { p.resolve(keyguardManager.isKeyguardSecure()); + return; } p.reject("EUNSPECIFIED", "Unable to determine keyguard status. KeyguardManager was null"); } @@ -208,7 +208,6 @@ public void getIpAddress(Promise p) { } @ReactMethod - @SuppressWarnings("ConstantConditions") public void getCameraPresence(Promise p) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CameraManager manager=(CameraManager)getReactApplicationContext().getSystemService(Context.CAMERA_SERVICE); @@ -341,14 +340,14 @@ public void getBatteryLevel(Promise p) { } @ReactMethod - public void isAirPlaneMode(Promise p) { - boolean isAirPlaneMode; + public void isAirplaneMode(Promise p) { + boolean isAirplaneMode; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { - isAirPlaneMode = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0; + isAirplaneMode = Settings.System.getInt(getReactApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) != 0; } else { - isAirPlaneMode = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, 0) != 0; + isAirplaneMode = Settings.Global.getInt(getReactApplicationContext().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } - p.resolve(isAirPlaneMode); + p.resolve(isAirplaneMode); } @ReactMethod @@ -359,8 +358,7 @@ public void hasSystemFeature(String feature, Promise p) { return; } - boolean hasFeature = getReactApplicationContext().getPackageManager().hasSystemFeature(feature); - p.resolve(hasFeature); + p.resolve(getReactApplicationContext().getPackageManager().hasSystemFeature(feature)); } @ReactMethod @@ -377,7 +375,6 @@ public void getSystemAvailableFeatures(Promise p) { p.resolve(promiseArray); } - @SuppressWarnings("ConstantConditions") @ReactMethod public void isLocationEnabled(Promise p) { boolean locationEnabled = false; @@ -388,6 +385,7 @@ public void isLocationEnabled(Promise p) { locationEnabled = mLocationManager.isLocationEnabled(); } catch (Exception e) { p.reject("EUNSPECIFIED", "Unable to determine if location enabled. LocationManager was null"); + return; } } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int locationMode = Settings.Secure.getInt(getReactApplicationContext().getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); @@ -401,7 +399,6 @@ public void isLocationEnabled(Promise p) { } @ReactMethod - @SuppressWarnings("ConstantConditions") public void getAvailableLocationProviders(Promise p) { LocationManager mLocationManager = (LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE); List providers = Collections.emptyList(); @@ -409,6 +406,7 @@ public void getAvailableLocationProviders(Promise p) { providers = mLocationManager.getProviders(false); } catch (Exception e) { p.reject("EUNSPECIFIED", "Unable to get location providers. LocationManager was null"); + return; } WritableMap providersAvailability = Arguments.createMap(); @@ -422,7 +420,7 @@ public void getAvailableLocationProviders(Promise p) { @ReactMethod public void getInstallReferrer(Promise p) { SharedPreferences sharedPref = getReactApplicationContext().getSharedPreferences("react-native-device-info", Context.MODE_PRIVATE); - p.resolve(sharedPref.getString("installReferrer", null)); + p.resolve(sharedPref.getString("installReferrer", Build.UNKNOWN)); } private PackageInfo getPackageInfo() throws Exception { @@ -449,7 +447,7 @@ public void getBuildNumber(Promise p) { @ReactMethod public void getBuildVersion(Promise p) { - p.resolve("not available"); + p.resolve(Build.UNKNOWN); } @ReactMethod @@ -482,19 +480,43 @@ public void getAppName(Promise p) { @ReactMethod public void getDeviceName(Promise p) { try { - if (Build.VERSION.SDK_INT >= 25) { - p.resolve(Settings.Global.getString(getReactApplicationContext().getContentResolver(), Settings.Global.DEVICE_NAME)); - } else { - p.resolve(Settings.Secure.getString(getReactApplicationContext().getContentResolver(), "bluetooth_name")); - } + String bluetoothName = Settings.Secure.getString(getReactApplicationContext().getContentResolver(), "bluetooth_name"); + if (bluetoothName != null) { + p.resolve(bluetoothName); + return; + } + + if (Build.VERSION.SDK_INT >= 25) { + String deviceName = Settings.Global.getString(getReactApplicationContext().getContentResolver(), Settings.Global.DEVICE_NAME); + if (deviceName != null) { + p.resolve(deviceName); + return; + } + } } catch (Exception e) { p.reject(e); + return; } + p.resolve(Build.UNKNOWN); } - @SuppressLint("HardwareIds") + @SuppressLint({"HardwareIds", "MissingPermission"}) @ReactMethod - public void getSerialNumber(Promise p) { p.resolve(Build.SERIAL); } + public void getSerialNumber(Promise p) { + try { + if (Build.VERSION.SDK_INT >= 26) { + if (getReactApplicationContext().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { + p.resolve(Build.getSerial()); + return; + } + } + } catch (Exception e) { + // This is almost always a PermissionException. We will log it but return unknown + System.err.println("getSerialNumber failed, it probably should not be used: " + e.getMessage()); + } + + p.resolve(Build.UNKNOWN); + } @ReactMethod public void getSystemName(Promise p) { p.resolve("Android"); } @@ -560,6 +582,10 @@ public void getDeviceName(Promise p) { @ReactMethod public void getUniqueId(Promise p) { p.resolve(getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)); } + @SuppressLint("HardwareIds") + @ReactMethod + public void getAndroidId(Promise p) { p.resolve(getString(getReactApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)); } + @ReactMethod public void getMaxMemory(Promise p) { p.resolve((double)Runtime.getRuntime().maxMemory()); } @@ -574,6 +600,7 @@ public void getTotalMemory(Promise p) { actMgr.getMemoryInfo(memInfo); } else { p.reject("EUNSPECIFIED", "Unable to getMemoryInfo. ActivityManager was null"); + return; } p.resolve((double)memInfo.totalMem); } @@ -591,30 +618,31 @@ public void getInstanceId(Promise p) { } @ReactMethod - public void getBaseOS(Promise p) { + public void getBaseOs(Promise p) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { p.resolve(Build.VERSION.BASE_OS); - } else { - p.resolve("not available"); + return; } + p.resolve(Build.UNKNOWN); + } @ReactMethod public void getPreviewSdkInt(Promise p) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { p.resolve(Build.VERSION.PREVIEW_SDK_INT); - } else { - p.resolve("not available"); + return; } + p.resolve(Build.UNKNOWN); } @ReactMethod public void getSecurityPatch(Promise p) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { p.resolve(Build.VERSION.SECURITY_PATCH); - } else { - p.resolve("not available"); + return; } + p.resolve(Build.UNKNOWN); } @ReactMethod @@ -644,7 +672,7 @@ public void getPhoneNumber(Promise p) { p.reject("EUNSPECIFIED", "Unable to getPhoneNumber. TelephonyManager was null"); } } else { - p.resolve(null); + p.resolve(Build.UNKNOWN); } } diff --git a/default/index.js b/default/index.js deleted file mode 100644 index 280cf26fc..000000000 --- a/default/index.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Empty default export functions. - */ - -module.exports = { - uniqueId: () => Promise.resolve(''), - instanceId: () => Promise.resolve(''), - serialNumber: () => Promise.resolve(''), - getIpAddress: () => Promise.resolve(''), - getMacAddress: () => Promise.resolve(''), - deviceId: () => Promise.resolve(''), - systemManufacturer: () => Promise.resolve(''), - model: () => Promise.resolve(''), - brand: () => Promise.resolve(''), - systemName: () => Promise.resolve(''), - systemVersion: () => Promise.resolve(''), - buildId: () => Promise.resolve(''), - apiLevel: () => Promise.resolve(0), - bundleId: () => Promise.resolve(''), - appName: () => Promise.resolve(''), - buildNumber: () => Promise.resolve(0), - appVersion: () => Promise.resolve(0), - deviceName: () => Promise.resolve(''), - getUsedMemory: () => Promise.resolve(0), - userAgent: () => Promise.resolve(''), - fontScale: () => Promise.resolve(0), - isEmulator: () => Promise.resolve(false), - isTablet: () => Promise.resolve(false), - isPinOrFingerprintSet: () => Promise.resolve(false), - firstInstallTime: () => Promise.resolve(0), - installReferrer: () => Promise.resolve(''), - lastUpdateTime: () => Promise.resolve(0), - phoneNumber: () => Promise.resolve(''), - carrier: () => Promise.resolve(''), - totalMemory: () => Promise.resolve(0), - maxMemory: () => Promise.resolve(0), - totalDiskCapacity: () => Promise.resolve(0), - freeDiskStorage: () => Promise.resolve(0), - getBatteryLevel: () => Promise.resolve(0), - isLandscape: () => Promise.resolve(false), - deviceType: () => Promise.resolve('Unknown'), - getPowerState: () => Promise.resolve({}), - hasSystemFeature: () => Promise.resolve(false), - getSystemAvailableFeatures: () => Promise.resolve([]), - getCameraPresence: () => Promise.resolve(false), - isLocationEnabled: () => Promise.resolve(false), - getAvailableLocationProviders: () => Promise.resolve({}), -}; diff --git a/deviceinfo.d.ts b/deviceinfo.d.ts index 4cceaf4e7..23bc29b04 100644 --- a/deviceinfo.d.ts +++ b/deviceinfo.d.ts @@ -17,7 +17,7 @@ export interface LocationProviderInfo { } declare const _default: { - getUniqueID: () => Promise; + getUniqueId: () => Promise; getManufacturer: () => Promise; getBrand: () => Promise; getModel: () => Promise; @@ -33,7 +33,7 @@ declare const _default: { getDeviceName: () => Promise; getUsedMemory: () => Promise; getUserAgent: () => Promise; - getInstanceID: () => Promise; + getInstanceId: () => Promise; getInstallReferrer: () => string | null; isEmulator: () => Promise; isTablet: () => Promise; @@ -47,7 +47,7 @@ declare const _default: { getProduct: () => Promise; getTags: () => Promise; getType: () => Promise; - getBaseOS: () => Promise; + getBaseOs: () => Promise; getPreviewSdkInt: () => Promise; getSecurityPatch: () => Promise; getCodename: () => Promise; @@ -57,11 +57,12 @@ declare const _default: { getFirstInstallTime: () => Promise; getLastUpdateTime: () => Promise; getSerialNumber: () => Promise; - getIPAddress: () => Promise; + getAndroidId: () => Promise; + getIpAddress: () => Promise; getCameraPresence: () => Promise; - getMACAddress: () => Promise; + getMacAddress: () => Promise; getPhoneNumber: () => Promise; - getAPILevel: () => Promise; + getApiLevel: () => Promise; getCarrier: () => Promise; getTotalMemory: () => Promise; getMaxMemory: () => Promise; @@ -71,7 +72,7 @@ declare const _default: { getPowerState: () => Promise; isBatteryCharging: () => Promise; isLandscape: () => Promise; - isAirPlaneMode: () => Promise; + isAirplaneMode: () => Promise; getDeviceType: () => Promise; supportedAbis: () => Promise; supported32BitAbis: () => Promise; diff --git a/deviceinfo.js b/deviceinfo.js index 54e6424e0..6edf342af 100644 --- a/deviceinfo.js +++ b/deviceinfo.js @@ -4,26 +4,20 @@ import { Platform, NativeModules, Dimensions } from 'react-native'; var RNDeviceInfo = NativeModules.RNDeviceInfo; +var OS = Platform.OS; -if (Platform.OS === 'web' || Platform.OS === 'dom') { +if (OS === 'web' || OS === 'dom') { RNDeviceInfo = require('./web'); } if (!RNDeviceInfo) { // Produce an error if we don't have the native module - if ( - Platform.OS === 'android' || - Platform.OS === 'ios' || - Platform.OS === 'web' || - Platform.OS === 'dom' - ) { + if (OS === 'android' || OS === 'ios' || OS === 'web' || OS === 'dom') { throw new Error(`@react-native-community/react-native-device-info: NativeModule.RNDeviceInfo is null. To fix this issue try these steps: - • Run \`react-native link react-native-device-info\` in the project root. + • For react-native <= 0.59: Run \`react-native link react-native-device-info\` in the project root. • Rebuild and re-run the app. • If you are using CocoaPods on iOS, run \`pod install\` in the \`ios\` directory and then rebuild and re-run the app. You may also need to re-open Xcode to get the new pods. If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-device-info`); } - - RNDeviceInfo = require('./default'); } const devicesWithNotch = [ @@ -352,40 +346,72 @@ const deviceNamesByCode = { 'AppleTV6,2': 'Apple TV 4K', // Apple TV 4K }; -export async function getUniqueID() { - return RNDeviceInfo.getUniqueId(); +export async function getUniqueId() { + if (OS === 'android' || OS === 'ios' || OS === 'windows') { + return RNDeviceInfo.getUniqueId(); + } + return Promise.resolve('unknown'); } -export async function getInstanceID() { - return RNDeviceInfo.getInstanceId(); +export async function getInstanceId() { + if (OS === 'android') { + return RNDeviceInfo.getInstanceId(); + } else { + return Promise.resolve('unknown'); + } } export async function getSerialNumber() { - return RNDeviceInfo.getSerialNumber(); + if (OS === 'android') { + return RNDeviceInfo.getSerialNumber(); + } + return Promise.resolve('unknown'); } -export async function getIPAddress() { - return RNDeviceInfo.getIpAddress(); +export async function getAndroidId() { + if (OS === 'android') { + return RNDeviceInfo.getAndroidId(); + } + return Promise.resolve('unknown'); +} + +export async function getIpAddress() { + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getIpAddress(); + } + return Promise.resolve('unknown'); } export async function getCameraPresence() { - return RNDeviceInfo.getCameraPresence(); + if (OS === 'android' || OS === 'windows') { + return RNDeviceInfo.getCameraPresence(); + } + return Promise.resolve(false); } -export async function getMACAddress() { - return RNDeviceInfo.getMacAddress(); +export async function getMacAddress() { + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getMacAddress(); + } + return Promise.resolve('unknown'); } export async function getDeviceId() { - return RNDeviceInfo.getDeviceId(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getDeviceId(); + } + return Promise.resolve('unknown'); } export async function getManufacturer() { - return RNDeviceInfo.getSystemManufacturer(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getSystemManufacturer(); + } + return Promise.resolve('unknown'); } export async function getModel() { - if (Platform.OS === 'ios') { + if (OS === 'ios') { var deviceName; var deviceId = await RNDeviceInfo.getDeviceId(); if (deviceId) { @@ -404,45 +430,74 @@ export async function getModel() { } } return Promise.resolve(deviceName); - } else { + } + if (OS === 'android' || OS === 'windows') { return RNDeviceInfo.getModel(); } + return Promise.resolve('unknown'); } export async function getBrand() { - return RNDeviceInfo.getBrand(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getBrand(); + } + return Promise.resolve('unknown'); } export async function getSystemName() { - return RNDeviceInfo.getSystemName(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getSystemName(); + } + return Promise.resolve('unknown'); } export async function getSystemVersion() { - return RNDeviceInfo.getSystemVersion(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getSystemVersion(); + } + return Promise.resolve('unknown'); } export async function getBuildId() { - return RNDeviceInfo.getBuildId(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getBuildId(); + } + return Promise.resolve('unknown'); } -export async function getAPILevel() { - return RNDeviceInfo.getApiLevel(); +export async function getApiLevel() { + if (OS === 'android') { + return RNDeviceInfo.getApiLevel(); + } + return Promise.resolve(-1); } export async function getBundleId() { - return RNDeviceInfo.getBundleId(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getBundleId(); + } + return Promise.resolve('unknown'); } export async function getApplicationName() { - return RNDeviceInfo.getAppName(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getAppName(); + } + return Promise.resolve('unknown'); } export async function getBuildNumber() { - return RNDeviceInfo.getBuildNumber(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getBuildNumber(); + } + return Promise.resolve('unknown'); } export async function getVersion() { - return RNDeviceInfo.getAppVersion(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getAppVersion(); + } + return Promise.resolve('unknown'); } export async function getReadableVersion() { @@ -450,87 +505,150 @@ export async function getReadableVersion() { } export async function getDeviceName() { - return RNDeviceInfo.getDeviceName(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getDeviceName(); + } + return Promise.resolve('unknown'); } export async function getUsedMemory() { - return RNDeviceInfo.getUsedMemory(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getUsedMemory(); + } + return Promise.resolve(-1); } export async function getUserAgent() { - return RNDeviceInfo.getUserAgent(); + if (OS === 'android' || OS === 'macos' || OS === 'web') { + return RNDeviceInfo.getUserAgent(); + } + return Promise.resolve('unknown'); } export async function getFontScale() { - return RNDeviceInfo.getFontScale(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getFontScale(); + } + return Promise.resolve('unknown'); } export async function getBootloader() { - return RNDeviceInfo.getBootloader(); + if (OS === 'android') { + return RNDeviceInfo.getBootloader(); + } + return Promise.resolve('unknown'); } export async function getDevice() { - return RNDeviceInfo.getDevice(); + if (OS === 'android') { + return RNDeviceInfo.getDevice(); + } + return Promise.resolve('unknown'); } export async function getDisplay() { - return RNDeviceInfo.getDisplay(); + if (OS === 'android') { + return RNDeviceInfo.getDisplay(); + } + return Promise.resolve('unknown'); } export async function getFingerprint() { - return RNDeviceInfo.getFingerprint(); + if (OS === 'android') { + return RNDeviceInfo.getFingerprint(); + } + return Promise.resolve('unknown'); } export async function getHardware() { - return RNDeviceInfo.getHardware(); + if (OS === 'android') { + return RNDeviceInfo.getHardware(); + } + return Promise.resolve('unknown'); } export async function getHost() { - return RNDeviceInfo.getHost(); + if (OS === 'android') { + return RNDeviceInfo.getHost(); + } + return Promise.resolve('unknown'); } export async function getProduct() { - return RNDeviceInfo.getProduct(); + if (OS === 'android') { + return RNDeviceInfo.getProduct(); + } + return Promise.resolve('unknown'); } export async function getTags() { - return RNDeviceInfo.getTags(); + if (OS === 'android') { + return RNDeviceInfo.getTags(); + } + return Promise.resolve('unknown'); } export async function getType() { - return RNDeviceInfo.getType(); + if (OS === 'android') { + return RNDeviceInfo.getType(); + } + return Promise.resolve('unknown'); } -export async function getBaseOS() { - return RNDeviceInfo.getBaseOS(); +export async function getBaseOs() { + if (OS === 'android') { + return RNDeviceInfo.getBaseOs(); + } + return Promise.resolve('unknown'); } export async function getPreviewSdkInt() { - return RNDeviceInfo.getPreviewSdkInt(); + if (OS === 'android') { + return RNDeviceInfo.getPreviewSdkInt(); + } + return Promise.resolve('unknown'); } export async function getSecurityPatch() { - return RNDeviceInfo.getSecurityPatch(); + if (OS === 'android') { + return RNDeviceInfo.getSecurityPatch(); + } + return Promise.resolve('unknown'); } export async function getCodename() { - return RNDeviceInfo.getCodename(); + if (OS === 'android') { + return RNDeviceInfo.getCodename(); + } + return Promise.resolve('unknown'); } export async function getIncremental() { - return RNDeviceInfo.getIncremental(); + if (OS === 'android') { + return RNDeviceInfo.getIncremental(); + } + return Promise.resolve('unknown'); } export async function isEmulator() { - return RNDeviceInfo.isEmulator(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.isEmulator(); + } + return Promise.resolve(false); } export async function isTablet() { - return RNDeviceInfo.isTablet(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.isTablet(); + } + return Promise.resolve(false); } export async function isPinOrFingerprintSet() { - return RNDeviceInfo.isPinOrFingerprintSet(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.isPinOrFingerprintSet(); + } + return Promise.resolve(false); } export async function hasNotch() { @@ -546,50 +664,87 @@ export async function hasNotch() { } export async function getFirstInstallTime() { - return RNDeviceInfo.getFirstInstallTime(); + if (OS === 'android' || OS === 'windows') { + return RNDeviceInfo.getFirstInstallTime(); + } + return Promise.resolve(-1); } export async function getInstallReferrer() { - return RNDeviceInfo.getInstallReferrer(); + if (OS === 'android') { + return RNDeviceInfo.getInstallReferrer(); + } + return Promise.resolve('unknown'); } export async function getLastUpdateTime() { - return RNDeviceInfo.getLastUpdateTime(); + if (OS === 'android') { + return RNDeviceInfo.getLastUpdateTime(); + } + return Promise.resolve(-1); } export async function getPhoneNumber() { - return RNDeviceInfo.getPhoneNumber(); + if (OS === 'android') { + return RNDeviceInfo.getPhoneNumber(); + } + return Promise.resolve('unknown'); } export async function getCarrier() { - return RNDeviceInfo.getCarrier(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getCarrier(); + } + return Promise.resolve('unknown'); } export async function getTotalMemory() { - return RNDeviceInfo.getTotalMemory(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getTotalMemory(); + } + return Promise.resolve(-1); } export async function getMaxMemory() { - return RNDeviceInfo.getMaxMemory(); + if (OS === 'android' || OS === 'windows') { + return RNDeviceInfo.getMaxMemory(); + } + return Promise.resolve(-1); } export async function getTotalDiskCapacity() { - return RNDeviceInfo.getTotalDiskCapacity(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getTotalDiskCapacity(); + } + return Promise.resolve(-1); } export async function getFreeDiskStorage() { - return RNDeviceInfo.getFreeDiskStorage(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getFreeDiskStorage(); + } + return Promise.resolve(-1); } export async function getBatteryLevel() { - return RNDeviceInfo.getBatteryLevel(); + if (OS === 'android' || OS === 'macos' || OS === 'windows') { + return RNDeviceInfo.getBatteryLevel(); + } + return Promise.resolve(-1); } export async function getPowerState() { - return RNDeviceInfo.getPowerState(); + if (OS === 'macos') { + return RNDeviceInfo.getPowerState(); + } + return Promise.resolve({}); } + export async function isBatteryCharging() { - return RNDeviceInfo.isBatteryCharging(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.isBatteryCharging(); + } + return Promise.resolve(false); } export async function isLandscape() { @@ -597,49 +752,77 @@ export async function isLandscape() { return width >= height; } -export async function isAirPlaneMode() { - return RNDeviceInfo.isAirPlaneMode(); +export async function isAirplaneMode() { + if (OS === 'android') { + return RNDeviceInfo.isAirplaneMode(); + } + return Promise.resolve(false); } export async function getDeviceType() { - return RNDeviceInfo.getDeviceType(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getDeviceType(); + } + return Promise.resolve('unknown'); } export async function supportedAbis() { - return RNDeviceInfo.getSupportedAbis(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getSupportedAbis(); + } + return Promise.resolve([]); } export async function supported32BitAbis() { - return RNDeviceInfo.getSupported32BitAbis(); + if (OS === 'android') { + return RNDeviceInfo.getSupported32BitAbis(); + } + return Promise.resolve([]); } export async function supported64BitAbis() { - return RNDeviceInfo.getSupported64BitAbis(); + if (OS === 'android') { + return RNDeviceInfo.getSupported64BitAbis(); + } + return Promise.resolve([]); } export async function hasSystemFeature(feature) { - return RNDeviceInfo.hasSystemFeature(feature); + if (OS === 'android') { + return RNDeviceInfo.hasSystemFeature(feature); + } + return Promise.resolve(false); } export async function getSystemAvailableFeatures() { - return RNDeviceInfo.getSystemAvailableFeatures(); + if (OS === 'android') { + return RNDeviceInfo.getSystemAvailableFeatures(); + } + return Promise.resolve([]); } export async function isLocationEnabled() { - return RNDeviceInfo.isLocationEnabled(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.isLocationEnabled(); + } + return Promise.resolve(false); } export async function getAvailableLocationProviders() { - return RNDeviceInfo.getAvailableLocationProviders(); + if (OS === 'android' || OS === 'macos') { + return RNDeviceInfo.getAvailableLocationProviders(); + } + return Promise.resolve({}); } export default { - getUniqueID, - getInstanceID, + getUniqueId, + getInstanceId, getSerialNumber, - getIPAddress, + getAndroidId, + getIpAddress, getCameraPresence, - getMACAddress, + getMacAddress, getDeviceId, getManufacturer, getModel, @@ -647,7 +830,7 @@ export default { getSystemName, getSystemVersion, getBuildId, - getAPILevel, + getApiLevel, getBundleId, getApplicationName, getBuildNumber, @@ -666,7 +849,7 @@ export default { getProduct, getTags, getType, - getBaseOS, + getBaseOs, getPreviewSdkInt, getSecurityPatch, getCodename, @@ -688,7 +871,7 @@ export default { getPowerState, isBatteryCharging, isLandscape, - isAirPlaneMode, + isAirplaneMode, getDeviceType, supportedAbis, supported32BitAbis, diff --git a/deviceinfo.js.flow b/deviceinfo.js.flow index e4c71629c..fd2195d5d 100644 --- a/deviceinfo.js.flow +++ b/deviceinfo.js.flow @@ -3,7 +3,7 @@ export type DeviceType = 'Handset' | 'Tablet' | 'Tv' | 'Unknown'; declare module.exports: { - getUniqueID: () => Promise, + getUniqueId: () => Promise, getManufacturer: () => Promise, getBrand: () => Promise, getModel: () => Promise, @@ -19,7 +19,7 @@ declare module.exports: { getDeviceName: () => Promise, getUsedMemory: () => Promise, getUserAgent: () => Promise, - getInstanceID: () => Promise, + getInstanceId: () => Promise, getInstallReferrer: () => Promise, isEmulator: () => Promise, isTablet: () => Promise, @@ -33,7 +33,7 @@ declare module.exports: { getProduct: () => Promise, getTags: () => Promise, getType: () => Promise, - getBaseOS: () => Promise, + getBaseOs: () => Promise, getPreviewSdkInt: () => Promise, getSecurityPatch: () => Promise, getCodename: () => Promise, @@ -43,10 +43,11 @@ declare module.exports: { getFirstInstallTime: () => Promise, getLastUpdateTime: () => Promise, getSerialNumber: () => Promise, - getAPILevel: () => Promise, - getIPAddress: () => Promise, + getAndroidId: () => Promise, + getApiLevel: () => Promise, + getIpAddress: () => Promise, getCameraPresence: () => Promise, - getMACAddress: () => Promise, + getMacAddress: () => Promise, getPhoneNumber: () => Promise, getCarrier: () => Promise, getTotalMemory: () => Promise, @@ -57,7 +58,7 @@ declare module.exports: { getPowerState: () => Promise, isBatteryCharging:() => Promise, isLandscape: () => Promise, - isAirPlaneMode: () => Promise, + isAirplaneMode: () => Promise, getDeviceType: () => Promise, supportedAbis: () => Promise>, supported32BitAbis: () => Promise>, diff --git a/example/App.js b/example/App.js index 95d0d6922..894473801 100644 --- a/example/App.js +++ b/example/App.js @@ -11,7 +11,7 @@ import React, {Component} from 'react'; import {Platform, ScrollView, StyleSheet, Text, SafeAreaView} from 'react-native'; import DeviceInfo from 'react-native-device-info'; import { - getUniqueID, + getUniqueId, getManufacturer, getBrand, getModel, @@ -31,7 +31,7 @@ export default class App extends Component { const ios = Platform.OS === 'ios'; try { - deviceJSON.uniqueID = await getUniqueID(); + deviceJSON.uniqueId = await getUniqueId(); deviceJSON.manufacturer = await getManufacturer(); deviceJSON.brand = await getBrand(); deviceJSON.model = await getModel(); @@ -40,63 +40,60 @@ export default class App extends Component { deviceJSON.systemVersion = await DeviceInfo.getSystemVersion(); deviceJSON.buildId = await DeviceInfo.getBuildId(); deviceJSON.bundleId = await DeviceInfo.getBundleId(); - deviceJSON.isCameraPresent = ios ? -1 : await DeviceInfo.getCameraPresence(); + deviceJSON.isCameraPresent = await DeviceInfo.getCameraPresence(); deviceJSON.buildNumber = await DeviceInfo.getBuildNumber(); deviceJSON.version = await DeviceInfo.getVersion(); deviceJSON.readableVersion = await DeviceInfo.getReadableVersion(); deviceJSON.deviceName = await DeviceInfo.getDeviceName(); deviceJSON.usedMemory = await DeviceInfo.getUsedMemory(); deviceJSON.userAgent = await DeviceInfo.getUserAgent(); - deviceJSON.instanceID = ios ? '' : await DeviceInfo.getInstanceID(); - deviceJSON.installReferrer = ios ? '' : await DeviceInfo.getInstallReferrer(); + deviceJSON.instanceId = await DeviceInfo.getInstanceId(); + deviceJSON.installReferrer = await DeviceInfo.getInstallReferrer(); deviceJSON.isEmulator = await DeviceInfo.isEmulator(); deviceJSON.isTablet = await DeviceInfo.isTablet(); deviceJSON.fontScale = await DeviceInfo.getFontScale(); deviceJSON.hasNotch = await DeviceInfo.hasNotch(); - deviceJSON.firstInstallTime = ios ? -1 : await DeviceInfo.getFirstInstallTime(); - deviceJSON.lastUpdateTime = ios ? -1 : await DeviceInfo.getLastUpdateTime(); - deviceJSON.serialNumber = ios ? -1 : await DeviceInfo.getSerialNumber(); - deviceJSON.IPAddress = await DeviceInfo.getIPAddress(); - deviceJSON.MACAddress = await DeviceInfo.getMACAddress(); // needs android.permission.ACCESS_WIFI_STATE ? - deviceJSON.phoneNumber = ios ? '' : await DeviceInfo.getPhoneNumber(); // needs android.permission.READ_PHONE_STATE ? - deviceJSON.APILevel = ios ? -1 : await DeviceInfo.getAPILevel(); + deviceJSON.firstInstallTime = await DeviceInfo.getFirstInstallTime(); + deviceJSON.lastUpdateTime = await DeviceInfo.getLastUpdateTime(); + deviceJSON.serialNumber = await DeviceInfo.getSerialNumber(); + deviceJSON.androidId = await DeviceInfo.getAndroidId(); + deviceJSON.IpAddress = await DeviceInfo.getIpAddress(); + deviceJSON.MacAddress = await DeviceInfo.getMacAddress(); // needs android.permission.ACCESS_WIFI_STATE + deviceJSON.phoneNumber = await DeviceInfo.getPhoneNumber(); // needs android.permission.READ_PHONE_STATE + deviceJSON.ApiLevel = await DeviceInfo.getApiLevel(); deviceJSON.carrier = await DeviceInfo.getCarrier(); deviceJSON.totalMemory = await DeviceInfo.getTotalMemory(); - deviceJSON.maxMemory = ios ? -1 : await DeviceInfo.getMaxMemory(); - deviceJSON.totalDiskCapacity = await DeviceInfo.getTotalDiskCapacity(); // FIXME needs a patch for integer overflow on Android - deviceJSON.freeDiskStorage = await DeviceInfo.getFreeDiskStorage(); // FIXME needs a patch for integer overflow on Android + deviceJSON.maxMemory = await DeviceInfo.getMaxMemory(); + deviceJSON.totalDiskCapacity = await DeviceInfo.getTotalDiskCapacity(); + deviceJSON.freeDiskStorage = await DeviceInfo.getFreeDiskStorage(); deviceJSON.batteryLevel = await DeviceInfo.getBatteryLevel(); deviceJSON.isLandscape = await DeviceInfo.isLandscape(); - deviceJSON.isAirplaneMode = ios ? false : await DeviceInfo.isAirPlaneMode(); - deviceJSON.isBatteryCharging = ios ? false : await DeviceInfo.isBatteryCharging(); + deviceJSON.isAirplaneMode = await DeviceInfo.isAirplaneMode(); + deviceJSON.isBatteryCharging = await DeviceInfo.isBatteryCharging(); deviceJSON.deviceType = await DeviceInfo.getDeviceType(); deviceJSON.isPinOrFingerprintSet = await DeviceInfo.isPinOrFingerprintSet(); deviceJSON.supportedAbis = await DeviceInfo.supportedAbis(); - deviceJSON.hasSystemFeature = ios - ? false - : await DeviceInfo.hasSystemFeature('amazon.hardware.fire_tv'); - deviceJSON.getSystemAvailableFeatures = ios - ? [] - : await DeviceInfo.getSystemAvailableFeatures(); - deviceJSON.powerState = ios ? await DeviceInfo.getPowerState() : ''; + deviceJSON.hasSystemFeature = await DeviceInfo.hasSystemFeature('android.software.webview'); + deviceJSON.getSystemAvailableFeatures = await DeviceInfo.getSystemAvailableFeatures(); + deviceJSON.powerState = await DeviceInfo.getPowerState(); deviceJSON.isLocationEnabled = await DeviceInfo.isLocationEnabled(); deviceJSON.getAvailableLocationProviders = await DeviceInfo.getAvailableLocationProviders(); - deviceJSON.bootloader = ios ? '' : await DeviceInfo.getBootloader(); - deviceJSON.device = ios ? '' : await DeviceInfo.getDevice(); - deviceJSON.display = ios ? '' : await DeviceInfo.getDisplay(); - deviceJSON.fingerprint = ios ? '' : await DeviceInfo.getFingerprint(); - deviceJSON.hardware = ios ? '' : await DeviceInfo.getHardware(); - deviceJSON.host = ios ? '' : await DeviceInfo.getHost(); - deviceJSON.product = ios ? '' : await DeviceInfo.getProduct(); - deviceJSON.tags = ios ? '' : await DeviceInfo.getTags(); - deviceJSON.type = ios ? '' : await DeviceInfo.getType(); - deviceJSON.baseOS = ios ? '' : await DeviceInfo.getBaseOS(); - deviceJSON.previewSdkInt = ios ? -1 : await DeviceInfo.getPreviewSdkInt(); - deviceJSON.securityPatch = ios ? '' : await DeviceInfo.getSecurityPatch(); - deviceJSON.codename = ios ? '' : await DeviceInfo.getCodename(); - deviceJSON.incremental = ios ? '' : await DeviceInfo.getIncremental(); - deviceJSON.supported32BitAbis = ios ? [] : await DeviceInfo.supported32BitAbis(); - deviceJSON.supported64BitAbis = ios ? [] : await DeviceInfo.supported64BitAbis(); + deviceJSON.bootloader = await DeviceInfo.getBootloader(); + deviceJSON.device = await DeviceInfo.getDevice(); + deviceJSON.display = await DeviceInfo.getDisplay(); + deviceJSON.fingerprint = await DeviceInfo.getFingerprint(); + deviceJSON.hardware = await DeviceInfo.getHardware(); + deviceJSON.host = await DeviceInfo.getHost(); + deviceJSON.product = await DeviceInfo.getProduct(); + deviceJSON.tags = await DeviceInfo.getTags(); + deviceJSON.type = await DeviceInfo.getType(); + deviceJSON.baseOS = await DeviceInfo.getBaseOs(); + deviceJSON.previewSdkInt = await DeviceInfo.getPreviewSdkInt(); + deviceJSON.securityPatch = await DeviceInfo.getSecurityPatch(); + deviceJSON.codename = await DeviceInfo.getCodename(); + deviceJSON.incremental = await DeviceInfo.getIncremental(); + deviceJSON.supported32BitAbis = await DeviceInfo.supported32BitAbis(); + deviceJSON.supported64BitAbis = await DeviceInfo.supported64BitAbis(); } catch (e) { console.log('Trouble getting device info ', e); } diff --git a/package.json b/package.json index 4bbe2f083..653219b88 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "scripts": { "analyze": "yarn ts-check && yarn flow-check", - "dev-sync": "cp -r deviceinfo* *podspec default windows web android ios example/node_modules/react-native-device-info/", + "dev-sync": "cp -r deviceinfo* *podspec windows web android ios example/node_modules/react-native-device-info/", "flow-check": "npx flow check-contents < deviceinfo.js.flow", "lint": "npx eslint ./ --ignore-pattern example --ignore-pattern node_modules --fix --quiet", "shipit": "np --no-yarn", @@ -57,7 +57,7 @@ "eslint": "^6.3.0", "eslint-plugin-prettier": "^3.1.0", "flow-bin": "^0.106.3", - "husky": "^3.0.2", + "husky": "^3.0.5", "lint-staged": "^9.2.1", "np": "^5.0.3", "prettier": "^1.18.2", diff --git a/web/index.js b/web/index.js index 630cde1bc..896a0d90e 100644 --- a/web/index.js +++ b/web/index.js @@ -1,8 +1,8 @@ /** * react-native-web empty polyfill. */ -var RNDeviceInfo = require('../default'); - -RNDeviceInfo.userAgent = window.navigator.userAgent; - -module.exports = RNDeviceInfo; +module.exports = { + getUserAgent: () => { + return Promise.resolve(window.navigator.userAgent); + }, +}; diff --git a/windows/RNDeviceInfo/RNDeviceInfoModule.cs b/windows/RNDeviceInfo/RNDeviceInfoModule.cs index 00039660b..a6a07eba2 100644 --- a/windows/RNDeviceInfo/RNDeviceInfoModule.cs +++ b/windows/RNDeviceInfo/RNDeviceInfoModule.cs @@ -109,22 +109,6 @@ public async void getBatteryLevel(IPromise promise) } } - [ReactMethod] - public async void getCameraPresence(IPromise promise) - { - var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture); - promise.Resolve(devices.Count > 0); - } - - [ReactMethod] - public async void getAppVersion(IPromise promise) { promise.Resolve("not available"); } - - [ReactMethod] - public async void getBuildVersion(IPromise promise) { promise.Resolve("not available"); } - - [ReactMethod] - public async void getBuildNumber(IPromise promise) { promise.Resolve(0); } - [ReactMethod] public async void getAppVersion(IPromise promise) { @@ -159,24 +143,9 @@ public async void getBuildVersion(IPromise promise) getBuildNumber(promise); } - [ReactMethod] - public async void getInstanceId(IPromise promise) { promise.Resolve("not available"); } - [ReactMethod] public async void getSystemName(IPromise promise) { promise.Resolve("Windows"); } - [ReactMethod] - public async void getApiLevel(IPromise promise) { promise.Resolve("not available"); } - - [ReactMethod] - public async void getBuildId(IPromise promise) { promise.Resolve("not available"); } - - [ReactMethod] - public async void getUserAgent(IPromise promise) { promise.Resolve("not available"); } - - [ReactMethod] - public async void getCarrier(IPromise promise) { promise.Resolve("not available"); } - [ReactMethod] public async void getMaxMemory(IPromise promise) { promise.Resolve(MemoryManager.AppMemoryUsageLimit); } diff --git a/yarn.lock b/yarn.lock index 6cd40dbce..67411cd31 100644 --- a/yarn.lock +++ b/yarn.lock @@ -668,9 +668,9 @@ integrity sha512-1dVNHT76Uu5N3eJNTYcvxee+jzX4Z9lfciqRRHCU27ihbUcYi+iSc2iml5Ke1LXe1SyJCLA0+14Jh4tXJgOppA== "@hapi/hoek@8.x.x": - version "8.2.1" - resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-8.2.1.tgz#924af04cbb22e17359c620d2a9c946e63f58eb77" - integrity sha512-JPiBy+oSmsq3St7XlipfN5pNA6bDJ1kpa73PrK/zR29CVClDVqy04AanM/M/qx5bSF+I61DdCfAvRrujau+zRg== + version "8.2.2" + resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-8.2.2.tgz#6eaa2e1ec3b50dfb8dccbe705dc289094652bc2d" + integrity sha512-18P3VwngjNEcmvPj1mmiHLPyUPjhPAxIyJKDj4PRIY0F5ac3P0Vd0hkASPyWXHK0rfY3P9N2FoxV8ZuYaRBZ1g== "@hapi/joi@^15.0.3": version "15.1.1" @@ -2258,7 +2258,7 @@ eslint-visitor-keys@^1.0.0, eslint-visitor-keys@^1.1.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2" integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A== -eslint@^6.2.2: +eslint@^6.3.0: version "6.3.0" resolved "https://registry.yarnpkg.com/eslint/-/eslint-6.3.0.tgz#1f1a902f67bfd4c354e7288b81e40654d927eb6a" integrity sha512-ZvZTKaqDue+N8Y9g0kp6UPZtS4FSY3qARxBs7p4f0H0iof381XHduqVerFWtK8DPtKmemqbqCFENWSQgPR/Gow== @@ -3000,10 +3000,10 @@ http-errors@~1.7.2: statuses ">= 1.5.0 < 2" toidentifier "1.0.0" -husky@^3.0.2: - version "3.0.4" - resolved "https://registry.yarnpkg.com/husky/-/husky-3.0.4.tgz#10a48ac11ab50859b0939750fa0b4e07ad0bf669" - integrity sha512-7Rnt8aJfy+MlV28snmYK7O7vWwtOfeVxV6KhLpUFXlmx5ukQ1nQmNUB7QsAwSgdySB5X+bm7q7JIRgazqBUzKA== +husky@^3.0.5: + version "3.0.5" + resolved "https://registry.yarnpkg.com/husky/-/husky-3.0.5.tgz#d7db27c346645a8dc52df02aa534a377ad7925e0" + integrity sha512-cKd09Jy9cDyNIvAdN2QQAP/oA21sle4FWXjIMDttailpLAYZuBE7WaPmhrkj+afS8Sj9isghAtFvWSQ0JiwOHg== dependencies: chalk "^2.4.2" cosmiconfig "^5.2.1" @@ -5805,9 +5805,9 @@ shell-quote@1.6.1: jsonify "~0.0.0" shell-quote@^1.6.1: - version "1.7.1" - resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.1.tgz#3161d969886fb14f9140c65245a5dd19b6f0b06b" - integrity sha512-2kUqeAGnMAu6YrTPX4E3LfxacH9gKljzVjlkUeSqY0soGwK4KLl7TURXCem712tkhBCeeaFP9QK4dKn88s3Icg== + version "1.7.2" + resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.7.2.tgz#67a7d02c76c9da24f99d20808fcaded0e0e04be2" + integrity sha512-mRz/m/JVscCrkMyPqHc/bczi3OQHkLTqXHEFu0zDhK/qfv3UcOA4SVmRCLmos4bhjr9ekVQubj/R7waKapmiQg== shellwords@^0.1.1: version "0.1.1"