Ejemplo n.º 1
0
        private Drawable CreateUserIconDrawable()
        {
            int           dp16       = PixelSizeConverter.DpToPx(16);
            int           dp12       = PixelSizeConverter.DpToPx(12);
            int           dp4        = PixelSizeConverter.DpToPx(4);
            var           fillColor  = new Color(ResourcesCompat.GetColor(_layout.Resources, Resource.Color.user_location, null));
            var           ringColor  = new Color(ResourcesCompat.GetColor(_layout.Resources, Resource.Color.user_location_stroke, null));
            var           pointColor = new Color(ResourcesCompat.GetColor(_layout.Resources, Resource.Color.user_location_center, null));
            LayerDrawable drw        = new LayerDrawable(new Drawable[] { CircleDrawable(ringColor, dp16), CircleDrawable(fillColor, dp12), CircleDrawable(pointColor, dp4) });

            drw.SetLayerSize(0, dp16, dp16);
            drw.SetLayerSize(1, dp12, dp12);
            drw.SetLayerSize(2, dp4, dp4);
            drw.SetLayerGravity(0, GravityFlags.Center);
            drw.SetLayerGravity(1, GravityFlags.Center);
            drw.SetLayerGravity(2, GravityFlags.Center);
            return(drw);
        }
        public static void SendLocalNotification(Context ctx, string notificationChannelId, string title, int smallIconResId, Color smallIconColor, string notificationCatagory,
                                                 int notificationVisibility, int notificationId = 0, string notificationTag = "Edison", PendingIntent pendingIntent = null, bool autoCancel = true,
                                                 string summaryContent = null, int contentIconResId = 0, int color = -1, NotificationCompat.Style style = null, string groupKey = null)
        {
            var notificationManager = NotificationManager.FromContext(ctx);
            var channel             = notificationManager.GetNotificationChannel(notificationChannelId);

            if (channel == null)
            {
                return;
            }

            //Map channel importance (Android 8+) to notification importance (android 7)
            int priority = NotificationCompat.PriorityDefault;

            switch (channel.Importance)
            {
            case NotificationImportance.Max:
                priority = NotificationCompat.PriorityMax;
                break;

            case NotificationImportance.High:
                priority = NotificationCompat.PriorityHigh;
                break;

            case NotificationImportance.Low:
                priority = NotificationCompat.PriorityLow;
                break;

            case NotificationImportance.Min:
                priority = NotificationCompat.PriorityMin;
                break;

            default:
                priority = NotificationCompat.PriorityDefault;
                break;
            }
            // ensure visibility is valid
            if (notificationVisibility > NotificationCompat.VisibilityPublic || notificationVisibility < NotificationCompat.VisibilitySecret)
            {
                notificationVisibility = NotificationCompat.VisibilityPrivate;
            }

            var notificationBuilder = new NotificationCompat.Builder(ctx, notificationChannelId)
                                      .SetContentTitle(title)
                                      .SetSmallIcon(smallIconResId)
                                      .SetColor(smallIconColor)
                                      .SetPriority(priority)
                                      .SetCategory(notificationCatagory)
                                      .SetVisibility(notificationVisibility)
                                      .SetAutoCancel(autoCancel);

            if (!string.IsNullOrWhiteSpace(summaryContent))
            {
                notificationBuilder.SetContentText(summaryContent);
            }
            if (pendingIntent != null)
            {
                notificationBuilder.SetContentIntent(pendingIntent);
            }

            Bitmap icon = null;

            if (contentIconResId != 0)
            {
                if (color != -1)
                {
                    Color col = new Color(color);
                    if (contentIconResId != 0)
                    {
                        // build circular icon
                        Drawable iconDrawable = Drawables.CircularIcon(ctx, contentIconResId, Color.White, col, PixelSizeConverter.DpToPx(15), PixelSizeConverter.DpToPx(140));
                        // try to get bitmap
                        icon = iconDrawable.ToBitmap();
                    }
                }
                else
                {
                    // try to get bitmap
                    icon = BitmapFactory.DecodeResource(ctx.Resources, contentIconResId);
                }

                if (icon != null)
                {
                    notificationBuilder.SetLargeIcon(icon);
                }
            }

            if (style != null)
            {
                notificationBuilder.SetStyle(style);
            }

            if (Build.VERSION.SdkInt >= BuildVersionCodes.N && !string.IsNullOrWhiteSpace(groupKey))
            {
                notificationBuilder.SetGroup(groupKey);
            }

            notificationManager.Notify(notificationTag, notificationId, notificationBuilder.Build());

            icon?.Dispose();
        }
        public static void SendLocalNotification(Context ctx, string notificationChannelId, string title, int smallIconResId, Color smallIconColor, string notificationCatagory,
                                                 int notificationVisibility, int notificationId, string notificationTag, PendingIntent pendingIntent, bool autoCancel, string summaryTitle,
                                                 string summaryContent, int contentIconResId, Color contentIconBackgroundColor, string groupKey = null, List <NotificationCompat.Action> actions = null)
        {
            var notificationManager = NotificationManager.FromContext(ctx);
            var channel             = notificationManager.GetNotificationChannel(notificationChannelId);

            if (channel == null)
            {
                return;
            }

            //Map channel importance (Android 8+) to notification importance (android 7)
            int priority = MapToPriority(channel.Importance);

            // ensure visibility is valid
            if (notificationVisibility > NotificationCompat.VisibilityPublic || notificationVisibility < NotificationCompat.VisibilitySecret)
            {
                notificationVisibility = NotificationCompat.VisibilityPrivate;
            }

            var notificationBuilder = new NotificationCompat.Builder(ctx, notificationChannelId)
                                      .SetContentTitle(title)
                                      .SetSmallIcon(smallIconResId)
                                      .SetColor(smallIconColor)
                                      .SetPriority(priority)
                                      .SetCategory(notificationCatagory)
                                      .SetVisibility(notificationVisibility)
                                      .SetAutoCancel(autoCancel);

            if (!string.IsNullOrWhiteSpace(summaryTitle))
            {
                SpannableStringBuilder sb = new SpannableStringBuilder(summaryTitle);
                sb.SetSpan(new StyleSpan(TypefaceStyle.Bold), 0, summaryTitle.Length, SpanTypes.ExclusiveExclusive);
                notificationBuilder.SetContentText(sb);
                var content = summaryContent;
                if (!string.IsNullOrWhiteSpace(content))
                {
                    sb = new SpannableStringBuilder(summaryTitle);
                    sb.Append("\n");
                    sb.Append(summaryContent);
                    sb.SetSpan(new StyleSpan(TypefaceStyle.Bold), 0, summaryTitle.Length, SpanTypes.ExclusiveExclusive);
                    notificationBuilder.SetStyle(new NotificationCompat.BigTextStyle().BigText(sb));
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(summaryContent))
                {
                    notificationBuilder.SetContentText(summaryContent);
                    if (!string.IsNullOrWhiteSpace(summaryContent))
                    {
                        notificationBuilder.SetStyle(new NotificationCompat.BigTextStyle().BigText(summaryContent));
                    }
                }
            }


            if (pendingIntent != null)
            {
                notificationBuilder.SetContentIntent(pendingIntent);
            }

            Bitmap icon = null;

            if (contentIconResId != 0)
            {
                // build circular icon
                Drawable iconDrawable = Drawables.CircularIcon(ctx, contentIconResId, Color.White, contentIconBackgroundColor, PixelSizeConverter.DpToPx(15), PixelSizeConverter.DpToPx(140));
                // try to get bitmap
                icon = iconDrawable.ToBitmap();
                if (icon != null)
                {
                    notificationBuilder.SetLargeIcon(icon);
                }
            }


            if (Build.VERSION.SdkInt >= BuildVersionCodes.N && !string.IsNullOrWhiteSpace(groupKey))
            {
                notificationBuilder.SetGroup(groupKey);
            }

            if (actions != null && actions.Count > 0)
            {
                foreach (var action in actions)
                {
                    notificationBuilder.AddAction(action);
                }
            }

            notificationManager.Notify(notificationTag, notificationId, notificationBuilder.Build());
            icon?.Dispose();
        }
        public static void SendLocalEdisonNotification(Context ctx, string notificationChannelId, string title, int smallIconResId, Color smallIconColor, string notificationCatagory,
                                                       int notificationVisibility, int notificationId, string notificationTag, PendingIntent pendingIntent, bool autoCancel, string summaryTitle,
                                                       string messageText, int contentIconResId, Color contentIconBackgroundColor,
                                                       int collapsedLayoutResId, int expandedLayoutResId, int headsupLayoutResId,
                                                       int largeIconResId, int titleResId, int responseTitleResId, int messageTextResId, int emergencyButtonResId, int activityButtonResId, int safeButtonResId,
                                                       PendingIntent emergencyIntent, PendingIntent activityIntent, PendingIntent safeIntent, string groupKey = null)
        {
            var notificationManager = NotificationManager.FromContext(ctx);
            var channel             = notificationManager.GetNotificationChannel(notificationChannelId);

            if (channel == null)
            {
                return;
            }

            //Map channel importance (Android 8+) to notification importance (android 7)
            int priority = MapToPriority(channel.Importance);

            // ensure visibility is valid
            if (notificationVisibility > NotificationCompat.VisibilityPublic || notificationVisibility < NotificationCompat.VisibilitySecret)
            {
                notificationVisibility = NotificationCompat.VisibilityPrivate;
            }

            var notificationBuilder = new NotificationCompat.Builder(ctx, notificationChannelId)
                                      .SetContentTitle(title)
                                      .SetSmallIcon(smallIconResId)
                                      .SetColor(smallIconColor)
                                      .SetPriority(priority)
                                      .SetCategory(notificationCatagory)
                                      .SetVisibility(notificationVisibility)
                                      .SetAutoCancel(autoCancel);

            SpannableStringBuilder sb = null;

            if (!string.IsNullOrWhiteSpace(summaryTitle))
            {
                sb = new SpannableStringBuilder(summaryTitle);
                sb.SetSpan(new StyleSpan(TypefaceStyle.Bold), 0, summaryTitle.Length, SpanTypes.ExclusiveExclusive);
                notificationBuilder.SetContentText(sb);
                if (!string.IsNullOrWhiteSpace(messageText))
                {
                    sb = new SpannableStringBuilder(summaryTitle);
                    sb.Append("\n");
                    sb.Append(messageText);
                    sb.SetSpan(new StyleSpan(TypefaceStyle.Bold), 0, summaryTitle.Length, SpanTypes.ExclusiveExclusive);
                    notificationBuilder.SetStyle(new NotificationCompat.BigTextStyle().BigText(sb));
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(messageText))
                {
                    notificationBuilder.SetContentText(messageText);
                    if (!string.IsNullOrWhiteSpace(messageText))
                    {
                        notificationBuilder.SetStyle(new NotificationCompat.BigTextStyle().BigText(messageText));
                    }
                }
            }


            if (pendingIntent != null)
            {
                notificationBuilder.SetContentIntent(pendingIntent);
            }

            Bitmap icon = null;

            if (contentIconResId != 0)
            {
                // build circular icon
                Drawable iconDrawable = Drawables.CircularIcon(ctx, contentIconResId, Color.White, contentIconBackgroundColor, PixelSizeConverter.DpToPx(15), PixelSizeConverter.DpToPx(140));
                // try to get bitmap
                icon = iconDrawable.ToBitmap();
                // Don't set the large icon otherwise it takes up the right hand side of the entire notification. Instead will set in the custom view
                //             if (icon != null)
                //                 notificationBuilder.SetLargeIcon(icon);
            }


            if (Build.VERSION.SdkInt >= BuildVersionCodes.N && !string.IsNullOrWhiteSpace(groupKey))
            {
                notificationBuilder.SetGroup(groupKey);
            }


            notificationBuilder.SetStyle(new NotificationCompat.DecoratedCustomViewStyle());

            RemoteViews collapsedContent = new RemoteViews(ctx.PackageName, collapsedLayoutResId);

            if (!string.IsNullOrWhiteSpace(title))
            {
                var titleText = title;
                if (!string.IsNullOrWhiteSpace(summaryTitle))
                {
                    titleText = titleText + ": " + summaryTitle;
                }
                collapsedContent.SetTextViewText(titleResId, titleText);
            }
            else if (!string.IsNullOrWhiteSpace(summaryTitle))
            {
                collapsedContent.SetTextViewText(titleResId, summaryTitle);
            }
            if (icon != null)
            {
                collapsedContent.SetImageViewBitmap(largeIconResId, icon);
            }
            collapsedContent.SetOnClickPendingIntent(emergencyButtonResId, emergencyIntent);
            collapsedContent.SetOnClickPendingIntent(activityButtonResId, activityIntent);
            collapsedContent.SetOnClickPendingIntent(safeButtonResId, safeIntent);
            notificationBuilder.SetCustomContentView(collapsedContent);

            RemoteViews headsupContent = new RemoteViews(ctx.PackageName, headsupLayoutResId);

            if (!string.IsNullOrWhiteSpace(title))
            {
                var titleText = title;
                if (!string.IsNullOrWhiteSpace(summaryTitle))
                {
                    titleText = titleText + ": " + summaryTitle;
                }
                headsupContent.SetTextViewText(titleResId, titleText);
            }
            else if (!string.IsNullOrWhiteSpace(summaryTitle))
            {
                headsupContent.SetTextViewText(titleResId, summaryTitle);
            }
            if (icon != null)
            {
                headsupContent.SetImageViewBitmap(largeIconResId, icon);
            }
            headsupContent.SetOnClickPendingIntent(emergencyButtonResId, emergencyIntent);
            headsupContent.SetOnClickPendingIntent(activityButtonResId, activityIntent);
            headsupContent.SetOnClickPendingIntent(safeButtonResId, safeIntent);
            notificationBuilder.SetCustomHeadsUpContentView(headsupContent);


            RemoteViews expandedContent = new RemoteViews(ctx.PackageName, expandedLayoutResId);

            if (!string.IsNullOrWhiteSpace(title))
            {
                expandedContent.SetTextViewText(titleResId, title);
            }
            if (!string.IsNullOrWhiteSpace(summaryTitle))
            {
                expandedContent.SetTextViewText(responseTitleResId, summaryTitle);
            }
            if (icon != null)
            {
                expandedContent.SetImageViewBitmap(largeIconResId, icon);
            }
            if (messageText != null)
            {
                expandedContent.SetTextViewText(messageTextResId, messageText);
            }
            expandedContent.SetOnClickPendingIntent(emergencyButtonResId, emergencyIntent);
            expandedContent.SetOnClickPendingIntent(activityButtonResId, activityIntent);
            expandedContent.SetOnClickPendingIntent(safeButtonResId, safeIntent);
            notificationBuilder.SetCustomBigContentView(expandedContent);

            notificationManager.Notify(notificationTag, notificationId, notificationBuilder.Build());
            icon?.Dispose();
        }
Ejemplo n.º 5
0
            public override bool OnDependentViewChanged(CoordinatorLayout parent, Java.Lang.Object jChild, View dependency)
            {
                if (dependency is T view)
                {
                    var child = jChild.JavaCast <View>();
                    if (_dependsOnView)
                    {
                        //TODO this 4dp margin is actual shadow layout height, which is 4 dp in bottomBar library ver. 2.0.2
                        float transitionY = view.TranslationY - view.Height + (State != StateExpanded ? PixelSizeConverter.DpToPx(4) : 0);
                        child.TranslationY = System.Math.Min(transitionY, 0);
                    }

                    if (view.TranslationY >= view.Height)
                    {
                        _dependsOnView  = false;
                        view.Visibility = ViewStates.Gone;
                    }
                    if (State != StateExpanded)
                    {
                        _dependsOnView  = true;
                        view.Visibility = ViewStates.Visible;
                    }

                    return(false);
                }

                return(base.OnDependentViewChanged(parent, jChild, dependency));
            }