Prepare delivery id before sending text message. Should prevent some crash

This commit is contained in:
Mathieu Maret 2011-07-25 01:00:48 +02:00
parent b0a3e0959f
commit 1ddedcc67b
3 changed files with 81 additions and 137 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hectorone.multismssender" android:versionName="1.6" android:versionCode="6"> package="com.hectorone.multismssender" android:versionName="1.7" android:versionCode="7">
<application android:label="@string/app_name" android:icon="@drawable/multisms"> <application android:label="@string/app_name" android:icon="@drawable/multisms">
<activity android:name=".MultiSmsSender" android:label="@string/app_name"> <activity android:name=".MultiSmsSender" android:label="@string/app_name">
<intent-filter> <intent-filter>
@ -13,7 +13,7 @@
<activity android:name="GroupEditActivity"></activity> <activity android:name="GroupEditActivity"></activity>
<activity android:name="SelectDeliveryActivity"></activity> <activity android:name="SelectDeliveryActivity"></activity>
<activity android:name="ListEntryActivity"></activity> <activity android:name="ListEntryActivity"></activity>
<receiver android:name=".MessageReceiver" android:enabled="true" > <receiver android:name="MessageReceiver" android:enabled="true" >
<intent-filter> <intent-filter>
<action android:name="com.hectorone.multismssender.SMS_RECEIVED"/> <action android:name="com.hectorone.multismssender.SMS_RECEIVED"/>
<data android:mimeType="com.hectorone.multismssender.item/entry"/> <data android:mimeType="com.hectorone.multismssender.item/entry"/>

View File

