/
ReactNativeUtils.java
112 lines (93 loc) · 4.26 KB
/
ReactNativeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.airbnb.android.react.navigation;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.FragmentActivity;
import com.airbnb.android.R;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
public final class ReactNativeUtils {
static final String VERSION_CONSTANT_KEY = "VERSION";
private static final String IS_SHARED_ELEMENT_TRANSITION = "isSharedElementTransition";
private static final int SHARED_ELEMENT_TARGET_API = Build.VERSION_CODES.LOLLIPOP_MR1;
private ReactNativeUtils() {
}
public static void pushScreen(Context context, String moduleName) {
pushScreen(context, moduleName, null);
}
public static void pushScreen(Context context, String moduleName, @Nullable Bundle props) {
Bundle options = ActivityOptionsCompat
.makeCustomAnimation(context, R.anim.slide_in_right, R.anim.delay)
.toBundle();
showScreen(context, moduleName, props, options, false);
}
public static void presentScreen(Context context, String moduleName) {
presentScreen(context, moduleName, null);
}
public static void presentScreen(Context context, String moduleName, @Nullable Bundle props) {
Bundle options = ActivityOptionsCompat
.makeCustomAnimation(context, R.anim.slide_up, R.anim.delay)
.toBundle();
showScreen(context, moduleName, props, options, true);
}
private static void showScreen(
Context context, String moduleName, Bundle props, Bundle options, boolean isModal) {
Intent intent = intent(context, moduleName, props, isModal);
context.startActivity(intent, options);
}
// TODO: delete this?
public static Intent intentWithDismissFlag() {
return new Intent().putExtra(ReactNativeIntents.EXTRA_IS_DISMISS, true);
}
private static Intent intent(Context context, String moduleName, Bundle props, boolean isModal) {
return new Intent(context, ReactNativeActivity.class)
.putExtra(ReactNativeFragment.EXTRA_IS_MODAL, isModal)
.putExtra(ReactNativeFragment.EXTRA_REACT_MODULE_NAME, moduleName)
.putExtra(ReactNativeFragment.EXTRA_REACT_PROPS, props);
}
/** Emits a JS event with the provided name and data if the rect context is initialized */
static void maybeEmitEvent(ReactContext context, String name, Object data) {
if (context == null) {
throw new IllegalArgumentException(
String.format("reactContext is null (calling event: %s)", name));
}
if (context.hasActiveCatalystInstance()) {
try {
context.getJSModule(RCTDeviceEventEmitter.class).emit(name, data);
} catch (RuntimeException e) {
// the JS bundle hasn't finished executing, so this call is going to be lost.
// In the future, we could maybe set something up to queue the call, and then pass them through once
// the bundle has finished getting parsed, but for now I am going to just swallow the error.
}
}
}
/** Returns true if the provided intent will launch a ReactNative Activity, false otherwise. */
static boolean isReactNativeIntent(Intent intent) {
String className = intent.getComponent().getClassName();
return ReactNativeActivity.class.getName().equals(className);
}
static boolean isSharedElementTransition(Activity activity) {
return isSharedElementTransition(activity.getIntent().getExtras());
}
static boolean isSharedElementTransition(Intent intent) {
return isSharedElementTransition(intent.getExtras());
}
static boolean isSharedElementTransition(@Nullable Bundle args) {
return Build.VERSION.SDK_INT >= SHARED_ELEMENT_TARGET_API && args != null &&
args.getBoolean(IS_SHARED_ELEMENT_TRANSITION, false);
}
static void setIsSharedElementTransition(Intent intent) {
intent.putExtra(IS_SHARED_ELEMENT_TRANSITION, true);
}
// static boolean isSuccessfullyInitialized(ReactInstanceManager reactInstanceManager) {
// // TODO
// return false;
// }
static void showAlertBecauseChecksFailed(FragmentActivity activity, Object o) {
// TODO
}
}