/
ReactActivityDelegate.java
220 lines (185 loc) · 6.73 KB
/
ReactActivityDelegate.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) Meta Platforms, Inc. and 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;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Callback;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.modules.core.PermissionListener;
/**
* Delegate class for {@link ReactActivity}. You can subclass this to provide custom implementations
* for e.g. {@link #getReactNativeHost()}, if your Application class doesn't implement {@link
* ReactApplication}.
*/
public class ReactActivityDelegate {
private final @Nullable Activity mActivity;
private final @Nullable String mMainComponentName;
private @Nullable PermissionListener mPermissionListener;
private @Nullable Callback mPermissionsCallback;
private @Nullable ReactDelegate mReactDelegate;
/**
* Prefer using ReactActivity when possible, as it hooks up all Activity lifecycle methods by
* default. It also implements DefaultHardwareBackBtnHandler, which ReactDelegate requires.
*/
@Deprecated
public ReactActivityDelegate(@Nullable Activity activity, @Nullable String mainComponentName) {
mActivity = activity;
mMainComponentName = mainComponentName;
}
public ReactActivityDelegate(
@Nullable ReactActivity activity, @Nullable String mainComponentName) {
mActivity = activity;
mMainComponentName = mainComponentName;
}
/**
* Public API to populate the launch options that will be passed to React. Here you can customize
* the values that will be passed as `initialProperties` to the Renderer.
*
* @return Either null or a key-value map as a Bundle
*/
protected @Nullable Bundle getLaunchOptions() {
return null;
}
protected @Nullable Bundle composeLaunchOptions() {
return getLaunchOptions();
}
/**
* Override to customize ReactRootView creation.
*
* <p>Not used on bridgeless
*/
protected ReactRootView createRootView() {
return null;
}
/**
* Get the {@link ReactNativeHost} used by this app. By default, assumes {@link
* Activity#getApplication()} is an instance of {@link ReactApplication} and calls {@link
* ReactApplication#getReactNativeHost()}. Override this method if your application class does not
* implement {@code ReactApplication} or you simply have a different mechanism for storing a
* {@code ReactNativeHost}, e.g. as a static field somewhere.
*/
protected ReactNativeHost getReactNativeHost() {
return ((ReactApplication) getPlainActivity().getApplication()).getReactNativeHost();
}
public ReactHost getReactHost() {
return ((ReactApplication) getPlainActivity().getApplication()).getReactHost();
}
protected @Nullable ReactDelegate getReactDelegate() {
return mReactDelegate;
}
public ReactInstanceManager getReactInstanceManager() {
return mReactDelegate.getReactInstanceManager();
}
public String getMainComponentName() {
return mMainComponentName;
}
public void onCreate(Bundle savedInstanceState) {
String mainComponentName = getMainComponentName();
final Bundle launchOptions = composeLaunchOptions();
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactDelegate =
new ReactDelegate(getPlainActivity(), getReactHost(), mainComponentName, launchOptions);
} else {
mReactDelegate =
new ReactDelegate(
getPlainActivity(), getReactNativeHost(), mainComponentName, launchOptions) {
@Override
protected ReactRootView createRootView() {
ReactRootView rootView = ReactActivityDelegate.this.createRootView();
if (rootView == null) {
rootView = super.createRootView();
}
return rootView;
}
};
}
if (mainComponentName != null) {
loadApp(mainComponentName);
}
}
protected void loadApp(String appKey) {
mReactDelegate.loadApp(appKey);
getPlainActivity().setContentView(mReactDelegate.getReactRootView());
}
public void onUserLeaveHint() {
mReactDelegate.onUserLeaveHint();
}
public void onPause() {
mReactDelegate.onHostPause();
}
public void onResume() {
mReactDelegate.onHostResume();
if (mPermissionsCallback != null) {
mPermissionsCallback.invoke();
mPermissionsCallback = null;
}
}
public void onDestroy() {
mReactDelegate.onHostDestroy();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mReactDelegate.onActivityResult(requestCode, resultCode, data, true);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mReactDelegate.onKeyDown(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mReactDelegate.shouldShowDevMenuOrReload(keyCode, event);
}
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
return mReactDelegate.onKeyLongPress(keyCode);
}
public boolean onBackPressed() {
return mReactDelegate.onBackPressed();
}
public boolean onNewIntent(Intent intent) {
return mReactDelegate.onNewIntent(intent);
}
public void onWindowFocusChanged(boolean hasFocus) {
mReactDelegate.onWindowFocusChanged(hasFocus);
}
public void onConfigurationChanged(Configuration newConfig) {
mReactDelegate.onConfigurationChanged(newConfig);
}
public void requestPermissions(
String[] permissions, int requestCode, PermissionListener listener) {
mPermissionListener = listener;
getPlainActivity().requestPermissions(permissions, requestCode);
}
public void onRequestPermissionsResult(
final int requestCode, final String[] permissions, final int[] grantResults) {
mPermissionsCallback =
args -> {
if (mPermissionListener != null
&& mPermissionListener.onRequestPermissionsResult(
requestCode, permissions, grantResults)) {
mPermissionListener = null;
}
};
}
protected Context getContext() {
return Assertions.assertNotNull(mActivity);
}
protected Activity getPlainActivity() {
return ((Activity) getContext());
}
/**
* Override this method if you wish to selectively toggle Fabric for a specific surface. This will
* also control if Concurrent Root (React 18) should be enabled or not.
*
* @return true if Fabric is enabled for this Activity, false otherwise.
*/
protected boolean isFabricEnabled() {
return ReactFeatureFlags.enableFabricRenderer;
}
}