Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Created April 3, 2014 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpotts18/9962274 to your computer and use it in GitHub Desktop.
Save jpotts18/9962274 to your computer and use it in GitHub Desktop.
package io.starterkit.droid.data.provider;
import io.starterkit.droid.data.database.StarterDatabase;
import io.starterkit.droid.data.database.table.*;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.content.ContentUris;
import android.database.sqlite.SQLiteQueryBuilder;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import java.util.ArrayList;
import java.util.List;
public class StarterProvider extends ContentProvider {
public static final String AUTHORITY = "io.starterkit.droid.data.provider";
public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
public static final Uri FRIEND_CONTENT_URI = Uri.withAppendedPath(StarterProvider.AUTHORITY_URI, FriendContent.CONTENT_PATH);
public static final Uri USER_CONTENT_URI = Uri.withAppendedPath(StarterProvider.AUTHORITY_URI, UserContent.CONTENT_PATH);
public static final Uri MESSAGE_CONTENT_URI = Uri.withAppendedPath(StarterProvider.AUTHORITY_URI, MessageContent.CONTENT_PATH);
public static final Uri USER_JOIN_MESSAGE_CONTENT_URI = Uri.withAppendedPath(StarterProvider.AUTHORITY_URI, UserJoinMessageContent.CONTENT_PATH);
private static final UriMatcher URI_MATCHER;
private StarterDatabase mDatabase;
private static final int FRIEND_DIR = 0;
private static final int FRIEND_ID = 1;
private static final int USER_DIR = 2;
private static final int USER_ID = 3;
private static final int MESSAGE_DIR = 4;
private static final int MESSAGE_ID = 5;
private static final int USER_JOIN_MESSAGE_DIR = 6;
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(AUTHORITY, FriendContent.CONTENT_PATH, FRIEND_DIR);
URI_MATCHER.addURI(AUTHORITY, FriendContent.CONTENT_PATH + "/#", FRIEND_ID);
URI_MATCHER.addURI(AUTHORITY, UserContent.CONTENT_PATH, USER_DIR);
URI_MATCHER.addURI(AUTHORITY, UserContent.CONTENT_PATH + "/#", USER_ID);
URI_MATCHER.addURI(AUTHORITY, MessageContent.CONTENT_PATH, MESSAGE_DIR);
URI_MATCHER.addURI(AUTHORITY, MessageContent.CONTENT_PATH + "/#", MESSAGE_ID);
URI_MATCHER.addURI(AUTHORITY, UserJoinMessageContent.CONTENT_PATH, USER_JOIN_MESSAGE_DIR);
}
public static final class FriendContent implements BaseColumns {
public static final String CONTENT_PATH = "friend";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.starter_database.friend";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.starter_database.friend";
}
public static final class UserContent implements BaseColumns {
public static final String CONTENT_PATH = "user";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.starter_database.user";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.starter_database.user";
}
public static final class MessageContent implements BaseColumns {
public static final String CONTENT_PATH = "message";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.starter_database.message";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.starter_database.message";
}
public static final class UserJoinMessageContent implements BaseColumns {
public static final String CONTENT_PATH = "user_join_message";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.starter_database.user_join_message";
}
@Override
public final boolean onCreate() {
mDatabase = new StarterDatabase(getContext());
return true;
}
@Override
public final String getType(final Uri uri) {
switch (URI_MATCHER.match(uri)) {
case FRIEND_DIR:
return FriendContent.CONTENT_TYPE;
case FRIEND_ID:
return FriendContent.CONTENT_ITEM_TYPE;
case USER_DIR:
return UserContent.CONTENT_TYPE;
case USER_ID:
return UserContent.CONTENT_ITEM_TYPE;
case MESSAGE_DIR:
return MessageContent.CONTENT_TYPE;
case MESSAGE_ID:
return MessageContent.CONTENT_ITEM_TYPE;
case USER_JOIN_MESSAGE_DIR:
return UserJoinMessageContent.CONTENT_TYPE;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public final Cursor query(final Uri uri, String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
final SQLiteDatabase dbConnection = mDatabase.getReadableDatabase();
switch (URI_MATCHER.match(uri)) {
case FRIEND_ID:
queryBuilder.appendWhere(FriendTable._ID + "=" + uri.getLastPathSegment());
case FRIEND_DIR:
queryBuilder.setTables(FriendTable.TABLE_NAME);
break;
case USER_ID:
queryBuilder.appendWhere(UserTable._ID + "=" + uri.getLastPathSegment());
case USER_DIR:
queryBuilder.setTables(UserTable.TABLE_NAME);
break;
case MESSAGE_ID:
queryBuilder.appendWhere(MessageTable._ID + "=" + uri.getLastPathSegment());
case MESSAGE_DIR:
queryBuilder.setTables(MessageTable.TABLE_NAME);
break;
case USER_JOIN_MESSAGE_DIR:
queryBuilder.setTables(UserTable.TABLE_NAME + " JOIN " + MessageTable.TABLE_NAME + " ON (" + UserTable.TABLE_NAME + "." + UserTable._ID + "=" + MessageTable.TABLE_NAME + "." + MessageTable.USER_ID + ")");
projection = new String[] {
//add left table columns
UserTable.TABLE_NAME + "._id AS " + UserTable.TABLE_NAME + "__id",
UserTable.TABLE_NAME + "." + UserTable.FIRST_NAME + " AS " + UserTable.TABLE_NAME + "_" + UserTable.FIRST_NAME,
UserTable.TABLE_NAME + "." + UserTable.LAST_NAME + " AS " + UserTable.TABLE_NAME + "_" + UserTable.LAST_NAME,
UserTable.TABLE_NAME + "." + UserTable.EMAIL + " AS " + UserTable.TABLE_NAME + "_" + UserTable.EMAIL,
UserTable.TABLE_NAME + "." + UserTable.PASSWORD + " AS " + UserTable.TABLE_NAME + "_" + UserTable.PASSWORD,
UserTable.TABLE_NAME + "." + UserTable.USERNAME + " AS " + UserTable.TABLE_NAME + "_" + UserTable.USERNAME,
UserTable.TABLE_NAME + "." + UserTable.AVATAR_URL + " AS " + UserTable.TABLE_NAME + "_" + UserTable.AVATAR_URL,
UserTable.TABLE_NAME + "." + UserTable.API_KEY + " AS " + UserTable.TABLE_NAME + "_" + UserTable.API_KEY,
MessageTable.TABLE_NAME + "._id AS " + MessageTable.TABLE_NAME + "__id",
MessageTable.TABLE_NAME + "." + MessageTable.BODY + " AS " + MessageTable.TABLE_NAME + "_" + MessageTable.BODY,
MessageTable.TABLE_NAME + "." + MessageTable.LIKES + " AS " + MessageTable.TABLE_NAME + "_" + MessageTable.LIKES,
MessageTable.TABLE_NAME + "." + MessageTable.CREATED_AT + " AS " + MessageTable.TABLE_NAME + "_" + MessageTable.CREATED_AT,
};
default :
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
Cursor cursor = queryBuilder.query(dbConnection, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public final Uri insert(final Uri uri, final ContentValues values) {
final SQLiteDatabase dbConnection = mDatabase.getWritableDatabase();
try {
dbConnection.beginTransaction();
switch (URI_MATCHER.match(uri)) {
case FRIEND_DIR:
case FRIEND_ID:
final long friendId = dbConnection.insertOrThrow(FriendTable.TABLE_NAME, null, values);
final Uri newFriendUri = ContentUris.withAppendedId(FRIEND_CONTENT_URI, friendId);
getContext().getContentResolver().notifyChange(newFriendUri, null);
dbConnection.setTransactionSuccessful();
return newFriendUri;
case USER_DIR:
case USER_ID:
final long userId = dbConnection.insertOrThrow(UserTable.TABLE_NAME, null, values);
final Uri newUserUri = ContentUris.withAppendedId(USER_CONTENT_URI, userId);
getContext().getContentResolver().notifyChange(newUserUri, null);
getContext().getContentResolver().notifyChange(USER_JOIN_MESSAGE_CONTENT_URI, null);
dbConnection.setTransactionSuccessful();
return newUserUri;
case MESSAGE_DIR:
case MESSAGE_ID:
final long messageId = dbConnection.insertOrThrow(MessageTable.TABLE_NAME, null, values);
final Uri newMessageUri = ContentUris.withAppendedId(MESSAGE_CONTENT_URI, messageId);
getContext().getContentResolver().notifyChange(newMessageUri, null);
getContext().getContentResolver().notifyChange(USER_JOIN_MESSAGE_CONTENT_URI, null);
dbConnection.setTransactionSuccessful();
return newMessageUri;
default :
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dbConnection.endTransaction();
}
return null;
}
@Override
public final int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs) {
final SQLiteDatabase dbConnection = mDatabase.getWritableDatabase();
int updateCount = 0;
List<Uri> joinUris = new ArrayList<Uri>();
try {
dbConnection.beginTransaction();
switch (URI_MATCHER.match(uri)) {
case FRIEND_DIR:
updateCount = dbConnection.update(FriendTable.TABLE_NAME, values, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case FRIEND_ID:
final long friendId = ContentUris.parseId(uri);
updateCount = dbConnection.update(FriendTable.TABLE_NAME, values,
FriendTable._ID + "=" + friendId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case USER_DIR:
updateCount = dbConnection.update(UserTable.TABLE_NAME, values, selection, selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case USER_ID:
final long userId = ContentUris.parseId(uri);
updateCount = dbConnection.update(UserTable.TABLE_NAME, values,
UserTable._ID + "=" + userId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case MESSAGE_DIR:
updateCount = dbConnection.update(MessageTable.TABLE_NAME, values, selection, selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case MESSAGE_ID:
final long messageId = ContentUris.parseId(uri);
updateCount = dbConnection.update(MessageTable.TABLE_NAME, values,
MessageTable._ID + "=" + messageId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
default :
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
} finally {
dbConnection.endTransaction();
}
if (updateCount > 0) {
getContext().getContentResolver().notifyChange(uri, null);
for (Uri joinUri : joinUris) {
getContext().getContentResolver().notifyChange(joinUri, null);
}
}
return updateCount;
}
@Override
public final int delete(final Uri uri, final String selection, final String[] selectionArgs) {
final SQLiteDatabase dbConnection = mDatabase.getWritableDatabase();
int deleteCount = 0;
List<Uri> joinUris = new ArrayList<Uri>();
try {
dbConnection.beginTransaction();
switch (URI_MATCHER.match(uri)) {
case FRIEND_DIR:
deleteCount = dbConnection.delete(FriendTable.TABLE_NAME, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case FRIEND_ID:
deleteCount = dbConnection.delete(FriendTable.TABLE_NAME, FriendTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
dbConnection.setTransactionSuccessful();
break;
case USER_DIR:
deleteCount = dbConnection.delete(UserTable.TABLE_NAME, selection, selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case USER_ID:
deleteCount = dbConnection.delete(UserTable.TABLE_NAME, UserTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case MESSAGE_DIR:
deleteCount = dbConnection.delete(MessageTable.TABLE_NAME, selection, selectionArgs);
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case MESSAGE_ID:
deleteCount = dbConnection.delete(MessageTable.TABLE_NAME, MessageTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
joinUris.add(USER_JOIN_MESSAGE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
default :
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
} finally {
dbConnection.endTransaction();
}
if (deleteCount > 0) {
getContext().getContentResolver().notifyChange(uri, null);
for (Uri joinUri : joinUris) {
getContext().getContentResolver().notifyChange(joinUri, null);
}
}
return deleteCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment