Skip to content

Commit

Permalink
DialogModule supports only FragmentActivity (#23365)
Browse files Browse the repository at this point in the history
Summary:
Now RN has only ReactActivity which extends AppCompatActivity, subclass of FragmentActivity, therefore no need to check if activity is FragmentActivity or not. This PR changes DialogModule to work only with FragmentActivity.

Also DialogFragment from Android is deprecated in API 28, and recommends to use DialogFragment from Support Library. Excerpt from DialogFragment documentation.

> **This class was deprecated in API level 28.**
> Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

**BREAKING CHANGE**: Brown field apps must extend FragmentActivity or its subclasses.

[Android] [Changed] - DialogModule supports only FragmentActivity
Pull Request resolved: #23365

Differential Revision: D14021986

Pulled By: cpojer

fbshipit-source-id: b0ede60ef19cec48111a12701659a8bc1f66c331
  • Loading branch information
dulmandakh authored and facebook-github-bot committed Apr 26, 2019
1 parent ffc0986 commit 243070a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;

/**
* A fragment used to display the dialog.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
Expand All @@ -26,6 +28,8 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@ReactModule(name = DialogModule.NAME)
Expand Down Expand Up @@ -60,35 +64,17 @@ public DialogModule(ReactApplicationContext reactContext) {
}

@Override
public String getName() {
public @Nonnull String getName() {
return NAME;
}

/**
* Helper to allow this module to work with both the standard FragmentManager
* and the Support FragmentManager (for apps that need to use it for legacy reasons).
* Since the two APIs don't share a common interface there's unfortunately some
* code duplication.
*/
private class FragmentManagerHelper {

// Exactly one of the two is null
private final @Nullable android.app.FragmentManager mFragmentManager;
private final @Nullable androidx.fragment.app.FragmentManager mSupportFragmentManager;
private final @Nonnull FragmentManager mFragmentManager;

private @Nullable Object mFragmentToShow;

private boolean isUsingSupportLibrary() {
return mSupportFragmentManager != null;
}

public FragmentManagerHelper(androidx.fragment.app.FragmentManager supportFragmentManager) {
mFragmentManager = null;
mSupportFragmentManager = supportFragmentManager;
}
public FragmentManagerHelper(android.app.FragmentManager fragmentManager) {
public FragmentManagerHelper(@Nonnull FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
mSupportFragmentManager = null;
}

public void showPendingAlert() {
Expand All @@ -99,30 +85,18 @@ public void showPendingAlert() {
}

dismissExisting();
if (isUsingSupportLibrary()) {
((SupportAlertFragment) mFragmentToShow).show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
((AlertFragment) mFragmentToShow).show(mFragmentManager, FRAGMENT_TAG);
}
((AlertFragment) mFragmentToShow).show(mFragmentManager, FRAGMENT_TAG);
mFragmentToShow = null;
}

private void dismissExisting() {
if (!mIsInForeground) {
return;
}
if (isUsingSupportLibrary()) {
SupportAlertFragment oldFragment =
(SupportAlertFragment) mSupportFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
} else {
AlertFragment oldFragment =
(AlertFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
AlertFragment oldFragment =
(AlertFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null && oldFragment.isResumed()) {
oldFragment.dismiss();
}
}

Expand All @@ -134,26 +108,14 @@ public void showNewAlert(Bundle arguments, Callback actionCallback) {
AlertFragmentListener actionListener =
actionCallback != null ? new AlertFragmentListener(actionCallback) : null;

if (isUsingSupportLibrary()) {
SupportAlertFragment alertFragment = new SupportAlertFragment(actionListener, arguments);
if (mIsInForeground && !mSupportFragmentManager.isStateSaved()) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
if (mIsInForeground && !mFragmentManager.isStateSaved()) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
} else {
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
if (mIsInForeground) {
if (arguments.containsKey(KEY_CANCELABLE)) {
alertFragment.setCancelable(arguments.getBoolean(KEY_CANCELABLE));
}
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
}
mFragmentToShow = alertFragment;
}
}
}
Expand Down Expand Up @@ -269,8 +231,8 @@ public void run() {
}

/**
* Creates a new helper to work with either the FragmentManager or the legacy support
* FragmentManager transparently. Returns null if we're not attached to an Activity.
* Creates a new helper to work with FragmentManager.
* Returns null if we're not attached to an Activity.
*
* DO NOT HOLD LONG-LIVED REFERENCES TO THE OBJECT RETURNED BY THIS METHOD, AS THIS WILL CAUSE
* MEMORY LEAKS.
Expand All @@ -280,10 +242,6 @@ public void run() {
if (activity == null) {
return null;
}
if (activity instanceof FragmentActivity) {
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
} else {
return new FragmentManagerHelper(activity.getFragmentManager());
}
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Activity;


import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -25,15 +25,18 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ActivityController;

import androidx.fragment.app.FragmentActivity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

@RunWith(RobolectricTestRunner.class)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"})
public class DialogModuleTest {

private ActivityController<Activity> mActivityController;
private Activity mActivity;
private ActivityController<FragmentActivity> mActivityController;
private FragmentActivity mActivity;
private DialogModule mDialogModule;

final static class SimpleCallback implements Callback {
Expand All @@ -57,7 +60,7 @@ public Object[] getArgs() {

@Before
public void setUp() throws Exception {
mActivityController = Robolectric.buildActivity(Activity.class);
mActivityController = Robolectric.buildActivity(FragmentActivity.class);
mActivity = mActivityController
.create()
.start()
Expand Down Expand Up @@ -94,7 +97,7 @@ public void testAllOptions() {

final AlertFragment fragment = getFragment();
assertNotNull("Fragment was not displayed", fragment);
assertEquals(false, fragment.isCancelable());
assertFalse(fragment.isCancelable());

final AlertDialog dialog = (AlertDialog) fragment.getDialog();
assertEquals("OK", dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText().toString());
Expand Down Expand Up @@ -164,7 +167,7 @@ public void testCallbackDismiss() {
}

private AlertFragment getFragment() {
return (AlertFragment) mActivity.getFragmentManager()
return (AlertFragment) mActivity.getSupportFragmentManager()
.findFragmentByTag(DialogModule.FRAGMENT_TAG);
}
}

0 comments on commit 243070a

Please sign in to comment.