This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
/
WXSDKInstance.java
1681 lines (1427 loc) · 53.2 KB
/
WXSDKInstance.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.adapter.IDrawableLoader;
import com.taobao.weex.adapter.IWXHttpAdapter;
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.adapter.IWXUserTrackAdapter;
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.appfram.websocket.IWebSocketAdapter;
import com.taobao.weex.bridge.NativeInvokeHelper;
import com.taobao.weex.bridge.SimpleJSCallback;
import com.taobao.weex.bridge.WXBridgeManager;
import com.taobao.weex.bridge.WXModuleManager;
import com.taobao.weex.common.Constants;
import com.taobao.weex.common.Destroyable;
import com.taobao.weex.common.IWXDebugProxy;
import com.taobao.weex.common.OnWXScrollListener;
import com.taobao.weex.common.WXErrorCode;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.common.WXPerformance;
import com.taobao.weex.common.WXRefreshData;
import com.taobao.weex.common.WXRenderStrategy;
import com.taobao.weex.common.WXRequest;
import com.taobao.weex.common.WXResponse;
import com.taobao.weex.dom.DomContext;
import com.taobao.weex.dom.WXDomHandler;
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.dom.WXDomTask;
import com.taobao.weex.dom.WXEvent;
import com.taobao.weex.http.WXHttpUtil;
import com.taobao.weex.tracing.WXTracing;
import com.taobao.weex.ui.component.NestedContainer;
import com.taobao.weex.ui.component.WXBasicComponentType;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXComponentFactory;
import com.taobao.weex.ui.view.WXScrollView;
import com.taobao.weex.ui.view.WXScrollView.WXScrollViewListener;
import com.taobao.weex.utils.Trace;
import com.taobao.weex.utils.WXFileUtils;
import com.taobao.weex.utils.WXJsonUtils;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXReflectionUtils;
import com.taobao.weex.utils.WXViewUtils;
import com.taobao.weex.WXSDKEngine;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import static com.taobao.weex.http.WXHttpUtil.KEY_USER_AGENT;
/**
* Each instance of WXSDKInstance represents an running weex instance.
* It can be a pure weex view, or mixed with native view
*/
public class WXSDKInstance implements IWXActivityStateListener,DomContext, View.OnLayoutChangeListener {
//Performance
public boolean mEnd = false;
public static final String BUNDLE_URL = "bundleUrl";
private IWXUserTrackAdapter mUserTrackAdapter;
private IWXRenderListener mRenderListener;
private IWXStatisticsListener mStatisticsListener;
/** package **/ Context mContext;
private final String mInstanceId;
private RenderContainer mRenderContainer;
private WXComponent mRootComp;
private boolean mRendered;
private WXRefreshData mLastRefreshData;
private NestedInstanceInterceptor mNestedInstanceInterceptor;
private String mBundleUrl = "";
private boolean isDestroy=false;
private Map<String,Serializable> mUserTrackParams;
private NativeInvokeHelper mNativeInvokeHelper;
private boolean isCommit=false;
private WXGlobalEventReceiver mGlobalEventReceiver=null;
private boolean trackComponent;
private boolean enableLayerType = true;
private boolean mNeedValidate = false;
private boolean mNeedReLoad = false;
private static volatile int mViewPortWidth = 750;
private int mInstanceViewPortWidth = 750;
public long mRenderStartNanos;
public int mExecJSTraceId = WXTracing.nextId();
/**
* Render strategy.
*/
private WXRenderStrategy mRenderStrategy = WXRenderStrategy.APPEND_ASYNC;
/**
* Render start time
*/
private long mRenderStartTime;
/**
* Refresh start time
*/
private long mRefreshStartTime;
private WXPerformance mWXPerformance;
private ScrollView mScrollView;
private WXScrollViewListener mWXScrollViewListener;
private List<OnWXScrollListener> mWXScrollListeners;
/**
* whether we are in preRender mode
* */
private volatile boolean isPreRenderMode;
private LayoutFinishListener mLayoutFinishListener;
private boolean mCurrentGround = false;
private ComponentObserver mComponentObserver;
/**
* If anchor is created manually(etc. define a layout xml resource ),
* be aware do not add it to twice when {@link IWXRenderListener#onViewCreated(WXSDKInstance, View)}.
* @param a
*/
public void setRenderContainer(RenderContainer a){
if(a != null) {
a.setSDKInstance(this);
a.addOnLayoutChangeListener(this);
}
mRenderContainer = a;
}
private int mMaxDeepLayer;
public boolean isTrackComponent() {
return trackComponent;
}
public void setTrackComponent(boolean trackComponent) {
this.trackComponent = trackComponent;
}
/**
* Tell whether it is enabled to change the layerType
* {@link android.view.View#setLayerType(int, Paint)}
* @return True for enable to change the layerType of component, false otherwise. The default
* is True
*/
public boolean isLayerTypeEnabled() {
return enableLayerType;
}
/**
* Enable the ability of changing layerType. e.g. {@link android.view.View#setLayerType(int, Paint)}
* Disable the ability of changing layerType will have tremendous <strong>performance
* punishment</strong>.
*
* <strong>Do not</strong> set this to false unless you know exactly what you are doing.
* @param enable True for enable to change the layerType of component, false otherwise. The default
* is True
*/
public void enableLayerType(boolean enable) {
enableLayerType = enable;
}
public boolean isNeedValidate() {
return mNeedValidate;
}
public boolean isNeedReLoad() {
return mNeedReLoad;
}
public void setNeedLoad(boolean load) {
mNeedReLoad = load;
}
/*
* Warning: use setInstanceViewPortWidth instead.
* store custom ViewPort Width
*/
@Deprecated
public void setViewPortWidth(int viewPortWidth) {
mViewPortWidth = viewPortWidth;
}
/**
* Warning: use getInstanceViewPortWidth instead.
* @return
*/
@Deprecated
public static int getViewPortWidth() {
return mViewPortWidth;
}
public void setInstanceViewPortWidth(int instanceViewPortWidth) {
this.mInstanceViewPortWidth = instanceViewPortWidth;
}
public int getInstanceViewPortWidth(){
return mInstanceViewPortWidth;
}
public interface OnInstanceVisibleListener{
void onAppear();
void onDisappear();
}
private List<OnInstanceVisibleListener> mVisibleListeners = new ArrayList<>();
public WXSDKInstance(Context context) {
mInstanceId = WXSDKManager.getInstance().generateInstanceId();
init(context);
}
/**
* For unittest only.
*/
WXSDKInstance(Context context,String id) {
mInstanceId = id;
init(context);
}
public WXComponent getRootComponent() {
return mRootComp;
}
public void setNestedInstanceInterceptor(NestedInstanceInterceptor interceptor){
mNestedInstanceInterceptor = interceptor;
}
public final WXSDKInstance createNestedInstance(NestedContainer container){
WXSDKInstance sdkInstance = newNestedInstance();
if(mNestedInstanceInterceptor != null){
mNestedInstanceInterceptor.onCreateNestInstance(sdkInstance,container);
}
return sdkInstance;
}
protected WXSDKInstance newNestedInstance() {
return new WXSDKInstance(mContext);
}
public void addOnInstanceVisibleListener(OnInstanceVisibleListener l){
mVisibleListeners.add(l);
}
public void removeOnInstanceVisibleListener(OnInstanceVisibleListener l){
mVisibleListeners.remove(l);
}
public void init(Context context) {
mContext = context;
mNativeInvokeHelper = new NativeInvokeHelper(mInstanceId);
mWXPerformance = new WXPerformance();
mWXPerformance.WXSDKVersion = WXEnvironment.WXSDK_VERSION;
mWXPerformance.JSLibInitTime = WXEnvironment.sJSLibInitTime;
mUserTrackAdapter=WXSDKManager.getInstance().getIWXUserTrackAdapter();
}
/**
* Set a Observer for component.
* This observer will be called in each component, should not doing
* anything will impact render performance.
*
* @param observer
*/
public void setComponentObserver(ComponentObserver observer){
mComponentObserver = observer;
}
public ComponentObserver getComponentObserver(){
return mComponentObserver;
}
public NativeInvokeHelper getNativeInvokeHelper() {
return mNativeInvokeHelper;
}
public void setBizType(String bizType) {
if (!TextUtils.isEmpty(bizType)) {
mWXPerformance.bizType = bizType;
}
}
public ScrollView getScrollView() {
return mScrollView;
}
public void setRootScrollView(ScrollView scrollView) {
mScrollView = scrollView;
if (mWXScrollViewListener != null) {
((WXScrollView) mScrollView).addScrollViewListener(mWXScrollViewListener);
}
}
@Deprecated
public void registerScrollViewListener(WXScrollViewListener scrollViewListener) {
mWXScrollViewListener = scrollViewListener;
}
@Deprecated
public WXScrollViewListener getScrollViewListener() {
return mWXScrollViewListener;
}
@Deprecated
public void setIWXUserTrackAdapter(IWXUserTrackAdapter adapter) {
}
/**
* Render template asynchronously, use {@link WXRenderStrategy#APPEND_ASYNC} as render strategy
* @param template bundle js
* @param options os iphone/android/ipad
* weexversion Weex version(like 1.0.0)
* appversion App version(like 1.0.0)
* devid Device id(like Aqh9z8dRJNBhmS9drLG5BKCmXhecHUXIZoXOctKwFebH)
* sysversion Device system version(like 5.4.4、7.0.4, should be used with os)
* sysmodel Device model(like iOS:"MGA82J/A", android:"MI NOTE LTE")
* Time UNIX timestamp, UTC+08:00
* TTID(Optional)
* MarkertId
* Appname(Optional) tm,tb,qa
* Bundleurl(Optional) template url
* @param jsonInitData Initial data for rendering
*/
public void render(String template, Map<String, Object> options, String jsonInitData) {
render(template, options, jsonInitData, WXRenderStrategy.APPEND_ASYNC);
}
/**
* Render template asynchronously
* @param template bundle js
* @param options os iphone/android/ipad
* weexversion Weex version(like 1.0.0)
* appversion App version(like 1.0.0)
* devid Device id(like Aqh9z8dRJNBhmS9drLG5BKCmXhecHUXIZoXOctKwFebH)
* sysversion Device system version(like 5.4.4、7.0.4, should be used with os)
* sysmodel Device model(like iOS:"MGA82J/A", android:"MI NOTE LTE")
* Time UNIX timestamp, UTC+08:00
* TTID(Optional)
* MarkertId
* Appname(Optional) tm,tb,qa
* Bundleurl(Optional) template url
* @param jsonInitData Initial data for rendering
* @param flag RenderStrategy {@link WXRenderStrategy}
*/
@Deprecated
public void render(String template, Map<String, Object> options, String jsonInitData, WXRenderStrategy flag) {
render(WXPerformance.DEFAULT, template, options, jsonInitData, flag);
}
/**
* Render template asynchronously
*
* @param pageName, used for performance log.
* @param template bundle js
* @param options os iphone/android/ipad
* weexversion Weex version(like 1.0.0)
* appversion App version(like 1.0.0)
* devid Device id(like Aqh9z8dRJNBhmS9drLG5BKCmXhecHUXIZoXOctKwFebH)
* sysversion Device system version(like 5.4.4、7.0.4, should be used with os)
* sysmodel Device model(like iOS:"MGA82J/A", android:"MI NOTE LTE")
* Time UNIX timestamp, UTC+08:00
* TTID(Optional)
* MarkertId
* Appname(Optional) tm,tb,qa
* Bundleurl(Optional) template url
* @param jsonInitData Initial data for rendering
* @param flag RenderStrategy {@link WXRenderStrategy}
*/
public void render(String pageName, String template, Map<String, Object> options, String jsonInitData, WXRenderStrategy flag) {
if(WXEnvironment.isApkDebugable() && WXPerformance.DEFAULT.equals(pageName)){
WXLogUtils.e("WXSDKInstance", "Please set your pageName or your js bundle url !!!!!!!");
if (getUIContext() != null) {
new AlertDialog.Builder(getUIContext())
.setTitle("Error: Missing pageName")
.setMessage("We highly recommend you to set pageName. Call" +
"\nWXSDKInstance#render(String pageName, String template, Map<String, Object> options, String jsonInitData, WXRenderStrategy flag)\n" +
"to fix it.")
.show();
}
return;
}
renderInternal(pageName,template,options,jsonInitData,flag);
}
private void ensureRenderArchor(){
if(mRenderContainer == null){
if (getContext() != null) {
mRenderContainer = new RenderContainer(getContext());
mRenderContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mRenderContainer.setBackgroundColor(Color.TRANSPARENT);
mRenderContainer.setSDKInstance(this);
mRenderContainer.addOnLayoutChangeListener(this);
}
}
}
private void renderInternal(String pageName,
String template,
Map<String, Object> options,
String jsonInitData,
WXRenderStrategy flag){
if (mRendered || TextUtils.isEmpty(template)) {
return;
}
WXLogUtils.d("WXSDKInstance", "Start render page: " + pageName);
if (WXTracing.isAvailable()) {
WXTracing.TraceEvent traceEvent = WXTracing.newEvent("executeBundleJS", mInstanceId, -1);
traceEvent.traceId = mExecJSTraceId;
traceEvent.iid = mInstanceId;
traceEvent.tname = "JSThread";
traceEvent.ph = "B";
traceEvent.submit();
mRenderStartNanos = System.nanoTime();
}
ensureRenderArchor();
Map<String, Object> renderOptions = options;
if (renderOptions == null) {
renderOptions = new HashMap<>();
}
if (WXEnvironment.sDynamicMode && !TextUtils.isEmpty(WXEnvironment.sDynamicUrl) && renderOptions.get("dynamicMode") == null) {
renderOptions.put("dynamicMode", "true");
renderByUrl(pageName, WXEnvironment.sDynamicUrl, renderOptions, jsonInitData, flag);
return;
}
mWXPerformance.pageName = pageName;
mWXPerformance.JSTemplateSize = template.length() / 1024f;
mRenderStartTime = System.currentTimeMillis();
mRenderStrategy = flag;
WXSDKManager.getInstance().setCrashInfo(WXEnvironment.WEEX_CURRENT_KEY,pageName);
WXSDKManager.getInstance().createInstance(this, template, renderOptions, jsonInitData);
mRendered = true;
if (TextUtils.isEmpty(mBundleUrl)) {
mBundleUrl = pageName;
}
}
private void renderByUrlInternal(String pageName,
final String url,
Map<String, Object> options,
final String jsonInitData,
final WXRenderStrategy flag) {
ensureRenderArchor();
pageName = wrapPageName(pageName, url);
mBundleUrl = url;
if(WXSDKManager.getInstance().getValidateProcessor()!=null) {
mNeedValidate = WXSDKManager.getInstance().getValidateProcessor().needValidate(mBundleUrl);
}
Map<String, Object> renderOptions = options;
if (renderOptions == null) {
renderOptions = new HashMap<>();
}
if (!renderOptions.containsKey(BUNDLE_URL)) {
renderOptions.put(BUNDLE_URL, url);
}
Uri uri = Uri.parse(url);
if (uri != null && TextUtils.equals(uri.getScheme(), "file")) {
render(pageName, WXFileUtils.loadFileOrAsset(assembleFilePath(uri), mContext), renderOptions, jsonInitData, flag);
return;
}
IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();
WXRequest wxRequest = new WXRequest();
wxRequest.url = rewriteUri(Uri.parse(url),URIAdapter.BUNDLE).toString();
if (wxRequest.paramMap == null) {
wxRequest.paramMap = new HashMap<String, String>();
}
wxRequest.paramMap.put(KEY_USER_AGENT, WXHttpUtil.assembleUserAgent(mContext,WXEnvironment.getConfig()));
WXHttpListener httpListener =
new WXHttpListener(pageName, renderOptions, jsonInitData, flag, System.currentTimeMillis());
httpListener.setSDKInstance(this);
adapter.sendRequest(wxRequest, (IWXHttpAdapter.OnHttpListener) httpListener);
}
/**
* Use {@link #render(String, String, Map, String, WXRenderStrategy)} instead.
* @param pageName
* @param template
* @param options
* @param jsonInitData
* @param width
* @param height
* @param flag
*/
@Deprecated
public void render(String pageName, String template, Map<String, Object> options, String jsonInitData, int width, int height, WXRenderStrategy flag) {
render(pageName,template,options,jsonInitData,flag);
}
/**
* Render template asynchronously, use {@link WXRenderStrategy#APPEND_ASYNC} as render strategy
* @param template bundle js
*/
public void render(String template) {
render(WXPerformance.DEFAULT, template, null, null, mRenderStrategy);
}
/**
* Use {@link #render(String)} instead.
* @param template
* @param width
* @param height
*/
@Deprecated
public void render(String template, int width, int height) {
render(template);
}
/**
* Use {@link #renderByUrl(String, String, Map, String, WXRenderStrategy)} instead.
* @param pageName
* @param url
* @param options
* @param jsonInitData
* @param width
* @param height
* @param flag
*/
@Deprecated
public void renderByUrl(String pageName, final String url, Map<String, Object> options, final String jsonInitData, final int width, final int height, final WXRenderStrategy flag){
renderByUrl(pageName,url,options,jsonInitData,flag);
}
public void renderByUrl(String pageName, final String url, Map<String, Object> options, final String jsonInitData, final WXRenderStrategy flag) {
renderByUrlInternal(pageName,url,options,jsonInitData,flag);
}
private String wrapPageName(String pageName, String url) {
if(TextUtils.equals(pageName, WXPerformance.DEFAULT)){
pageName=url;
try {
Uri uri=Uri.parse(url);
if(uri!=null){
Uri.Builder builder=new Uri.Builder();
builder.scheme(uri.getScheme());
builder.authority(uri.getAuthority());
builder.path(uri.getPath());
pageName=builder.toString();
}
} catch (Exception e) {
}
}
return pageName;
}
private String assembleFilePath(Uri uri) {
if(uri!=null && uri.getPath()!=null){
return uri.getPath().replaceFirst("/","");
}
return "";
}
public void reloadPage(boolean reloadThis) {
WXSDKEngine.reload();
if (reloadThis) {
// 可以发送广播吗?
if (mContext != null) {
Intent intent = new Intent();
intent.setAction(IWXDebugProxy.ACTION_INSTANCE_RELOAD);
intent.putExtra("url", mBundleUrl);
mContext.sendBroadcast(intent);
}
// mRendered = false;
// destroy();
// renderInternal(mPackage, mTemplate, mOptions, mJsonInitData, mFlag);
// refreshInstance("{}");
}
}
/**
* Refresh instance asynchronously.
* @param data the new data
*/
public void refreshInstance(Map<String, Object> data) {
if (data == null) {
return;
}
refreshInstance(WXJsonUtils.fromObjectToJSONString(data));
}
/**
* Refresh instance asynchronously.
* @param jsonData the new data
*/
public void refreshInstance(String jsonData) {
if (jsonData == null) {
return;
}
mRefreshStartTime = System.currentTimeMillis();
//cancel last refresh message
if (mLastRefreshData != null) {
mLastRefreshData.isDirty = true;
}
mLastRefreshData = new WXRefreshData(jsonData, false);
WXSDKManager.getInstance().refreshInstance(mInstanceId, mLastRefreshData);
}
public WXRenderStrategy getRenderStrategy() {
return mRenderStrategy;
}
@Override
public Context getUIContext() {
return mContext;
}
public String getInstanceId() {
return mInstanceId;
}
public Context getContext() {
if(mContext == null){
WXLogUtils.e("WXSdkInstance mContext == null");
}
return mContext;
}
public int getWeexHeight() {
return mRenderContainer == null ? 0: mRenderContainer.getHeight();
}
public int getWeexWidth() {
return mRenderContainer == null ? 0: mRenderContainer.getWidth();
}
public IWXImgLoaderAdapter getImgLoaderAdapter() {
return WXSDKManager.getInstance().getIWXImgLoaderAdapter();
}
public IDrawableLoader getDrawableLoader() {
return WXSDKManager.getInstance().getDrawableLoader();
}
public URIAdapter getURIAdapter(){
return WXSDKManager.getInstance().getURIAdapter();
}
public Uri rewriteUri(Uri uri,String type){
return getURIAdapter().rewrite(this,type,uri);
}
public IWXHttpAdapter getWXHttpAdapter() {
return WXSDKManager.getInstance().getIWXHttpAdapter();
}
public IWXStatisticsListener getWXStatisticsListener() {
return mStatisticsListener;
}
public @Nullable
IWebSocketAdapter getWXWebSocketAdapter() {
return WXSDKManager.getInstance().getIWXWebSocketAdapter();
}
@Deprecated
public void reloadImages() {
if (mScrollView == null) {
return;
}
}
public boolean isPreRenderMode() {
return this.isPreRenderMode;
}
public void setPreRenderMode(final boolean isPreRenderMode) {
WXSDKManager.getInstance().getWXRenderManager().postOnUiThread(new Runnable() {
@Override
public void run() {
WXSDKInstance.this.isPreRenderMode = isPreRenderMode;
}
},0);
}
public void setContext(@NonNull Context context) {
this.mContext = context;
}
/********************************
* begin register listener
********************************************************/
public void registerRenderListener(IWXRenderListener listener) {
mRenderListener = listener;
}
@Deprecated
public void registerActivityStateListener(IWXActivityStateListener listener) {
}
public void registerStatisticsListener(IWXStatisticsListener listener) {
mStatisticsListener = listener;
}
public void setLayoutFinishListener(@Nullable LayoutFinishListener listener) {
this.mLayoutFinishListener = listener;
}
public LayoutFinishListener getLayoutFinishListener() {
return this.mLayoutFinishListener;
}
/**set render start time*/
public void setRenderStartTime(long renderStartTime) {
this.mRenderStartTime = renderStartTime;
}
/********************************
* end register listener
********************************************************/
/********************************
* begin hook Activity life cycle callback
********************************************************/
@Override
public void onActivityCreate() {
// module listen Activity onActivityCreate
WXModuleManager.onActivityCreate(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityCreate();
}else{
WXLogUtils.w("Warning :Component tree has not build completely,onActivityCreate can not be call!");
}
mGlobalEventReceiver=new WXGlobalEventReceiver(this);
getContext().registerReceiver(mGlobalEventReceiver,new IntentFilter(WXGlobalEventReceiver.EVENT_ACTION));
}
@Override
public void onActivityStart() {
// module listen Activity onActivityCreate
WXModuleManager.onActivityStart(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityStart();
}else{
WXLogUtils.w("Warning :Component tree has not build completely,onActivityStart can not be call!");
}
}
public boolean onCreateOptionsMenu(Menu menu) {
WXModuleManager.onCreateOptionsMenu(getInstanceId(),menu);
if(mRootComp != null) {
mRootComp.onCreateOptionsMenu(menu);
}else{
WXLogUtils.w("Warning :Component tree has not build completely,onActivityStart can not be call!");
}
return true;
}
@Override
public void onActivityPause() {
onViewDisappear();
if(!isCommit){
Set<String> componentTypes= WXComponentFactory.getComponentTypesByInstanceId(getInstanceId());
if(componentTypes!=null && componentTypes.contains(WXBasicComponentType.SCROLLER)){
mWXPerformance.useScroller=1;
}
mWXPerformance.maxDeepViewLayer=getMaxDeepLayer();
if (mUserTrackAdapter != null) {
mUserTrackAdapter.commit(mContext, null, IWXUserTrackAdapter.LOAD, mWXPerformance, getUserTrackParams());
}
isCommit=true;
}
// module listen Activity onActivityPause
WXModuleManager.onActivityPause(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityPause();
}else{
WXLogUtils.w("Warning :Component tree has not build completely,onActivityPause can not be call!");
}
WXLogUtils.i("Application onActivityPause()");
if (!mCurrentGround) {
WXLogUtils.i("Application to be in the backround");
Intent intent = new Intent(WXGlobalEventReceiver.EVENT_ACTION);
intent.putExtra(WXGlobalEventReceiver.EVENT_NAME, Constants.Event.PAUSE_EVENT);
intent.putExtra(WXGlobalEventReceiver.EVENT_WX_INSTANCEID, getInstanceId());
mContext.sendBroadcast(intent);
this.mCurrentGround = true;
}
}
@Override
public void onActivityResume() {
// notify onActivityResume callback to module
WXModuleManager.onActivityResume(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityResume();
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onActivityResume can not be call!");
}
if (mCurrentGround) {
WXLogUtils.i("Application to be in the foreground");
Intent intent = new Intent(WXGlobalEventReceiver.EVENT_ACTION);
intent.putExtra(WXGlobalEventReceiver.EVENT_NAME, Constants.Event.RESUME_EVENT);
intent.putExtra(WXGlobalEventReceiver.EVENT_WX_INSTANCEID, getInstanceId());
mContext.sendBroadcast(intent);
mCurrentGround = false;
}
onViewAppear();
setViewPortWidth(mInstanceViewPortWidth);
}
@Override
public void onActivityStop() {
// notify onActivityResume callback to module
WXModuleManager.onActivityStop(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityStop();
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onActivityStop can not be call!");
}
}
@Override
public void onActivityDestroy() {
WXModuleManager.onActivityDestroy(getInstanceId());
if(mRootComp != null) {
mRootComp.onActivityDestroy();
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onActivityDestroy can not be call!");
}
destroy();
}
@Override
public boolean onActivityBack() {
WXModuleManager.onActivityBack(getInstanceId());
if(mRootComp != null) {
return mRootComp.onActivityBack();
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onActivityBack can not be call!");
}
return false;
}
public boolean onBackPressed() {
WXComponent comp = getRootComponent();
if(comp != null) {
WXEvent events= comp.getDomObject().getEvents();
boolean hasBackPressed = events.contains(Constants.Event.CLICKBACKITEM);
if (hasBackPressed) {
fireEvent(comp.getRef(), Constants.Event.CLICKBACKITEM,null, null);
}
return hasBackPressed;
}
return false;
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
WXModuleManager.onActivityResult(getInstanceId(),requestCode,resultCode,data);
if(mRootComp != null) {
mRootComp.onActivityResult(requestCode,resultCode,data);
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onActivityResult can not be call!");
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
WXModuleManager.onRequestPermissionsResult(getInstanceId(),requestCode,permissions,grantResults);
if(mRootComp != null) {
mRootComp.onRequestPermissionsResult(requestCode,permissions,grantResults);
}else{
WXLogUtils.w("Warning :Component tree has not build completely, onRequestPermissionsResult can not be call!");
}
}
/********************************
* end hook Activity life cycle callback
********************************************************/
public void onViewDisappear(){
WXComponent comp = getRootComponent();
if(comp != null) {
fireEvent(comp.getRef(), Constants.Event.VIEWDISAPPEAR, null, null);
//call disappear of nested instances
for(OnInstanceVisibleListener instance:mVisibleListeners){
instance.onDisappear();
}
}
}
public void onViewAppear(){
WXComponent comp = getRootComponent();
if(comp != null) {
fireEvent( comp.getRef(), Constants.Event.VIEWAPPEAR,null, null);
for(OnInstanceVisibleListener instance:mVisibleListeners){
instance.onAppear();
}
}
}
public void onCreateFinish() {
if (mContext != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if ( mContext != null) {
onViewAppear();
View wxView= mRenderContainer;
if(WXEnvironment.isApkDebugable() && WXSDKManager.getInstance().getIWXDebugAdapter()!=null){
wxView = WXSDKManager.getInstance().getIWXDebugAdapter().wrapContainer(WXSDKInstance.this,wxView);
}
if(mRenderListener != null) {
mRenderListener.onViewCreated(WXSDKInstance.this, wxView);
}
if (mStatisticsListener != null) {
mStatisticsListener.onFirstView();
}