Search in sources :

Example 1 with EncodedStringValue

use of com.google.android.mms.pdu.EncodedStringValue in project XobotOS by xamarin.

the class PduPersister method loadParts.

private PduPart[] loadParts(long msgId) throws MmsException {
    Cursor c = SqliteWrapper.query(mContext, mContentResolver, Uri.parse("content://mms/" + msgId + "/part"), PART_PROJECTION, null, null, null);
    PduPart[] parts = null;
    try {
        if ((c == null) || (c.getCount() == 0)) {
            if (LOCAL_LOGV) {
                Log.v(TAG, "loadParts(" + msgId + "): no part to load.");
            }
            return null;
        }
        int partCount = c.getCount();
        int partIdx = 0;
        parts = new PduPart[partCount];
        while (c.moveToNext()) {
            PduPart part = new PduPart();
            Integer charset = getIntegerFromPartColumn(c, PART_COLUMN_CHARSET);
            if (charset != null) {
                part.setCharset(charset);
            }
            byte[] contentDisposition = getByteArrayFromPartColumn(c, PART_COLUMN_CONTENT_DISPOSITION);
            if (contentDisposition != null) {
                part.setContentDisposition(contentDisposition);
            }
            byte[] contentId = getByteArrayFromPartColumn(c, PART_COLUMN_CONTENT_ID);
            if (contentId != null) {
                part.setContentId(contentId);
            }
            byte[] contentLocation = getByteArrayFromPartColumn(c, PART_COLUMN_CONTENT_LOCATION);
            if (contentLocation != null) {
                part.setContentLocation(contentLocation);
            }
            byte[] contentType = getByteArrayFromPartColumn(c, PART_COLUMN_CONTENT_TYPE);
            if (contentType != null) {
                part.setContentType(contentType);
            } else {
                throw new MmsException("Content-Type must be set.");
            }
            byte[] fileName = getByteArrayFromPartColumn(c, PART_COLUMN_FILENAME);
            if (fileName != null) {
                part.setFilename(fileName);
            }
            byte[] name = getByteArrayFromPartColumn(c, PART_COLUMN_NAME);
            if (name != null) {
                part.setName(name);
            }
            // Construct a Uri for this part.
            long partId = c.getLong(PART_COLUMN_ID);
            Uri partURI = Uri.parse("content://mms/part/" + partId);
            part.setDataUri(partURI);
            // For images/audio/video, we won't keep their data in Part
            // because their renderer accept Uri as source.
            String type = toIsoString(contentType);
            if (!ContentType.isImageType(type) && !ContentType.isAudioType(type) && !ContentType.isVideoType(type)) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream is = null;
                // faster.
                if (ContentType.TEXT_PLAIN.equals(type) || ContentType.APP_SMIL.equals(type) || ContentType.TEXT_HTML.equals(type)) {
                    String text = c.getString(PART_COLUMN_TEXT);
                    byte[] blob = new EncodedStringValue(text != null ? text : "").getTextString();
                    baos.write(blob, 0, blob.length);
                } else {
                    try {
                        is = mContentResolver.openInputStream(partURI);
                        byte[] buffer = new byte[256];
                        int len = is.read(buffer);
                        while (len >= 0) {
                            baos.write(buffer, 0, len);
                            len = is.read(buffer);
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Failed to load part data", e);
                        c.close();
                        throw new MmsException(e);
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e) {
                                Log.e(TAG, "Failed to close stream", e);
                            }
                        // Ignore
                        }
                    }
                }
                part.setData(baos.toByteArray());
            }
            parts[partIdx++] = part;
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return parts;
}
Also used : EncodedStringValue(com.google.android.mms.pdu.EncodedStringValue) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Cursor(android.database.Cursor) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException)

Example 2 with EncodedStringValue

use of com.google.android.mms.pdu.EncodedStringValue in project XobotOS by xamarin.

the class PduPersister method persist.

/**
     * Persist a PDU object to specific location in the storage.
     *
     * @param pdu The PDU object to be stored.
     * @param uri Where to store the given PDU object.
     * @return A Uri which can be used to access the stored PDU.
     */
