Skip to content

Commit

Permalink
Avoid throwing exceptions when the host activity is not FragmentActiv…
Browse files Browse the repository at this point in the history
…ity (#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: #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
  • Loading branch information
mganandraj authored and facebook-github-bot committed Dec 7, 2019
1 parent 3aba90b commit 7cfabf4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,13 +102,16 @@ public void onDismiss(DialogInterface dialog) {
*/
@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void run() {
*/
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
Activity activity = getCurrentActivity();
if (activity == null) {
if (activity == null || !(activity instanceof FragmentActivity)) {
return null;
}
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,12 +90,16 @@ public void onDismiss(DialogInterface dialog) {
@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();
Expand Down

0 comments on commit 7cfabf4

Please sign in to comment.