Add extension support to InspectorSonarPlugin

Summary:
This mirrors the extension support added to the desktop plugin and allows people to dynamically
add extension commands

Reviewed By: danielbuechele

Differential Revision: D8691167

fbshipit-source-id: 60136b960f8bbdfa42b2077d1f5f7b391fc44443
This commit is contained in:
Hilal Alsibai
2018-07-03 16:25:28 -07:00
committed by Facebook Github Bot
parent 44f561a683
commit e6fa377d75
3 changed files with 60 additions and 16 deletions

View File

@@ -38,6 +38,19 @@ public class InspectorSonarPlugin implements SonarPlugin {
private String mHighlightedId;
private TouchOverlayView mTouchOverlay;
private SonarConnection mConnection;
private @Nullable List<ExtensionCommand> mExtensionCommands;
/** An interface for extensions to the Inspector Sonar plugin */
public interface ExtensionCommand {
/** The command to respond to */
String command();
/** The corresponding SonarReceiver for the command */
SonarReceiver receiver(ObjectTracker tracker, SonarConnection connection);
}
public InspectorSonarPlugin(Context context, DescriptorMapping descriptorMapping) {
this(context, descriptorMapping, new NullScriptingEnvironment());
}
public InspectorSonarPlugin(
Context context,
@@ -46,23 +59,45 @@ public class InspectorSonarPlugin implements SonarPlugin {
this(
new ApplicationWrapper((Application) context.getApplicationContext()),
descriptorMapping,
scriptingEnvironment);
scriptingEnvironment,
null);
}
public InspectorSonarPlugin(Context context, DescriptorMapping descriptorMapping) {
this(context, descriptorMapping, new NullScriptingEnvironment());
public InspectorSonarPlugin(
Context context,
DescriptorMapping descriptorMapping,
@Nullable List<ExtensionCommand> extensions) {
this(
new ApplicationWrapper((Application) context.getApplicationContext()),
descriptorMapping,
new NullScriptingEnvironment(),
extensions);
}
public InspectorSonarPlugin(
Context context,
DescriptorMapping descriptorMapping,
ScriptingEnvironment scriptingEnvironment,
@Nullable List<ExtensionCommand> extensions) {
this(
new ApplicationWrapper((Application) context.getApplicationContext()),
descriptorMapping,
scriptingEnvironment,
extensions);
}
// Package visible for testing
InspectorSonarPlugin(
ApplicationWrapper wrapper,
DescriptorMapping descriptorMapping,
ScriptingEnvironment scriptingEnvironment) {
ScriptingEnvironment scriptingEnvironment,
@Nullable List<ExtensionCommand> extensions) {
mDescriptorMapping = descriptorMapping;
mObjectTracker = new ObjectTracker();
mApplication = wrapper;
mScriptingEnvironment = scriptingEnvironment;
mExtensionCommands = extensions;
}
@Override
@@ -91,6 +126,13 @@ public class InspectorSonarPlugin implements SonarPlugin {
connection.receive("setHighlighted", mSetHighlighted);
connection.receive("setSearchActive", mSetSearchActive);
connection.receive("getSearchResults", mGetSearchResults);
if (mExtensionCommands != null) {
for (ExtensionCommand extensionCommand : mExtensionCommands) {
connection.receive(
extensionCommand.command(), extensionCommand.receiver(mObjectTracker, mConnection));
}
}
}
@Override

View File

@@ -13,7 +13,9 @@ import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
class ObjectTracker {
public class ObjectTracker {
ObjectTracker() {}
private final Map<String, WeakReference<Object>> mObjects = new HashMap<>();
void put(String id, Object obj) {
@@ -21,7 +23,7 @@ class ObjectTracker {
}
@Nullable
Object get(String id) {
public Object get(String id) {
final WeakReference<Object> weakObj = mObjects.get(id);
if (weakObj == null) {
return null;