basic widget functionality + misc cleanup

This commit is contained in:
tibbi 2016-01-30 22:32:21 +01:00
parent 90f1122eac
commit 975c625fb2
4 changed files with 83 additions and 10 deletions

View file

@ -15,15 +15,14 @@ public class CalendarImpl {
private final String today;
private DateTime targetDate;
public CalendarImpl(Calendar callback, DateTime targetDate) {
public CalendarImpl(Calendar callback) {
this.callback = callback;
this.targetDate = targetDate;
today = new DateTime().toString(DATE_PATTERN);
}
public void updateCalendar(DateTime targetDateTime) {
this.targetDate = targetDateTime;
getDays(targetDateTime);
public void updateCalendar(DateTime targetDate) {
this.targetDate = targetDate;
getDays(targetDate);
getMonthName();
}

View file

@ -46,9 +46,8 @@ public class MainActivity extends AppCompatActivity implements MyDatePickerDialo
dayTextSize /= getResources().getDisplayMetrics().density;
todayTextSize /= getResources().getDisplayMetrics().density;
DateTime now = new DateTime();
calendar = new CalendarImpl(this, now);
calendar.updateCalendar(now);
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
setupLabelSizes();
}

View file

@ -1,13 +1,88 @@
package calendar.simplemobiletools.com;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
import org.joda.time.DateTime;
import java.util.List;
public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private static final String PREV = "prev";
private static final String NEXT = "next";
private static RemoteViews remoteViews;
private static int[] widgetIds;
private static AppWidgetManager widgetManager;
private static Intent intent;
private static Context cxt;
private static CalendarImpl calendar;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
initVariables(context);
}
private void initVariables(Context context) {
cxt = context;
final ComponentName component = new ComponentName(cxt, MyWidgetProvider.class);
widgetManager = AppWidgetManager.getInstance(cxt);
widgetIds = widgetManager.getAppWidgetIds(component);
remoteViews = new RemoteViews(cxt.getPackageName(), R.layout.activity_main);
remoteViews.setInt(R.id.table_month, "setTextColor", Color.WHITE);
intent = new Intent(cxt, MyWidgetProvider.class);
setupButtons();
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
private void setupIntent(String action, int id) {
intent.setAction(action);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent);
}
private void setupButtons() {
setupIntent(PREV, R.id.left_arrow);
setupIntent(NEXT, R.id.right_arrow);
}
@Override
public void onReceive(Context context, Intent intent) {
if (remoteViews == null || widgetManager == null || widgetIds == null || calendar == null)
initVariables(context);
final String action = intent.getAction();
switch (action) {
case PREV:
calendar.getPrevMonth();
break;
case NEXT:
calendar.getNextMonth();
break;
default:
super.onReceive(context, intent);
}
}
@Override
public void updateDays(List<Day> days) {
}
@Override
public void updateMonth(String month) {
remoteViews.setTextViewText(R.id.table_month, month);
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
}

View file

@ -28,7 +28,7 @@
android:paddingBottom="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"
android:textColor="@color/darkGrey"
android:textSize="24sp"/>
android:textSize="22sp"/>
<ImageView
android:id="@+id/right_arrow"