Skip to content

Commit

Permalink
feat(native): add expo compatibility for android
Browse files Browse the repository at this point in the history
  • Loading branch information
Almouro committed Jun 15, 2022
1 parent 3fb71ca commit d2ed444
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ import tech.bam.rnperformance.flipper.RNPerfMonitorPlugin;

...

client.addPlugin(new RNPerfMonitorPlugin(reactInstanceManager));
client.addPlugin(new RNPerfMonitorPlugin());
```

#### Migrating from flipper-plugin-rn-performance-android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,46 @@
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;

import com.facebook.flipper.android.AndroidFlipperClient;
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;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactApplicationContext;

public class RNPerfMonitorPlugin implements FlipperPlugin {
private FlipperConnection connection;
private ReactInstanceManager reactInstanceManager;
private FPSMonitor fpsMonitor;
private ReactApplicationContext reactContext;

public static void passReactApplicationContext(@NonNull ReactApplicationContext reactContext) {
final RNPerfMonitorPlugin plugin = AndroidFlipperClient.getInstance(reactContext.getBaseContext())
.getPluginByClass(RNPerfMonitorPlugin.class);

if (plugin != null) {
plugin.setReactContext(reactContext);
} else {
Log.d(RNPerfMonitorPlugin.class.getName(), "Flipper plugin isn't installed");
}
}

// Keeping this constructor for retrocompatibility
@Deprecated
public RNPerfMonitorPlugin(ReactInstanceManager reactInstanceManager) {
this.reactInstanceManager = reactInstanceManager;
fpsMonitor = new FPSMonitor();
}

public RNPerfMonitorPlugin() {
fpsMonitor = new FPSMonitor();
}

public void setReactContext(@NonNull ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

@Override
public String getId() {
Expand Down Expand Up @@ -61,7 +84,7 @@ public void startMeasuring() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
fpsMonitor.start(reactInstanceManager.getCurrentReactContext(), new MonitorCallback() {
fpsMonitor.start(reactContext, new MonitorCallback() {
@Override
public void setCurrentFPS(int uiFrames, int jsFrames, int timeInMs) {
if (connection == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -17,6 +18,11 @@ public class FlipperPerformancePluginPackage implements ReactPackage {
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new FlipperPerformancePluginModule(reactContext));

if (BuildConfig.DEBUG) {
passReactContextToFlipperPlugin(reactContext);
}

return modules;
}

Expand All @@ -25,4 +31,20 @@ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext r
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}

private void passReactContextToFlipperPlugin(@NonNull ReactApplicationContext reactContext) {
try {
/*
We use reflection since Flipper library is not available in release mode
We pass React context here, for compatibility with expo apps
See https://github.com/expo/expo/issues/17852
*/
Class<?> aClass = Class.forName("tech.bam.rnperformance.flipper.RNPerfMonitorPlugin");
aClass
.getMethod("passReactApplicationContext", ReactApplicationContext.class)
.invoke(null, reactContext);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}

0 comments on commit d2ed444

Please sign in to comment.