Skip to content

Commit

Permalink
fix issues with long sms messages
Browse files Browse the repository at this point in the history
  • Loading branch information
serggl committed Oct 27, 2015
1 parent f1128ff commit fb1bf01
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -9,4 +9,4 @@ build
.settings
.metadata
*.iml

local.properties
Expand Up @@ -7,9 +7,9 @@
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import java.net.URLEncoder;

public class HttpHookIntentService extends IntentService {
public static final String URL = "url";
Expand All @@ -27,7 +27,7 @@ protected void onHandleIntent(Intent intent) {
try
{
java.net.URL url = new URL(extras.getString(URL));
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
Expand All @@ -37,7 +37,7 @@ protected void onHandleIntent(Intent intent) {
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(String.format("content=%s&from_number=%s",
extras.getString(TEXT),
URLEncoder.encode(extras.getString(TEXT), "UTF-8"),
extras.getString(SENDER)));
writer.flush();
writer.close();
Expand Down
Expand Up @@ -10,6 +10,7 @@
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;
Expand All @@ -34,8 +35,12 @@ protected void onHandleIntent(Intent intent) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
String number = extras.getString("number");
String message = extras.getString("message");
if (number != null && number.length() > 0 && message != null && message.length() > 0) {
if (!TextUtils.isEmpty(number) && !TextUtils.isEmpty(message)) {
try {
if (!number.startsWith("+"))
{
number = "+" + number;
}
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, message, null, null);

Expand Down
Expand Up @@ -20,8 +20,28 @@ public void onReceive(Context context, Intent intent)
Object[] pdus = (Object[]) intent.getExtras().get(PDUS);
if (pdus != null && pdus.length > 0)
{
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[0]);
String text = message.getMessageBody();
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}

SmsMessage message = messages[0];
String text;
if (messages.length == 1 || message.isReplace())
{
text = message.getDisplayMessageBody();
}
else
{
StringBuilder textBuilder = new StringBuilder();
for (SmsMessage msg : messages)
{
textBuilder.append(msg.getMessageBody());
}
text = textBuilder.toString();
}

String number = message.getDisplayOriginatingAddress();

Intent i = new Intent(context, HttpHookIntentService.class);
Expand Down

0 comments on commit fb1bf01

Please sign in to comment.