@ -3,6 +3,7 @@ package com.hectorone.multismssender;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import android.app.Activity; import android.app.Activity;
@ -18,6 +19,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.telephony.PhoneNumberUtils; import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -218,23 +220,20 @@ public class MultiSmsSender extends Activity {
} }
public void sendMessage(Handler handler) { public void sendMessage(Handler handler) {
MySMSManager manager = new MySMSManager(); SmsManager manager = SmsManager.getDefault();
String message = mEditor.getText().toString(); String message = mEditor.getText().toString();
HashMap<String, Long> deliveryIdMap = new HashMap<String, Long>();
mManyMessageContinue = true; mManyMessageContinue = true;
if("".equals(message)) { if("".equals(message)) {
{ {
Message msg = handler.obtainMessage(); displayDialog(handler, DIALOG_PROGRESS_CANCEL,null);
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_PROGRESS_CANCEL);
msg.setData(b);
handler.sendMessage(msg);
} }
return; return;
} }
String[] numbers = mContacts.getText().toString().split(","); String[] numbers = mContacts.getText().toString().split(",");
HashSet<String> allreadySend = new HashSet<String>(); ArrayList<String> phoneNumberConform = new ArrayList<String>();
int size = numbers.length; int size = numbers.length;
boolean haveDeliveryReports = mDeliveryCheckBox.isChecked(); boolean haveDeliveryReports = mDeliveryCheckBox.isChecked();
long deliveryId = -1; long deliveryId = -1;
@ -248,14 +247,33 @@ public class MultiSmsSender extends Activity {
.format(new Date())); .format(new Date()));
} }
//Check if numbers are correct and prepare deliveryId
for(int i= 0; i< size; i++) {
String newN = numbers[i].trim();
if (!newN.equals("")
&& PhoneNumberUtils.isWellFormedSmsAddress(newN)
&& !phoneNumberConform.contains(newN)) {
phoneNumberConform.add(newN);
if(haveDeliveryReports) {
long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId);
deliveryIdMap.put(newN,entryId);
}
}
}
numbers = new String[size];
numbers = phoneNumberConform.toArray(numbers);
size = phoneNumberConform.size();
if (size != 0) { if (size != 0) {
if(size > MANY_MESSAGE){ if(size > MANY_MESSAGE){
{ {
Message msg = handler.obtainMessage(); displayDialog(handler, DIALOG_MANYMESSAGE,null);
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_MANYMESSAGE);
msg.setData(b);
handler.sendMessage(msg);
} }
synchronized (MultiSmsSender.this) { synchronized (MultiSmsSender.this) {
try { try {
@ -269,15 +287,13 @@ public class MultiSmsSender extends Activity {
} }
if(mManyMessageContinue) { if(mManyMessageContinue) {
int message_sent = 0; int message_sent = 0;
int chunk_max = Math.min(MANY_MESSAGE, size); int chunk_max = Math.min(MANY_MESSAGE, size);
do { do {
if(message_sent > 0) { if(message_sent > 0) {
Message msg = handler.obtainMessage(); displayDialog(handler, DIALOG_STARTWAIT,null);
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_STARTWAIT);
msg.setData(b);
handler.sendMessage(msg);
synchronized (MultiSmsSender.this) { synchronized (MultiSmsSender.this) {
try { try {
MultiSmsSender.this.wait(); MultiSmsSender.this.wait();
@ -290,10 +306,9 @@ public class MultiSmsSender extends Activity {
//Log.d(DEBUG_TAG, "size is"+size+" message_sent "+message_sent+" max"+chunk_max); //Log.d(DEBUG_TAG, "size is"+size+" message_sent "+message_sent+" max"+chunk_max);
for(int i = message_sent; i < chunk_max; i++) { for(int i = message_sent; i < chunk_max; i++) {
message_sent ++; message_sent ++;
String newN = numbers[i].trim(); String newN = numbers[i];
if (!newN.equals("")
&& PhoneNumberUtils.isWellFormedSmsAddress(newN)) {
Message msg = handler.obtainMessage(); Message msg = handler.obtainMessage();
Bundle b = new Bundle(); Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_PROGRESS); b.putInt("ORIGIN", DIALOG_PROGRESS);
@ -302,21 +317,21 @@ public class MultiSmsSender extends Activity {
handler.sendMessage(msg); handler.sendMessage(msg);
if (!allreadySend.contains(newN)) { ArrayList<PendingIntent> deliveryIntents = null;
allreadySend.add(newN);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(messageCount);
ArrayList<PendingIntent> sentIntents = null; ArrayList<PendingIntent> sentIntents = null;
if (haveDeliveryReports) { if (haveDeliveryReports) {
deliveryIntents = new ArrayList<PendingIntent>(messageCount);
// Add to the Google MMS app
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("address", newN); values.put("address", newN);
values.put("body", message); values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values); getContentResolver().insert(Uri.parse("content://sms/sent"), values);
long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId); long entryId = deliveryIdMap.get(newN);
//Log.d(DEBUG_TAG, "entry is "+entryId+" to number"+newN); //Log.d(DEBUG_TAG, "entry is "+entryId+" to number"+newN);
for (int j = 0; j < messageCount; j++) { for (int j = 0; j < messageCount; j++) {
Uri entryURI = Uri.withAppendedPath(EntryContentProvider.CONTENT_URI, ""+entryId); Uri entryURI = Uri.withAppendedPath(EntryContentProvider.CONTENT_URI, ""+entryId);
@ -330,8 +345,6 @@ public class MultiSmsSender extends Activity {
} }
manager.sendMultipartTextMessage(newN, null, messages, sentIntents, deliveryIntents ); manager.sendMultipartTextMessage(newN, null, messages, sentIntents, deliveryIntents );
}
}
} }
chunk_max = Math.min(message_sent +MANY_MESSAGE, size); chunk_max = Math.min(message_sent +MANY_MESSAGE, size);
@ -341,7 +354,7 @@ public class MultiSmsSender extends Activity {
Message msg = handler.obtainMessage(); Message msg = handler.obtainMessage();
Bundle b = new Bundle(); Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_FINISHED); b.putInt("ORIGIN", DIALOG_FINISHED);
b.putInt("total", allreadySend.size() ); b.putInt("total", phoneNumberConform.size() );
msg.setData(b); msg.setData(b);
handler.sendMessage(msg); handler.sendMessage(msg);
@ -349,14 +362,22 @@ public class MultiSmsSender extends Activity {
} else { } else {
Message msg = handler.obtainMessage(); displayDialog(handler, DIALOG_NONUMBER,null);
Bundle b = new Bundle(); }
b.putInt("ORIGIN", DIALOG_NONUMBER);
msg.setData(b);
handler.sendMessage(msg);
} }
private void displayDialog(Handler handler, int dialogId, HashMap<String, Integer> params) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", dialogId);
if (params != null) {
for (String paramName : params.keySet()) {
b.putInt(paramName, params.get(paramName));
}
}
msg.setData(b);
handler.sendMessage(msg);
} }
@Override @Override

View File

@ -1,77 +0,0 @@
package com.hectorone.multismssender;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import android.app.PendingIntent;
import android.os.Build;
import android.util.Log;
public class MySMSManager {
private static Object manager;
private static Method getDefault;
private static Method divide;
private static Method sendMutlipart;
public MySMSManager() {
if(manager == null) {
String className;
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
if (sdkVersion <= 3) { //CUPCAKE
Log.d("MultiSMSSender", "Cupcake version");
className = "android.telephony.gsm.SmsManager";
} else {
Log.d("MultiSMSSender", "version > Cupcake");
className = "android.telephony.SmsManager";
}
try {
Class clazz =
Class.forName(className);
Object[] paramInvoke = new Object[0];
Class[] signGetDefault = new Class[]{};
Class<String>[] signDivide = new Class[] {String.class};
Class[] signSend = new Class[] {String.class, String.class, ArrayList.class, ArrayList.class, ArrayList.class};
getDefault = clazz.getMethod("getDefault", signGetDefault);
divide = clazz.getMethod("divideMessage", signDivide);
sendMutlipart = clazz.getMethod("sendMultipartTextMessage", signSend);
manager = getDefault.invoke(null, paramInvoke);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public ArrayList<String> divideMessage(String text){
Object[] param = new Object[1];
param[0] = text;
try {
return (ArrayList<String>)divide.invoke(manager, param);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents ) {
Object[] param = new Object[5];
param[0] = destinationAddress;
param[1] = scAddress;
param[2] = parts;
param[3] = sentIntents;
param[4] = deliveryIntents;
try {
sendMutlipart.invoke(manager, param);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}