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 package com.github.rosjava.android_apps.application_management;
00035
00036 import android.content.ContentProvider;
00037 import android.content.ContentValues;
00038 import android.content.Context;
00039 import android.database.Cursor;
00040 import android.database.sqlite.SQLiteOpenHelper;
00041 import android.database.sqlite.SQLiteDatabase;
00042 import android.net.Uri;
00043 import android.util.Log;
00044 public class RobotsContentProvider extends ContentProvider {
00045 DatabaseHelper dbh;
00046 private class DatabaseHelper extends SQLiteOpenHelper {
00047 DatabaseHelper(Context context) {
00048 super(context, DATABASE_NAME, null, DATABASE_VERSION);
00049 }
00053 @Override
00054 public void onCreate(SQLiteDatabase db) {
00055 Log.i("MasterChooser", "Creating database...");
00056 db.execSQL(TABLE_CREATE);
00057 }
00061 @Override
00062 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
00063 Log.i("MasterChooser", "The database is being upgraded from " + oldVersion + " to " + newVersion);
00064 }
00065 }
00066
00067 public static final Uri CONTENT_URI = Uri.parse("content://com.github.rosjava.android_apps.application_management");
00068 private static final int DATABASE_VERSION = 2;
00069 private static final String DATABASE_NAME = "robotlist_table";
00070 private static final String TABLE_NAME = "robotlist";
00071 public static final String TABLE_COLUMN = "robots";
00072 private static final String TABLE_CREATE =
00073 "CREATE TABLE " + TABLE_NAME + " (" + TABLE_COLUMN + " TEXT);";
00074 @Override
00075 public int delete(Uri arg0, String arg1, String[] arg2) {
00076 Log.e("CurrentRobotContentProvider", "Invalid method: delete");
00077 return 0;
00078 }
00079
00080 @Override
00081 public String getType(Uri uri) {
00082 Log.e("CurrentRobotContentProvider", "Invalid method: getType");
00083 return null;
00084 }
00085
00086 @Override
00087 public Uri insert(Uri uri, ContentValues values) {
00088 SQLiteDatabase db = dbh.getWritableDatabase();
00089 if (db == null) {
00090 Log.e("CurrentRobotContentProvider", "Could not get the writable database.");
00091 return null;
00092 }
00093 db.beginTransaction();
00094 if (values.get(TABLE_COLUMN) != null) {
00095 Log.i("CurrentRobotContentProvider", "Saving robot...");
00096
00097 Cursor c = db.query(TABLE_NAME, new String[] {TABLE_COLUMN, },
00098 null, new String[] {}, null, null, null);
00099 if (c.getCount() > 0) {
00100 Log.i("CurrentRobotContentProvider", "Update currently existing row");
00101 db.update(TABLE_NAME, values, null, new String[] {});
00102 } else {
00103 Log.i("CurrentRobotContentProvider", "Insert new row");
00104 if (db.insert(TABLE_NAME, null, values) < 0) {
00105 Log.e("CurrentRobotContentProvider", "Error inserting row!");
00106 }
00107 }
00108 } else {
00109 Log.i("CurrentRobotContentProvider", "Deleting saved robot...");
00110 db.delete(TABLE_NAME, null, new String[] {});
00111 }
00112
00113 db.setTransactionSuccessful();
00114 db.endTransaction();
00115 db.close();
00116 Log.i("CurrentRobotContentProvider", "Done saving current robot");
00117 return CONTENT_URI;
00118 }
00119
00120 @Override
00121 public boolean onCreate() {
00122 dbh = new DatabaseHelper(this.getContext());
00123 if(dbh == null) {
00124 return false;
00125 } else {
00126 return true;
00127 }
00128 }
00129
00130 @Override
00131 public Cursor query(Uri uri, String[] projection, String selection,
00132 String[] selectionArgs, String sortOrder) {
00133 SQLiteDatabase db = dbh.getReadableDatabase();
00134 Cursor res = db.query(TABLE_NAME, new String[] {TABLE_COLUMN, },
00135 null, new String[] {}, null, null, null);
00136
00137 return res;
00138 }
00139
00140 @Override
00141 public int update(Uri uri, ContentValues values, String selection,
00142 String[] selectionArgs) {
00143 Log.e("CurrentRobotContentProvider", "Invalid method: update");
00144
00145 return 0;
00146 }
00147 }