Prepare delivery id before sending text message. Should prevent some crash
This commit is contained in:
parent
b0a3e0959f
commit
1ddedcc67b
@ -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"/>
|
||||||
|
@ -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;
|
||||||
@ -171,7 +173,7 @@ public class MultiSmsSender extends Activity {
|
|||||||
mDeliveryCheckBox = (CheckBox) findViewById(R.id.deliveryCheckBox);
|
mDeliveryCheckBox = (CheckBox) findViewById(R.id.deliveryCheckBox);
|
||||||
|
|
||||||
mContacts.setImeOptions(EditorInfo.IME_ACTION_NEXT);
|
mContacts.setImeOptions(EditorInfo.IME_ACTION_NEXT);
|
||||||
// mEditor.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
// mEditor.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
||||||
|
|
||||||
|
|
||||||
mAddButton.setOnClickListener(new OnClickListener() {
|
mAddButton.setOnClickListener(new OnClickListener() {
|
||||||
@ -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,48 +306,45 @@ 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();
|
|
||||||
Bundle b = new Bundle();
|
|
||||||
b.putInt("ORIGIN", DIALOG_PROGRESS);
|
|
||||||
b.putInt("total", (i*100)/size);
|
|
||||||
msg.setData(b);
|
|
||||||
handler.sendMessage(msg);
|
|
||||||
|
|
||||||
|
|
||||||
if (!allreadySend.contains(newN)) {
|
Message msg = handler.obtainMessage();
|
||||||
allreadySend.add(newN);
|
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) {
|
if (haveDeliveryReports) {
|
||||||
ContentValues values = new ContentValues();
|
deliveryIntents = new ArrayList<PendingIntent>(messageCount);
|
||||||
values.put("address", newN);
|
// Add to the Google MMS app
|
||||||
values.put("body", message);
|
ContentValues values = new ContentValues();
|
||||||
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
|
values.put("address", newN);
|
||||||
|
values.put("body", message);
|
||||||
|
|
||||||
long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId);
|
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
|
||||||
//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 );
|
|
||||||
|
|
||||||
|
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);
|
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,16 +362,24 @@ 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
|
||||||
protected Dialog onCreateDialog(int id) {
|
protected Dialog onCreateDialog(int id) {
|
||||||
switch (id) {
|
switch (id) {
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user