|
18 | 18 | package com.sensorsdata.analytics.utils; |
19 | 19 |
|
20 | 20 | import android.app.Activity; |
21 | | -import android.app.ActivityManager; |
22 | | -import android.content.Context; |
23 | | -import android.util.Log; |
24 | 21 | import android.view.View; |
25 | | - |
26 | | -import com.facebook.react.uimanager.NativeViewHierarchyManager; |
27 | | -import com.facebook.react.uimanager.UIImplementation; |
28 | | -import com.facebook.react.uimanager.UIManagerModule; |
29 | | -import com.facebook.react.uimanager.UIViewOperationQueue; |
30 | | -import java.lang.ref.SoftReference; |
31 | | -import com.sensorsdata.analytics.android.sdk.SALog; |
32 | | - |
33 | | -import java.lang.ref.WeakReference; |
34 | | -import java.lang.reflect.Field; |
35 | 22 | import android.view.ViewGroup; |
36 | | -import android.view.View; |
37 | 23 | import android.view.ViewParent; |
| 24 | + |
38 | 25 | import org.json.JSONObject; |
39 | 26 |
|
| 27 | +import java.lang.ref.WeakReference; |
| 28 | +import java.util.WeakHashMap; |
| 29 | + |
40 | 30 | public class RNViewUtils { |
41 | 31 |
|
42 | | - private static SoftReference mSoftCurrentActivityReference; |
| 32 | + private static WeakReference mWeakCurrentActivityReference; |
43 | 33 | private static String currentTitle; |
44 | 34 | private static String currentScreenName; |
45 | 35 | public static boolean isScreenVisiable = false; |
46 | | - private static JSONObject properties = new JSONObject(); |
| 36 | + private static JSONObject screenProperties; |
47 | 37 | private static WeakReference onTouchViewReference; |
48 | | - |
| 38 | + private static WeakHashMap<Activity, JSONObject> mScreenMap = new WeakHashMap<>(); |
49 | 39 |
|
50 | 40 | public static void setOnTouchView(View nativeTargetView) { |
51 | 41 | onTouchViewReference = new WeakReference(nativeTargetView); |
52 | 42 | } |
53 | 43 |
|
54 | 44 | public static View getViewByTag(int viewTag) { |
55 | 45 | View clickView = null; |
56 | | - try{ |
| 46 | + try { |
57 | 47 | Activity currentActivity = getCurrentActivity(); |
58 | | - if(currentActivity != null){ |
| 48 | + if (currentActivity != null) { |
59 | 49 | clickView = currentActivity.findViewById(viewTag); |
60 | 50 | } |
61 | | - if(clickView == null){ |
| 51 | + if (clickView == null) { |
62 | 52 | clickView = getTouchViewByTag(viewTag); |
63 | 53 | } |
64 | | - }catch (Exception ignored){ |
| 54 | + } catch (Exception ignored) { |
65 | 55 |
|
66 | 56 | } |
67 | 57 | return clickView; |
@@ -113,56 +103,80 @@ private static View getClickViewInChild(int viewId, ViewGroup currentView) { |
113 | 103 | return null; |
114 | 104 | } |
115 | 105 |
|
116 | | - public static void saveScreenAndTitle(String screenName,String title){ |
| 106 | + public static void saveScreenAndTitle(String screenName, String title) { |
117 | 107 | currentScreenName = screenName; |
118 | 108 | currentTitle = title; |
119 | | - try{ |
120 | | - properties.put("$title", title); |
121 | | - properties.put("$screen_name", screenName); |
122 | | - }catch (Exception ignored){ |
| 109 | + try { |
| 110 | + screenProperties = new JSONObject(); |
| 111 | + screenProperties.put("$title", title); |
| 112 | + screenProperties.put("$screen_name", screenName); |
| 113 | + } catch (Exception ignored) { |
123 | 114 |
|
124 | 115 | } |
| 116 | + Activity currentActivity; |
| 117 | + if ((currentActivity = getCurrentActivity()) != null) { |
| 118 | + mScreenMap.put(currentActivity, screenProperties); |
| 119 | + } |
125 | 120 | } |
126 | 121 |
|
127 | | - public static String getTitle(){ |
| 122 | + public static String getTitle() { |
128 | 123 | return currentTitle; |
129 | 124 | } |
130 | 125 |
|
131 | | - public static String getScreenName(){ |
| 126 | + public static String getScreenName() { |
132 | 127 | return currentScreenName; |
133 | 128 | } |
134 | 129 |
|
135 | 130 | /** |
136 | 131 | * 供可视化调用,返回 $title,$screen_name,勿删 |
| 132 | + * |
137 | 133 | * @return json 格式 |
138 | 134 | */ |
139 | | - public static String getVisualizeProperties(){ |
140 | | - if(!isScreenVisiable){ |
| 135 | + public static String getVisualizeProperties() { |
| 136 | + //当前页面不可见或无 $screen_name $title 时,可视化获取原生页面信息 |
| 137 | + if (!isScreenVisiable || screenProperties == null) { |
141 | 138 | return ""; |
142 | 139 | } |
143 | | - return properties.toString(); |
| 140 | + return screenProperties.toString(); |
144 | 141 | } |
145 | 142 |
|
146 | | - public static void setScreenVisiable(boolean isVisiable){ |
| 143 | + public static void setScreenVisiable(boolean isVisiable) { |
147 | 144 | isScreenVisiable = isVisiable; |
148 | 145 | } |
149 | 146 |
|
150 | | - public static void setCurrentActivity(Activity currentActivity) { |
| 147 | + private static void setCurrentActivity(Activity currentActivity) { |
151 | 148 | clearCurrentActivityReference(); |
152 | | - mSoftCurrentActivityReference = new SoftReference(currentActivity); |
| 149 | + mWeakCurrentActivityReference = new WeakReference(currentActivity); |
| 150 | + JSONObject properties = mScreenMap.get(currentActivity); |
| 151 | + if (properties != null && properties.has("$screen_name")) { |
| 152 | + saveScreenAndTitle(properties.optString("$screen_name"), properties.optString("$title")); |
| 153 | + } else { |
| 154 | + currentScreenName = null; |
| 155 | + currentTitle = null; |
| 156 | + screenProperties = null; |
| 157 | + } |
153 | 158 | } |
154 | 159 |
|
155 | | - private static Activity getCurrentActivity(){ |
156 | | - if(mSoftCurrentActivityReference == null){ |
| 160 | + private static Activity getCurrentActivity() { |
| 161 | + if (mWeakCurrentActivityReference == null) { |
157 | 162 | return null; |
158 | 163 | } |
159 | | - return (Activity)mSoftCurrentActivityReference.get(); |
| 164 | + return (Activity) mWeakCurrentActivityReference.get(); |
160 | 165 | } |
161 | 166 |
|
162 | 167 | public static void clearCurrentActivityReference() { |
163 | | - if(mSoftCurrentActivityReference != null){ |
164 | | - mSoftCurrentActivityReference.clear(); |
165 | | - mSoftCurrentActivityReference = null; |
| 168 | + if (mWeakCurrentActivityReference != null) { |
| 169 | + mWeakCurrentActivityReference.clear(); |
| 170 | + mWeakCurrentActivityReference = null; |
166 | 171 | } |
167 | 172 | } |
| 173 | + |
| 174 | + public static void onActivityResumed(Activity currentActivity) { |
| 175 | + setScreenVisiable(true); |
| 176 | + setCurrentActivity(currentActivity); |
| 177 | + } |
| 178 | + |
| 179 | + public static void onActivityPaused() { |
| 180 | + setScreenVisiable(false); |
| 181 | + } |
168 | 182 | } |
0 commit comments