# HG changeset patch # User dasilvj@jean-manuel-da-silvas-macbook.local # Date 1238225774 14400 # Node ID ff7b74becc50806d7e2434ce850b0fc23d5bfe52 # Parent c537c1ea363639a5012657e91af496f137eacfc1 Issue #60 Création d'un package pour les providers. Ecritures des providers pour les Utilisateurs de BEEM et leurs contacts. diff -r c537c1ea3636 -r ff7b74becc50 src/com/beem/project/beem/provider/Beem.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/provider/Beem.java Sat Mar 28 03:36:14 2009 -0400 @@ -0,0 +1,157 @@ +package com.beem.project.beem.provider; + +import android.net.Uri; +import android.provider.BaseColumns; + +/** + * Convenience definitions for BEEM's providers + */ +public final class Beem { + + public final static String AUTHORITY = "com.beem.project.provider"; + + public final static String DB_NAME = "beem.db"; + public final static int DB_VERSION = 2; + + public final static String USERS_TABLE_NAME = "users"; + public final static String CONTACTS_TABLE_NAME = "contacts"; + + /** + * Constructor + */ + private Beem() {} + + + /** + * Users table + */ + public static final class Users implements BaseColumns { + + /** + * The query used to create the table + */ + public final static String QUERY_CREATE = + "CREATE TABLE " + Beem.USERS_TABLE_NAME + " (" + + Users._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + + Users.JUSERNAME + " TEXT," + + Users.DATE_CREATED + " INTEGER," + + Users.DATE_MODIFIED + " INTEGER" + + ");"; + + /** + * The content:// style URL for Contacts table + */ + public final static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/users"); + + /** + * The MIME type of {@link #CONTENT_URI} providing a directory of users. + */ + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.beem.project.user"; + + /** + * The MIME type of a {@link #CONTENT_URI} sub-directory of a single user. + */ + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.beem.project.user"; + + /** + * The default sort order for this table + */ + public final static String DEFAULT_SORT_ORDER = "_id ASC"; + + /** + * The Jabber username of the user + *

Type: TEXT

