use of android.telephony.SmsManager in project Signal-Android by WhisperSystems.
the class IncomingLollipopMmsConnection method retrieve.
@Override
@TargetApi(VERSION_CODES.LOLLIPOP)
@Nullable
public synchronized RetrieveConf retrieve(@NonNull String contentLocation, byte[] transactionId, int subscriptionId) throws MmsException {
beginTransaction();
try {
MmsBodyProvider.Pointer pointer = MmsBodyProvider.makeTemporaryPointer(getContext());
Log.w(TAG, "downloading multimedia from " + contentLocation + " to " + pointer.getUri());
SmsManager smsManager;
if (VERSION.SDK_INT >= 22 && subscriptionId != -1) {
smsManager = SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
} else {
smsManager = SmsManager.getDefault();
}
smsManager.downloadMultimediaMessage(getContext(), contentLocation, pointer.getUri(), null, getPendingIntent());
waitForResult();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Util.copy(pointer.getInputStream(), baos);
pointer.close();
Log.w(TAG, baos.size() + "-byte response: " + Hex.dump(baos.toByteArray()));
return (RetrieveConf) new PduParser(baos.toByteArray()).parse();
} catch (IOException | TimeoutException e) {
Log.w(TAG, e);
throw new MmsException(e);
} finally {
endTransaction();
}
}
use of android.telephony.SmsManager in project Signal-Android by WhisperSystems.
the class OutgoingLollipopMmsConnection method send.
@Override
@TargetApi(VERSION_CODES.LOLLIPOP)
@Nullable
public synchronized SendConf send(@NonNull byte[] pduBytes, int subscriptionId) throws UndeliverableMessageException {
beginTransaction();
try {
MmsBodyProvider.Pointer pointer = MmsBodyProvider.makeTemporaryPointer(getContext());
Util.copy(new ByteArrayInputStream(pduBytes), pointer.getOutputStream());
SmsManager smsManager;
if (VERSION.SDK_INT >= 22 && subscriptionId != -1) {
smsManager = SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
} else {
smsManager = SmsManager.getDefault();
}
smsManager.sendMultimediaMessage(getContext(), pointer.getUri(), null, null, getPendingIntent());
waitForResult();
Log.w(TAG, "MMS broadcast received and processed.");
pointer.close();
if (response == null) {
throw new UndeliverableMessageException("Null response.");
}
return (SendConf) new PduParser(response).parse();
} catch (IOException | TimeoutException e) {
throw new UndeliverableMessageException(e);
} finally {
endTransaction();
}
}
use of android.telephony.SmsManager 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.telephony.SmsManager in project android-common by litesuits.
the class SmsReceiver method sendMsgToPhone.
/**
* Call requires API level 4
* <uses-permission android:name="android.permission.SEND_SMS"/>
*
* @param phone
* @param msg
*/
public static void sendMsgToPhone(String phone, String msg) {
Log.i(TAG, "发送手机:" + phone + " ,内容: " + msg);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
SmsManager manager = SmsManager.getDefault();
List<String> texts = manager.divideMessage(msg);
for (String txt : texts) {
manager.sendTextMessage(phone, null, txt, null, null);
}
} else {
Log.e(TAG, "发送失败,系统版本低于DONUT," + phone + " ,内容: " + msg);
}
}
use of android.telephony.SmsManager in project AndroidUtilCode by Blankj.
the class PhoneUtils method sendSmsSilent.
/**
* 发送短信
* <p>需添加权限 {@code <uses-permission android:name="android.permission.SEND_SMS"/>}</p>
*
* @param phoneNumber 接收号码
* @param content 短信内容
*/
public static void sendSmsSilent(String phoneNumber, String content) {
if (StringUtils.isEmpty(content))
return;
PendingIntent sentIntent = PendingIntent.getBroadcast(Utils.getContext(), 0, new Intent(), 0);
SmsManager smsManager = SmsManager.getDefault();
if (content.length() >= 70) {
List<String> ms = smsManager.divideMessage(content);
for (String str : ms) {
smsManager.sendTextMessage(phoneNumber, null, str, sentIntent, null);
}
} else {
smsManager.sendTextMessage(phoneNumber, null, content, sentIntent, null);
}
}
Aggregations