00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 package com.github.rosjava.android_remocons.rocon_remocon;
00037
00038 import android.content.ContentProvider;
00039 import android.content.ContentValues;
00040 import android.content.Context;
00041 import android.database.Cursor;
00042 import android.database.sqlite.SQLiteDatabase;
00043 import android.database.sqlite.SQLiteOpenHelper;
00044 import android.net.Uri;
00045 import android.util.Log;
00046
00047 public class Database extends ContentProvider {
00048 DatabaseHelper dbh;
00049
00050 private class DatabaseHelper extends SQLiteOpenHelper {
00051 DatabaseHelper(Context context) {
00052 super(context, DATABASE_NAME, null, DATABASE_VERSION);
00053 }
00054
00058 @Override
00059 public void onCreate(SQLiteDatabase db) {
00060 Log.i("MasterChooser", "Creating database...");
00061 db.execSQL(TABLE_CREATE);
00062 }
00063
00067 @Override
00068 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
00069 Log.i("MasterChooser", "The database is being upgraded from " + oldVersion + " to " + newVersion);
00070 }
00071 }
00072
00073 public static final Uri CONTENT_URI = Uri.parse("content://com.github.rosjava.android_remocons.rocon_remocon");
00074 public static final int DATABASE_VERSION = 2;
00075 public static final String DATABASE_NAME = "concertlist_table";
00076 public static final String TABLE_NAME = "concertlist";
00077 public static final String TABLE_COLUMN = "concerts";
00078 private static final String TABLE_CREATE =
00079 "CREATE TABLE " + TABLE_NAME + " (" + TABLE_COLUMN + " TEXT);";
00080
00081 @Override
00082 public int delete(Uri arg0, String arg1, String[] arg2) {
00083 Log.e("CurrentConcertContentProvider", "Invalid method: delete");
00084 return 0;
00085 }
00086
00087 @Override
00088 public String getType(Uri uri) {
00089 Log.e("CurrentConcertContentProvider", "Invalid method: getType");
00090 return null;
00091 }
00092
00093 @Override
00094 public Uri insert(Uri uri, ContentValues values) {
00095 SQLiteDatabase db = dbh.getWritableDatabase();
00096 if (db == null) {
00097 Log.e("CurrentConcertContentProvider", "Could not get the writable database.");
00098 return null;
00099 }
00100 db.beginTransaction();
00101 if (values.get(TABLE_COLUMN) != null) {
00102 Log.i("CurrentConcertContentProvider", "Saving concert...");
00103
00104 Cursor c = db.query(TABLE_NAME, new String[]{TABLE_COLUMN,},
00105 null, new String[]{}, null, null, null);
00106 if (c.getCount() > 0) {
00107 Log.i("CurrentConcertContentProvider", "Update currently existing row");
00108 db.update(TABLE_NAME, values, null, new String[]{});
00109 } else {
00110 Log.i("CurrentConcertContentProvider", "Insert new row");
00111 if (db.insert(TABLE_NAME, null, values) < 0) {
00112 Log.e("CurrentConcertContentProvider", "Error inserting row!");
00113 }
00114 }
00115 } else {
00116 Log.i("CurrentConcertContentProvider", "Deleting saved concert...");
00117 db.delete(TABLE_NAME, null, new String[]{});
00118 }
00119
00120 db.setTransactionSuccessful();
00121 db.endTransaction();
00122 db.close();
00123 Log.i("CurrentConcertContentProvider", "Done saving current concert");
00124 return CONTENT_URI;
00125 }
00126
00127 @Override
00128 public boolean onCreate() {
00129 dbh = new DatabaseHelper(this.getContext());
00130 if (dbh == null) {
00131 return false;
00132 } else {
00133 return true;
00134 }
00135 }
00136
00137 @Override
00138 public Cursor query(Uri uri, String[] projection, String selection,
00139 String[] selectionArgs, String sortOrder) {
00140 SQLiteDatabase db = dbh.getReadableDatabase();
00141 Cursor res = db.query(TABLE_NAME, new String[]{TABLE_COLUMN,},
00142 null, new String[]{}, null, null, null);
00143
00144 return res;
00145 }
00146
00147 @Override
00148 public int update(Uri uri, ContentValues values, String selection,
00149 String[] selectionArgs) {
00150 Log.e("CurrentConcertContentProvider", "Invalid method: update");
00151
00152 return 0;
00153 }
00154 }