Search in sources :

Example 1 with MmsMessageSender

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);
        }
    }
}
Also used : Context(android.content.Context) MmsMessageSender(com.android.mms.transaction.MmsMessageSender) IntentFilter(android.content.IntentFilter) ArrayList(java.util.ArrayList) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MmsException(com.google.android.mms.MmsException) MMSPart(com.google.android.mms.MMSPart)

Aggregations

PendingIntent (android.app.PendingIntent)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 MmsMessageSender (com.android.mms.transaction.MmsMessageSender)1 MMSPart (com.google.android.mms.MMSPart)1 MmsException (com.google.android.mms.MmsException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1