From a1e81185416a53c7c7d0cfc67e40079fd0073e7c Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Wed, 5 Jun 2024 04:24:00 -0700 Subject: [PATCH] Fix ANDROID_ID segment for inspector device ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes a bug where page IDs would collide for multiple connected Android devices running the same React Native app. The `Secure.ANDROID_ID` key (not value!) was being substituted in the ID string (pre-hashing) — now this segment is fixed. Changelog: [Android][Changed] - Update constructor signature of `DevServerHelper` Reviewed By: hoxyq Differential Revision: D58134323 fbshipit-source-id: 859e2758108e266167205a777bb6a6e87ca0573b --- .../react-native/ReactAndroid/api/ReactAndroid.api | 2 +- .../facebook/react/devsupport/DevServerHelper.java | 12 ++++++++---- .../react/devsupport/DevSupportManagerBase.java | 4 +--- .../react/devsupport/PerftestDevSupportManager.java | 4 +--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index ba5759635d5197..5a6275d610c873 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -2067,7 +2067,7 @@ public final class com/facebook/react/devsupport/DefaultDevSupportManagerFactory public class com/facebook/react/devsupport/DevServerHelper { public static final field RELOAD_APP_EXTRA_JS_PROXY Ljava/lang/String; - public fun (Lcom/facebook/react/modules/debug/interfaces/DeveloperSettings;Ljava/lang/String;Lcom/facebook/react/packagerconnection/PackagerConnectionSettings;)V + public fun (Lcom/facebook/react/modules/debug/interfaces/DeveloperSettings;Landroid/content/Context;Lcom/facebook/react/packagerconnection/PackagerConnectionSettings;)V public fun closeInspectorConnection ()V public fun closePackagerConnection ()V public fun disableDebugger ()V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java index a6990a1c9771ca..5e907fd884a469 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java @@ -7,9 +7,10 @@ package com.facebook.react.devsupport; +import android.content.Context; import android.net.Uri; import android.os.AsyncTask; -import android.provider.Settings; +import android.provider.Settings.Secure; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; @@ -104,6 +105,7 @@ public String typeID() { private final OkHttpClient mClient; private final BundleDownloader mBundleDownloader; private final PackagerStatusCheck mPackagerStatusCheck; + private final Context mApplicationContext; private final String mPackageName; private @Nullable JSPackagerClient mPackagerClient; @@ -111,7 +113,7 @@ public String typeID() { public DevServerHelper( DeveloperSettings developerSettings, - String packageName, + Context applicationContext, PackagerConnectionSettings packagerConnectionSettings) { mSettings = developerSettings; mPackagerConnectionSettings = packagerConnectionSettings; @@ -123,7 +125,8 @@ public DevServerHelper( .build(); mBundleDownloader = new BundleDownloader(mClient); mPackagerStatusCheck = new PackagerStatusCheck(mClient); - mPackageName = packageName; + mApplicationContext = applicationContext; + mPackageName = applicationContext.getPackageName(); } public void openPackagerConnection( @@ -301,7 +304,8 @@ private String getInspectorDeviceId() { // * randomly generated when the user first sets up the device and should remain constant for // the lifetime of the user's device (API level < 26). // [Source: Android docs] - String androidId = Settings.Secure.ANDROID_ID; + String androidId = + Secure.getString(mApplicationContext.getContentResolver(), Secure.ANDROID_ID); String rawDeviceId = String.format( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index 15184779f68052..418215500e80e6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -143,9 +143,7 @@ public DevSupportManagerBase( mDevSettings = new DevInternalSettings(applicationContext, this::reloadSettings); mDevServerHelper = new DevServerHelper( - mDevSettings, - mApplicationContext.getPackageName(), - mDevSettings.getPackagerConnectionSettings()); + mDevSettings, mApplicationContext, mDevSettings.getPackagerConnectionSettings()); mBundleDownloadListener = devBundleDownloadListener; // Prepare shake gesture detector (will be started/stopped from #reload) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PerftestDevSupportManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PerftestDevSupportManager.java index 4e832f054cb1a4..a4acb012d59777 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PerftestDevSupportManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PerftestDevSupportManager.java @@ -29,9 +29,7 @@ public void onInternalSettingsChanged() {} }); mDevServerHelper = new DevServerHelper( - mDevSettings, - applicationContext.getPackageName(), - mDevSettings.getPackagerConnectionSettings()); + mDevSettings, applicationContext, mDevSettings.getPackagerConnectionSettings()); } @Override