Correctly deprecate ReactContextBaseJavaModule.getCurrentActivity() method

Summary:
Users should not be using `getCurrentActivity()` method on `ReactContextBaseJavaModule`.
Instead they should use `getReactApplicationContext.getCurrentActivity()`

This correctly deprecates this method for 0.80.x

Changelog:
[Android] [Deprecated] - Correctly deprecate ReactContextBaseJavaModule.getCurrentActivity() method

Reviewed By: mdvacca

Differential Revision: D72449062

fbshipit-source-id: f74bf94980447497e7e9049ede2b44c1d1f0d48b
This commit is contained in:
Nicola Corti
2025-04-08 17:34:59 -07:00
committed by Facebook GitHub Bot
parent f107c28d2f
commit 1408c69fd8
7 changed files with 22 additions and 24 deletions
@@ -9,15 +9,10 @@ package com.facebook.react.bridge;
import android.app.Activity;
import androidx.annotation.Nullable;
import com.facebook.react.common.annotations.DeprecatedInNewArchitecture;
/**
* Base class for Catalyst native modules that require access to the {@link ReactContext} instance.
*/
@DeprecatedInNewArchitecture(
message =
"ReactContextBaseJavaModule will be deprecated in new Architecture of React Native, use"
+ " BaseJavaModule instead")
public abstract class ReactContextBaseJavaModule extends BaseJavaModule {
public ReactContextBaseJavaModule() {
@@ -37,9 +32,10 @@ public abstract class ReactContextBaseJavaModule extends BaseJavaModule {
* <p>For example, never store the value returned by this method in a member variable. Instead,
* call this method whenever you actually need the Activity and make sure to check for {@code
* null}.
*
* @deprecated "Use 'getReactApplicationContext.getCurrentActivity() instead."
*/
@DeprecatedInNewArchitecture(
message = "Use 'getReactApplicationContext.getCurrentActivity() instead.")
@Deprecated(forRemoval = true, since = "0.80.0")
protected @Nullable final Activity getCurrentActivity() {
return getReactApplicationContext().getCurrentActivity();
}
@@ -54,8 +54,8 @@ constructor(
// Attempt to use the Activity context first in order to get the most up to date
// scheme. This covers the scenario when AppCompatDelegate.setDefaultNightMode()
// is called directly (which can occur in Brownfield apps for example).
val activity = getCurrentActivity()
return colorSchemeForCurrentConfiguration(activity ?: getReactApplicationContext())
val activity = reactApplicationContext.getCurrentActivity()
return colorSchemeForCurrentConfiguration(activity ?: reactApplicationContext)
}
public override fun setColorScheme(style: String) {
@@ -230,7 +230,7 @@ public class DialogModule extends NativeDialogManagerAndroidSpec implements Life
* MEMORY LEAKS.
*/
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
Activity activity = getCurrentActivity();
Activity activity = getReactApplicationContext().getCurrentActivity();
if (activity == null || !(activity instanceof FragmentActivity)) {
return null;
}
@@ -47,7 +47,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
*/
override fun getInitialURL(promise: Promise) {
try {
val currentActivity = getCurrentActivity()
val currentActivity = getReactApplicationContext().getCurrentActivity()
if (currentActivity == null) {
waitForActivityAndGetInitialURL(promise)
return
@@ -159,7 +159,8 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
override fun openSettings(promise: Promise) {
try {
val intent = Intent()
val currentActivity: Activity = checkNotNull(getCurrentActivity())
val currentActivity: Activity =
checkNotNull(getReactApplicationContext().getCurrentActivity())
val selfPackageName = getReactApplicationContext().getPackageName()
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
@@ -243,7 +244,7 @@ public open class IntentModule(reactContext: ReactApplicationContext) :
}
private fun sendOSIntent(intent: Intent, useNewTaskFlag: Boolean) {
val currentActivity = getCurrentActivity()
val currentActivity = getReactApplicationContext().getCurrentActivity()
val selfPackageName = getReactApplicationContext().getPackageName()
val packageManager = getReactApplicationContext().getPackageManager()
@@ -175,7 +175,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) :
private val permissionAwareActivity: PermissionAwareActivity
get() {
val activity = getCurrentActivity()
val activity = reactApplicationContext.getCurrentActivity()
checkNotNull(activity) { "Tried to use permissions API while not attached to an Activity." }
check(activity is PermissionAwareActivity) {
("Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity.")
@@ -44,7 +44,7 @@ public class ShareModule(reactContext: ReactApplicationContext) :
}
val chooser = Intent.createChooser(intent, dialogTitle)
chooser.addCategory(Intent.CATEGORY_DEFAULT)
val currentActivity = getCurrentActivity()
val currentActivity = reactApplicationContext.getCurrentActivity()
if (currentActivity != null) {
currentActivity.startActivity(chooser)
} else {
@@ -35,7 +35,7 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
@Suppress("DEPRECATION")
override fun getTypedExportedConstants(): Map<String, Any> {
val statusBarColor =
currentActivity?.window?.statusBarColor?.let { color ->
reactApplicationContext.getCurrentActivity()?.window?.statusBarColor?.let { color ->
String.format("#%06X", 0xFFFFFF and color)
} ?: "black"
return mapOf(
@@ -44,10 +44,13 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
)
}
@Suppress("DEPRECATION")
private fun getStatusBarHeightPx(): Float {
val windowInsets =
currentActivity?.window?.decorView?.let(ViewCompat::getRootWindowInsets) ?: return 0f
reactApplicationContext
.getCurrentActivity()
?.window
?.decorView
?.let(ViewCompat::getRootWindowInsets) ?: return 0f
return windowInsets
.getInsets(
WindowInsetsCompat.Type.statusBars() or
@@ -60,7 +63,7 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
@Suppress("DEPRECATION")
override fun setColor(colorDouble: Double, animated: Boolean) {
val color = colorDouble.toInt()
val activity = currentActivity
val activity = reactApplicationContext.getCurrentActivity()
if (activity == null) {
FLog.w(
ReactConstants.TAG,
@@ -87,9 +90,8 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
})
}
@Suppress("DEPRECATION")
override fun setTranslucent(translucent: Boolean) {
val activity = currentActivity
val activity = reactApplicationContext.getCurrentActivity()
if (activity == null) {
FLog.w(
ReactConstants.TAG,
@@ -104,9 +106,8 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
})
}
@Suppress("DEPRECATION")
override fun setHidden(hidden: Boolean) {
val activity = currentActivity
val activity = reactApplicationContext.getCurrentActivity()
if (activity == null) {
FLog.w(
ReactConstants.TAG,
@@ -118,7 +119,7 @@ public class StatusBarModule(reactContext: ReactApplicationContext?) :
@Suppress("DEPRECATION")
override fun setStyle(style: String?) {
val activity = currentActivity
val activity = reactApplicationContext.getCurrentActivity()
if (activity == null) {
FLog.w(
ReactConstants.TAG,