use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class PduPersister method load.
/**
* Load a PDU from storage by given Uri.
*
* @param uri The Uri of the PDU to be loaded.
* @return A generic PDU object, it may be cast to dedicated PDU.
* @throws MmsException Failed to load some fields of a PDU.
*/
public GenericPdu load(Uri uri) throws MmsException {
GenericPdu pdu = null;
PduCacheEntry cacheEntry = null;
int msgBox = 0;
long threadId = DUMMY_THREAD_ID;
try {
synchronized (PDU_CACHE_INSTANCE) {
if (PDU_CACHE_INSTANCE.isUpdating(uri)) {
if (LOCAL_LOGV)
Log.v(TAG, "load: " + uri + " blocked by isUpdating()");
try {
PDU_CACHE_INSTANCE.wait();
} catch (InterruptedException e) {
Log.e(TAG, "load: ", e);
}
cacheEntry = PDU_CACHE_INSTANCE.get(uri);
if (cacheEntry != null) {
return cacheEntry.getPdu();
}
}
// Tell the cache to indicate to other callers that this item
// is currently being updated.
PDU_CACHE_INSTANCE.setUpdating(uri, true);
}
Cursor c = SqliteWrapper.query(mContext, mContentResolver, uri, PDU_PROJECTION, null, null, null);
PduHeaders headers = new PduHeaders();
Set<Entry<Integer, Integer>> set;
long msgId = ContentUris.parseId(uri);
try {
if ((c == null) || (c.getCount() != 1) || !c.moveToFirst()) {
throw new MmsException("Bad uri: " + uri);
}
msgBox = c.getInt(PDU_COLUMN_MESSAGE_BOX);
threadId = c.getLong(PDU_COLUMN_THREAD_ID);
set = ENCODED_STRING_COLUMN_INDEX_MAP.entrySet();
for (Entry<Integer, Integer> e : set) {
setEncodedStringValueToHeaders(c, e.getValue(), headers, e.getKey());
}
set = TEXT_STRING_COLUMN_INDEX_MAP.entrySet();
for (Entry<Integer, Integer> e : set) {
setTextStringToHeaders(c, e.getValue(), headers, e.getKey());
}
set = OCTET_COLUMN_INDEX_MAP.entrySet();
for (Entry<Integer, Integer> e : set) {
setOctetToHeaders(c, e.getValue(), headers, e.getKey());
}
set = LONG_COLUMN_INDEX_MAP.entrySet();
for (Entry<Integer, Integer> e : set) {
setLongToHeaders(c, e.getValue(), headers, e.getKey());
}
} finally {
if (c != null) {
c.close();
}
}
// Check whether 'msgId' has been assigned a valid value.
if (msgId == -1L) {
throw new MmsException("Error! ID of the message: -1.");
}
// Load address information of the MM.
loadAddress(msgId, headers);
int msgType = headers.getOctet(PduHeaders.MESSAGE_TYPE);
PduBody body = new PduBody();
// load multiparts and put them into the body of the PDU.
if ((msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) || (msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)) {
PduPart[] parts = loadParts(msgId);
if (parts != null) {
int partsNum = parts.length;
for (int i = 0; i < partsNum; i++) {
body.addPart(parts[i]);
}
}
}
switch(msgType) {
case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
pdu = new NotificationInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_DELIVERY_IND:
pdu = new DeliveryInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND:
pdu = new ReadOrigInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF:
pdu = new RetrieveConf(headers, body);
break;
case PduHeaders.MESSAGE_TYPE_SEND_REQ:
pdu = new SendReq(headers, body);
break;
case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND:
pdu = new AcknowledgeInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND:
pdu = new NotifyRespInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_READ_REC_IND:
pdu = new ReadRecInd(headers);
break;
case PduHeaders.MESSAGE_TYPE_SEND_CONF:
case PduHeaders.MESSAGE_TYPE_FORWARD_REQ:
case PduHeaders.MESSAGE_TYPE_FORWARD_CONF:
case PduHeaders.MESSAGE_TYPE_MBOX_STORE_REQ:
case PduHeaders.MESSAGE_TYPE_MBOX_STORE_CONF:
case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_REQ:
case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_CONF:
case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_REQ:
case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_CONF:
case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_REQ:
case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_CONF:
case PduHeaders.MESSAGE_TYPE_MBOX_DESCR:
case PduHeaders.MESSAGE_TYPE_DELETE_REQ:
case PduHeaders.MESSAGE_TYPE_DELETE_CONF:
case PduHeaders.MESSAGE_TYPE_CANCEL_REQ:
case PduHeaders.MESSAGE_TYPE_CANCEL_CONF:
throw new MmsException("Unsupported PDU type: " + Integer.toHexString(msgType));
default:
throw new MmsException("Unrecognized PDU type: " + Integer.toHexString(msgType));
}
} finally {
synchronized (PDU_CACHE_INSTANCE) {
if (pdu != null) {
assert (PDU_CACHE_INSTANCE.get(uri) == null);
// Update the cache entry with the real info
cacheEntry = new PduCacheEntry(pdu, msgBox, threadId);
PDU_CACHE_INSTANCE.put(uri, cacheEntry);
}
PDU_CACHE_INSTANCE.setUpdating(uri, false);
// tell anybody waiting on this entry to go ahead
PDU_CACHE_INSTANCE.notifyAll();
}
}
return pdu;
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
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;
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class DownloadManager method markState.
public void markState(final Uri uri, int state) {
// Notify user if the message has expired.
try {
NotificationInd nInd = (NotificationInd) PduPersister.getPduPersister(mContext).load(uri);
if ((nInd.getExpiry() < System.currentTimeMillis() / 1000L) && (state == STATE_DOWNLOADING || state == STATE_PRE_DOWNLOADING)) {
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(mContext, R.string.service_message_not_found, Toast.LENGTH_LONG).show();
}
});
SqliteWrapper.delete(mContext, mContext.getContentResolver(), uri, null, null);
return;
}
} catch (MmsException e) {
Log.e(TAG, e.getMessage(), e);
return;
}
// Notify user if downloading permanently failed.
if (state == STATE_PERMANENT_FAILURE) {
/*
* TODO: Until we can generate a better notification message, don't do this.
try {
final String message = getMessage(uri);
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
});
} catch (MmsException e) {
Log.e(TAG, e.getMessage(), e);
}
*/
} else if (!mAutoDownload) {
state |= DEFERRED_MASK;
}
// Use the STATUS field to store the state of downloading process
// because it's useless for M-Notification.ind.
ContentValues values = new ContentValues(1);
values.put("st", state);
SqliteWrapper.update(mContext, mContext.getContentResolver(), uri, values, null, null);
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class MessageUtils method haveSomethingToCopyToSDCard.
/**
* Looks to see if there are any valid parts of the attachment that can be copied to a SD card.
*
* @param context
* @param msgId
*/
public static boolean haveSomethingToCopyToSDCard(Context context, long msgId) {
PduBody body = null;
try {
body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
} catch (MmsException e) {
Log.e(TAG, "haveSomethingToCopyToSDCard can't load pdu body: " + msgId);
}
if (body == null) {
return false;
}
boolean result = false;
int partNum = body.getPartsNum();
for (int i = 0; i < partNum; i++) {
PduPart part = body.getPart(i);
String type = new String(part.getContentType());
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.v(TAG, "[CMA] haveSomethingToCopyToSDCard: part[" + i + "] contentType=" + type);
}
if (ContentType.isImageType(type) || ContentType.isVideoType(type) || ContentType.isAudioType(type) || DrmUtils.isDrmType(type)) {
result = true;
break;
}
}
return result;
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class MessageUtils method isDrmRingtoneWithRights.
/**
* Returns true if any part is drm'd audio with ringtone rights.
*
* @param context
* @param msgId
* @return true if one of the parts is drm'd audio with rights to save as a ringtone.
*/
public static boolean isDrmRingtoneWithRights(Context context, long msgId) {
PduBody body = null;
try {
body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
} catch (MmsException e) {
Log.e(TAG, "isDrmRingtoneWithRights 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)) {
String mimeType = QKSMSApp.getApplication().getDrmManagerClient().getOriginalMimeType(part.getDataUri());
if (ContentType.isAudioType(mimeType) && DrmUtils.haveRightsForAction(part.getDataUri(), DrmStore.Action.RINGTONE)) {
return true;
}
}
}
return false;
}
Aggregations