/*
         * Gets the first instance of an app secret corresponding to the given platform name, or returns the string
         * as-is if no identifier can be found. Logs a message if no identifiers can be found.
         */
        private static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            /*
             * If there are no colons, then there are no named identifiers, but log a message in case the developer made
             * a typing error.
             */
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            /*
             * This assumes that the key contains only lowercase letters, digits, and hyphens
             * (and that it has at least one character)
             */
            var pattern = platformIdentifier + @"=([^;]+)";
            var match   = Regex.Match(secrets, pattern);

            if (match.Value == string.Empty)
            {
                var message = "Error parsing key for '" + platformIdentifier + "'";
                throw new ArgumentException(message, nameof(platformIdentifier));
            }
            return(match.Groups[1].Value);
        }
 /// <summary>
 /// Sets the two-letter ISO country code to send to the backend.
 /// </summary>
 /// <param name="countryCode">The two-letter ISO country code. See <see href="https://www.iso.org/obp/ui/#search"/> for more information.</param>
 public static void SetCountryCode(string countryCode)
 {
     if (countryCode != null && countryCode.Length != 2)
     {
         MobileCenterLog.Error(MobileCenterLog.LogTag, $"MobileCenter accept only the two-letter ISO country code.");
         return;
     }
     DeviceInformationHelper.SetCountryCode(countryCode);
 }
Esempio n. 3
0
 public void StartInstanceAndConfigure(string appSecret, params Type[] services)
 {
     try
     {
         InstanceConfigure(appSecret);
         StartInstance(services);
     }
     catch (MobileCenterException ex)
     {
         MobileCenterLog.Warn(MobileCenterLog.LogTag, ex.Message);
     }
 }
Esempio n. 4
0
        private void SetInstanceCustomProperties(CustomProperties customProperties)
        {
            if (customProperties == null || customProperties.Properties.Count == 0)
            {
                MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom properties may not be null or empty");
                return;
            }
            var customPropertiesLog = new CustomPropertiesLog();

            customPropertiesLog.Properties = customProperties.Properties;
            _channel.Enqueue(customPropertiesLog);
        }
Esempio n. 5
0
 public void StartInstanceAndConfigure(string appSecret, params Type[] services)
 {
     try
     {
         InstanceConfigure(appSecret);
         StartInstance(services);
     }
     catch (MobileCenterException ex)
     {
         var message = _instanceConfigured ? StartErrorMessage : ConfigurationErrorMessage;
         MobileCenterLog.Error(MobileCenterLog.LogTag, message, ex);
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Start services.
 /// This may be called only once per service per application process lifetime.
 /// </summary>
 /// <param name="services">List of services to use.</param>
 public static void Start(params Type[] services)
 {
     lock (MobileCenterLock)
     {
         try
         {
             Instance.StartInstance(services);
         }
         catch (MobileCenterException ex)
         {
             MobileCenterLog.Error(MobileCenterLog.LogTag, StartErrorMessage, ex);
         }
     }
 }
Esempio n. 7
0
 /// <summary>
 /// Configure the SDK.
 /// This may be called only once per application process lifetime.
 /// </summary>
 /// <param name="appSecret">A unique and secret key used to identify the application.</param>
 public static void Configure(string appSecret)
 {
     lock (MobileCenterLock)
     {
         try
         {
             Instance.InstanceConfigure(appSecret);
         }
         catch (MobileCenterException ex)
         {
             MobileCenterLog.Error(MobileCenterLog.LogTag, ConfigurationErrorMessage, ex);
         }
     }
 }
Esempio n. 8
0
        internal void StartInstance(params Type[] services)
        {
            if (services == null)
            {
                throw new MobileCenterException("Services array is null.");
            }
            if (!_instanceConfigured)
            {
                throw new MobileCenterException("Mobile Center has not been configured.");
            }

            var startServiceLog = new StartServiceLog();

            foreach (var serviceType in services)
            {
                if (serviceType == null)
                {
                    MobileCenterLog.Warn(MobileCenterLog.LogTag, "Skipping null service. Please check that you did not pass a null argument.");
                    continue;
                }
                try
                {
                    // We don't support distribute in UWP, not even a custom start.
                    if (IsDistributeService(serviceType))
                    {
                        MobileCenterLog.Warn(MobileCenterLog.LogTag, "Distribute service is not yet supported on UWP.");
                    }
                    else
                    {
                        var serviceInstance = serviceType.GetRuntimeProperty("Instance")?.GetValue(null) as IMobileCenterService;
                        if (serviceInstance == null)
                        {
                            throw new MobileCenterException("Service type does not contain static 'Instance' property of type IMobileCenterService");
                        }
                        StartService(serviceInstance);
                        startServiceLog.Services.Add(serviceInstance.ServiceName);
                    }
                }
                catch (MobileCenterException ex)
                {
                    MobileCenterLog.Warn(MobileCenterLog.LogTag, $"Failed to start service '{serviceType.Name}'; skipping it.", ex);
                }
            }

            // Enqueue a log indicating which services have been initialized
            if (startServiceLog.Services.Count > 0)
            {
                _channel.Enqueue(startServiceLog);
            }
        }
Esempio n. 9
0
 private void StartService(IMobileCenterService service)
 {
     if (service == null)
     {
         throw new MobileCenterException("Attempted to start an invalid Mobile Center service.");
     }
     if (_services.Contains(service))
     {
         MobileCenterLog.Warn(MobileCenterLog.LogTag, $"Mobile Center has already started the service with class name '{service.GetType().Name}'");
         return;
     }
     service.OnChannelGroupReady(_channelGroup, _appSecret);
     _services.Add(service);
     MobileCenterLog.Info(MobileCenterLog.LogTag, $"'{service.GetType().Name}' service started.");
 }
        /// <summary>
        /// Initialize the SDK with the list of services to start.
        /// This may be called only once per application process lifetime.
        /// </summary>
        /// <param name="appSecret">A unique and secret key used to identify the application.</param>
        /// <param name="services">List of services to use.</param>
        public static void Start(string appSecret, params Type[] services)
        {
            string parsedSecret;

            try
            {
                parsedSecret = GetSecretForPlatform(appSecret, PlatformIdentifier);
            }
            catch (ArgumentException ex)
            {
                MobileCenterLog.Assert(MobileCenterLog.LogTag, ex.Message);
                return;
            }
            AndroidMobileCenter.Start(SetWrapperSdkAndGetApplication(), parsedSecret, GetServices(services));
        }
Esempio n. 11
0
 private CustomProperties SetObject(string key, object value)
 {
     if (ValidateKey(key))
     {
         if (value == null)
         {
             MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" value cannot be null, did you mean to call clear?");
         }
         else
         {
             Properties[key] = value;
         }
     }
     return(this);
 }
Esempio n. 12
0
        private void StartService(IMobileCenterService service)
        {
            if (service == null)
            {
                throw new MobileCenterException("Service instance is null; static 'Instance' property either doesn't exist or returned null");
            }
            if (_services.Contains(service))
            {
                ThrowStartedServiceException(service.GetType().Name);
            }

            service.OnChannelGroupReady(_channelGroup, _appSecret);
            _services.Add(service);
            MobileCenterLog.Info(MobileCenterLog.LogTag, $"'{service.GetType().Name}' service started.");
        }
        static void PlatformStart(string appSecret, params Type[] services)
        {
            SetWrapperSdk();
            string parsedSecret;

            try
            {
                parsedSecret = GetSecretForPlatform(appSecret, PlatformIdentifier);
            }
            catch (MobileCenterException ex)
            {
                MobileCenterLog.Assert(MobileCenterLog.LogTag, ex.Message);
                return;
            }
            iOSMobileCenter.Start(parsedSecret, GetServices(services));
        }
Esempio n. 14
0
        private void SetInstanceCustomProperties(CustomProperties customProperties)
        {
            if (!Configured)
            {
                MobileCenterLog.Error(MobileCenterLog.LogTag, "Mobile Center hasn't been configured. You need to call MobileCenter.Start with appSecret or MobileCenter.Configure first.");
                return;
            }
            if (customProperties == null || customProperties.Properties.Count == 0)
            {
                MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom properties may not be null or empty");
                return;
            }
            var customPropertiesLog = new CustomPropertiesLog();

            customPropertiesLog.Properties = customProperties.Properties;
            _channel.EnqueueAsync(customPropertiesLog);
        }
Esempio n. 15
0
        // Gets the first instance of an app secret corresponding to the given platform name, or returns the string
        // as-is if no identifier can be found. Logs a message if no identifiers can be found.
        internal static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            if (string.IsNullOrEmpty(secrets))
            {
                throw new MobileCenterException("App secrets string is null or empty");
            }

            // If there are no equals signs, then there are no named identifiers, but log a message in case the developer made
            // a typing error.
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            var parseErrorMessage = $"Error parsing key for '{platformIdentifier}'";

            var platformIndicator = platformIdentifier + "=";
            var secretIdx         = secrets.IndexOf(platformIndicator, StringComparison.Ordinal);

            if (secretIdx == -1)
            {
                throw new MobileCenterException(parseErrorMessage);
            }
            secretIdx += platformIndicator.Length;
            var platformSecret = string.Empty;

            while (secretIdx < secrets.Length)
            {
                var nextChar = secrets[secretIdx++];
                if (nextChar == ';')
                {
                    break;
                }

                platformSecret += nextChar;
            }

            if (platformSecret == string.Empty)
            {
                throw new MobileCenterException(parseErrorMessage);
            }

            return(platformSecret);
        }
        /*
         * Gets the first instance of an app secret corresponding to the given platform name, or returns the string
         * as-is if no identifier can be found. Logs a message if no identifiers can be found.
         */
        internal static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            if (string.IsNullOrEmpty(secrets))
            {
                throw new MobileCenterException("App secrets string is null or empty");
            }

            /*
             * If there are no colons, then there are no named identifiers, but log a message in case the developer made
             * a typing error.
             */
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            var parseErrorMessage = $"Error parsing key for '{platformIdentifier}'";

            /*
             * This assumes that the key contains only lowercase letters, digits, and hyphens
             * (and that it has at least one character)
             */
            var pattern = platformIdentifier + @"=([^;]+)";

            try
            {
                var match = Regex.Match(secrets, pattern);
                if (match.Value == string.Empty)
                {
                    throw new MobileCenterException(parseErrorMessage);
                }
                return(match.Groups[1].Value);
            }
            catch (ArgumentException e)
            {
                throw new MobileCenterException(parseErrorMessage, e);
            }
            catch (RegexMatchTimeoutException e)
            {
                throw new MobileCenterException(parseErrorMessage, e);
            }
        }
