From 8fb32529158a9edc08c5e33d3d1603c3a05972ef Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 22 Jan 2010 15:21:33 +0100 Subject: [PATCH] First test for delivery messages --- AndroidManifest.xml | 9 +- res/layout/delivery_list.xml | 12 + res/layout/delivery_row.xml | 11 + res/layout/entry_list.xml | 12 + res/layout/entry_row.xml | 14 ++ res/layout/group_list.xml | 2 +- res/layout/main.xml | 14 +- res/values-en/strings.xml | 6 + res/values-fr/strings.xml | 11 +- src/com/openwide/android/ContactRow.java | 14 +- .../openwide/android/DeliveryDbAdapter.java | 234 ++++++++++++++++++ .../openwide/android/GroupEditActivity.java | 5 + src/com/openwide/android/GroupsDbAdapter.java | 8 +- .../openwide/android/ListEntryActivity.java | 52 ++++ src/com/openwide/android/MessageReceiver.java | 29 +++ src/com/openwide/android/MultiSmsSender.java | 177 +++++++++---- .../android/PhoneNumberSelection.java | 34 ++- .../android/SelectDeliveryActivity.java | 103 ++++++++ .../openwide/android/SelectGroupActivity.java | 6 + 19 files changed, 670 insertions(+), 83 deletions(-) create mode 100644 res/layout/delivery_list.xml create mode 100644 res/layout/delivery_row.xml create mode 100644 res/layout/entry_list.xml create mode 100644 res/layout/entry_row.xml create mode 100644 src/com/openwide/android/DeliveryDbAdapter.java create mode 100644 src/com/openwide/android/ListEntryActivity.java create mode 100644 src/com/openwide/android/MessageReceiver.java create mode 100644 src/com/openwide/android/SelectDeliveryActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index e0e0818..779d405 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,7 +1,7 @@ + package="com.openwide.android" android:versionCode="4" + android:versionName="1.0.2"> @@ -12,10 +12,13 @@ - + + + + diff --git a/res/layout/delivery_list.xml b/res/layout/delivery_list.xml new file mode 100644 index 0000000..df21540 --- /dev/null +++ b/res/layout/delivery_list.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/res/layout/delivery_row.xml b/res/layout/delivery_row.xml new file mode 100644 index 0000000..3bb7d0c --- /dev/null +++ b/res/layout/delivery_row.xml @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/res/layout/entry_list.xml b/res/layout/entry_list.xml new file mode 100644 index 0000000..df21540 --- /dev/null +++ b/res/layout/entry_list.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/res/layout/entry_row.xml b/res/layout/entry_row.xml new file mode 100644 index 0000000..a3765eb --- /dev/null +++ b/res/layout/entry_row.xml @@ -0,0 +1,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/res/layout/group_list.xml b/res/layout/group_list.xml index dc317e5..035fd06 100644 --- a/res/layout/group_list.xml +++ b/res/layout/group_list.xml @@ -2,7 +2,7 @@ - - + - + - + - + - + + + + diff --git a/res/values-en/strings.xml b/res/values-en/strings.xml index d8515d3..deb6d5e 100644 --- a/res/values-en/strings.xml +++ b/res/values-en/strings.xml @@ -11,8 +11,14 @@ Please wait while sending... Enter your message Enter Group Name + No delivery reports available + Enable delivery reports To + Refresh + Remove All + Remove Add from group + Delivery reports Create a new group Remove this group Edit this group diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index 235ae42..2a04031 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -7,12 +7,17 @@ Ok Pas de contacts!! Pas de groupes!! - Envoie d'un message... - Veuillez patienter pendant l'envoie... + Envoie d\'un message... + Veuillez patienter pendant l\'envoie... Saisissez votre message Nom du groupe À + Rafraichir + Tout supprimer + Supprimer Ajouter un groupe + Accusé de récéption + Accusé de récéption Créer un group Supprimer ce groupe Editer ce groupe @@ -20,5 +25,5 @@ Selectionner tout DeSelectionner tout Message(s) envoyé(s) - Entrez un numero ou utilisez l'icone de contact + Entrez un numero ou utilisez l\'icone de contact diff --git a/src/com/openwide/android/ContactRow.java b/src/com/openwide/android/ContactRow.java index 34e9696..c43b81f 100644 --- a/src/com/openwide/android/ContactRow.java +++ b/src/com/openwide/android/ContactRow.java @@ -1,6 +1,7 @@ package com.openwide.android; import android.content.Context; +import android.view.View; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; @@ -27,6 +28,7 @@ public class ContactRow extends LinearLayout { mSelectedCheckBox.setText(""); + mSelectedCheckBox.setFocusable(false); this.addView(mSelectedCheckBox, new LayoutParams(LayoutParams.WRAP_CONTENT, @@ -40,7 +42,14 @@ public class ContactRow extends LinearLayout { this.addView(mInfoLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT)); - + this.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + mSelectedCheckBox.performClick(); + + } + }); } @@ -55,7 +64,8 @@ public class ContactRow extends LinearLayout { public void display(String name, String number, boolean isSelected, long id) { display( name, number, isSelected); mId = id; + } - + } diff --git a/src/com/openwide/android/DeliveryDbAdapter.java b/src/com/openwide/android/DeliveryDbAdapter.java new file mode 100644 index 0000000..7de924b --- /dev/null +++ b/src/com/openwide/android/DeliveryDbAdapter.java @@ -0,0 +1,234 @@ +package com.openwide.android; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +public class DeliveryDbAdapter { + + + public static final String KEY_DELIVERY_ENTRY_ROWID = "_id"; + public static final String KEY_DELIVERY_ENTRY_NAME = "name"; + public static final String KEY_DELIVERY_ENTRY_NUMBER = "number"; + public static final String KEY_DELIVERY_ENTRY_DELIVERED = "delivered"; + public static final String KEY_DELIVERY_ENTRY_DELIVERY_ID = "delivery_id"; + + + public static final String KEY_DELIVERY_ROWID = "_id"; + public static final String KEY_DELIVERY_NAME = "name"; + public static final String KEY_DELIVERY_DATE = "date"; + + private static final String TAG = "deliveryDbAdapter"; + private DeliveryDbHelper mDbHelper; + private SQLiteDatabase mDb; + + /** + * Database creation sql statement + */ + + private static final String DATABASE_NAME = "data"; + private static final String DATABASE_DELIVERY_ENTRY_TABLE = "delivery_entry"; + private static final String DATABASE_DELIVERY_TABLE = "delivery"; + private static final String DATABASE_DELIVERY_ENTRY_CREATE = "create table " + + DATABASE_DELIVERY_ENTRY_TABLE + + " ("+KEY_DELIVERY_ENTRY_ROWID+" integer primary key autoincrement, " + + KEY_DELIVERY_ENTRY_NAME + " text not null," + + KEY_DELIVERY_ENTRY_NUMBER + " text not null," + + KEY_DELIVERY_ENTRY_DELIVERED + " integer," + + KEY_DELIVERY_ENTRY_DELIVERY_ID + " integer);"; + + private static final String DATABASE_DELIVERY_CREATE = "create table " + + DATABASE_DELIVERY_TABLE + + " (" + KEY_DELIVERY_ROWID + " integer primary key autoincrement, " + + KEY_DELIVERY_NAME + " text not null," + + KEY_DELIVERY_DATE + " text not null);"; + + private static final int DATABASE_VERSION = 2; + + private final Context mCtx; + + private static class DeliveryDbHelper extends SQLiteOpenHelper { + + DeliveryDbHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + + } + + @Override + public void onCreate(SQLiteDatabase db) { + + db.execSQL(DATABASE_DELIVERY_ENTRY_CREATE); + db.execSQL(DATABASE_DELIVERY_CREATE); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + + newVersion + ", which will destroy all old data"); + db.execSQL("DROP TABLE IF EXISTS " + DATABASE_DELIVERY_ENTRY_TABLE); + db.execSQL("DROP TABLE IF EXISTS " + DATABASE_DELIVERY_TABLE); + onCreate(db); + } + } + + /** + * Constructor - takes the context to allow the database to be + * opened/created + * + * @param ctx + * the Context within which to work + */ + public DeliveryDbAdapter(Context ctx) { + this.mCtx = ctx; + } + + + /** + * Open the groups database. If it cannot be opened, try to create a new + * instance of the database. If it cannot be created, throw an exception to + * signal the failure + * + * @return this (self reference, allowing this to be chained in an + * initialization call) + * @throws SQLException if the database could be neither opened or created + */ + public DeliveryDbAdapter open() throws SQLException { + mDbHelper = new DeliveryDbHelper(mCtx); + mDb = mDbHelper.getWritableDatabase(); + return this; + } + + public void close() { + mDbHelper.close(); + } + + // ********************************* ENTRY FUNCTION *********************** + + /** + * Create a new entry using the name provided. If the entry is + * successfully created return the new rowId for that entry, otherwise return + * a -1 to indicate failure. + * + * @param name the name of the entry + * @param date the date of the entry + * @param deliveryID the deliveryID of the entry + */ + public long createEntry(String name, String number, long deliveryID) { + ContentValues initialValues = new ContentValues(); + initialValues.put(KEY_DELIVERY_ENTRY_NAME, name); + initialValues.put(KEY_DELIVERY_ENTRY_NUMBER, number); + initialValues.put(KEY_DELIVERY_ENTRY_DELIVERY_ID, deliveryID); + initialValues.put(KEY_DELIVERY_ENTRY_DELIVERED, 0); + + return mDb.insert(DATABASE_DELIVERY_ENTRY_TABLE, null, initialValues); + } + + /** + * Delete the entry with the given rowId + * + * @param rowId id of entry to delete + * @return true if deleted, false otherwise + */ + public boolean deleteEntry(long rowId) { + + return mDb.delete(DATABASE_DELIVERY_ENTRY_TABLE, KEY_DELIVERY_ENTRY_ROWID + "=" + rowId, null) > 0 ; + + } + + /** + * Delete all the entries where the delivery_id is the given deliveryId + * + * @param deliveryId id of the delivery + * @return true if deleted, false otherwise + */ + public boolean deleteAllEntry(long deliveryId) { + return mDb.delete(DATABASE_DELIVERY_ENTRY_TABLE, KEY_DELIVERY_ENTRY_DELIVERY_ID + "=" + deliveryId, null) > 0 ; + } + + /** + * Return a Cursor over the list of all entry in the database associated with the given delivery_id + * + * @param deliveryId id of the entry + * @return Cursor over all entries + */ + public Cursor fetchAllEntry(long deliveryId) { + return mDb.query(DATABASE_DELIVERY_ENTRY_TABLE, new String[] {KEY_DELIVERY_ENTRY_ROWID, KEY_DELIVERY_ENTRY_NAME, KEY_DELIVERY_ENTRY_NUMBER, KEY_DELIVERY_ENTRY_DELIVERED}, KEY_DELIVERY_ENTRY_DELIVERY_ID + "=" + deliveryId, null, null, null , KEY_DELIVERY_ENTRY_NAME); + } + + /** + * Return a Cursor over the list of all entry in the database associated with the given delivery_id + * + * @param mDeliveryId id of the entry + * @return Cursor over all entries + */ + public Cursor fetchEntry(long entryId) { + Cursor cursor = + + mDb.query(true, DATABASE_DELIVERY_ENTRY_TABLE, new String[] {KEY_DELIVERY_ENTRY_ROWID, KEY_DELIVERY_ENTRY_NAME, KEY_DELIVERY_ENTRY_NUMBER, KEY_DELIVERY_ENTRY_DELIVERED}, KEY_DELIVERY_ENTRY_DELIVERY_ID + "=" + entryId, null, + null, null, null, null); + if (cursor != null) { + cursor.moveToFirst(); + } + return cursor; + + } + + public boolean setEntryDelivered(long entryId) { + ContentValues content = new ContentValues(1); + content.put(KEY_DELIVERY_ENTRY_DELIVERED, 1); + return mDb.update(DATABASE_DELIVERY_ENTRY_TABLE, content, KEY_DELIVERY_ENTRY_DELIVERY_ID +"="+entryId , null) > 0; + } + + + + // ************************* DELIVERY ************************************* + + + + /** + * Create a new delivery using the name provided. If the delivery is + * successfully created return the new rowId for that delivery, otherwise return + * a -1 to indicate failure. + * + * @param name the name of the delivery + * @param date the date of the delivery + */ + public long createDelivery(String name, String date) { + ContentValues initialValues = new ContentValues(); + initialValues.put(KEY_DELIVERY_NAME, name); + initialValues.put(KEY_DELIVERY_DATE, date); + + return mDb.insert(DATABASE_DELIVERY_TABLE, null, initialValues); + } + + /** + * Delete the delivery with the given rowId and all entry associated + * + * @param rowId id of entry to delete + * @return true if deleted, false otherwise + */ + public boolean deleteDelivery(long rowId) { + + return mDb.delete(DATABASE_DELIVERY_TABLE, KEY_DELIVERY_ROWID + "=" + rowId, null) > 0 && deleteAllEntry(rowId) ; + + } + + /** + * Return a Cursor over the list of all deliveries in the database + * + * @param mDeliveryId id of the delivery + * @return Cursor over all delivery + */ + public Cursor fetchAllDeliveries() { + return mDb.query(DATABASE_DELIVERY_TABLE, new String[] {KEY_DELIVERY_ROWID, KEY_DELIVERY_NAME, KEY_DELIVERY_DATE}, null, null, null, null , KEY_DELIVERY_DATE); + } + + + + + +} diff --git a/src/com/openwide/android/GroupEditActivity.java b/src/com/openwide/android/GroupEditActivity.java index 2a39b7b..837b91d 100644 --- a/src/com/openwide/android/GroupEditActivity.java +++ b/src/com/openwide/android/GroupEditActivity.java @@ -78,6 +78,11 @@ public class GroupEditActivity extends ListActivity { }); } + protected void onDestroy() { + mDb.close(); + super.onDestroy(); + } + private void createGroup() { if (mGid == null) { String name = mGroupNameText.getText().toString(); diff --git a/src/com/openwide/android/GroupsDbAdapter.java b/src/com/openwide/android/GroupsDbAdapter.java index ba3bbab..af3bcde 100755 --- a/src/com/openwide/android/GroupsDbAdapter.java +++ b/src/com/openwide/android/GroupsDbAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Google Inc. + * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of @@ -27,13 +27,9 @@ import android.util.Log; /** * Simple groups database access helper class. Defines the basic CRUD operations - * for the grouppad example, and gives the ability to list all groups as well as + * for the group add example, and gives the ability to list all groups as well as * retrieve or modify a specific group. * - * This has been improved from the first version of this tutorial through the - * addition of better error handling and also using returning a Cursor instead - * of using a collection of inner classes (which is less scalable and not - * recommended). */ public class GroupsDbAdapter { diff --git a/src/com/openwide/android/ListEntryActivity.java b/src/com/openwide/android/ListEntryActivity.java new file mode 100644 index 0000000..8259390 --- /dev/null +++ b/src/com/openwide/android/ListEntryActivity.java @@ -0,0 +1,52 @@ +package com.openwide.android; + +import android.app.ListActivity; +import android.database.Cursor; +import android.os.Bundle; +import android.widget.SimpleCursorAdapter; + +public class ListEntryActivity extends ListActivity { + DeliveryDbAdapter mDbHelper; + Long mDeliveryId; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.entry_list); + Bundle extras = getIntent().getExtras(); + mDeliveryId = extras != null ? extras.getLong(SelectDeliveryActivity.PARAM_DELIVERY_ID): null; + + mDbHelper = new DeliveryDbAdapter(this); + mDbHelper.open(); + fillData(); + registerForContextMenu(getListView()); + + + } + + public void fillData() { + Cursor deliveryCursor = mDbHelper.fetchAllEntry(mDeliveryId); + + startManagingCursor(deliveryCursor); + + String[] from = new String[]{DeliveryDbAdapter.KEY_DELIVERY_ENTRY_NAME, DeliveryDbAdapter.KEY_DELIVERY_ENTRY_NUMBER, DeliveryDbAdapter.KEY_DELIVERY_ENTRY_DELIVERED }; + + int[] to = new int[]{R.id.name, R.id.number, R.id.delivered}; + + SimpleCursorAdapter notes = + new SimpleCursorAdapter(this, R.layout.entry_row, deliveryCursor, from, to); + setListAdapter(notes); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + } + + + @Override + protected void onDestroy() { + mDbHelper.close(); + super.onDestroy(); + } + +} diff --git a/src/com/openwide/android/MessageReceiver.java b/src/com/openwide/android/MessageReceiver.java new file mode 100644 index 0000000..fe8220e --- /dev/null +++ b/src/com/openwide/android/MessageReceiver.java @@ -0,0 +1,29 @@ +package com.openwide.android; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +public class MessageReceiver extends BroadcastReceiver{ + public static final String MESSAGE_RECEIVED = "com.openwide.android.mutliSmsSend.message_received"; + + @Override + public void onReceive(Context context, Intent intent) { + if (MESSAGE_RECEIVED.equals(intent.getAction())) { + Long entryId; + Bundle extras = intent.getExtras(); + entryId = extras != null ? extras.getLong(MultiSmsSender.PARAM_ENTRY_ID): null; + DeliveryDbAdapter mDbHelper = new DeliveryDbAdapter(context); + mDbHelper.open(); + mDbHelper.setEntryDelivered(entryId); + mDbHelper.close(); + } + } + + + + + + +} diff --git a/src/com/openwide/android/MultiSmsSender.java b/src/com/openwide/android/MultiSmsSender.java index 0fa3642..b2080f9 100644 --- a/src/com/openwide/android/MultiSmsSender.java +++ b/src/com/openwide/android/MultiSmsSender.java @@ -1,11 +1,16 @@ package com.openwide.android; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Date; import java.util.HashSet; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; +import android.app.PendingIntent; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; @@ -17,6 +22,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo; import android.widget.Button; +import android.widget.CheckBox; import android.widget.TextView; public class MultiSmsSender extends Activity { @@ -24,25 +30,34 @@ public class MultiSmsSender extends Activity { private Button mSend; private TextView mContacts; private TextView mEditor; + private CheckBox mDeliveryCheckBox; + private DeliveryDbAdapter mDbHelper; private ProgressDialog mSendingDialog; public static final int ACTIVITY_EDIT = 0; public static final int ACTIVITY_ADD_GROUP = 1; + public static final int ACTIVITY_DELIVERY = 2; private static final int SENDING_DIALOG_KEY = 0; private static final int INSERT_ID = Menu.FIRST; 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"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); + mDbHelper = new DeliveryDbAdapter(this); + mDbHelper.open(); + mAddButton = (Button) findViewById(R.id.contacts); mSend = (Button) findViewById(R.id.send); mContacts = (TextView) findViewById(R.id.numbers); mEditor = (TextView) findViewById(R.id.editor); + mDeliveryCheckBox = (CheckBox) findViewById(R.id.deliveryCheckBox); mContacts.setImeOptions(EditorInfo.IME_ACTION_NEXT); mEditor.setImeOptions(EditorInfo.IME_ACTION_DONE); @@ -79,41 +94,82 @@ public class MultiSmsSender extends Activity { String[] numbers = mContacts.getText().toString().split(","); HashSet allreadySend = new HashSet(); int size = numbers.length; - showDialog(SENDING_DIALOG_KEY); - if(numbers.length != 0) { + boolean haveDeliveryReports = mDeliveryCheckBox.isChecked(); + long deliveryId = -1; + ArrayList messages = manager.divideMessage(message); + int messageCount = messages.size(); + + //showDialog(SENDING_DIALOG_KEY); + 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(); + + if (haveDeliveryReports) { + deliveryId = mDbHelper.createDelivery(message.substring(0, Math.min(30, message + .length())).replace('\n', ' '), DateFormat.getDateInstance() + .format(new Date())); + + } + if (numbers.length != 0) { for (int i = 0; i < numbers.length; i++) { String newN = numbers[i].trim(); - mSendingDialog.setMax(size); - if (!newN.equals("") && PhoneNumberUtils.isWellFormedSmsAddress(newN)) { + + if (!newN.equals("") + && PhoneNumberUtils.isWellFormedSmsAddress(newN)) { mSendingDialog.setProgress(i / size); - + if (!allreadySend.contains(newN)) { allreadySend.add(newN); - manager.sendMultipartTextMessage(newN, null, manager.divideMessage(message), null, null); + + ArrayList sentIntents = null; + if (haveDeliveryReports) { + + sentIntents = new ArrayList(messageCount); + long entryId = mDbHelper.createEntry("", newN, deliveryId); + + for (int j = 0; j < messageCount; j++) { + Intent intent = new Intent(MessageReceiver.MESSAGE_RECEIVED, null, this, MessageReceiver.class); + intent.putExtra(PARAM_ENTRY_ID, entryId); + sentIntents.add(PendingIntent.getBroadcast( + this, 0, intent , 0)); + } + } + manager.sendMultipartTextMessage(newN, null, messages, null, sentIntents); } } } mSendingDialog.dismiss(); - new AlertDialog.Builder(this).setPositiveButton(getResources().getString(R.string.ok), + new AlertDialog.Builder(this).setPositiveButton( + getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); + @Override + 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), + } + }).setMessage( + allreadySend.size() + " " + + getResources().getString(R.string.message_sent)) + .show(); + } else { + new AlertDialog.Builder(this).setPositiveButton( + getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); - } - }).setMessage(getResources().getString(R.string.enter_number)).show(); + } + }).setMessage( + getResources().getString(R.string.enter_number)).show(); } } @@ -125,8 +181,10 @@ public class MultiSmsSender extends Activity { 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(true); + mSendingDialog.setCancelable(false); + return mSendingDialog; } } @@ -137,55 +195,84 @@ public class MultiSmsSender extends Activity { protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); - if (intent != null) { - String[] numbers = intent.getExtras().getStringArray( - PARAM_NUMBERS_LIST); - String string = ""; - HashSet res = new HashSet(); - for (int i = 0; i < numbers.length; i++) { - String newN = numbers[i].trim(); - if (!newN.equals("")) { - res.add(newN); + 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 res = new HashSet(); + for (int i = 0; i < numbers.length; i++) { + String newN = numbers[i].trim(); + if (!newN.equals("")) { + res.add(newN); + } } - } - 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); + 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 + ", "; - } + for (String number : res) { + string += number + ", "; + } - mContacts.setText(string); + mContacts.setText(string); + } + break; + + default: + break; } + } @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; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { - switch(item.getItemId()) { + switch (item.getItemId()) { case INSERT_ID: display_group_list(); return true; - } + case INSERT_ID + 1: + display_delivery_list(); + return true; + } return super.onMenuItemSelected(featureId, item); } + protected void onDestroy() { + mDbHelper.close(); + super.onDestroy(); + } - public void display_group_list(){ + public void display_group_list() { Intent i = new Intent(this, SelectGroupActivity.class); startActivityForResult(i, ACTIVITY_ADD_GROUP); } + + public void display_delivery_list() { + + Intent i = new Intent(this, SelectDeliveryActivity.class); + startActivityForResult(i, ACTIVITY_DELIVERY); + } + + + } \ No newline at end of file diff --git a/src/com/openwide/android/PhoneNumberSelection.java b/src/com/openwide/android/PhoneNumberSelection.java index 2730b93..d3b20f0 100644 --- a/src/com/openwide/android/PhoneNumberSelection.java +++ b/src/com/openwide/android/PhoneNumberSelection.java @@ -15,14 +15,13 @@ import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; -import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class PhoneNumberSelection extends ListActivity { PhoneDataListAdapter mAdpater; - String[] mSelected; + HashSet mSelectedSet; private static final int INSERT_ID = Menu.FIRST; private static final int SELECT_ALL_ID = Menu.FIRST + 1; @@ -34,17 +33,17 @@ public class PhoneNumberSelection extends ListActivity setContentView(R.layout.number_list); + String[] selected; + selected = savedInstanceState != null ? savedInstanceState.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null; - mSelected = savedInstanceState != null ? savedInstanceState.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null; - - if (mSelected == null) { + if (selected == null) { Bundle extras = getIntent().getExtras(); - mSelected = extras != null ? extras.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null; + selected = extras != null ? extras.getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST ) : null; } mSelectedSet = new HashSet(); - for (int i = 0; i < mSelected.length; i++) { - mSelectedSet.add(mSelected[i].trim()); + for (int i = 0; i < selected.length; i++) { + mSelectedSet.add(selected[i].trim()); } fillData(); @@ -54,14 +53,16 @@ public class PhoneNumberSelection extends ListActivity @Override public void onClick(View v) { Intent i = new Intent(); - String [] numbers = new String[mAdpater.selected.size()]; - mAdpater.selected.toArray(numbers); + String [] numbers = new String[mSelectedSet.size()]; + mSelectedSet.toArray(numbers); Bundle bundle = new Bundle(); bundle.putStringArray(MultiSmsSender.PARAM_NUMBERS_LIST, numbers); + bundle.putBoolean(MultiSmsSender.PARAM_FLUSH, true); i.putExtras(bundle); setResult(RESULT_OK, i); finish(); + } }); @@ -118,11 +119,11 @@ public class PhoneNumberSelection extends ListActivity startActivityForResult(i, MultiSmsSender.ACTIVITY_ADD_GROUP); } - @Override + /*@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); l.getItemAtPosition(position); - } + }*/ /*@Override protected void onSaveInstanceState(Bundle outState) { @@ -137,7 +138,6 @@ public class PhoneNumberSelection extends ListActivity public class PhoneDataListAdapter extends SimpleCursorAdapter{ - public HashSet selected; int nameidx; int numberidx; Context mContext; @@ -148,7 +148,7 @@ public class PhoneNumberSelection extends ListActivity nameidx = c.getColumnIndex(Phones.NAME); numberidx = c.getColumnIndex(Phones.NUMBER); mContext = context; - this.selected = selected; + } @@ -163,7 +163,7 @@ public class PhoneNumberSelection extends ListActivity }else { contact = (ContactRow) convertView; } - contact.display(name, number, selected.contains(number)); + contact.display(name, number, mSelectedSet.contains(number)); contact.mSelectedCheckBox.setOnClickListener(new addNumberToSelectedClickListener(contact)); return contact; @@ -196,12 +196,10 @@ public class PhoneNumberSelection extends ListActivity if(intent != null) { String [] numbers = intent.getExtras().getStringArray(MultiSmsSender.PARAM_NUMBERS_LIST); - HashSet mSelectedGlobal = new HashSet(); for (int i = 0; i < numbers.length; i++) { mSelectedSet.add(numbers[i]); } - mSelected = new String[mSelectedSet.size()]; - mSelectedGlobal.toArray(mSelected); + fillData(); } } diff --git a/src/com/openwide/android/SelectDeliveryActivity.java b/src/com/openwide/android/SelectDeliveryActivity.java new file mode 100644 index 0000000..a9b573e --- /dev/null +++ b/src/com/openwide/android/SelectDeliveryActivity.java @@ -0,0 +1,103 @@ +package com.openwide.android; + +import android.app.ListActivity; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; +import android.view.ContextMenu; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ContextMenu.ContextMenuInfo; +import android.widget.ListView; +import android.widget.SimpleCursorAdapter; +import android.widget.AdapterView.AdapterContextMenuInfo; + +public class SelectDeliveryActivity extends ListActivity { + + + DeliveryDbAdapter mDbHelper; + public static final int DELETE_ID = Menu.FIRST; + public static final int DELETE_ALL_ID = Menu.FIRST + 1; + public static final int REFRESH_ID = Menu.FIRST + 2; + + public static final String PARAM_DELIVERY_ID = "param_delivery_id"; + + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.delivery_list); + mDbHelper = new DeliveryDbAdapter(this); + mDbHelper.open(); + fillData(); + registerForContextMenu(getListView()); + } + + + protected void onDestroy() { + mDbHelper.close(); + super.onDestroy(); + } + + public void fillData() { + Cursor deliveryCursor = mDbHelper.fetchAllDeliveries(); + + startManagingCursor(deliveryCursor); + + String[] from = new String[]{DeliveryDbAdapter.KEY_DELIVERY_DATE, DeliveryDbAdapter.KEY_DELIVERY_NAME }; + + int[] to = new int[]{R.id.date, R.id.name}; + + SimpleCursorAdapter notes = + new SimpleCursorAdapter(this, R.layout.delivery_row, deliveryCursor, from, to); + setListAdapter(notes); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + menu.add(0, DELETE_ALL_ID,0, R.string.remove_all); + return true; + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + menu.add(0, DELETE_ID, 0, R.string.remove); + + } + + + @Override + public boolean onContextItemSelected(MenuItem item) { + switch(item.getItemId()) { + case DELETE_ID: + AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + mDbHelper.deleteDelivery(info.id); + fillData(); + return true; + } + return super.onContextItemSelected(item); + } + + @Override + public boolean onMenuItemSelected(int featureId, MenuItem item) { + switch(item.getItemId()) { + case DELETE_ALL_ID: + //TODO + return true; + } + return super.onMenuItemSelected(featureId, item); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + Intent i = new Intent(this, ListEntryActivity.class); + i.putExtra(PARAM_DELIVERY_ID, id); + startActivity(i); + super.onListItemClick(l, v, position, id); + } +} diff --git a/src/com/openwide/android/SelectGroupActivity.java b/src/com/openwide/android/SelectGroupActivity.java index bce8cb8..4dfd000 100644 --- a/src/com/openwide/android/SelectGroupActivity.java +++ b/src/com/openwide/android/SelectGroupActivity.java @@ -37,6 +37,12 @@ public class SelectGroupActivity extends ListActivity{ fillData(); registerForContextMenu(getListView()); } + + + protected void onDestroy() { + mDbHelper.close(); + super.onDestroy(); + }