use of com.android.mms.transaction.MmsMessageSender 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