@Override public int onStartCommand(Intent intent, int flags, int startId) { Utils.log("onStartCommand " + intent.getAction()); if (Utils.ACTION_SEND_TASKS.equals(intent.getAction())) { // We start foregroud as soon as we receive an ACTION_SEND_TASKS // action. This will make the service foreground when // SendTaskActivity goes away startForeground(NOTIFICATION_SEND, buildNotification(NOTIFICATION_SEND).build()); } return super.onStartCommand(intent, flags, startId); }
private void processOptions(LocalTask task) throws IOException { URLOptions urlOptions = new URLOptions(); String[] parts; cache_unshorten = null; cache_titles = null; switch (task.getStatus()) { case ADDED: case READY: if (task.hasOption(Options.OPTION_UNSHORTEN)) { task.setStatus(Status.PROCESSING_UNSHORTEN).persist(); String links = Utils.filterLinks(task.getTitle()).trim(); parts = urlOptions.unshorten(links.split(" ")); cache_unshorten = parts.clone(); task.setTitle(Utils.replace(task.getTitle(), parts)) .removeOption(Options.OPTION_UNSHORTEN); } if (task.hasOption(Options.OPTION_GETTITLES)) { task.setStatus(Status.PROCESSING_TITLE).persist(); String links = Utils.filterLinks(task.getTitle()).trim(); parts = urlOptions.getTitles(links.split(" ")); cache_titles = parts.clone(); task.setTitle(Utils.appendInBrackets(task.getTitle(), parts)) .removeOption(Options.OPTION_GETTITLES); } task.setStatus(Status.READY); break; case PROCESSING_UNSHORTEN: parts = Utils.filterLinks(task.getTitle()).split(" "); parts = urlOptions.unshorten(parts); cache_unshorten = parts.clone(); task.setTitle(Utils.replace(task.getTitle(), parts)).removeOption(Options.OPTION_UNSHORTEN); if (!task.hasOption(Options.OPTION_GETTITLES)) { task.setStatus(Status.READY); break; } case PROCESSING_TITLE: parts = Utils.filterLinks(task.getTitle()).split(" "); parts = urlOptions.getTitles(parts); cache_titles = parts.clone(); task.setTitle(Utils.appendInBrackets(task.getTitle(), parts)) .removeOption(Options.OPTION_GETTITLES) .setStatus(Status.READY); break; } }
private void handleActionSend(LocalTask task) throws UserRecoverableAuthIOException, IOException { PersistCallback callback = new PersistCallback() { @Override public void run() { LocalBroadcastManager.getInstance(GoogleTasksService.this) .sendBroadcast(new Intent(Utils.ACTION_LIST_LOCAL_TASKS)); } }; task.setStatus(Status.SENDING).persist(callback); Utils.log("Sending task " + task.getTitle()); Task new_task = new Task().setTitle(task.getTitle()); client.tasks().insert(list_id, new_task).execute(); task.setStatus(Status.SENT).persist(callback); }
@Override public void onDestroy() { super.onDestroy(); Utils.log("Destroying service"); }
@Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); Utils.log("onHandleIntent " + intent.getAction()); long[] tasks_ids; try { if (action.equals(Utils.ACTION_PROCESS_TASK)) { long task_id = intent.getLongExtra(Utils.EXTRA_TASK_ID, -1); LocalTask task = DatabaseHelper.getInstance(this).getTask(task_id); final Intent result = new Intent(Utils.ACTION_RESULT_PROCESS_TASK); result.putExtra(Utils.EXTRA_TASK_ID, task.getLocalId()); if (isOnline()) { processOptions(task); task.persistBlocking( new PersistCallback() { @Override public void run() { if (cache_unshorten != null) { result.putExtra(Utils.EXTRA_CACHE_UNSHORTEN, cache_unshorten); } if (cache_titles != null) { result.putExtra(Utils.EXTRA_CACHE_TITLES, cache_titles); } LocalBroadcastManager.getInstance(GoogleTasksService.this) .sendBroadcast(result); } }); } else { tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); revertTaskToReady(tasks_ids); LocalBroadcastManager.getInstance(this).sendBroadcast(result); } } else if (action.equals(Utils.ACTION_SEND_TASKS)) { if (isOnline()) { tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); if (tasks_ids.length == 1) { DatabaseHelper databaseHelper = DatabaseHelper.getInstance(this); LocalTask task = databaseHelper.getTask(tasks_ids[0]); handleActionSend(task); } else { handleActionSendMultiple(tasks_ids); } stopForeground(true); } else { tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); revertTaskToReady(tasks_ids); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) .notify( NOTIFICATION_SEND_LATER, buildNotification(NOTIFICATION_SEND_LATER).build()); } } else if (action.equals(Utils.ACTION_LIST_TASKS)) { handleActionList(); } else if (action.equals(Utils.ACTION_REMOVE_TASKS)) { handleActionRemove(intent.getStringArrayListExtra(Utils.EXTRA_TASKS_IDS)); } } catch (UserRecoverableAuthIOException userRecoverableException) { Utils.log(Log.getStackTraceString(userRecoverableException)); stopForeground(true); tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); revertTaskToReady(tasks_ids); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) .notify( NOTIFICATION_NEED_AUTHORIZE, buildNotification(NOTIFICATION_NEED_AUTHORIZE).build()); } catch (IOException ioException) { Utils.log(Log.getStackTraceString(ioException)); if (action.equals(Utils.ACTION_SEND_TASKS)) { tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); stopForeground(true); revertTaskToReady(tasks_ids); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) .notify(NOTIFICATION_ERROR, buildNotification(NOTIFICATION_ERROR).build()); } else { Intent broadcast = new Intent(Utils.ACTION_LIST_TASKS); broadcast.putExtra(Utils.EXTRA_ERROR_TEXT, getString(R.string.txt_error_list)); LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast); } } catch (NullPointerException npe) { tasks_ids = intent.getLongArrayExtra(Utils.EXTRA_TASKS_IDS); Utils.log(Log.getStackTraceString(npe)); stopForeground(true); revertTaskToReady(tasks_ids); ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) .notify( NOTIFICATION_NEED_AUTHORIZE, buildNotification(NOTIFICATION_NEED_AUTHORIZE).build()); } } }