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
This commit is contained in:
Anandraj Govindan 2019-12-06 20:37:26 -08:00 коммит произвёл Facebook Github Bot
Родитель 3aba90b58a
Коммит 7cfabf42b8
3 изменённых файлов: 16 добавлений и 7 удалений

Просмотреть файл

@ -7,6 +7,7 @@
package com.facebook.react.modules.datepicker; package com.facebook.react.modules.datepicker;
import android.app.Activity;
import android.app.DatePickerDialog.OnDateSetListener; import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnDismissListener;
@ -101,13 +102,16 @@ public class DatePickerDialogModule extends ReactContextBaseJavaModule {
*/ */
@ReactMethod @ReactMethod
public void open(@Nullable final ReadableMap options, Promise promise) { public void open(@Nullable final ReadableMap options, Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity(); Activity raw_activity = getCurrentActivity();
if (activity == null) { if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
promise.reject( 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; return;
} }
FragmentActivity activity = (FragmentActivity) raw_activity;
FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentManager fragmentManager = activity.getSupportFragmentManager();
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null) { if (oldFragment != null) {

Просмотреть файл

@ -234,7 +234,7 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
*/ */
private @Nullable FragmentManagerHelper getFragmentManagerHelper() { private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
Activity activity = getCurrentActivity(); Activity activity = getCurrentActivity();
if (activity == null) { if (activity == null || !(activity instanceof FragmentActivity)) {
return null; return null;
} }
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager()); return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());

Просмотреть файл

@ -7,6 +7,7 @@
package com.facebook.react.modules.timepicker; package com.facebook.react.modules.timepicker;
import android.app.Activity;
import android.app.TimePickerDialog.OnTimeSetListener; import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnDismissListener;
@ -89,12 +90,16 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
@ReactMethod @ReactMethod
public void open(@Nullable final ReadableMap options, Promise promise) { public void open(@Nullable final ReadableMap options, Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity(); Activity raw_activity = getCurrentActivity();
if (activity == null) { if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
promise.reject( 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; return;
} }
FragmentActivity activity = (FragmentActivity) raw_activity;
// We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity // 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. // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentManager fragmentManager = activity.getSupportFragmentManager();