use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class MessageUtils method copyMedia.
/**
* Copies media from an Mms to the "download" directory on the SD card. If any of the parts
* are audio types, drm'd or not, they're copied to the "Ringtones" directory.
*
* @param context
* @param msgId
*/
public static boolean copyMedia(Context context, long msgId) {
boolean result = true;
PduBody body = null;
try {
body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
} catch (MmsException e) {
Log.e(TAG, "copyMedia can't load pdu body: " + msgId);
}
if (body == null) {
return false;
}
int partNum = body.getPartsNum();
for (int i = 0; i < partNum; i++) {
PduPart part = body.getPart(i);
// all parts have to be successful for a valid result.
result &= copyPart(context, part, Long.toHexString(msgId));
}
return result;
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class MessageUtils method isForwardable.
/**
* Returns true if all drm'd parts are forwardable.
*
* @param context
* @param msgId
* @return true if all drm'd parts are forwardable.
*/
public static boolean isForwardable(Context context, long msgId) {
PduBody body = null;
try {
body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
} catch (MmsException e) {
Log.e(TAG, "getDrmMimeType can't load pdu body: " + msgId);
}
if (body == null) {
return false;
}
int partNum = body.getPartsNum();
for (int i = 0; i < partNum; i++) {
PduPart part = body.getPart(i);
String type = new String(part.getContentType());
if (DrmUtils.isDrmType(type) && !DrmUtils.haveRightsForAction(part.getDataUri(), DrmStore.Action.TRANSFER)) {
return false;
}
}
return true;
}
use of com.google.android.mms.MmsException in project android_frameworks_opt_telephony by LineageOS.
the class PduPersister method persistPart.
public Uri persistPart(PduPart part, long msgId, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
Uri uri = Uri.parse("content://mms/" + msgId + "/part");
ContentValues values = new ContentValues(8);
int charset = part.getCharset();
if (charset != 0) {
values.put(Part.CHARSET, charset);
}
String contentType = getPartContentType(part);
if (contentType != null) {
// Change it to "image/jpeg"
if (ContentType.IMAGE_JPG.equals(contentType)) {
contentType = ContentType.IMAGE_JPEG;
}
values.put(Part.CONTENT_TYPE, contentType);
// To ensure the SMIL part is always the first part.
if (ContentType.APP_SMIL.equals(contentType)) {
values.put(Part.SEQ, -1);
}
} else {
throw new MmsException("MIME type of the part must be set.");
}
if (part.getFilename() != null) {
String fileName = new String(part.getFilename());
values.put(Part.FILENAME, fileName);
}
if (part.getName() != null) {
String name = new String(part.getName());
values.put(Part.NAME, name);
}
Object value = null;
if (part.getContentDisposition() != null) {
value = toIsoString(part.getContentDisposition());
values.put(Part.CONTENT_DISPOSITION, (String) value);
}
if (part.getContentId() != null) {
value = toIsoString(part.getContentId());
values.put(Part.CONTENT_ID, (String) value);
}
if (part.getContentLocation() != null) {
value = toIsoString(part.getContentLocation());
values.put(Part.CONTENT_LOCATION, (String) value);
}
Uri res = SqliteWrapper.insert(mContext, mContentResolver, uri, values);
if (res == null) {
throw new MmsException("Failed to persist part, return null.");
}
persistData(part, res, contentType, preOpenedFiles);
// After successfully store the data, we should update
// the dataUri of the part.
part.setDataUri(res);
return res;
}
use of com.google.android.mms.MmsException in project android_frameworks_opt_telephony by LineageOS.
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.
* @param createThreadId if true, this function may create a thread id for the recipients
* @param groupMmsEnabled if true, all of the recipients addressed in the PDU will be used
* to create the associated thread. When false, only the sender will be used in finding or
* creating the appropriate thread or conversation.
* @param preOpenedFiles if not null, a map of preopened InputStreams for the parts.
* @return A Uri which can be used to access the stored PDU.
*/
public Uri persist(GenericPdu pdu, Uri uri, boolean createThreadId, boolean groupMmsEnabled, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
if (uri == null) {
throw new MmsException("Uri may not be null.");
}
long msgId = -1;
try {
msgId = ContentUris.parseId(uri);
} catch (NumberFormatException e) {
// the uri ends with "inbox" or something else like that
}
boolean existingUri = msgId != -1;
if (!existingUri && MESSAGE_BOX_MAP.get(uri) == 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.");
}
synchronized (PDU_CACHE_INSTANCE) {
// purging it.
if (PDU_CACHE_INSTANCE.isUpdating(uri)) {
if (LOCAL_LOGV) {
Log.v(TAG, "persist: " + uri + " blocked by isUpdating()");
}
try {
PDU_CACHE_INSTANCE.wait();
} catch (InterruptedException e) {
Log.e(TAG, "persist1: ", e);
}
}
}
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>();
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)) {
switch(msgType) {
case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF:
loadRecipients(PduHeaders.FROM, recipients, addressMap, false);
// we have to ignore it in loadRecipients.
if (groupMmsEnabled) {
loadRecipients(PduHeaders.TO, recipients, addressMap, true);
// Also load any numbers in the CC field to address group messaging
// compatibility issues with devices that place numbers in this field
// for group messages.
loadRecipients(PduHeaders.CC, recipients, addressMap, true);
}
break;
case PduHeaders.MESSAGE_TYPE_SEND_REQ:
loadRecipients(PduHeaders.TO, recipients, addressMap, false);
break;
}
long threadId = 0;
if (createThreadId && !recipients.isEmpty()) {
// Given all the recipients associated with this message, find (or create) the
// correct thread.
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();
// Figure out if this PDU is a text-only message
boolean textOnly = true;
// Sum up the total message size
int messageSize = 0;
// 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();
if (partsNum > 2) {
// For a text-only message there will be two parts: 1-the SMIL, 2-the text.
// Down a few lines below we're checking to make sure we've only got SMIL or
// text. We also have to check then we don't have more than two parts.
// Otherwise, a slideshow with two text slides would be marked as textOnly.
textOnly = false;
}
for (int i = 0; i < partsNum; i++) {
PduPart part = body.getPart(i);
messageSize += part.getDataLength();
persistPart(part, dummyId, preOpenedFiles);
// If we've got anything besides text/plain or SMIL part, then we've got
// an mms message with some other type of attachment.
String contentType = getPartContentType(part);
if (contentType != null && !ContentType.APP_SMIL.equals(contentType) && !ContentType.TEXT_PLAIN.equals(contentType)) {
textOnly = false;
}
}
}
}
// Record whether this mms message is a simple plain text or not. This is a hint for the
// UI.
values.put(Mms.TEXT_ONLY, textOnly ? 1 : 0);
// PDU header. If not, then we insert the message size as well.
if (values.getAsInteger(Mms.MESSAGE_SIZE) == null) {
values.put(Mms.MESSAGE_SIZE, messageSize);
}
Uri res = null;
if (existingUri) {
res = uri;
SqliteWrapper.update(mContext, mContentResolver, res, values, null, null);
} else {
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.
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);
// FIXME: Should the MmsProvider be responsible for this???
if (!existingUri) {
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;
}
use of com.google.android.mms.MmsException in project android_frameworks_opt_telephony by LineageOS.
the class PduPersister method updatePart.
private void updatePart(Uri uri, PduPart part, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
ContentValues values = new ContentValues(7);
int charset = part.getCharset();
if (charset != 0) {
values.put(Part.CHARSET, charset);
}
String contentType = null;
if (part.getContentType() != null) {
contentType = toIsoString(part.getContentType());
values.put(Part.CONTENT_TYPE, contentType);
} else {
throw new MmsException("MIME type of the part must be set.");
}
if (part.getFilename() != null) {
String fileName = new String(part.getFilename());
values.put(Part.FILENAME, fileName);
}
if (part.getName() != null) {
String name = new String(part.getName());
values.put(Part.NAME, name);
}
Object value = null;
if (part.getContentDisposition() != null) {
value = toIsoString(part.getContentDisposition());
values.put(Part.CONTENT_DISPOSITION, (String) value);
}
if (part.getContentId() != null) {
value = toIsoString(part.getContentId());
values.put(Part.CONTENT_ID, (String) value);
}
if (part.getContentLocation() != null) {
value = toIsoString(part.getContentLocation());
values.put(Part.CONTENT_LOCATION, (String) value);
}
SqliteWrapper.update(mContext, mContentResolver, uri, values, null, null);
// 2. The Uri of the part is different from the current one.
if ((part.getData() != null) || (!uri.equals(part.getDataUri()))) {
persistData(part, uri, contentType, preOpenedFiles);
}
}
Aggregations