Exemple #1
0
  @Override
  public boolean onOptionsItemSelected(final MenuItem item) {
    long itemId;

    if (!isCurrentTaskListFragment()) {
      return false;
    }

    switch (item.getItemId()) {
        // --- context menu items

      case CONTEXT_MENU_EDIT_TASK_ID:
        itemId = item.getGroupId();
        mListener.onTaskListItemClicked(itemId);
        return true;
      case CONTEXT_MENU_COPY_TASK_ID:
        itemId = item.getGroupId();
        duplicateTask(itemId);
        return true;
      case CONTEXT_MENU_DELETE_TASK_ID:
        {
          itemId = item.getGroupId();
          Task task = taskService.fetchById(itemId, Task.ID, Task.UUID);
          if (task != null) {
            deleteTask(task);
          }
          return true;
        }
      case CONTEXT_MENU_UNDELETE_TASK_ID:
        {
          itemId = item.getGroupId();
          Task task = new Task();
          task.setId(itemId);
          task.setDeletionDate(0L);
          taskService.save(task);
          loadTaskListContent();
          return true;
        }
      case CONTEXT_MENU_PURGE_TASK_ID:
        {
          itemId = item.getGroupId();
          Task task = new Task();
          task.setId(itemId);
          TimerPlugin.updateTimer(notificationManager, taskService, getActivity(), task, false);
          taskDeleter.purge(itemId);
          loadTaskListContent();
          return true;
        }
      default:
        return super.onOptionsItemSelected(item);
    }
  }
  public String getTaskEventUri(Task task) {
    String uri;
    if (!TextUtils.isEmpty(task.getCalendarURI())) {
      uri = task.getCalendarURI();
    } else {
      task = taskService.fetchById(task.getId(), Task.CALENDAR_URI);
      if (task == null) {
        return null;
      }
      uri = task.getCalendarURI();
    }

    return uri;
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, -1);
    if (taskId == -1) {
      return;
    }

    Task task = taskService.fetchById(taskId, Task.PROPERTIES);
    if (task == null || !task.isCompleted()) {
      return;
    }

    String recurrence = task.sanitizedRecurrence();
    boolean repeatAfterCompletion = task.repeatAfterCompletion();

    if (recurrence != null && recurrence.length() > 0) {
      long newDueDate;
      try {
        newDueDate = computeNextDueDate(task, recurrence, repeatAfterCompletion);
        if (newDueDate == -1) {
          return;
        }
      } catch (ParseException e) {
        log.error(e.getMessage(), e);
        return;
      }

      long oldDueDate = task.getDueDate();
      long repeatUntil = task.getRepeatUntil();

      if (repeatFinished(newDueDate, repeatUntil)) {
        return;
      }

      rescheduleTask(context, gcalHelper, taskService, task, newDueDate);

      // send a broadcast
      Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_EVENT_TASK_REPEATED);
      broadcastIntent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
      broadcastIntent.putExtra(AstridApiConstants.EXTRAS_OLD_DUE_DATE, oldDueDate);
      broadcastIntent.putExtra(AstridApiConstants.EXTRAS_NEW_DUE_DATE, newDueDate);
      context.sendOrderedBroadcast(broadcastIntent, null);
      Flags.set(Flags.REFRESH);
    }
  }
  /**
   * Loads action item from the given intent
   *
   * @param intent
   */
  @SuppressWarnings("nls")
  protected void loadItem(Intent intent) {
    if (model != null) {
      // came from bundle
      isNewTask = (model.getValue(Task.TITLE).length() == 0);
      return;
    }

    long idParam = intent.getLongExtra(TOKEN_ID, -1L);

    database.openForReading();
    if (idParam > -1L) {
      model = taskService.fetchById(idParam, Task.PROPERTIES);
    }

    // not found by id or was never passed an id
    if (model == null) {
      String valuesAsString = intent.getStringExtra(TOKEN_VALUES);
      ContentValues values = null;
      try {
        if (valuesAsString != null)
          values = AndroidUtilities.contentValuesFromSerializedString(valuesAsString);
      } catch (Exception e) {
        // oops, can't serialize
      }
      model = TaskListActivity.createWithValues(values, null, taskService, metadataService);
    }

    if (model.getValue(Task.TITLE).length() == 0) {
      StatisticsService.reportEvent("create-task");
      isNewTask = true;

      // set deletion date until task gets a title
      model.setValue(Task.DELETION_DATE, DateUtilities.now());
    } else {
      StatisticsService.reportEvent("edit-task");
    }

    if (model == null) {
      exceptionService.reportError("task-edit-no-task", new NullPointerException("model"));
      finish();
      return;
    }

    // clear notification
    Notifications.cancelNotifications(model.getId());
  }
  public boolean deleteTaskEvent(Task task) {
    boolean eventDeleted = false;
    String uri;
    if (task.containsNonNullValue(Task.CALENDAR_URI)) {
      uri = task.getCalendarURI();
    } else {
      task = taskService.fetchById(task.getId(), Task.CALENDAR_URI);
      if (task == null) {
        return false;
      }
      uri = task.getCalendarURI();
    }

    if (!TextUtils.isEmpty(uri)) {
      try {
        Uri calendarUri = Uri.parse(uri);

        // try to load calendar
        ContentResolver cr = context.getContentResolver();
        Cursor cursor =
            cr.query(calendarUri, new String[] {"dtstart"}, null, null, null); // $NON-NLS-1$
        try {
          boolean alreadydeleted = cursor.getCount() == 0;

          if (!alreadydeleted) {
            cr.delete(calendarUri, null, null);
            eventDeleted = true;
          }
        } finally {
          cursor.close();
        }

        task.setCalendarUri("");
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }

    return eventDeleted;
  }
Exemple #6
0
 /**
  * Synchronize complete task with server
  *
  * @param task
  */
 public void pushTask(long taskId) {
   Task task = taskService.fetchById(taskId, Task.PROPERTIES);
   pushTaskOnSave(task, task.getMergedValues());
 }
Exemple #7
0
  /** Synchronize with server when data changes */
  public void pushTaskOnSave(Task task, ContentValues values) {
    long remoteId;
    if (task.containsValue(Task.REMOTE_ID)) remoteId = task.getValue(Task.REMOTE_ID);
    else {
      Task taskForRemote = taskService.fetchById(task.getId(), Task.REMOTE_ID);
      if (taskForRemote == null) return;
      remoteId = taskForRemote.getValue(Task.REMOTE_ID);
    }
    boolean newlyCreated = remoteId == 0;

    ArrayList<Object> params = new ArrayList<Object>();

    if (values.containsKey(Task.TITLE.name)) {
      params.add("title");
      params.add(task.getValue(Task.TITLE));
    }
    if (values.containsKey(Task.DUE_DATE.name)) {
      params.add("due");
      params.add(task.getValue(Task.DUE_DATE) / 1000L);
      params.add("has_due_time");
      params.add(task.hasDueTime() ? 1 : 0);
    }
    if (values.containsKey(Task.NOTES.name)) {
      params.add("notes");
      params.add(task.getValue(Task.NOTES));
    }
    if (values.containsKey(Task.DELETION_DATE.name)) {
      params.add("deleted_at");
      params.add(task.getValue(Task.DELETION_DATE) / 1000L);
    }
    if (values.containsKey(Task.COMPLETION_DATE.name)) {
      params.add("completed");
      params.add(task.getValue(Task.COMPLETION_DATE) / 1000L);
    }
    if (values.containsKey(Task.IMPORTANCE.name)) {
      params.add("importance");
      params.add(task.getValue(Task.IMPORTANCE));
    }
    if (values.containsKey(Task.RECURRENCE.name)) {
      params.add("repeat");
      params.add(task.getValue(Task.RECURRENCE));
    }
    if (values.containsKey(Task.USER_ID.name) && task.getValue(Task.USER_ID) >= 0) {
      params.add("user_id");
      if (task.getValue(Task.USER_ID) == 0) params.add(ActFmPreferenceService.userId());
      else params.add(task.getValue(Task.USER_ID));
    }
    if (Flags.checkAndClear(Flags.TAGS_CHANGED) || newlyCreated) {
      TodorooCursor<Metadata> cursor = TagService.getInstance().getTags(task.getId());
      try {
        if (cursor.getCount() == 0) {
          params.add("tags");
          params.add("");
        } else {
          Metadata metadata = new Metadata();
          for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            metadata.readFromCursor(cursor);
            if (metadata.containsNonNullValue(TagService.REMOTE_ID)
                && metadata.getValue(TagService.REMOTE_ID) > 0) {
              params.add("tag_ids[]");
              params.add(metadata.getValue(TagService.REMOTE_ID));
            } else {
              params.add("tags[]");
              params.add(metadata.getValue(TagService.TAG));
            }
          }
        }
      } finally {
        cursor.close();
      }
    }

    if (params.size() == 0 || !checkForToken()) return;

    System.err.println("PUSHN ON SAVE: " + task.getMergedValues());
    System.err.println("SETVALUES: " + values);

    if (!newlyCreated) {
      params.add("id");
      params.add(remoteId);
    } else if (!params.contains(Task.TITLE.name)) return;

    try {
      params.add("token");
      params.add(token);
      JSONObject result =
          actFmInvoker.invoke("task_save", params.toArray(new Object[params.size()]));
      ArrayList<Metadata> metadata = new ArrayList<Metadata>();
      JsonHelper.taskFromJson(result, task, metadata);
      task.setValue(Task.MODIFICATION_DATE, DateUtilities.now());
      task.setValue(Task.LAST_SYNC, DateUtilities.now());
      Flags.set(Flags.SUPPRESS_SYNC);
      taskDao.saveExisting(task);
    } catch (JSONException e) {
      handleException("task-save-json", e);
    } catch (IOException e) {
      handleException("task-save-io", e);
    }
  }