2010-12-12 02:36:52 +01:00
package com.hectorone.multismssender ;
2009-10-22 14:26:02 +02:00
2010-01-22 15:21:33 +01:00
import java.text.DateFormat ;
import java.util.ArrayList ;
import java.util.Date ;
2009-10-22 14:26:02 +02:00
import java.util.HashSet ;
2010-12-12 03:06:07 +01:00
2010-12-12 02:36:52 +01:00
2009-10-22 14:26:02 +02:00
import android.app.Activity ;
import android.app.AlertDialog ;
import android.app.Dialog ;
2010-01-22 15:21:33 +01:00
import android.app.PendingIntent ;
2009-10-22 14:26:02 +02:00
import android.app.ProgressDialog ;
2010-12-10 22:31:06 +01:00
import android.content.ContentValues ;
2009-10-22 14:26:02 +02:00
import android.content.DialogInterface ;
import android.content.Intent ;
2010-01-27 17:59:55 +01:00
import android.net.Uri ;
2009-10-22 14:26:02 +02:00
import android.os.Bundle ;
2010-12-10 22:31:06 +01:00
import android.os.Handler ;
import android.os.Message ;
2010-01-05 14:57:29 +01:00
import android.telephony.PhoneNumberUtils ;
2010-01-27 17:59:55 +01:00
import android.util.Log ;
2009-10-22 14:26:02 +02:00
import android.view.Menu ;
import android.view.MenuItem ;
import android.view.View ;
import android.view.View.OnClickListener ;
import android.view.inputmethod.EditorInfo ;
import android.widget.Button ;
2010-01-22 15:21:33 +01:00
import android.widget.CheckBox ;
2009-10-22 14:26:02 +02:00
import android.widget.TextView ;
public class MultiSmsSender extends Activity {
private Button mAddButton ;
private Button mSend ;
private TextView mContacts ;
private TextView mEditor ;
2010-01-22 15:21:33 +01:00
private CheckBox mDeliveryCheckBox ;
2009-10-22 14:26:02 +02:00
2010-01-22 15:21:33 +01:00
private DeliveryDbAdapter mDbHelper ;
2009-10-22 14:26:02 +02:00
private ProgressDialog mSendingDialog ;
public static final int ACTIVITY_EDIT = 0 ;
public static final int ACTIVITY_ADD_GROUP = 1 ;
2010-01-22 15:21:33 +01:00
public static final int ACTIVITY_DELIVERY = 2 ;
2009-10-22 14:26:02 +02:00
private static final int SENDING_DIALOG_KEY = 0 ;
private static final int INSERT_ID = Menu . FIRST ;
2010-12-12 02:28:33 +01:00
public static final int MANY_MESSAGE = 50 ;
2010-12-10 22:31:06 +01:00
private static final int DIALOG_PROGRESS = 0 ;
private static final int DIALOG_FINISHED = 1 ;
private static final int DIALOG_NONUMBER = 2 ;
2010-12-12 02:28:33 +01:00
private static final int DIALOG_MANYMESSAGE = 3 ;
private static final int DIALOG_STARTWAIT = 4 ;
private static final int DIALOG_PROGRESS_CANCEL = 5 ;
2010-12-10 22:31:06 +01:00
2009-10-22 14:26:02 +02:00
public static final String PARAM_NUMBERS_LIST = " param number list " ;
2010-01-22 15:21:33 +01:00
public static final String PARAM_FLUSH = " param flush " ;
public static final String PARAM_ENTRY_ID = " entry_id " ;
2010-12-10 22:31:06 +01:00
2010-01-27 17:59:55 +01:00
public static final String DEBUG_TAG = " MultiSmsSender " ;
2009-10-22 14:26:02 +02:00
2010-12-12 02:28:33 +01:00
MessageSenderThread mThreadSender ;
private boolean mManyMessageContinue ;
2010-12-10 22:31:06 +01:00
final Handler mHandler = new Handler ( ) {
public void handleMessage ( Message msg ) {
int type = msg . getData ( ) . getInt ( " ORIGIN " ) ;
switch ( type ) {
case DIALOG_PROGRESS : {
int total = msg . getData ( ) . getInt ( " total " ) ;
Log . d ( DEBUG_TAG , " ========= total is " + total ) ;
mSendingDialog . setProgress ( total ) ;
}
break ;
2010-12-12 02:28:33 +01:00
case DIALOG_PROGRESS_CANCEL : {
mSendingDialog . cancel ( ) ;
}
break ;
2010-12-10 22:31:06 +01:00
case DIALOG_FINISHED : {
dismissDialog ( SENDING_DIALOG_KEY ) ;
int total = msg . getData ( ) . getInt ( " total " ) ;
new AlertDialog . Builder ( MultiSmsSender . this ) . setPositiveButton (
getResources ( ) . getString ( R . string . ok ) ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int which ) {
dialog . dismiss ( ) ;
}
} ) . setMessage (
total + " "
+ getResources ( ) . getString ( R . string . message_sent ) )
. show ( ) ;
break ;
}
2010-12-12 02:28:33 +01:00
2010-12-10 22:31:06 +01:00
case DIALOG_NONUMBER : {
new AlertDialog . Builder ( MultiSmsSender . this ) . setPositiveButton (
getResources ( ) . getString ( R . string . ok ) ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int which ) {
dialog . dismiss ( ) ;
}
} ) . setMessage (
getResources ( ) . getString ( R . string . enter_number ) ) . show ( ) ;
2010-12-12 02:28:33 +01:00
break ;
}
case DIALOG_MANYMESSAGE : {
new AlertDialog . Builder ( MultiSmsSender . this )
. setMessage ( " You are about to sent more than 50 Messages. As it may produce Android error that may crash the app, they will be send by chunk of 50 with a pause between each chunk " )
. setCancelable ( false )
. setPositiveButton ( getResources ( ) . getString ( R . string . ok ) , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int id ) {
synchronized ( MultiSmsSender . this ) {
MultiSmsSender . this . notify ( ) ;
mManyMessageContinue = true ;
}
dialog . dismiss ( ) ;
}
} )
. setNegativeButton ( getResources ( ) . getString ( R . string . cancel ) , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int id ) {
synchronized ( MultiSmsSender . this ) {
MultiSmsSender . this . notify ( ) ;
mManyMessageContinue = false ;
}
dialog . dismiss ( ) ;
}
} )
. show ( ) ;
break ;
}
case DIALOG_STARTWAIT : {
new AlertDialog . Builder ( MultiSmsSender . this ) . setPositiveButton (
getResources ( ) . getString ( R . string . ok ) ,
new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int which ) {
synchronized ( MultiSmsSender . this ) {
MultiSmsSender . this . notify ( ) ;
}
dialog . dismiss ( ) ;
}
} ) . setMessage ( " You are ready to continue for 50 more messages? " ) . show ( ) ;
break ;
2010-12-10 22:31:06 +01:00
}
}
}
} ;
2009-10-22 14:26:02 +02:00
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . main ) ;
2010-01-22 15:21:33 +01:00
mDbHelper = new DeliveryDbAdapter ( this ) ;
mDbHelper . open ( ) ;
2009-10-22 14:26:02 +02:00
mAddButton = ( Button ) findViewById ( R . id . contacts ) ;
mSend = ( Button ) findViewById ( R . id . send ) ;
mContacts = ( TextView ) findViewById ( R . id . numbers ) ;
mEditor = ( TextView ) findViewById ( R . id . editor ) ;
2010-01-22 15:21:33 +01:00
mDeliveryCheckBox = ( CheckBox ) findViewById ( R . id . deliveryCheckBox ) ;
2009-10-22 14:26:02 +02:00
mContacts . setImeOptions ( EditorInfo . IME_ACTION_NEXT ) ;
mEditor . setImeOptions ( EditorInfo . IME_ACTION_DONE ) ;
mAddButton . setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View v ) {
selectNumbers ( ) ;
}
} ) ;
mSend . setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View v ) {
2010-12-10 22:31:06 +01:00
showDialog ( SENDING_DIALOG_KEY ) ;
2010-12-12 02:28:33 +01:00
mThreadSender = new MessageSenderThread ( mHandler ) ;
mThreadSender . start ( ) ;
2010-12-10 22:31:06 +01:00
//sendMessage();
2009-10-22 14:26:02 +02:00
}
} ) ;
}
public void selectNumbers ( ) {
2010-12-10 22:31:06 +01:00
//startActivityForResult(new Intent(Intent.ACTION_PICK, People.CONTENT_URI), 0);
2009-10-22 14:26:02 +02:00
Intent i = new Intent ( this , PhoneNumberSelection . class ) ;
String rawNumbers = mContacts . getText ( ) . toString ( ) ;
String [ ] numbers = rawNumbers . split ( " , " ) ;
i . putExtra ( PARAM_NUMBERS_LIST , numbers ) ;
startActivityForResult ( i , ACTIVITY_EDIT ) ;
}
2010-12-10 22:31:06 +01:00
private class MessageSenderThread extends Thread {
Handler mHandler ;
public MessageSenderThread ( Handler h ) {
mHandler = h ;
}
2010-12-12 02:28:33 +01:00
public synchronized void run ( ) {
super . run ( ) ;
2010-12-10 22:31:06 +01:00
sendMessage ( mHandler ) ;
}
}
2010-12-12 02:28:33 +01:00
public void sendMessage ( Handler handler ) {
2010-05-06 14:05:03 +02:00
MySMSManager manager = new MySMSManager ( ) ;
2009-10-22 14:26:02 +02:00
String message = mEditor . getText ( ) . toString ( ) ;
2010-12-12 03:06:07 +01:00
mManyMessageContinue = true ;
2010-12-10 22:31:06 +01:00
2010-01-27 13:47:08 +01:00
if ( " " . equals ( message ) ) {
2010-12-12 02:28:33 +01:00
{
Message msg = handler . obtainMessage ( ) ;
Bundle b = new Bundle ( ) ;
b . putInt ( " ORIGIN " , DIALOG_PROGRESS_CANCEL ) ;
msg . setData ( b ) ;
handler . sendMessage ( msg ) ;
}
2010-01-27 13:47:08 +01:00
return ;
}
2010-12-10 22:31:06 +01:00
2009-10-22 14:26:02 +02:00
String [ ] numbers = mContacts . getText ( ) . toString ( ) . split ( " , " ) ;
HashSet < String > allreadySend = new HashSet < String > ( ) ;
int size = numbers . length ;
2010-01-22 15:21:33 +01:00
boolean haveDeliveryReports = mDeliveryCheckBox . isChecked ( ) ;
long deliveryId = - 1 ;
ArrayList < String > messages = manager . divideMessage ( message ) ;
int messageCount = messages . size ( ) ;
2010-01-26 18:28:38 +01:00
2010-01-22 15:21:33 +01:00
if ( haveDeliveryReports ) {
deliveryId = mDbHelper . createDelivery ( message . substring ( 0 , Math . min ( 30 , message
. length ( ) ) ) . replace ( '\n' , ' ' ) , DateFormat . getDateInstance ( )
. format ( new Date ( ) ) ) ;
}
2010-12-12 02:28:33 +01:00
if ( size ! = 0 ) {
if ( size > MANY_MESSAGE ) {
{
2010-12-10 22:31:06 +01:00
Message msg = handler . obtainMessage ( ) ;
Bundle b = new Bundle ( ) ;
2010-12-12 02:28:33 +01:00
b . putInt ( " ORIGIN " , DIALOG_MANYMESSAGE ) ;
2010-12-10 22:31:06 +01:00
msg . setData ( b ) ;
handler . sendMessage ( msg ) ;
2010-12-12 02:28:33 +01:00
}
synchronized ( MultiSmsSender . this ) {
try {
MultiSmsSender . this . wait ( ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
2010-01-22 15:21:33 +01:00
2010-01-26 18:28:38 +01:00
2010-12-12 02:28:33 +01:00
}
2010-01-26 18:28:38 +01:00
2010-12-12 02:28:33 +01:00
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 ) ;
synchronized ( MultiSmsSender . this ) {
try {
MultiSmsSender . this . wait ( ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
2010-12-10 22:31:06 +01:00
2010-12-12 02:28:33 +01:00
}
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 ( ) ;
2010-01-26 18:28:38 +01:00
2010-12-12 02:28:33 +01:00
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 ) ;
2010-12-10 22:31:06 +01:00
2010-12-12 02:28:33 +01:00
if ( ! allreadySend . contains ( newN ) ) {
allreadySend . add ( newN ) ;
ArrayList < PendingIntent > deliveryIntents = new ArrayList < PendingIntent > ( messageCount ) ;
ArrayList < PendingIntent > sentIntents = null ;
2010-01-26 18:28:38 +01:00
2010-12-12 02:28:33 +01:00
if ( haveDeliveryReports ) {
ContentValues values = new ContentValues ( ) ;
values . put ( " address " , newN ) ;
values . put ( " body " , message ) ;
getContentResolver ( ) . insert ( Uri . parse ( " content://sms/sent " ) , values ) ;
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 ) ;
}
2010-01-22 15:21:33 +01:00
}
2009-10-22 14:26:02 +02:00
}
2010-12-12 02:28:33 +01:00
chunk_max = Math . min ( message_sent + MANY_MESSAGE , size ) ;
} while ( ( size - message_sent ) > 0 ) ;
2009-10-22 14:26:02 +02:00
}
2010-12-10 22:31:06 +01:00
Message msg = handler . obtainMessage ( ) ;
Bundle b = new Bundle ( ) ;
b . putInt ( " ORIGIN " , DIALOG_FINISHED ) ;
b . putInt ( " total " , allreadySend . size ( ) ) ;
msg . setData ( b ) ;
handler . sendMessage ( msg ) ;
2009-10-22 14:26:02 +02:00
2010-12-12 02:28:33 +01:00
2010-01-22 15:21:33 +01:00
} else {
2010-12-10 22:31:06 +01:00
Message msg = handler . obtainMessage ( ) ;
Bundle b = new Bundle ( ) ;
b . putInt ( " ORIGIN " , DIALOG_NONUMBER ) ;
msg . setData ( b ) ;
handler . sendMessage ( msg ) ;
2009-10-22 14:26:02 +02:00
}
}
@Override
protected Dialog onCreateDialog ( int id ) {
switch ( id ) {
case SENDING_DIALOG_KEY : {
mSendingDialog = new ProgressDialog ( this ) ;
mSendingDialog . setTitle ( R . string . sending ) ;
mSendingDialog . setMessage ( getResources ( ) . getString ( R . string . wait ) ) ;
2010-01-22 15:21:33 +01:00
mSendingDialog . setProgressStyle ( ProgressDialog . STYLE_HORIZONTAL ) ;
mSendingDialog . setCancelable ( false ) ;
2009-10-22 14:26:02 +02:00
return mSendingDialog ;
}
}
return null ;
}
@Override
protected void onActivityResult ( int requestCode , int resultCode ,
Intent intent ) {
super . onActivityResult ( requestCode , resultCode , intent ) ;
2010-01-22 15:21:33 +01:00
switch ( requestCode ) {
case ACTIVITY_ADD_GROUP :
case ACTIVITY_EDIT :
if ( intent ! = null ) {
String [ ] numbers = intent . getExtras ( ) . getStringArray (
PARAM_NUMBERS_LIST ) ;
boolean flush = intent . getExtras ( ) . getBoolean ( PARAM_FLUSH ) ;
String string = " " ;
HashSet < String > res = new HashSet < String > ( ) ;
for ( int i = 0 ; i < numbers . length ; i + + ) {
String newN = numbers [ i ] . trim ( ) ;
if ( ! newN . equals ( " " ) ) {
res . add ( newN ) ;
}
2009-10-22 14:26:02 +02:00
}
2010-01-22 15:21:33 +01:00
if ( ! flush ) {
String oldContactsString = mContacts . getText ( ) . toString ( ) ;
String [ ] oldContacts = oldContactsString . split ( " , " ) ;
for ( int i = 0 ; i < oldContacts . length ; i + + ) {
String newN = oldContacts [ i ] . trim ( ) ;
if ( ! newN . equals ( " " ) ) {
res . add ( newN ) ;
}
}
}
for ( String number : res ) {
string + = number + " , " ;
2009-10-22 14:26:02 +02:00
}
2010-01-22 15:21:33 +01:00
mContacts . setText ( string ) ;
2009-10-22 14:26:02 +02:00
}
2010-01-22 15:21:33 +01:00
break ;
2009-10-22 14:26:02 +02:00
2010-01-22 15:21:33 +01:00
default :
break ;
2009-10-22 14:26:02 +02:00
}
2010-01-22 15:21:33 +01:00
2009-10-22 14:26:02 +02:00
}
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
super . onCreateOptionsMenu ( menu ) ;
2010-12-10 22:31:06 +01:00
//menu.add(0, INSERT_ID, 0, R.string.add_group);
2010-01-22 15:21:33 +01:00
menu . add ( 0 , INSERT_ID + 1 , 0 , R . string . delivery ) ;
2009-10-22 14:26:02 +02:00
return true ;
}
@Override
public boolean onMenuItemSelected ( int featureId , MenuItem item ) {
2010-01-22 15:21:33 +01:00
switch ( item . getItemId ( ) ) {
2009-10-22 14:26:02 +02:00
case INSERT_ID :
display_group_list ( ) ;
return true ;
2010-01-22 15:21:33 +01:00
case INSERT_ID + 1 :
display_delivery_list ( ) ;
return true ;
}
2009-10-22 14:26:02 +02:00
return super . onMenuItemSelected ( featureId , item ) ;
}
2010-01-22 15:21:33 +01:00
protected void onDestroy ( ) {
mDbHelper . close ( ) ;
super . onDestroy ( ) ;
}
2009-10-22 14:26:02 +02:00
2010-01-22 15:21:33 +01:00
public void display_group_list ( ) {
2009-10-22 14:26:02 +02:00
Intent i = new Intent ( this , SelectGroupActivity . class ) ;
startActivityForResult ( i , ACTIVITY_ADD_GROUP ) ;
}
2010-01-22 15:21:33 +01:00
public void display_delivery_list ( ) {
Intent i = new Intent ( this , SelectDeliveryActivity . class ) ;
startActivityForResult ( i , ACTIVITY_DELIVERY ) ;
}
2009-10-22 14:26:02 +02:00
}