Skip to content

Commit

Permalink
fix: optional uniqueId constant on some platforms
Browse files Browse the repository at this point in the history
Some platforms like Windows cannot call synchronous methods like
getUniqueIdSync and have to rely on constants instead.
In other cases sync call is preferred, for example to avoid store
policy issues like react-native-device-info#1427
  • Loading branch information
mstawecki committed Jul 5, 2022
1 parent 29cd398 commit 43bbc68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 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.getUniqueIdSync(),
getter: () => RNDeviceInfo.uniqueId || RNDeviceInfo.getUniqueIdSync(),
});

let uniqueId: string;
Expand Down
1 change: 1 addition & 0 deletions src/internal/privateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface NativeConstants {
model: string;
systemName: string;
systemVersion: string;
uniqueId?: string;
}

interface HiddenNativeMethods {
Expand Down
15 changes: 8 additions & 7 deletions windows/code/RNDeviceInfoCPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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 All @@ -48,7 +49,7 @@ namespace winrt::RNDeviceInfoCPP

// What is a tablet is a debateable topic in Windows, as some windows devices can dynamically switch back and forth.
// Also, see isTabletMode() instead of isTablet or deviceType.
// More refinement should be applied into this area as neccesary.
// More refinement should be applied into this area as neccesary.
bool isTabletHelper()
{
// AnalyticsInfo doesn't always return the values one might expect.
Expand All @@ -58,9 +59,9 @@ namespace winrt::RNDeviceInfoCPP
// [Windows.Desktop, Windows.Mobile, Windows.Xbox, Windows.Holographic, Windows.Team, Windows.IoT]
auto deviceForm = winrt::Windows::System::Profile::AnalyticsInfo::DeviceForm();
auto deviceFamily = winrt::Windows::System::Profile::AnalyticsInfo::VersionInfo().DeviceFamily();

bool isTabletByAnalytics = deviceForm == L"Tablet" || deviceForm == L"Mobile" || deviceFamily == L"Windows.Mobile";

if (isTabletByAnalytics)
{
return true;
Expand All @@ -85,7 +86,7 @@ namespace winrt::RNDeviceInfoCPP
JSValueArray getSupportedAbisSync() noexcept
{
JSValueArray result = JSValueArray{};
winrt::Windows::System::ProcessorArchitecture architecture =
winrt::Windows::System::ProcessorArchitecture architecture =
winrt::Windows::ApplicationModel::Package::Current().Id().Architecture();
std::string arch;
switch (architecture)
Expand All @@ -109,13 +110,13 @@ namespace winrt::RNDeviceInfoCPP
result.push_back(arch);
return result;
}

REACT_METHOD(getSupportedAbis)
void getSupportedAbis(ReactPromise<JSValueArray> promise) noexcept
{
promise.Resolve(getSupportedAbisSync());
}

REACT_SYNC_METHOD(getDeviceTypeSync);
std::string getDeviceTypeSync() noexcept
{
Expand Down Expand Up @@ -685,7 +686,7 @@ namespace winrt::RNDeviceInfoCPP
{
promise.Resolve(getDeviceIdSync());
}

REACT_SYNC_METHOD(getSerialNumberSync);
std::string getSerialNumberSync() noexcept
{
Expand Down

0 comments on commit 43bbc68

Please sign in to comment.