private void sendMessage(long accountId, long recipientId, String text, String imageUri) { if (accountId <= 0 || recipientId <= 0 || isEmpty(text)) return; final String title = getString(R.string.sending_direct_message); final Builder builder = new Builder(this); builder.setSmallIcon(R.drawable.ic_stat_send); builder.setProgress(100, 0, true); builder.setTicker(title); builder.setContentTitle(title); builder.setContentText(text); builder.setOngoing(true); final Notification notification = builder.build(); startForeground(NOTIFICATION_ID_SEND_DIRECT_MESSAGE, notification); final SingleResponse<ParcelableDirectMessage> result = sendDirectMessage(builder, accountId, recipientId, text, imageUri); if (result.getData() != null && result.getData().id > 0) { final ContentValues values = ContentValuesCreator.createDirectMessage(result.getData()); final String delete_where = DirectMessages.ACCOUNT_ID + " = " + accountId + " AND " + DirectMessages.MESSAGE_ID + " = " + result.getData().id; mResolver.delete(DirectMessages.Outbox.CONTENT_URI, delete_where, null); mResolver.insert(DirectMessages.Outbox.CONTENT_URI, values); showOkMessage(R.string.direct_message_sent, false); } else { final ContentValues values = createMessageDraft(accountId, recipientId, text, imageUri); mResolver.insert(Drafts.CONTENT_URI, values); showErrorMessage(R.string.action_sending_direct_message, result.getException(), true); } stopForeground(false); mNotificationManager.cancel(NOTIFICATION_ID_SEND_DIRECT_MESSAGE); }
private void updateStatuses(ParcelableStatusUpdate... statuses) { final Builder builder = new Builder(this); startForeground( NOTIFICATION_ID_UPDATE_STATUS, updateUpdateStatusNotification(this, builder, 0, null)); for (final ParcelableStatusUpdate item : statuses) { mNotificationManager.notify( NOTIFICATION_ID_UPDATE_STATUS, updateUpdateStatusNotification(this, builder, 0, item)); final ContentValues draftValues = ContentValuesCreator.createStatusDraft( item, ParcelableAccount.getAccountIds(item.accounts)); final Uri draftUri = mResolver.insert(Drafts.CONTENT_URI, draftValues); final long draftId = ParseUtils.parseLong(draftUri.getLastPathSegment(), -1); mTwitter.addSendingDraftId(draftId); final List<SingleResponse<ParcelableStatus>> result = updateStatus(builder, item); boolean failed = false; Exception exception = null; final Expression where = Expression.equals(Drafts._ID, draftId); final List<Long> failedAccountIds = ListUtils.fromArray(ParcelableAccount.getAccountIds(item.accounts)); for (final SingleResponse<ParcelableStatus> response : result) { if (response.getData() == null) { failed = true; if (exception == null) { exception = response.getException(); } } else if (response.getData().account_id > 0) { failedAccountIds.remove(response.getData().account_id); // spice if (response.getData().media == null) { SpiceProfilingUtil.log( response.getData().id + ",Tweet," + response.getData().account_id + "," + response.getData().in_reply_to_user_id + "," + response.getData().in_reply_to_status_id); SpiceProfilingUtil.profile( this.getBaseContext(), response.getData().account_id, response.getData().id + ",Tweet," + response.getData().account_id + "," + response.getData().in_reply_to_user_id + "," + response.getData().in_reply_to_status_id); } else for (final ParcelableMedia spiceMedia : response.getData().media) { SpiceProfilingUtil.log( response.getData().id + ",Media," + response.getData().account_id + "," + response.getData().in_reply_to_user_id + "," + response.getData().in_reply_to_status_id + "," + spiceMedia.media_url + "," + TypeMappingUtil.getMediaType(spiceMedia.type)); SpiceProfilingUtil.profile( this.getBaseContext(), response.getData().account_id, response.getData().id + ",Media," + response.getData().account_id + "," + response.getData().in_reply_to_user_id + "," + response.getData().in_reply_to_status_id + "," + spiceMedia.media_url + "," + TypeMappingUtil.getMediaType(spiceMedia.type)); } // end } } if (result.isEmpty()) { showErrorMessage( R.string.action_updating_status, getString(R.string.no_account_selected), false); } else if (failed) { // If the status is a duplicate, there's no need to save it to // drafts. if (exception instanceof TwitterException && ((TwitterException) exception).getErrorCode() == StatusCodeMessageUtils.STATUS_IS_DUPLICATE) { showErrorMessage(getString(R.string.status_is_duplicate), false); } else { final ContentValues accountIdsValues = new ContentValues(); accountIdsValues.put( Drafts.ACCOUNT_IDS, ListUtils.toString(failedAccountIds, ',', false)); mResolver.update(Drafts.CONTENT_URI, accountIdsValues, where.getSQL(), null); showErrorMessage(R.string.action_updating_status, exception, true); final ContentValues notifValues = new ContentValues(); notifValues.put(BaseColumns._ID, draftId); mResolver.insert(Drafts.CONTENT_URI_NOTIFICATIONS, notifValues); } } else { showOkMessage(R.string.status_updated, false); mResolver.delete(Drafts.CONTENT_URI, where.getSQL(), null); if (item.media != null) { for (final ParcelableMediaUpdate media : item.media) { final String path = getImagePathFromUri(this, Uri.parse(media.uri)); if (path != null) { if (!new File(path).delete()) { Log.d(LOGTAG, String.format("unable to delete %s", path)); } } } } } mTwitter.removeSendingDraftId(draftId); if (mPreferences.getBoolean(KEY_REFRESH_AFTER_TWEET, false)) { mHandler.post( new Runnable() { @Override public void run() { mTwitter.refreshAll(); } }); } } stopForeground(false); mNotificationManager.cancel(NOTIFICATION_ID_UPDATE_STATUS); }