Exemple #1
0
        public String GetUniqueID()
        {
            AndroidJavaClass unityPlayer = PlatformIntegrationUtil.GetAndroidJavaClass("com.unity3d.player.UnityPlayer");

            if (unityPlayer == null)
            {
                Logger.Log("Can't get UnityPlayer");
                return(null);
            }

            AndroidJavaObject activity = PlatformIntegrationUtil.GetStatic <AndroidJavaObject>(unityPlayer, "currentActivity");

            if (activity == null)
            {
                Logger.Log("Can't find an activity!");
                return(null);
            }

            AndroidJavaObject context = PlatformIntegrationUtil.Call <AndroidJavaObject>(activity, "getApplicationContext");

            if (context == null)
            {
                Logger.Log("Can't find an app context!");
                return(null);
            }

            AndroidJavaObject contentResolver = PlatformIntegrationUtil.Call <AndroidJavaObject>(context, "getContentResolver");

            if (contentResolver == null)
            {
                Logger.Log("Can't get content resolver from context");
                return(null);
            }

            AndroidJavaClass secureClass = PlatformIntegrationUtil.GetAndroidJavaClass("android.provider.Settings$Secure");

            if (secureClass == null)
            {
                Logger.Log("Can't get secure class");
                return(null);
            }

            AndroidJavaObject androidID = PlatformIntegrationUtil.GetStatic <AndroidJavaObject>(secureClass, "ANDROID_ID");

            if (androidID == null)
            {
                Logger.Log("Cant get Android ID static string");
                return(null);
            }

            object[] parameters = new object[2];
            parameters[0] = contentResolver;
            parameters[1] = androidID;

            string aid = PlatformIntegrationUtil.CallStatic <string>(secureClass, "getString", parameters);

            if (aid != null)
            {
                string hashedAdId = HexUtil.HexStringSha512(aid);
                return(hashedAdId);
            }
            return(aid);
        }
    // Placeholder, if available, just use the Unity version
