@SuppressWarnings("nls")
  private TodorooCursor<Task> constructCursor() {
    String tagName = null;
    if (getActiveTagData() != null) tagName = getActiveTagData().getValue(TagData.NAME);

    String[] emergentTags = TagService.getInstance().getEmergentTags();
    StringProperty tagProperty =
        new StringProperty(null, TAGS_METADATA_JOIN + "." + TagService.TAG.name);

    Criterion tagsJoinCriterion =
        Criterion.and(
            Field.field(TAGS_METADATA_JOIN + "." + Metadata.KEY.name)
                .eq(TagService.KEY), // $NON-NLS-1$
            Task.ID.eq(Field.field(TAGS_METADATA_JOIN + "." + Metadata.TASK.name)),
            Criterion.not(tagProperty.in(emergentTags)));
    if (tagName != null)
      tagsJoinCriterion =
          Criterion.and(
              tagsJoinCriterion,
              Field.field(TAGS_METADATA_JOIN + "." + TagService.TAG.name).neq(tagName));

    // TODO: For now, we'll modify the query to join and include the task rabbit and tag data here.
    // Eventually, we might consider restructuring things so that this query is constructed
    // elsewhere.
    String joinedQuery =
        Join.left(
                    Metadata.TABLE.as(TR_METADATA_JOIN),
                    Criterion.and(
                        Field.field(TR_METADATA_JOIN + "." + Metadata.KEY.name)
                            .eq(TaskRabbitMetadata.METADATA_KEY), // $NON-NLS-1$
                        Task.ID.eq(Field.field(TR_METADATA_JOIN + "." + Metadata.TASK.name))))
                .toString() //$NON-NLS-1$
            + Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN), tagsJoinCriterion)
                .toString() //$NON-NLS-1$
            + filter.getSqlQuery();

    sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(joinedQuery, sortFlags, sortSort));

    String groupedQuery;
    if (sqlQueryTemplate.get().contains("GROUP BY")) groupedQuery = sqlQueryTemplate.get();
    else if (sqlQueryTemplate.get().contains("ORDER BY")) // $NON-NLS-1$
    groupedQuery =
          sqlQueryTemplate
              .get()
              .replace("ORDER BY", "GROUP BY " + Task.ID + " ORDER BY"); // $NON-NLS-1$
    else groupedQuery = sqlQueryTemplate.get() + " GROUP BY " + Task.ID;
    sqlQueryTemplate.set(groupedQuery);

    // Peform query
    try {
      return taskService.fetchFiltered(sqlQueryTemplate.get(), null, taskProperties());
    } catch (SQLiteException e) {
      // We don't show this error anymore--seems like this can get triggered
      // by a strange bug, but there seems to not be any negative side effect.
      // For now, we'll suppress the error
      // See http://astrid.com/home#tags-7tsoi/task-1119pk
      return null;
    }
  }
Exemple #2
0
  private TodorooCursor<Task> constructCursor() {
    String tagName = null;
    if (getActiveTagData() != null) {
      tagName = getActiveTagData().getName();
    }

    Criterion tagsJoinCriterion =
        Criterion.and(
            Field.field(TAGS_METADATA_JOIN + "." + Metadata.KEY.name)
                .eq(TaskToTagMetadata.KEY), // $NON-NLS-1$
            Field.field(TAGS_METADATA_JOIN + "." + Metadata.DELETION_DATE.name).eq(0),
            Task.ID.eq(Field.field(TAGS_METADATA_JOIN + "." + Metadata.TASK.name)));
    if (tagName != null) {
      tagsJoinCriterion =
          Criterion.and(
              tagsJoinCriterion,
              Field.field(TAGS_METADATA_JOIN + "." + TaskToTagMetadata.TAG_NAME.name).neq(tagName));
    }

    // TODO: For now, we'll modify the query to join and include the things like tag data here.
    // Eventually, we might consider restructuring things so that this query is constructed
    // elsewhere.
    String joinedQuery =
        Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN), tagsJoinCriterion)
                .toString() //$NON-NLS-1$
            + Join.left(
                TaskAttachment.TABLE.as(FILE_METADATA_JOIN),
                Task.UUID.eq(Field.field(FILE_METADATA_JOIN + "." + TaskAttachment.TASK_UUID.name)))
            + filter.getSqlQuery();

    sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(joinedQuery, sortFlags, sortSort));

    String groupedQuery;
    if (sqlQueryTemplate.get().contains("GROUP BY")) {
      groupedQuery = sqlQueryTemplate.get();
    } else if (sqlQueryTemplate.get().contains("ORDER BY")) // $NON-NLS-1$
    {
      groupedQuery =
          sqlQueryTemplate
              .get()
              .replace("ORDER BY", "GROUP BY " + Task.ID + " ORDER BY"); // $NON-NLS-1$
    } else {
      groupedQuery = sqlQueryTemplate.get() + " GROUP BY " + Task.ID;
    }
    sqlQueryTemplate.set(groupedQuery);

    // Peform query
    try {
      return taskService.fetchFiltered(sqlQueryTemplate.get(), null, taskProperties());
    } catch (SQLiteException e) {
      // We don't show this error anymore--seems like this can get triggered
      // by a strange bug, but there seems to not be any negative side effect.
      // For now, we'll suppress the error
      // See http://astrid.com/home#tags-7tsoi/task-1119pk
      log.error(e.getMessage(), e);
      return null;
    }
  }
