Advertisement
Ankhwatcher

StatusProvider

Jul 25th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. package ie.appz.youtubevideostestapp.persistence;
  2.  
  3. import android.content.ContentProvider;
  4. import android.content.ContentResolver;
  5. import android.content.ContentUris;
  6. import android.content.ContentValues;
  7. import android.content.UriMatcher;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteConstraintException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteQueryBuilder;
  12. import android.net.Uri;
  13. import android.text.TextUtils;
  14. import android.util.Log;
  15.  
  16. import java.sql.SQLException;
  17.  
  18. /**
  19.  * Created by rory on 17/07/13.
  20.  */
  21. public class StatusProvider extends ContentProvider {
  22.     public static final int STATUS = 100;
  23.     public static final String AUTHORITY = "ie.appz.youtubevideostestapp.persistence.StatusProvider";
  24.     public static final String CONTENT_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
  25.             + "/ia-status";
  26.     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
  27.     public static final String STATUS_BASE_PATH = "status";
  28.     private static final UriMatcher sURIMatcher;
  29.  
  30.     static {
  31.         sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  32.         sURIMatcher.addURI(AUTHORITY, STATUS_BASE_PATH + "/*", STATUS);
  33.     }
  34.  
  35.     private StatusOpenHelper mStatusOpenHelper;
  36.  
  37.     @Override
  38.     public boolean onCreate() {
  39.         mStatusOpenHelper = new StatusOpenHelper(getContext());
  40.         return true;
  41.     }
  42.  
  43.     @Override
  44.     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  45.         SQLiteQueryBuilder sqLiteQueryBuilder = new SQLiteQueryBuilder();
  46.         sqLiteQueryBuilder.setTables(StatusOpenHelper.STATUS_TABLE);
  47.         int uriType = sURIMatcher.match(uri);
  48.         switch (uriType) {
  49.             case STATUS:
  50.                 sqLiteQueryBuilder.appendWhere(StatusOpenHelper.STATUS_SCREENNAME + " = '" + uri.getPathSegments().get(1) + "'");
  51.                 break;
  52.             default:
  53.                 Log.e(this.getClass().getName(), "Unknown URI: " + uri + " Failed to recognise uriType: " + uriType);
  54.                 throw new IllegalArgumentException("Unknown URI");
  55.         }
  56.         Cursor cursor = sqLiteQueryBuilder.query(mStatusOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
  57.         cursor.setNotificationUri(getContext().getContentResolver(), uri);
  58.         return cursor;
  59.     }
  60.  
  61.     @Override
  62.     public String getType(Uri uri) {
  63.         int uriType = sURIMatcher.match(uri);
  64.         switch (uriType) {
  65.             case STATUS:
  66.                 return CONTENT_TYPE;
  67.             default:
  68.                 return null;
  69.         }
  70.     }
  71.  
  72.     @Override
  73.     public Uri insert(Uri uri, ContentValues contentValues) {
  74.         int uriType = sURIMatcher.match(uri);
  75.         assert uriType == STATUS;
  76.         Log.d(this.getClass().getName(), "Get Context returns: " + getContext().toString());
  77.  
  78.         SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
  79.         try {
  80.             long newID = sqLiteDatabase.insertOrThrow(StatusOpenHelper.STATUS_TABLE, null, contentValues);
  81.             if (newID > 0) {
  82.                 Uri newUri = ContentUris.withAppendedId(uri, newID);
  83.                 getContext().getContentResolver().notifyChange(uri, null);
  84.                 sqLiteDatabase.close();
  85.                 return newUri;
  86.             } else
  87.                 throw new SQLException("Failed to insert row into " + uri);
  88.  
  89.         } catch (SQLiteConstraintException e) {
  90.             Log.i(this.getClass().getName(), "Ignoring constraint failure.");
  91.         } catch (SQLException e) {
  92.             e.printStackTrace();
  93.         }
  94.         sqLiteDatabase.close();
  95.         return null;
  96.     }
  97.  
  98.     @Override
  99.     public int delete(Uri uri, String selection, String[] selectionArgs) {
  100.         int uriType = sURIMatcher.match(uri);
  101.         SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
  102.         int rowsAffected = 0;
  103.  
  104.         switch (uriType) {
  105.             case STATUS:
  106.                 String screenName = uri.getLastPathSegment();
  107.                 if (TextUtils.isEmpty(selection))
  108.                     rowsAffected = sqLiteDatabase.delete(StatusOpenHelper.STATUS_TABLE, StatusOpenHelper.STATUS_SCREENNAME + " = " + screenName, null);
  109.                 else
  110.                     rowsAffected = sqLiteDatabase.delete(StatusOpenHelper.STATUS_TABLE, selection + " and " + StatusOpenHelper.STATUS_SCREENNAME + " = " + screenName, selectionArgs);
  111.  
  112.                 break;
  113.             default:
  114.                 throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
  115.         }
  116.         getContext().getContentResolver().notifyChange(uri, null);
  117.         sqLiteDatabase.close();
  118.         return rowsAffected;
  119.     }
  120.  
  121.     @Override
  122.     public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
  123.         int uriType = sURIMatcher.match(uri);
  124.         SQLiteDatabase sqLiteDatabase = mStatusOpenHelper.getWritableDatabase();
  125.         int rowsAffected = 0;
  126.  
  127.         switch (uriType) {
  128.             case STATUS:
  129.                 rowsAffected = sqLiteDatabase.update(StatusOpenHelper.STATUS_TABLE, contentValues, selection, selectionArgs);
  130.                 break;
  131.             default:
  132.                 throw new IllegalArgumentException("Unknown URI");
  133.         }
  134.         getContext().getContentResolver().notifyChange(uri, null);
  135.         sqLiteDatabase.close();
  136.         return rowsAffected;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement