|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +@file:Suppress("DEPRECATION") |
| 9 | + |
| 10 | +package com.facebook.react.uimanager |
| 11 | + |
| 12 | +import android.app.Activity |
| 13 | +import android.content.Context |
| 14 | +import com.facebook.react.bridge.Callback |
| 15 | +import com.facebook.react.bridge.CatalystInstance |
| 16 | +import com.facebook.react.bridge.JavaScriptContextHolder |
| 17 | +import com.facebook.react.bridge.JavaScriptModule |
| 18 | +import com.facebook.react.bridge.LifecycleEventListener |
| 19 | +import com.facebook.react.bridge.NativeModule |
| 20 | +import com.facebook.react.bridge.ReactApplicationContext |
| 21 | +import com.facebook.react.bridge.ReactContext |
| 22 | +import com.facebook.react.bridge.UIManager |
| 23 | +import com.facebook.react.common.annotations.internal.LegacyArchitecture |
| 24 | +import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder |
| 25 | + |
| 26 | +/** |
| 27 | + * Wraps [ReactContext] with the base [Context] passed into the constructor. It provides also a way |
| 28 | + * to start activities using the viewContext to which RN native views belong. It delegates lifecycle |
| 29 | + * listener registration to the original instance of [ReactContext] which is supposed to receive the |
| 30 | + * lifecycle events. At the same time we disallow receiving lifecycle events for this wrapper |
| 31 | + * instances. TODO: T7538544 Rename ThemedReactContext to be in alignment with name of |
| 32 | + * ReactApplicationContext |
| 33 | + * |
| 34 | + * @property moduleName A [String] that represents the module name of the js application that is |
| 35 | + * being rendered with this [ThemedReactContext] |
| 36 | + */ |
| 37 | +public class ThemedReactContext( |
| 38 | + public val reactApplicationContext: ReactApplicationContext, |
| 39 | + base: Context, |
| 40 | + public val moduleName: String?, |
| 41 | + public val surfaceId: Int |
| 42 | +) : ReactContext(base) { |
| 43 | + |
| 44 | + @Deprecated("This constructor is deprecated and you should not be using it.") |
| 45 | + public constructor( |
| 46 | + reactApplicationContext: ReactApplicationContext, |
| 47 | + base: Context, |
| 48 | + moduleName: String? = null |
| 49 | + ) : this(reactApplicationContext, base, moduleName, -1) |
| 50 | + |
| 51 | + @Deprecated("This constructor is deprecated and you should not be using it.") |
| 52 | + public constructor( |
| 53 | + reactApplicationContext: ReactApplicationContext, |
| 54 | + base: Context |
| 55 | + ) : this(reactApplicationContext, base, null, -1) |
| 56 | + |
| 57 | + init { |
| 58 | + initializeFromOther(reactApplicationContext) |
| 59 | + } |
| 60 | + |
| 61 | + override fun addLifecycleEventListener(listener: LifecycleEventListener) { |
| 62 | + reactApplicationContext.addLifecycleEventListener(listener) |
| 63 | + } |
| 64 | + |
| 65 | + override fun removeLifecycleEventListener(listener: LifecycleEventListener) { |
| 66 | + reactApplicationContext.removeLifecycleEventListener(listener) |
| 67 | + } |
| 68 | + |
| 69 | + override fun hasCurrentActivity(): Boolean = reactApplicationContext.hasCurrentActivity() |
| 70 | + |
| 71 | + override fun getCurrentActivity(): Activity? = reactApplicationContext.getCurrentActivity() |
| 72 | + |
| 73 | + override fun <T : JavaScriptModule> getJSModule(jsInterface: Class<T>): T = |
| 74 | + reactApplicationContext.getJSModule<T>(jsInterface) |
| 75 | + |
| 76 | + override fun <T : NativeModule> hasNativeModule(nativeModuleInterface: Class<T>): Boolean = |
| 77 | + reactApplicationContext.hasNativeModule<T>(nativeModuleInterface) |
| 78 | + |
| 79 | + override fun getNativeModules(): MutableCollection<NativeModule?>? = |
| 80 | + reactApplicationContext.getNativeModules() |
| 81 | + |
| 82 | + override fun <T : NativeModule> getNativeModule(nativeModuleInterface: Class<T>): T? = |
| 83 | + reactApplicationContext.getNativeModule<T>(nativeModuleInterface) |
| 84 | + |
| 85 | + override fun getNativeModule(moduleName: String): NativeModule? = |
| 86 | + reactApplicationContext.getNativeModule(moduleName) |
| 87 | + |
| 88 | + @Deprecated( |
| 89 | + "This method is deprecated and will be removed once the Legacy Architecture is removed") |
| 90 | + @LegacyArchitecture |
| 91 | + override fun getCatalystInstance(): CatalystInstance? = |
| 92 | + reactApplicationContext.getCatalystInstance() |
| 93 | + |
| 94 | + @Deprecated( |
| 95 | + "This API has been deprecated due to naming consideration, please use hasActiveReactInstance() instead", |
| 96 | + ReplaceWith("hasActiveReactInstance()")) |
| 97 | + @LegacyArchitecture |
| 98 | + override fun hasActiveCatalystInstance(): Boolean = |
| 99 | + reactApplicationContext.hasActiveCatalystInstance() |
| 100 | + |
| 101 | + override fun hasActiveReactInstance(): Boolean = |
| 102 | + reactApplicationContext.hasActiveCatalystInstance() |
| 103 | + |
| 104 | + @Deprecated( |
| 105 | + "This API has been deprecated due to naming consideration, please use hasReactInstance() instead", |
| 106 | + ReplaceWith("hasReactInstance()")) |
| 107 | + @LegacyArchitecture |
| 108 | + override fun hasCatalystInstance(): Boolean = reactApplicationContext.hasCatalystInstance() |
| 109 | + |
| 110 | + override fun hasReactInstance(): Boolean = reactApplicationContext.hasReactInstance() |
| 111 | + |
| 112 | + override fun destroy() { |
| 113 | + reactApplicationContext.destroy() |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * This is misnamed but has some uses out in the wild. It will be deleted in a future release of |
| 118 | + * RN. |
| 119 | + * |
| 120 | + * @return a [String] that represents the module name of the js application that is being rendered |
| 121 | + * with this [ThemedReactContext] |
| 122 | + */ |
| 123 | + @Deprecated( |
| 124 | + "Do not depend on this method. It will be removed in a future release of React Native.") |
| 125 | + public fun getSurfaceID(): String? = moduleName |
| 126 | + |
| 127 | + override fun handleException(e: Exception?) { |
| 128 | + reactApplicationContext.handleException(e) |
| 129 | + } |
| 130 | + |
| 131 | + @Deprecated( |
| 132 | + "You should not invoke isBridgeless and let your code depend on this check. This function will be removed in the future.") |
| 133 | + override fun isBridgeless(): Boolean = reactApplicationContext.isBridgeless() |
| 134 | + |
| 135 | + override fun getJavaScriptContextHolder(): JavaScriptContextHolder? = |
| 136 | + reactApplicationContext.getJavaScriptContextHolder() |
| 137 | + |
| 138 | + override fun getJSCallInvokerHolder(): CallInvokerHolder? = |
| 139 | + reactApplicationContext.getJSCallInvokerHolder() |
| 140 | + |
| 141 | + @Deprecated( |
| 142 | + "This method is deprecated, please use UIManagerHelper.getUIManager() instead.", |
| 143 | + ReplaceWith("UIManagerHelper.getUIManager()")) |
| 144 | + override fun getFabricUIManager(): UIManager? = reactApplicationContext.getFabricUIManager() |
| 145 | + |
| 146 | + override fun getSourceURL(): String? = reactApplicationContext.getSourceURL() |
| 147 | + |
| 148 | + override fun registerSegment(segmentId: Int, path: String?, callback: Callback?) { |
| 149 | + reactApplicationContext.registerSegment(segmentId, path, callback) |
| 150 | + } |
| 151 | +} |
0 commit comments