From 9744b85edf157a22ed61f0b86f572dadbc485e93 Mon Sep 17 00:00:00 2001 From: Trogel Date: Sun, 22 Mar 2020 16:27:22 +0100 Subject: [PATCH 1/4] Update widgets when task starts or becomes due (#17) --- .../tasks/actions/UpdateWidgetsAction.java | 53 +++++++++++++++++++ .../tasks/notification/ActionService.java | 7 ++- 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java new file mode 100644 index 000000000..7cbc7d9a9 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java @@ -0,0 +1,53 @@ +/* + * Copyright 2020 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.actions; + +import android.appwidget.AppWidgetManager; +import android.content.ComponentName; +import android.content.ContentProviderClient; +import android.content.Context; +import android.net.Uri; + +import org.dmfs.android.contentpal.RowDataSnapshot; +import org.dmfs.tasks.contract.TaskContract; +import org.dmfs.tasks.homescreen.TaskListWidgetProvider; +import org.dmfs.tasks.R; + + +/** + * A {@link TaskAction} that updates the widgets. + * + * @author Trogel + */ +public final class UpdateWidgetsAction implements TaskAction +{ + private final static String TAG = "UpdateWidgetsAction"; + + public UpdateWidgetsAction() + { + } + + + @Override + public void execute(Context context, ContentProviderClient contentProviderClient, RowDataSnapshot rowSnapshot, Uri taskUri) + { + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + final ComponentName componentName = new ComponentName(context, TaskListWidgetProvider.class); + final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName); + appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.task_list_widget_lv); + } +} diff --git a/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java b/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java index 8972b62a1..76b570820 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/notification/ActionService.java @@ -53,6 +53,7 @@ import org.dmfs.tasks.actions.PostUndoAction; import org.dmfs.tasks.actions.RemoveNotificationAction; import org.dmfs.tasks.actions.TaskAction; +import org.dmfs.tasks.actions.UpdateWidgetsAction; import org.dmfs.tasks.actions.WipeNotificationAction; import org.dmfs.tasks.contract.TaskContract; @@ -180,8 +181,10 @@ private TaskAction resolveAction(String action) case TaskContract.ACTION_BROADCAST_TASK_DUE: case TaskContract.ACTION_BROADCAST_TASK_STARTING: - // post start and due notification on the due date channel - return new NotifyStickyAction(data -> CHANNEL_DUE_DATES, true); + return new Composite( + // post start and due notification on the due date channel + new NotifyStickyAction(data -> CHANNEL_DUE_DATES, true), + new UpdateWidgetsAction()); case ACTION_UNDO_COMPLETE: return new Composite( From 4fc7dc29ca700a2c0d9b21eee1b6afd154bf4ad2 Mon Sep 17 00:00:00 2001 From: Trogel Date: Sun, 22 Mar 2020 16:34:29 +0100 Subject: [PATCH 2/4] Show task in widget in red color also if due date is equal to now Otherwise a task would wrongly remain white if its view is updated exactly at the due date. --- .../org/dmfs/tasks/homescreen/TaskListWidgetUpdaterService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentasks/src/main/java/org/dmfs/tasks/homescreen/TaskListWidgetUpdaterService.java b/opentasks/src/main/java/org/dmfs/tasks/homescreen/TaskListWidgetUpdaterService.java index 323675984..0e90d7c0e 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/homescreen/TaskListWidgetUpdaterService.java +++ b/opentasks/src/main/java/org/dmfs/tasks/homescreen/TaskListWidgetUpdaterService.java @@ -233,7 +233,7 @@ public RemoteViews getViewAt(int position) row.setTextViewText(android.R.id.text1, mDueDateFormatter.format(dueDate, DateFormatContext.WIDGET_VIEW)); // highlight overdue dates & times - if ((!dueDate.allDay && dueDate.before(mNow) || dueDate.allDay + if ((!dueDate.allDay && Time.compare(dueDate, mNow) <= 0 || dueDate.allDay && (dueDate.year < mNow.year || dueDate.yearDay <= mNow.yearDay && dueDate.year == mNow.year)) && !items[position].getIsClosed()) { From ad72be7481c55cdb12e5ae1dc8e838e57b106dae Mon Sep 17 00:00:00 2001 From: Trogel Date: Mon, 23 Mar 2020 15:30:51 +0100 Subject: [PATCH 3/4] Remove unused field --- .../main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java index 7cbc7d9a9..c11115f01 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java +++ b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java @@ -35,8 +35,6 @@ */ public final class UpdateWidgetsAction implements TaskAction { - private final static String TAG = "UpdateWidgetsAction"; - public UpdateWidgetsAction() { } From 33662dd9eba874b29ee0ce605265f2c25182082f Mon Sep 17 00:00:00 2001 From: Trogel Date: Mon, 23 Mar 2020 15:39:59 +0100 Subject: [PATCH 4/4] Also update TaskListWidgetProviderLarge --- .../java/org/dmfs/tasks/actions/UpdateWidgetsAction.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java index c11115f01..6541b0800 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java +++ b/opentasks/src/main/java/org/dmfs/tasks/actions/UpdateWidgetsAction.java @@ -25,6 +25,7 @@ import org.dmfs.android.contentpal.RowDataSnapshot; import org.dmfs.tasks.contract.TaskContract; import org.dmfs.tasks.homescreen.TaskListWidgetProvider; +import org.dmfs.tasks.homescreen.TaskListWidgetProviderLarge; import org.dmfs.tasks.R; @@ -44,7 +45,13 @@ public UpdateWidgetsAction() public void execute(Context context, ContentProviderClient contentProviderClient, RowDataSnapshot rowSnapshot, Uri taskUri) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); - final ComponentName componentName = new ComponentName(context, TaskListWidgetProvider.class); + notifyTaskListDataChanged(appWidgetManager, new ComponentName(context, TaskListWidgetProvider.class)); + notifyTaskListDataChanged(appWidgetManager, new ComponentName(context, TaskListWidgetProviderLarge.class)); + } + + + private void notifyTaskListDataChanged(AppWidgetManager appWidgetManager, ComponentName componentName) + { final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName); appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.task_list_widget_lv); }