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
This commit is contained in:
Kevin Gozali
2018-06-27 15:17:50 -07:00
committed by Facebook Github Bot
parent bc2f12c68c
commit b3ef1c3a56
6 changed files with 55 additions and 9 deletions

View File

@@ -81,6 +81,13 @@ public class DevServerHelper {
void onPackagerReloadCommand();
void onPackagerDevMenuCommand();
void onCaptureHeapCommand(final Responder responder);
// Allow apps to provide listeners for custom packager commands.
@Nullable Map<String, RequestHandler> customCommandHandlers();
}
public interface PackagerCustomCommandProvider {
}
public interface SymbolicationListener {
@@ -162,6 +169,10 @@ public class DevServerHelper {
commandListener.onCaptureHeapCommand(responder);
}
});
Map<String, RequestHandler> customHandlers = commandListener.customCommandHandlers();
if (customHandlers != null) {
handlers.putAll(customHandlers);
}
handlers.putAll(new FileIoHandler().handlers());
ConnectionCallback onPackagerConnectedCallback =

View File

@@ -11,8 +11,10 @@ import android.content.Context;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.packagerconnection.RequestHandler;
import java.lang.reflect.Constructor;
import java.util.Map;
import javax.annotation.Nullable;
@@ -41,7 +43,8 @@ public class DevSupportManagerFactory {
enableOnCreate,
null,
null,
minNumShakes);
minNumShakes,
null);
}
public static DevSupportManager create(
@@ -51,7 +54,8 @@ public class DevSupportManagerFactory {
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
if (!enableOnCreate) {
return new DisabledDevSupportManager();
}
@@ -74,7 +78,8 @@ public class DevSupportManagerFactory {
boolean.class,
RedBoxHandler.class,
DevBundleDownloadListener.class,
int.class);
int.class,
Map.class);
return (DevSupportManager) constructor.newInstance(
applicationContext,
reactInstanceManagerHelper,
@@ -82,7 +87,8 @@ public class DevSupportManagerFactory {
true,
redBoxHandler,
devBundleDownloadListener,
minNumShakes);
minNumShakes,
customPackagerCommandHandlers);
} catch (Exception e) {
throw new RuntimeException(
"Requested enabled DevSupportManager, but DevSupportManagerImpl class was not found" +

View File

@@ -60,6 +60,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -150,6 +151,8 @@ public class DevSupportManagerImpl implements
private InspectorPackagerConnection.BundleStatus mBundleStatus;
private @Nullable Map<String, RequestHandler> mCustomPackagerCommandHandlers;
private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8");
@@ -196,7 +199,8 @@ public class DevSupportManagerImpl implements
enableOnCreate,
null,
null,
minNumShakes);
minNumShakes,
null);
}
public DevSupportManagerImpl(
@@ -206,7 +210,8 @@ public class DevSupportManagerImpl implements
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes) {
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
mReactInstanceManagerHelper = reactInstanceManagerHelper;
mApplicationContext = applicationContext;
mJSAppBundleName = packagerPathForJSBundleName;
@@ -232,6 +237,8 @@ public class DevSupportManagerImpl implements
}
}, minNumShakes);
mCustomPackagerCommandHandlers = customPackagerCommandHandlers;
// Prepare reload APP broadcast receiver (will be registered/unregistered from #reload)
mReloadAppBroadcastReceiver = new BroadcastReceiver() {
@Override
@@ -841,6 +848,11 @@ public class DevSupportManagerImpl implements
});
}
@Override
public @Nullable Map<String, RequestHandler> customCommandHandlers() {
return mCustomPackagerCommandHandlers;
}
private void handleCaptureHeap(final Responder responder) {
if (mCurrentContext == null) {
return;