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


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Fri Aug 28 2015 10:04:40