update: 调整组件初始化生命周期, 完善 Excpetion 上报组件的调用逻辑
							parent
							
								
									9c3795227c
								
							
						
					
					
						commit
						041cbdc36e
					
				|  | @ -58,9 +58,9 @@ namespace Guru | |||
| 
 | ||||
| 		        _hasInited = true; | ||||
| 	        } | ||||
| 	        catch (Exception e) | ||||
| 	        catch (Exception ex) | ||||
| 	        { | ||||
|         		Crashlytics.LogException(e); | ||||
| 		        LogCrashlytics(ex); | ||||
| 	        } | ||||
| 	    } | ||||
| 
 | ||||
|  |  | |||
|  | @ -25,6 +25,8 @@ namespace Guru | |||
| 		 | ||||
| 		private static bool _isInited;				//Analytics是否初始化完成 | ||||
| 		public static bool EnableDebugAnalytics;	//允许Debug包上报打点 | ||||
| 
 | ||||
| 		public static bool IsDebugMode => PlatformUtil.IsDebug(); | ||||
| 		 | ||||
| 		private static bool IsEnable  | ||||
| 		{ | ||||
|  | @ -39,14 +41,12 @@ namespace Guru | |||
| 					return false; | ||||
| 
 | ||||
| 				//开发环境打点不上报 | ||||
| 				if (PlatformUtil.IsDebug() && !EnableDebugAnalytics) | ||||
| 				if (IsDebugMode && !EnableDebugAnalytics) | ||||
| 					return false; | ||||
| 
 | ||||
| 				return true; | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		 | ||||
| 		 | ||||
| 		#region 初始化 | ||||
| 
 | ||||
|  | @ -54,7 +54,13 @@ namespace Guru | |||
| 		{ | ||||
| 			if (_isInited) return; | ||||
| 			_isInited = true; | ||||
| 			Crashlytics.IsCrashlyticsCollectionEnabled = true; | ||||
| 			 | ||||
| 			// -------- 初始化 Exception ---------- | ||||
| 			CrashlyticsAgent.Install(); | ||||
| 			 | ||||
| 			// ------- 初始化自打点 ---------- | ||||
| 			InstallGuruAnalytics(IsDebug); | ||||
| 			 | ||||
| 			 | ||||
| 			if (_defaultEventSetting == null) | ||||
| 			{ | ||||
|  | @ -67,13 +73,24 @@ namespace Guru | |||
| 				}; | ||||
| 			} | ||||
| 			 | ||||
| 			if (_defaultEventSetting.EnableFirebaseAnalytics) | ||||
| 			FirebaseUtil.onInitComplete += OnFirebaseCompleted; | ||||
| 		} | ||||
| 
 | ||||
| 		private static void OnFirebaseCompleted(bool success) | ||||
| 		{ | ||||
| 			FirebaseUtil.onInitComplete -= OnFirebaseCompleted; | ||||
| 
 | ||||
| 			if (success) | ||||
| 			{ | ||||
| 				FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); | ||||
| 				FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); | ||||
| 				SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); | ||||
| 				SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); | ||||
| 				SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); | ||||
| 				Crashlytics.IsCrashlyticsCollectionEnabled = true; | ||||
| 				if (_defaultEventSetting.EnableFirebaseAnalytics) | ||||
| 				{ | ||||
| 					FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); | ||||
| 					FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); | ||||
| 					SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); | ||||
| 					SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); | ||||
| 					SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
|  | @ -91,8 +108,6 @@ namespace Guru | |||
| 				new Parameter(FirebaseAnalytics.ParameterScreenClass, className), | ||||
| 				new Parameter(FirebaseAnalytics.ParameterScreenName, screenName) | ||||
| 			); | ||||
| 			 | ||||
| 
 | ||||
| 		} | ||||
| 
 | ||||
| 		#endregion | ||||
|  | @ -255,7 +270,7 @@ namespace Guru | |||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				Crashlytics.Log(msg); | ||||
| 				CrashlyticsAgent.Log(msg); | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
|  | @ -263,8 +278,7 @@ namespace Guru | |||
| 		public static void LogCrashlytics(Exception exp) | ||||
| 		{ | ||||
| 			if (!_isInited) return; | ||||
| 			Crashlytics.LogException(exp); | ||||
| 			Crashlytics.SetUserId(IPMConfig.IPM_UID); | ||||
| 			CrashlyticsAgent.LogException(exp); | ||||
| 		} | ||||
| 
 | ||||
