|
7 | 7 |
|
8 | 8 | package com.facebook.react.modules.datepicker;
|
9 | 9 |
|
10 |
| -import android.app.Activity; |
| 10 | + |
11 | 11 | import android.app.DatePickerDialog.OnDateSetListener;
|
12 |
| -import android.app.DialogFragment; |
13 |
| -import android.app.FragmentManager; |
14 | 12 | import android.content.DialogInterface;
|
15 | 13 | import android.content.DialogInterface.OnDismissListener;
|
16 | 14 | import android.os.Bundle;
|
| 15 | +import android.support.v4.app.DialogFragment; |
| 16 | +import android.support.v4.app.FragmentActivity; |
| 17 | +import android.support.v4.app.FragmentManager; |
17 | 18 | import android.widget.DatePicker;
|
18 | 19 | import com.facebook.react.bridge.*;
|
19 | 20 | import com.facebook.react.common.annotations.VisibleForTesting;
|
20 | 21 | import com.facebook.react.module.annotations.ReactModule;
|
21 | 22 |
|
| 23 | +import javax.annotation.Nonnull; |
22 | 24 | import javax.annotation.Nullable;
|
23 | 25 |
|
24 | 26 | /**
|
@@ -46,7 +48,7 @@ public DatePickerDialogModule(ReactApplicationContext reactContext) {
|
46 | 48 | }
|
47 | 49 |
|
48 | 50 | @Override
|
49 |
| - public String getName() { |
| 51 | + public @Nonnull String getName() { |
50 | 52 | return DatePickerDialogModule.FRAGMENT_TAG;
|
51 | 53 | }
|
52 | 54 |
|
@@ -110,48 +112,28 @@ public void onDismiss(DialogInterface dialog) {
|
110 | 112 | */
|
111 | 113 | @ReactMethod
|
112 | 114 | public void open(@Nullable final ReadableMap options, Promise promise) {
|
113 |
| - Activity activity = getCurrentActivity(); |
| 115 | + FragmentActivity activity = (FragmentActivity) getCurrentActivity(); |
114 | 116 | if (activity == null) {
|
115 | 117 | promise.reject(
|
116 | 118 | ERROR_NO_ACTIVITY,
|
117 | 119 | "Tried to open a DatePicker dialog while not attached to an Activity");
|
118 | 120 | return;
|
119 | 121 | }
|
120 |
| - // We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity |
121 |
| - // (for apps that use it for legacy reasons). This unfortunately leads to some code duplication. |
122 |
| - if (activity instanceof android.support.v4.app.FragmentActivity) { |
123 |
| - android.support.v4.app.FragmentManager fragmentManager = |
124 |
| - ((android.support.v4.app.FragmentActivity) activity).getSupportFragmentManager(); |
125 |
| - android.support.v4.app.DialogFragment oldFragment = |
126 |
| - (android.support.v4.app.DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); |
127 |
| - if (oldFragment != null) { |
128 |
| - oldFragment.dismiss(); |
129 |
| - } |
130 |
| - SupportDatePickerDialogFragment fragment = new SupportDatePickerDialogFragment(); |
131 |
| - if (options != null) { |
132 |
| - final Bundle args = createFragmentArguments(options); |
133 |
| - fragment.setArguments(args); |
134 |
| - } |
135 |
| - final DatePickerDialogListener listener = new DatePickerDialogListener(promise); |
136 |
| - fragment.setOnDismissListener(listener); |
137 |
| - fragment.setOnDateSetListener(listener); |
138 |
| - fragment.show(fragmentManager, FRAGMENT_TAG); |
139 |
| - } else { |
140 |
| - FragmentManager fragmentManager = activity.getFragmentManager(); |
141 |
| - DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); |
142 |
| - if (oldFragment != null) { |
143 |
| - oldFragment.dismiss(); |
144 |
| - } |
145 |
| - DatePickerDialogFragment fragment = new DatePickerDialogFragment(); |
146 |
| - if (options != null) { |
147 |
| - final Bundle args = createFragmentArguments(options); |
148 |
| - fragment.setArguments(args); |
149 |
| - } |
150 |
| - final DatePickerDialogListener listener = new DatePickerDialogListener(promise); |
151 |
| - fragment.setOnDismissListener(listener); |
152 |
| - fragment.setOnDateSetListener(listener); |
153 |
| - fragment.show(fragmentManager, FRAGMENT_TAG); |
| 122 | + |
| 123 | + FragmentManager fragmentManager = activity.getSupportFragmentManager(); |
| 124 | + DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG); |
| 125 | + if (oldFragment != null) { |
| 126 | + oldFragment.dismiss(); |
| 127 | + } |
| 128 | + DatePickerDialogFragment fragment = new DatePickerDialogFragment(); |
| 129 | + if (options != null) { |
| 130 | + final Bundle args = createFragmentArguments(options); |
| 131 | + fragment.setArguments(args); |
154 | 132 | }
|
| 133 | + final DatePickerDialogListener listener = new DatePickerDialogListener(promise); |
| 134 | + fragment.setOnDismissListener(listener); |
| 135 | + fragment.setOnDateSetListener(listener); |
| 136 | + fragment.show(fragmentManager, FRAGMENT_TAG); |
155 | 137 | }
|
156 | 138 |
|
157 | 139 | private Bundle createFragmentArguments(ReadableMap options) {
|
|
0 commit comments