/
ExperienceActivity.java
860 lines (718 loc) · 29.1 KB
/
ExperienceActivity.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
// Copyright 2015-present 650 Industries. All rights reserved.
package host.exp.exponent.experience;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RemoteViews;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.soloader.SoLoader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import javax.inject.Inject;
import de.greenrobot.event.EventBus;
import expo.core.interfaces.Package;
import host.exp.exponent.ABIVersion;
import host.exp.exponent.AppLoader;
import host.exp.exponent.Constants;
import host.exp.exponent.ExponentIntentService;
import host.exp.exponent.ExponentManifest;
import host.exp.exponent.LauncherActivity;
import host.exp.exponent.RNObject;
import host.exp.exponent.analytics.Analytics;
import host.exp.exponent.analytics.EXL;
import host.exp.exponent.branch.BranchManager;
import host.exp.exponent.di.NativeModuleDepsProvider;
import host.exp.exponent.kernel.ExperienceId;
import host.exp.exponent.kernel.ExponentError;
import host.exp.exponent.kernel.ExponentUrls;
import host.exp.exponent.kernel.Kernel;
import host.exp.exponent.kernel.KernelConstants;
import host.exp.exponent.kernel.KernelProvider;
import host.exp.exponent.notifications.ExponentNotification;
import host.exp.exponent.notifications.ExponentNotificationManager;
import host.exp.exponent.notifications.NotificationConstants;
import host.exp.exponent.notifications.PushNotificationHelper;
import host.exp.exponent.notifications.ReceivedNotificationEvent;
import host.exp.exponent.storage.ExponentSharedPreferences;
import host.exp.exponent.utils.AsyncCondition;
import host.exp.exponent.utils.ExperienceActivityUtils;
import host.exp.expoview.Exponent;
import host.exp.expoview.R;
import versioned.host.exp.exponent.ExponentPackageDelegate;
import versioned.host.exp.exponent.ReactUnthemedRootView;
import static host.exp.exponent.kernel.KernelConstants.IS_OPTIMISTIC_KEY;
import static host.exp.exponent.kernel.KernelConstants.MANIFEST_URL_KEY;
public class ExperienceActivity extends BaseExperienceActivity implements Exponent.StartReactInstanceDelegate {
// Override
public List<ReactPackage> reactPackages() {
return null;
}
public List<Package> expoPackages() {
return Collections.emptyList();
}
@Override
public ExponentPackageDelegate getExponentPackageDelegate() {
return null;
}
public boolean forceUnversioned() {
return false;
}
private static final String TAG = ExperienceActivity.class.getSimpleName();
private static final String KERNEL_STARTED_RUNNING_KEY = "experienceActivityKernelDidLoad";
private static final String NUX_REACT_MODULE_NAME = "ExperienceNuxApp";
private static final int NOTIFICATION_ID = 10101;
private static String READY_FOR_BUNDLE = "readyForBundle";
private ReactUnthemedRootView mNuxOverlayView;
private ExponentNotification mNotification;
private ExponentNotification mTempNotification;
private boolean mIsShellApp;
private String mIntentUri;
private boolean mIsReadyForBundle;
private RemoteViews mNotificationRemoteViews;
private Handler mNotificationAnimationHandler;
private Runnable mNotificationAnimator;
private int mNotificationAnimationFrame;
private NotificationCompat.Builder mNotificationBuilder;
private boolean mIsLoadExperienceAllowedToRun = false;
private boolean mShouldShowLoadingScreenWithOptimisticManifest = false;
@Inject
ExponentManifest mExponentManifest;
private DevBundleDownloadProgressListener mDevBundleDownloadProgressListener = new DevBundleDownloadProgressListener() {
@Override
public void onProgress(final @Nullable String status, final @Nullable Integer done, final @Nullable Integer total) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateLoadingProgress(status, done, total);
}
});
}
@Override
public void onSuccess() {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
checkForReactViews();
}
});
}
@Override
public void onFailure(Exception error) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
stopLoading();
}
});
}
};
/*
*
* Lifecycle
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIsLoadExperienceAllowedToRun = true;
mShouldShowLoadingScreenWithOptimisticManifest = true;
NativeModuleDepsProvider.getInstance().inject(ExperienceActivity.class, this);
EventBus.getDefault().registerSticky(this);
mActivityId = Exponent.getActivityId();
// TODO: audit this now that kernel logic is in Java
boolean shouldOpenImmediately = true;
// If our activity was killed for memory reasons or because of "Don't keep activities",
// try to reload manifest using the savedInstanceState
if (savedInstanceState != null) {
String manifestUrl = savedInstanceState.getString(MANIFEST_URL_KEY);
if (manifestUrl != null) {
mManifestUrl = manifestUrl;
}
}
// On cold boot to experience, we're given this information from the Java kernel, instead of
// the JS kernel.
Bundle bundle = getIntent().getExtras();
if (bundle != null && mManifestUrl == null) {
String manifestUrl = bundle.getString(MANIFEST_URL_KEY);
if (manifestUrl != null) {
mManifestUrl = manifestUrl;
}
// Don't want to get here if savedInstanceState has manifestUrl. Only care about
// IS_OPTIMISTIC_KEY the first time onCreate is called.
boolean isOptimistic = bundle.getBoolean(IS_OPTIMISTIC_KEY);
if (isOptimistic) {
shouldOpenImmediately = false;
}
}
if (mManifestUrl != null && shouldOpenImmediately) {
new AppLoader(mManifestUrl) {
@Override
public void onOptimisticManifest(final JSONObject optimisticManifest) {
Exponent.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
setLoadingScreenManifest(optimisticManifest);
}
});
}
@Override
public void onManifestCompleted(final JSONObject manifest) {
Exponent.getInstance().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String bundleUrl = ExponentUrls.toHttp(manifest.getString("bundleUrl"));
setManifest(mManifestUrl, manifest, bundleUrl, null);
} catch (JSONException e) {
mKernel.handleError(e);
}
}
});
}
@Override
public void onBundleCompleted(String localBundlePath) {
setBundle(localBundlePath);
}
@Override
public void emitEvent(JSONObject params) {
emitUpdatesEvent(params);
}
@Override
public void onError(Exception e) {
mKernel.handleError(e);
}
@Override
public void onError(String e) {
mKernel.handleError(e);
}
}.start();
}
mKernel.setOptimisticActivity(this, getTaskId());
}
@Override
protected void onResume() {
super.onResume();
soloaderInit();
addNotification(null);
Analytics.logEventWithManifestUrl(Analytics.EXPERIENCE_APPEARED, mManifestUrl);
registerForNotifications();
}
public void soloaderInit() {
if (mDetachSdkVersion != null) {
try {
new RNObject("com.facebook.soloader.SoLoader")
.loadVersion(mDetachSdkVersion)
.callStatic("init", this, false);
} catch (Throwable e) {
// Starting with SDK 8, SoLoader moved out into a library, so it isn't versioned anymore
SoLoader.init(this, false);
}
}
}
public void shouldCheckOptions() {
if (mManifestUrl != null && mKernel.hasOptionsForManifestUrl(mManifestUrl)) {
handleOptions(mKernel.popOptionsForManifestUrl(mManifestUrl));
}
// this is old code from before `Expo.Notifications.dismissAllNotificationsAsync` existed
// since removing it is a breaking change (people have to call ^^^ explicitly if they want
// this behavior) we only do it starting in SDK 28
// TODO: eric: remove this once SDK 27 is phased out
if (mSDKVersion != null && ABIVersion.toNumber(mSDKVersion) < ABIVersion.toNumber("28.0.0")) {
clearNotifications();
}
}
// TODO: eric: remove this once SDK 27 is phased out
protected void clearNotifications() {
String experienceId = mManifest.optString(ExponentManifest.MANIFEST_ID_KEY);
if (experienceId != null) {
ExponentNotificationManager manager = new ExponentNotificationManager(this);
manager.cancelAll(experienceId);
}
}
@Override
protected void onPause() {
super.onPause();
removeNotification();
Analytics.clearTimedEvents();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(MANIFEST_URL_KEY, mManifestUrl);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Uri uri = intent.getData();
if (uri != null) {
handleUri(uri.toString());
}
} else {
// Always just restart this activity. Don't call Activity.recreate() because that uses
// the old savedInstanceState.
finish();
startActivity(intent);
}
}
public void onEventMainThread(Kernel.KernelStartedRunningEvent event) {
AsyncCondition.notify(KERNEL_STARTED_RUNNING_KEY);
}
@Override
protected void onDoneLoading() {
Analytics.markEvent(Analytics.TimedEvent.FINISHED_LOADING_REACT_NATIVE);
Analytics.sendTimedEvents(mManifestUrl);
}
/*
*
* Experience Loading
*
*/
public void setLoadingScreenManifest(final JSONObject manifest) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isInForeground()) {
return;
}
if (!mShouldShowLoadingScreenWithOptimisticManifest) {
return;
}
// grab SDK version from optimisticManifest -- in this context we just need to know ensure it's above 5.0.0 (which it should always be)
String optimisticSdkVersion = manifest.optString(ExponentManifest.MANIFEST_SDK_VERSION_KEY);
ExperienceActivityUtils.setWindowTransparency(optimisticSdkVersion, manifest, ExperienceActivity.this);
showLoadingScreen(manifest);
ExperienceActivityUtils.setTaskDescription(mExponentManifest, manifest, ExperienceActivity.this);
}
});
}
public void setManifest(String manifestUrl, final JSONObject manifest, final String bundleUrl, final JSONObject kernelOptions) {
if (!isInForeground()) {
return;
}
if (!mIsLoadExperienceAllowedToRun) {
return;
}
// Only want to run once per onCreate. There are some instances with ShellAppActivity where this would be called
// twice otherwise. Turn on "Don't keep activites", trigger a notification, background the app, and then
// press on the notification in a shell app to see this happen.
mIsLoadExperienceAllowedToRun = false;
mIsReadyForBundle = false;
mManifestUrl = manifestUrl;
mManifest = manifest;
new ExponentNotificationManager(this).maybeCreateNotificationChannelGroup(mManifest);
Kernel.ExperienceActivityTask task = mKernel.getExperienceActivityTask(mManifestUrl);
task.taskId = getTaskId();
task.experienceActivity = new WeakReference<>(this);
task.activityId = mActivityId;
task.bundleUrl = bundleUrl;
mSDKVersion = manifest.optString(ExponentManifest.MANIFEST_SDK_VERSION_KEY);
mIsShellApp = manifestUrl.equals(Constants.INITIAL_URL);
// Sometime we want to release a new version without adding a new .aar. Use TEMPORARY_ABI_VERSION
// to point to the unversioned code in ReactAndroid.
if (Constants.TEMPORARY_ABI_VERSION != null && Constants.TEMPORARY_ABI_VERSION.equals(mSDKVersion)) {
mSDKVersion = RNObject.UNVERSIONED;
}
mDetachSdkVersion = forceUnversioned() ? RNObject.UNVERSIONED : mSDKVersion;
if (!RNObject.UNVERSIONED.equals(mSDKVersion)) {
boolean isValidVersion = false;
for (final String version : Constants.SDK_VERSIONS_LIST) {
if (version.equals(mSDKVersion)) {
isValidVersion = true;
break;
}
}
if (!isValidVersion) {
KernelProvider.getInstance().handleError(mSDKVersion + " is not a valid SDK version. Options are " +
TextUtils.join(", ", Constants.SDK_VERSIONS_LIST) + ", " + RNObject.UNVERSIONED + ".");
return;
}
}
soloaderInit();
try {
mExperienceIdString = manifest.getString(ExponentManifest.MANIFEST_ID_KEY);
mExperienceId = ExperienceId.create(mExperienceIdString);
AsyncCondition.notify(KernelConstants.EXPERIENCE_ID_SET_FOR_ACTIVITY_KEY);
} catch (JSONException e) {
KernelProvider.getInstance().handleError("No ID found in manifest.");
return;
}
mIsCrashed = false;
Analytics.logEventWithManifestUrlSdkVersion(Analytics.LOAD_EXPERIENCE, mManifestUrl, mSDKVersion);
ExperienceActivityUtils.updateOrientation(mManifest, this);
addNotification(kernelOptions);
ExponentNotification notificationObject = null;
if (mKernel.hasOptionsForManifestUrl(manifestUrl)) {
KernelConstants.ExperienceOptions options = mKernel.popOptionsForManifestUrl(manifestUrl);
// if the kernel has an intent for our manifest url, that's the intent that triggered
// the loading of this experience.
if (options.uri != null) {
mIntentUri = options.uri;
}
notificationObject = options.notificationObject;
}
// if we have an embedded initial url, we never need any part of this in the initial url
// passed to the JS, so we check for that and filter it out here.
// this can happen in dev mode on a detached app, for example, because the intent will have
// a url like customscheme://localhost:19000 but we don't care about the localhost:19000 part.
if (mIntentUri == null || mIntentUri.equals(Constants.INITIAL_URL)) {
if (Constants.SHELL_APP_SCHEME != null) {
mIntentUri = Constants.SHELL_APP_SCHEME + "://";
} else {
mIntentUri = mManifestUrl;
}
}
final ExponentNotification finalNotificationObject = notificationObject;
BranchManager.handleLink(this, mIntentUri, mDetachSdkVersion);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isInForeground()) {
return;
}
if (mReactInstanceManager.isNotNull()) {
mReactInstanceManager.onHostDestroy();
mReactInstanceManager.assign(null);
}
// ReactUnthemedRootView was moved after ABI 5.0.0 as part of a refactor
if (ABIVersion.toNumber(mDetachSdkVersion) <= ABIVersion.toNumber("5.0.0")) {
mReactRootView = new RNObject("host.exp.exponent.views.ReactUnthemedRootView");
} else {
mReactRootView = new RNObject("host.exp.exponent.ReactUnthemedRootView");
}
mReactRootView.loadVersion(mDetachSdkVersion).construct(ExperienceActivity.this);
setView((View) mReactRootView.get());
String id;
try {
id = Exponent.getInstance().encodeExperienceId(mExperienceIdString);
} catch (UnsupportedEncodingException e) {
KernelProvider.getInstance().handleError("Can't URL encode manifest ID");
return;
}
boolean hasCachedBundle;
if (isDebugModeEnabled()) {
hasCachedBundle = false;
mNotification = finalNotificationObject;
waitForDrawOverOtherAppPermission("");
} else {
mTempNotification = finalNotificationObject;
mIsReadyForBundle = true;
AsyncCondition.notify(READY_FOR_BUNDLE);
}
ExperienceActivityUtils.setWindowTransparency(mDetachSdkVersion, manifest, ExperienceActivity.this);
showLoadingScreen(manifest);
ExperienceActivityUtils.setTaskDescription(mExponentManifest, manifest, ExperienceActivity.this);
handleExperienceOptions(kernelOptions);
}
});
}
public void setBundle(final String localBundlePath) {
// by this point, setManifest should have also been called, so prevent
// setLoadingScreenManifest from showing a rogue loading screen
mShouldShowLoadingScreenWithOptimisticManifest = false;
if (!isDebugModeEnabled()) {
final boolean finalIsReadyForBundle = mIsReadyForBundle;
AsyncCondition.wait(READY_FOR_BUNDLE, new AsyncCondition.AsyncConditionListener() {
@Override
public boolean isReady() {
return finalIsReadyForBundle;
}
@Override
public void execute() {
mNotification = mTempNotification;
mTempNotification = null;
waitForDrawOverOtherAppPermission(localBundlePath);
AsyncCondition.remove(READY_FOR_BUNDLE);
}
});
}
}
public void onEventMainThread(ReceivedNotificationEvent event) {
if (ABIVersion.toNumber(mDetachSdkVersion) < ABIVersion.toNumber("8.0.0")) {
return;
}
if (event.experienceId.equals(mExperienceIdString)) {
try {
RNObject rctDeviceEventEmitter = new RNObject("com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter");
rctDeviceEventEmitter.loadVersion(mDetachSdkVersion);
mReactInstanceManager.callRecursive("getCurrentReactContext")
.callRecursive("getJSModule", rctDeviceEventEmitter.rnClass())
.call("emit", "Exponent.notification", event.toWriteableMap(mDetachSdkVersion, "received"));
} catch (Throwable e) {
EXL.e(TAG, e);
}
}
}
public void handleOptions(KernelConstants.ExperienceOptions options) {
try {
if (options.uri != null) {
handleUri(options.uri);
String uri = options.uri;
if (uri != null) {
RNObject rctDeviceEventEmitter = new RNObject("com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter");
rctDeviceEventEmitter.loadVersion(mDetachSdkVersion);
mReactInstanceManager.callRecursive("getCurrentReactContext")
.callRecursive("getJSModule", rctDeviceEventEmitter.rnClass())
.call("emit", "Exponent.openUri", uri);
}
BranchManager.handleLink(this, options.uri, mDetachSdkVersion);
}
if ((options.notification != null || options.notificationObject != null) && mDetachSdkVersion != null) {
if (ABIVersion.toNumber(mDetachSdkVersion) < ABIVersion.toNumber("8.0.0")) {
// TODO: kill
RNObject rctDeviceEventEmitter = new RNObject("com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter");
rctDeviceEventEmitter.loadVersion(mDetachSdkVersion);
mReactInstanceManager.callRecursive("getCurrentReactContext")
.callRecursive("getJSModule", rctDeviceEventEmitter.rnClass())
.call("emit", "Exponent.notification", options.notification);
} else {
RNObject rctDeviceEventEmitter = new RNObject("com.facebook.react.modules.core.DeviceEventManagerModule$RCTDeviceEventEmitter");
rctDeviceEventEmitter.loadVersion(mDetachSdkVersion);
mReactInstanceManager.callRecursive("getCurrentReactContext")
.callRecursive("getJSModule", rctDeviceEventEmitter.rnClass())
.call("emit", "Exponent.notification", options.notificationObject.toWriteableMap(mDetachSdkVersion, "selected"));
}
}
} catch (Throwable e) {
EXL.e(TAG, e);
}
}
private void handleUri(String uri) {
// Emits a "url" event to the Linking event emitter
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
super.onNewIntent(intent);
}
public void emitUpdatesEvent(JSONObject params) {
KernelProvider.getInstance().addEventForExperience(mManifestUrl, new KernelConstants.ExperienceEvent(AppLoader.UPDATES_EVENT_NAME, params.toString()));
}
@Override
public boolean isDebugModeEnabled() {
return ExponentManifest.isDebugModeEnabled(mManifest);
}
@Override
protected void startReactInstance() {
Exponent.getInstance().testPackagerStatus(isDebugModeEnabled(), mManifest, new Exponent.PackagerStatusCallback() {
@Override
public void onSuccess() {
mReactInstanceManager = startReactInstance(ExperienceActivity.this, mIntentUri, null, mDetachSdkVersion, mNotification, mIsShellApp, reactPackages(), expoPackages(), mDevBundleDownloadProgressListener);
}
@Override
public void onFailure(final String errorMessage) {
KernelProvider.getInstance().handleError(errorMessage);
}
});
}
@Override
public void handleUnreadNotifications(JSONArray unreadNotifications) {
PushNotificationHelper pushNotificationHelper = PushNotificationHelper.getInstance();
if (pushNotificationHelper != null) {
pushNotificationHelper.removeNotifications(this, unreadNotifications);
}
}
public void onEvent(BaseExperienceActivity.ExperienceDoneLoadingEvent event) {
// On cold boot to this experience, wait until we're done loading to load the kernel.
mKernel.startJSKernel();
}
/*
*
* Notification
*
*/
private void addNotification(final JSONObject options) {
if (mManifestUrl == null || mManifest == null) {
return;
}
String name = mManifest.optString(ExponentManifest.MANIFEST_NAME_KEY, null);
if (name == null) {
return;
}
if (!mManifest.optBoolean(ExponentManifest.MANIFEST_SHOW_EXPONENT_NOTIFICATION_KEY) && mIsShellApp) {
return;
}
RemoteViews remoteViews = new RemoteViews(getPackageName(), mIsShellApp ? R.layout.notification_shell_app : R.layout.notification);
remoteViews.setCharSequence(R.id.home_text_button, "setText", name);
// Home
Intent homeIntent = new Intent(this, LauncherActivity.class);
remoteViews.setOnClickPendingIntent(R.id.home_image_button, PendingIntent.getActivity(this, 0,
homeIntent, 0));
// Info
// Doing PendingIntent.getActivity doesn't work here - it opens the activity in the main
// stack and not in the experience's stack
remoteViews.setOnClickPendingIntent(R.id.home_text_button, PendingIntent.getService(this, 0,
ExponentIntentService.getActionInfoScreen(this, mManifestUrl), PendingIntent.FLAG_UPDATE_CURRENT));
if (!mIsShellApp) {
// Share
// TODO: add analytics
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, name + " on Exponent");
shareIntent.putExtra(Intent.EXTRA_TEXT, mManifestUrl);
remoteViews.setOnClickPendingIntent(R.id.share_button, PendingIntent.getActivity(this, 0,
Intent.createChooser(shareIntent, "Share a link to " + name), PendingIntent.FLAG_UPDATE_CURRENT));
// Save
remoteViews.setOnClickPendingIntent(R.id.save_button, PendingIntent.getService(this, 0,
ExponentIntentService.getActionSaveExperience(this, mManifestUrl),
PendingIntent.FLAG_UPDATE_CURRENT));
}
// Reload
remoteViews.setOnClickPendingIntent(R.id.reload_button, PendingIntent.getService(this, 0,
ExponentIntentService.getActionReloadExperience(this, mManifestUrl), PendingIntent.FLAG_UPDATE_CURRENT));
mNotificationRemoteViews = remoteViews;
// Build the actual notification
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
new ExponentNotificationManager(this).maybeCreateExpoPersistentNotificationChannel();
mNotificationBuilder = new NotificationCompat.Builder(this, NotificationConstants.NOTIFICATION_EXPERIENCE_CHANNEL_ID)
.setContent(mNotificationRemoteViews)
.setSmallIcon(R.drawable.notification_icon)
.setShowWhen(false)
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mNotificationBuilder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
}
notificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
}
private void removeNotification() {
mNotificationRemoteViews = null;
mNotificationBuilder = null;
removeNotification(this);
}
private void handleExperienceOptions(JSONObject options) {
if (options != null) {
try {
if (options.getBoolean(KernelConstants.OPTION_LOAD_NUX_KEY) && !Constants.DISABLE_NUX) {
//addNuxView();
}
} catch (JSONException e) {
EXL.e(TAG, e.getMessage());
}
}
}
private void addNuxView() {
AsyncCondition.wait(KERNEL_STARTED_RUNNING_KEY, new AsyncCondition.AsyncConditionListener() {
@Override
public boolean isReady() {
return mKernel.isRunning();
}
@Override
public void execute() {
ReactInstanceManager kernelReactInstanceManager = mKernel.getReactInstanceManager();
mNuxOverlayView = new ReactUnthemedRootView(ExperienceActivity.this);
mNuxOverlayView.startReactApplication(
kernelReactInstanceManager,
NUX_REACT_MODULE_NAME,
null
);
kernelReactInstanceManager.onHostResume(ExperienceActivity.this, ExperienceActivity.this);
addView(mNuxOverlayView);
Analytics.logEvent("NUX_EXPERIENCE_OVERLAY_SHOWN");
}
});
}
public static void removeNotification(Context context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
}
public void onNotificationAction() {
dismissNuxViewIfVisible(true);
}
/**
* @param isFromNotification true if this is the result of the user taking an
* action in the notification view.
*/
public void dismissNuxViewIfVisible(final boolean isFromNotification) {
if (mNuxOverlayView != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(500);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
removeViewFromParent(mNuxOverlayView);
mNuxOverlayView = null;
JSONObject eventProperties = new JSONObject();
try {
eventProperties.put("IS_FROM_NOTIFICATION", isFromNotification);
} catch (JSONException e) {
EXL.e(TAG, e.getMessage());
}
Analytics.logEvent("NUX_EXPERIENCE_OVERLAY_DISMISSED", eventProperties);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
mNuxOverlayView.startAnimation(fadeOut);
}
});
}
}
/*
*
* Errors
*
*/
@Override
protected void onError(final Intent intent) {
if (mManifestUrl != null) {
intent.putExtra(ErrorActivity.MANIFEST_URL_KEY, mManifestUrl);
}
}
@Override
protected void onError(final ExponentError error) {
if (mManifest == null) {
return;
}
JSONObject errorJson = error.toJSONObject();
if (errorJson == null) {
return;
}
String experienceId = mManifest.optString(ExponentManifest.MANIFEST_ID_KEY);
if (experienceId == null) {
return;
}
JSONObject metadata = mExponentSharedPreferences.getExperienceMetadata(experienceId);
if (metadata == null) {
metadata = new JSONObject();
}
JSONArray errors = metadata.optJSONArray(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS);
if (errors == null) {
errors = new JSONArray();
}
errors.put(errorJson);
try {
metadata.put(ExponentSharedPreferences.EXPERIENCE_METADATA_LAST_ERRORS, errors);
mExponentSharedPreferences.updateExperienceMetadata(experienceId, metadata);
} catch (JSONException e) {
e.printStackTrace();
}
}
public String getExperienceId() {
return mExperienceIdString;
}
}