public Uri persist(GenericPdu pdu, Uri uri) throws MmsException {
    if (uri == null) {
        throw new MmsException("Uri may not be null.");
    }
    Integer msgBox = MESSAGE_BOX_MAP.get(uri);
    if (msgBox == null) {
        throw new MmsException("Bad destination, must be one of " + "content://mms/inbox, content://mms/sent, " + "content://mms/drafts, content://mms/outbox, " + "content://mms/temp.");
    }
    PDU_CACHE_INSTANCE.purge(uri);
    PduHeaders header = pdu.getPduHeaders();
    PduBody body = null;
    ContentValues values = new ContentValues();
    Set<Entry<Integer, String>> set;
    set = ENCODED_STRING_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        int field = e.getKey();
        EncodedStringValue encodedString = header.getEncodedStringValue(field);
        if (encodedString != null) {
            String charsetColumn = CHARSET_COLUMN_NAME_MAP.get(field);
            values.put(e.getValue(), toIsoString(encodedString.getTextString()));
            values.put(charsetColumn, encodedString.getCharacterSet());
        }
    }
    set = TEXT_STRING_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        byte[] text = header.getTextString(e.getKey());
        if (text != null) {
            values.put(e.getValue(), toIsoString(text));
        }
    }
    set = OCTET_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        int b = header.getOctet(e.getKey());
        if (b != 0) {
            values.put(e.getValue(), b);
        }
    }
    set = LONG_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        long l = header.getLongInteger(e.getKey());
        if (l != -1L) {
            values.put(e.getValue(), l);
        }
    }
    HashMap<Integer, EncodedStringValue[]> addressMap = new HashMap<Integer, EncodedStringValue[]>(ADDRESS_FIELDS.length);
    // Save address information.
    for (int addrType : ADDRESS_FIELDS) {
        EncodedStringValue[] array = null;
        if (addrType == PduHeaders.FROM) {
            EncodedStringValue v = header.getEncodedStringValue(addrType);
            if (v != null) {
                array = new EncodedStringValue[1];
                array[0] = v;
            }
        } else {
            array = header.getEncodedStringValues(addrType);
        }
        addressMap.put(addrType, array);
    }
    HashSet<String> recipients = new HashSet<String>();
    long threadId = DUMMY_THREAD_ID;
    int msgType = pdu.getMessageType();
    // this scope.
    if ((msgType == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) || (msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) || (msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)) {
        EncodedStringValue[] array = null;
        switch(msgType) {
            case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
            case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF:
                array = addressMap.get(PduHeaders.FROM);
                break;
            case PduHeaders.MESSAGE_TYPE_SEND_REQ:
                array = addressMap.get(PduHeaders.TO);
                break;
        }
        if (array != null) {
            for (EncodedStringValue v : array) {
                if (v != null) {
                    recipients.add(v.getString());
                }
            }
        }
        threadId = Threads.getOrCreateThreadId(mContext, recipients);
    }
    values.put(Mms.THREAD_ID, threadId);
    // Save parts first to avoid inconsistent message is loaded
    // while saving the parts.
    // Dummy ID of the msg.
    long dummyId = System.currentTimeMillis();
    // Get body if the PDU is a RetrieveConf or SendReq.
    if (pdu instanceof MultimediaMessagePdu) {
        body = ((MultimediaMessagePdu) pdu).getBody();
        // Start saving parts if necessary.
        if (body != null) {
            int partsNum = body.getPartsNum();
            for (int i = 0; i < partsNum; i++) {
                PduPart part = body.getPart(i);
                persistPart(part, dummyId);
            }
        }
    }
    Uri res = SqliteWrapper.insert(mContext, mContentResolver, uri, values);
    if (res == null) {
        throw new MmsException("persist() failed: return null.");
    }
    // Get the real ID of the PDU and update all parts which were
    // saved with the dummy ID.
    long msgId = ContentUris.parseId(res);
    values = new ContentValues(1);
    values.put(Part.MSG_ID, msgId);
    SqliteWrapper.update(mContext, mContentResolver, Uri.parse("content://mms/" + dummyId + "/part"), values, null, null);
    // We should return the longest URI of the persisted PDU, for
    // example, if input URI is "content://mms/inbox" and the _ID of
    // persisted PDU is '8', we should return "content://mms/inbox/8"
    // instead of "content://mms/8".
    // FIXME: Should the MmsProvider be responsible for this???
    res = Uri.parse(uri + "/" + msgId);
    // Save address information.
    for (int addrType : ADDRESS_FIELDS) {
        EncodedStringValue[] array = addressMap.get(addrType);
        if (array != null) {
            persistAddress(msgId, addrType, array);
        }
    }
    return res;
}
Also used : ContentValues(android.content.ContentValues) EncodedStringValue(com.google.android.mms.pdu.EncodedStringValue) HashMap(java.util.HashMap) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException) PduCacheEntry(com.google.android.mms.util.PduCacheEntry) Entry(java.util.Map.Entry) HashSet(java.util.HashSet)

Example 3 with EncodedStringValue

use of com.google.android.mms.pdu.EncodedStringValue in project XobotOS by xamarin.

the class PduPersister method persistData.

/**
     * Save data of the part into storage. The source data may be given
     * by a byte[] or a Uri. If it's a byte[], directly save it
     * into storage, otherwise load source data from the dataUri and then
     * save it. If the data is an image, we may scale down it according
     * to user preference.
     *
     * @param part The PDU part which contains data to be saved.
     * @param uri The URI of the part.
     * @param contentType The MIME type of the part.
     * @throws MmsException Cannot find source data or error occurred
     *         while saving the data.
     */
private void persistData(PduPart part, Uri uri, String contentType) throws MmsException {
    OutputStream os = null;
    InputStream is = null;
    try {
        byte[] data = part.getData();
        if (ContentType.TEXT_PLAIN.equals(contentType) || ContentType.APP_SMIL.equals(contentType) || ContentType.TEXT_HTML.equals(contentType)) {
            ContentValues cv = new ContentValues();
            cv.put(Telephony.Mms.Part.TEXT, new EncodedStringValue(data).getString());
            if (mContentResolver.update(uri, cv, null, null) != 1) {
                throw new MmsException("unable to update " + uri.toString());
            }
        } else {
            os = mContentResolver.openOutputStream(uri);
            if (data == null) {
                Uri dataUri = part.getDataUri();
                if ((dataUri == null) || (dataUri == uri)) {
                    Log.w(TAG, "Can't find data for this part.");
                    return;
                }
                is = mContentResolver.openInputStream(dataUri);
                if (LOCAL_LOGV) {
                    Log.v(TAG, "Saving data to: " + uri);
                }
                byte[] buffer = new byte[256];
                for (int len = 0; (len = is.read(buffer)) != -1; ) {
                    os.write(buffer, 0, len);
                }
            } else {
                if (LOCAL_LOGV) {
                    Log.v(TAG, "Saving data to: " + uri);
                }
                os.write(data);
            }
        }
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Failed to open Input/Output stream.", e);
        throw new MmsException(e);
    } catch (IOException e) {
        Log.e(TAG, "Failed to read/write data.", e);
        throw new MmsException(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing: " + os, e);
            }
        // Ignore
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing: " + is, e);
            }
        // Ignore
        }
    }
}
Also used : ContentValues(android.content.ContentValues) EncodedStringValue(com.google.android.mms.pdu.EncodedStringValue) MmsException(com.google.android.mms.MmsException) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri)

Example 4 with EncodedStringValue

use of com.google.android.mms.pdu.EncodedStringValue in project android-aosp-mms by slvn.

the class ComposeMessageActivity method forwardMessage.

