Open souce the Android Dialog module

Summary:
public

The `DialogModule` requires `android.support.v4.app.FragmentManager` which means
every app that wants to use Dialogs would need to have its Activity extend the legacy
`android.support.v4.app.FragmentActivity`.

This diff makes the `DialogModule` work with both the Support `FragmentManager`
(for AdsManager & potentially other fb apps) and the `android.app.FragmentManager`
(for new apps with no legacy dependencies).

Also wrap the native module in the same `Alert` API that we have on iOS and provide
a cross-platform example. In my opinion the iOS Alert API is quite nice and easy to use.

We still keep `AlertIOS` around because of its `prompt` function which is iOS-specific
and also for backwards compatibility.

Reviewed By: foghina

Differential Revision: D2647000

fb-gh-sync-id: e2280451890bff58bd9c933ab53cd99055403858
This commit is contained in:
Martin Konicek
2015-12-17 11:09:22 -08:00
committed by facebook-github-bot-5
parent fe86771a22
commit 3a3af8a385
13 changed files with 831 additions and 73 deletions

View File

@@ -186,7 +186,7 @@ public abstract class ReactInstanceManager {
/**
* Path to the JS bundle file to be loaded from the file system.
*
* Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle}
* Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"}
*/
public Builder setJSBundleFile(String jsBundleFile) {
mJSBundleFile = jsBundleFile;

View File

@@ -0,0 +1,77 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.modules.dialog;
import javax.annotation.Nullable;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Context;
import android.os.Bundle;
/**
* A fragment used to display the dialog.
*/
/* package */ class AlertFragment extends DialogFragment implements DialogInterface.OnClickListener {
/* package */ static final String ARG_TITLE = "title";
/* package */ static final String ARG_MESSAGE = "message";
/* package */ static final String ARG_BUTTON_POSITIVE = "button_positive";
/* package */ static final String ARG_BUTTON_NEGATIVE = "button_negative";
/* package */ static final String ARG_BUTTON_NEUTRAL = "button_neutral";
private final @Nullable DialogModule.AlertFragmentListener mListener;
public AlertFragment(@Nullable DialogModule.AlertFragmentListener listener, Bundle arguments) {
mListener = listener;
setArguments(arguments);
}
public static Dialog createDialog(
Context activityContext, Bundle arguments, DialogInterface.OnClickListener fragment) {
AlertDialog.Builder builder = new AlertDialog.Builder(activityContext)
.setTitle(arguments.getString(ARG_TITLE))
.setMessage(arguments.getString(ARG_MESSAGE));
if (arguments.containsKey(ARG_BUTTON_POSITIVE)) {
builder.setPositiveButton(arguments.getString(ARG_BUTTON_POSITIVE), fragment);
}
if (arguments.containsKey(ARG_BUTTON_NEGATIVE)) {
builder.setNegativeButton(arguments.getString(ARG_BUTTON_NEGATIVE), fragment);
}
if (arguments.containsKey(ARG_BUTTON_NEUTRAL)) {
builder.setNeutralButton(arguments.getString(ARG_BUTTON_NEUTRAL), fragment);
}
return builder.create();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return createDialog(getActivity(), getArguments(), this);
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null) {
mListener.onClick(dialog, which);
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (mListener != null) {
mListener.onDismiss(dialog);
}
}
}

View File

@@ -0,0 +1,255 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.modules.dialog;
import javax.annotation.Nullable;
import java.util.Map;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
public class DialogModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
/* package */ static final String FRAGMENT_TAG =
"com.facebook.catalyst.react.dialog.DialogModule";
/* package */ static final String NAME = "DialogManagerAndroid";
/* package */ static final String ACTION_BUTTON_CLICKED = "buttonClicked";
/* package */ static final String ACTION_DISMISSED = "dismissed";
/* package */ static final String KEY_TITLE = "title";
/* package */ static final String KEY_MESSAGE = "message";
/* package */ static final String KEY_BUTTON_POSITIVE = "buttonPositive";
/* package */ static final String KEY_BUTTON_NEGATIVE = "buttonNegative";
/* package */ static final String KEY_BUTTON_NEUTRAL = "buttonNeutral";
/* package */ static final Map<String, Object> CONSTANTS = MapBuilder.<String, Object>of(
ACTION_BUTTON_CLICKED, ACTION_BUTTON_CLICKED,
ACTION_DISMISSED, ACTION_DISMISSED,
KEY_BUTTON_POSITIVE, DialogInterface.BUTTON_POSITIVE,
KEY_BUTTON_NEGATIVE, DialogInterface.BUTTON_NEGATIVE,
KEY_BUTTON_NEUTRAL, DialogInterface.BUTTON_NEUTRAL);
private boolean mIsInForeground;
public DialogModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public 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 android.support.v4.app.FragmentManager mSupportFragmentManager;
private @Nullable Object mFragmentToShow;
private boolean isUsingSupportLibrary() {
return mSupportFragmentManager != null;
}
public FragmentManagerHelper(android.support.v4.app.FragmentManager supportFragmentManager) {
mFragmentManager = null;
mSupportFragmentManager = supportFragmentManager;
}
public FragmentManagerHelper(android.app.FragmentManager fragmentManager) {
mFragmentManager = fragmentManager;
mSupportFragmentManager = null;
}
public void showPendingAlert() {
if (mFragmentToShow == null) {
return;
}
if (isUsingSupportLibrary()) {
((SupportAlertFragment) mFragmentToShow).show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
((AlertFragment) mFragmentToShow).show(mFragmentManager, FRAGMENT_TAG);
}
mFragmentToShow = null;
}
private void dismissExisting() {
if (isUsingSupportLibrary()) {
SupportAlertFragment oldFragment =
(SupportAlertFragment) mSupportFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null) {
oldFragment.dismiss();
}
} else {
AlertFragment oldFragment =
(AlertFragment) mFragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null) {
oldFragment.dismiss();
}
}
}
public void showNewAlert(boolean isInForeground, Bundle arguments, Callback actionCallback) {
dismissExisting();
AlertFragmentListener actionListener =
actionCallback != null ? new AlertFragmentListener(actionCallback) : null;
if (isUsingSupportLibrary()) {
SupportAlertFragment alertFragment = new SupportAlertFragment(actionListener, arguments);
if (isInForeground) {
alertFragment.show(mSupportFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
}
} else {
AlertFragment alertFragment = new AlertFragment(actionListener, arguments);
if (isInForeground) {
alertFragment.show(mFragmentManager, FRAGMENT_TAG);
} else {
mFragmentToShow = alertFragment;
}
}
}
}
/* package */ class AlertFragmentListener implements OnClickListener, OnDismissListener {
private final Callback mCallback;
private boolean mCallbackConsumed = false;
public AlertFragmentListener(Callback callback) {
mCallback = callback;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (!mCallbackConsumed) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
mCallback.invoke(ACTION_BUTTON_CLICKED, which);
mCallbackConsumed = true;
}
}
}
@Override
public void onDismiss(DialogInterface dialog) {
if (!mCallbackConsumed) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
mCallback.invoke(ACTION_DISMISSED);
mCallbackConsumed = true;
}
}
}
}
@Override
public Map<String, Object> getConstants() {
return CONSTANTS;
}
@Override
public void initialize() {
getReactApplicationContext().addLifecycleEventListener(this);
}
@Override
public void onHostPause() {
// Don't show the dialog if the host is paused.
mIsInForeground = false;
}
@Override
public void onHostDestroy() {
}
@Override
public void onHostResume() {
mIsInForeground = true;
// Check if a dialog has been created while the host was paused, so that we can show it now.
FragmentManagerHelper fragmentManagerHelper = getFragmentManagerHelper();
Assertions.assertNotNull(
fragmentManagerHelper,
"Attached DialogModule to host with pending alert but no FragmentManager " +
"(not attached to an Activity).");
fragmentManagerHelper.showPendingAlert();
}
@ReactMethod
public void showAlert(
ReadableMap options,
Callback errorCallback,
Callback actionCallback) {
FragmentManagerHelper fragmentManagerHelper = getFragmentManagerHelper();
if (fragmentManagerHelper == null) {
errorCallback.invoke("Tried to show an alert while not attached to an Activity");
return;
}
final Bundle args = new Bundle();
if (options.hasKey(KEY_TITLE)) {
args.putString(AlertFragment.ARG_TITLE, options.getString(KEY_TITLE));
}
if (options.hasKey(KEY_MESSAGE)) {
args.putString(AlertFragment.ARG_MESSAGE, options.getString(KEY_MESSAGE));
}
if (options.hasKey(KEY_BUTTON_POSITIVE)) {
args.putString(AlertFragment.ARG_BUTTON_POSITIVE, options.getString(KEY_BUTTON_POSITIVE));
}
if (options.hasKey(KEY_BUTTON_NEGATIVE)) {
args.putString(AlertFragment.ARG_BUTTON_NEGATIVE, options.getString(KEY_BUTTON_NEGATIVE));
}
if (options.hasKey(KEY_BUTTON_NEUTRAL)) {
args.putString(AlertFragment.ARG_BUTTON_NEUTRAL, options.getString(KEY_BUTTON_NEUTRAL));
}
fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback);
}
/**
* 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.
*
* DO NOT HOLD LONG-LIVED REFERENCES TO THE OBJECT RETURNED BY THIS METHOD, AS THIS WILL CAUSE
* MEMORY LEAKS.
*/
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
Activity activity = getCurrentActivity();
if (activity == null) {
return null;
}
if (activity instanceof FragmentActivity) {
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
} else {
return new FragmentManagerHelper(activity.getFragmentManager());
}
}
}