Esempio n. 17
0
 public CustomProperties PlatformSet(string key, string value)
 {
     if (ValidateKey(key))
     {
         if (value == null)
         {
             MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" value cannot be null, did you mean to call clear?");
         }
         else if (value.Length > MaxCustomPropertiesStringValueLength)
         {
             MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" value length cannot be longer than " + MaxCustomPropertiesStringValueLength + " characters.");
         }
         else
         {
             Properties[key] = value;
         }
     }
     return(this);
 }
Esempio n. 18
0
        // Internal for testing
        internal void InstanceConfigure(string appSecretOrSecrets)
        {
            if (_instanceConfigured)
            {
                MobileCenterLog.Warn(MobileCenterLog.LogTag, "Mobile Center may only be configured once.");
                return;
            }
            _appSecret = GetSecretForPlatform(appSecretOrSecrets, PlatformIdentifier);

            // If a factory has been supplied, use it to construct the channel group - this is designed for testing.
            // Normal scenarios will use new ChannelGroup(string).
            _channelGroup = _channelGroupFactory?.CreateChannelGroup(_appSecret) ?? new ChannelGroup(_appSecret);
            ApplicationLifecycleHelper.Instance.UnhandledExceptionOccurred += (sender, e) => _channelGroup.ShutdownAsync();
            _channel = _channelGroup.AddChannel(ChannelName, Constants.DefaultTriggerCount, Constants.DefaultTriggerInterval,
                                                Constants.DefaultTriggerMaxParallelRequests);
            if (_logUrl != null)
            {
                _channelGroup.SetLogUrl(_logUrl);
            }
            _instanceConfigured = true;
            MobileCenterLog.Assert(MobileCenterLog.LogTag, "Mobile Center SDK configured successfully.");
        }
Esempio n. 19
0
 private bool ValidateKey(string key)
 {
     if (key == null || !KeyPattern.IsMatch(key))
     {
         MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" must match \"" + KeyPattern + "\"");
         return(false);
     }
     if (key.Length > MaxCustomPropertiesKeyLength)
     {
         MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" key length cannot be longer than " + MaxCustomPropertiesKeyLength + " characters.");
         return(false);
     }
     if (Properties.ContainsKey(key))
     {
         MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom property \"" + key + "\" is already set or cleared and will be overridden.");
     }
     else if (Properties.Count >= MaxCustomPropertiesCount)
     {
         MobileCenterLog.Error(MobileCenterLog.LogTag, "Custom properties cannot contain more than " + MaxCustomPropertiesCount + " items.");
         return(false);
     }
     return(true);
 }