| 		#endregion | ||||
|  |  | |||
|  | @ -15,10 +15,15 @@ namespace Guru | |||
| 		 | ||||
| 		public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther; | ||||
| 		public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available; | ||||
| 		public static Action<bool> onInitComplete; | ||||
| 		 | ||||
| 		public static void InitFirebase(Action callback, bool isDebug = false) | ||||
| 		{ | ||||
| 			_isReady = false; | ||||
| 			_isDebug = isDebug; | ||||
| 			GuruRepoter.Install(); | ||||
| 			Analytics.InitAnalytics(); // 打点提前初始化 | ||||
| 			 | ||||
| 			// 初始化 Fireabse 依赖 | ||||
| 			FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { | ||||
| 				DependencyStatus = task.Result; | ||||
| 				if (DependencyStatus == DependencyStatus.Available) | ||||
|  | @ -31,15 +36,14 @@ namespace Guru | |||
| 				{ | ||||
| 					Log.E(LOG_TAG, "Could not resolve all Firebase dependencies: " + DependencyStatus); | ||||
| 				} | ||||
| 				onInitComplete?.Invoke(_isReady); | ||||
| 			}); | ||||
| 		} | ||||
| 		private static void InitializeFirebaseComp() | ||||
| 		{ | ||||
| 			InitCrashlytics();  // 老项目沿用此逻辑 | ||||
| 			InitRemoteConfig();	// 老项目沿用此逻辑 | ||||
| 			Analytics.InstallGuruAnalytics(_isDebug); // 初始化Guru自打点 | ||||
| 			Analytics.InitAnalytics(); | ||||
| 			InitAdjustService(); | ||||
| 			InitAdjustService();  // 初始化 Firebase 服务 | ||||
| 			 | ||||
| 			if (IPMConfig.IPM_UID.IsNullOrEmpty()) | ||||
| 			{ | ||||
|  |  | |||
|  | @ -6,9 +6,13 @@ namespace Guru | |||
|     using System; | ||||
|     using System.Linq; | ||||
|      | ||||
|     public class GuruRepoter | ||||
|     public static class CrashlyticsAgent | ||||
|     { | ||||
|         private static bool _initOnce; | ||||
|         private static bool _isReady; | ||||
| 
 | ||||
|         private static Queue<Exception> _expCache; | ||||
|         private static bool _hasSetUser = false; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 捕获列表 | ||||
|  | @ -29,20 +33,46 @@ namespace Guru | |||
| 
 | ||||
|         public static void Install() | ||||
|         { | ||||
|             _isReady = true; | ||||
|             Crashlytics.IsCrashlyticsCollectionEnabled = true; | ||||
|             if (_initOnce) return; | ||||
|             _initOnce = true; | ||||
| 
 | ||||
|             _expCache = new Queue<Exception>(20); | ||||
|              | ||||
|             Application.logMessageReceived -= OnReceivedMessage; | ||||
|             Application.logMessageReceived += OnReceivedMessage; | ||||
|              | ||||
|             Application.logMessageReceivedThreaded -= OnReceivedMessage; | ||||
|             Application.logMessageReceivedThreaded += OnReceivedMessage; | ||||
| 
 | ||||
|             if (FirebaseUtil.IsReady) | ||||
|             { | ||||
|                 OnFirebaseComplete(true); | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             FirebaseUtil.onInitComplete -= OnFirebaseComplete; | ||||
|             FirebaseUtil.onInitComplete += OnFirebaseComplete; | ||||
|         } | ||||
| 
 | ||||
|         private static void CheckReady() | ||||
|         private static void OnFirebaseComplete(bool success) | ||||
|         { | ||||
|             if (_isReady) return; | ||||
|             Install(); | ||||
|             FirebaseUtil.onInitComplete -= OnFirebaseComplete; | ||||
|             if (success) | ||||
|             { | ||||
|                 Crashlytics.IsCrashlyticsCollectionEnabled = true; | ||||
|                 if (_expCache != null && _expCache.Count > 0) | ||||
|                 { | ||||
|                     while (_expCache.Count > 0) | ||||
|                     { | ||||
|                         LogException(_expCache.Dequeue()); | ||||
|                     } | ||||
|                     _expCache.Clear(); | ||||
|                 } | ||||
|                 _isReady = true; | ||||
|                  | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         private static string ToLogTypeString(LogType type) | ||||
|         { | ||||
|             switch (type) | ||||
|  | @ -72,20 +102,44 @@ namespace Guru | |||
| 
 | ||||
|         public static void LogException(Exception ex) | ||||
|         { | ||||
|             CheckReady(); | ||||
|             if (!_isReady) | ||||
|             { | ||||
|                 Install(); | ||||
|                 _expCache.Enqueue(ex); | ||||
|                 return; | ||||
|             } | ||||
|             Crashlytics.LogException(ex); | ||||
|             CheckSetUser(); | ||||
|         } | ||||
| 
 | ||||
|         public static void LogException(string msg) | ||||
|         { | ||||
|             CheckReady(); | ||||
|             Crashlytics.LogException(new Exception(msg)); | ||||
|         } | ||||
| 
 | ||||
|         public static void Log(string msg) | ||||
|         { | ||||
|             CheckReady(); | ||||
|             if (!_isReady) | ||||
|             { | ||||
|                 Install(); | ||||
|                 return; | ||||
|             } | ||||
|             Crashlytics.Log(msg); | ||||
|             CheckSetUser(); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         private static void CheckSetUser() | ||||
|         { | ||||
|             if (!_hasSetUser) | ||||
|             { | ||||
|                 if (!string.IsNullOrEmpty(IPMConfig.IPM_UID)) | ||||
|                 { | ||||
|                     Crashlytics.SetUserId(IPMConfig.IPM_UID); | ||||
|                     _hasSetUser = true; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  | @ -49,13 +49,21 @@ namespace Guru | |||
|         // onFetchComplete 传参 true: 拉取成功   false: 拉取失败 | ||||
|         public static void FetchAll(bool immediately = false) | ||||
|         { | ||||
|             if (!_initOnce) Init(null, _fetchIntervalHours, _isDebug); | ||||
|             if (!_initOnce) | ||||
|             { | ||||
|                 LogE("Mgr not ready, call Init first."); | ||||
|                 return; | ||||
|             } | ||||
|             Instance.FetchAllConfigs(immediately); | ||||
|         } | ||||
| 
 | ||||
|         public static void AddDefaultValues(Dictionary<string, object> dict) | ||||
|         { | ||||
|             if (!_initOnce) return; | ||||
|             if (!_initOnce) | ||||
|             { | ||||
|                 LogE("Mgr not ready, call Init first."); | ||||
|                 return; | ||||
|             } | ||||
|             Instance.AppendDefaultValues(dict); | ||||
|         } | ||||
| 
 | ||||
|  | @ -500,7 +508,6 @@ namespace Guru | |||
| 
 | ||||
|         #endregion | ||||
|          | ||||
|          | ||||
|         #region Log | ||||
| 
 | ||||
|         private static void LogI(string msg, params object[] args) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue