Search in sources :

Example 41 with Uri

use of android.net.Uri in project qksms by moezbhatti.

the class DeliveredReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (LOCAL_LOGV)
        Log.v(TAG, "marking message as delivered");
    Uri uri;
    try {
        uri = Uri.parse(intent.getStringExtra("message_uri"));
        if (uri.equals("")) {
            uri = null;
        }
    } catch (Exception e) {
        uri = null;
    }
    switch(getResultCode()) {
        case Activity.RESULT_OK:
            // notify user that message was delivered
            Intent delivered = new Intent(Transaction.NOTIFY_OF_DELIVERY);
            delivered.putExtra("result", true);
            delivered.putExtra("message_uri", uri == null ? "" : uri.toString());
            context.sendBroadcast(delivered);
            if (uri != null) {
                ContentValues values = new ContentValues();
                values.put("status", "0");
                values.put("date_sent", Calendar.getInstance().getTimeInMillis());
                values.put("read", true);
                context.getContentResolver().update(uri, values, null, null);
            } else {
                Cursor query = context.getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, "date desc");
                // mark message as delivered in database
                if (query.moveToFirst()) {
                    String id = query.getString(query.getColumnIndex("_id"));
                    ContentValues values = new ContentValues();
                    values.put("status", "0");
                    values.put("date_sent", Calendar.getInstance().getTimeInMillis());
                    values.put("read", true);
                    context.getContentResolver().update(Uri.parse("content://sms/sent"), values, "_id=" + id, null);
                }
                query.close();
            }
            break;
        case Activity.RESULT_CANCELED:
            // notify user that message failed to be delivered
            Intent notDelivered = new Intent(Transaction.NOTIFY_OF_DELIVERY);
            notDelivered.putExtra("result", false);
            notDelivered.putExtra("message_uri", uri == null ? "" : uri.toString());
            context.sendBroadcast(notDelivered);
            if (uri != null) {
                ContentValues values = new ContentValues();
                values.put("status", "64");
                values.put("date_sent", Calendar.getInstance().getTimeInMillis());
                values.put("read", true);
                values.put("error_code", getResultCode());
                context.getContentResolver().update(uri, values, null, null);
            } else {
                Cursor query2 = context.getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, "date desc");
                // mark failed in database
                if (query2.moveToFirst()) {
                    String id = query2.getString(query2.getColumnIndex("_id"));
                    ContentValues values = new ContentValues();
                    values.put("status", "64");
                    values.put("read", true);
                    values.put("error_code", getResultCode());
                    context.getContentResolver().update(Uri.parse("content://sms/sent"), values, "_id=" + id, null);
                }
                query2.close();
            }
            break;
    }
    context.sendBroadcast(new Intent("com.moez.QKSMS.send_message.REFRESH"));
}
Also used : ContentValues(android.content.ContentValues) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 42 with Uri

use of android.net.Uri in project qksms by moezbhatti.

the class SentReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    if (LOCAL_LOGV)
        Log.v(TAG, "marking message as sent");
    Uri uri;
    try {
        uri = Uri.parse(intent.getStringExtra("message_uri"));
        if (uri.equals("")) {
            uri = null;
        }
    } catch (Exception e) {
        uri = null;
    }
    switch(getResultCode()) {
        case Activity.RESULT_OK:
            if (uri != null) {
                try {
                    if (LOCAL_LOGV)
                        Log.v(TAG, "using supplied uri");
                    ContentValues values = new ContentValues();
                    values.put("type", 2);
                    values.put("read", 1);
                    context.getContentResolver().update(uri, values, null, null);
                } catch (NullPointerException e) {
                    markFirstAsSent(context);
                }
            } else {
                markFirstAsSent(context);
            }
            break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
        case SmsManager.RESULT_ERROR_NO_SERVICE:
        case SmsManager.RESULT_ERROR_NULL_PDU:
        case SmsManager.RESULT_ERROR_RADIO_OFF:
            if (uri != null) {
                if (LOCAL_LOGV)
                    Log.v(TAG, "using supplied uri");
                ContentValues values = new ContentValues();
                values.put("type", 5);
                values.put("read", true);
                values.put("error_code", getResultCode());
                context.getContentResolver().update(uri, values, null, null);
            } else {
                if (LOCAL_LOGV)
                    Log.v(TAG, "using first message");
                Cursor query = context.getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
                // mark message failed
                if (query != null && query.moveToFirst()) {
                    String id = query.getString(query.getColumnIndex("_id"));
                    ContentValues values = new ContentValues();
                    values.put("type", 5);
                    values.put("read", 1);
                    values.put("error_code", getResultCode());
                    context.getContentResolver().update(Uri.parse("content://sms/outbox"), values, "_id=" + id, null);
                }
            }
            context.sendBroadcast(new Intent(Transaction.NOTIFY_SMS_FAILURE));
            break;
    }
    context.sendBroadcast(new Intent("com.moez.QKSMS.send_message.REFRESH"));
}
Also used : ContentValues(android.content.ContentValues) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 43 with Uri

