public override void OnAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)
        {
            var           defconStatus  = GetApplicationDefconStatus().ToString();
            var           widgetHeight  = newOptions.GetInt(AppWidgetManager.OptionAppwidgetMinHeight);
            Intent        intent        = new Intent(context, typeof(MainActivity));
            PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, 0);
            ComponentName componentName = new ComponentName(context, Java.Lang.Class.FromType(typeof(MyDefconWidget)).Name);
            RemoteViews   remoteViews   = new RemoteViews(context.PackageName, Resource.Layout.mydefcon_widget);

            remoteViews.SetTextViewTextSize(Resource.Id.mydefconWidgetTextView, 2, widgetHeight * (float)0.5);
            remoteViews.SetTextViewText(Resource.Id.mydefconWidgetTextView, defconStatus);
            remoteViews.SetTextColor(Resource.Id.mydefconWidgetTextView, GetLightColor(defconStatus));
            remoteViews.SetInt(Resource.Id.mydefconWidgetLinearLayout, "setBackgroundColor", GetDarkColor(defconStatus));
            remoteViews.SetInt(Resource.Id.mydefconFrameLayout, "setBackgroundColor", GetLightColor(defconStatus));
            remoteViews.SetOnClickPendingIntent(Resource.Id.mydefconWidgetLinearLayout, pendingIntent);
            appWidgetManager.UpdateAppWidget(componentName, remoteViews);
        }
Esempio n. 2
0
        public static void Update(Context context, NextAlarm nextAlarm, int[] appWidgetIds = null)
        {
            AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
            ComponentName    thisWidget       = new ComponentName(context, Class.FromType(typeof(Widget)).Name);

            if (appWidgetIds == null)
            {
                appWidgetIds = appWidgetManager.GetAppWidgetIds(thisWidget);
            }
            var prefs = PrefsKeys.GetPrefs(context);

            foreach (var appWidgetId in appWidgetIds)
            {
                var timeOffset = prefs.GetInt(PrefsKeys.TimeOffset + appWidgetId, 0);
                nextAlarm.RefreshDisplay(context, timeOffset);

                RemoteViews updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
                var         useTodDom   = prefs.GetBoolean(PrefsKeys.DateUseTodTom + appWidgetId, true);
                if (useTodDom)
                {
                    updateViews.SetTextViewText(Resource.Id.alarm_date, nextAlarm.DayAbbreviated);
                }
                else
                {
                    updateViews.SetTextViewText(Resource.Id.alarm_date, nextAlarm.Day);
                }
                if (nextAlarm.Time.Length > 5)
                {
                    updateViews.SetTextViewText(Resource.Id.alarm_time_12, nextAlarm.Time);
                    updateViews.SetViewVisibility(Resource.Id.alarm_time_12, ViewStates.Visible);
                    updateViews.SetViewVisibility(Resource.Id.alarm_time_24, ViewStates.Gone);
                }
                else
                {
                    updateViews.SetTextViewText(Resource.Id.alarm_time_24, nextAlarm.Time);
                    updateViews.SetViewVisibility(Resource.Id.alarm_time_24, ViewStates.Visible);
                    updateViews.SetViewVisibility(Resource.Id.alarm_time_12, ViewStates.Gone);
                }

                var dateColor = new Color(prefs.GetInt(PrefsKeys.DateColor + appWidgetId, context.GetCompatColor(Resource.Color.date)));
                updateViews.SetTextColor(Resource.Id.alarm_date, dateColor);
                var dateTextSize = prefs.GetInt(PrefsKeys.DateTextSize + appWidgetId, -1);
                if (dateTextSize != -1)
                {
                    updateViews.SetTextViewTextSize(Resource.Id.alarm_date, (int)ComplexUnitType.Dip, dateTextSize);
                }


                var timeColor = new Color(prefs.GetInt(PrefsKeys.TimeColor + appWidgetId, context.GetCompatColor(Resource.Color.time)));
                updateViews.SetTextColor(Resource.Id.alarm_time_24, timeColor);
                updateViews.SetTextColor(Resource.Id.alarm_time_12, timeColor);

                var timeTextSize = prefs.GetInt(PrefsKeys.TimeTextSize + appWidgetId, -1);
                if (timeTextSize != -1)
                {
                    updateViews.SetTextViewTextSize(Resource.Id.alarm_time_24, (int)ComplexUnitType.Dip, timeTextSize);
                    updateViews.SetTextViewTextSize(Resource.Id.alarm_time_12, (int)ComplexUnitType.Dip, timeTextSize);
                }

                var         iconColor    = new Color(prefs.GetInt(PrefsKeys.IconColor + appWidgetId, context.GetCompatColor(Resource.Color.icon)));
                Bitmap      sourceBitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.ic_alarm_white_18dp);
                Bitmap      resultBitmap = Bitmap.CreateBitmap(sourceBitmap.Width, sourceBitmap.Height, Bitmap.Config.Argb8888);
                Paint       p            = new Paint();
                float[]     matrix       = { 0, 0, 0, iconColor.R / 255f, 0,
                                             0,           0, 0, iconColor.G / 255f, 0,
                                             0,           0, 0, iconColor.B / 255f, 0,
                                             0,           0, 0, iconColor.A / 255f, 0 };
                ColorFilter filter = new ColorMatrixColorFilter(matrix);
                p.SetColorFilter(filter);
                Canvas canvas = new Canvas(resultBitmap);
                canvas.DrawBitmap(sourceBitmap, 0, 0, p);
                updateViews.SetImageViewBitmap(Resource.Id.alarm_icon, resultBitmap);

                var backgroundColor = new Color(prefs.GetInt(PrefsKeys.BackgroundColor + appWidgetId, context.GetCompatColor(Resource.Color.background)));
                updateViews.SetInt(Resource.Id.background, "setColorFilter", backgroundColor.ToArgb());
                updateViews.SetInt(Resource.Id.background, "setAlpha", backgroundColor.A);

                updateViews.SetOnClickPendingIntent(Resource.Id.widget_root, nextAlarm.RelayIntent);
                appWidgetManager.UpdateAppWidget(appWidgetId, updateViews);
            }

            ScheduleMidnightUpdate(context);
        }