Use Content Provider for delivery reports. This should fix issue when trying to access locked database

This commit is contained in:
Mathieu Maret 2012-11-05 18:05:48 +01:00
parent 2205f64ce5
commit 95bb2d1651
7 changed files with 359 additions and 321 deletions

View File

@ -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="2.2" android:versionCode="12"> package="com.hectorone.multismssender" android:versionName="2.3" android:versionCode="13">
<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>
@ -16,10 +16,10 @@
<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="vnd.android.cursor.item/vnd.com.hectorone.multismssender.provider.entry"/>
</intent-filter> </intent-filter>
</receiver> </receiver>
<provider android:authorities="com.hectorone.multismssender" android:name="EntryContentProvider"></provider> <provider android:authorities="com.hectorone.multismssender.provider" android:name=".DeliveryDbAdapter"></provider>
</application> </application>
<uses-sdk android:minSdkVersion="5" /> <uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

View File

@ -1,57 +1,99 @@
package com.hectorone.multismssender; package com.hectorone.multismssender;
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup; import android.provider.UserDictionary.Words;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
public class DeliveryDbAdapter { public class DeliveryDbAdapter extends ContentProvider {
public static final String KEY_DELIVERY_ENTRY_ROWID = "_id"; public static final String PROVIDER_NAME ="com.hectorone.multismssender.provider";
public static final String KEY_DELIVERY_ENTRY_NAME = "name"; public static final Uri CONTENT_DELIVERY_URI = Uri.parse("content://" + PROVIDER_NAME + "/delivery");
public static final String KEY_DELIVERY_ENTRY_NUMBER = "number"; public static final Uri CONTENT_MESSAGE_URI = Uri.parse("content://" + PROVIDER_NAME + "/message");
public static final String KEY_DELIVERY_ENTRY_DELIVERED = "delivered";
public static final String KEY_DELIVERY_ENTRY_DELIVERY_ID = "delivery_id"; private static final int ENTRIES = 1;
private static final int ENTRY_ID = 2;
private static final int MESSAGES = 3;
private static final int MESSAGES_ID = 4;
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "delivery" , ENTRIES);
uriMatcher.addURI(PROVIDER_NAME, "delivery/#", ENTRY_ID);
uriMatcher.addURI(PROVIDER_NAME, "message" , MESSAGES);
uriMatcher.addURI(PROVIDER_NAME, "message/#" , MESSAGES_ID);
}
public static final String KEY_DELIVERY_ROWID = "_id"; public static final String KEY_DELIVERY_ENTRY_ROWID = "_id";
public static final String KEY_DELIVERY_NAME = "name"; public static final String KEY_DELIVERY_ENTRY_NAME = "name";
public static final String KEY_DELIVERY_DATE = "date"; 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_MESSAGE_ID = "message_id";
public static final String KEY_MESSAGE_ROWID = "_id";
public static final String KEY_MESSAGE_NAME = "name";
public static final String KEY_MESSAGE_DATE = "date";
private static final String TAG = "deliveryDbAdapter"; private static final String TAG = "deliveryDbAdapter";
private DeliveryDbHelper mDbHelper; private DeliveryDbHelper mDbHelper;
private SQLiteDatabase mDb; //private SQLiteDatabase mDb;
/** /**
* Database creation sql statement * Database creation sql statement
*/ */
public static final String DATABASE_NAME = "data"; public static final String DATABASE_NAME = "delivery";
public static final String DATABASE_DELIVERY_ENTRY_TABLE = "delivery_entry"; public static final String DATABASE_DELIVERY_ENTRY_TABLE = "delivery_entry";
public static final String DATABASE_DELIVERY_TABLE = "delivery"; public static final String DATABASE_MESSAGE_TABLE = "message";
public static final String DATABASE_DELIVERY_ENTRY_CREATE = "create table " public static final String DATABASE_DELIVERY_ENTRY_CREATE = "create table "
+ DATABASE_DELIVERY_ENTRY_TABLE + DATABASE_DELIVERY_ENTRY_TABLE
+ " ("+KEY_DELIVERY_ENTRY_ROWID+" integer primary key autoincrement, " + " ("+KEY_DELIVERY_ENTRY_ROWID+" integer primary key autoincrement, "
+ KEY_DELIVERY_ENTRY_NAME + " text not null," + KEY_DELIVERY_ENTRY_NAME + " text not null,"
+ KEY_DELIVERY_ENTRY_NUMBER + " text not null," + KEY_DELIVERY_ENTRY_NUMBER + " text not null,"
+ KEY_DELIVERY_ENTRY_DELIVERED + " integer," + KEY_DELIVERY_ENTRY_DELIVERED + " integer,"
+ KEY_DELIVERY_ENTRY_DELIVERY_ID + " integer);"; + KEY_DELIVERY_ENTRY_MESSAGE_ID + " integer);";
public static final String DATABASE_DELIVERY_CREATE = "create table " public static final String DATABASE_DELIVERY_CREATE = "create table "
+ DATABASE_DELIVERY_TABLE + DATABASE_MESSAGE_TABLE
+ " (" + KEY_DELIVERY_ROWID + " integer primary key autoincrement, " + " (" + KEY_MESSAGE_ROWID + " integer primary key autoincrement, "
+ KEY_DELIVERY_NAME + " text not null," + KEY_MESSAGE_NAME + " text not null,"
+ KEY_DELIVERY_DATE + " text not null);"; + KEY_MESSAGE_DATE + " text not null);";
public static final int DATABASE_VERSION = 3; public static final int DATABASE_VERSION = 4;
private static HashMap<String, String> sDeliveryProjectionMap;
private static HashMap<String, String> sMessageProjectionMap;
static {
sDeliveryProjectionMap = new HashMap<String, String>();
sMessageProjectionMap = new HashMap<String, String>();
sDeliveryProjectionMap.put(KEY_DELIVERY_ENTRY_ROWID , KEY_DELIVERY_ENTRY_ROWID);
sDeliveryProjectionMap.put(KEY_DELIVERY_ENTRY_NAME , KEY_DELIVERY_ENTRY_NAME);
sDeliveryProjectionMap.put(KEY_DELIVERY_ENTRY_NUMBER , KEY_DELIVERY_ENTRY_NUMBER);
sDeliveryProjectionMap.put(KEY_DELIVERY_ENTRY_DELIVERED , KEY_DELIVERY_ENTRY_DELIVERED);
sDeliveryProjectionMap.put(KEY_DELIVERY_ENTRY_MESSAGE_ID, KEY_DELIVERY_ENTRY_MESSAGE_ID);
sMessageProjectionMap.put(KEY_MESSAGE_ROWID , KEY_MESSAGE_ROWID);
sMessageProjectionMap.put(KEY_MESSAGE_NAME , KEY_MESSAGE_NAME);
sMessageProjectionMap.put(KEY_MESSAGE_DATE , KEY_MESSAGE_DATE);
}
private final Context mCtx;
private static class DeliveryDbHelper extends SQLiteOpenHelper { private static class DeliveryDbHelper extends SQLiteOpenHelper {
@ -72,196 +114,221 @@ public class DeliveryDbAdapter {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data"); + 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_ENTRY_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_DELIVERY_TABLE); db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MESSAGE_TABLE);
onCreate(db); 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;
}
/** // *********************** Content Provider ****************************************
* 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; @Override
} public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
int count;
public void close() { switch (uriMatcher.match(uri)) {
mDb.close(); case ENTRIES:
mDbHelper.close(); count = db.delete(DATABASE_DELIVERY_ENTRY_TABLE, selection, selectionArgs);
} break;
case ENTRY_ID:
{
String id = uri.getPathSegments().get(1);
count = db.delete(DATABASE_DELIVERY_ENTRY_TABLE, KEY_DELIVERY_ENTRY_ROWID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
// ********************************* ENTRY FUNCTION ***********************
/** break;
* 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; case MESSAGES:
count = db.delete(DATABASE_MESSAGE_TABLE, selection, selectionArgs);
} break;
case MESSAGES_ID:
{
String id = uri.getPathSegments().get(1);
count = db.delete(DATABASE_DELIVERY_ENTRY_TABLE, KEY_MESSAGE_ROWID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
public boolean setEntryDelivered(long entryId) { break;
ContentValues content = new ContentValues(); }
content.put(KEY_DELIVERY_ENTRY_DELIVERED, 1); default:
return mDb.update(DATABASE_DELIVERY_ENTRY_TABLE, content, KEY_DELIVERY_ENTRY_ROWID +"="+entryId , null) > 0; throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
} }
// ************************* DELIVERY ************************************* @Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case ENTRIES:
/** return "vnd.android.cursor.dir/vnd." + PROVIDER_NAME + ".entry";
* Create a new delivery using the name provided. If the delivery is case ENTRY_ID:
* successfully created return the new rowId for that delivery, otherwise return return "vnd.android.cursor.item/vnd."+ PROVIDER_NAME + ".entry";
* a -1 to indicate failure. case MESSAGES:
* return "vnd.android.cursor.dir/vnd." + PROVIDER_NAME + ".message";
* @param name the name of the delivery case MESSAGES_ID:
* @param date the date of the delivery return "vnd.android.cursor.item/vnd."+ PROVIDER_NAME + ".message";
*/ default:
public long createDelivery(String name, String date) { throw new IllegalArgumentException("Unsupported URI: " + uri);
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) ; @Override
public Uri insert(Uri uri, ContentValues values) {
} ContentValues initialValues;
if (values != null) {
public void deleteAllDeliveries() { initialValues = new ContentValues(values);
mDb.delete(DATABASE_DELIVERY_TABLE, null, null); } else {
mDb.delete(DATABASE_DELIVERY_ENTRY_TABLE, null, null) ; initialValues = new ContentValues();
}
/**
* 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);
}
// *********************** HELPER ****************************************
public String nameFromNumber(String number) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = null;
try {
c = mCtx.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null, null, null);
} catch (Exception e) {
return "";
} }
/*Uri contactUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number)); switch (uriMatcher.match(uri)) {
Cursor c = mCtx.getContentResolver().query(contactUri, new String[] { Phones.DISPLAY_NAME }, null, null, null); case ENTRIES:{
*/ if (initialValues.containsKey(KEY_DELIVERY_ENTRY_NAME) == false) {
throw new SQLException(KEY_DELIVERY_ENTRY_NAME+ " must be specified");
}
if (initialValues.containsKey(KEY_DELIVERY_ENTRY_NUMBER) == false) {
throw new SQLException(KEY_DELIVERY_ENTRY_NUMBER+ " must be specified");
}
if (initialValues.containsKey(KEY_DELIVERY_ENTRY_MESSAGE_ID) == false) {
throw new SQLException(KEY_DELIVERY_ENTRY_MESSAGE_ID+ " must be specified");
}
initialValues.put(KEY_DELIVERY_ENTRY_DELIVERED, 0);
if(c != null) { SQLiteDatabase db = mDbHelper.getWritableDatabase();
c.moveToFirst(); long rowId = db.insert(DATABASE_DELIVERY_ENTRY_TABLE, null, initialValues);
if(c.isFirst()) { if (rowId > 0) {
return c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME)); Uri newUri = ContentUris.withAppendedId( CONTENT_DELIVERY_URI, rowId);
}else { getContext().getContentResolver().notifyChange(newUri, null);
return ""; return newUri;
} }
} throw new SQLException("Failed to insert row into " + uri);
return "";
}
}
case MESSAGES:
{
if (initialValues.containsKey(KEY_MESSAGE_NAME) == false) {
throw new SQLException(KEY_MESSAGE_NAME+ " must be specified");
}
if (initialValues.containsKey(KEY_MESSAGE_DATE) == false) {
throw new SQLException(KEY_MESSAGE_DATE+ " must be specified");
}
SQLiteDatabase db = mDbHelper.getWritableDatabase();
long rowId = db.insert(DATABASE_MESSAGE_TABLE,null, initialValues);
if (rowId > 0) {
Uri newUri = ContentUris.withAppendedId( CONTENT_MESSAGE_URI, rowId);
getContext().getContentResolver().notifyChange(newUri, null);
return newUri;
}
throw new SQLException("Failed to insert row into " + uri);
}
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public boolean onCreate() {
mDbHelper = new DeliveryDbHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
switch (uriMatcher.match(uri)) {
case ENTRIES:
qb.setTables(DATABASE_DELIVERY_ENTRY_TABLE);
qb.setProjectionMap(sDeliveryProjectionMap);
break;
case ENTRY_ID:
qb.setTables(DATABASE_DELIVERY_ENTRY_TABLE);
qb.setProjectionMap(sDeliveryProjectionMap);
qb.appendWhere(KEY_DELIVERY_ENTRY_ROWID + "=" + uri.getPathSegments().get(1));
break;
case MESSAGES:
qb.setTables(DATABASE_MESSAGE_TABLE);
qb.setProjectionMap(sMessageProjectionMap);
break;
case MESSAGES_ID:
qb.setTables(DATABASE_MESSAGE_TABLE);
qb.setProjectionMap(sMessageProjectionMap);
qb.appendWhere(KEY_MESSAGE_ROWID + "=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
//Run the query
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
// Tell the cursor what uri to watch, so it knows when its source data changes
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
int count;
switch (uriMatcher.match(uri)) {
case ENTRIES:
count = db.update(DATABASE_DELIVERY_ENTRY_TABLE, values, selection, selectionArgs);
break;
case ENTRY_ID:
{
String id = uri.getPathSegments().get(1);
count = db.update(DATABASE_DELIVERY_ENTRY_TABLE, values, Words._ID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
}
case MESSAGES:
count = db.update(DATABASE_MESSAGE_TABLE, values, selection, selectionArgs);
break;
case MESSAGES_ID:
{
String id = uri.getPathSegments().get(1);
count = db.update(DATABASE_MESSAGE_TABLE, values, Words._ID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
break;
}
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
} }

View File

@ -1,78 +0,0 @@
package com.hectorone.multismssender;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
/**
*
* This class is not very usefull... It have been created so we can set Uri for deliveryEntry in the database.
* Thanks to this, sending a SMS (function sendMultipartTextMessage in MultiSmsSender) with a PendingIntent for delivery reports will use different Intent (Intent i = new Intent(message, uri))
* so PendingIntent.getBroadcast() will return different PendingIntent
*
* @author mathieu
*
*/
public class EntryContentProvider extends ContentProvider{
public static final String PROVIDER_NAME="com.hectorone.multismssender";
public static final Uri CONTENT_URI =
Uri.parse("content://"+PROVIDER_NAME+ "/entries");
private static final int ENTRIES = 1;
private static final int ENTRY_ID = 2;
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "entries", ENTRIES);
uriMatcher.addURI(PROVIDER_NAME, "entries/#", ENTRY_ID);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case ENTRIES:
return "com.hectorone.multismssender.dir/entry ";
case ENTRY_ID:
return "com.hectorone.multismssender.item/entry ";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}

View File

@ -23,8 +23,8 @@ public class ListEntryActivity extends ListActivity {
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
mDeliveryId = extras != null ? extras.getLong(SelectDeliveryActivity.PARAM_DELIVERY_ID): null; mDeliveryId = extras != null ? extras.getLong(SelectDeliveryActivity.PARAM_DELIVERY_ID): null;
mDbHelper = new DeliveryDbAdapter(this); //mDbHelper = new DeliveryDbAdapter(this);
mDbHelper.open(); //mDbHelper.open();
fillData(); fillData();
registerForContextMenu(getListView()); registerForContextMenu(getListView());
@ -32,7 +32,9 @@ public class ListEntryActivity extends ListActivity {
} }
public void fillData() { public void fillData() {
Cursor deliveryCursor = mDbHelper.fetchAllEntry(mDeliveryId);
Cursor deliveryCursor = getContentResolver().query(DeliveryDbAdapter.CONTENT_DELIVERY_URI, null, DeliveryDbAdapter.KEY_DELIVERY_ENTRY_MESSAGE_ID+" = " + mDeliveryId, null, null);
//Cursor deliveryCursor = mDbHelper.fetchAllEntry(mDeliveryId);
startManagingCursor(deliveryCursor); startManagingCursor(deliveryCursor);
@ -53,7 +55,6 @@ public class ListEntryActivity extends ListActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
mDbHelper.close();
super.onDestroy(); super.onDestroy();
} }

View File

@ -1,6 +1,7 @@
package com.hectorone.multismssender; package com.hectorone.multismssender;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -17,23 +18,28 @@ public class MessageReceiver extends BroadcastReceiver{
if (MESSAGE_RECEIVED.equals(intent.getAction())) { if (MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(DEBUG_TAG, "SMS_RECEIVED"); Log.d(DEBUG_TAG, "SMS_RECEIVED");
Long entryId;
//Bundle extras = intent.getExtras();
//entryId = extras != null ? extras.getLong(MultiSmsSender.PARAM_ENTRY_ID): null;
Uri entryURI = intent.getData(); Uri entryURI = intent.getData();
String entryStr = entryURI.getLastPathSegment(); if (entryURI != null){
entryId = Long.parseLong(entryStr); ContentValues values = new ContentValues(1);
values.put(DeliveryDbAdapter.KEY_DELIVERY_ENTRY_DELIVERED, 1);
context.getContentResolver().update(entryURI, values, null, null);
}
/*String entryStr = entryURI.getLastPathSegment();
entryId = Long.parseLong(entryStr);*/
//byte[] pdu = (byte[]) intent.getByteArrayExtra("pdu"); //byte[] pdu = (byte[]) intent.getByteArrayExtra("pdu");
//SmsMessage message = SmsMessage.createFromPdu(pdu); //SmsMessage message = SmsMessage.createFromPdu(pdu);
//int status = message.getStatus(); //int status = message.getStatus();
if(entryId != null) {
/*if(entryId != null) {
DeliveryDbAdapter mDbHelper = new DeliveryDbAdapter(context); DeliveryDbAdapter mDbHelper = new DeliveryDbAdapter(context);
mDbHelper.open(); mDbHelper.open();
mDbHelper.setEntryDelivered(entryId); mDbHelper.setEntryDelivered(entryId);
mDbHelper.close(); mDbHelper.close();
} }*/
} }
} }

View File

@ -11,13 +11,16 @@ import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneNumberUtils; import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.view.Menu; import android.view.Menu;
@ -228,7 +231,7 @@ public class MultiSmsSender extends Activity {
} }
public void sendMessage(Handler handler) { public void sendMessage(Handler handler) {
DeliveryDbAdapter mDbHelper = new DeliveryDbAdapter(this); //DeliveryDbAdapter mDbHelper = new DeliveryDbAdapter(this);
SmsManager manager = SmsManager.getDefault(); SmsManager manager = SmsManager.getDefault();
String message = mEditor.getText().toString(); String message = mEditor.getText().toString();
@ -246,17 +249,23 @@ public class MultiSmsSender extends Activity {
ArrayList<String> phoneNumberConform = new ArrayList<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 messageId = -1;
ArrayList<String> messages = manager.divideMessage(message); ArrayList<String> messages = manager.divideMessage(message);
int messageCount = messages.size(); int messageCount = messages.size();
if (haveDeliveryReports) { if (haveDeliveryReports) {
ContentValues values = new ContentValues(2);
values.put(DeliveryDbAdapter.KEY_MESSAGE_NAME, message.substring(0, Math.min(30, message
.length())).replace('\n', ' '));
values.put(DeliveryDbAdapter.KEY_MESSAGE_DATE, DateFormat.getDateInstance().format(new Date()));
messageId = Long.parseLong(getContentResolver().insert(DeliveryDbAdapter.CONTENT_MESSAGE_URI, values).getPathSegments().get(1));
/*
mDbHelper.open(); mDbHelper.open();
deliveryId = mDbHelper.createDelivery(message.substring(0, Math.min(30, message deliveryId = mDbHelper.createMessage(message.substring(0, Math.min(30, message
.length())).replace('\n', ' '), DateFormat.getDateInstance() .length())).replace('\n', ' '), DateFormat.getDateInstance()
.format(new Date())); .format(new Date()));
mDbHelper.close(); mDbHelper.close();*/
} }
@ -270,10 +279,18 @@ public class MultiSmsSender extends Activity {
&& !phoneNumberConform.contains(newN)) { && !phoneNumberConform.contains(newN)) {
phoneNumberConform.add(newN); phoneNumberConform.add(newN);
if(haveDeliveryReports) { if(haveDeliveryReports) {
mDbHelper.open(); ContentValues values = new ContentValues(3);
long entryId = mDbHelper.createEntry(mDbHelper.nameFromNumber(newN), newN, deliveryId); values.put(DeliveryDbAdapter.KEY_DELIVERY_ENTRY_NAME, nameFromNumber(getContentResolver(), newN));
values.put(DeliveryDbAdapter.KEY_DELIVERY_ENTRY_NUMBER, newN);
values.put(DeliveryDbAdapter.KEY_DELIVERY_ENTRY_MESSAGE_ID, messageId);
long entryId = Long.parseLong(getContentResolver().insert(DeliveryDbAdapter.CONTENT_DELIVERY_URI, values).getPathSegments().get(1));
deliveryIdMap.put(newN,entryId); deliveryIdMap.put(newN,entryId);
mDbHelper.close(); /*
mDbHelper.open();
long entryId = mDbHelper.createEntry(nameFromNumber(getContentResolver(), newN), newN, messageId);
deliveryIdMap.put(newN,entryId);
mDbHelper.close();*/
} }
@ -332,11 +349,13 @@ public class MultiSmsSender extends Activity {
handler.sendMessage(msg); handler.sendMessage(msg);
ArrayList<PendingIntent> deliveryIntents = null; ArrayList<PendingIntent> deliveryIntents = null;
ArrayList<PendingIntent> sentIntents = null; ArrayList<PendingIntent> sentIntents = null;
if (haveDeliveryReports) { if (haveDeliveryReports) {
deliveryIntents = new ArrayList<PendingIntent>( deliveryIntents = new ArrayList<PendingIntent>(
messageCount); messageCount);
sentIntents = new ArrayList<PendingIntent>(
messageCount);
// Add to the Google MMS app // Add to the Google MMS app
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("address", newN); values.put("address", newN);
@ -349,15 +368,19 @@ public class MultiSmsSender extends Activity {
// Log.d(DEBUG_TAG, // Log.d(DEBUG_TAG,
// "entry is "+entryId+" to number"+newN); // "entry is "+entryId+" to number"+newN);
for (int j = 0; j < messageCount; j++) { for (int j = 0; j < messageCount; j++) {
Uri entryURI = Uri.withAppendedPath( if (j == (messageCount -1)){
EntryContentProvider.CONTENT_URI, "" Uri entryURI = Uri.withAppendedPath(
+ entryId); DeliveryDbAdapter.CONTENT_DELIVERY_URI, ""
Intent intent = new Intent( + entryId);
MessageReceiver.MESSAGE_RECEIVED, Intent intent = new Intent(
entryURI, this, MessageReceiver.class); MessageReceiver.MESSAGE_RECEIVED,
// intent.putExtra(PARAM_ENTRY_ID, entryId); entryURI, this, MessageReceiver.class);
deliveryIntents.add(PendingIntent.getBroadcast(
this, 0, intent, 0)); deliveryIntents.add(PendingIntent.getBroadcast(
this, 0, intent, 0));
}else{
deliveryIntents.add(null);
}
} }
} }
@ -496,6 +519,28 @@ public class MultiSmsSender extends Activity {
startActivityForResult(i, ACTIVITY_DELIVERY); startActivityForResult(i, ACTIVITY_DELIVERY);
} }
// *********************** HELPER ****************************************
public String nameFromNumber(ContentResolver resolver, String number) {
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor c = null;
try {
c = resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null, null, null);
} catch (Exception e) {
return "";
}
if(c != null) {
c.moveToFirst();
if(c.isFirst()) {
return c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}else {
return "";
}
}
return "";
}
} }

View File

@ -6,13 +6,13 @@ import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class SelectDeliveryActivity extends ListActivity { public class SelectDeliveryActivity extends ListActivity {
@ -29,24 +29,21 @@ public class SelectDeliveryActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.delivery_list); setContentView(R.layout.delivery_list);
mDbHelper = new DeliveryDbAdapter(this);
mDbHelper.open();
fillData(); fillData();
registerForContextMenu(getListView()); registerForContextMenu(getListView());
} }
protected void onDestroy() { protected void onDestroy() {
mDbHelper.close();
super.onDestroy(); super.onDestroy();
} }
public void fillData() { public void fillData() {
Cursor deliveryCursor = mDbHelper.fetchAllDeliveries(); Cursor deliveryCursor = getContentResolver().query(DeliveryDbAdapter.CONTENT_MESSAGE_URI, null, null, null, null);
startManagingCursor(deliveryCursor); startManagingCursor(deliveryCursor);
String[] from = new String[]{DeliveryDbAdapter.KEY_DELIVERY_DATE, DeliveryDbAdapter.KEY_DELIVERY_NAME }; String[] from = new String[]{DeliveryDbAdapter.KEY_MESSAGE_DATE, DeliveryDbAdapter.KEY_MESSAGE_NAME };
int[] to = new int[]{R.id.date, R.id.name}; int[] to = new int[]{R.id.date, R.id.name};
@ -77,7 +74,7 @@ public class SelectDeliveryActivity extends ListActivity {
switch(item.getItemId()) { switch(item.getItemId()) {
case DELETE_ID: case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteDelivery(info.id); getContentResolver().delete(DeliveryDbAdapter.CONTENT_MESSAGE_URI, DeliveryDbAdapter.KEY_MESSAGE_ROWID+ "="+ info.id, null);
fillData(); fillData();
return true; return true;
} }
@ -88,7 +85,7 @@ public class SelectDeliveryActivity extends ListActivity {
public boolean onMenuItemSelected(int featureId, MenuItem item) { public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) { switch(item.getItemId()) {
case DELETE_ALL_ID: case DELETE_ALL_ID:
mDbHelper.deleteAllDeliveries(); getContentResolver().delete(DeliveryDbAdapter.CONTENT_MESSAGE_URI, null, null);
fillData(); fillData();
return true; return true;
} }