fix: 优化 NetworkStatus代码

--story=1021014 --user=yufei.hu 【中台】【SDK】用户属性上报添加 Network 属性 https://www.tapd.cn/33527076/s/1159774

Signed-off-by: huyufei <yufei.hu@castbox.fm>
dev
胡宇飞 2024-07-26 11:40:43 +08:00
parent 345b4ac971
commit ef547caba9
4 changed files with 34 additions and 23 deletions

View File

@ -33,8 +33,14 @@ namespace Guru.Network
#elif UNITY_IOS
_proxy = new NetworkStatusIOS();
#endif
}
if (_proxy == null)
{
// 如果未实现则报异常
throw new NotImplementedException("Can't find NetworkStatusProxy instance!!");
}
return _proxy;
}
@ -79,12 +85,13 @@ namespace Guru.Network
statusName = _statusNameList[i];
if (status.Contains(statusName))
{
Debug.Log($"{Tag} --- GetNetworkStatus : {statusName}");
return statusName;
}
}
}
}
Debug.LogWarning($"{Tag} --- Network Monitor is not ready!");
return NETWORK_STATUS_NONE;
}

View File

@ -7,7 +7,7 @@ namespace Guru.Network
public class NetworkStatusAndroid: INetworkStatusProxy
{
private const string Tag = "[NET]";
private const string Tag = "[NET][AND]";
#if UNITY_ANDROID
private const string CONNECTIVITY_ANDROID_CLASS_NAME = "com.guru.unity.monitor.Connectivity";
private AndroidJavaObject _connectivity;
@ -26,9 +26,8 @@ namespace Guru.Network
AndroidJavaObject currentActivity = new AndroidJavaObject("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
_connectivity = new AndroidJavaObject(CONNECTIVITY_ANDROID_CLASS_NAME).CallStatic<AndroidJavaObject>("getInstance");
_isReady = _connectivity.Call<bool>("initialize", currentActivity);
Debug.LogWarning($"{Tag} --- Network status init result:{_isReady}");
Debug.LogWarning($"{Tag} --- _connectivity:{_connectivity}");
// Debug.LogWarning($"{Tag} --- Network status init result:{_isReady}");
// Debug.LogWarning($"{Tag} --- _connectivity:{_connectivity}");
onInitComplete?.Invoke(_isReady); // 调用初始化回调
#endif
}
@ -40,11 +39,11 @@ namespace Guru.Network
public string[] GetNetworkStatus()
{
#if UNITY_ANDROID
if (_connectivity == null)
if (!_isReady)
{
Debug.LogError($"{Tag} --- GetNetworkStatus :: _connectivity is Null!!");
Debug.LogError($"{Tag} --- GetNetworkStatus :: initialized failed.");
}
else
else if(_connectivity != null)
{
return _connectivity.Call<string[]>("checkConnectionType");
}

View File

@ -10,7 +10,7 @@ namespace Guru.Network
public class NetworkStatusEditor:INetworkStatusProxy
{
private const string Tag = "[NET][EDT]";
/// <summary>
/// 初始化
/// </summary>
@ -18,7 +18,7 @@ namespace Guru.Network
public async void Init(Action<bool> onInitComplete)
{
await Task.Delay(300);
Debug.Log($"[NET][EDT] --- init NetworkStatusEditor success int Editor");
Debug.Log($"{Tag} --- init NetworkStatusEditor success int Editor");
onInitComplete?.Invoke(true);
}

View File

@ -5,11 +5,11 @@ namespace Guru.Network
using System;
using System.Runtime.InteropServices;
using AOT;
using UnityEngine;
public class NetworkStatusIOS:INetworkStatusProxy
{
public const string Tag = "[NET][iOS]";
#if UNITY_IOS
[DllImport("__Internal")]
private static extern IntPtr checkNetworkConnection();
@ -28,15 +28,13 @@ namespace Guru.Network
[MonoPInvokeCallback(typeof(InitializationCallback))]
private static void OnInitializationComplete(bool success)
{
_isReady = success;
_initCompleteCallback?.Invoke(success);
}
#endif
#if UNITY_IOS
#endif
#endif
private static bool _isReady = false;
public void Init(Action<bool> onInitComplete)
{
@ -49,10 +47,17 @@ namespace Guru.Network
public string[] GetNetworkStatus()
{
#if UNITY_IOS
IntPtr ptr = checkNetworkConnection();
string result = Marshal.PtrToStringAnsi(ptr);
freeCheckNetworkConnectionResult(ptr);
return result.Split(',');
if(!_isReady)
{
Debug.LogError($"{Tag} --- GetNetworkStatus :: initialized failed.");
}
else
{
IntPtr ptr = checkNetworkConnection();
string result = Marshal.PtrToStringAnsi(ptr);
freeCheckNetworkConnectionResult(ptr);
return result.Split(',');
}
#endif
return new string[] {"none"};
}