Skip to content

Commit

Permalink
fix(android): remove uniqueId from constants
Browse files Browse the repository at this point in the history
Obtaining uniqueId on Android has to be explicit in line with newly enforced Huawei and Google Play guidelines.
As an alternative, native method getUniqueIdSync has been implemented on all applicable platforms and is used in the current getUniqueId API method instead of constants.

Fixes react-native-device-info#1427

BREAKING CHANGE: `uniqueId` constant is no longer available,
use `getUniqueId()` instead.
  • Loading branch information
mstawecki committed Jul 6, 2022
1 parent 082bf6b commit 47e561c
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 10 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,6 @@ DeviceInfo.getTotalMemory().then((totalMemory) => {
### getUniqueId()
This is a constant and may be referenced directly
Gets the device unique ID.
On Android it is currently identical to `getAndroidId()` in this module.
On iOS it uses the `DeviceUID` uid identifier.
Expand All @@ -1014,7 +1012,7 @@ On Windows it uses `Windows.Security.ExchangeActiveSyncProvisioning.EasClientDev
let uniqueId = DeviceInfo.getUniqueId();
// iOS: "FCDBD8EF-62FC-4ECB-B2F5-92C9E79AC7F9"
// Android: "dd96dec43fb81c97"
// Windows: ?
// Windows: "b97e203a-fd09-11ec-b939-0242ac120002"
```
#### Notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public Map<String, Object> getConstants() {

final Map<String, Object> constants = new HashMap<>();

constants.put("uniqueId", getUniqueIdSync());
constants.put("deviceId", Build.BOARD);
constants.put("bundleId", getReactApplicationContext().getPackageName());
constants.put("systemName", "Android");
Expand Down
3 changes: 2 additions & 1 deletion example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getManufacturer,
getManufacturerSync,
syncUniqueId,
getUniqueId,
useBatteryLevel,
useBatteryLevelIsLow,
usePowerState,
Expand Down Expand Up @@ -107,7 +108,6 @@ export default class App extends Component {
getConstantDeviceInfo() {
let deviceJSON = {};

deviceJSON.uniqueId = DeviceInfo.getUniqueId();
deviceJSON.deviceId = DeviceInfo.getDeviceId();
deviceJSON.bundleId = DeviceInfo.getBundleId();
deviceJSON.systemName = DeviceInfo.getSystemName();
Expand All @@ -127,6 +127,7 @@ export default class App extends Component {
getSyncDeviceInfo() {
let deviceJSON = {};

deviceJSON.uniqueId = getUniqueId();
deviceJSON.manufacturer = getManufacturerSync();
deviceJSON.buildId = DeviceInfo.getBuildIdSync();
deviceJSON.isCameraPresent = DeviceInfo.isCameraPresentSync();
Expand Down
3 changes: 1 addition & 2 deletions ios/RNDeviceInfo/RNDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ + (BOOL)requiresMainQueueSetup

- (NSDictionary *)constantsToExport {
return @{
@"uniqueId": [self getUniqueId],
@"deviceId": [self getDeviceId],
@"bundleId": [self getBundleId],
@"systemName": [self getSystemName],
Expand Down Expand Up @@ -364,7 +363,7 @@ - (NSString *) getBuildId {
return self.getBuildId;
}

- (NSString *) getUniqueId {
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getUniqueIdSync) {
return [DeviceUID uid];
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const getUniqueId = () =>
defaultValue: 'unknown',
memoKey: 'uniqueId',
supportedPlatforms: ['android', 'ios', 'windows'],
getter: () => RNDeviceInfo.uniqueId,
getter: () => RNDeviceInfo.getUniqueIdSync(),
});

let uniqueId: string;
Expand Down Expand Up @@ -918,6 +918,7 @@ const deviceInfoModule: DeviceInfoModule = {
getType,
getTypeSync,
getUniqueId,
getUniqueIdSync: getUniqueId,
getUsedMemory,
getUsedMemorySync,
getUserAgent,
Expand Down
2 changes: 1 addition & 1 deletion src/internal/privateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface NativeConstants {
model: string;
systemName: string;
systemVersion: string;
uniqueId: string;
}

interface HiddenNativeMethods {
Expand Down Expand Up @@ -114,6 +113,7 @@ interface ExposedNativeMethods {
getTotalMemorySync: () => number;
getType: () => Promise<string>;
getTypeSync: () => string;
getUniqueIdSync: () => string;
getUsedMemory: () => Promise<number>;
getUsedMemorySync: () => number;
getUserAgent: () => Promise<string>;
Expand Down
1 change: 0 additions & 1 deletion windows/code/RNDeviceInfoCPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace winrt::RNDeviceInfoCPP
REACT_CONSTANT_PROVIDER(constantsViaConstantsProvider);
void constantsViaConstantsProvider(ReactConstantProvider& provider) noexcept
{
provider.Add(L"uniqueId", getUniqueIdSync());
provider.Add(L"deviceId", getDeviceIdSync());
provider.Add(L"serialNumber", getSerialNumberSync());
provider.Add(L"bundleId", getBundleIdSync());
Expand Down

0 comments on commit 47e561c

Please sign in to comment.