Add getPluginByClass interface
Summary: This deprecates the `getPlugin(String)` method and introduces a `getPluginByClass(Class<T>)` instead which avoids having to `instanceof`-check and then cast the result, which provides a nicer experience for Java users. Reviewed By: jknoxville Differential Revision: D13277568 fbshipit-source-id: fb7b5b8c0180470ef0ad322559b5b7424520848b
This commit is contained in:
committed by
Facebook Github Bot
parent
606d689cae
commit
214f112c14
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2004-present, Facebook, Inc.
|
||||
* Copyright (c) Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
@@ -11,7 +11,6 @@ import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import com.facebook.flipper.android.AndroidFlipperClient;
|
||||
import com.facebook.flipper.core.FlipperClient;
|
||||
import com.facebook.flipper.core.FlipperPlugin;
|
||||
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
|
||||
import com.facebook.litho.ComponentContext;
|
||||
import com.facebook.litho.LithoView;
|
||||
@@ -26,10 +25,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
|
||||
if (client != null) {
|
||||
final FlipperPlugin samplePlugin = client.getPlugin(ExampleFlipperPlugin.ID);
|
||||
if (samplePlugin instanceof ExampleFlipperPlugin) {
|
||||
((ExampleFlipperPlugin) samplePlugin).setActivity(this);
|
||||
}
|
||||
final ExampleFlipperPlugin samplePlugin = client.getPluginByClass(ExampleFlipperPlugin.class);
|
||||
samplePlugin.setActivity(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,9 +335,10 @@ class JFlipperClient : public jni::HybridClass<JFlipperClient> {
|
||||
makeNativeMethod("getInstance", JFlipperClient::getInstance),
|
||||
makeNativeMethod("start", JFlipperClient::start),
|
||||
makeNativeMethod("stop", JFlipperClient::stop),
|
||||
makeNativeMethod("addPlugin", JFlipperClient::addPlugin),
|
||||
makeNativeMethod("removePlugin", JFlipperClient::removePlugin),
|
||||
makeNativeMethod("subscribeForUpdates", JFlipperClient::subscribeForUpdates),
|
||||
makeNativeMethod("addPluginNative", JFlipperClient::addPlugin),
|
||||
makeNativeMethod("removePluginNative", JFlipperClient::removePlugin),
|
||||
makeNativeMethod(
|
||||
"subscribeForUpdates", JFlipperClient::subscribeForUpdates),
|
||||
makeNativeMethod("unsubscribe", JFlipperClient::unsubscribe),
|
||||
makeNativeMethod("getPlugin", JFlipperClient::getPlugin),
|
||||
makeNativeMethod("getState", JFlipperClient::getState),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
* Copyright (c) Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
@@ -15,6 +15,8 @@ import com.facebook.flipper.core.StateSummary;
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.facebook.soloader.SoLoader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@DoNotStrip
|
||||
class FlipperClientImpl implements FlipperClient {
|
||||
@@ -25,6 +27,7 @@ class FlipperClientImpl implements FlipperClient {
|
||||
}
|
||||
|
||||
private final HybridData mHybridData;
|
||||
private final Map<Class<?>, String> mClassIdentifierMap = new HashMap(8);
|
||||
|
||||
private FlipperClientImpl(HybridData hd) {
|
||||
mHybridData = hd;
|
||||
@@ -44,13 +47,34 @@ class FlipperClientImpl implements FlipperClient {
|
||||
public static native FlipperClientImpl getInstance();
|
||||
|
||||
@Override
|
||||
public native void addPlugin(FlipperPlugin plugin);
|
||||
public void addPlugin(FlipperPlugin plugin) {
|
||||
mClassIdentifierMap.put(plugin.getClass(), plugin.getId());
|
||||
addPluginNative(plugin);
|
||||
}
|
||||
|
||||
public native void addPluginNative(FlipperPlugin plugin);
|
||||
|
||||
/**
|
||||
* @deprecated Prefer using {@link #getPluginByClass(Class)} over the stringly-typed interface.
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public native <T extends FlipperPlugin> T getPlugin(String id);
|
||||
|
||||
@Override
|
||||
public native void removePlugin(FlipperPlugin plugin);
|
||||
public <T extends FlipperPlugin> T getPluginByClass(Class<T> cls) {
|
||||
final String id = mClassIdentifierMap.get(cls);
|
||||
//noinspection deprecation
|
||||
return getPlugin(id);
|
||||
}
|
||||
|
||||
public native void removePluginNative(FlipperPlugin plugin);
|
||||
|
||||
@Override
|
||||
public void removePlugin(FlipperPlugin plugin) {
|
||||
mClassIdentifierMap.remove(plugin.getClass());
|
||||
removePluginNative(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public native void start();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
* Copyright (c) Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
@@ -12,6 +12,8 @@ public interface FlipperClient {
|
||||
|
||||
<T extends FlipperPlugin> T getPlugin(String id);
|
||||
|
||||
<T extends FlipperPlugin> T getPluginByClass(Class<T> cls);
|
||||
|
||||
void removePlugin(FlipperPlugin plugin);
|
||||
|
||||
void start();
|
||||
|
||||
Reference in New Issue
Block a user