use of android.content.ContentValues 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) {
}
}
}
}
}
}
use of android.content.ContentValues 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;
}
use of android.content.ContentValues in project qksms by moezbhatti.
the class Transaction method successVoice.
private void successVoice() {
if (saveMessage) {
Cursor query = context.getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
// mark message as sent successfully
if (query.moveToFirst()) {
String id = query.getString(query.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("type", "2");
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/outbox"), values, "_id=" + id, null);
}
query.close();
}
context.sendBroadcast(new Intent(REFRESH));
}
use of android.content.ContentValues in project rainbow by juankysoriano.
the class CaptureSketchUtils method insertImage.
/**
* A copy of the Android internals insertImage method, this method populates the
* meta data with DATE_ADDED and DATE_TAKEN. This fixes a common problem where media
* that is inserted manually gets saved at the end of the gallery (because date is not populated).
*
* @see android.provider.MediaStore.Images.Media#insertImage(ContentResolver, Bitmap, String, String)
*/
public static Uri insertImage(ContentResolver cr, Bitmap source, String title, String description) {
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, title);
values.put(Images.Media.DISPLAY_NAME, title);
values.put(Images.Media.DESCRIPTION, description);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
Uri uri = null;
try {
uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (source != null) {
OutputStream imageOut = cr.openOutputStream(uri);
try {
source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
} finally {
imageOut.close();
}
long id = ContentUris.parseId(uri);
// Wait until MINI_KIND thumbnail is generated.
Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id, Images.Thumbnails.MINI_KIND, null);
// This is for backward compatibility.
storeThumbnail(cr, miniThumb, id, 50F, 50F, Images.Thumbnails.MICRO_KIND);
} else {
cr.delete(uri, null, null);
uri = null;
}
} catch (Exception e) {
if (uri != null) {
cr.delete(uri, null, null);
uri = null;
}
}
return uri;
}
use of android.content.ContentValues in project rainbow by juankysoriano.
the class CaptureSketchUtils method storeThumbnail.
/**
* A copy of the Android internals StoreThumbnail method, it used with the insertImage to
* populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
* meta data. The StoreThumbnail method is private so it must be duplicated here.
*
* @see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
*/
private static Bitmap storeThumbnail(ContentResolver cr, Bitmap source, long id, float width, float height, int kind) {
// create the matrix to scale it
Matrix matrix = new Matrix();
float scaleX = width / source.getWidth();
float scaleY = height / source.getHeight();
matrix.setScale(scaleX, scaleY);
Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
ContentValues values = new ContentValues(4);
values.put(Images.Thumbnails.KIND, kind);
values.put(Images.Thumbnails.IMAGE_ID, (int) id);
values.put(Images.Thumbnails.HEIGHT, thumb.getHeight());
values.put(Images.Thumbnails.WIDTH, thumb.getWidth());
Uri url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values);
try {
OutputStream thumbOut = cr.openOutputStream(url);
thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
thumbOut.close();
return thumb;
} catch (FileNotFoundException ex) {
return null;
} catch (IOException ex) {
return null;
}
}
Aggregations