From c6c5a173bce3d8c847931d26eddb295956285438 Mon Sep 17 00:00:00 2001 From: Dulmandakh Date: Mon, 11 Feb 2019 14:33:54 -0800 Subject: [PATCH] DatePickerDialogModule supports only FragmentActivity (#23371) 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 DatePickerDialogModule 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] - DatePickerDialogModule supports only FragmentActivity Pull Request resolved: https://github.com/facebook/react-native/pull/23371 Differential Revision: D14030765 Pulled By: cpojer fbshipit-source-id: 3a1811102cf68b82c139f0e20b2fc8dab5d98b69 --- .../datepicker/DatePickerDialogFragment.java | 2 +- .../datepicker/DatePickerDialogModule.java | 60 +++++++------------ .../SupportDatePickerDialogFragment.java | 49 --------------- 3 files changed, 22 insertions(+), 89 deletions(-) delete mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/SupportDatePickerDialogFragment.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogFragment.java b/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogFragment.java index db327d4bd..5ddeae7b9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogFragment.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogFragment.java @@ -16,12 +16,12 @@ import android.annotation.SuppressLint; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; -import android.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.os.Build; import android.os.Bundle; +import android.support.v4.app.DialogFragment; import android.widget.DatePicker; @SuppressLint("ValidFragment") 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 8dfe5b49e..8b8162af1 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,18 +7,20 @@ package com.facebook.react.modules.datepicker; -import android.app.Activity; + import android.app.DatePickerDialog.OnDateSetListener; -import android.app.DialogFragment; -import android.app.FragmentManager; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.widget.DatePicker; import com.facebook.react.bridge.*; import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.module.annotations.ReactModule; +import javax.annotation.Nonnull; import javax.annotation.Nullable; /** @@ -46,7 +48,7 @@ public class DatePickerDialogModule extends ReactContextBaseJavaModule { } @Override - public String getName() { + public @Nonnull String getName() { return DatePickerDialogModule.FRAGMENT_TAG; } @@ -110,48 +112,28 @@ public class DatePickerDialogModule extends ReactContextBaseJavaModule { */ @ReactMethod public void open(@Nullable final ReadableMap options, Promise promise) { - Activity activity = getCurrentActivity(); + FragmentActivity activity = (FragmentActivity) getCurrentActivity(); if (activity == null) { promise.reject( ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity"); return; } - // 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. - if (activity instanceof android.support.v4.app.FragmentActivity) { - android.support.v4.app.FragmentManager fragmentManager = - ((android.support.v4.app.FragmentActivity) activity).getSupportFragmentManager(); - android.support.v4.app.DialogFragment oldFragment = - (android.support.v4.app.DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); - if (oldFragment != null) { - oldFragment.dismiss(); - } - SupportDatePickerDialogFragment fragment = new SupportDatePickerDialogFragment(); - if (options != null) { - final Bundle args = createFragmentArguments(options); - fragment.setArguments(args); - } - final DatePickerDialogListener listener = new DatePickerDialogListener(promise); - fragment.setOnDismissListener(listener); - fragment.setOnDateSetListener(listener); - fragment.show(fragmentManager, FRAGMENT_TAG); - } else { - FragmentManager fragmentManager = activity.getFragmentManager(); - DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); - if (oldFragment != null) { - oldFragment.dismiss(); - } - DatePickerDialogFragment fragment = new DatePickerDialogFragment(); - if (options != null) { - final Bundle args = createFragmentArguments(options); - fragment.setArguments(args); - } - final DatePickerDialogListener listener = new DatePickerDialogListener(promise); - fragment.setOnDismissListener(listener); - fragment.setOnDateSetListener(listener); - fragment.show(fragmentManager, FRAGMENT_TAG); + + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); + if (oldFragment != null) { + oldFragment.dismiss(); } + DatePickerDialogFragment fragment = new DatePickerDialogFragment(); + if (options != null) { + final Bundle args = createFragmentArguments(options); + fragment.setArguments(args); + } + final DatePickerDialogListener listener = new DatePickerDialogListener(promise); + fragment.setOnDismissListener(listener); + fragment.setOnDateSetListener(listener); + fragment.show(fragmentManager, FRAGMENT_TAG); } private Bundle createFragmentArguments(ReadableMap options) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/SupportDatePickerDialogFragment.java b/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/SupportDatePickerDialogFragment.java deleted file mode 100644 index 13d334692..000000000 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/SupportDatePickerDialogFragment.java +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.modules.datepicker; - -import javax.annotation.Nullable; - -import android.annotation.SuppressLint; -import android.app.DatePickerDialog.OnDateSetListener; -import android.app.Dialog; -import android.content.DialogInterface; -import android.content.DialogInterface.OnDismissListener; -import android.os.Bundle; -import android.support.v4.app.DialogFragment; - -@SuppressLint("ValidFragment") -public class SupportDatePickerDialogFragment extends DialogFragment { - - @Nullable - private OnDateSetListener mOnDateSetListener; - @Nullable - private OnDismissListener mOnDismissListener; - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final Bundle args = getArguments(); - return DatePickerDialogFragment.createDialog(args, getActivity(), mOnDateSetListener); - } - - @Override - public void onDismiss(DialogInterface dialog) { - super.onDismiss(dialog); - if (mOnDismissListener != null) { - mOnDismissListener.onDismiss(dialog); - } - } - - /*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) { - mOnDateSetListener = onDateSetListener; - } - - /*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) { - mOnDismissListener = onDismissListener; - } -}