Add sample js and android plugin

Summary:
The start of an example plugin.

My intention is for this to be a place that we keep up to date with the current best practice for doing things.

For example, with the introduction on persistedStateReducer, there are two ways to receive incoming messages, but only one of them works in the background. This should act as a guideline.

For this reason, don't hold back on reviewing it. I want it to be 👌

Reviewed By: priteshrnandgaonkar

Differential Revision: D10448592

fbshipit-source-id: d5fa978c14e47a7fa3c9a29d0929d5a6109267af
This commit is contained in:
John Knox
2018-10-19 09:42:00 -07:00
committed by Facebook Github Bot
parent 881d066369
commit 6cc7f60cde
8 changed files with 283 additions and 6 deletions

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*
*/
package com.facebook.flipper.plugins.example;
import android.app.Activity;
import android.widget.Toast;
import com.facebook.flipper.core.FlipperConnection;
import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperReceiver;
import com.facebook.flipper.core.FlipperResponder;
public class ExampleFlipperPlugin implements FlipperPlugin {
public static final String ID = "Example";
private Activity mActivity;
private FlipperConnection mConnection;
private int mNotificationsSent = 0;
@Override
public String getId() {
return ID;
}
/*
* Activity to be used to display incoming messages
*/
public void setActivity(Activity activity) {
mActivity = activity;
}
@Override
public void onConnect(FlipperConnection connection) throws Exception {
mConnection = connection;
connection.receive(
"displayMessage",
new FlipperReceiver() {
@Override
public void onReceive(final FlipperObject params, FlipperResponder responder)
throws Exception {
if (mActivity != null) {
mActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, params.getString("message"), Toast.LENGTH_SHORT)
.show();
}
});
}
responder.success(new FlipperObject.Builder().put("greeting", "Hello").build());
}
});
}
public void triggerNotification() {
if (mConnection != null) {
mConnection.send(
"triggerNotification", new FlipperObject.Builder().put("id", mNotificationsSent).build());
mNotificationsSent++;
}
}
@Override
public void onDisconnect() throws Exception {
mConnection = null;
}
@Override
public boolean runInBackground() {
return true;
}
}