/// <summary>
        /// Pushes a notification alert for the user
        /// </summary>
        /// <param name="message">The message to notify</param>
        public void PushAlertNotification(NotificationMeasure message)
        {
            // Create pending intent, mention the Activity which needs to be
            //triggered when user clicks on notification(StopScript.class in this case)
            var resultIntent = new Intent(this, typeof(MainActivity));

            resultIntent.SetFlags(ActivityFlags.PreviousIsTop);
            //resultIntent.SetAction(Intent.ActionMain);
            resultIntent.AddCategory(Intent.CategoryLauncher);

            // Construct a back stack for cross-task navigation:
            var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(this);

            stackBuilder.AddNextIntent(resultIntent);

            // Create the PendingIntent with the back stack:
            var resultPendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.CancelCurrent);

            // Build the notification:
            var builder = new NotificationCompat.Builder(this, CHANNEL_ALERT_ID)
                          .SetAutoCancel(true)                                    // Dismiss the notification from the notification area when the user clicks on it
                          .SetContentIntent(resultPendingIntent)                  // Start up this activity when the user clicks the intent.
                          .SetContentTitle("Alerte glycémie")                     // Set the title
                          .SetSmallIcon(Resource.Drawable.sigle_logo_splash_grey) // This is the icon to display
                          .SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_alerte_orange))
                          .SetContentText($"{message.NotificationMessage}");      // the message to display.

            // Finally, publish the notification:
            var notificationManager = NotificationManagerCompat.From(this);

            CreateAlertChannel();
            notificationManager.Notify(MEASURE_ALERT_ID, builder.Build());
        }
        public void RefreshWidget()
        {
            if (lastNotificationMeasure != null)
            {
                NotificationMeasure cloned = new NotificationMeasure(lastNotificationMeasure);
                cloned.IsAlert = false;

                IEventAggregator eventAggregator = (IEventAggregator)App.Current.Container.Resolve(typeof(IEventAggregator));
                eventAggregator.GetEvent <NotificationMeasureEvent>().Publish(cloned);
            }
        }
        /// <summary>
        /// Return the new measure into an adequate string
        /// </summary>
        /// <param name="notification"></param>
        /// <returns></returns>
        private string NewMeasureToString(NotificationMeasure notification)
        {
            AppSettings settings = new AppSettings();
            string      result   = String.Empty;

            if (notification.NewMeasure <= settings.LO_VALUE)
            {
                result = "Lo";
            }
            else if (settings.HI_VALUE <= notification.NewMeasure)
            {
                result = "Hi";
            }
            else
            {
                result = notification.NewMeasure.ToString();
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Spreads the last measure on the application (database, UI, alerts)
        /// </summary>
        /// <param name="lastMeasure"></param>
        private void SpreadLastMeasure(float lastMeasure)
        {
            var  currentUser          = this.userRepository.GetCurrentUser();
            var  userSettings         = this.userSettingsRepository.GetCurrentUserSettings();
            long countExistingMesures = this.glucoseMeasureRepository.CountByUser(currentUser.Id);
            bool isFirstReading       = countExistingMesures == 0;

            // 1 - Driving mode
            // NOTE : We only handle MGDL Measure for the H&D MVP
            this.userRepository.ChangeCurrentMeasure(currentUser, lastMeasure);

            // 2 The notification channel
            var notification = new NotificationMeasure();

            notification.NotificationMessage    = string.Format(Utils.GetTranslation("NotificationMessageCurrentMeasure"), HiLowValueHelper.NewMeasureToString(lastMeasure));
            notification.NewMeasure             = lastMeasure;
            notification.MinimumGlucoseTreshold = userSettings.MinimumGlucoseTreshold;
            notification.MaximumGlucoseTreshold = userSettings.MaximumGlucoseTreshold;
            notification.IsAlert = userRepository.GetCurrentUser().IsAlert;

            //Current Trend management //
            GlucoseMeasure pastFifteenMinMeasure      = glucoseMeasureRepository.GetPastFifteenMinutesMeasureByUser(currentUser.Id);
            float?         pastFifteenMinMeasureValue = null;

            if (pastFifteenMinMeasure != null)
            {
                pastFifteenMinMeasureValue = pastFifteenMinMeasure.GlucoseLevelMGDL;
            }

            //Trend management : Builder //
            switch (TrendHelper.ValuesToTypeTrend(lastMeasure, pastFifteenMinMeasureValue))
            {
            case MeasureTrend.IncreasingHeavy:
                notification.MeasureTrend = MeasureTrend.IncreasingHeavy;
                break;

            case MeasureTrend.Increasing:
                notification.MeasureTrend = MeasureTrend.Increasing;
                break;

            case MeasureTrend.Constant:
                notification.MeasureTrend = MeasureTrend.Constant;
                break;

            case MeasureTrend.Decreasing:
                notification.MeasureTrend = MeasureTrend.Decreasing;
                break;

            case MeasureTrend.DecreasingHeavy:
                notification.MeasureTrend = MeasureTrend.DecreasingHeavy;
                break;

            default:
                notification.MeasureTrend = MeasureTrend.None;
                break;
            }

            //Trend management : Notifications //
            //Save the measure trend in the user data
            this.userRepository.UpdateMeasureTrend(currentUser, notification.MeasureTrend);
            //Send the measure trend event to the drive page
            this.eventAggregator.GetEvent <TrendEvent>().Publish(notification.MeasureTrend);
            //Send the measure trend event to the notification
            this.eventAggregator.GetEvent <NotificationMeasureEvent>().Publish(notification);

            // 3 - Alerts raised to the user ?
            if (userRepository.GetCurrentUser().IsAlert)
            {
                if (lastMeasure > userSettings.MaximumGlucoseTreshold || lastMeasure < userSettings.MinimumGlucoseTreshold)
                {
                    notification.NotificationMessage = string.Format(Utils.GetTranslation("NotificationMessageAlertGlycemia"), HiLowValueHelper.NewMeasureToString(lastMeasure));
                    this.eventAggregator.GetEvent <PushNotificationAlertEvent>().Publish(notification);
                }
            }

            this.precedingMeasure = lastMeasure;
        }
        public void RefreshDatas(NotificationMeasure notification)
        {
            try
            {
                bloodSugarLevel.SetText(NewMeasureToString(notification), TextView.BufferType.Normal);

                // compute arrow
                switch (notification.MeasureTrend)
                {
                case MeasureTrend.Constant:
                    ArrowDirection = WidgetArrowDirection.right;
                    break;

                case MeasureTrend.Decreasing:
                    ArrowDirection = WidgetArrowDirection.diagonal_down_right;
                    break;

                case MeasureTrend.DecreasingHeavy:
                    ArrowDirection = WidgetArrowDirection.down;
                    break;

                case MeasureTrend.Increasing:
                    ArrowDirection = WidgetArrowDirection.diagonal_up_right;
                    break;

                case MeasureTrend.IncreasingHeavy:
                    ArrowDirection = WidgetArrowDirection.up;
                    break;

                default:
                    ArrowDirection = WidgetArrowDirection.right;
                    break;
                }

                //compute color
                float  currentValue = notification.NewMeasure;
                string color        = this.appSettings.COLOR_RED;
                //Rouge low
                if (currentValue < notification.MinimumGlucoseTreshold - (notification.MinimumGlucoseTreshold * 25 / 100))
                {
                    color = this.appSettings.COLOR_RED;
                }
                //Orange foncé low
                else if (notification.MinimumGlucoseTreshold - (notification.MinimumGlucoseTreshold * 25 / 100) <= currentValue && currentValue < notification.MinimumGlucoseTreshold - (notification.MinimumGlucoseTreshold * 15 / 100))
                {
                    color = this.appSettings.COLOR_ORANGE;
                }
                //Orange low
                else if (notification.MinimumGlucoseTreshold - (notification.MinimumGlucoseTreshold * 15 / 100) <= currentValue && currentValue < notification.MinimumGlucoseTreshold)
                {
                    color = this.appSettings.COLOR_YELLOW;
                }
                //Vert
                else if (notification.MinimumGlucoseTreshold <= currentValue && currentValue < notification.MaximumGlucoseTreshold)
                {
                    color = this.appSettings.COLOR_GREEN;
                }
                //Orange high
                else if (notification.MaximumGlucoseTreshold <= currentValue && currentValue < notification.MaximumGlucoseTreshold + (notification.MaximumGlucoseTreshold * 20 / 100))
                {
                    color = this.appSettings.COLOR_YELLOW;
                }
                //Orange foncé high
                else if (notification.MaximumGlucoseTreshold + (notification.MaximumGlucoseTreshold * 20 / 100) <= currentValue && currentValue < notification.MaximumGlucoseTreshold + (notification.MaximumGlucoseTreshold * 35 / 100))
                {
                    color = this.appSettings.COLOR_ORANGE;
                }
                //Rouge high
                else if (notification.MaximumGlucoseTreshold + (notification.MaximumGlucoseTreshold * 35 / 100) <= currentValue)
                {
                    color = this.appSettings.COLOR_RED;
                }
                bloodSugarLevel.SetTextColor(ColorConverters.FromHex(color).ToPlatformColor());
                unit.SetTextColor(ColorConverters.FromHex(color).ToPlatformColor());
                bloodSugarEvolution.SetTextColor(ColorConverters.FromHex(color).ToPlatformColor());
            }
            catch (Exception e)
            {
                Log.Error("MeasureWidgetService", $"RefreshDatas : {e.Message}");
            }
        }
        public override void OnCreate()
        {
            base.OnCreate();

            floatingView = LayoutInflater.From(this).Inflate(Resource.Layout.floatingWidget, null);

            bloodSugarLevel     = floatingView.FindViewById <TextView>(Resource.Id.bloodSugarLevel);
            unit                = floatingView.FindViewById <TextView>(Resource.Id.unit);
            bloodSugarEvolution = floatingView.FindViewById <TextView>(Resource.Id.bloodSugarEvolution);

            appSettings = (AppSettings)App.Current.Container.Resolve(typeof(AppSettings));

            //Repo init
            userRepository = (IUserRepository)App.Current.Container.Resolve(typeof(IUserRepository));
            IUserSettingsRepository userSettingsRepository = (IUserSettingsRepository)App.Current.Container.Resolve(typeof(IUserSettingsRepository));
            var  userSettings = userSettingsRepository.GetCurrentUserSettings();
            User currentUsr   = userRepository.GetCurrentUser();

            SetTouchListener();

            windowManager = GetSystemService(WindowService).JavaCast <IWindowManager>();

            WindowManagerTypes LAYOUT_FLAG;

            if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
            {
                LAYOUT_FLAG = WindowManagerTypes.ApplicationOverlay;
            }
            else
            {
                LAYOUT_FLAG = WindowManagerTypes.Phone;
            }

            layoutParams = new WindowManagerLayoutParams(ViewGroup.LayoutParams.WrapContent,
                                                         ViewGroup.LayoutParams.WrapContent,
                                                         LAYOUT_FLAG,
                                                         WindowManagerFlags.NotFocusable,
                                                         Format.Translucent)
            {
                Gravity = GravityFlags.Left | GravityFlags.CenterVertical,
                X       = currentUsr.WidgetPositionX,
                Y       = currentUsr.WidgetPositionY
            };
            if (Settings.CanDrawOverlays(this))
            {
                windowManager.AddView(floatingView, layoutParams);
            }

            SetCenter();

            //Fill the widget with default values
            DefaultState();

            //Startup data refreshed management
            DateTimeOffset empty = new DateTimeOffset();

            if (currentUsr != null && currentUsr.GetMeasureTrend() != MeasureTrend.None)
            {
                NotificationMeasure notif = new NotificationMeasure();
                notif.NewMeasure             = currentUsr.CurrentMeasure;
                notif.MaximumGlucoseTreshold = userSettings.MaximumGlucoseTreshold;
                notif.MinimumGlucoseTreshold = userSettings.MinimumGlucoseTreshold;
                notif.MeasureTrend           = currentUsr.GetMeasureTrend();
                RefreshDatas(notif);
            }

            IEventAggregator eventAggregator = (IEventAggregator)App.Current.Container.Resolve(typeof(IEventAggregator));

            actionRefresh = new Action <NotificationMeasure>((notification) => { this.RefreshDatas(notification); });
            eventAggregator.GetEvent <NotificationMeasureEvent>().Subscribe(actionRefresh);

            eventAggregator.GetEvent <ExitApplicationEvent>().Publish();

            Analytics.TrackEvent(AnalyticsEvent.WidgetViewed);
        }
        /// <summary>
        /// Pushes a notification measure for the user
        /// </summary>
        /// <param name="message">The message to notify</param>
        /// <param name="trend">The trending for the measures</param>
        public void PushMeasureNotification(NotificationMeasure message)
        {
            // Create pending intent, mention the Activity which needs to be
            //triggered when user clicks on notification(StopScript.class in this case)
            var resultIntent = new Intent(this, typeof(MainActivity));

            resultIntent.SetFlags(ActivityFlags.PreviousIsTop);
            //resultIntent.SetAction(Intent.ActionMain);
            resultIntent.AddCategory(Intent.CategoryLauncher);

            // Construct a back stack for cross-task navigation:
            var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(this);

            stackBuilder.AddNextIntent(resultIntent);

            lastNotificationMeasure = message;

            // Create the PendingIntent with the back stack:
            var resultPendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.CancelCurrent);

            // Build the notification:
            var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                          .SetAutoCancel(true)                                    // Dismiss the notification from the notification area when the user clicks on it
                          .SetContentIntent(resultPendingIntent)                  // Start up this activity when the user clicks the intent.
                          .SetContentTitle("Mesure de glycémie")                  // Set the title
                          .SetSmallIcon(Resource.Drawable.sigle_logo_splash_grey) // This is the icon to display
                          .SetContentText($"{message.NotificationMessage}");      // the message to display.

            switch (message.MeasureTrend)
            {
            case MeasureTrend.IncreasingHeavy:
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_arrows_top_blue));
                break;

            case MeasureTrend.Increasing:
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_arrows_top_mid_blue));
                break;

            case MeasureTrend.Constant:
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_arrows_straight_blue));
                break;

            case MeasureTrend.Decreasing:
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_arrows_bot_mid_blue));
                break;

            case MeasureTrend.DecreasingHeavy:
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.ic_arrows_bottom_blue));
                break;
            }

            // Finally, publish the notification:
            var notificationManager = NotificationManagerCompat.From(this);

            notificationManager.Notify(MEASURE_NOTIFICATION_ID, builder.Build());

            if (message.IsAlert)
            {
                PhysicalNotification(MeasureZoneHelper.ComputeMeasureZone(message.NewMeasure, message.MinimumGlucoseTreshold, message.MaximumGlucoseTreshold));
            }
        }
 /// <summary>
 /// Update the current value
 /// </summary>
 /// <param name="notification"></param>
 private void ChangeCurrentValue(NotificationMeasure notification)
 {
     CurrentValue = notification.NewMeasure;
     GlucoseValue = HiLowValueHelper.NewMeasureToString(CurrentValue);
 }