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"?>
<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">
<activity android:name=".MultiSmsSender" android:label="@string/app_name">
<intent-filter>
@ -13,7 +13,7 @@
<activity android:name="GroupEditActivity"></activity>
<activity android:name="SelectDeliveryActivity"></activity>
<activity android:name="ListEntryActivity"></activity>
<receiver android:name=".MessageReceiver" android:enabled="true" >
<receiver android:name="MessageReceiver" android:enabled="true" >
<intent-filter>
<action android:name="com.hectorone.multismssender.SMS_RECEIVED"/>
<data android:mimeType="com.hectorone.multismssender.item/entry"/>

View File

@ -3,6 +3,7 @@ package com.hectorone.multismssender;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import android.app.Activity;
@ -18,6 +19,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -57,7 +59,7 @@ public class MultiSmsSender extends Activity {
public static final String PARAM_ENTRY_ID = "entry_id";
public static final String DEBUG_TAG="MultiSmsSender";
private boolean appli_running = true;
MessageSenderThread mThreadSender;
@ -171,7 +173,7 @@ public class MultiSmsSender extends Activity {
mDeliveryCheckBox = (CheckBox) findViewById(R.id.deliveryCheckBox);
mContacts.setImeOptions(EditorInfo.IME_ACTION_NEXT);
// mEditor.setImeOptions(EditorInfo.IME_ACTION_DONE);
// mEditor.setImeOptions(EditorInfo.IME_ACTION_DONE);
mAddButton.setOnClickListener(new OnClickListener() {
@ -218,23 +220,20 @@ public class MultiSmsSender extends Activity {
}
public void sendMessage(Handler handler) {
MySMSManager manager = new MySMSManager();
SmsManager manager = SmsManager.getDefault();
String message = mEditor.getText().toString();
HashMap<String, Long> deliveryIdMap = new HashMap<String, Long>();
mManyMessageContinue = true;
if("".equals(message)) {
{
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_PROGRESS_CANCEL);
msg.setData(b);
handler.sendMessage(msg);
displayDialog(handler, DIALOG_PROGRESS_CANCEL,null);
}
return;
}
String[] numbers = mContacts.getText().toString().split(",");
HashSet<String> allreadySend = new HashSet<String>();
ArrayList<String> phoneNumberConform = new ArrayList<String>();
int size = numbers.length;
boolean haveDeliveryReports = mDeliveryCheckBox.isChecked();
long deliveryId = -1;
@ -248,14 +247,33 @@ public class MultiSmsSender extends Activity {
.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 > MANY_MESSAGE){
{
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_MANYMESSAGE);
msg.setData(b);
handler.sendMessage(msg);
displayDialog(handler, DIALOG_MANYMESSAGE,null);
}
synchronized (MultiSmsSender.this) {
try {
@ -269,15 +287,13 @@ public class MultiSmsSender extends Activity {
}
if(mManyMessageContinue) {
int message_sent = 0;
int chunk_max = Math.min(MANY_MESSAGE, size);
do {
if(message_sent > 0) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_STARTWAIT);
msg.setData(b);
handler.sendMessage(msg);
displayDialog(handler, DIALOG_STARTWAIT,null);
synchronized (MultiSmsSender.this) {
try {
MultiSmsSender.this.wait();
@ -290,48 +306,45 @@ public class MultiSmsSender extends Activity {
//Log.d(DEBUG_TAG, "size is"+size+" message_sent "+message_sent+" max"+chunk_max);
for(int i = message_sent; i < chunk_max; i++) {
message_sent ++;
String newN = numbers[i].trim();
if (!newN.equals("")
&& PhoneNumberUtils.isWellFormedSmsAddress(newN)) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_PROGRESS);
b.putInt("total", (i*100)/size);
msg.setData(b);
handler.sendMessage(msg);
String newN = numbers[i];
if (!allreadySend.contains(newN)) {
allreadySend.add(newN);
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_PROGRESS);
b.putInt("total", (i*100)/size);
msg.setData(b);
handler.sendMessage(msg);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(messageCount);
ArrayList<PendingIntent> sentIntents = null;
ArrayList<PendingIntent> deliveryIntents = null;
ArrayList<PendingIntent> sentIntents = null;
if (haveDeliveryReports) {
ContentValues values = new ContentValues();
values.put("address", newN);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
if (haveDeliveryReports) {
deliveryIntents = new ArrayList<PendingIntent>(messageCount);
// Add to the Google MMS app
ContentValues values = new ContentValues();
values.put("address", newN);
values.put("body", message);
long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId);
//Log.d(DEBUG_TAG, "entry is "+entryId+" to number"+newN);
for (int j = 0; j < messageCount; j++) {
Uri entryURI = Uri.withAppendedPath(EntryContentProvider.CONTENT_URI, ""+entryId);
Intent intent = new Intent(MessageReceiver.MESSAGE_RECEIVED,entryURI, this, MessageReceiver.class);
//intent.putExtra(PARAM_ENTRY_ID, entryId);
deliveryIntents.add(PendingIntent.getBroadcast(
this, 0, intent, 0));
}
}
manager.sendMultipartTextMessage(newN, null, messages, sentIntents, deliveryIntents );
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
long entryId = deliveryIdMap.get(newN);
//Log.d(DEBUG_TAG, "entry is "+entryId+" to number"+newN);
for (int j = 0; j < messageCount; j++) {
Uri entryURI = Uri.withAppendedPath(EntryContentProvider.CONTENT_URI, ""+entryId);
Intent intent = new Intent(MessageReceiver.MESSAGE_RECEIVED,entryURI, this, MessageReceiver.class);
//intent.putExtra(PARAM_ENTRY_ID, entryId);
deliveryIntents.add(PendingIntent.getBroadcast(
this, 0, intent, 0));
}
}
manager.sendMultipartTextMessage(newN, null, messages, sentIntents, deliveryIntents );
}
chunk_max = Math.min(message_sent +MANY_MESSAGE, size);
@ -341,7 +354,7 @@ public class MultiSmsSender extends Activity {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_FINISHED);
b.putInt("total", allreadySend.size() );
b.putInt("total", phoneNumberConform.size() );
msg.setData(b);
handler.sendMessage(msg);
@ -349,16 +362,24 @@ public class MultiSmsSender extends Activity {
} else {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("ORIGIN", DIALOG_NONUMBER);
msg.setData(b);
handler.sendMessage(msg);
displayDialog(handler, DIALOG_NONUMBER,null);
}
}
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
protected Dialog onCreateDialog(int id) {
switch (id) {

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();
}
}
}