|
9 | 9 |
|
10 | 10 | import android.net.Uri;
|
11 | 11 | import android.os.AsyncTask;
|
| 12 | +import android.provider.Settings; |
12 | 13 | import androidx.annotation.NonNull;
|
13 | 14 | import androidx.annotation.Nullable;
|
14 | 15 | import com.facebook.common.logging.FLog;
|
|
30 | 31 | import com.facebook.react.util.RNLog;
|
31 | 32 | import java.io.File;
|
32 | 33 | import java.io.IOException;
|
| 34 | +import java.io.UnsupportedEncodingException; |
| 35 | +import java.security.MessageDigest; |
| 36 | +import java.security.NoSuchAlgorithmException; |
33 | 37 | import java.util.HashMap;
|
34 | 38 | import java.util.Locale;
|
35 | 39 | import java.util.Map;
|
@@ -244,13 +248,73 @@ public String getWebsocketProxyURL() {
|
244 | 248 | mPackagerConnectionSettings.getDebugServerHost());
|
245 | 249 | }
|
246 | 250 |
|
| 251 | + private static String getSHA256(String string) { |
| 252 | + MessageDigest digest = null; |
| 253 | + try { |
| 254 | + digest = MessageDigest.getInstance("SHA-256"); |
| 255 | + } catch (NoSuchAlgorithmException e) { |
| 256 | + throw new AssertionError("Could not get standard SHA-256 algorithm", e); |
| 257 | + } |
| 258 | + digest.reset(); |
| 259 | + byte[] result; |
| 260 | + try { |
| 261 | + result = digest.digest(string.getBytes("UTF-8")); |
| 262 | + } catch (UnsupportedEncodingException e) { |
| 263 | + throw new AssertionError("This environment doesn't support UTF-8 encoding", e); |
| 264 | + } |
| 265 | + return String.format( |
| 266 | + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", |
| 267 | + result[0], |
| 268 | + result[1], |
| 269 | + result[2], |
| 270 | + result[3], |
| 271 | + result[4], |
| 272 | + result[5], |
| 273 | + result[6], |
| 274 | + result[7], |
| 275 | + result[8], |
| 276 | + result[9], |
| 277 | + result[10], |
| 278 | + result[11], |
| 279 | + result[12], |
| 280 | + result[13], |
| 281 | + result[14], |
| 282 | + result[15], |
| 283 | + result[16], |
| 284 | + result[17], |
| 285 | + result[18], |
| 286 | + result[19]); |
| 287 | + } |
| 288 | + |
| 289 | + // Returns an opaque ID which is stable for the current combination of device and app, stable |
| 290 | + // across installs, and unique across devices. |
| 291 | + private String getInspectorDeviceId() { |
| 292 | + // Every Android app has a unique application ID that looks like a Java or Kotlin package name, |
| 293 | + // such as com.example.myapp. This ID uniquely identifies your app on the device and in the |
| 294 | + // Google Play Store. |
| 295 | + // [Source: Android docs] |
| 296 | + String packageName = mPackageName; |
| 297 | + |
| 298 | + // A 64-bit number expressed as a hexadecimal string, which is either: |
| 299 | + // * unique to each combination of app-signing key, user, and device (API level >= 26), or |
| 300 | + // * randomly generated when the user first sets up the device and should remain constant for |
| 301 | + // the lifetime of the user's device (API level < 26). |
| 302 | + // [Source: Android docs] |
| 303 | + String androidId = Settings.Secure.ANDROID_ID; |
| 304 | + |
| 305 | + String rawDeviceId = String.format(Locale.US, "android-%s-%s", packageName, androidId); |
| 306 | + |
| 307 | + return getSHA256(rawDeviceId); |
| 308 | + } |
| 309 | + |
247 | 310 | private String getInspectorDeviceUrl() {
|
248 | 311 | return String.format(
|
249 | 312 | Locale.US,
|
250 |
| - "http://%s/inspector/device?name=%s&app=%s", |
| 313 | + "http://%s/inspector/device?name=%s&app=%s&device=%s", |
251 | 314 | mPackagerConnectionSettings.getInspectorServerHost(),
|
252 |
| - AndroidInfoHelpers.getFriendlyDeviceName(), |
253 |
| - mPackageName); |
| 315 | + Uri.encode(AndroidInfoHelpers.getFriendlyDeviceName()), |
| 316 | + Uri.encode(mPackageName), |
| 317 | + Uri.encode(getInspectorDeviceId())); |
254 | 318 | }
|
255 | 319 |
|
256 | 320 | public void downloadBundleFromURL(
|
@@ -425,9 +489,10 @@ public void openDebugger(final ReactContext context, final String errorMessage)
|
425 | 489 | String requestUrl =
|
426 | 490 | String.format(
|
427 | 491 | Locale.US,
|
428 |
| - "http://%s/open-debugger?appId=%s", |
| 492 | + "http://%s/open-debugger?appId=%s&device=%s", |
429 | 493 | mPackagerConnectionSettings.getInspectorServerHost(),
|
430 |
| - Uri.encode(mPackageName)); |
| 494 | + Uri.encode(mPackageName), |
| 495 | + Uri.encode(getInspectorDeviceId())); |
431 | 496 | Request request =
|
432 | 497 | new Request.Builder().url(requestUrl).method("POST", RequestBody.create(null, "")).build();
|
433 | 498 |
|
|
0 commit comments