private int countTasks(Filter filter) { String queryTemplate = PermaSql.replacePlaceholders(filter.getSqlQuery()); TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID).withQueryTemplate(queryTemplate)); try { return cursor.getCount(); } finally { cursor.close(); } }
/** Recalculate all sizes */ void updateList() { int max = 0, last = -1; StringBuilder sql = new StringBuilder(Query.select(new CountProperty()).from(Task.TABLE).toString()) .append(" WHERE "); for (int i = 0; i < adapter.getCount(); i++) { CriterionInstance instance = adapter.getItem(i); String value = instance.getValueFromCriterion(); if (value == null && instance.criterion.sql != null && instance.criterion.sql.contains("?")) { value = ""; } switch (instance.type) { case CriterionInstance.TYPE_ADD: sql.append("OR "); break; case CriterionInstance.TYPE_SUBTRACT: sql.append("AND NOT "); break; case CriterionInstance.TYPE_INTERSECT: sql.append("AND "); break; case CriterionInstance.TYPE_UNIVERSE: } // special code for all tasks universe if (instance.criterion.sql == null) { sql.append(TaskCriteria.activeAndVisible()).append(' '); } else { String subSql = instance.criterion.sql.replace("?", UnaryCriterion.sanitize(value)); subSql = PermaSql.replacePlaceholders(subSql); sql.append(Task.ID).append(" IN (").append(subSql).append(") "); } Cursor cursor = database.rawQuery(sql.toString()); try { cursor.moveToNext(); instance.start = last == -1 ? cursor.getInt(0) : last; instance.end = cursor.getInt(0); last = instance.end; max = Math.max(max, last); } finally { cursor.close(); } } for (int i = 0; i < adapter.getCount(); i++) { CriterionInstance instance = adapter.getItem(i); instance.max = max; } adapter.notifyDataSetInvalidated(); }
@SuppressWarnings("nls") public RemoteViews buildUpdate(Context context, int widgetId) { DependencyInjectionService.getInstance().inject(this); RemoteViews views = null; views = new RemoteViews(context.getPackageName(), R.layout.widget_initialized); int[] textIDs = TEXT_IDS; int[] separatorIDs = SEPARATOR_IDS; int numberOfTasks = 5; for (int i = 0; i < textIDs.length; i++) views.setTextViewText(textIDs[i], ""); TodorooCursor<Task> cursor = null; Filter filter = null; try { filter = getFilter(widgetId); views.setTextViewText(R.id.widget_title, filter.title); SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(this); int flags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0); int sort = publicPrefs.getInt(SortHelper.PREF_SORT_SORT, 0); String query = SortHelper.adjustQueryForFlagsAndSort(filter.sqlQuery, flags, sort) .replaceAll("LIMIT \\d+", "") + " LIMIT " + numberOfTasks; database.openForReading(); cursor = taskService.fetchFiltered( query, null, Task.ID, Task.TITLE, Task.DUE_DATE, Task.COMPLETION_DATE); Task task = new Task(); for (int i = 0; i < cursor.getCount() && i < numberOfTasks; i++) { cursor.moveToPosition(i); task.readFromCursor(cursor); String textContent = ""; int textColor = Color.WHITE; textContent = task.getValue(Task.TITLE); if (task.isCompleted()) textColor = context.getResources().getColor(R.color.task_list_done); else if (task.hasDueDate() && task.getValue(Task.DUE_DATE) < DateUtilities.now()) textColor = context.getResources().getColor(R.color.task_list_overdue); if (i > 0) views.setViewVisibility(separatorIDs[i - 1], View.VISIBLE); views.setTextViewText(textIDs[i], textContent); views.setTextColor(textIDs[i], textColor); } for (int i = cursor.getCount() - 1; i < separatorIDs.length; i++) { if (i >= 0) views.setViewVisibility(separatorIDs[i], View.INVISIBLE); } } catch (Exception e) { // can happen if database is not ready Log.e("WIDGET-UPDATE", "Error updating widget", e); } finally { if (cursor != null) cursor.close(); } updateForScreenSize(views); Intent listIntent = new Intent(context, TaskListActivity.class); String customIntent = Preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_INTENT + widgetId); if (customIntent != null) { listIntent.setComponent(ComponentName.unflattenFromString(customIntent)); String serializedExtras = Preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_EXTRAS + widgetId); Bundle extras = AndroidUtilities.bundleFromSerializedString(serializedExtras); listIntent.putExtras(extras); } listIntent.putExtra(TaskListActivity.TOKEN_SOURCE, Constants.SOURCE_WIDGET); listIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); if (filter != null) { listIntent.putExtra(TaskListActivity.TOKEN_FILTER, filter); listIntent.setAction("L" + widgetId + filter.sqlQuery); } else { listIntent.setAction("L" + widgetId); } PendingIntent pListIntent = PendingIntent.getActivity( context, widgetId, listIntent, PendingIntent.FLAG_CANCEL_CURRENT); views.setOnClickPendingIntent(R.id.taskbody, pListIntent); Intent editIntent = new Intent(context, TaskEditActivity.class); editIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); editIntent.putExtra(TaskEditActivity.OVERRIDE_FINISH_ANIM, false); if (filter != null && filter.valuesForNewTasks != null) { String values = AndroidUtilities.contentValuesToSerializedString(filter.valuesForNewTasks); values = PermaSql.replacePlaceholders(values); editIntent.putExtra(TaskEditActivity.TOKEN_VALUES, values); editIntent.setAction("E" + widgetId + values); } else { editIntent.setAction("E" + widgetId); } PendingIntent pEditIntent = PendingIntent.getActivity(context, -widgetId, editIntent, 0); views.setOnClickPendingIntent(R.id.widget_button, pEditIntent); views.setOnClickPendingIntent(R.id.widget_title, pEditIntent); return views; }