From 1ddedcc67b09a545ab269401190337539b2b291c Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Mon, 25 Jul 2011 01:00:48 +0200 Subject: [PATCH] Prepare delivery id before sending text message. Should prevent some crash --- AndroidManifest.xml | 4 +- .../multismssender/MultiSmsSender.java | 137 ++++++++++-------- .../multismssender/MySMSManager.java | 77 ---------- 3 files changed, 81 insertions(+), 137 deletions(-) delete mode 100644 src/com/hectorone/multismssender/MySMSManager.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index d06aefa..a66c04c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.hectorone.multismssender" android:versionName="1.7" android:versionCode="7"> @@ -13,7 +13,7 @@ - + diff --git a/src/com/hectorone/multismssender/MultiSmsSender.java b/src/com/hectorone/multismssender/MultiSmsSender.java index f95cf28..5513253 100644 --- a/src/com/hectorone/multismssender/MultiSmsSender.java +++ b/src/com/hectorone/multismssender/MultiSmsSender.java @@ -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 deliveryIdMap = new HashMap(); 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 allreadySend = new HashSet(); + ArrayList phoneNumberConform = new ArrayList(); 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 deliveryIntents = new ArrayList(messageCount); - ArrayList sentIntents = null; + + ArrayList deliveryIntents = null; + ArrayList 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(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 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) { diff --git a/src/com/hectorone/multismssender/MySMSManager.java b/src/com/hectorone/multismssender/MySMSManager.java deleted file mode 100644 index 4503835..0000000 --- a/src/com/hectorone/multismssender/MySMSManager.java +++ /dev/null @@ -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[] 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 divideMessage(String text){ - Object[] param = new Object[1]; - param[0] = text; - try { - return (ArrayList)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 parts, ArrayList sentIntents, ArrayList 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(); - } - } - -}