Search in sources :

Example 1 with PduComposer

use of com.google.android.mms.pdu_alt.PduComposer in project qksms by moezbhatti.

the class ReadRecTransaction method run.

public void run() {
    PduPersister persister = PduPersister.getPduPersister(mContext);
    try {
        // Load M-read-rec.ind from outbox
        ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
        // insert the 'from' address per spec
        String lineNumber = Utils.getMyPhoneNumber(mContext);
        readRecInd.setFrom(new EncodedStringValue(lineNumber));
        // Pack M-read-rec.ind and send it
        byte[] postingData = new PduComposer(mContext, readRecInd).make();
        sendPdu(postingData);
        Uri uri = persister.move(mReadReportURI, Uri.parse("content://mms/sent"));
        mTransactionState.setState(TransactionState.SUCCESS);
        mTransactionState.setContentUri(uri);
    } catch (IOException e) {
        if (LOCAL_LOGV)
            Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
    } catch (MmsException e) {
        if (LOCAL_LOGV)
            Log.v(TAG, "Failed to load message from Outbox.", e);
    } catch (RuntimeException e) {
        Log.e(TAG, "Unexpected RuntimeException.", e);
    } finally {
        if (mTransactionState.getState() != TransactionState.SUCCESS) {
            mTransactionState.setState(TransactionState.FAILED);
            mTransactionState.setContentUri(mReadReportURI);
        }
        notifyObservers();
    }
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) MmsException(com.google.android.mms.MmsException) PduPersister(com.google.android.mms.pdu_alt.PduPersister) ReadRecInd(com.google.android.mms.pdu_alt.ReadRecInd) IOException(java.io.IOException) Uri(android.net.Uri) PduComposer(com.google.android.mms.pdu_alt.PduComposer)

Example 2 with PduComposer

use of com.google.android.mms.pdu_alt.PduComposer 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();
    }
}
Also used : ContentValues(android.content.ContentValues) SendConf(com.google.android.mms.pdu_alt.SendConf) PduParser(com.google.android.mms.pdu_alt.PduParser) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) PduPersister(com.google.android.mms.pdu_alt.PduPersister) RateController(com.android.mms.util.RateController) SendReq(com.google.android.mms.pdu_alt.SendReq) Uri(android.net.Uri) PduComposer(com.google.android.mms.pdu_alt.PduComposer)

Example 3 with PduComposer

use of com.google.android.mms.pdu_alt.PduComposer in project qksms by moezbhatti.

the class RetrieveTransaction method sendAcknowledgeInd.

private void sendAcknowledgeInd(RetrieveConf rc) throws MmsException, IOException {
    // Send M-Acknowledge.ind to MMSC if required.
    // If the Transaction-ID isn't set in the M-Retrieve.conf, it means
    // the MMS proxy-relay doesn't require an ACK.
    byte[] tranId = rc.getTransactionId();
    if (tranId != null) {
        // Create M-Acknowledge.ind
        AcknowledgeInd acknowledgeInd = new AcknowledgeInd(PduHeaders.CURRENT_MMS_VERSION, tranId);
        // insert the 'from' address per spec
        String lineNumber = Utils.getMyPhoneNumber(mContext);
        acknowledgeInd.setFrom(new EncodedStringValue(lineNumber));
        // Pack M-Acknowledge.ind and send it
        if (MmsConfig.getNotifyWapMMSC()) {
            sendPdu(new PduComposer(mContext, acknowledgeInd).make(), mContentLocation);
        } else {
            sendPdu(new PduComposer(mContext, acknowledgeInd).make());
        }
    }
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) AcknowledgeInd(com.google.android.mms.pdu_alt.AcknowledgeInd) PduComposer(com.google.android.mms.pdu_alt.PduComposer)

Example 4 with PduComposer

use of com.google.android.mms.pdu_alt.PduComposer 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;
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) PduBody(com.google.android.mms.pdu_alt.PduBody) PduPersister(com.google.android.mms.pdu_alt.PduPersister) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cursor(android.database.Cursor) SendReq(com.google.android.mms.pdu_alt.SendReq) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PduComposer(com.google.android.mms.pdu_alt.PduComposer) MmsException(com.google.android.mms.MmsException) PduPart(com.google.android.mms.pdu_alt.PduPart) MMSPart(com.google.android.mms.MMSPart)

Aggregations

EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)4 PduComposer (com.google.android.mms.pdu_alt.PduComposer)4 PduPersister (com.google.android.mms.pdu_alt.PduPersister)3 Uri (android.net.Uri)2 MmsException (com.google.android.mms.MmsException)2 SendReq (com.google.android.mms.pdu_alt.SendReq)2 IOException (java.io.IOException)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 RateController (com.android.mms.util.RateController)1 MMSPart (com.google.android.mms.MMSPart)1 AcknowledgeInd (com.google.android.mms.pdu_alt.AcknowledgeInd)1 PduBody (com.google.android.mms.pdu_alt.PduBody)1 PduParser (com.google.android.mms.pdu_alt.PduParser)1 PduPart (com.google.android.mms.pdu_alt.PduPart)1 ReadRecInd (com.google.android.mms.pdu_alt.ReadRecInd)1 SendConf (com.google.android.mms.pdu_alt.SendConf)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ExecutionException (java.util.concurrent.ExecutionException)1