1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| public class DatabaseProvider extends ContentProvider
{
private SQLiteDatabase db;
private static final String AUTHORITY = "net.moi.monprog.DatabaseProvider";
private static final String PROJET_PATH = "projet";
public static final Uri PROJET_URI = Uri.parse("content://" + AUTHORITY + "/" + PROJET_PATH);
private static final int PROJET_URI_ID = 1;
public static final Uri PROJET_UNIQUE_URI = Uri.parse("content://" + AUTHORITY + "/" + PROJET_PATH + "/#");
private static final int PROJET_UNIQUE_URI_ID = 2;
public static final String PROJET_CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/projets";
public static final String PROJET_CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/projet";
private static final String TACHE_PATH = "tache";
public static final Uri TACHE_URI = Uri.parse("content://" + AUTHORITY + "/" + TACHE_PATH);
private static final int TACHE_URI_ID = 101;
public static final Uri TACHE_UNIQUE_URI = Uri.parse("content://" + AUTHORITY + "/" + TACHE_PATH + "/#");
private static final int TACHE_UNIQUE_URI_ID = 102;
public static final String TACHE_CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/taches";
public static final String TACHE_CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/tache";
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
uriMatcher.addURI(AUTHORITY, PROJET_PATH, PROJET_URI_ID);
uriMatcher.addURI(AUTHORITY, PROJET_PATH + "/#", PROJET_UNIQUE_URI_ID);
uriMatcher.addURI(AUTHORITY, TACHE_PATH, TACHE_URI_ID);
uriMatcher.addURI(AUTHORITY, TACHE_PATH + "/#", TACHE_UNIQUE_URI_ID);
}
@Override
public boolean onCreate()
{
this.db = new MyDBHelper(this.getContext()).getWritableDatabase();
return (this.db == null) ? false:true;
}
@Override
public String getType(Uri uri)
{
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
String table;
Uri newUri;
switch (uriMatcher.match(uri))
{
case PROJET_UNIQUE_URI_ID :
case PROJET_URI_ID :
table = ProjetTable.TABLE_NAME;
newUri = PROJET_UNIQUE_URI;
break;
case TACHE_UNIQUE_URI_ID :
case TACHE_URI_ID :
table = TacheTable.TABLE_NAME;
newUri = TACHE_UNIQUE_URI;
break;
default: throw new IllegalArgumentException("URI inconnu : " + uri);
}
long id = this.db.insertOrThrow(table, null, values);
this.getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(newUri, id);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
String table;
switch (uriMatcher.match(uri))
{
case PROJET_UNIQUE_URI_ID :
case PROJET_URI_ID :
table = ProjetTable.TABLE_NAME;
break;
case TACHE_UNIQUE_URI_ID :
case TACHE_URI_ID :
table = TacheTable.TABLE_NAME;
break;
default: throw new IllegalArgumentException("URI inconnu : " + uri);
}
int nbrSup = this.db.delete(table, selection, selectionArgs);
this.getContext().getContentResolver().notifyChange(uri, null);
return nbrSup;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
String table;
switch (uriMatcher.match(uri))
{
case PROJET_UNIQUE_URI_ID :
case PROJET_URI_ID :
table = ProjetTable.TABLE_NAME;
break;
case TACHE_UNIQUE_URI_ID :
case TACHE_URI_ID :
table = TacheTable.TABLE_NAME;
break;
default: throw new IllegalArgumentException("URI inconnu : " + uri);
}
int nbrMod = this.db.update(table, values, selection, selectionArgs);
this.getContext().getContentResolver().notifyChange(uri, null);
return nbrMod;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
switch (uriMatcher.match(uri))
{
case PROJET_UNIQUE_URI_ID :
queryBuilder.appendWhere("_id=" + uri.getLastPathSegment());
case PROJET_URI_ID :
queryBuilder.setTables(ProjetTable.TABLE_NAME);
break;
case TACHE_UNIQUE_URI_ID :
queryBuilder.appendWhere("_id=" + uri.getLastPathSegment());
case TACHE_URI_ID :
queryBuilder.setTables(TacheTable.TABLE_NAME);
break;
default: throw new IllegalArgumentException("URI inconnu : " + uri);
}
Cursor cursor = queryBuilder.query(this.db, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(this.getContext().getContentResolver(), uri);
return cursor;
}
} |
Partager