--- a/src/com/beem/project/beem/provider/UserProvider.java Fri May 22 19:17:50 2009 +0200
+++ b/src/com/beem/project/beem/provider/UserProvider.java Tue May 26 19:56:38 2009 +0200
@@ -11,176 +11,175 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
+import android.provider.BaseColumns;
import android.text.TextUtils;
public class UserProvider extends ContentProvider {
- private final static String TAG = "UserProvider";
+ private final static String TAG = "UserProvider";
- private static HashMap<String, String> sUsersProjectionMap;
+ private static HashMap<String, String> sUsersProjectionMap;
- private static final int USERS = 1;
- private static final int USER_ID = 2;
+ private static final int USERS = 1;
+ private static final int USER_ID = 2;
- private static final UriMatcher sUriMatcher;
- private BeemDatabaseHelper mOpenHelper;
+ private static final UriMatcher sUriMatcher;
+ static {
+ sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+ sUriMatcher.addURI(Beem.AUTHORITY, "users", USERS);
+ sUriMatcher.addURI(Beem.AUTHORITY, "users/#", USER_ID);
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = mOpenHelper.getWritableDatabase();
- int count;
+ sUsersProjectionMap = new HashMap<String, String>();
+ sUsersProjectionMap.put(BaseColumns._ID, BaseColumns._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);
+ }
- switch (sUriMatcher.match(uri)) {
- case USERS:
- count = db.delete(Beem.USERS_TABLE_NAME, selection, selectionArgs);
- break;
+ private BeemDatabaseHelper mOpenHelper;
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ SQLiteDatabase db = mOpenHelper.getWritableDatabase();
+ int count;
- 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;
+ switch (sUriMatcher.match(uri)) {
+ case USERS:
+ count = db.delete(Beem.USERS_TABLE_NAME, selection, selectionArgs);
+ break;
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
+ case USER_ID:
+ String userID = uri.getPathSegments().get(1);
+ count = db.delete(Beem.USERS_TABLE_NAME, BaseColumns._ID + "=" + userID
+ + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
+ break;
- getContext().getContentResolver().notifyChange(uri, null);
- return count;
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
}
- @Override
- public String getType(Uri uri) {
- switch (sUriMatcher.match(uri)) {
- case USERS:
- return Beem.Users.CONTENT_TYPE;
+ 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);
+ }
+ }
- case USER_ID:
- return Beem.Users.CONTENT_ITEM_TYPE;
+ @Override
+ public Uri insert(Uri uri, ContentValues initialValues) {
+ // Validate the requested uri
+ if (sUriMatcher.match(uri) != USERS) {
+ throw new IllegalArgumentException("Unknown URI " + uri);
+ }
- default:
- 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);
}
- @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);
+ if (values.containsKey(Beem.Users.DATE_CREATED) == false) {
+ values.put(Beem.Users.DATE_CREATED, now);
}
- @Override
- public boolean onCreate() {
- mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.USERS_TABLE_NAME, Beem.Users.QUERY_CREATE);
- return true;
+ 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, BaseColumns._ID, values);
+ if (rowId > 0) {
+ Uri userUri = ContentUris.withAppendedId(Beem.Users.CONTENT_URI, rowId);
+ getContext().getContentResolver().notifyChange(userUri, null);
+ return userUri;
}
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
+ throw new SQLException("Failed to insert row into " + uri);
+ }
- switch (sUriMatcher.match(uri)) {
- case USERS:
- qb.setTables(Beem.USERS_TABLE_NAME);
- qb.setProjectionMap(sUsersProjectionMap);
- break;
+ @Override
+ public boolean onCreate() {
+ mOpenHelper = new BeemDatabaseHelper(getContext(), TAG, Beem.USERS_TABLE_NAME, Beem.Users.QUERY_CREATE);
+ return true;
+ }
- case USER_ID:
- qb.setTables(Beem.USERS_TABLE_NAME);
- qb.setProjectionMap(sUsersProjectionMap);
- qb.appendWhere(Beem.Users._ID + "=" + uri.getPathSegments().get(1));
- break;
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
+ SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
- default:
- throw new IllegalArgumentException("Unknown URI " + uri);
- }
+ switch (sUriMatcher.match(uri)) {
+ case USERS:
+ qb.setTables(Beem.USERS_TABLE_NAME);
+ qb.setProjectionMap(sUsersProjectionMap);
+ break;
- // If no sort order is specified use the default
- String orderBy;
- if (TextUtils.isEmpty(sortOrder)) {
- orderBy = Beem.Users.DEFAULT_SORT_ORDER;
- } else {
- orderBy = sortOrder;
- }
+ case USER_ID:
+ qb.setTables(Beem.USERS_TABLE_NAME);
+ qb.setProjectionMap(sUsersProjectionMap);
+ qb.appendWhere(BaseColumns._ID + "=" + uri.getPathSegments().get(1));
+ break;
- // 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;
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
}
- @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;
+ // If no sort order is specified use the default
+ String orderBy;
+ if (TextUtils.isEmpty(sortOrder)) {
+ orderBy = Beem.Users.DEFAULT_SORT_ORDER;
+ } else {
+ orderBy = sortOrder;
}
- static {
- sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- sUriMatcher.addURI(Beem.AUTHORITY, "users", USERS);
- sUriMatcher.addURI(Beem.AUTHORITY, "users/#", USER_ID);
+ // 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;
- sUsersProjectionMap = new HashMap<String, String>();
- 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);
+ 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, BaseColumns._ID + "=" + userId
+ + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
+ break;
+
+ default:
+ throw new IllegalArgumentException("Unknown URI " + uri);
}
+
+ getContext().getContentResolver().notifyChange(uri, null);
+ return count;
+ }
}