miduo_client/Assets/LuaFramework/Scripts/SDK/Tools/AndroidDeviceInfo.cs

330 lines
7.1 KiB
C#
Raw Normal View History

2020-05-09 13:31:21 +08:00
using System;
using UnityEngine;
using System.Collections.Generic;
2020-08-22 15:31:14 +08:00
using System.Runtime.InteropServices;
2021-04-19 19:36:43 +08:00
using Beebyte.Obfuscator;
2020-05-09 13:31:21 +08:00
2021-04-19 19:36:43 +08:00
[Skip]
2020-05-09 13:31:21 +08:00
public class AndroidDeviceInfo
{
private AndroidJavaObject jo;
private AndroidJavaObject context;
private static AndroidDeviceInfo _instance;
public static AndroidDeviceInfo Instance
{
get
{
if (_instance == null)
{
_instance = new AndroidDeviceInfo();
}
return _instance;
}
}
2020-08-22 15:31:14 +08:00
#if UNITY_IOS
//设备机型类型
[DllImport("__Internal")]
private static extern string m_GetDeviceBrand();
//设备机型名称
[DllImport("__Internal")]
private static extern string m_GetDeviceModel();
//设备系统版本号
[DllImport("__Internal")]
private static extern string m_GetSystemVersion();
//设备分辨率
[DllImport("__Internal")]
private static extern string m_GetScreenRatio();
//设备运营商类型
[DllImport("__Internal")]
private static extern string m_GetOperatorName();
//设备网络状态
[DllImport("__Internal")]
private static extern string m_GetNetworkType();
//获取IP
[DllImport("__Internal")]
private static extern string m_GetLocalIpAddress();
//获取设备标识
[DllImport("__Internal")]
private static extern string m_GetDeviceID();
//获取IMEI
[DllImport("__Internal")]
private static extern string m_GetIMEICode();
//获取app名
[DllImport("__Internal")]
private static extern string m_GetAppName();
//获取版本名称
[DllImport("__Internal")]
private static extern string m_GetVersionName();
//获取版本号
[DllImport("__Internal")]
private static extern int m_GetVersionCode();
//获取包名
[DllImport("__Internal")]
private static extern string m_GetPackageName();
#endif
2020-05-09 13:31:21 +08:00
private AndroidDeviceInfo()
{
2020-08-22 15:31:14 +08:00
#if UNITY_ANDROID
2020-07-16 17:00:24 +08:00
using (var up = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
context = up.GetStatic<AndroidJavaObject>("currentActivity");
2020-05-09 13:31:21 +08:00
using (var adi = new AndroidJavaClass("com.bluewhale.androidutils.AndroidDeviceInfo"))
{
jo = adi.CallStatic<AndroidJavaObject>("instance");
jo.Call("Init", context);
}
2020-07-16 17:00:24 +08:00
}
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
public void DeviceInit()
{
XDebug.Log.l("设备信息初始化");
2020-05-09 13:31:21 +08:00
}
//设备机型类型
public string GetDeviceBrand()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetDeviceBrand();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetDeviceBrand");
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//设备机型名称
public string GetDeviceModel()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetDeviceModel();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetDeviceModel");
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//设备系统版本号
public string GetSystemVersion()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetSystemVersion();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetSystemVersion");
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//设备分辨率
public string GetScreenRatio()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetScreenRatio();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetScreenRatio",context);
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//设备运营商类型
public string GetOperatorName()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetOperatorName();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetOperatorName", context);
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//设备网络状态
public string GetNetworkType()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetNetworkType();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetNetworkType", context);
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
//获取IP
public string GetLocalIpAddress()
{
string type = "";
try
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
type = m_GetLocalIpAddress();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
type = jo.CallStatic<string>("GetLocalIpAddress", context);
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
catch (Exception e)
{
Debug.LogError(e);
}
return type;
}
2020-07-15 13:44:37 +08:00
//sdk 获取设备标识
public string GetDeviceID()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetDeviceID();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-07-16 19:51:38 +08:00
return jo.CallStatic<string>("GetDeviceID");
#else
2020-08-15 15:21:56 +08:00
return "";
2020-08-15 10:13:32 +08:00
#endif
2020-07-15 13:44:37 +08:00
}
//sdk 获取IMEI
public string GetIMEICode()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetIMEICode();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-07-16 19:51:38 +08:00
return jo.CallStatic<string>("GetIMEICode");
#else
2020-08-15 15:21:56 +08:00
return "";
2020-08-15 10:13:32 +08:00
#endif
2020-07-15 13:44:37 +08:00
}
//sdk 获取app名
public string GetAppName()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetAppName();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
return jo.CallStatic<string>("getAppName");
#else
2020-08-15 15:21:56 +08:00
return "";
2020-08-15 10:13:32 +08:00
#endif
}
//sdk 获取版本名称
public string GetVersionName()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetVersionName();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
return jo.CallStatic<string>("getVersionName");
#else
2020-08-15 15:21:56 +08:00
return "";
2020-08-15 10:13:32 +08:00
#endif
}
//sdk 获取版本号
public int GetVersionCode()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetVersionCode();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
return jo.CallStatic<int>("getVersionCode");
#else
2020-08-15 15:21:56 +08:00
return 0;
2020-08-15 10:13:32 +08:00
#endif
}
//sdk 获取包名
public string GetPackageName()
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
return m_GetPackageName();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
return jo.CallStatic<string>("getPackageName");
#else
2020-08-15 15:21:56 +08:00
return "";
2020-08-15 10:13:32 +08:00
#endif
}
2020-08-22 15:31:14 +08:00
#if UNITY_IOS
//设备机型类型
[DllImport("__Internal")]
private static extern void m_CopyToClipBoard(string str);
//设备机型名称
[DllImport("__Internal")]
private static extern string m_PasteFromClipBoard();
#endif
2020-05-09 13:31:21 +08:00
//暂时存放--复制粘贴功能(本不隶属于此)
public void SetCopyValue(string str)
{
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
m_CopyToClipBoard(str);
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
jo.CallStatic("CopyToClipBoard", str);
#else
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
}
public string GetPastValue()
{
string result = "";
2020-08-15 10:13:32 +08:00
#if UNITY_IOS
2020-08-22 15:31:14 +08:00
result = m_PasteFromClipBoard();
2020-08-15 10:13:32 +08:00
#elif UNITY_ANDROID
2020-05-09 13:31:21 +08:00
result = jo.CallStatic<string>("PasteFromClipBoard", context);
#else
2020-08-15 10:13:32 +08:00
#endif
2020-05-09 13:31:21 +08:00
return result;
}
}