use of com.google.android.mms.MmsException in project XobotOS by xamarin.
the class PduPersister method move.
/**
* Move a PDU object from one location to another.
*
* @param from Specify the PDU object to be moved.
* @param to The destination location, should be one of the following:
* "content://mms/inbox", "content://mms/sent",
* "content://mms/drafts", "content://mms/outbox",
* "content://mms/trash".
* @return New Uri of the moved PDU.
* @throws MmsException Error occurred while moving the message.
*/
public Uri move(Uri from, Uri to) throws MmsException {
// Check whether the 'msgId' has been assigned a valid value.
long msgId = ContentUris.parseId(from);
if (msgId == -1L) {
throw new MmsException("Error! ID of the message: -1.");
}
// Get corresponding int value of destination box.
Integer msgBox = MESSAGE_BOX_MAP.get(to);
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.");
}
ContentValues values = new ContentValues(1);
values.put(Mms.MESSAGE_BOX, msgBox);
SqliteWrapper.update(mContext, mContentResolver, from, values, null, null);
return ContentUris.withAppendedId(to, msgId);
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class SmsHelper method getUnreadUnseenConversations.
/**
* List of messages grouped by thread id, used for showing notifications
*/
public static HashMap<Long, ArrayList<MessageItem>> getUnreadUnseenConversations(Context context) {
HashMap<Long, ArrayList<MessageItem>> result = new HashMap<>();
String selection = SmsHelper.UNSEEN_SELECTION + " AND " + SmsHelper.UNREAD_SELECTION;
// Create a cursor for the conversation list
Cursor conversationCursor = context.getContentResolver().query(SmsHelper.CONVERSATIONS_CONTENT_PROVIDER, Conversation.ALL_THREADS_PROJECTION, SmsHelper.UNREAD_SELECTION, null, SmsHelper.sortDateAsc);
if (conversationCursor != null && conversationCursor.moveToFirst()) {
do {
ArrayList<MessageItem> messages = new ArrayList<>();
long threadId = conversationCursor.getLong(Conversation.ID);
Uri threadUri = Uri.withAppendedPath(Message.MMS_SMS_CONTENT_PROVIDER, Long.toString(threadId));
Cursor messageCursor = context.getContentResolver().query(threadUri, MessageColumns.PROJECTION, selection, null, SmsHelper.sortDateAsc);
if (messageCursor != null && messageCursor.moveToFirst()) {
do {
MessageColumns.ColumnsMap columnsMap = new MessageColumns.ColumnsMap(messageCursor);
MessageItem message = null;
try {
message = new MessageItem(context, messageCursor.getString(columnsMap.mColumnMsgType), messageCursor, columnsMap, null, true);
} catch (MmsException e) {
e.printStackTrace();
}
messages.add(message);
} while (messageCursor.moveToNext());
messageCursor.close();
result.put(threadId, messages);
}
} while (conversationCursor.moveToNext());
conversationCursor.close();
}
return result;
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
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);
// and we have to ignore it in loadRecipients.
if (groupMmsEnabled) {
loadRecipients(PduHeaders.TO, recipients, addressMap, true);
loadRecipients(PduHeaders.CC, recipients, addressMap, true);
}
break;
case PduHeaders.MESSAGE_TYPE_SEND_REQ:
loadRecipients(PduHeaders.TO, recipients, addressMap, false);
break;
}
long threadId = DUMMY_THREAD_ID;
if (createThreadId && !recipients.isEmpty()) {
// Given all the recipients associated with this message, find (or create) the
// correct thread.
threadId = Utils.getOrCreateThreadId(mContext, recipients);
}
values.put("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;
// 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);
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);
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("mid", 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 qksms by moezbhatti.
the class Transaction method sendMmsMessage.
private void sendMmsMessage(String text, String[] addresses, Bitmap[] image, String[] imageNames, byte[] media, String mimeType, String subject) {
// merge the string[] of addresses into a single string so they can be inserted into the database easier
String address = "";
for (int i = 0; i < addresses.length; i++) {
address += addresses[i] + " ";
}
address = address.trim();
// create the parts to send
ArrayList<MMSPart> data = new ArrayList<>();
for (int i = 0; i < image.length; i++) {
// turn bitmap into byte array to be stored
byte[] imageBytes = Message.bitmapToByteArray(image[i]);
MMSPart part = new MMSPart();
part.MimeType = "image/jpeg";
part.Name = (imageNames != null) ? imageNames[i] : ("image" + i);
part.Data = imageBytes;
data.add(part);
}
// eg. videos, audio, contact cards, location maybe?
if (media.length > 0 && mimeType != null) {
MMSPart part = new MMSPart();
part.MimeType = mimeType;
part.Name = mimeType.split("/")[0];
part.Data = media;
data.add(part);
}
if (!text.equals("")) {
// add text to the end of the part and send
MMSPart part = new MMSPart();
part.Name = "text";
part.MimeType = "text/plain";
part.Data = text.getBytes();
data.add(part);
}
MessageInfo info;
try {
info = getBytes(context, saveMessage, address.split(" "), data.toArray(new MMSPart[data.size()]), subject);
} catch (MmsException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
try {
MmsMessageSender sender = new MmsMessageSender(context, info.location, info.bytes.length);
sender.sendMessage(info.token);
IntentFilter filter = new IntentFilter();
filter.addAction(ProgressCallbackEntity.PROGRESS_STATUS_ACTION);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int progress = intent.getIntExtra("progress", -3);
if (LOCAL_LOGV)
Log.v(TAG, "progress: " + progress);
// send progress broadcast to update ui if desired...
Intent progressIntent = new Intent(MMS_PROGRESS);
progressIntent.putExtra("progress", progress);
context.sendBroadcast(progressIntent);
if (progress == ProgressCallbackEntity.PROGRESS_COMPLETE) {
context.sendBroadcast(new Intent(REFRESH));
try {
context.unregisterReceiver(this);
} catch (Exception e) {
// TODO fix me
// receiver is not registered force close error... hmm.
}
} else if (progress == ProgressCallbackEntity.PROGRESS_ABORT) {
// This seems to get called only after the progress has reached 100 and then something else goes wrong, so here we will try and send again and see if it works
if (LOCAL_LOGV)
Log.v(TAG, "sending aborted for some reason...");
}
}
};
context.registerReceiver(receiver, filter);
} catch (Throwable e) {
Log.e(TAG, "exception thrown", e);
// insert the pdu into the database and return the bytes to send
if (settings.getWifiMmsFix()) {
sendMMS(info.bytes);
} else {
sendMMSWiFi(info.bytes);
}
}
}
use of com.google.android.mms.MmsException in project qksms by moezbhatti.
the class MessageUtils method saveRingtone.
/**
* Copies media from an Mms to the DrmProvider
*
* @param context
* @param msgId
*/
public static boolean saveRingtone(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, "copyToDrmProvider 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)) {
// All parts (but there's probably only a single one) have to be successful
// for a valid result.
result &= copyPart(context, part, Long.toHexString(msgId));
}
}
return result;
}
Aggregations