Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jpotts18/9327361 to your computer and use it in GitHub Desktop.
Save jpotts18/9327361 to your computer and use it in GitHub Desktop.
package com.rain.example.data.provider;
import com.rain.example.data.database.RainEmployeeDatabase;
import com.rain.example.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 RainEmployeeProvider extends ContentProvider {
public static final String AUTHORITY = "com.rain.example.data.provider";
public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
public static final Uri PERSON_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, PersonContent.CONTENT_PATH);
public static final Uri HOBBY_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, HobbyContent.CONTENT_PATH);
public static final Uri TALENT_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, TalentContent.CONTENT_PATH);
public static final Uri PHONE_CONTACT_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, PhoneContactContent.CONTENT_PATH);
public static final Uri WORK_PLACE_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, WorkPlaceContent.CONTENT_PATH);
public static final Uri VEHICLE_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, VehicleContent.CONTENT_PATH);
public static final Uri PERSON_JOIN_VEHICLE_CONTENT_URI = Uri.withAppendedPath(RainEmployeeProvider.AUTHORITY_URI, PersonJoinVehicleContent.CONTENT_PATH);
private static final UriMatcher URI_MATCHER;
private RainEmployeeDatabase mDatabase;
private static final int PERSON_DIR = 0;
private static final int PERSON_ID = 1;
private static final int HOBBY_DIR = 2;
private static final int HOBBY_ID = 3;
private static final int TALENT_DIR = 4;
private static final int TALENT_ID = 5;
private static final int PHONE_CONTACT_DIR = 6;
private static final int PHONE_CONTACT_ID = 7;
private static final int WORK_PLACE_DIR = 8;
private static final int WORK_PLACE_ID = 9;
private static final int VEHICLE_DIR = 10;
private static final int VEHICLE_ID = 11;
private static final int PERSON_JOIN_VEHICLE_DIR = 12;
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(AUTHORITY, PersonContent.CONTENT_PATH, PERSON_DIR);
URI_MATCHER.addURI(AUTHORITY, PersonContent.CONTENT_PATH + "/#", PERSON_ID);
URI_MATCHER.addURI(AUTHORITY, HobbyContent.CONTENT_PATH, HOBBY_DIR);
URI_MATCHER.addURI(AUTHORITY, HobbyContent.CONTENT_PATH + "/#", HOBBY_ID);
URI_MATCHER.addURI(AUTHORITY, TalentContent.CONTENT_PATH, TALENT_DIR);
URI_MATCHER.addURI(AUTHORITY, TalentContent.CONTENT_PATH + "/#", TALENT_ID);
URI_MATCHER.addURI(AUTHORITY, PhoneContactContent.CONTENT_PATH, PHONE_CONTACT_DIR);
URI_MATCHER.addURI(AUTHORITY, PhoneContactContent.CONTENT_PATH + "/#", PHONE_CONTACT_ID);
URI_MATCHER.addURI(AUTHORITY, WorkPlaceContent.CONTENT_PATH, WORK_PLACE_DIR);
URI_MATCHER.addURI(AUTHORITY, WorkPlaceContent.CONTENT_PATH + "/#", WORK_PLACE_ID);
URI_MATCHER.addURI(AUTHORITY, VehicleContent.CONTENT_PATH, VEHICLE_DIR);
URI_MATCHER.addURI(AUTHORITY, VehicleContent.CONTENT_PATH + "/#", VEHICLE_ID);
URI_MATCHER.addURI(AUTHORITY, PersonJoinVehicleContent.CONTENT_PATH, PERSON_JOIN_VEHICLE_DIR);
}
public static final class PersonContent implements BaseColumns {
public static final String CONTENT_PATH = "person";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.person";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.person";
}
public static final class HobbyContent implements BaseColumns {
public static final String CONTENT_PATH = "hobby";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.hobby";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.hobby";
}
public static final class TalentContent implements BaseColumns {
public static final String CONTENT_PATH = "talent";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.talent";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.talent";
}
public static final class PhoneContactContent implements BaseColumns {
public static final String CONTENT_PATH = "phonecontact";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.phonecontact";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.phonecontact";
}
public static final class WorkPlaceContent implements BaseColumns {
public static final String CONTENT_PATH = "workplace";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.workplace";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.workplace";
}
public static final class VehicleContent implements BaseColumns {
public static final String CONTENT_PATH = "vehicle";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.vehicle";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.rainemployee_database.vehicle";
}
public static final class PersonJoinVehicleContent implements BaseColumns {
public static final String CONTENT_PATH = "person_join_vehicle";
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.rainemployee_database.person_join_vehicle";
}
@Override
public final boolean onCreate() {
mDatabase = new RainEmployeeDatabase(getContext());
return true;
}
@Override
public final String getType(final Uri uri) {
switch (URI_MATCHER.match(uri)) {
case PERSON_DIR:
return PersonContent.CONTENT_TYPE;
case PERSON_ID:
return PersonContent.CONTENT_ITEM_TYPE;
case HOBBY_DIR:
return HobbyContent.CONTENT_TYPE;
case HOBBY_ID:
return HobbyContent.CONTENT_ITEM_TYPE;
case TALENT_DIR:
return TalentContent.CONTENT_TYPE;
case TALENT_ID:
return TalentContent.CONTENT_ITEM_TYPE;
case PHONE_CONTACT_DIR:
return PhoneContactContent.CONTENT_TYPE;
case PHONE_CONTACT_ID:
return PhoneContactContent.CONTENT_ITEM_TYPE;
case WORK_PLACE_DIR:
return WorkPlaceContent.CONTENT_TYPE;
case WORK_PLACE_ID:
return WorkPlaceContent.CONTENT_ITEM_TYPE;
case VEHICLE_DIR:
return VehicleContent.CONTENT_TYPE;
case VEHICLE_ID:
return VehicleContent.CONTENT_ITEM_TYPE;
case PERSON_JOIN_VEHICLE_DIR:
return PersonJoinVehicleContent.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 PERSON_ID:
queryBuilder.appendWhere(PersonTable._ID + "=" + uri.getLastPathSegment());
case PERSON_DIR:
queryBuilder.setTables(PersonTable.TABLE_NAME);
break;
case HOBBY_ID:
queryBuilder.appendWhere(HobbyTable._ID + "=" + uri.getLastPathSegment());
case HOBBY_DIR:
queryBuilder.setTables(HobbyTable.TABLE_NAME);
break;
case TALENT_ID:
queryBuilder.appendWhere(TalentTable._ID + "=" + uri.getLastPathSegment());
case TALENT_DIR:
queryBuilder.setTables(TalentTable.TABLE_NAME);
break;
case PHONE_CONTACT_ID:
queryBuilder.appendWhere(PhoneContactTable._ID + "=" + uri.getLastPathSegment());
case PHONE_CONTACT_DIR:
queryBuilder.setTables(PhoneContactTable.TABLE_NAME);
break;
case WORK_PLACE_ID:
queryBuilder.appendWhere(WorkPlaceTable._ID + "=" + uri.getLastPathSegment());
case WORK_PLACE_DIR:
queryBuilder.setTables(WorkPlaceTable.TABLE_NAME);
break;
case VEHICLE_ID:
queryBuilder.appendWhere(VehicleTable._ID + "=" + uri.getLastPathSegment());
case VEHICLE_DIR:
queryBuilder.setTables(VehicleTable.TABLE_NAME);
break;
case PERSON_JOIN_VEHICLE_DIR:
queryBuilder.setTables(PersonTable.TABLE_NAME + " JOIN " + VehicleTable.TABLE_NAME + " ON (" + PersonTable.TABLE_NAME + "." + PersonTable._ID + "=" + VehicleTable.TABLE_NAME + "." + VehicleTable.PERSON_ID + ")");
projection = new String[] {
//add left table columns
PersonTable.TABLE_NAME + "._id AS " + PersonTable.TABLE_NAME + "__id",
PersonTable.TABLE_NAME + "." + PersonTable.NAME + " AS " + PersonTable.TABLE_NAME + "_" + PersonTable.NAME,
PersonTable.TABLE_NAME + "." + PersonTable.AGE + " AS " + PersonTable.TABLE_NAME + "_" + PersonTable.AGE,
VehicleTable.TABLE_NAME + "._id AS " + VehicleTable.TABLE_NAME + "__id",
VehicleTable.TABLE_NAME + "." + VehicleTable.MAKE + " AS " + VehicleTable.TABLE_NAME + "_" + VehicleTable.MAKE,
VehicleTable.TABLE_NAME + "." + VehicleTable.MODEL + " AS " + VehicleTable.TABLE_NAME + "_" + VehicleTable.MODEL,
VehicleTable.TABLE_NAME + "." + VehicleTable.COLOR + " AS " + VehicleTable.TABLE_NAME + "_" + VehicleTable.COLOR,
};
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 PERSON_DIR:
case PERSON_ID:
final long personId = dbConnection.insertOrThrow(PersonTable.TABLE_NAME, null, values);
final Uri newPersonUri = ContentUris.withAppendedId(PERSON_CONTENT_URI, personId);
getContext().getContentResolver().notifyChange(newPersonUri, null);
getContext().getContentResolver().notifyChange(PERSON_JOIN_VEHICLE_CONTENT_URI, null);
dbConnection.setTransactionSuccessful();
return newPersonUri;
case HOBBY_DIR:
case HOBBY_ID:
final long hobbyId = dbConnection.insertOrThrow(HobbyTable.TABLE_NAME, null, values);
final Uri newHobbyUri = ContentUris.withAppendedId(HOBBY_CONTENT_URI, hobbyId);
getContext().getContentResolver().notifyChange(newHobbyUri, null);
dbConnection.setTransactionSuccessful();
return newHobbyUri;
case TALENT_DIR:
case TALENT_ID:
final long talentId = dbConnection.insertOrThrow(TalentTable.TABLE_NAME, null, values);
final Uri newTalentUri = ContentUris.withAppendedId(TALENT_CONTENT_URI, talentId);
getContext().getContentResolver().notifyChange(newTalentUri, null);
dbConnection.setTransactionSuccessful();
return newTalentUri;
case PHONE_CONTACT_DIR:
case PHONE_CONTACT_ID:
final long phone_contactId = dbConnection.insertOrThrow(PhoneContactTable.TABLE_NAME, null, values);
final Uri newPhoneContactUri = ContentUris.withAppendedId(PHONE_CONTACT_CONTENT_URI, phone_contactId);
getContext().getContentResolver().notifyChange(newPhoneContactUri, null);
dbConnection.setTransactionSuccessful();
return newPhoneContactUri;
case WORK_PLACE_DIR:
case WORK_PLACE_ID:
final long work_placeId = dbConnection.insertOrThrow(WorkPlaceTable.TABLE_NAME, null, values);
final Uri newWorkPlaceUri = ContentUris.withAppendedId(WORK_PLACE_CONTENT_URI, work_placeId);
getContext().getContentResolver().notifyChange(newWorkPlaceUri, null);
dbConnection.setTransactionSuccessful();
return newWorkPlaceUri;
case VEHICLE_DIR:
case VEHICLE_ID:
final long vehicleId = dbConnection.insertOrThrow(VehicleTable.TABLE_NAME, null, values);
final Uri newVehicleUri = ContentUris.withAppendedId(VEHICLE_CONTENT_URI, vehicleId);
getContext().getContentResolver().notifyChange(newVehicleUri, null);
getContext().getContentResolver().notifyChange(PERSON_JOIN_VEHICLE_CONTENT_URI, null);
dbConnection.setTransactionSuccessful();
return newVehicleUri;
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 PERSON_DIR:
updateCount = dbConnection.update(PersonTable.TABLE_NAME, values, selection, selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case PERSON_ID:
final long personId = ContentUris.parseId(uri);
updateCount = dbConnection.update(PersonTable.TABLE_NAME, values,
PersonTable._ID + "=" + personId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case HOBBY_DIR:
updateCount = dbConnection.update(HobbyTable.TABLE_NAME, values, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case HOBBY_ID:
final long hobbyId = ContentUris.parseId(uri);
updateCount = dbConnection.update(HobbyTable.TABLE_NAME, values,
HobbyTable._ID + "=" + hobbyId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case TALENT_DIR:
updateCount = dbConnection.update(TalentTable.TABLE_NAME, values, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case TALENT_ID:
final long talentId = ContentUris.parseId(uri);
updateCount = dbConnection.update(TalentTable.TABLE_NAME, values,
TalentTable._ID + "=" + talentId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case PHONE_CONTACT_DIR:
updateCount = dbConnection.update(PhoneContactTable.TABLE_NAME, values, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case PHONE_CONTACT_ID:
final long phone_contactId = ContentUris.parseId(uri);
updateCount = dbConnection.update(PhoneContactTable.TABLE_NAME, values,
PhoneContactTable._ID + "=" + phone_contactId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case WORK_PLACE_DIR:
updateCount = dbConnection.update(WorkPlaceTable.TABLE_NAME, values, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case WORK_PLACE_ID:
final long work_placeId = ContentUris.parseId(uri);
updateCount = dbConnection.update(WorkPlaceTable.TABLE_NAME, values,
WorkPlaceTable._ID + "=" + work_placeId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case VEHICLE_DIR:
updateCount = dbConnection.update(VehicleTable.TABLE_NAME, values, selection, selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case VEHICLE_ID:
final long vehicleId = ContentUris.parseId(uri);
updateCount = dbConnection.update(VehicleTable.TABLE_NAME, values,
VehicleTable._ID + "=" + vehicleId + (TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")"), selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_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 PERSON_DIR:
deleteCount = dbConnection.delete(PersonTable.TABLE_NAME, selection, selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case PERSON_ID:
deleteCount = dbConnection.delete(PersonTable.TABLE_NAME, PersonTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case HOBBY_DIR:
deleteCount = dbConnection.delete(HobbyTable.TABLE_NAME, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case HOBBY_ID:
deleteCount = dbConnection.delete(HobbyTable.TABLE_NAME, HobbyTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
dbConnection.setTransactionSuccessful();
break;
case TALENT_DIR:
deleteCount = dbConnection.delete(TalentTable.TABLE_NAME, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case TALENT_ID:
deleteCount = dbConnection.delete(TalentTable.TABLE_NAME, TalentTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
dbConnection.setTransactionSuccessful();
break;
case PHONE_CONTACT_DIR:
deleteCount = dbConnection.delete(PhoneContactTable.TABLE_NAME, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case PHONE_CONTACT_ID:
deleteCount = dbConnection.delete(PhoneContactTable.TABLE_NAME, PhoneContactTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
dbConnection.setTransactionSuccessful();
break;
case WORK_PLACE_DIR:
deleteCount = dbConnection.delete(WorkPlaceTable.TABLE_NAME, selection, selectionArgs);
dbConnection.setTransactionSuccessful();
break;
case WORK_PLACE_ID:
deleteCount = dbConnection.delete(WorkPlaceTable.TABLE_NAME, WorkPlaceTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
dbConnection.setTransactionSuccessful();
break;
case VEHICLE_DIR:
deleteCount = dbConnection.delete(VehicleTable.TABLE_NAME, selection, selectionArgs);
joinUris.add(PERSON_JOIN_VEHICLE_CONTENT_URI);
dbConnection.setTransactionSuccessful();
break;
case VEHICLE_ID:
deleteCount = dbConnection.delete(VehicleTable.TABLE_NAME, VehicleTable.WHERE_ID_EQUALS, new String[] { uri.getLastPathSegment() });
joinUris.add(PERSON_JOIN_VEHICLE_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