From 7cfabf42b816de758d8e52896bbab0c50e3a802a Mon Sep 17 00:00:00 2001 From: Anandraj Govindan Date: Fri, 6 Dec 2019 20:37:26 -0800 Subject: [PATCH] Avoid throwing exceptions when the host activity is not FragmentActivity (#27425) Summary: There are some code in native modules which currently throws exception when the host activity is not a FragmentActivity, even if the native module is not used. This change avoids the throw and fail the promise instead. ## Changelog There are some code in native modules which currently throws exception when the host activity is not a FragmentActivity, even if the native module is not used. This change avoids the throw and fail the promise instead. [CATEGORY] [TYPE] - Message Pull Request resolved: https://github.com/facebook/react-native/pull/27425 Test Plan: We've tested the change on MS Office applications, which currently don't use FragmentActivity. Differential Revision: D18873012 Pulled By: mdvacca fbshipit-source-id: 1b7c9efba5a59b2051487510da9ef7e1232877a5 --- .../modules/datepicker/DatePickerDialogModule.java | 10 +++++++--- .../facebook/react/modules/dialog/DialogModule.java | 2 +- .../modules/timepicker/TimePickerDialogModule.java | 11 ++++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java index 026e752a58d..d44c4f8b9be 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java @@ -7,6 +7,7 @@ package com.facebook.react.modules.datepicker; +import android.app.Activity; import android.app.DatePickerDialog.OnDateSetListener; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; @@ -101,13 +102,16 @@ public class DatePickerDialogModule extends ReactContextBaseJavaModule { */ @ReactMethod public void open(@Nullable final ReadableMap options, Promise promise) { - FragmentActivity activity = (FragmentActivity) getCurrentActivity(); - if (activity == null) { + Activity raw_activity = getCurrentActivity(); + if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) { promise.reject( - ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity"); + ERROR_NO_ACTIVITY, + "Tried to open a DatePicker dialog while not attached to a FragmentActivity"); return; } + FragmentActivity activity = (FragmentActivity) raw_activity; + FragmentManager fragmentManager = activity.getSupportFragmentManager(); DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); if (oldFragment != null) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java index 6dea0d63740..437336f0ab0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java @@ -234,7 +234,7 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl */ private @Nullable FragmentManagerHelper getFragmentManagerHelper() { Activity activity = getCurrentActivity(); - if (activity == null) { + if (activity == null || !(activity instanceof FragmentActivity)) { return null; } return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager()); diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/timepicker/TimePickerDialogModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/timepicker/TimePickerDialogModule.java index f0c55360ba5..412cf5d369a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/timepicker/TimePickerDialogModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/timepicker/TimePickerDialogModule.java @@ -7,6 +7,7 @@ package com.facebook.react.modules.timepicker; +import android.app.Activity; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; @@ -89,12 +90,16 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule { @ReactMethod public void open(@Nullable final ReadableMap options, Promise promise) { - FragmentActivity activity = (FragmentActivity) getCurrentActivity(); - if (activity == null) { + Activity raw_activity = getCurrentActivity(); + if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) { promise.reject( - ERROR_NO_ACTIVITY, "Tried to open a TimePicker dialog while not attached to an Activity"); + ERROR_NO_ACTIVITY, + "Tried to open a DatePicker dialog while not attached to a FragmentActivity"); return; } + + FragmentActivity activity = (FragmentActivity) raw_activity; + // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication. FragmentManager fragmentManager = activity.getSupportFragmentManager();