use of android.net.Uri in project qksms by moezbhatti.

the class Transaction method createAddr.

// add address to the request
private static Uri createAddr(Context context, String id, String addr) throws Exception {
    ContentValues addrValues = new ContentValues();
    addrValues.put("address", addr);
    addrValues.put("charset", "106");
    // TO
    addrValues.put("type", 151);
    Uri addrUri = Uri.parse("content://mms/" + id + "/addr");
    Uri res = context.getContentResolver().insert(addrUri, addrValues);
    return res;
}
Also used : ContentValues(android.content.ContentValues) Uri(android.net.Uri)

Example 44 with Uri

use of android.net.Uri in project qksms by moezbhatti.

the class Transaction method sendSmsMessage.

private void sendSmsMessage(String text, String[] addresses, long threadId, int delay) {
    if (LOCAL_LOGV)
        Log.v(TAG, "message text: " + text);
    Uri messageUri = null;
    int messageId = 0;
    if (saveMessage) {
        if (LOCAL_LOGV)
            Log.v(TAG, "saving message");
        // add signature to original text to be saved in database (does not strip unicode for saving though)
        if (!settings.getSignature().equals("")) {
            text += "\n" + settings.getSignature();
        }
        // save the message for each of the addresses
        for (int i = 0; i < addresses.length; i++) {
            Calendar cal = Calendar.getInstance();
            ContentValues values = new ContentValues();
            values.put("address", addresses[i]);
            values.put("body", settings.getStripUnicode() ? StripAccents.stripAccents(text) : text);
            values.put("date", cal.getTimeInMillis() + "");
            values.put("read", 1);
            values.put("type", 4);
            // attempt to create correct thread id if one is not supplied
            if (threadId == NO_THREAD_ID || addresses.length > 1) {
                threadId = Utils.getOrCreateThreadId(context, addresses[i]);
            }
            if (LOCAL_LOGV)
                Log.v(TAG, "saving message with thread id: " + threadId);
            values.put("thread_id", threadId);
            messageUri = context.getContentResolver().insert(Uri.parse("content://sms/"), values);
            if (LOCAL_LOGV)
                Log.v(TAG, "inserted to uri: " + messageUri);
            Cursor query = context.getContentResolver().query(messageUri, new String[] { "_id" }, null, null, null);
            if (query != null && query.moveToFirst()) {
                messageId = query.getInt(0);
            }
            if (LOCAL_LOGV)
                Log.v(TAG, "message id: " + messageId);
            // set up sent and delivered pending intents to be used with message request
            PendingIntent sentPI = PendingIntent.getBroadcast(context, messageId, new Intent(SMS_SENT).putExtra("message_uri", messageUri == null ? "" : messageUri.toString()), PendingIntent.FLAG_UPDATE_CURRENT);
            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, messageId, new Intent(SMS_DELIVERED).putExtra("message_uri", messageUri == null ? "" : messageUri.toString()), PendingIntent.FLAG_UPDATE_CURRENT);
            ArrayList<PendingIntent> sPI = new ArrayList<>();
            ArrayList<PendingIntent> dPI = new ArrayList<>();
            String body = text;
            // edit the body of the text if unicode needs to be stripped
            if (settings.getStripUnicode()) {
                body = StripAccents.stripAccents(body);
            }
            if (!settings.getPreText().equals("")) {
                body = settings.getPreText() + " " + body;
            }
            SmsManager smsManager = SmsManager.getDefault();
            if (LOCAL_LOGV)
                Log.v(TAG, "found sms manager");
            if (QKPreferences.getBoolean(QKPreference.SPLIT_SMS)) {
                if (LOCAL_LOGV)
                    Log.v(TAG, "splitting message");
                // figure out the length of supported message
                int[] splitData = SmsMessage.calculateLength(body, false);
                // we take the current length + the remaining length to get the total number of characters
                // that message set can support, and then divide by the number of message that will require
                // to get the length supported by a single message
                int length = (body.length() + splitData[2]) / splitData[0];
                if (LOCAL_LOGV)
                    Log.v(TAG, "length: " + length);
                boolean counter = false;
                if (settings.getSplitCounter() && body.length() > length) {
                    counter = true;
                    length -= 6;
                }
                // get the split messages
                String[] textToSend = splitByLength(body, length, counter);
                // send each message part to each recipient attached to message
                for (int j = 0; j < textToSend.length; j++) {
                    ArrayList<String> parts = smsManager.divideMessage(textToSend[j]);
                    for (int k = 0; k < parts.size(); k++) {
                        sPI.add(saveMessage ? sentPI : null);
                        dPI.add(settings.getDeliveryReports() && saveMessage ? deliveredPI : null);
                    }
                    if (LOCAL_LOGV)
                        Log.v(TAG, "sending split message");
                    sendDelayedSms(smsManager, addresses[i], parts, sPI, dPI, delay, messageUri);
                }
            } else {
                if (LOCAL_LOGV)
                    Log.v(TAG, "sending without splitting");
                // send the message normally without forcing anything to be split
                ArrayList<String> parts = smsManager.divideMessage(body);
                for (int j = 0; j < parts.size(); j++) {
                    sPI.add(saveMessage ? sentPI : null);
                    dPI.add(settings.getDeliveryReports() && saveMessage ? deliveredPI : null);
                }
                try {
                    if (LOCAL_LOGV)
                        Log.v(TAG, "sent message");
                    sendDelayedSms(smsManager, addresses[i], parts, sPI, dPI, delay, messageUri);
                } catch (Exception e) {
                    // whoops...
                    if (LOCAL_LOGV)
                        Log.v(TAG, "error sending message");
                    Log.e(TAG, "exception thrown", e);
                    try {
                        ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content).post(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(context, "Message could not be sent", Toast.LENGTH_LONG).show();
                            }
                        });
                    } catch (Exception f) {
                    }
                }
            }
        }
    }
}
Also used : ContentValues(android.content.ContentValues) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Activity(android.app.Activity) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Cursor(android.database.Cursor) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SmsManager(android.telephony.SmsManager) PendingIntent(android.app.PendingIntent)

