From fb69f406dfa77fd267bb71957ed9dff1e912c253 Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Thu, 7 Nov 2024 01:03:33 -0800 Subject: [PATCH] Make IntentModule Nullsafe (#47430) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47430 Make IntentModule Nullsafe. Fixed nullability warnings. Changelog: [Internal] Reviewed By: tdn120 Differential Revision: D65463226 fbshipit-source-id: 2da597e66ad170ec4a2a2949348e522bb361c209 --- .../react/modules/intent/IntentModule.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java index 4324495b07a..c471c56af3e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java @@ -15,7 +15,9 @@ import android.net.Uri; import android.nfc.NfcAdapter; import android.provider.Settings; import androidx.annotation.Nullable; +import androidx.core.util.Preconditions; import com.facebook.fbreact.specs.NativeIntentAndroidSpec; +import com.facebook.infer.annotation.Nullsafe; import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.Promise; @@ -28,6 +30,7 @@ import java.util.ArrayList; import java.util.List; /** Intent module. Launch other activities or open URLs. */ +@Nullsafe(Nullsafe.Mode.LOCAL) @ReactModule(name = NativeIntentAndroidSpec.NAME) public class IntentModule extends NativeIntentAndroidSpec { @@ -124,7 +127,7 @@ public class IntentModule extends NativeIntentAndroidSpec { * @param url the URL to open */ @Override - public void openURL(String url, Promise promise) { + public void openURL(@Nullable String url, Promise promise) { if (url == null || url.isEmpty()) { promise.reject(new JSApplicationIllegalArgumentException("Invalid URL: " + url)); return; @@ -149,7 +152,7 @@ public class IntentModule extends NativeIntentAndroidSpec { * @param promise a promise that is always resolved with a boolean argument */ @Override - public void canOpenURL(String url, Promise promise) { + public void canOpenURL(@Nullable String url, Promise promise) { if (url == null || url.isEmpty()) { promise.reject(new JSApplicationIllegalArgumentException("Invalid URL: " + url)); return; @@ -160,8 +163,8 @@ public class IntentModule extends NativeIntentAndroidSpec { // We need Intent.FLAG_ACTIVITY_NEW_TASK since getReactApplicationContext() returns // the ApplicationContext instead of the Activity context. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - boolean canOpen = - intent.resolveActivity(getReactApplicationContext().getPackageManager()) != null; + PackageManager packageManager = getReactApplicationContext().getPackageManager(); + boolean canOpen = packageManager != null && intent.resolveActivity(packageManager) != null; promise.resolve(canOpen); } catch (Exception e) { promise.reject( @@ -179,7 +182,7 @@ public class IntentModule extends NativeIntentAndroidSpec { public void openSettings(Promise promise) { try { Intent intent = new Intent(); - Activity currentActivity = getCurrentActivity(); + Activity currentActivity = Preconditions.checkNotNull(getCurrentActivity()); String selfPackageName = getReactApplicationContext().getPackageName(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); @@ -219,7 +222,7 @@ public class IntentModule extends NativeIntentAndroidSpec { Intent intent = new Intent(action); PackageManager packageManager = getReactApplicationContext().getPackageManager(); - if (intent.resolveActivity(packageManager) == null) { + if (packageManager == null || intent.resolveActivity(packageManager) == null) { promise.reject( new JSApplicationIllegalArgumentException( "Could not launch Intent with action " + action + ".")); @@ -235,6 +238,7 @@ public class IntentModule extends NativeIntentAndroidSpec { switch (type) { case String: { + // NULLSAFE_FIXME[Parameter Not Nullable] intent.putExtra(name, map.getString(EXTRA_MAP_KEY_FOR_VALUE)); break; } @@ -244,11 +248,13 @@ public class IntentModule extends NativeIntentAndroidSpec { // See: https://github.com/facebook/react-native/issues/4141 // We might need to find a workaround if this is really an issue Double number = map.getDouble(EXTRA_MAP_KEY_FOR_VALUE); + // NULLSAFE_FIXME[Parameter Not Nullable] intent.putExtra(name, number); break; } case Boolean: { + // NULLSAFE_FIXME[Parameter Not Nullable] intent.putExtra(name, map.getBoolean(EXTRA_MAP_KEY_FOR_VALUE)); break; } @@ -270,8 +276,13 @@ public class IntentModule extends NativeIntentAndroidSpec { Activity currentActivity = getCurrentActivity(); String selfPackageName = getReactApplicationContext().getPackageName(); - ComponentName componentName = - intent.resolveActivity(getReactApplicationContext().getPackageManager()); + PackageManager packageManager = getReactApplicationContext().getPackageManager(); + ComponentName componentName = null; + if (packageManager == null) { + componentName = intent.getComponent(); + } else { + componentName = intent.resolveActivity(packageManager); + } String otherPackageName = (componentName != null ? componentName.getPackageName() : ""); // If there is no currentActivity or we are launching to a different package we need to set