Skip to content

Commit b3ef1c3

Browse files
fkgozalifacebook-github-bot
authored andcommitted
android: allow registering custom packager command handlers
Summary: @public Apps may need to listen for custom commands via the packager connection. This allows registering such listeners. Reviewed By: raluca-elena Differential Revision: D8654477 fbshipit-source-id: 5f17298a88fec31b8939236fef48ee46c0ba2ee8
1 parent bc2f12c commit b3ef1c3

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

ReactAndroid/src/main/java/com/facebook/react/BUCK

+3
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ rn_android_library(
3535
react_native_target("java/com/facebook/react/uimanager:uimanager"),
3636
react_native_target("java/com/facebook/react/views/imagehelper:imagehelper"),
3737
],
38+
exported_deps = [
39+
react_native_target("java/com/facebook/react/packagerconnection:packagerconnection"),
40+
],
3841
)

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import com.facebook.react.modules.core.ReactChoreographer;
7979
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
8080
import com.facebook.react.modules.fabric.ReactFabric;
81+
import com.facebook.react.packagerconnection.RequestHandler;
8182
import com.facebook.react.uimanager.DisplayMetricsHolder;
8283
import com.facebook.react.uimanager.UIManagerHelper;
8384
import com.facebook.react.uimanager.ViewManager;
@@ -90,6 +91,7 @@
9091
import java.util.Collections;
9192
import java.util.HashSet;
9293
import java.util.List;
94+
import java.util.Map;
9395
import java.util.Set;
9496
import javax.annotation.Nullable;
9597

@@ -205,7 +207,8 @@ public static ReactInstanceManagerBuilder builder() {
205207
@Nullable DevBundleDownloadListener devBundleDownloadListener,
206208
int minNumShakes,
207209
int minTimeLeftInFrameForNonBatchedOperationMs,
208-
@Nullable JSIModulePackage jsiModulePackage) {
210+
@Nullable JSIModulePackage jsiModulePackage,
211+
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
209212
Log.d(ReactConstants.TAG, "ReactInstanceManager.ctor()");
210213
initializeSoLoaderIfNecessary(applicationContext);
211214

@@ -227,7 +230,8 @@ public static ReactInstanceManagerBuilder builder() {
227230
useDeveloperSupport,
228231
redBoxHandler,
229232
devBundleDownloadListener,
230-
minNumShakes);
233+
minNumShakes,
234+
customPackagerCommandHandlers);
231235
mBridgeIdleDebugListener = bridgeIdleDebugListener;
232236
mLifecycleState = initialLifecycleState;
233237
mMemoryPressureRouter = new MemoryPressureRouter(applicationContext);

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
2222
import com.facebook.react.devsupport.interfaces.DevSupportManager;
2323
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
24+
import com.facebook.react.packagerconnection.RequestHandler;
2425
import java.util.ArrayList;
2526
import java.util.List;
27+
import java.util.Map;
2628
import javax.annotation.Nullable;
2729

2830
/**
@@ -50,6 +52,7 @@ public class ReactInstanceManagerBuilder {
5052
private int mMinNumShakes = 1;
5153
private int mMinTimeLeftInFrameForNonBatchedOperationMs = -1;
5254
private @Nullable JSIModulePackage mJSIModulesPackage;
55+
private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;
5356

5457
/* package protected */ ReactInstanceManagerBuilder() {
5558
}
@@ -216,6 +219,12 @@ public ReactInstanceManagerBuilder setMinTimeLeftInFrameForNonBatchedOperationMs
216219
return this;
217220
}
218221

