public static NextAlarm ObtainFromSystem(Context context) { var nextAlarm = new NextAlarm(); AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService); var alarmInfo = alarmManager.NextAlarmClock; if (alarmInfo != null) { long alarmUnixTime = alarmInfo.TriggerTime; nextAlarm._alarmDateTime = UnixDateToLocalDateTime(alarmUnixTime); if (alarmInfo.ShowIntent != null) { nextAlarm._showAlarmIntent = alarmInfo.ShowIntent; } } else { nextAlarm.Time = "--:--"; } nextAlarm._showAllAlarmsIntent = BuildShowAlarmsSystemIntent(context); var relayIntent = new Intent(context, typeof(TapReceiver)); nextAlarm.RelayIntent = PendingIntent.GetBroadcast(context, 0, relayIntent, PendingIntentFlags.UpdateCurrent); return(nextAlarm); }
public override void OnReceive(Context context, Intent intent) { #if DEBUG Toast.MakeText(context, "Received tap intent!", ToastLength.Short).Show(); #endif var nextAlarm = NextAlarm.ObtainFromSystem(context); nextAlarm.Show(context); WidgetsUpdater.Update(context, nextAlarm); }
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); }
public static void Update(Context context, int[] appWidgetIds = null) { var nextAlarm = NextAlarm.ObtainFromSystem(context); Update(context, nextAlarm, appWidgetIds); }