+ */ + public final static String JUSERNAME = "username"; + + /** + * The timestamp for when the user was created + *

Type: INTEGER (long from System.curentTimeMillis())

+ */ + public final static String DATE_CREATED = "created"; + + /** + * The timestamp for when the user was last modified + *

Type: INTEGER (long from System.curentTimeMillis())

+ */ + public final static String DATE_MODIFIED = "modified"; + } + + + /** + * Contacts table + */ + public static final class Contacts implements BaseColumns { + + /** + * The query used to create the table + */ + public final static String QUERY_CREATE = + "CREATE TABLE " + Beem.CONTACTS_TABLE_NAME + " (" + + Contacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + + Contacts.UID + " INTEGER, " + + Contacts.JID + " INTEGER," + + Contacts.NICKNAME + " TEXT," + + Contacts.ALIAS + " TEXT," + + Contacts.DATE_CREATED + " INTEGER," + + Contacts.DATE_MODIFIED + " INTEGER" + + ");"; + + /** + * The content:// style URL for Contacts table + */ + public final static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/contacts"); + + /** + * The MIME type of {@link #CONTENT_URI} providing a directory of contacts. + */ + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.beem.project.contact"; + + /** + * The MIME type of a {@link #CONTENT_URI} sub-directory of a single contact. + */ + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.beem.project.contact"; + + /** + * The default sort order for this table + */ + public final static String DEFAULT_SORT_ORDER = "nickname ASC"; + + /** + * The user id having the contact + *

Type: INTEGER

+ */ + public final static String UID = "uid"; + + /** + * The JabberID of the contact + *

Type: INTEGER

+ */ + public final static String JID = "jid"; + + /** + * The nickname of the contact + *

Type: TEXT

+ */ + public final static String NICKNAME = "nickname"; + + /** + * The alias of the contact + *

Type: TEXT

+ */ + public final static String ALIAS = "alias"; + + /** + * The timestamp for when the contact was created + *

Type: INTEGER (long from System.curentTimeMillis())

+ */ + public final static String DATE_CREATED = "created"; + + /** + * The timestamp for when the contact was last modified + *

Type: INTEGER (long from System.curentTimeMillis())

+ */ + public final static String DATE_MODIFIED = "modified"; + + } +} diff -r c537c1ea3636 -r ff7b74becc50 src/com/beem/project/beem/provider/BeemDatabaseHelper.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/provider/BeemDatabaseHelper.java Sat Mar 28 03:36:14 2009 -0400 @@ -0,0 +1,34 @@ +package com.beem.project.beem.provider; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +public class BeemDatabaseHelper extends SQLiteOpenHelper { + + private String tag; + private String tableName; + private String creationQuery; + + public BeemDatabaseHelper(Context context, String tag, String tableName, String creationQuery) { + super(context, Beem.DB_NAME, null, Beem.DB_VERSION); + + this.tag = tag; + this.tableName = tableName; + this.creationQuery = creationQuery; + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL(this.creationQuery); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + Log.w(this.tag, "Upgrading database from version " + oldVersion + " to " + + newVersion + ", which will destroy all old data"); + db.execSQL("DROP TABLE IF EXISTS " + this.tableName + ";"); + onCreate(db); + } +} diff -r c537c1ea3636 -r ff7b74becc50 src/com/beem/project/beem/provider/ContactProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/provider/ContactProvider.java Sat Mar 28 03:36:14 2009 -0400 @@ -0,0 +1,210 @@ +/** + * + */ +package com.beem.project.beem.provider; + +import java.util.HashMap; + +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.content.res.Resources; +import android.database.Cursor; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.net.Uri; +import android.text.TextUtils; + +/** + * @author dasilvj + * + */ +public class ContactProvider extends ContentProvider { + + private final static String TAG = "ContactProvider"; + + private static HashMap sContactsProjectionMap; + + private static final int CONTACTS = 1; + private static final int CONTACT_ID = 2; + + private static final UriMatcher sUriMatcher; + private BeemDatabaseHelper mOpenHelper; + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + int count; + + switch (sUriMatcher.match(uri)) { + case CONTACTS: + count = db.delete(Beem.CONTACTS_TABLE_NAME, selection, selectionArgs); + break; + + case CONTACT_ID: + String contactId = uri.getPathSegments().get(1); + count = db.delete(Beem.CONTACTS_TABLE_NAME, Beem.Contacts._ID + "=" + contactId + + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + @Override + public String getType(Uri uri) { + switch (sUriMatcher.match(uri)) { + case CONTACTS: + return Beem.Contacts.CONTENT_TYPE; + + case CONTACT_ID: + return Beem.Contacts.CONTENT_ITEM_TYPE; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + } + + @Override + public Uri insert(Uri uri, ContentValues initialValues) { + // Validate the requested uri + if (sUriMatcher.match(uri) != CONTACTS) { + throw new IllegalArgumentException("Unknown URI " + uri); + } + + ContentValues values; + if (initialValues != null) { + values = new ContentValues(initialValues); + } else { + values = new ContentValues(); + } + + Long now = Long.valueOf(System.currentTimeMillis()); + + // Make sure that the fields are all set + if (values.containsKey(Beem.Contacts.UID) == false) { + // TODO :: Must check that the UID exists using UserProvider + throw new SQLException("No UID specified. Failed to insert row into " + uri); + } + + if (values.containsKey(Beem.Contacts.JID) == false) { + values.put(Beem.Contacts.JID, ""); + } + + if (values.containsKey(Beem.Contacts.NICKNAME) == false) { + values.put(Beem.Contacts.JID, ""); + } + + if (values.containsKey(Beem.Contacts.ALIAS) == false) { + values.put(Beem.Contacts.JID, ""); + } + + if (values.containsKey(Beem.Contacts.DATE_CREATED) == false) { + values.put(Beem.Contacts.DATE_CREATED, now); + } + + if (values.containsKey(Beem.Contacts.DATE_MODIFIED) == false) { + values.put(Beem.Contacts.DATE_MODIFIED, now); + } + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + long rowId = db.insert(Beem.CONTACTS_TABLE_NAME, Beem.Contacts._ID, values); + if (rowId > 0) { + Uri contactUri = ContentUris.withAppendedId(Beem.Contacts.CONTENT_URI, rowId); + getContext().getContentResolver().notifyChange(contactUri, null); + return contactUri; + } + + throw new SQLException("Failed to insert row into " + uri); + } + + @Override + public boolean onCreate() { + mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.CONTACTS_TABLE_NAME, Beem.Contacts.QUERY_CREATE); + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder) { + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + + switch (sUriMatcher.match(uri)) { + case CONTACTS: + qb.setTables(Beem.CONTACTS_TABLE_NAME); + qb.setProjectionMap(sContactsProjectionMap); + break; + + case CONTACT_ID: + qb.setTables(Beem.USERS_TABLE_NAME); + qb.setProjectionMap(sContactsProjectionMap); + qb.appendWhere(Beem.Contacts._ID + "=" + uri.getPathSegments().get(1)); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // If no sort order is specified use the default + String orderBy; + if (TextUtils.isEmpty(sortOrder)) { + orderBy = Beem.Contacts.DEFAULT_SORT_ORDER; + } else { + orderBy = sortOrder; + } + + // Get the database and run the query + SQLiteDatabase db = mOpenHelper.getReadableDatabase(); + Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); + + // 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 = mOpenHelper.getWritableDatabase(); + int count; + + switch (sUriMatcher.match(uri)) { + case CONTACTS: + count = db.update(Beem.CONTACTS_TABLE_NAME, values, selection, selectionArgs); + break; + + case CONTACT_ID: + String contactId = uri.getPathSegments().get(1); + count = db.update(Beem.CONTACTS_TABLE_NAME, values, Beem.Contacts._ID + "=" + contactId + + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + static { + sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); + sUriMatcher.addURI(Beem.AUTHORITY, "contacts", CONTACTS); + sUriMatcher.addURI(Beem.AUTHORITY, "contacts/#", CONTACT_ID); + + sContactsProjectionMap = new HashMap(); + sContactsProjectionMap.put(Beem.Contacts._ID, Beem.Contacts._ID); + sContactsProjectionMap.put(Beem.Contacts.UID, Beem.Contacts.UID); + sContactsProjectionMap.put(Beem.Contacts.JID, Beem.Contacts.JID); + sContactsProjectionMap.put(Beem.Contacts.NICKNAME, Beem.Contacts.NICKNAME); + sContactsProjectionMap.put(Beem.Contacts.ALIAS, Beem.Contacts.ALIAS); + sContactsProjectionMap.put(Beem.Contacts.DATE_CREATED, Beem.Contacts.DATE_CREATED); + sContactsProjectionMap.put(Beem.Contacts.DATE_MODIFIED, Beem.Contacts.DATE_MODIFIED); + } +} diff -r c537c1ea3636 -r ff7b74becc50 src/com/beem/project/beem/provider/UserProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/provider/UserProvider.java Sat Mar 28 03:36:14 2009 -0400 @@ -0,0 +1,186 @@ +package com.beem.project.beem.provider; + +import java.util.HashMap; + +import android.content.ContentProvider; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.net.Uri; +import android.text.TextUtils; + +public class UserProvider extends ContentProvider { + + private final static String TAG = "UserProvider"; + + private static HashMap sUsersProjectionMap; + + private static final int USERS = 1; + private static final int USER_ID = 2; + + private static final UriMatcher sUriMatcher; + private BeemDatabaseHelper mOpenHelper; + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + int count; + + switch (sUriMatcher.match(uri)) { + case USERS: + count = db.delete(Beem.USERS_TABLE_NAME, selection, selectionArgs); + break; + + case USER_ID: + String userID = uri.getPathSegments().get(1); + count = db.delete(Beem.USERS_TABLE_NAME, Beem.Users._ID + "=" + userID + + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + @Override + public String getType(Uri uri) { + switch (sUriMatcher.match(uri)) { + case USERS: + return Beem.Users.CONTENT_TYPE; + + case USER_ID: + return Beem.Users.CONTENT_ITEM_TYPE; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + } + + @Override + public Uri insert(Uri uri, ContentValues initialValues) { + // Validate the requested uri + if (sUriMatcher.match(uri) != USERS) { + throw new IllegalArgumentException("Unknown URI " + uri); + } + + ContentValues values; + if (initialValues != null) { + values = new ContentValues(initialValues); + } else { + values = new ContentValues(); + } + + Long now = Long.valueOf(System.currentTimeMillis()); + + // Make sure that the fields are all set + if (values.containsKey(Beem.Users.JUSERNAME) == false) { + throw new SQLException("No JUSERNAME specified. Failed to insert row into " + uri); + } + + if (values.containsKey(Beem.Users.DATE_CREATED) == false) { + values.put(Beem.Users.DATE_CREATED, now); + } + + if (values.containsKey(Beem.Users.DATE_MODIFIED) == false) { + values.put(Beem.Users.DATE_MODIFIED, now); + } + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + long rowId = db.insert(Beem.USERS_TABLE_NAME, Beem.Users._ID, values); + if (rowId > 0) { + Uri userUri = ContentUris.withAppendedId(Beem.Users.CONTENT_URI, rowId); + getContext().getContentResolver().notifyChange(userUri, null); + return userUri; + } + + throw new SQLException("Failed to insert row into " + uri); + } + + @Override + public boolean onCreate() { + mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.USERS_TABLE_NAME, Beem.Users.QUERY_CREATE); + return true; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder) { + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + + switch (sUriMatcher.match(uri)) { + case USERS: + qb.setTables(Beem.USERS_TABLE_NAME); + qb.setProjectionMap(sUsersProjectionMap); + break; + + case USER_ID: + qb.setTables(Beem.USERS_TABLE_NAME); + qb.setProjectionMap(sUsersProjectionMap); + qb.appendWhere(Beem.Users._ID + "=" + uri.getPathSegments().get(1)); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + // If no sort order is specified use the default + String orderBy; + if (TextUtils.isEmpty(sortOrder)) { + orderBy = Beem.Users.DEFAULT_SORT_ORDER; + } else { + orderBy = sortOrder; + } + + // Get the database and run the query + SQLiteDatabase db = mOpenHelper.getReadableDatabase(); + Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); + + // 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 = mOpenHelper.getWritableDatabase(); + int count; + + switch (sUriMatcher.match(uri)) { + case USERS: + count = db.update(Beem.USERS_TABLE_NAME, values, selection, selectionArgs); + break; + + case USER_ID: + String userId = uri.getPathSegments().get(1); + count = db.update(Beem.USERS_TABLE_NAME, values, Beem.Users._ID + "=" + userId + + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); + break; + + default: + throw new IllegalArgumentException("Unknown URI " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + return count; + } + + static { + sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); + sUriMatcher.addURI(Beem.AUTHORITY, "users", USERS); + sUriMatcher.addURI(Beem.AUTHORITY, "users/#", USER_ID); + + sUsersProjectionMap = new HashMap(); + sUsersProjectionMap.put(Beem.Users._ID, Beem.Users._ID); + sUsersProjectionMap.put(Beem.Users.JUSERNAME, Beem.Users.JUSERNAME); + sUsersProjectionMap.put(Beem.Users.DATE_CREATED, Beem.Users.DATE_CREATED); + sUsersProjectionMap.put(Beem.Users.DATE_MODIFIED, Beem.Users.DATE_MODIFIED); + } +}