View File

@@ -0,0 +1,52 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.modules.dialog;
import javax.annotation.Nullable;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
/**
* {@link AlertFragment} for apps that use the Support FragmentActivity and FragmentManager
* for legacy reasons.
*/
/* package */ class SupportAlertFragment extends DialogFragment implements DialogInterface.OnClickListener {
private final @Nullable DialogModule.AlertFragmentListener mListener;
public SupportAlertFragment(@Nullable DialogModule.AlertFragmentListener listener, Bundle arguments) {
mListener = listener;
setArguments(arguments);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return AlertFragment.createDialog(getActivity(), getArguments(), this);
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (mListener != null) {
mListener.onClick(dialog, which);
}
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (mListener != null) {
mListener.onDismiss(dialog);
}
}
}

View File

@@ -17,6 +17,7 @@ import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.modules.dialog.DialogModule;
import com.facebook.react.modules.fresco.FrescoModule;
import com.facebook.react.modules.intent.IntentModule;
import com.facebook.react.modules.location.LocationModule;
@@ -53,6 +54,7 @@ public class MainReactPackage implements ReactPackage {
return Arrays.<NativeModule>asList(
new AsyncStorageModule(reactContext),
new ClipboardModule(reactContext),
new DialogModule(reactContext),
new FrescoModule(reactContext),
new IntentModule(reactContext),
new LocationModule(reactContext),