222+
public ReactInstanceManagerBuilder setCustomPackagerCommandHandlers(
223+
Map<String, RequestHandler> customPackagerCommandHandlers) {
224+
mCustomPackagerCommandHandlers = customPackagerCommandHandlers;
225+
return this;
226+
}
227+
219228
/**
220229
* Instantiates a new {@link ReactInstanceManager}.
221230
* Before calling {@code build}, the following must be called:
@@ -266,6 +275,7 @@ public ReactInstanceManager build() {
266275
mDevBundleDownloadListener,
267276
mMinNumShakes,
268277
mMinTimeLeftInFrameForNonBatchedOperationMs,
269-
mJSIModulesPackage);
278+
mJSIModulesPackage,
279+
mCustomPackagerCommandHandlers);
270280
}
271281
}

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public interface PackagerCommandListener {
8181
void onPackagerReloadCommand();
8282
void onPackagerDevMenuCommand();
8383
void onCaptureHeapCommand(final Responder responder);
84+
85+
// Allow apps to provide listeners for custom packager commands.
86+
@Nullable Map<String, RequestHandler> customCommandHandlers();
87+
}
88+
89+
public interface PackagerCustomCommandProvider {
90+
8491
}
8592

8693
public interface SymbolicationListener {
@@ -162,6 +169,10 @@ public void onRequest(@Nullable Object params, Responder responder) {
162169
commandListener.onCaptureHeapCommand(responder);
163170
}
164171
});
172+
Map<String, RequestHandler> customHandlers = commandListener.customCommandHandlers();
173+
if (customHandlers != null) {
174+
handlers.putAll(customHandlers);
175+
}
165176
handlers.putAll(new FileIoHandler().handlers());
166177

167178
ConnectionCallback onPackagerConnectedCallback =

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerFactory.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
1313
import com.facebook.react.devsupport.interfaces.DevSupportManager;
14+
import com.facebook.react.packagerconnection.RequestHandler;
1415

1516
import java.lang.reflect.Constructor;
17+
import java.util.Map;
1618

1719
import javax.annotation.Nullable;
1820

@@ -41,7 +43,8 @@ public static DevSupportManager create(
4143
enableOnCreate,
4244
null,
4345
null,
44-
minNumShakes);
46+
minNumShakes,
47+
null);
4548
}
4649

4750
public static DevSupportManager create(
@@ -51,7 +54,8 @@ public static DevSupportManager create(
5154
boolean enableOnCreate,
5255
@Nullable RedBoxHandler redBoxHandler,
5356
@Nullable DevBundleDownloadListener devBundleDownloadListener,
54-
int minNumShakes) {
57+
int minNumShakes,
58+
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
5559
if (!enableOnCreate) {
5660
return new DisabledDevSupportManager();
5761
}
@@ -74,15 +78,17 @@ public static DevSupportManager create(
7478
boolean.class,
7579
RedBoxHandler.class,
7680
DevBundleDownloadListener.class,
77-
int.class);
81+
int.class,
82+
Map.class);
7883
return (DevSupportManager) constructor.newInstance(
7984
applicationContext,
8085
reactInstanceManagerHelper,
8186
packagerPathForJSBundleName,
8287
true,
8388
redBoxHandler,
8489
devBundleDownloadListener,
85-
minNumShakes);
90+
minNumShakes,
91+
customPackagerCommandHandlers);
8692
} catch (Exception e) {
8793
throw new RuntimeException(
8894
"Requested enabled DevSupportManager, but DevSupportManagerImpl class was not found" +

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.LinkedHashMap;
6161
import java.util.List;
6262
import java.util.Locale;
63+
import java.util.Map;
6364
import java.util.concurrent.ExecutionException;
6465
import java.util.concurrent.TimeUnit;
6566
import java.util.concurrent.TimeoutException;
@@ -150,6 +151,8 @@ private enum ErrorType {
150151

151152
private InspectorPackagerConnection.BundleStatus mBundleStatus;
152153

154+
private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;
155+
153156
private static class JscProfileTask extends AsyncTask<String, Void, Void> {
154157
private static final MediaType JSON =
155158
MediaType.parse("application/json; charset=utf-8");
@@ -196,7 +199,8 @@ public DevSupportManagerImpl(
196199
enableOnCreate,
197200
null,
198201
null,
199-
minNumShakes);
202+
minNumShakes,
203+
null);
200204
}
201205

202206
public DevSupportManagerImpl(
@@ -206,7 +210,8 @@ public DevSupportManagerImpl(
206210
boolean enableOnCreate,
207211
@Nullable RedBoxHandler redBoxHandler,
208212
@Nullable DevBundleDownloadListener devBundleDownloadListener,
209-
int minNumShakes) {
213+
int minNumShakes,
214+
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
210215
mReactInstanceManagerHelper = reactInstanceManagerHelper;
211216
mApplicationContext = applicationContext;
212217
mJSAppBundleName = packagerPathForJSBundleName;
@@ -232,6 +237,8 @@ public void onShake() {
232237
}
233238
}, minNumShakes);
234239

240+
mCustomPackagerCommandHandlers = customPackagerCommandHandlers;
241+
235242
// Prepare reload APP broadcast receiver (will be registered/unregistered from #reload)
236243
mReloadAppBroadcastReceiver = new BroadcastReceiver() {
237244
@Override
@@ -841,6 +848,11 @@ public void run() {
841848
});
842849
}
843850

851+
@Override
852+
public @Nullable Map<String, RequestHandler> customCommandHandlers() {
853+
return mCustomPackagerCommandHandlers;
854+
}
855+
844856
private void handleCaptureHeap(final Responder responder) {
845857
if (mCurrentContext == null) {
846858
return;

0 commit comments

Comments
 (0)