From 6598292edeee26a92c369080e6286713b4ffd96e Mon Sep 17 00:00:00 2001 From: Emily Janzer Date: Tue, 5 Nov 2019 18:58:33 -0800 Subject: [PATCH] Add a field + getter for instanceKey to ReactContext Summary: Adding a new String field for `instanceKey` to ReactContext, which is set via a new constructor on ReactApplicationContext. Also adding getters to ReactContext and ThemedReactContext so that it's accessible from any instance/subclass of ReactContext. This will only be used in bridgeless mode. Reviewed By: mdvacca Differential Revision: D18316556 fbshipit-source-id: 9757da72fde4ba36034c1e129326461fed496229 --- .../react/bridge/ReactApplicationContext.java | 11 ++++++++++ .../facebook/react/bridge/ReactContext.java | 21 +++++++++++++++++++ .../react/uimanager/ThemedReactContext.java | 6 ++++++ 3 files changed, 38 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactApplicationContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactApplicationContext.java index 9fa01ba5f89..86e90392d88 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactApplicationContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactApplicationContext.java @@ -20,4 +20,15 @@ public class ReactApplicationContext extends ReactContext { public ReactApplicationContext(Context context) { super(context.getApplicationContext()); } + + /** + * A constructor that takes a unique string identifier for the React instance. For bridgeless mode + * only - do not use. + * + *

TODO T43898341 Make this package-private once we've consolidated the classes that need this + * in this package + */ + public ReactApplicationContext(Context context, String instanceKey) { + super(context.getApplicationContext(), instanceKey); + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index b3db3af93d2..11428cba257 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -53,9 +53,20 @@ public class ReactContext extends ContextWrapper { private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler; private @Nullable NativeModuleCallExceptionHandler mExceptionHandlerWrapper; private @Nullable WeakReference mCurrentActivity; + private final @Nullable String mInstanceKey; public ReactContext(Context base) { super(base); + mInstanceKey = null; + } + + /** + * A constructor that takes a unique string identifier for the React instance. For bridgeless mode + * only - do not use. + */ + /* package */ ReactContext(Context base, String instanceKey) { + super(base); + mInstanceKey = instanceKey; } /** Set and initialize CatalystInstance for this Context. This should be called exactly once. */ @@ -408,4 +419,14 @@ public class ReactContext extends ContextWrapper { public JavaScriptContextHolder getJavaScriptContextHolder() { return mCatalystInstance.getJavaScriptContextHolder(); } + + /** + * TODO T43898341 Make this package-private once we've consolidated the classes that need this in + * this package + * + * @return The key for the associated React instance + */ + public @Nullable String getInstanceKey() { + return mInstanceKey; + } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java index af7c8748af0..91d6cdc59be 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java @@ -53,4 +53,10 @@ public class ThemedReactContext extends ReactContext { public @Nullable Activity getCurrentActivity() { return mReactApplicationContext.getCurrentActivity(); } + + @Nullable + @Override + public String getInstanceKey() { + return mReactApplicationContext.getInstanceKey(); + } }