From 36d309edf616373959eeaa006b48e0cf62479d23 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Tue, 16 Apr 2013 17:45:20 +0200 Subject: [PATCH] Add ConnectionProfileContentProvider class ContentProvider to interface with application connection profiles. Backed on a sqlite DB --- .../ConnectionProfileContentProvider.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 com.rcarrillocruz.android.openstackdroid/src/com/rcarrillocruz/android/openstackdroid/ConnectionProfileContentProvider.java diff --git a/com.rcarrillocruz.android.openstackdroid/src/com/rcarrillocruz/android/openstackdroid/ConnectionProfileContentProvider.java b/com.rcarrillocruz.android.openstackdroid/src/com/rcarrillocruz/android/openstackdroid/ConnectionProfileContentProvider.java new file mode 100644 index 0000000..44f74b2 --- /dev/null +++ b/com.rcarrillocruz.android.openstackdroid/src/com/rcarrillocruz/android/openstackdroid/ConnectionProfileContentProvider.java @@ -0,0 +1,147 @@ +package com.rcarrillocruz.android.openstackdroid; + +import android.content.ContentProvider; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.UriMatcher; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.net.Uri; +import android.text.TextUtils; + +public class ConnectionProfileContentProvider extends ContentProvider { + private ConnectionProfileDatabaseHelper database; + + private static final int CONNECTION_PROFILES = 10; + private static final int CONNECTION_PROFILE_ID = 20; + + private static final String AUTHORITY = "com.rcarrillocruz.android.openstackdroid"; + private static final String BASE_PATH = "ConnectionProfileContentProvider"; + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH); + public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/connection_profiles"; + public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/connection_profile"; + + private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); + static { + sURIMatcher.addURI(AUTHORITY, BASE_PATH, CONNECTION_PROFILES); + sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", CONNECTION_PROFILE_ID); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + // TODO Auto-generated method stub + int uriType = sURIMatcher.match(uri); + SQLiteDatabase sqlDB = database.getWritableDatabase(); + int rowsDeleted = 0; + + switch (uriType) { + case CONNECTION_PROFILES: + rowsDeleted = sqlDB.delete(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, selection, selectionArgs); + break; + case CONNECTION_PROFILE_ID: + + String id = uri.getLastPathSegment(); + + if (TextUtils.isEmpty(selection)) { + rowsDeleted = sqlDB.delete(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, ConnectionProfileTable.COLUMN_ID + "=" + id, null); + } else { + rowsDeleted = sqlDB.delete(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, ConnectionProfileTable.COLUMN_ID + "=" + id + " and " + selection, selectionArgs); + } + + break; + default: + throw new IllegalArgumentException("Unknown URI: " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + + return rowsDeleted; + } + + @Override + public String getType(Uri uri) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + // TODO Auto-generated method stub + int uriType = sURIMatcher.match(uri); + SQLiteDatabase sqlDB = database.getWritableDatabase(); + long id = 0; + + switch (uriType) { + case CONNECTION_PROFILES: + id = sqlDB.insert(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, null, values); + break; + default: + throw new IllegalArgumentException("Unknown URI: " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + + return Uri.parse(BASE_PATH + "/" + id); + } + + @Override + public boolean onCreate() { + // TODO Auto-generated method stub + database = new ConnectionProfileDatabaseHelper(getContext()); + return false; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + // TODO Auto-generated method stub + SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); + queryBuilder.setTables(ConnectionProfileTable.TABLE_CONNECTION_PROFILE); + int uriType = sURIMatcher.match(uri); + + switch (uriType) { + case CONNECTION_PROFILES: + break; + case CONNECTION_PROFILE_ID: + queryBuilder.appendWhere(ConnectionProfileTable.COLUMN_ID + "=" + uri.getLastPathSegment()); + break; + default: + throw new IllegalArgumentException("Unknown URI"); + } + + SQLiteDatabase sqlDB = database.getReadableDatabase(); + Cursor cursor = queryBuilder.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder); + cursor.setNotificationUri(getContext().getContentResolver(), uri); + + return cursor; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + // TODO Auto-generated method stub + int uriType = sURIMatcher.match(uri); + SQLiteDatabase sqlDB = database.getWritableDatabase(); + int rowsUpdated = 0; + + switch (uriType) { + case CONNECTION_PROFILES: + rowsUpdated = sqlDB.update(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, values, selection, selectionArgs); + break; + case CONNECTION_PROFILE_ID: + String id = uri.getLastPathSegment(); + if (TextUtils.isEmpty(selection)) { + rowsUpdated = sqlDB.update(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, values, ConnectionProfileTable.COLUMN_ID + "=" + id, null); + } else { + rowsUpdated = sqlDB.update(ConnectionProfileTable.TABLE_CONNECTION_PROFILE, values, ConnectionProfileTable.COLUMN_ID + "=" + id + " and " + selection, selectionArgs); + } + break; + default: + throw new IllegalArgumentException("Unknown URI: " + uri); + } + + getContext().getContentResolver().notifyChange(uri, null); + + return rowsUpdated; + } + +}