Permalink
Browse files

implement Timeline db and provider

  • Loading branch information...
1 parent a707279 commit a95a94e4996363b7f7a4cfbeb2745762113f23e2 @bradarmstrong bradarmstrong committed Feb 12, 2012
View
@@ -14,7 +14,7 @@
android:label="@string/app_name"
android:theme="@style/RESTfulAndroidTheme" >
<activity
- android:name="com.jeremyhaberman.restfulandroid.activity.LoginActivity"
+ android:name=".activity.LoginActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -33,14 +33,18 @@
</data>
</intent-filter>
</activity>
- <activity android:name="com.jeremyhaberman.restfulandroid.activity.TimelineActivity" />
+ <activity android:name=".activity.TimelineActivity" />
<activity android:name=".activity.AboutActivity" />
<service android:name=".service.TwitterService" />
<provider
android:name=".provider.ProfileProvider"
- android:authorities="com.jeremyhaberman.restfulandroid.profiles" />
+ android:authorities="mn.aug.restfulandroid.profiles" />
+
+ <provider
+ android:name=".provider.TimelineProvider"
+ android:authorities="mn.aug.restfulandroid.timelineprovider" />
</application>
</manifest>
@@ -8,7 +8,7 @@
public static final String TIMELINE_TABLE_NAME = "timeline";
- public static final String AUTHORITY = "com.jeremyhaberman.restfulandroid.profiles";
+ public static final String AUTHORITY = "mn.aug.restfulandroid.profiles";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
@@ -1,37 +0,0 @@
-package mn.aug.restfulandroid.provider;
-
-import android.content.Context;
-import android.database.sqlite.SQLiteDatabase;
-import android.database.sqlite.SQLiteOpenHelper;
-
-import static android.provider.BaseColumns._ID;
-import static mn.aug.restfulandroid.provider.ProfileConstants.NAME;
-import static mn.aug.restfulandroid.provider.ProfileConstants.TABLE_NAME;
-
-class ProfileData extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "profiles.db";
- private static final int DATABASE_VERSION = 1;
-
- /**
- * Create a helper object for the Profile database
- */
- ProfileData(Context ctx) {
- super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
- + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
- + NAME + " TEXT);");
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion,
- int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
- onCreate(db);
- }
-
-}
-
@@ -33,15 +33,15 @@
*/
private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.example.profile";
- private ProfileData profiles;
+ private ProviderDbHelper dbHelper;
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "profiles", PROFILES);
uriMatcher.addURI(AUTHORITY, "profiles/#", PROFILE_ID);
- profiles = new ProfileData(getContext());
+ this.dbHelper = new ProviderDbHelper(this.getContext());
return true;
}
@@ -54,7 +54,7 @@ public Cursor query(Uri uri, String[] projection, String selection,
}
// Get the database and run the query
- SQLiteDatabase db = profiles.getReadableDatabase();
+ SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, projection, selection,
selectionArgs, null, null, orderBy);
@@ -78,7 +78,7 @@ public String getType(Uri uri) {
@Override
public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = profiles.getWritableDatabase();
+ SQLiteDatabase db = dbHelper.getWritableDatabase();
// Validate the requested uri
if (uriMatcher.match(uri) != PROFILES) {
@@ -107,7 +107,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
- SQLiteDatabase db = profiles.getWritableDatabase();
+ SQLiteDatabase db = dbHelper.getWritableDatabase();
// Validate the requested uri
if (uriMatcher.match(uri) != PROFILE_ID) {
@@ -0,0 +1,63 @@
+package mn.aug.restfulandroid.provider;
+
+import static android.provider.BaseColumns._ID;
+import static mn.aug.restfulandroid.provider.ProfileConstants.NAME;
+import static mn.aug.restfulandroid.provider.ProfileConstants.TABLE_NAME;
+import mn.aug.restfulandroid.provider.TimelineProviderContract.TimelineTable;
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.util.Log;
+
+/**
+ * This creates, updates, and opens the database. Opening is handled by the superclass, we handle
+ * the create & upgrade steps
+ */
+public class ProviderDbHelper extends SQLiteOpenHelper {
+
+ public final String TAG = getClass().getSimpleName();
+
+ //Name of the database file
+ private static final String DATABASE_NAME = "restdroid.db";
+ private static final int DATABASE_VERSION = 1;
+
+ public ProviderDbHelper(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+
+ // CREATE TIMELINE TABLE
+ StringBuilder sqlBuilder = new StringBuilder();
+ sqlBuilder.append("CREATE TABLE " + TimelineTable.TABLE_NAME + " (");
+ sqlBuilder.append(TimelineTable._ID + " INTEGER, ");
+ sqlBuilder.append(TimelineTable._STATUS + " TEXT, ");
+ sqlBuilder.append(TimelineTable._RESULT + " INTEGER, ");
+ sqlBuilder.append(TimelineTable.AUTHOR + " TEXT, ");
+ sqlBuilder.append(TimelineTable.TWEET_TEXT + " TEXT, ");
+ sqlBuilder.append(TimelineTable.CREATED + " INTEGER, ");
+ sqlBuilder.append(");");
+ String sql = sqlBuilder.toString();
+ Log.i(TAG, "Creating DB table with string: '" + sql + "'");
+ db.execSQL(sql);
+
+ // CREATE PROFILE TABLE
+ sqlBuilder = new StringBuilder();
+ sqlBuilder.append("CREATE TABLE " + ProfileConstants.TABLE_NAME + " (");
+ sqlBuilder.append(ResourceTable._ID + " INTEGER, ");
+ sqlBuilder.append(ResourceTable._STATUS + " TEXT, ");
+ sqlBuilder.append(ResourceTable._RESULT + " INTEGER, ");
+ sqlBuilder.append(ProfileConstants.NAME + " TEXT, ");
+
+ sql = sqlBuilder.toString();
+ Log.i(TAG, "Creating DB table with string: '" + sql + "'");
+ db.execSQL(sql);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ //Gets called when the database is upgraded, i.e. the version number changes
+ }
+
+}
@@ -0,0 +1,17 @@
+package mn.aug.restfulandroid.provider;
+
+import android.provider.BaseColumns;
+
+public interface ResourceTable extends BaseColumns {
+
+ /* The current transaction status. May be null if no current transaction for this resource
+ * <P>Type: STRING</P>
+ */
+ public static final String _STATUS = "_status";
+
+ /* The transaction result code, typically an http status.
+ * <P>Type: INTEGER </P>
+ */
+ public static final String _RESULT = "_result";
+
+}
Oops, something went wrong.

0 comments on commit a95a94e

Please sign in to comment.