From f75fd5ae76c9168b6b0848fd62610dd251061a96 Mon Sep 17 00:00:00 2001 From: Yuri Volkov Date: Sun, 4 Aug 2019 20:54:43 +0300 Subject: [PATCH] #308 Fix for a case when no Tasks or no Calendars are selected --- .../provider/MockCalendarContentProvider.java | 19 +++++++++++++++---- .../calendar/CalendarEventProvider.java | 8 +++++--- .../todoagenda/task/AbstractTaskProvider.java | 16 ++++++++++++++-- .../task/dmfs/DmfsOpenTasksProvider.java | 12 +----------- .../task/samsung/SamsungTasksProvider.java | 12 +----------- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/app/src/androidTest/java/org/andstatus/todoagenda/provider/MockCalendarContentProvider.java b/app/src/androidTest/java/org/andstatus/todoagenda/provider/MockCalendarContentProvider.java index 4d603e08..f5182889 100644 --- a/app/src/androidTest/java/org/andstatus/todoagenda/provider/MockCalendarContentProvider.java +++ b/app/src/androidTest/java/org/andstatus/todoagenda/provider/MockCalendarContentProvider.java @@ -17,6 +17,7 @@ import org.andstatus.todoagenda.EventAppWidgetProvider; import org.andstatus.todoagenda.calendar.CalendarEvent; import org.andstatus.todoagenda.prefs.ApplicationPreferences; +import org.andstatus.todoagenda.prefs.EventSource; import org.andstatus.todoagenda.prefs.InstanceSettings; import org.andstatus.todoagenda.prefs.MockSettingsProvider; import org.andstatus.todoagenda.util.DateUtil; @@ -27,11 +28,11 @@ import org.json.JSONObject; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; -import static junit.framework.Assert.assertTrue; import static org.andstatus.todoagenda.prefs.ApplicationPreferences.PREF_WIDGET_ID; import static org.andstatus.todoagenda.provider.QueryResultsStorage.KEY_SETTINGS; @@ -85,8 +86,18 @@ private void setPreferences(Context context) throws JSONException { if (InstanceSettings.getInstances(context).isEmpty()) { InstanceSettings.save(context, widgetId.incrementAndGet()); } - InstanceSettings settings = InstanceSettings.fromId(context, widgetId.incrementAndGet()); - assertTrue("widgetId:" + getWidgetId(), settings.isJustCreated()); + + ApplicationPreferences.startEditing(context, widgetId.incrementAndGet()); + List sources = new ArrayList<>(); + sources.add(new EventSource(EventProviderType.CALENDAR, 1, "", "", 0xFF)); + for(int i = 0; i < numberOfOpenTaskSources; i++) { + sources.add(new EventSource(EventProviderType.DMFS_OPEN_TASKS, 2 + i, + getClass().getSimpleName() + ".task" + i, "my.task@example.com", 0x0FF0000)); + } + ApplicationPreferences.setActiveEventSources(context, sources); + ApplicationPreferences.save(context, widgetId.get()); + + InstanceSettings settings = InstanceSettings.fromId(context, widgetId.get()); JSONObject json = settings.toJson(); JSONArray jsonArray = new JSONArray(); jsonArray.put(json); @@ -123,7 +134,7 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel MatrixCursor cursor = new MatrixCursor(projection); for(int i = 0; i < numberOfOpenTaskSources; i++) { - cursor.addRow(new Object[]{2L + i, getClass().getSimpleName() + ".task" + i, 0x0FF0000, + cursor.addRow(new Object[]{2 + i, getClass().getSimpleName() + ".task" + i, 0x0FF0000, "my.task@example.com"}); } return cursor; diff --git a/app/src/main/java/org/andstatus/todoagenda/calendar/CalendarEventProvider.java b/app/src/main/java/org/andstatus/todoagenda/calendar/CalendarEventProvider.java index 0590182a..40631d9d 100644 --- a/app/src/main/java/org/andstatus/todoagenda/calendar/CalendarEventProvider.java +++ b/app/src/main/java/org/andstatus/todoagenda/calendar/CalendarEventProvider.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -41,10 +42,11 @@ public CalendarEventProvider(EventProviderType type, Context context, int widget super(type, context, widgetId); } - public List getEvents() { + List getEvents() { initialiseParameters(); - if (PermissionsUtil.isPermissionNeeded(context, type.permission)) { - return new ArrayList<>(); + if (PermissionsUtil.isPermissionNeeded(context, type.permission) || + getSettings().getActiveEventSources(type).isEmpty()) { + return Collections.emptyList(); } List eventList = getTimeFilteredEventList(); if (getSettings().getShowPastEventsWithDefaultColor()) { diff --git a/app/src/main/java/org/andstatus/todoagenda/task/AbstractTaskProvider.java b/app/src/main/java/org/andstatus/todoagenda/task/AbstractTaskProvider.java index 563586cd..c7eae668 100644 --- a/app/src/main/java/org/andstatus/todoagenda/task/AbstractTaskProvider.java +++ b/app/src/main/java/org/andstatus/todoagenda/task/AbstractTaskProvider.java @@ -3,11 +3,13 @@ import android.content.Context; import android.content.Intent; +import org.andstatus.todoagenda.provider.EventProvider; import org.andstatus.todoagenda.provider.EventProviderType; import org.andstatus.todoagenda.util.DateUtil; -import org.andstatus.todoagenda.provider.EventProvider; +import org.andstatus.todoagenda.util.PermissionsUtil; import org.joda.time.DateTime; +import java.util.Collections; import java.util.List; public abstract class AbstractTaskProvider extends EventProvider { @@ -25,7 +27,17 @@ protected void initialiseParameters() { now = DateUtil.now(zone); } - public abstract List getEvents(); + public List getEvents() { + initialiseParameters(); + if (PermissionsUtil.isPermissionNeeded(context, type.permission) || + getSettings().getActiveEventSources(type).isEmpty()) { + return Collections.emptyList(); + } + + return queryTasks(); + } + + public abstract List queryTasks(); public abstract Intent createViewEventIntent(TaskEvent event); } diff --git a/app/src/main/java/org/andstatus/todoagenda/task/dmfs/DmfsOpenTasksProvider.java b/app/src/main/java/org/andstatus/todoagenda/task/dmfs/DmfsOpenTasksProvider.java index fb5dfc17..a902910e 100644 --- a/app/src/main/java/org/andstatus/todoagenda/task/dmfs/DmfsOpenTasksProvider.java +++ b/app/src/main/java/org/andstatus/todoagenda/task/dmfs/DmfsOpenTasksProvider.java @@ -14,7 +14,6 @@ import org.andstatus.todoagenda.task.AbstractTaskProvider; import org.andstatus.todoagenda.task.TaskEvent; import org.andstatus.todoagenda.util.CalendarIntentUtil; -import org.andstatus.todoagenda.util.PermissionsUtil; import java.util.ArrayList; import java.util.Collection; @@ -29,16 +28,7 @@ public DmfsOpenTasksProvider(EventProviderType type, Context context, int widget } @Override - public List getEvents() { - initialiseParameters(); - if (PermissionsUtil.isPermissionNeeded(context, type.permission)) { - return new ArrayList<>(); - } - - return queryTasks(); - } - - private List queryTasks() { + public List queryTasks() { Uri uri = DmfsOpenTasksContract.Tasks.PROVIDER_URI; String[] projection = { DmfsOpenTasksContract.Tasks.COLUMN_ID, diff --git a/app/src/main/java/org/andstatus/todoagenda/task/samsung/SamsungTasksProvider.java b/app/src/main/java/org/andstatus/todoagenda/task/samsung/SamsungTasksProvider.java index ba3c16d7..1460bfdc 100644 --- a/app/src/main/java/org/andstatus/todoagenda/task/samsung/SamsungTasksProvider.java +++ b/app/src/main/java/org/andstatus/todoagenda/task/samsung/SamsungTasksProvider.java @@ -16,7 +16,6 @@ import org.andstatus.todoagenda.task.AbstractTaskProvider; import org.andstatus.todoagenda.task.TaskEvent; import org.andstatus.todoagenda.util.CalendarIntentUtil; -import org.andstatus.todoagenda.util.PermissionsUtil; import java.util.ArrayList; import java.util.Collection; @@ -31,16 +30,7 @@ public SamsungTasksProvider(EventProviderType type, Context context, int widgetI } @Override - public List getEvents() { - initialiseParameters(); - if (PermissionsUtil.isPermissionNeeded(context, type.permission)) { - return new ArrayList<>(); - } - - return queryTasks(); - } - - private List queryTasks() { + public List queryTasks() { Uri uri = SamsungTasksContract.Tasks.PROVIDER_URI; String[] projection = { SamsungTasksContract.Tasks.COLUMN_ID,