use of com.google.android.mms.pdu_alt.SendReq in project qksms by moezbhatti.
the class SendTransaction method run.
public void run() {
try {
RateController.init(mContext);
RateController rateCtlr = RateController.getInstance();
if (rateCtlr.isLimitSurpassed() && !rateCtlr.isAllowedByUser()) {
Log.e(TAG, "Sending rate limit surpassed.");
return;
}
// Load M-Send.req from outbox
PduPersister persister = PduPersister.getPduPersister(mContext);
SendReq sendReq = (SendReq) persister.load(mSendReqURI);
// Update the 'date' field of the PDU right before sending it.
long date = System.currentTimeMillis() / 1000L;
sendReq.setDate(date);
// Persist the new date value into database.
ContentValues values = new ContentValues(1);
values.put(Mms.DATE, date);
SqliteWrapper.update(mContext, mContext.getContentResolver(), mSendReqURI, values, null, null);
// fix bug 2100169: insert the 'from' address per spec
String lineNumber = Utils.getMyPhoneNumber(mContext);
if (!TextUtils.isEmpty(lineNumber)) {
sendReq.setFrom(new EncodedStringValue(lineNumber));
}
// Pack M-Send.req, send it, retrieve confirmation data, and parse it
long tokenKey = ContentUris.parseId(mSendReqURI);
byte[] response = sendPdu(SendingProgressTokenManager.get(tokenKey), new PduComposer(mContext, sendReq).make());
SendingProgressTokenManager.remove(tokenKey);
if (LOCAL_LOGV) {
String respStr = new String(response);
Log.d(TAG, "[SendTransaction] run: send mms msg (" + mId + "), resp=" + respStr);
}
SendConf conf = (SendConf) new PduParser(response).parse();
if (conf == null) {
Log.e(TAG, "No M-Send.conf received.");
}
// Check whether the responding Transaction-ID is consistent
// with the sent one.
byte[] reqId = sendReq.getTransactionId();
byte[] confId = conf.getTransactionId();
if (!Arrays.equals(reqId, confId)) {
Log.e(TAG, "Inconsistent Transaction-ID: req=" + new String(reqId) + ", conf=" + new String(confId));
return;
}
// From now on, we won't save the whole M-Send.conf into
// our database. Instead, we just save some interesting fields
// into the related M-Send.req.
values = new ContentValues(2);
int respStatus = conf.getResponseStatus();
values.put(Mms.RESPONSE_STATUS, respStatus);
if (respStatus != PduHeaders.RESPONSE_STATUS_OK) {
SqliteWrapper.update(mContext, mContext.getContentResolver(), mSendReqURI, values, null, null);
Log.e(TAG, "Server returned an error code: " + respStatus);
return;
}
String messageId = PduPersister.toIsoString(conf.getMessageId());
values.put(Mms.MESSAGE_ID, messageId);
SqliteWrapper.update(mContext, mContext.getContentResolver(), mSendReqURI, values, null, null);
// Move M-Send.req from Outbox into Sent.
Uri uri = persister.move(mSendReqURI, Sent.CONTENT_URI);
mTransactionState.setState(TransactionState.SUCCESS);
mTransactionState.setContentUri(uri);
} catch (Throwable t) {
Log.e(TAG, Log.getStackTraceString(t));
} finally {
if (mTransactionState.getState() != TransactionState.SUCCESS) {
mTransactionState.setState(TransactionState.FAILED);
mTransactionState.setContentUri(mSendReqURI);
Log.e(TAG, "Delivery failed.");
}
notifyObservers();
}
}
use of com.google.android.mms.pdu_alt.SendReq in project qksms by moezbhatti.
the class MmsMessageSender method sendMessage.
public boolean sendMessage(long token) throws Throwable {
// Load the MMS from the message uri
PduPersister p = PduPersister.getPduPersister(mContext);
GenericPdu pdu = p.load(mMessageUri);
if (pdu.getMessageType() != PduHeaders.MESSAGE_TYPE_SEND_REQ) {
throw new MmsException("Invalid message: " + pdu.getMessageType());
}
SendReq sendReq = (SendReq) pdu;
// Update headers.
updatePreferencesHeaders(sendReq);
// MessageClass.
sendReq.setMessageClass(DEFAULT_MESSAGE_CLASS.getBytes());
// Update the 'date' field of the message before sending it.
sendReq.setDate(System.currentTimeMillis() / 1000L);
sendReq.setMessageSize(mMessageSize);
p.updateHeaders(mMessageUri, sendReq);
long messageId = ContentUris.parseId(mMessageUri);
// Move the message into MMS Outbox.
if (!mMessageUri.toString().startsWith(Uri.parse("content://mms/drafts").toString())) {
try {
// If the message is already in the outbox (most likely because we created a "primed"
// message in the outbox when the user hit send), then we have to manually put an
// entry in the pending_msgs table which is where TransacationService looks for
// messages to send. Normally, the entry in pending_msgs is created by the trigger:
// insert_mms_pending_on_update, when a message is moved from drafts to the outbox.
ContentValues values = new ContentValues(7);
values.put(Telephony.MmsSms.PendingMessages.PROTO_TYPE, 1);
values.put(Telephony.MmsSms.PendingMessages.MSG_ID, messageId);
values.put(Telephony.MmsSms.PendingMessages.MSG_TYPE, pdu.getMessageType());
values.put(Telephony.MmsSms.PendingMessages.ERROR_TYPE, 0);
values.put(Telephony.MmsSms.PendingMessages.ERROR_CODE, 0);
values.put(Telephony.MmsSms.PendingMessages.RETRY_INDEX, 0);
values.put(Telephony.MmsSms.PendingMessages.DUE_TIME, 0);
SqliteWrapper.insert(mContext, mContext.getContentResolver(), Telephony.MmsSms.PendingMessages.CONTENT_URI, values);
} catch (Throwable e) {
p.move(mMessageUri, Telephony.Mms.Outbox.CONTENT_URI);
}
} else {
p.move(mMessageUri, Telephony.Mms.Outbox.CONTENT_URI);
}
// Start MMS transaction service
try {
SendingProgressTokenManager.put(messageId, token);
Intent service = new Intent(TransactionService.HANDLE_PENDING_TRANSACTIONS_ACTION, null, mContext, TransactionService.class);
mContext.startService(service);
} catch (Exception e) {
throw new Exception("transaction service not registered in manifest");
}
return true;
}
use of com.google.android.mms.pdu_alt.SendReq in project qksms by moezbhatti.
the class MessageUtils method getMultimediaMessageDetails.
private static String getMultimediaMessageDetails(Context context, Cursor cursor, int size) {
int type = cursor.getInt(MessageColumns.COLUMN_MMS_MESSAGE_TYPE);
if (type == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) {
return getNotificationIndDetails(context, cursor);
}
StringBuilder details = new StringBuilder();
Resources res = context.getResources();
long id = cursor.getLong(MessageColumns.COLUMN_ID);
Uri uri = ContentUris.withAppendedId(Mms.CONTENT_URI, id);
MultimediaMessagePdu msg;
try {
msg = (MultimediaMessagePdu) PduPersister.getPduPersister(context).load(uri);
} catch (MmsException e) {
Log.e(TAG, "Failed to load the message: " + uri, e);
return context.getResources().getString(R.string.cannot_get_details);
}
// Message Type: Text message.
details.append(res.getString(R.string.message_type_label));
details.append(res.getString(R.string.multimedia_message));
if (msg instanceof RetrieveConf) {
// From: ***
String from = extractEncStr(context, ((RetrieveConf) msg).getFrom());
details.append("\n\n");
details.append(res.getString(R.string.from_label));
details.append(!TextUtils.isEmpty(from) ? from : res.getString(R.string.hidden_sender_address));
}
// To: ***
details.append("\n\n");
details.append(res.getString(R.string.to_address_label));
EncodedStringValue[] to = msg.getTo();
if (to != null) {
details.append(EncodedStringValue.concat(to));
} else {
Log.w(TAG, "recipient list is empty!");
}
// Bcc: ***
if (msg instanceof SendReq) {
EncodedStringValue[] values = ((SendReq) msg).getBcc();
if ((values != null) && (values.length > 0)) {
details.append("\n\n");
details.append(res.getString(R.string.bcc_label));
details.append(EncodedStringValue.concat(values));
}
}
// Date: ***
details.append("\n\n");
int msgBox = cursor.getInt(MessageColumns.COLUMN_MMS_MESSAGE_BOX);
if (msgBox == Mms.MESSAGE_BOX_DRAFTS) {
details.append(res.getString(R.string.saved_label));
} else if (msgBox == Mms.MESSAGE_BOX_INBOX) {
details.append(res.getString(R.string.received_label));
} else {
details.append(res.getString(R.string.sent_label));
}
details.append(MessageUtils.formatTimeStampString(context, msg.getDate() * 1000L, true));
// Subject: ***
details.append("\n\n");
details.append(res.getString(R.string.subject_label));
EncodedStringValue subject = msg.getSubject();
if (subject != null) {
String subStr = subject.getString();
// Message size should include size of subject.
size += subStr.length();
details.append(subStr);
}
// Priority: High/Normal/Low
details.append("\n\n");
details.append(res.getString(R.string.priority_label));
details.append(getPriorityDescription(context, msg.getPriority()));
// Message size: *** KB
details.append("\n\n");
details.append(res.getString(R.string.message_size_label));
details.append((size - 1) / 1000 + 1);
details.append(" KB");
return details.toString();
}
use of com.google.android.mms.pdu_alt.SendReq in project qksms by moezbhatti.
the class Transaction method getBytes.
public static MessageInfo getBytes(Context context, boolean saveMessage, String[] recipients, MMSPart[] parts, String subject) throws MmsException {
final SendReq sendRequest = new SendReq();
// create send request addresses
for (int i = 0; i < recipients.length; i++) {
final EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipients[i]);
if (phoneNumbers != null && phoneNumbers.length > 0) {
sendRequest.addTo(phoneNumbers[0]);
}
}
if (subject != null) {
sendRequest.setSubject(new EncodedStringValue(subject));
}
sendRequest.setDate(Calendar.getInstance().getTimeInMillis() / 1000L);
try {
sendRequest.setFrom(new EncodedStringValue(Utils.getMyPhoneNumber(context)));
} catch (Exception e) {
// my number is nothing
}
final PduBody pduBody = new PduBody();
// assign parts to the pdu body which contains sending data
if (parts != null) {
for (int i = 0; i < parts.length; i++) {
MMSPart part = parts[i];
if (part != null) {
try {
PduPart partPdu = new PduPart();
partPdu.setName(part.Name.getBytes());
partPdu.setContentType(part.MimeType.getBytes());
if (part.MimeType.startsWith("text")) {
partPdu.setCharset(CharacterSets.UTF_8);
}
partPdu.setData(part.Data);
pduBody.addPart(partPdu);
} catch (Exception e) {
}
}
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
SmilXmlSerializer.serialize(SmilHelper.createSmilDocument(pduBody), out);
PduPart smilPart = new PduPart();
smilPart.setContentId("smil".getBytes());
smilPart.setContentLocation("smil.xml".getBytes());
smilPart.setContentType(ContentType.APP_SMIL.getBytes());
smilPart.setData(out.toByteArray());
pduBody.addPart(0, smilPart);
sendRequest.setBody(pduBody);
// create byte array which will actually be sent
final PduComposer composer = new PduComposer(context, sendRequest);
final byte[] bytesToSend;
try {
bytesToSend = composer.make();
} catch (OutOfMemoryError e) {
throw new MmsException("Out of memory!");
}
MessageInfo info = new MessageInfo();
info.bytes = bytesToSend;
if (saveMessage) {
try {
PduPersister persister = PduPersister.getPduPersister(context);
info.location = persister.persist(sendRequest, Uri.parse("content://mms/outbox"), true, settings.getGroup(), null);
} catch (Exception e) {
if (LOCAL_LOGV)
Log.v(TAG, "error saving mms message");
Log.e(TAG, "exception thrown", e);
// use the old way if something goes wrong with the persister
insert(context, recipients, parts, subject);
}
}
try {
Cursor query = context.getContentResolver().query(info.location, new String[] { "thread_id" }, null, null, null);
if (query != null && query.moveToFirst()) {
info.token = query.getLong(query.getColumnIndex("thread_id"));
} else {
// just default sending token for what I had before
info.token = 4444L;
}
} catch (Exception e) {
Log.e(TAG, "exception thrown", e);
info.token = 4444L;
}
return info;
}
Aggregations