Guest User

Untitled

a guest
Feb 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. package com.jalbasri.squawk;
  2.  
  3. import android.content.ContentProvider;
  4. import android.content.ContentUris;
  5. import android.content.ContentValues;
  6. import android.content.Context;
  7. import android.content.UriMatcher;
  8. import android.database.Cursor;
  9. import android.database.SQLException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteQueryBuilder;
  12. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  13. import android.database.sqlite.SQLiteOpenHelper;
  14. import android.net.Uri;
  15. import android.text.TextUtils;
  16. import android.util.Log;
  17.  
  18. public class TwitterStatusContentProvider extends ContentProvider{
  19. private static final String TAG = TwitterStatusContentProvider.class.getSimpleName();
  20.  
  21. public static final Uri CONTENT_URI =
  22. Uri.parse("content://com.jalbasri.squawk.twitterstatusprovider/twitteritems");
  23. public static final String KEY_STATUS_ID = "_id";
  24. public static final String KEY_STATUS_TEXT = "_status_text";
  25. public static final String KEY_CREATED_AT = "_created_at";
  26.  
  27. public static final String KEY_USER_ID = "_user_id";
  28. public static final String KEY_USER_NAME = "_user_name";
  29. public static final String KEY_USER_SCREEN_NAME = "_user_screen_name";
  30. public static final String KEY_USER_IMAGE = "_user_image";
  31. public static final String KEY_USER_URL = "_user_url";
  32.  
  33. public static final String KEY_LATITUDE = "_latitude";
  34. public static final String KEY_LONGITUDE = "_longitude";
  35.  
  36.  
  37.  
  38. private MySQLiteOpenHelper mySQLiteOpenHelper;
  39. private static final int ALL_ROWS = 1;
  40. private static final int SINGLE_ROW = 2;
  41. private static final UriMatcher uriMatcher;
  42.  
  43. static {
  44. uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  45. uriMatcher.addURI("com.jalbasri.twitterstatusprovider", "twitteritems", ALL_ROWS);
  46. uriMatcher.addURI("com.jalbasri.twitterstatusprovider", "twitteritems/#", SINGLE_ROW);
  47. }
  48.  
  49. @Override
  50. public boolean onCreate() {
  51.  
  52. mySQLiteOpenHelper = new MySQLiteOpenHelper(getContext(),
  53. MySQLiteOpenHelper.DATABASE_NAME, null, MySQLiteOpenHelper.DATABASE_VERSION);
  54.  
  55. return false;
  56. }
  57.  
  58. @Override
  59. public String getType(Uri uri) {
  60. switch(uriMatcher.match(uri)) {
  61. case ALL_ROWS: return "vnd.android.cursor.dir/vnd.jalbasri.twitterstatus";
  62. case SINGLE_ROW: return "vnd.android.cursor.item/vnd.jalbasri.twitterstatus";
  63. default: throw new IllegalArgumentException("Unsupported URI: " + uri);
  64. }
  65. }
  66.  
  67. @Override
  68. public Cursor query(Uri uri, String[] projection, String selection,
  69. String[] selectionArgs, String sortOrder) {
  70. SQLiteDatabase db;
  71. try {
  72. db = mySQLiteOpenHelper.getWritableDatabase();
  73. } catch (SQLException e) {
  74. Log.e(TAG, "Query: Unable to get a writable database, " +
  75. "falling back to readable database");
  76. db = mySQLiteOpenHelper.getReadableDatabase();
  77. }
  78.  
  79. String groupBy = null;
  80. String having = null;
  81.  
  82. SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
  83. queryBuilder.setTables(MySQLiteOpenHelper.DATABASE_TABLE);
  84.  
  85. switch (uriMatcher.match(uri)) {
  86. case SINGLE_ROW:
  87. String rowId = uri.getPathSegments().get(1);
  88. queryBuilder.appendWhere(KEY_STATUS_ID + "=" + rowId);
  89. default: break;
  90. }
  91.  
  92. Cursor cursor = queryBuilder.query(db, projection, selection,
  93. selectionArgs, groupBy, having, sortOrder);
  94.  
  95. cursor.setNotificationUri(getContext().getContentResolver(), CONTENT_URI);
  96.  
  97. return cursor;
  98. }
  99.  
  100. @Override
  101. public Uri insert(Uri uri, ContentValues values) {
  102.  
  103. SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
  104. String nullColumnHack = null;
  105.  
  106. long id = db.insert(MySQLiteOpenHelper.DATABASE_TABLE, nullColumnHack, values);
  107.  
  108. if (id > -1) {
  109. Uri insertedId = ContentUris.withAppendedId(CONTENT_URI, id);
  110. getContext().getContentResolver().notifyChange(insertedId, null);
  111. return insertedId;
  112. }
  113. else
  114. return null;
  115. }
  116.  
  117. @Override
  118. public int delete(Uri uri, String selection, String[] selectionArgs) {
  119.  
  120. SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
  121.  
  122. switch(uriMatcher.match(uri)) {
  123. case SINGLE_ROW:
  124. String rowId= uri.getPathSegments().get(1);
  125. selection = KEY_STATUS_ID + "=" + rowId
  126. + (!TextUtils.isEmpty(selection) ?
  127. " AND (" + selection + ")" : "" );
  128. default: break;
  129. }
  130.  
  131. if (selection == null)
  132. selection = "1";
  133.  
  134. int deleteCount = db.delete(MySQLiteOpenHelper.DATABASE_TABLE, selection, selectionArgs);
  135.  
  136. getContext().getContentResolver().notifyChange(uri, null);
  137.  
  138. return deleteCount;
  139. }
  140.  
  141.  
  142. @Override
  143. public int update(Uri uri, ContentValues values, String selection,
  144. String[] selectionArgs) {
  145.  
  146. SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
  147.  
  148. switch(uriMatcher.match(uri)) {
  149. case SINGLE_ROW:
  150. String rowId= uri.getPathSegments().get(1);
  151. selection = KEY_STATUS_ID + " = " + rowId
  152. + (!TextUtils.isEmpty(selection) ?
  153. " AND (" + selection + ")" : "" );
  154. default: break;
  155. }
  156.  
  157. int updateCount = db.update(MySQLiteOpenHelper.DATABASE_TABLE, values, selection, selectionArgs);
  158. getContext().getContentResolver().notifyChange(uri, null);
  159.  
  160. return updateCount;
  161. }
  162.  
  163. private static class MySQLiteOpenHelper extends SQLiteOpenHelper {
  164.  
  165. public static final String DATABASE_NAME = "twitterStatusDatabase.db";
  166. public static final int DATABASE_VERSION = 1;
  167. public static final String DATABASE_TABLE = "twitterStatusTable";
  168.  
  169. private static final String DATABASE_CREATE = "CREATE TABLE " +
  170. DATABASE_TABLE + " (" +
  171. KEY_STATUS_ID + " INTEGER, " +
  172. KEY_STATUS_TEXT + " TEXT, " +
  173. KEY_CREATED_AT + " INTEGER, " +
  174. KEY_USER_ID + " INTEGER, " +
  175. KEY_USER_NAME + " TEXT, " +
  176. KEY_USER_SCREEN_NAME + " TEXT, " +
  177. KEY_USER_IMAGE + " TEXT, " +
  178. KEY_USER_URL + " TEXT, " +
  179. KEY_LATITUDE + " REAL, " +
  180. KEY_LONGITUDE + " REAL " +
  181. ");" ;
  182.  
  183. public MySQLiteOpenHelper(Context context, String name,
  184. CursorFactory factory, int version) {
  185. super(context, name, factory, version);
  186. }
  187.  
  188. @Override
  189. public void onCreate(SQLiteDatabase db) {
  190. Log.d(TAG, "Create Databse: " + DATABASE_CREATE);
  191. db.execSQL(DATABASE_CREATE);
  192. }
  193.  
  194. @Override
  195. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  196. Log.w(TAG, "Upgrading from version " + oldVersion +
  197. " to " + newVersion + ", which will destroy all old data");
  198. db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE);
  199. onCreate(db);
  200. }
  201.  
  202. }
  203.  
  204. }
Add Comment
Please, Sign In to add comment