Database.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * Copyright (c) 2013, Yujin Robot.
00006  *
00007  * All rights reserved.
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  *  * Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  *  * Redistributions in binary form must reproduce the above
00015  *    copyright notice, this list of conditions and the following
00016  *    disclaimer in the documentation and/or other materials provided
00017  *    with the distribution.
00018  *  * Neither the name of Willow Garage, Inc. nor the names of its
00019  *    contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *   
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  * POSSIBILITY OF SUCH DAMAGE.
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         //db.close();
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         // TODO Auto-generated method stub
00152         return 0;
00153     }
00154 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24