|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * <p>This source code is licensed under the MIT license found in the LICENSE file in the root |
| 5 | + * directory of this source tree. |
| 6 | + */ |
| 7 | +package com.example; |
| 8 | + |
| 9 | +import android.content.Context; |
| 10 | +import com.facebook.flipper.android.AndroidFlipperClient; |
| 11 | +import com.facebook.flipper.android.utils.FlipperUtils; |
| 12 | +import com.facebook.flipper.core.FlipperClient; |
| 13 | +import com.facebook.flipper.plugins.crashreporter.CrashReporterPlugin; |
| 14 | +import com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin; |
| 15 | +import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin; |
| 16 | +import com.facebook.flipper.plugins.inspector.DescriptorMapping; |
| 17 | +import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin; |
| 18 | +import com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor; |
| 19 | +import com.facebook.flipper.plugins.network.NetworkFlipperPlugin; |
| 20 | +import com.facebook.flipper.plugins.react.ReactFlipperPlugin; |
| 21 | +import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin; |
| 22 | +import com.facebook.react.ReactInstanceManager; |
| 23 | +import com.facebook.react.bridge.ReactContext; |
| 24 | +import com.facebook.react.modules.network.NetworkingModule; |
| 25 | +import okhttp3.OkHttpClient; |
| 26 | + |
| 27 | +public class ReactNativeFlipper { |
| 28 | + public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) { |
| 29 | + if (FlipperUtils.shouldEnableFlipper(context)) { |
| 30 | + final FlipperClient client = AndroidFlipperClient.getInstance(context); |
| 31 | + |
| 32 | + client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults())); |
| 33 | + client.addPlugin(new ReactFlipperPlugin()); |
| 34 | + client.addPlugin(new DatabasesFlipperPlugin(context)); |
| 35 | + client.addPlugin(new SharedPreferencesFlipperPlugin(context)); |
| 36 | + client.addPlugin(CrashReporterPlugin.getInstance()); |
| 37 | + |
| 38 | + NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin(); |
| 39 | + NetworkingModule.setCustomClientBuilder( |
| 40 | + new NetworkingModule.CustomClientBuilder() { |
| 41 | + @Override |
| 42 | + public void apply(OkHttpClient.Builder builder) { |
| 43 | + builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin)); |
| 44 | + } |
| 45 | + }); |
| 46 | + client.addPlugin(networkFlipperPlugin); |
| 47 | + client.start(); |
| 48 | + |
| 49 | + // Fresco Plugin needs to ensure that ImagePipelineFactory is initialized |
| 50 | + // Hence we run if after all native modules have been initialized |
| 51 | + ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); |
| 52 | + if (reactContext == null) { |
| 53 | + reactInstanceManager.addReactInstanceEventListener( |
| 54 | + new ReactInstanceManager.ReactInstanceEventListener() { |
| 55 | + @Override |
| 56 | + public void onReactContextInitialized(ReactContext reactContext) { |
| 57 | + reactInstanceManager.removeReactInstanceEventListener(this); |
| 58 | + reactContext.runOnNativeModulesQueueThread( |
| 59 | + new Runnable() { |
| 60 | + @Override |
| 61 | + public void run() { |
| 62 | + client.addPlugin(new FrescoFlipperPlugin()); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
| 67 | + } else { |
| 68 | + client.addPlugin(new FrescoFlipperPlugin()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments