From 465b131420faee293a35c7d77952881cb7923060 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 10 Dec 2010 22:31:06 +0100 Subject: [PATCH] Contacts, Progress Dialog, No Group, Integration - Using Android 2.0 API for contacts lookup - Progress dialogue with progress bar when sending SMS - Desactive Group as they are not using Android2.0 API (Sync and so on...) - Every SMS sent are registrer in the sysem DB so they are available for the MMS App --- AndroidManifest.xml | 5 +- .../openwide/android/DeliveryDbAdapter.java | 10 +- src/com/openwide/android/MultiSmsSender.java | 135 +++++++++++++----- .../android/PhoneNumberSelection.java | 39 +++-- 4 files changed, 144 insertions(+), 45 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 42ad20f..8325ef3 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -21,9 +21,10 @@ - + - + + diff --git a/src/com/openwide/android/DeliveryDbAdapter.java b/src/com/openwide/android/DeliveryDbAdapter.java index 53d004b..80fec7e 100644 --- a/src/com/openwide/android/DeliveryDbAdapter.java +++ b/src/com/openwide/android/DeliveryDbAdapter.java @@ -7,7 +7,7 @@ import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri; -import android.provider.Contacts.Phones; +import android.provider.ContactsContract.PhoneLookup; import android.util.Log; public class DeliveryDbAdapter { @@ -237,12 +237,16 @@ public class DeliveryDbAdapter { public String nameFromNumber(String number) { - Uri contactUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number)); + Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); + Cursor c = mCtx.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null, null, null); + /*Uri contactUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number)); Cursor c = mCtx.getContentResolver().query(contactUri, new String[] { Phones.DISPLAY_NAME }, null, null, null); + */ + if(c != null) { c.moveToFirst(); if(c.isFirst()) { - return c.getString(c.getColumnIndex(Phones.DISPLAY_NAME)); + return c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)); }else { return ""; } diff --git a/src/com/openwide/android/MultiSmsSender.java b/src/com/openwide/android/MultiSmsSender.java index 3bd8ccc..112f132 100644 --- a/src/com/openwide/android/MultiSmsSender.java +++ b/src/com/openwide/android/MultiSmsSender.java @@ -10,10 +10,13 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.PendingIntent; import android.app.ProgressDialog; +import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.telephony.PhoneNumberUtils; import android.util.Log; import android.view.Menu; @@ -41,12 +44,62 @@ public class MultiSmsSender extends Activity { private static final int SENDING_DIALOG_KEY = 0; private static final int INSERT_ID = Menu.FIRST; + private static final int DIALOG_PROGRESS = 0; + private static final int DIALOG_FINISHED = 1; + private static final int DIALOG_NONUMBER = 2; + public static final String PARAM_NUMBERS_LIST = "param number list"; public static final String PARAM_FLUSH = "param flush"; public static final String PARAM_ENTRY_ID = "entry_id"; - + public static final String DEBUG_TAG="MultiSmsSender"; + 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; + + 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; + } + + 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(); + } + } + + } + }; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -74,13 +127,17 @@ public class MultiSmsSender extends Activity { mSend.setOnClickListener(new OnClickListener() { public void onClick(View v) { - sendMessage(); + showDialog(SENDING_DIALOG_KEY); + MessageSenderThread sender = new MessageSenderThread(mHandler); + sender.start(); + //sendMessage(); } }); } public void selectNumbers() { + //startActivityForResult(new Intent(Intent.ACTION_PICK, People.CONTENT_URI), 0); Intent i = new Intent(this, PhoneNumberSelection.class); String rawNumbers = mContacts.getText().toString(); String[] numbers = rawNumbers.split(","); @@ -88,14 +145,28 @@ public class MultiSmsSender extends Activity { startActivityForResult(i, ACTIVITY_EDIT); } - public void sendMessage() { + + + private class MessageSenderThread extends Thread{ + + Handler mHandler; + + public MessageSenderThread( Handler h) { + mHandler = h; + } + public void run() { + sendMessage(mHandler); + } + + } + public void sendMessage(Handler handler) { MySMSManager manager = new MySMSManager(); String message = mEditor.getText().toString(); - + if("".equals(message)) { return; } - + String[] numbers = mContacts.getText().toString().split(","); HashSet allreadySend = new HashSet(); int size = numbers.length; @@ -105,14 +176,14 @@ public class MultiSmsSender extends Activity { int messageCount = messages.size(); //showDialog(SENDING_DIALOG_KEY); - mSendingDialog = new ProgressDialog(this); + /*mSendingDialog = new ProgressDialog(this); mSendingDialog.setTitle(R.string.sending); mSendingDialog.setMessage(getResources().getString(R.string.wait)); mSendingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mSendingDialog.setIndeterminate(true); mSendingDialog.setCancelable(false); mSendingDialog.setMax(size); - mSendingDialog.show(); + mSendingDialog.show();*/ if (haveDeliveryReports) { deliveryId = mDbHelper.createDelivery(message.substring(0, Math.min(30, message @@ -126,7 +197,13 @@ public class MultiSmsSender extends Activity { if (!newN.equals("") && PhoneNumberUtils.isWellFormedSmsAddress(newN)) { - mSendingDialog.setProgress(i / size); + 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); + //mSendingDialog.setProgress( i/ size); if (!allreadySend.contains(newN)) { allreadySend.add(newN); @@ -134,9 +211,14 @@ public class MultiSmsSender extends Activity { ArrayList deliveryIntents = new ArrayList(messageCount); 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); + long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId); Log.d(DEBUG_TAG, "entry is "+entryId); for (int j = 0; j < messageCount; j++) { @@ -154,30 +236,20 @@ public class MultiSmsSender extends Activity { } } } - mSendingDialog.dismiss(); - new AlertDialog.Builder(this).setPositiveButton( - getResources().getString(R.string.ok), - new DialogInterface.OnClickListener() { + Message msg = handler.obtainMessage(); + Bundle b = new Bundle(); + b.putInt("ORIGIN", DIALOG_FINISHED); + b.putInt("total", allreadySend.size() ); + msg.setData(b); + handler.sendMessage(msg); - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - - } - }).setMessage( - allreadySend.size() + " " - + getResources().getString(R.string.message_sent)) - .show(); } else { - new AlertDialog.Builder(this).setPositiveButton( - getResources().getString(R.string.ok), - new DialogInterface.OnClickListener() { + Message msg = handler.obtainMessage(); + Bundle b = new Bundle(); + b.putInt("ORIGIN", DIALOG_NONUMBER); + msg.setData(b); + handler.sendMessage(msg); - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - - } - }).setMessage( - getResources().getString(R.string.enter_number)).show(); } } @@ -190,7 +262,6 @@ public class MultiSmsSender extends Activity { mSendingDialog.setTitle(R.string.sending); mSendingDialog.setMessage(getResources().getString(R.string.wait)); mSendingDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - mSendingDialog.setIndeterminate(true); mSendingDialog.setCancelable(false); return mSendingDialog; @@ -246,7 +317,7 @@ public class MultiSmsSender extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); - menu.add(0, INSERT_ID, 0, R.string.add_group); + //menu.add(0, INSERT_ID, 0, R.string.add_group); menu.add(0, INSERT_ID + 1, 0, R.string.delivery); return true; } diff --git a/src/com/openwide/android/PhoneNumberSelection.java b/src/com/openwide/android/PhoneNumberSelection.java index eee9404..b830ab5 100644 --- a/src/com/openwide/android/PhoneNumberSelection.java +++ b/src/com/openwide/android/PhoneNumberSelection.java @@ -7,12 +7,14 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; -import android.provider.Contacts.Phones; +import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.Data; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.view.ViewGroup; import android.view.View.OnClickListener; +import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.SimpleCursorAdapter; @@ -27,6 +29,7 @@ public class PhoneNumberSelection extends ListActivity private static final int SELECT_ALL_ID = Menu.FIRST + 1; private static final int DESELECT_ALL_ID = Menu.FIRST + 2; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -70,18 +73,30 @@ public class PhoneNumberSelection extends ListActivity } private void fillData() { - Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME); + + + Cursor c = getContentResolver().query(Data.CONTENT_URI, + new String[] {Data._ID, Data.MIMETYPE, Phone.NUMBER, Phone.TYPE, Phone.LABEL, Contacts.DISPLAY_NAME}, + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", + null, Contacts.DISPLAY_NAME); + startManagingCursor(c); + mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c, new String[] { + Contacts.DISPLAY_NAME, Phone.NUMBER + }, new int[] {R.id.name, R.id.phone}, mSelectedSet); + + /*Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME); startManagingCursor(c); mAdpater = new PhoneDataListAdapter(this, R.layout.number_row, c, new String[] { Phones.NAME, Phones.NUMBER - }, new int[] {R.id.name, R.id.phone}, mSelectedSet); + }, new int[] {R.id.name, R.id.phone}, mSelectedSet);*/ + setListAdapter(mAdpater); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); - menu.add(0, INSERT_ID,0, R.string.add_group); + //menu.add(0, INSERT_ID,0, R.string.add_group); menu.add(0, SELECT_ALL_ID, 0, R.string.select_all); menu.add(0, DESELECT_ALL_ID, 0, R.string.deselect_all); return true; @@ -94,8 +109,16 @@ public class PhoneNumberSelection extends ListActivity display_group_list(); return true; case SELECT_ALL_ID: - Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME); + + /*Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, Phones.NAME); int numberIdx = c.getColumnIndex(Phones.NUMBER); + */ + Cursor c = getContentResolver().query(Data.CONTENT_URI, + new String[] {Data._ID, Data.MIMETYPE, Phone.NUMBER, Phone.TYPE, Phone.LABEL, Contacts.DISPLAY_NAME}, + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", + null, null); + int numberIdx = c.getColumnIndex(Phone.NUMBER); + startManagingCursor(c); c.moveToFirst(); for (int i = 0; i < c.getCount(); i++) { mSelectedSet.add(c.getString(numberIdx)); @@ -144,8 +167,8 @@ public class PhoneNumberSelection extends ListActivity public PhoneDataListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, HashSet selected) { super(context, layout, c, from, to); - nameidx = c.getColumnIndex(Phones.NAME); - numberidx = c.getColumnIndex(Phones.NUMBER); + nameidx = c.getColumnIndex(Contacts.DISPLAY_NAME); + numberidx = c.getColumnIndex(Phone.NUMBER); mContext = context;