private void forwardMessage(final MessageItem msgItem) {
    mTempThreadId = 0;
    // The user wants to forward the message. If the message is an mms message, we need to
    // persist the pdu to disk. This is done in a background task.
    // If the task takes longer than a half second, a progress dialog is displayed.
    // Once the PDU persisting is done, another runnable on the UI thread get executed to start
    // the ForwardMessageActivity.
    getAsyncDialog().runAsync(new Runnable() {

        @Override
        public void run() {
            // This runnable gets run in a background thread.
            if (msgItem.mType.equals("mms")) {
                SendReq sendReq = new SendReq();
                String subject = getString(R.string.forward_prefix);
                if (msgItem.mSubject != null) {
                    subject += msgItem.mSubject;
                }
                sendReq.setSubject(new EncodedStringValue(subject));
                sendReq.setBody(msgItem.mSlideshow.makeCopy());
                mTempMmsUri = null;
                try {
                    PduPersister persister = PduPersister.getPduPersister(ComposeMessageActivity.this);
                    // Copy the parts of the message here.
                    mTempMmsUri = persister.persist(sendReq, Mms.Draft.CONTENT_URI, true, MessagingPreferenceActivity.getIsGroupMmsEnabled(ComposeMessageActivity.this), null);
                    mTempThreadId = MessagingNotification.getThreadId(ComposeMessageActivity.this, mTempMmsUri);
                } catch (MmsException e) {
                    Log.e(TAG, "Failed to copy message: " + msgItem.mMessageUri);
                    Toast.makeText(ComposeMessageActivity.this, R.string.cannot_save_message, Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }, new Runnable() {

        @Override
        public void run() {
            // Once the above background thread is complete, this runnable is run
            // on the UI thread.
            Intent intent = createIntent(ComposeMessageActivity.this, 0);
            intent.putExtra(KEY_EXIT_ON_SENT, true);
            intent.putExtra(KEY_FORWARDED_MESSAGE, true);
            if (mTempThreadId > 0) {
                intent.putExtra(THREAD_ID, mTempThreadId);
            }
            if (msgItem.mType.equals("sms")) {
                intent.putExtra("sms_body", msgItem.mBody);
            } else {
                intent.putExtra("msg_uri", mTempMmsUri);
                String subject = getString(R.string.forward_prefix);
                if (msgItem.mSubject != null) {
                    subject += msgItem.mSubject;
                }
                intent.putExtra("subject", subject);
            }
            // ForwardMessageActivity is simply an alias in the manifest for
            // ComposeMessageActivity. We have to make an alias because ComposeMessageActivity
            // launch flags specify singleTop. When we forward a message, we want to start a
            // separate ComposeMessageActivity. The only way to do that is to override the
            // singleTop flag, which is impossible to do in code. By creating an alias to the
            // activity, without the singleTop flag, we can launch a separate
            // ComposeMessageActivity to edit the forward message.
            intent.setClassName(ComposeMessageActivity.this, "com.android.mms.ui.ForwardMessageActivity");
            startActivity(intent);
        }
    }, R.string.building_slideshow_title);
}
Also used : EncodedStringValue(com.google.android.mms.pdu.EncodedStringValue) MmsException(com.google.android.mms.MmsException) PduPersister(com.google.android.mms.pdu.PduPersister) Intent(android.content.Intent) SpannableString(android.text.SpannableString) SendReq(com.google.android.mms.pdu.SendReq)

Example 5 with EncodedStringValue

use of com.google.android.mms.pdu.EncodedStringValue in project android-aosp-mms by slvn.

the class MessageUtils method getMultimediaMessageDetails.

private static String getMultimediaMessageDetails(Context context, Cursor cursor, int size) {
    int type = cursor.getInt(MessageListAdapter.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(MessageListAdapter.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');
        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');
    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');
            details.append(res.getString(R.string.bcc_label));
            details.append(EncodedStringValue.concat(values));
        }
    }
    // Date: ***
    details.append('\n');
    int msgBox = cursor.getInt(MessageListAdapter.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');
    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');
    details.append(res.getString(R.string.priority_label));
    details.append(getPriorityDescription(context, msg.getPriority()));
    // Message size: *** KB
    details.append('\n');
    details.append(res.getString(R.string.message_size_label));
    details.append((size - 1) / 1000 + 1);
    details.append(" KB");
    return details.toString();
}
Also used : MmsException(com.google.android.mms.MmsException) EncodedStringValue(com.google.android.mms.pdu.EncodedStringValue) MultimediaMessagePdu(com.google.android.mms.pdu.MultimediaMessagePdu) Resources(android.content.res.Resources) Uri(android.net.Uri) SendReq(com.google.android.mms.pdu.SendReq) RetrieveConf(com.google.android.mms.pdu.RetrieveConf)

Aggregations

EncodedStringValue (com.google.android.mms.pdu.EncodedStringValue)19 Uri (android.net.Uri)9 MmsException (com.google.android.mms.MmsException)9 ContentValues (android.content.ContentValues)6 IOException (java.io.IOException)5 Cursor (android.database.Cursor)4 SendReq (com.google.android.mms.pdu.SendReq)4 PduComposer (com.google.android.mms.pdu.PduComposer)3 PduPersister (com.google.android.mms.pdu.PduPersister)3 InputStream (java.io.InputStream)3 Intent (android.content.Intent)2 Resources (android.content.res.Resources)2 NotificationInd (com.google.android.mms.pdu.NotificationInd)2 ReadRecInd (com.google.android.mms.pdu.ReadRecInd)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashSet (java.util.HashSet)2 SpannableString (android.text.SpannableString)1 ContentRestrictionException (com.android.mms.ContentRestrictionException)1 ExceedMessageSizeException (com.android.mms.ExceedMessageSizeException)1 ResolutionException (com.android.mms.ResolutionException)1