Skip to content

Commit

Permalink
Add Calendar Month widget
Browse files Browse the repository at this point in the history
Adds a widget that displays a whole month.
You can tap on a day to open the calendar on that day.

Closes Etar-Group#162
  • Loading branch information
Dragoncraft89 committed Jan 17, 2019
1 parent 2cfb9c5 commit e3ca104
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 0 deletions.
43 changes: 43 additions & 0 deletions res/layout/appwidget_month.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:padding="@dimen/widget_margin"
android:focusable="true">

<TextView
android:id="@+id/month_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/actionbar_cursor"
android:textAlignment="center"
android:textColor="@color/appwidget_title"
android:textSize="26sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/line_background"
android:orientation="vertical">

<include
android:id="@+id/day_names"
layout="@layout/mini_month_header"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:id="@+id/weeks"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>


</LinearLayout>

</LinearLayout>
170 changes: 170 additions & 0 deletions res/layout/widget_week_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="horizontal">

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0.5dp"
android:layout_weight="1"
android:background="@color/month_bgcolor">

<LinearLayout
android:id="@+id/day_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/date_day_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="right"
android:textColor="@color/agenda_day_item_text_color" />
</LinearLayout>
</LinearLayout>

</LinearLayout>
169 changes: 169 additions & 0 deletions src/com/android/calendar/widget/CalendarMonthAppWidget.java
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit e3ca104

Please sign in to comment.