Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ie.appz.youtubevideostestapp.persistence;
- import android.content.ContentProvider;
- import android.content.ContentResolver;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteConstraintException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteQueryBuilder;
- import android.net.Uri;
- import android.text.TextUtils;
- import android.util.Log;
- import java.sql.SQLException;
- /**
- * Created by rory on 17/07/13.
- */
- public class StatusProvider extends ContentProvider {
- public static final int STATUS = 100;
- public static final String AUTHORITY = "ie.appz.youtubevideostestapp.persistence.StatusProvider";
- public static final String CONTENT_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
- + "/ia-status";
- public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
- public static final String STATUS_BASE_PATH = "status";
- private static final UriMatcher sURIMatcher;
- static {
- sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- sURIMatcher.addURI(AUTHORITY, STATUS_BASE_PATH + "/*", STATUS);
- }
- private StatusOpenHelper mStatusOpenHelper;
- @Override
- public boolean onCreate() {
- mStatusOpenHelper = new StatusOpenHelper(getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
- SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
- sqLiteQueryBuilder.setTables(StatusOpenHelper.STATUS_TABLE);
- int uriType = sURIMatcher.match(uri);
- switch (uriType) {
- case STATUS:
- sqLiteQueryBuilder.appendWhere(StatusOpenHelper.STATUS_SCREENNAME + " = '" + uri.getPathSegments().get(1) + "'");
- break;
- default:
- Log.e(this.getClass().getName(), "Unknown URI: " + uri + " Failed to recognise uriType: " + uriType);
- throw new IllegalArgumentException("Unknown URI");
- }
- Cursor cursor = sqLiteQueryBuilder.query(mStatusOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
- cursor.setNotificationUri(getContext().getContentResolver(), uri);
- return cursor;
- }
- @Override
- public String getType(Uri uri) {
- int uriType = sURIMatcher.match(uri);
- switch (uriType) {
- case STATUS:
- return CONTENT_TYPE;
- default:
- return null;
- }
- }
- @Override
- public Uri insert(Uri uri, ContentValues contentValues) {
- int uriType = sURIMatcher.match(uri);
- assert uriType == STATUS;
- Log.d(this.getClass().getName(), "Get Context returns: " + getContext().toString());
- SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
- try {
- long newID = sqLiteDatabase.insertOrThrow(StatusOpenHelper.STATUS_TABLE, null, contentValues);
- if (newID > 0) {
- Uri newUri = ContentUris.withAppendedId(uri, newID);
- getContext().getContentResolver().notifyChange(uri, null);
- sqLiteDatabase.close();
- return newUri;
- } else
- throw new SQLException("Failed to insert row into " + uri);
- } catch (SQLiteConstraintException e) {
- Log.i(this.getClass().getName(), "Ignoring constraint failure.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- sqLiteDatabase.close();
- return null;
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- int uriType = sURIMatcher.match(uri);
- SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
- int rowsAffected = 0;
- switch (uriType) {
- case STATUS:
- String screenName = uri.getLastPathSegment();
- if (TextUtils.isEmpty(selection))
- rowsAffected = sqLiteDatabase.delete(StatusOpenHelper.STATUS_TABLE, StatusOpenHelper.STATUS_SCREENNAME + " = " + screenName, null);
- else
- rowsAffected = sqLiteDatabase.delete(StatusOpenHelper.STATUS_TABLE, selection + " and " + StatusOpenHelper.STATUS_SCREENNAME + " = " + screenName, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- sqLiteDatabase.close();
- return rowsAffected;
- }
- @Override
- public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
- int uriType = sURIMatcher.match(uri);
- SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
- int rowsAffected = 0;
- switch (uriType) {
- case STATUS:
- rowsAffected = sqLiteDatabase.update(StatusOpenHelper.STATUS_TABLE, contentValues, selection, selectionArgs);
- break;
- default:
- throw new IllegalArgumentException("Unknown URI");
- }
- getContext().getContentResolver().notifyChange(uri, null);
- sqLiteDatabase.close();
- return rowsAffected;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement