@Override
  public void onSortSelected(boolean always, int flags, int sort) {
    boolean manualSettingChanged =
        SortHelper.isManualSort(sortFlags) != SortHelper.isManualSort(flags);

    sortFlags = flags;
    sortSort = sort;

    if (always) {
      SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(ContextManager.getContext());
      if (publicPrefs != null) {
        Editor editor = publicPrefs.edit();
        if (editor != null) {
          editor.putInt(SortHelper.PREF_SORT_FLAGS, flags);
          editor.putInt(SortHelper.PREF_SORT_SORT, sort);
          editor.commit();
          TasksWidget.updateWidgets(ContextManager.getContext());
        }
      }
    }

    try {
      if (manualSettingChanged) toggleDragDrop(SortHelper.isManualSort(sortFlags));
      else setUpTaskList();
    } catch (IllegalStateException e) {
      // TODO: Fragment got detached somehow (rare)
    }
  }
Exemple #2
0
  @Override
  public void onSortSelected(boolean always, int flags, int sort) {
    boolean manualSettingChanged =
        SortHelper.isManualSort(sortFlags) != SortHelper.isManualSort(flags);

    sortFlags = flags;
    sortSort = sort;

    if (always) {
      preferences.setSortFlags(flags);
      preferences.setSortMode(sort);
      TasksWidget.updateWidgets(context);
    }

    try {
      if (manualSettingChanged) {
        toggleDragDrop(SortHelper.isManualSort(sortFlags));
      } else {
        setUpTaskList();
      }
    } catch (IllegalStateException e) {
      log.error(e.getMessage(), e);
      // TODO: Fragment got detached somehow (rare)
    }
  }
  @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 #4
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 #5
0
  protected void setUpUiComponents() {
    // set listener for quick-changing task priority
    getListView()
        .setOnKeyListener(
            new OnKeyListener() {
              @Override
              public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (event.getAction() != KeyEvent.ACTION_UP || view == null) return false;

                boolean filterOn = getListView().isTextFilterEnabled();
                View selected = getListView().getSelectedView();

                // hot-key to set task priority - 1-4 or ALT + Q-R
                if (!filterOn
                    && event.getUnicodeChar() >= '1'
                    && event.getUnicodeChar() <= '4'
                    && selected != null) {
                  int importance = event.getNumber() - '1';
                  Task task = ((ViewHolder) selected.getTag()).task;
                  task.setValue(Task.IMPORTANCE, importance);
                  taskService.save(task);
                  taskAdapter.setFieldContentsAndVisibility(selected);
                }
                // filter
                else if (!filterOn && event.getUnicodeChar() != 0) {
                  getListView().setTextFilterEnabled(true);
                  getListView().setFilterText(Character.toString((char) event.getUnicodeChar()));
                }
                // turn off filter if nothing is selected
                else if (filterOn && TextUtils.isEmpty(getListView().getTextFilter())) {
                  getListView().setTextFilterEnabled(false);
                }

                return false;
              }
            });

    SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(getActivity());
    sortFlags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0);
    sortSort = publicPrefs.getInt(SortHelper.PREF_SORT_SORT, 0);
    sortFlags = SortHelper.setManualSort(sortFlags, isDraggable());

    getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
  }
  /**
   * Cursor with the following columns
   *
   * <ol>
   *   <li>task title, string
   *   <li>task importance color, int android RGB color
   *   <li>task due date (was: preferred due date), long millis since epoch
   *   <li>task due date (was: absolute due date), long millis since epoch
   *   <li>task importance, integer from 0 to 3 (0 => most important)
   *   <li>task id, long
   *   <li>task tags, string tags separated by |
   * </ol>
   *
   * @return cursor as described above
   */
  public Cursor getTasks() {

    MatrixCursor ret = new MatrixCursor(TASK_FIELD_LIST);

    TodorooCursor<Task> cursor =
        taskService.query(
            Query.select(Task.ID, Task.TITLE, Task.IMPORTANCE, Task.DUE_DATE)
                .where(Criterion.and(TaskCriteria.isActive(), TaskCriteria.isVisible()))
                .orderBy(SortHelper.defaultTaskOrder())
                .limit(MAX_NUMBER_OF_TASKS));
    try {
      int[] importanceColors = Task.getImportanceColors(ctx.getResources());
      Task task = new Task();
      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToNext();
        task.readFromCursor(cursor);

        StringBuilder taskTags = new StringBuilder();
        TodorooCursor<Metadata> tagCursor = TagService.getInstance().getTags(task.getId());
        try {
          for (tagCursor.moveToFirst(); !tagCursor.isAfterLast(); tagCursor.moveToNext())
            taskTags.append(tagCursor.get(TagService.TAG)).append(TAG_SEPARATOR);
        } finally {
          tagCursor.close();
        }

        Object[] values = new Object[7];
        values[0] = task.getValue(Task.TITLE);
        values[1] = importanceColors[task.getValue(Task.IMPORTANCE)];
        values[2] = task.getValue(Task.DUE_DATE);
        values[3] = task.getValue(Task.DUE_DATE);
        values[4] = task.getValue(Task.IMPORTANCE);
        values[5] = task.getId();
        values[6] = taskTags.toString();

        ret.addRow(values);
      }
    } finally {
      cursor.close();
    }

    return ret;
  }
Exemple #7
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 #9
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);
  }