use of com.google.android.mms.MMSPart in project qksms by moezbhatti.
the class Transaction method insert.
private static Uri insert(Context context, String[] to, MMSPart[] parts, String subject) {
try {
Uri destUri = Uri.parse("content://mms");
Set<String> recipients = new HashSet<>();
recipients.addAll(Arrays.asList(to));
long thread_id = Utils.getOrCreateThreadId(context, recipients);
// Create a dummy sms
ContentValues dummyValues = new ContentValues();
dummyValues.put("thread_id", thread_id);
dummyValues.put("body", " ");
Uri dummySms = context.getContentResolver().insert(Uri.parse("content://sms/sent"), dummyValues);
// Create a new message entry
long now = System.currentTimeMillis();
ContentValues mmsValues = new ContentValues();
mmsValues.put("thread_id", thread_id);
mmsValues.put("date", now / 1000L);
mmsValues.put("msg_box", 4);
//mmsValues.put("m_id", System.currentTimeMillis());
mmsValues.put("read", true);
mmsValues.put("sub", subject != null ? subject : "");
mmsValues.put("sub_cs", 106);
mmsValues.put("ct_t", "application/vnd.wap.multipart.related");
long imageBytes = 0;
for (MMSPart part : parts) {
imageBytes += part.Data.length;
}
mmsValues.put("exp", imageBytes);
mmsValues.put("m_cls", "personal");
// 132 (RETRIEVE CONF) 130 (NOTIF IND) 128 (SEND REQ)
mmsValues.put("m_type", 128);
mmsValues.put("v", 19);
mmsValues.put("pri", 129);
mmsValues.put("tr_id", "T" + Long.toHexString(now));
mmsValues.put("resp_st", 128);
// Insert message
Uri res = context.getContentResolver().insert(destUri, mmsValues);
String messageId = res.getLastPathSegment().trim();
// Create part
for (MMSPart part : parts) {
if (part.MimeType.startsWith("image")) {
createPartImage(context, messageId, part.Data, part.MimeType);
} else if (part.MimeType.startsWith("text")) {
createPartText(context, messageId, new String(part.Data, "UTF-8"));
}
}
// Create addresses
for (String addr : to) {
createAddr(context, messageId, addr);
}
//res = Uri.parse(destUri + "/" + messageId);
// Delete dummy sms
context.getContentResolver().delete(dummySms, null, null);
return res;
} catch (Exception e) {
if (LOCAL_LOGV)
Log.v(TAG, "still an error saving... :(");
Log.e(TAG, "exception thrown", e);
}
return null;
}
use of com.google.android.mms.MMSPart 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;
}
use of com.google.android.mms.MMSPart 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);
}
}
}
Aggregations