1717
1818package com .sensorsdata .analytics ;
1919
20+ import android .util .SparseArray ;
2021import android .view .MotionEvent ;
21- import android .view .MotionEvent .*;
2222import android .view .View ;
2323import android .view .ViewGroup ;
24- import com .facebook .react .bridge .ReadableMap ;
25- import com .sensorsdata .analytics .RNSensorsAnalyticsModule ;
26- import com .sensorsdata .analytics .utils .RNViewUtils ;
27- import com .sensorsdata .analytics .data .SAViewProperties ;
28- import com .sensorsdata .analytics .property .RNPropertyManager ;
2924
25+ import com .facebook .react .bridge .ReadableMap ;
3026import com .facebook .react .uimanager .JSTouchDispatcher ;
3127import com .facebook .react .uimanager .events .EventDispatcher ;
3228import com .sensorsdata .analytics .android .sdk .SALog ;
3329import com .sensorsdata .analytics .android .sdk .SensorsDataAPI ;
3430import com .sensorsdata .analytics .android .sdk .util .SensorsDataUtils ;
35- import com .sensorsdata .analytics .android .sdk .SensorsDataAutoTrackHelper ;
31+ import com .sensorsdata .analytics .data .SAViewProperties ;
32+ import com .sensorsdata .analytics .property .RNPropertyManager ;
33+ import com .sensorsdata .analytics .utils .RNTouchTargetHelper ;
34+ import com .sensorsdata .analytics .utils .RNViewUtils ;
35+
36+ import org .json .JSONObject ;
3637
3738import java .lang .reflect .Field ;
3839import java .util .WeakHashMap ;
39- import java .util .HashMap ;
40- import org .json .JSONObject ;
41- import android .util .SparseArray ;
4240
4341public class RNAgent {
4442 private static final WeakHashMap jsTouchDispatcherViewGroupWeakHashMap = new WeakHashMap ();
4543 private static SparseArray <SAViewProperties > viewPropertiesArray = new SparseArray ();
4644
4745 public static void handleTouchEvent (
4846 JSTouchDispatcher jsTouchDispatcher , MotionEvent event , EventDispatcher eventDispatcher ) {
47+ try {
48+ if (event .getAction () == MotionEvent .ACTION_DOWN ) { // ActionDown
49+ ViewGroup viewGroup = (ViewGroup ) jsTouchDispatcherViewGroupWeakHashMap .get (jsTouchDispatcher );
50+ if (viewGroup == null ) {
51+ try {
52+ Field viewGroupField = jsTouchDispatcher .getClass ().getDeclaredField ("mRootViewGroup" );
53+ viewGroupField .setAccessible (true );
54+ viewGroup = (ViewGroup ) viewGroupField .get (jsTouchDispatcher );
55+ jsTouchDispatcherViewGroupWeakHashMap .put (jsTouchDispatcher , viewGroup );
56+ } catch (Exception e ) {
57+ SALog .printStackTrace (e );
58+ }
59+ }
60+ if (viewGroup != null ) {
61+ View nativeTargetView =
62+ RNTouchTargetHelper .findTouchTargetView (
63+ new float []{event .getX (), event .getY ()}, viewGroup );
64+ if (nativeTargetView != null ) {
65+ View reactTargetView = RNTouchTargetHelper .findClosestReactAncestor (nativeTargetView );
66+ if (reactTargetView != null ) {
67+ nativeTargetView = reactTargetView ;
68+ }
69+ }
70+ if (nativeTargetView != null ) {
71+ RNViewUtils .setOnTouchView (nativeTargetView );
72+ }
73+ }
74+ }
75+ } catch (Exception ignored ) {
76+
77+ }
4978 }
5079
51- public static void trackViewScreen (String url , JSONObject properties , boolean isAuto ){
52- try {
80+ static void trackViewScreen (String url , JSONObject properties , boolean isAuto ) {
81+ try {
5382 String screenName = url ;
54- if (properties == null ){
83+ if (properties == null ) {
5584 properties = new JSONObject ();
5685 }
57- if (properties .has ("$screen_name" )){
86+ if (properties .has ("$screen_name" )) {
5887 screenName = properties .getString ("$screen_name" );
5988 }
6089 String title = screenName ;
61- if (properties .has ("$title" )){
90+ if (properties .has ("$title" )) {
6291 title = properties .getString ("$title" );
6392 }
64- if (screenName != null ){
65- properties .put ("$screen_name" ,screenName );
93+ if (screenName != null ) {
94+ properties .put ("$screen_name" , screenName );
6695 }
67- if (title != null ){
68- properties .put ("$title" ,title );
96+ if (title != null ) {
97+ properties .put ("$title" , title );
6998 }
70- RNViewUtils .saveScreenAndTitle (screenName ,title );
99+ RNViewUtils .saveScreenAndTitle (screenName , title );
71100 //关闭 AutoTrack
72101 if (isAuto && !SensorsDataAPI .sharedInstance ().isAutoTrackEnabled ()) {
73102 return ;
@@ -77,12 +106,12 @@ public static void trackViewScreen(String url, JSONObject properties, boolean is
77106 return ;
78107 }
79108 SensorsDataAPI .sharedInstance ().trackViewScreen (url , RNPropertyManager .mergeProperty (properties , isAuto ));
80- }catch (Exception e ){
109+ } catch (Exception e ) {
81110 SALog .printStackTrace (e );
82111 }
83112 }
84113
85- public static void trackViewClick (int viewId ){
114+ static void trackViewClick (int viewId ) {
86115 try {
87116 //关闭 AutoTrack
88117 if (!SensorsDataAPI .sharedInstance ().isAutoTrackEnabled ()) {
@@ -92,61 +121,66 @@ public static void trackViewClick(int viewId){
92121 if (SensorsDataAPI .sharedInstance ().isAutoTrackEventTypeIgnored (SensorsDataAPI .AutoTrackEventType .APP_CLICK )) {
93122 return ;
94123 }
95- View clickView = RNViewUtils .getTouchViewByTag (viewId );
124+ View clickView = RNViewUtils .getViewByTag (viewId );
96125 if (clickView != null ) {
97126 JSONObject properties = new JSONObject ();
98- if (RNViewUtils .getTitle () != null ){
99- properties .put ("$title" ,RNViewUtils .getTitle ());
127+ if (RNViewUtils .getTitle () != null ) {
128+ properties .put ("$title" , RNViewUtils .getTitle ());
100129 }
101- if (RNViewUtils .getScreenName () != null ){
102- properties .put ("$screen_name" ,RNViewUtils .getScreenName ());
130+ if (RNViewUtils .getScreenName () != null ) {
131+ properties .put ("$screen_name" , RNViewUtils .getScreenName ());
103132 }
104133 SAViewProperties viewProperties = viewPropertiesArray .get (viewId );
105- if (viewProperties != null && viewProperties .properties != null && viewProperties .properties .length () > 0 ){
106- if (viewProperties .properties .optBoolean ("ignore" , false )){
134+ if (viewProperties != null && viewProperties .properties != null && viewProperties .properties .length () > 0 ) {
135+ if (viewProperties .properties .optBoolean ("ignore" , false )) {
107136 return ;
108137 }
109138 viewProperties .properties .remove ("ignore" );
110139 SensorsDataUtils .mergeJSONObject (viewProperties .properties , properties );
111140 }
112- SensorsDataAPI .sharedInstance ().trackViewAppClick (clickView , RNPropertyManager .mergeProperty (properties , true ));
141+ SensorsDataAPI .sharedInstance ().trackViewAppClick (clickView , RNPropertyManager .mergeProperty (properties , true ));
113142 }
114143 } catch (Exception e ) {
115144 SALog .printStackTrace (e );
116145 }
117146 }
118147
119- public static void saveViewProperties (int viewId , boolean clickable , ReadableMap viewProperties ) {
120- if (clickable ){
148+ static void saveViewProperties (int viewId , boolean clickable , ReadableMap viewProperties ) {
149+ if (clickable ) {
121150 viewPropertiesArray .put (viewId , new SAViewProperties (clickable , viewProperties ));
122151 }
123152 }
124153
125- public static void addView (View view ,int index ){
154+ /**
155+ * 添加 View 调用,Android 插件调用,勿删
156+ * @param view View
157+ * @param index index
158+ */
159+ public static void addView (View view , int index ) {
126160 SAViewProperties properties = viewPropertiesArray .get (view .getId ());
127- if (properties != null ){
161+ if (properties != null ) {
128162 properties .setViewClickable (view );
129163 }
130164 }
131165
132166 /**
133167 * 忽略 Slider、Switch Android SDK 的采集逻辑,统一通过 Recat Native 采集
134168 */
135- static void ignoreView (){
136- try {
169+ static void ignoreView () {
170+ try {
137171 SensorsDataAPI .sharedInstance ().ignoreViewType (Class .forName ("com.facebook.react.views.switchview.ReactSwitch" ));
138- }catch (Exception e ){
139-
172+ } catch (Exception e ) {
173+ //ignored
140174 }
141- try {
175+ try {
142176 SensorsDataAPI .sharedInstance ().ignoreViewType (Class .forName ("com.facebook.react.views.slider.ReactSlider" ));
143- }catch (Exception e ){
144-
177+ } catch (Exception e ) {
178+ //ignored
145179 }
146- try {
180+ try {
147181 SensorsDataAPI .sharedInstance ().ignoreViewType (Class .forName ("com.reactnativecommunity.slider.ReactSlider" ));
148- }catch (Exception e ){
149-
182+ } catch (Exception e ) {
183+ //ignored
150184 }
151185 }
152186}
0 commit comments