Example 45 with Uri

use of android.net.Uri in project qksms by moezbhatti.

the class Transaction method createPartImage.

// create the image part to be stored in database
private static Uri createPartImage(Context context, String id, byte[] imageBytes, String mimeType) throws Exception {
    ContentValues mmsPartValue = new ContentValues();
    mmsPartValue.put("mid", id);
    mmsPartValue.put("ct", mimeType);
    mmsPartValue.put("cid", "<" + System.currentTimeMillis() + ">");
    Uri partUri = Uri.parse("content://mms/" + id + "/part");
    Uri res = context.getContentResolver().insert(partUri, mmsPartValue);
    // Add data to part
    OutputStream os = context.getContentResolver().openOutputStream(res);
    ByteArrayInputStream is = new ByteArrayInputStream(imageBytes);
    byte[] buffer = new byte[256];
    for (int len = 0; (len = is.read(buffer)) != -1; ) {
        os.write(buffer, 0, len);
    }
    os.close();
    is.close();
    return res;
}
Also used : ContentValues(android.content.ContentValues) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Uri(android.net.Uri)

Aggregations

Uri (android.net.Uri)6747 Intent (android.content.Intent)1547 Cursor (android.database.Cursor)894 File (java.io.File)744 Test (org.junit.Test)632 ContentValues (android.content.ContentValues)614 IOException (java.io.IOException)571 ContentResolver (android.content.ContentResolver)449 ArrayList (java.util.ArrayList)446 Bundle (android.os.Bundle)289 Context (android.content.Context)276 Bitmap (android.graphics.Bitmap)273 InputStream (java.io.InputStream)257 View (android.view.View)251 RemoteException (android.os.RemoteException)228 PendingIntent (android.app.PendingIntent)209 SuppressLint (android.annotation.SuppressLint)180 FileNotFoundException (java.io.FileNotFoundException)177 TextView (android.widget.TextView)175 ActivityNotFoundException (android.content.ActivityNotFoundException)169