Exemple #3
0
  /**
   * Fill in the Task List with current items
   *
   * @param withCustomId force task with given custom id to be part of list
   */
  protected void setUpTaskList() {
    if (filter == null) return;

    // TODO: For now, we'll modify the query to join and include the task rabbit data here.
    // Eventually, we might consider restructuring things so that this query is constructed
    // elsewhere.
    String joinedTaskRabbitQuery =
        Join.left(
                Metadata.TABLE.as(TR_METADATA_JOIN),
                Criterion.and(
                    Field.field(TR_METADATA_JOIN + "." + Metadata.KEY.name)
                        .eq(TaskRabbitMetadata.METADATA_KEY), // $NON-NLS-1$
                    Task.ID.eq(
                        Field.field(TR_METADATA_JOIN) + "." + Metadata.TASK.name))) // $NON-NLS-1$
            + filter.getSqlQuery();

    sqlQueryTemplate.set(
        SortHelper.adjustQueryForFlagsAndSort(joinedTaskRabbitQuery, sortFlags, sortSort));

    // perform query
    TodorooCursor<Task> currentCursor;
    try {
      currentCursor = taskService.fetchFiltered(sqlQueryTemplate.get(), null, taskProperties());
    } catch (SQLiteException e) {
      StartupService.handleSQLiteColumnMissing(getActivity(), e);
      return;
    }

    // set up list adapters
    taskAdapter = createTaskAdapter(currentCursor);

    setListAdapter(taskAdapter);
    getListView().setOnScrollListener(this);
    registerForContextMenu(getListView());

    loadTaskListContent(true);
  }
  private String getQuery(Context context) {
    if (SubtasksHelper.isTagFilter(filter)) {
      ((FilterWithCustomIntent) filter).customTaskList =
          new ComponentName(
              context,
              TagViewFragment.class); // In case legacy widget was created with subtasks fragment
    }

    SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(context);
    int flags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0);
    flags |= SortHelper.FLAG_SHOW_RECENTLY_COMPLETED;
    int sort = publicPrefs.getInt(SortHelper.PREF_SORT_SORT, 0);
    if (sort == 0) {
      sort = SortHelper.SORT_WIDGET;
    }

    String query =
        SortHelper.adjustQueryForFlagsAndSort(filter.getSqlQuery(), flags, sort)
            .replaceAll("LIMIT \\d+", "");

    String tagName = Preferences.getStringValue(WidgetConfigActivity.PREF_TITLE + widgetId);

    return SubtasksHelper.applySubtasksToWidgetFilter(filter, query, tagName, 0);
  }
Exemple #5
0
    @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);
      }
      PendingIntent pendingIntent =
          PendingIntent.getActivity(
              context, widgetId, listIntent, PendingIntent.FLAG_CANCEL_CURRENT);
      views.setOnClickPendingIntent(R.id.taskbody, pendingIntent);

      Intent editIntent = new Intent(context, TaskEditActivity.class);
      editIntent.setFlags(
          Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
      if (filter != null && filter.valuesForNewTasks != null) {
        String values = AndroidUtilities.contentValuesToSerializedString(filter.valuesForNewTasks);
        editIntent.putExtra(TaskEditActivity.TOKEN_VALUES, values);
        editIntent.setType(values);
      }
      pendingIntent = PendingIntent.getActivity(context, 0, editIntent, 0);
      views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);

      return views;
    }
  @Override
  public void onReceive(Context context, Intent intent) {
    if (!Preferences.getBoolean(R.string.p_rmd_enabled, true)) return;

    DependencyInjectionService.getInstance().inject(this);

    int reengagementReminders = Preferences.getInt(ReengagementService.PREF_REENGAGEMENT_COUNT, 1);
    Preferences.setInt(ReengagementService.PREF_REENGAGEMENT_COUNT, reengagementReminders + 1);

    Intent notifIntent = new Intent(context, TaskListActivity.class);

    QueryTemplate template = new QueryTemplate().where(TaskCriteria.activeVisibleMine());
    String sql =
        SortHelper.adjustQueryForFlagsAndSort(template.toString(), 0, SortHelper.SORT_AUTO)
            + " LIMIT "
            + TASK_LIMIT; //$NON-NLS-1$

    boolean hasTasks = false;
    TodorooCursor<Task> tasks =
        taskService.query(
            Query.select(Task.ID).where(TaskCriteria.activeVisibleMine()).limit(TASK_LIMIT));
    try {
      hasTasks = tasks.getCount() > 0;
    } finally {
      tasks.close();
    }

    String title =
        Notifications.getRandomReminder(
            context.getResources().getStringArray(R.array.rmd_reengage_notif_titles));
    if (title.contains("%s")) { // $NON-NLS-1$
      String name = ""; // $NON-NLS-1$
      if (actFmPreferenceService.isLoggedIn()) {
        JSONObject thisUser = ActFmPreferenceService.thisUser();
        name = thisUser.optString("first_name"); // $NON-NLS-1$
        if (TextUtils.isEmpty(name)) name = thisUser.optString("name"); // $NON-NLS-1$
        if (TextUtils.isEmpty(name)) name = context.getString(R.string.rmd_reengage_name_default);
      }
      title = String.format(title, name);
    }

    String text =
        Notifications.getRandomReminder(
            context
                .getResources()
                .getStringArray(
                    hasTasks
                        ? R.array.rmd_reengage_dialog_options
                        : R.array.rmd_reengage_dialog_empty_options));

    FilterWithCustomIntent filter =
        new FilterWithCustomIntent(
            context.getString(R.string.rmd_NoA_filter),
            context.getString(R.string.rmd_NoA_filter),
            sql,
            null);
    filter.customTaskList = new ComponentName(context, ReengagementFragment.class);
    filter.customExtras = new Bundle();
    filter.customExtras.putString(ReengagementFragment.EXTRA_TEXT, text);

    notifIntent.setAction("NOTIFY_reengagement"); // $NON-NLS-1$
    notifIntent.putExtra(TaskListFragment.TOKEN_FILTER, filter);
    notifIntent.putExtra(ReengagementFragment.EXTRA_TEXT, text);
    notifIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    notifIntent.putExtra(TaskListActivity.TOKEN_SOURCE, Constants.SOURCE_REENGAGEMENT);

    NotificationManager manager = new AndroidNotificationManager(context);
    Notification notification =
        new Notification(R.drawable.notif_astrid, text, System.currentTimeMillis());

    PendingIntent pendingIntent =
        PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(context, title, text, pendingIntent);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    if (Preferences.getBoolean(R.string.p_rmd_persistent, true)) {
      notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS;
      notification.ledOffMS = 5000;
      notification.ledOnMS = 700;
      notification.ledARGB = Color.YELLOW;
    } else {
      notification.defaults = Notification.DEFAULT_LIGHTS;
    }

    manager.notify(0, notification);
    Flags.set(Flags.REFRESH); // Forces a reload when app launches

    ReengagementService.scheduleReengagementAlarm(context);
  }