Files
flipper/docs/create-plugin.md
John Knox 9d9fa17134 Rename all The c++ JNI classes
Summary: Pull Request resolved: https://github.com/facebook/flipper/pull/272

Reviewed By: passy

Differential Revision: D9861432

Pulled By: jknoxville

fbshipit-source-id: 523b8fc28541b922bbcc0939514f4ddd0a3fc1b0
2018-09-18 07:27:28 -07:00

2.7 KiB

id, title, sidebar_label
id title sidebar_label
create-plugin Mobile Setup Mobile Setup

Implement SonarPlugin

Create a class implementing SonarPlugin.

Android

public class MySonarPlugin implements SonarPlugin {
  private SonarConnection mConnection;

  @Override
  public String getId() {
    return "MySonarPlugin";
  }

  @Override
  public void onConnect(SonarConnection connection) throws Exception {
    mConnection = connection;
  }

  @Override
  public void onDisconnect() throws Exception {
    mConnection = null;
  }
}

iOS

@interface MySonarPlugin : NSObject<SonarPlugin>
@end

@implementation MySonarPlugin

- (NSString*)identifier { return @"MySonarPlugin"; }
- (void)didConnect:(SonarConnection*)connection {}
- (void)didDisonnect {}

@end

C++

class MySonarPlugin : public SonarPlugin {
public:
  std::string identifier() const override { return "MySonarPlugin"; }
  void didConnect(std::shared_ptr<SonarConnection> conn) override;
  void didDisconnect() override;
};

Using SonarConnection

Using the SonarConnection object you can register a receiver of a desktop method call and respond with data.

Android

connection.receive("getData", new SonarReceiver() {
  @Override
  public void onReceive(SonarObject params, FlipperResponder responder) throws Exception {
    responder.success(
        new SonarObject.Builder()
            .put("data", MyData.get())
            .build());
  }
});

iOS

@interface MySonarPlugin : NSObject<SonarPlugin>
@end

@implementation MySonarPlugin

- (NSString*)identifier { return @"MySonarPlugin"; }

- (void)didConnect:(SonarConnection*)connection
{
  [connection receive:@"getData" withBlock:^(NSDictionary *params, FlipperResponder *responder) {
    [responder success:@{
      @"data":[MyData get],
    }];
  }];
}

- (void)didDisonnect {}

@end

C++

void MySonarPlugin::didConnect(std::shared_ptr<SonarConnection> conn) {
  conn->receive("getData", [](const folly::dynamic &params,
                             std::unique_ptr<FlipperResponder> responder) {
    dynamic response = folly::dynamic::object("data", getMyData());
    responder->success(response);
  });
}

Push data to the desktop

You don't have to wait for the desktop to request data though, you can also push data directly to the desktop.

Android

connection.send("MyMessage",
    new SonarObject.Builder()
        .put("message", "Hello")
        .build()

iOS

[connection send:@"getData" withParams:@{@"message":@"hello"}];

C++

void MySonarPlugin::didConnect(std::shared_ptr<SonarConnection> conn) {
  dynamic message = folly::dynamic::object("message", "hello");
  conn->send("getData", message);
}