using System; using System.Collections.Generic; using System.Text; using Plugins.Countly.Models; namespace Plugins.Countly.Impl { public class CountlyUtils : ICountlyUtils { private static readonly StringBuilder Builder = new StringBuilder(); private readonly CountlyBase _countly; public CountlyUtils(CountlyBase countly) { _countly = countly; } public string GetUniqueDeviceId() { #if UNITY_IOS return UnityEngine.iOS.Device.advertisingIdentifier; #else return UnityEngine.SystemInfo.deviceUniqueIdentifier; #endif } /// /// Gets the base url to make requests to the Countly server. /// /// public string GetBaseInputUrl() { return string.Format( _countly.Initialization.ServerUrl[_countly.Initialization.ServerUrl.Length - 1] == '/' ? "{0}i?" : "{0}/i?", _countly.Initialization.ServerUrl); } /// /// Gets the base url to make remote configrequests to the Countly server. /// /// public string GetBaseOutputUrl() { return string.Format( _countly.Initialization.ServerUrl[_countly.Initialization.ServerUrl.Length - 1] == '/' ? "{0}o?" : "{0}/o?", _countly.Initialization.ServerUrl); } /// /// Gets the base url to make remote configrequests to the Countly server. /// /// public string GetRemoteConfigOutputUrl() { return string.Format( _countly.Initialization.ServerUrl[_countly.Initialization.ServerUrl.Length - 1] == '/' ? "{0}o/sdk?" : "{0}/o/sdk?", _countly.Initialization.ServerUrl); } /// /// Gets the least set of paramas required to be sent along with each request. /// /// public Dictionary GetBaseParams() { var baseParams = new Dictionary { {"app_key", _countly.Initialization.AppKey}, {"device_id", _countly.Device.DeviceId} }; foreach (var item in TimeMetricModel.GetTimeMetricModel()) baseParams.Add(item.Key, item.Value); if (!string.IsNullOrEmpty(_countly.OptionalParameters.CountryCode)) baseParams.Add("country_code", _countly.OptionalParameters.CountryCode); if (!string.IsNullOrEmpty(_countly.OptionalParameters.City)) baseParams.Add("city", _countly.OptionalParameters.City); if (_countly.OptionalParameters.Location != null) baseParams.Add("location", _countly.OptionalParameters.Location); return baseParams; } /// /// Gets the least set of app key and device id required to be sent along with remote config request, /// /// public Dictionary GetAppKeyAndDeviceIdParams() { return new Dictionary { {"app_key", _countly.Initialization.AppKey}, {"device_id", _countly.Device.DeviceId} }; } public bool IsNullEmptyOrWhitespace(string input) { return string.IsNullOrEmpty(input) || string.IsNullOrWhiteSpace(input); } /// /// Validates the picture format. The Countly server supports a specific set of formats only. /// /// /// public bool IsPictureValid(string pictureUrl) { if (!string.IsNullOrEmpty(pictureUrl) && pictureUrl.Contains("?")) pictureUrl = pictureUrl.Split(new[] {'?'}, StringSplitOptions.RemoveEmptyEntries)[0]; return string.IsNullOrEmpty(pictureUrl) || pictureUrl.EndsWith(".png") || pictureUrl.EndsWith(".jpg") || pictureUrl.EndsWith(".jpeg") || pictureUrl.EndsWith(".gif"); } public string GetStringFromBytes(byte[] bytes) { for (var i = 0; i < bytes.Length; i++) Builder.Append(bytes[i].ToString("x2")); return Builder.ToString(); } } }