#if UNITY_ANDROID
    public Dictionary<string, string> GetDeviceInfo()
    {
      CarrierInfoClass carrierInfo = new CarrierInfoClass();
      Dictionary<string, string> map = new Dictionary<string, string>();
      int sdk_int = carrierInfo.getAndroidSDKVers();
      map["Build.VERSION.SDK_INT"] = sdk_int.ToString();
      if (UnityEngine.XR.XRSettings.loadedDeviceName.Contains("oculus"))
      {
          return map;
      }
      AndroidJavaObject telephonyManager = carrierInfo.GetTelephonyManager();
      if (telephonyManager == null)
      {
          Logger.Log("No TelephonyManager!");
          return map;
      }
      const string readPhoneStatePermissionString = "android.permission.READ_PHONE_STATE";
      try
      {
        if (Permission.HasUserAuthorizedPermission(readPhoneStatePermissionString))
        {
          string ver = PlatformIntegrationUtil.Call<string>(telephonyManager, "getDeviceSoftwareVersion");
          if (ver != null)
          {
            map["DeviceSoftwareVersion"] = ver.ToString();
          }
        }
      }
      catch (Exception e)
      {
        Logger.LogWarning("Exception retrieving properties: " + e.GetBaseException() + ", " + e.Message);
      }

      try
      {
        if (Permission.HasUserAuthorizedPermission(readPhoneStatePermissionString))
        {
          int nType = PlatformIntegrationUtil.Call<int>(telephonyManager, "getDataNetworkType");
          NetworkDataType datatype = (NetworkDataType)nType;
          map["DataNetworkType"] = datatype.ToString();
        }
      }
      catch (Exception e)
      {
        Logger.LogWarning("Exception retrieving properties: " + e.GetBaseException() + ", " + e.Message);
      }

      AndroidJavaClass versionCodesClass = new AndroidJavaClass("android.os.Build$VERSION_CODES");
      int versionCode = PlatformIntegrationUtil.GetStatic<int>(versionCodesClass, "Q");
      if (sdk_int > versionCode)
      {
        string mc = PlatformIntegrationUtil.Call<string>(telephonyManager, "getManufacturerCode");
        if (mc != null)
        {
          map["ManufacturerCode"] = mc;
        }
      }

      string niso = PlatformIntegrationUtil.Call<string>(telephonyManager, "getNetworkCountryIso");
      if (niso != null)
      {
        map["NetworkCountryIso"] = niso;
      }

      string siso = PlatformIntegrationUtil.Call<string>(telephonyManager, "getSimCountryIso");
      if (siso != null)
      {
        map["SimCountryCodeIso"] = siso;
      }

      int phoneType = PlatformIntegrationUtil.Call<int>(telephonyManager, "getPhoneType");
      map["PhoneType"] = phoneType.ToString();

      // Default one.
      string simOperatorName = PlatformIntegrationUtil.Call<string>(telephonyManager, "getSimOperatorName");
      if (simOperatorName != null)
      {
        map["SimOperatorName"] = simOperatorName;
      }

      // Default one.
      string networkOperator = PlatformIntegrationUtil.Call<string>(telephonyManager, "getNetworkOperatorName");
      if (networkOperator != null)
      {
        map["NetworkOperatorName"] = networkOperator;
      }

      return map;
    }
        KeyValuePair <string, ulong> GetCidKeyValuePair(AndroidJavaObject cellInfo)
        {
            KeyValuePair <string, ulong> pair = new KeyValuePair <string, ulong>(null, 0);

            string            simpleName   = PlatformIntegrationUtil.GetSimpleName(cellInfo);
            AndroidJavaObject cellIdentity = PlatformIntegrationUtil.Call <AndroidJavaObject>(cellInfo, "getCellIdentity");

            if (cellIdentity == null)
            {
                Logger.Log("Unable to get cellIdentity");
                return(pair);
            }

            if (simpleName.Equals(cellInfoTdscdmaString))
            {
                int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid");
                if (cid > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)cid);
                }
            }
            else if (simpleName.Equals(cellInfoNrString))
            {
                long nci = PlatformIntegrationUtil.Call <long>(cellIdentity, "getNci");
                if (nci > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)nci);
                }
            }
            else if (simpleName.Equals(cellInfoLteString))
            {
                int ci = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCi");
                if (ci > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)ci);
                }
            }
            else if (simpleName.Equals(cellInfoGsmString))
            {
                int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid");
                if (cid > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)cid);
                }
            }
            else if (simpleName.Equals(cellInfoWcdmaString))
            {
                int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid");
                if (cid > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)cid);
                }
            }
            else if (simpleName.Equals(cellInfoCdmaString))
            {
                int baseStationId = PlatformIntegrationUtil.Call <int>(cellIdentity, "getBaseStationId");
                if (baseStationId > 0)
                {
                    pair = new KeyValuePair <string, ulong>(simpleName, (ulong)baseStationId);
                }
            }
            else
            {
                Logger.Log("Object is not an instance of a CellInfo class");
            }

            return(pair);
        }
        public List <KeyValuePair <String, ulong> > GetCellInfoList()
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                Logger.Log("Not on android device.");
                return(null);
            }

            AndroidJavaObject telManager = GetTelephonyManager();

            if (telManager == null)
            {
                Logger.Log("Can't get telephony manager!");
                return(null);
            }

            if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation))
            {
                Permission.RequestUserPermission(Permission.FineLocation);
            }

            AndroidJavaObject cellInfoList = PlatformIntegrationUtil.Call <AndroidJavaObject>(telManager, "getAllCellInfo");

            if (cellInfoList == null)
            {
                Logger.Log("Can't get list of cellInfo objects.");
                return(null);
            }

            int length = PlatformIntegrationUtil.Call <int>(cellInfoList, "size");

            if (length <= 0)
            {
                Logger.Log("Unable to get valid length for cellInfoList");
                return(null);
            }

            List <KeyValuePair <String, ulong> > cellIDList = new List <KeyValuePair <string, ulong> >();
            // KeyValuePair to compare to in case GetCidKeyValuePair returns nothing
            KeyValuePair <string, ulong> empty = new KeyValuePair <string, ulong>(null, 0);

            for (int i = 0; i < length; i++)
            {
                AndroidJavaObject cellInfo = PlatformIntegrationUtil.Call <AndroidJavaObject>(cellInfoList, "get", new object[] { i });
                if (cellInfo == null)
                {
                    continue;
                }

                bool isRegistered = PlatformIntegrationUtil.Call <bool>(cellInfo, "isRegistered");
                if (isRegistered)
                {
                    KeyValuePair <string, ulong> pair = GetCidKeyValuePair(cellInfo);
                    if (!pair.Equals(empty))
                    {
                        cellIDList.Add(pair);
                    }
                }
            }

            return(cellIDList);
        }
        public AndroidJavaObject GetTelephonyManager()
        {
            AndroidJavaClass unityPlayer = PlatformIntegrationUtil.GetAndroidJavaClass("com.unity3d.player.UnityPlayer");

            if (unityPlayer == null)
            {
                Logger.Log("Unable to get UnityPlayer");
                return(null);
            }
            AndroidJavaObject activity = PlatformIntegrationUtil.GetStatic <AndroidJavaObject>(unityPlayer, "currentActivity");

            if (activity == null)
            {
                Logger.Log("Can't find an activity!");
                return(null);
            }

            AndroidJavaObject context = PlatformIntegrationUtil.Call <AndroidJavaObject>(activity, "getApplicationContext");

            if (context == null)
            {
                Logger.Log("Can't find an app context!");
                return(null);
            }

            // Context.TELEPHONY_SERVICE:
            string CONTEXT_TELEPHONY_SERVICE = context.GetStatic <string>("TELEPHONY_SERVICE");

            if (CONTEXT_TELEPHONY_SERVICE == null)
            {
                Logger.Log("Can't get Context Telephony Service");
                return(null);
            }

            AndroidJavaObject telManager = PlatformIntegrationUtil.Call <AndroidJavaObject>(context, "getSystemService", new object[] { CONTEXT_TELEPHONY_SERVICE });

            sdkVersion = getAndroidSDKVers();

            if (sdkVersion < 24)
            {
                return(telManager);
            }

            // Call SubscriptionManager to get a specific telManager:
            AndroidJavaClass subscriptionManagerCls = PlatformIntegrationUtil.GetAndroidJavaClass("android.telephony.SubscriptionManager");

            if (subscriptionManagerCls == null)
            {
                Logger.Log("Can't get Subscription Manager Class.");
                return(null);
            }
            int subId        = PlatformIntegrationUtil.CallStatic <int>(subscriptionManagerCls, "getDefaultDataSubscriptionId");
            int invalidSubId = PlatformIntegrationUtil.GetStatic <int>(subscriptionManagerCls, "INVALID_SUBSCRIPTION_ID");

            if (subId == invalidSubId)
            {
                Logger.Log("The Subscription ID is invalid: " + subId);
                return(null);
            }
            object[] idParam = new object[1] {
                subId
            };
            telManager = PlatformIntegrationUtil.Call <AndroidJavaObject>(telManager, "createForSubscriptionId", idParam);

            return(telManager);
        }