Skip to content

Commit

Permalink
Make IntentModule Nullsafe (#47430)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47430

Make IntentModule Nullsafe.
Fixed nullability warnings.

Changelog: [Internal]

Reviewed By: tdn120

Differential Revision: D65463226

fbshipit-source-id: 2da597e66ad170ec4a2a2949348e522bb361c209
  • Loading branch information
alanleedev authored and facebook-github-bot committed Nov 7, 2024
1 parent 2dab7b0 commit fb69f40
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
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;
Expand All @@ -28,6 +30,7 @@
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 {

Expand Down Expand Up @@ -124,7 +127,7 @@ public void onHostDestroy() {}
* @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;
Expand All @@ -149,7 +152,7 @@ public void openURL(String url, Promise promise) {
* @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;
Expand All @@ -160,8 +163,8 @@ public void canOpenURL(String url, Promise promise) {
// 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(
Expand All @@ -179,7 +182,7 @@ public void canOpenURL(String url, Promise promise) {
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);
Expand Down Expand Up @@ -219,7 +222,7 @@ public void sendIntent(String action, @Nullable ReadableArray extras, Promise pr
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 + "."));
Expand All @@ -235,6 +238,7 @@ public void sendIntent(String action, @Nullable ReadableArray extras, Promise pr
switch (type) {
case String:
{
// NULLSAFE_FIXME[Parameter Not Nullable]
intent.putExtra(name, map.getString(EXTRA_MAP_KEY_FOR_VALUE));
break;
}
Expand All @@ -244,11 +248,13 @@ public void sendIntent(String action, @Nullable ReadableArray extras, Promise pr
// 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;
}
Expand All @@ -270,8 +276,13 @@ private void sendOSIntent(Intent intent, Boolean useNewTaskFlag) {
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
Expand Down

0 comments on commit fb69f40

Please sign in to comment.