From e3ca104899e2ce08ca91ea562411c5005b96141e Mon Sep 17 00:00:00 2001 From: florian Date: Thu, 17 Jan 2019 21:49:07 +0100 Subject: [PATCH] Add Calendar Month widget Adds a widget that displays a whole month. You can tap on a day to open the calendar on that day. Closes #162 --- res/layout/appwidget_month.xml | 43 +++++ res/layout/widget_week_item.xml | 170 ++++++++++++++++++ .../widget/CalendarMonthAppWidget.java | 169 +++++++++++++++++ 3 files changed, 382 insertions(+) create mode 100644 res/layout/appwidget_month.xml create mode 100644 res/layout/widget_week_item.xml create mode 100644 src/com/android/calendar/widget/CalendarMonthAppWidget.java diff --git a/res/layout/appwidget_month.xml b/res/layout/appwidget_month.xml new file mode 100644 index 0000000000..980d910db0 --- /dev/null +++ b/res/layout/appwidget_month.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/widget_week_item.xml b/res/layout/widget_week_item.xml new file mode 100644 index 0000000000..a22260cdc5 --- /dev/null +++ b/res/layout/widget_week_item.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/com/android/calendar/widget/CalendarMonthAppWidget.java b/src/com/android/calendar/widget/CalendarMonthAppWidget.java new file mode 100644 index 0000000000..0a1b31827b --- /dev/null +++ b/src/com/android/calendar/widget/CalendarMonthAppWidget.java @@ -0,0 +1,169 @@ +package com.android.calendar.widget; + +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.provider.CalendarContract; +import android.text.format.DateUtils; +import android.text.format.Time; +import android.util.Log; +import android.widget.RemoteViews; + +import com.android.calendar.AllInOneActivity; +import com.android.calendar.DayOfMonthCursor; +import com.android.calendar.Utils; + +import java.util.Calendar; +import java.util.GregorianCalendar; + +import ws.xsoh.etar.R; + +/** + * Implementation of App Widget functionality. + */ +public class CalendarMonthAppWidget extends AppWidgetProvider { + + private static int header_day_labels[] = new int[]{ + R.id.d0_label, + R.id.d1_label, + R.id.d2_label, + R.id.d3_label, + R.id.d4_label, + R.id.d5_label, + R.id.d6_label + }; + + private static int[] day_labels = { + R.id.date_day_0, + R.id.date_day_1, + R.id.date_day_2, + R.id.date_day_3, + R.id.date_day_4, + R.id.date_day_5, + R.id.date_day_6 + }; + + private static int[] day_ids = { + R.id.day_0, + R.id.day_1, + R.id.day_2, + R.id.day_3, + R.id.day_4, + R.id.day_5, + R.id.day_6 + }; + +/* private static int[] day_event_ids = { + R.id.day_0_event, + R.id.day_1_event, + R.id.day_2_event, + R.id.day_3_event, + R.id.day_4_event, + R.id.day_5_event, + R.id.day_6_event + }; +*/ + + static void generateDayEvents(Context context, RemoteViews views) { + + } + + static void showWeek(Context context, RemoteViews views, int week, int selected_month, DayOfMonthCursor cursor, boolean showWeekNumber) { + RemoteViews week_view = new RemoteViews(context.getPackageName(), R.layout.widget_week_item); + views.addView(R.id.weeks, week_view); + + Calendar today = new GregorianCalendar(); + + for(int i = 0; i < day_labels.length; ++i){ + int label_id = day_labels[i]; + int day_id = day_ids[i]; + + int day = cursor.getSelectedDayOfMonth(); + int month = cursor.getMonth(); + int year = cursor.getYear(); + + week_view.setTextViewText(label_id, String.valueOf(day)); + generateDayEvents(context, week_view); + + if(year == today.get(Calendar.YEAR) && month == today.get(Calendar.MONTH) && day == today.get(Calendar.DAY_OF_MONTH)) { + week_view.setInt(day_id, "setBackgroundResource", R.color.month_today_bgcolor); + } else if(month == selected_month) { + week_view.setInt(day_id, "setBackgroundResource", R.color.month_bgcolor); + } else { + week_view.setInt(day_id, "setBackgroundResource", R.color.agenda_past_days_bar_background_color); + } + cursor.right(); + } + } + + static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, + int appWidgetId) { + Time time = new Time(); + time.setToNow(); + + int firstDayOfWeek = Utils.getFirstDayOfWeek(context); + boolean showWeekNumber = Utils.getShowWeekNumber(context); + + int year = time.year; + int month = time.month; + + int maxDays = time.getActualMaximum(Time.MONTH_DAY); + int daysPerWeek = Utils.getDaysPerWeek(context); + int startWeek = time.getWeekNumber() - time.monthDay/daysPerWeek; + int endWeek = startWeek + maxDays/7; + + int startDay = time.monthDay - (time.monthDay/ daysPerWeek)*daysPerWeek - ((10 - firstDayOfWeek) % 7); + + // Construct the RemoteViews object + RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_month); + views.setTextViewText(R.id.month_name, Utils.formatMonthYear(context, time)); + + for(int offset = 0; offset < daysPerWeek; ++offset) { + views.setTextViewText(header_day_labels[offset], + DateUtils.getDayOfWeekString((firstDayOfWeek + offset) % daysPerWeek + 1, + DateUtils.LENGTH_MEDIUM).toUpperCase()); + } + + views.removeAllViews(R.id.weeks); + DayOfMonthCursor cursor = new DayOfMonthCursor(year, month, Math.max(startDay, 0), firstDayOfWeek); + while(startDay <= 1) { + cursor.left(); + startDay++; + } + for(int week = startWeek; week <= endWeek; ++week) { + showWeek(context, views, week, month, cursor, showWeekNumber); + startDay += daysPerWeek; + } + + // Launch calendar app when the user taps on the header + final Intent launchCalendarIntent = new Intent(Intent.ACTION_VIEW); + launchCalendarIntent.setClass(context, AllInOneActivity.class); + final PendingIntent launchCalendarPendingIntent = PendingIntent.getActivity( + context, 0 /* no requestCode */, launchCalendarIntent, 0 /* no flags */); + views.setOnClickPendingIntent(R.id.month_name, launchCalendarPendingIntent); + + // Instruct the widget manager to update the widget + appWidgetManager.updateAppWidget(appWidgetId, views); + } + + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // There may be multiple widgets active, so update all of them + for (int appWidgetId : appWidgetIds) { + updateAppWidget(context, appWidgetManager, appWidgetId); + } + } + + @Override + public void onEnabled(Context context) { + } + + @Override + public void onDisabled(Context context) { + // Enter relevant functionality for when the last widget is disabled + } +} +