From f160979fef99366b1076771fa5ec905b02909896 Mon Sep 17 00:00:00 2001 From: Shamil Yessenkulov Date: Mon, 30 Sep 2024 04:44:50 -0700 Subject: [PATCH] Creating Java FlipperMultiPlugin Summary: Following the same logic as in the diff D63095017 and adding multiplugin on the java side. The logic of the methods are the same as in the C++ functions Reviewed By: mkareta Differential Revision: D63325568 fbshipit-source-id: 855708e6fc378715403ef3f398dc2c5c72cb1af3 --- .../flipper/core/FlipperMultiPlugin.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 android/src/main/java/com/facebook/flipper/core/FlipperMultiPlugin.java diff --git a/android/src/main/java/com/facebook/flipper/core/FlipperMultiPlugin.java b/android/src/main/java/com/facebook/flipper/core/FlipperMultiPlugin.java new file mode 100644 index 00000000000..6f5088ec493 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/core/FlipperMultiPlugin.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * 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.core; + +import java.util.List; + +/** + * A FlipperMultiPlugin is an object containing several plugins with the same identifiers exposeing + * an API to the Desktop Flipper application. + */ +public class FlipperMultiPlugin implements FlipperPlugin { + private List plugins; + + public FlipperMultiPlugin(List plugins) { + this.plugins = plugins; + // Assert that all plugins have the same identifier. + String expectedIdentifier = plugins.get(0).getId(); + boolean expectedRunInBackground = plugins.get(0).runInBackground(); + for (FlipperPlugin plugin : plugins) { + assert plugin.getId().equals(expectedIdentifier); + assert plugin.runInBackground() == expectedRunInBackground; + } + } + + public void addPlugin(FlipperPlugin plugin) { + // Assert that the new plugin has the same identifier as the existing plugins. + assert plugin.getId().equals(plugins.get(0).getId()); + assert plugin.runInBackground() == plugins.get(0).runInBackground(); + plugins.add(plugin); + } + + @Override + public String getId() { + return plugins.get(0).getId(); + } + + @Override + public void onConnect(FlipperConnection conn) throws Exception { + // Forward the connection to each plugin. + for (FlipperPlugin plugin : plugins) { + plugin.onConnect(conn); + } + } + + @Override + public void onDisconnect() throws Exception { + // Forward the disconnection to each plugin. + for (FlipperPlugin plugin : plugins) { + plugin.onDisconnect(); + } + } + + @Override + public boolean runInBackground() { + // Check if any of the plugins need to run in the background. + for (FlipperPlugin plugin : plugins) { + if (plugin.runInBackground()) { + return true; + } + } + return false; + } +}