/
ReactNativeActivity.java
727 lines (608 loc) · 26.1 KB
/
ReactNativeActivity.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
// Copyright 2015-present 650 Industries. All rights reserved.
package host.exp.exponent.experience;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.devsupport.DoubleTapReloadRecognizer;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.unimodules.core.interfaces.Package;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import de.greenrobot.event.EventBus;
import host.exp.exponent.ABIVersion;
import host.exp.exponent.Constants;
import host.exp.exponent.ExponentManifest;
import host.exp.exponent.LoadingView;
import host.exp.exponent.RNObject;
import host.exp.exponent.analytics.Analytics;
import host.exp.exponent.analytics.EXL;
import host.exp.exponent.di.NativeModuleDepsProvider;
import host.exp.exponent.kernel.ExperienceId;
import host.exp.exponent.kernel.ExponentError;
import host.exp.exponent.kernel.ExponentErrorMessage;
import host.exp.exponent.kernel.KernelConstants;
import host.exp.exponent.kernel.KernelProvider;
import host.exp.exponent.kernel.services.ErrorRecoveryManager;
import host.exp.exponent.kernel.services.ExpoKernelServiceRegistry;
import host.exp.exponent.kernel.services.SplashScreenKernelService;
import host.exp.exponent.notifications.ExponentNotification;
import host.exp.exponent.storage.ExponentSharedPreferences;
import host.exp.exponent.utils.ExperienceActivityUtils;
import host.exp.exponent.utils.JSONBundleConverter;
import host.exp.exponent.utils.ScopedPermissionsRequester;
import host.exp.expoview.Exponent;
import host.exp.expoview.R;
import versioned.host.exp.exponent.ExponentPackage;
import static host.exp.exponent.kernel.KernelConstants.INTENT_URI_KEY;
import static host.exp.exponent.kernel.KernelConstants.IS_HEADLESS_KEY;
import static host.exp.exponent.kernel.KernelConstants.LINKING_URI_KEY;
import static host.exp.exponent.kernel.KernelConstants.MANIFEST_URL_KEY;
import static host.exp.exponent.utils.ScopedPermissionsRequester.EXPONENT_PERMISSIONS_REQUEST;
public abstract class ReactNativeActivity extends AppCompatActivity implements com.facebook.react.modules.core.DefaultHardwareBackBtnHandler, PermissionAwareActivity {
public static class ExperienceDoneLoadingEvent {
private Activity mActivity;
ExperienceDoneLoadingEvent(Activity activity) {
super();
mActivity = activity;
}
public Activity getActivity() {
return mActivity;
}
}
// Override
public Bundle initialProps(Bundle expBundle) {
return expBundle;
}
// Override
protected void onDoneLoading() {
}
// Override
// Will be called after waitForDrawOverOtherAppPermission
protected void startReactInstance() {
}
private static final String TAG = ReactNativeActivity.class.getSimpleName();
private static final long VIEW_TEST_INTERVAL_MS = 20;
protected RNObject mReactInstanceManager = new RNObject("com.facebook.react.ReactInstanceManager");
protected boolean mIsCrashed = false;
protected String mManifestUrl;
protected String mExperienceIdString;
protected ExperienceId mExperienceId;
protected String mSDKVersion;
protected int mActivityId;
// In detach we want UNVERSIONED most places. We still need the numbered sdk version
// when creating cache keys.
protected String mDetachSdkVersion;
protected RNObject mReactRootView;
private FrameLayout mLayout;
private FrameLayout mContainer;
private LoadingView mLoadingView;
private Handler mHandler = new Handler();
private Handler mLoadingHandler = new Handler();
private DoubleTapReloadRecognizer mDoubleTapReloadRecognizer;
private SplashScreenKernelService mSplashScreenKernelService;
protected boolean mIsLoading = true;
protected String mJSBundlePath;
protected JSONObject mManifest;
protected boolean mIsInForeground = false;
protected static Queue<ExponentError> sErrorQueue = new LinkedList<>();
private ScopedPermissionsRequester mScopedPermissionsRequester;
@Inject
protected ExponentSharedPreferences mExponentSharedPreferences;
@Inject
ExpoKernelServiceRegistry mExpoKernelServiceRegistry;
public boolean isLoading() {
return mIsLoading;
}
public boolean isInForeground() {
return mIsInForeground;
}
public View getRootView() {
if (mReactRootView == null) {
return null;
} else {
return (View) mReactRootView.get();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
mLayout = new FrameLayout(this);
setContentView(mLayout);
mContainer = new FrameLayout(this);
mLayout.addView(mContainer);
mLoadingView = new LoadingView(this);
if (!Constants.isStandaloneApp() || Constants.SHOW_LOADING_VIEW_IN_SHELL_APP) {
mContainer.setBackgroundColor(ContextCompat.getColor(this, R.color.splashBackground));
mLayout.addView(mLoadingView);
}
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
Exponent.initialize(this, getApplication());
NativeModuleDepsProvider.getInstance().inject(ReactNativeActivity.class, this);
mSplashScreenKernelService = mExpoKernelServiceRegistry.getSplashScreenKernelService();
// Can't call this here because subclasses need to do other initialization
// before their listener methods are called.
// EventBus.getDefault().registerSticky(this);
}
protected void setView(final View view) {
mContainer.removeAllViews();
if (Constants.isStandaloneApp() && Constants.SHOW_LOADING_VIEW_IN_SHELL_APP) {
ViewGroup.LayoutParams layoutParams = mContainer.getLayoutParams();
layoutParams.height = 0;
mContainer.setLayoutParams(layoutParams);
}
addView(view);
}
public void addView(final View view) {
removeViewFromParent(view);
mContainer.addView(view);
}
public boolean hasView(final View view) {
return view.getParent() == mContainer;
}
protected void removeViewFromParent(final View view) {
if (view.getParent() != null) {
((FrameLayout) view.getParent()).removeView(view);
}
}
protected void stopLoading() {
if (!mIsLoading || !canHideLoadingScreen()) {
return;
}
mHandler.removeCallbacksAndMessages(null);
hideLoadingScreen();
}
protected void updateLoadingProgress(String status, Integer done, Integer total) {
if (!mIsLoading) {
showLoadingScreen(mManifest);
}
mLoadingView.updateProgress(status, done, total);
}
protected void removeViews() {
mContainer.removeAllViews();
}
// Loop until a view is added to the React root view.
protected void checkForReactViews() {
if (mReactRootView.isNull()) {
return;
}
if ((int) mReactRootView.call("getChildCount") > 0) {
if (canHideLoadingScreen()) {
fadeLoadingScreen();
}
onDoneLoading();
ErrorRecoveryManager.getInstance(mExperienceId).markExperienceLoaded();
pollForEventsToSendToRN();
} else {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
checkForReactViews();
}
}, VIEW_TEST_INTERVAL_MS);
}
}
public void showLoadingScreen(JSONObject manifest) {
mLoadingView.setManifest(manifest);
mLoadingView.setShowIcon(true);
mLoadingView.clearAnimation();
mLoadingView.setAlpha(1.0f);
mIsLoading = true;
}
private void fadeLoadingScreen() {
if (!mIsLoading) {
return;
}
runOnUiThread(() -> {
hideLoadingScreen();
EventBus.getDefault().post(new ExperienceDoneLoadingEvent(this));
});
}
private void hideLoadingScreen() {
if (Constants.isStandaloneApp() && Constants.SHOW_LOADING_VIEW_IN_SHELL_APP) {
ViewGroup.LayoutParams layoutParams = mContainer.getLayoutParams();
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
mContainer.setLayoutParams(layoutParams);
}
try {
ExperienceActivityUtils.setRootViewBackgroundColor(mManifest, getRootView());
} catch (Exception e) {
EXL.e(TAG, e);
}
if (mLoadingView != null && mLoadingView.getParent() == mLayout) {
mLoadingView.setAlpha(0.0f);
mLoadingView.setShowIcon(false);
mLoadingView.setDoneLoading();
}
mSplashScreenKernelService.reset();
mIsLoading = false;
mLoadingHandler.removeCallbacksAndMessages(null);
}
private boolean canHideLoadingScreen() {
return (!mSplashScreenKernelService.isAppLoadingStarted() || mSplashScreenKernelService.isAppLoadingFinished()) && mSplashScreenKernelService.shouldAutoHide();
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
RNObject devSupportManager = getDevSupportManager();
if (devSupportManager != null && (boolean) devSupportManager.call("getDevSupportEnabled")) {
boolean didDoubleTapR = Assertions.assertNotNull(mDoubleTapReloadRecognizer)
.didDoubleTapR(keyCode, getCurrentFocus());
// TODO: remove the path where we don't reload from manifest once SDK 35 is deprecated
boolean shouldReloadFromManifest = Exponent.getInstance().shouldAlwaysReloadFromManifest(mSDKVersion);
if (didDoubleTapR && !shouldReloadFromManifest) {
// The loading screen is hidden by versioned code when reloading JS so we can't show it
// on older sdks.
showLoadingScreen(mManifest);
devSupportManager.call("handleReloadJS");
return true;
} else if (didDoubleTapR && shouldReloadFromManifest) {
devSupportManager.call("reloadExpoApp");
return true;
}
}
}
return super.onKeyUp(keyCode, event);
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
mReactInstanceManager.call("onBackPressed");
} else {
super.onBackPressed();
}
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
mReactInstanceManager.onHostPause();
// TODO: use onHostPause(activity)
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
mReactInstanceManager.onHostResume(this, this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
destroyReactInstanceManager();
mHandler.removeCallbacksAndMessages(null);
mLoadingHandler.removeCallbacksAndMessages(null);
EventBus.getDefault().unregister(this);
}
@Override
public void onNewIntent(Intent intent) {
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
try {
mReactInstanceManager.call("onNewIntent", intent);
} catch (Throwable e) {
EXL.e(TAG, e.toString());
super.onNewIntent(intent);
}
} else {
super.onNewIntent(intent);
}
}
public boolean isDebugModeEnabled() {
return ExponentManifest.isDebugModeEnabled(mManifest);
}
protected void destroyReactInstanceManager() {
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
mReactInstanceManager.call("destroy");
}
}
protected void waitForDrawOverOtherAppPermission(String jsBundlePath) {
mJSBundlePath = jsBundlePath;
// TODO: remove once SDK 35 is deprecated
if (isDebugModeEnabled() && Exponent.getInstance().shouldRequestDrawOverOtherAppsPermission(mSDKVersion)) {
new AlertDialog.Builder(this)
.setTitle("Please enable \"Permit drawing over other apps\"")
.setMessage("Click \"ok\" to open settings. Press the back button once you've enabled the setting.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, KernelConstants.OVERLAY_PERMISSION_REQUEST_CODE);
}
})
.setCancelable(false)
.show();
return;
}
startReactInstance();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Exponent.getInstance().onActivityResult(requestCode, resultCode, data);
if (mReactInstanceManager != null && mReactInstanceManager.isNotNull() && !mIsCrashed) {
mReactInstanceManager.call("onActivityResult", this, requestCode, resultCode, data);
}
// Have permission to draw over other apps. Resume loading.
if (requestCode == KernelConstants.OVERLAY_PERMISSION_REQUEST_CODE) {
// startReactInstance() checks isInForeground and onActivityResult is called before onResume,
// so manually set this here.
mIsInForeground = true;
startReactInstance();
}
}
public RNObject startReactInstance(final Exponent.StartReactInstanceDelegate delegate, final String mIntentUri,
final String mSDKVersion, final ExponentNotification mNotification, final boolean mIsShellApp,
final List<? extends Object> extraNativeModules, final List<Package> extraExpoPackages, DevBundleDownloadProgressListener progressListener) {
if (mIsCrashed || !delegate.isInForeground()) {
// Can sometimes get here after an error has occurred. Return early or else we'll hit
// a null pointer at mReactRootView.startReactApplication
return new RNObject("com.facebook.react.ReactInstanceManager");
}
String linkingUri = getLinkingUri();
Map<String, Object> experienceProperties = MapBuilder.<String, Object>of(
MANIFEST_URL_KEY, mManifestUrl,
LINKING_URI_KEY, linkingUri,
INTENT_URI_KEY, mIntentUri,
IS_HEADLESS_KEY, false
);
Exponent.InstanceManagerBuilderProperties instanceManagerBuilderProperties = new Exponent.InstanceManagerBuilderProperties();
instanceManagerBuilderProperties.application = getApplication();
instanceManagerBuilderProperties.jsBundlePath = mJSBundlePath;
instanceManagerBuilderProperties.experienceProperties = experienceProperties;
instanceManagerBuilderProperties.expoPackages = extraExpoPackages;
instanceManagerBuilderProperties.exponentPackageDelegate = delegate.getExponentPackageDelegate();
instanceManagerBuilderProperties.manifest = mManifest;
instanceManagerBuilderProperties.singletonModules = ExponentPackage.getOrCreateSingletonModules(getApplicationContext(), mManifest, extraExpoPackages);
RNObject versionedUtils = new RNObject("host.exp.exponent.VersionedUtils").loadVersion(mSDKVersion);
RNObject builder = versionedUtils.callRecursive("getReactInstanceManagerBuilder", instanceManagerBuilderProperties);
if (ABIVersion.toNumber(mSDKVersion) >= ABIVersion.toNumber("36.0.0")) {
builder.call("setCurrentActivity", this);
}
// ReactNativeInstance is considered to be resumed when it has its activity attached, which is expected to be the case here
builder.call("setInitialLifecycleState", LifecycleState.RESUMED);
if (extraNativeModules != null) {
for (Object nativeModule : extraNativeModules) {
builder.call("addPackage", nativeModule);
}
}
if (delegate.isDebugModeEnabled()) {
String debuggerHost = mManifest.optString(ExponentManifest.MANIFEST_DEBUGGER_HOST_KEY);
String mainModuleName = mManifest.optString(ExponentManifest.MANIFEST_MAIN_MODULE_NAME_KEY);
Exponent.enableDeveloperSupport(mSDKVersion, debuggerHost, mainModuleName, builder);
RNObject devLoadingView = new RNObject("com.facebook.react.devsupport.DevLoadingViewController").loadVersion(mSDKVersion);
devLoadingView.callRecursive("setDevLoadingEnabled", false);
RNObject devBundleDownloadListener = new RNObject("host.exp.exponent.ExponentDevBundleDownloadListener")
.loadVersion(mSDKVersion)
.construct(progressListener);
builder.callRecursive("setDevBundleDownloadListener", devBundleDownloadListener.get());
} else {
checkForReactViews();
}
Bundle bundle = new Bundle();
JSONObject exponentProps = new JSONObject();
if (mNotification != null) {
bundle.putString("notification", mNotification.body); // Deprecated
try {
exponentProps.put("notification", mNotification.toJSONObject("selected"));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
exponentProps.put("manifest", mManifest);
exponentProps.put("shell", mIsShellApp);
exponentProps.put("initialUri", mIntentUri == null ? null : mIntentUri.toString());
exponentProps.put("errorRecovery", ErrorRecoveryManager.getInstance(mExperienceId).popRecoveryProps());
} catch (JSONException e) {
EXL.e(TAG, e);
}
JSONObject metadata = mExponentSharedPreferences.getExperienceMetadata(mExperienceIdString);
if (metadata != null) {
if (metadata.has(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS)) {
try {
exponentProps.put(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS,
metadata.getJSONArray(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS));
} catch (JSONException e) {
e.printStackTrace();
}
metadata.remove(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS);
}
// TODO: fix this. this is the only place that EXPERIENCE_METADATA_UNREAD_REMOTE_NOTIFICATIONS is sent to the experience,
// we need to sent them with the standard notification events so that you can get all the unread notification through an event
// Copy unreadNotifications into exponentProps
if (metadata.has(ExponentSharedPreferences.EXPERIENCE_METADATA_UNREAD_REMOTE_NOTIFICATIONS)) {
try {
JSONArray unreadNotifications = metadata.getJSONArray(ExponentSharedPreferences.EXPERIENCE_METADATA_UNREAD_REMOTE_NOTIFICATIONS);
exponentProps.put(ExponentSharedPreferences.EXPERIENCE_METADATA_UNREAD_REMOTE_NOTIFICATIONS, unreadNotifications);
delegate.handleUnreadNotifications(unreadNotifications);
} catch (JSONException e) {
e.printStackTrace();
}
metadata.remove(ExponentSharedPreferences.EXPERIENCE_METADATA_UNREAD_REMOTE_NOTIFICATIONS);
}
mExponentSharedPreferences.updateExperienceMetadata(mExperienceIdString, metadata);
}
bundle.putBundle("exp", JSONBundleConverter.JSONToBundle(exponentProps));
if (!delegate.isInForeground()) {
return new RNObject("com.facebook.react.ReactInstanceManager");
}
Analytics.markEvent(Analytics.TimedEvent.STARTED_LOADING_REACT_NATIVE);
RNObject mReactInstanceManager = builder.callRecursive("build");
RNObject devSettings = mReactInstanceManager.callRecursive("getDevSupportManager").callRecursive("getDevSettings");
if (devSettings != null) {
devSettings.setField("exponentActivityId", mActivityId);
if ((boolean) devSettings.call("isRemoteJSDebugEnabled")) {
checkForReactViews();
}
}
mReactInstanceManager.onHostResume(this, this);
mReactRootView.call("startReactApplication",
mReactInstanceManager.get(),
mManifest.optString(ExponentManifest.MANIFEST_APP_KEY_KEY, KernelConstants.DEFAULT_APPLICATION_KEY),
initialProps(bundle));
return mReactInstanceManager;
}
protected boolean shouldShowErrorScreen(ExponentErrorMessage errorMessage) {
if (mIsLoading) {
// Don't hit ErrorRecoveryManager until bridge is initialized.
// This is the same on iOS.
return true;
}
ErrorRecoveryManager errorRecoveryManager = ErrorRecoveryManager.getInstance(mExperienceId);
errorRecoveryManager.markErrored();
if (errorRecoveryManager.shouldReloadOnError()) {
if (!KernelProvider.getInstance().reloadVisibleExperience(mManifestUrl)) {
// Kernel couldn't reload, show error screen
return true;
}
sErrorQueue.clear();
try {
JSONObject eventProperties = new JSONObject();
eventProperties.put(Analytics.USER_ERROR_MESSAGE, errorMessage.userErrorMessage());
eventProperties.put(Analytics.DEVELOPER_ERROR_MESSAGE, errorMessage.developerErrorMessage());
eventProperties.put(Analytics.MANIFEST_URL, mManifestUrl);
Analytics.logEvent(Analytics.ERROR_RELOADED, eventProperties);
} catch (Exception e) {
EXL.e(TAG, e.getMessage());
}
return false;
} else {
return true;
}
}
public void onEventMainThread(KernelConstants.AddedExperienceEventEvent event) {
if (mManifestUrl != null && mManifestUrl.equals(event.manifestUrl)) {
pollForEventsToSendToRN();
}
}
public void onEvent(BaseExperienceActivity.ExperienceContentLoaded event) {
fadeLoadingScreen();
}
private void pollForEventsToSendToRN() {
if (mManifestUrl == null) {
return;
}
try {
RNObject rctDeviceEventEmitter = new RNObject("com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter");
rctDeviceEventEmitter.loadVersion(mDetachSdkVersion);
RNObject existingEmitter = mReactInstanceManager.callRecursive("getCurrentReactContext")
.callRecursive("getJSModule", rctDeviceEventEmitter.rnClass());
if (existingEmitter != null) {
Set<KernelConstants.ExperienceEvent> events = KernelProvider.getInstance().consumeExperienceEvents(mManifestUrl);
for (KernelConstants.ExperienceEvent event : events) {
existingEmitter.call("emit", event.eventName, event.eventPayload);
}
}
} catch (Throwable e) {
EXL.e(TAG, e);
}
}
// for getting global permission
@Override
public int checkSelfPermission(String permission) {
return super.checkPermission(permission, Process.myPid(), Process.myUid());
}
@Override
public boolean shouldShowRequestPermissionRationale(@NonNull String permission) {
// in scoped application we don't have `don't ask again` button
if (!Constants.isStandaloneApp() && checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return super.shouldShowRequestPermissionRationale(permission);
}
@Override
public void requestPermissions(final String[] permissions, final int requestCode, final PermissionListener listener) {
if (requestCode == EXPONENT_PERMISSIONS_REQUEST) {
mScopedPermissionsRequester = new ScopedPermissionsRequester(mExperienceId);
mScopedPermissionsRequester.requestPermissions(this, mManifest.optString(ExponentManifest.MANIFEST_NAME_KEY), permissions, listener);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
super.requestPermissions(permissions, requestCode);
}
}
@Override
public void onRequestPermissionsResult(final int requestCode, final String[] permissions, @NonNull final int[] grantResults) {
if (requestCode == EXPONENT_PERMISSIONS_REQUEST) {
// TODO: remove once SDK 35 is deprecated
String sdkVersion = "0.0.0";
try {
sdkVersion = mManifest.getString(ExponentManifest.MANIFEST_SDK_VERSION_KEY);
} catch (JSONException e) {
e.printStackTrace();
}
if (ABIVersion.toNumber(sdkVersion) < ABIVersion.toNumber("36.0.0")) {
Exponent.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
} else {
if (permissions.length > 0 && grantResults.length == permissions.length && mScopedPermissionsRequester != null) {
mScopedPermissionsRequester.onRequestPermissionsResult(permissions, grantResults);
mScopedPermissionsRequester = null;
}
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
// for getting scoped permission
@Override
public int checkPermission(final String permission, final int pid, final int uid) {
int globalResult = super.checkPermission(permission, pid, uid);
return mExpoKernelServiceRegistry.getPermissionsKernelService().getPermissions(globalResult, getPackageManager(), permission, mExperienceId);
}
public RNObject getDevSupportManager() {
return mReactInstanceManager.callRecursive("getDevSupportManager");
}
// deprecated in favor of Expo.Linking.makeUrl
// TODO: remove this
private String getLinkingUri() {
if (Constants.SHELL_APP_SCHEME != null) {
return Constants.SHELL_APP_SCHEME + "://";
} else {
Uri uri = Uri.parse(mManifestUrl);
String host = uri.getHost();
if (host != null && (host.equals("exp.host") || host.equals("expo.io") || host.equals("exp.direct") || host.equals("expo.test") ||
host.endsWith(".exp.host") || host.endsWith(".expo.io") || host.endsWith(".exp.direct") || host.endsWith(".expo.test"))) {
List<String> pathSegments = uri.getPathSegments();
Uri.Builder builder = uri.buildUpon();
builder.path(null);
for (String segment : pathSegments) {
if (ExponentManifest.DEEP_LINK_SEPARATOR.equals(segment)) {
break;
}
builder.appendEncodedPath(segment);
}
return builder.appendEncodedPath(ExponentManifest.DEEP_LINK_SEPARATOR_WITH_SLASH).build().toString();
} else {
return mManifestUrl;
}
}
}
}