ProfileProvider.java :  » UnTagged » ankfood » org » ank » food » Android Open Source

Android Open Source » UnTagged » ankfood 
ankfood » org » ank » food » ProfileProvider.java
/*
 * Copyright (C) 2009 AnK Team
 *   Andrey Korolev
 *   Anna Krel
 *   Anna Kapanina
 *   
 *   http://code.google.com/p/ankfood/  
 *    
     
 *    Licensed under the GNU General Public License, Version 2.0 (the "License");
 *
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *         http://www.gnu.org/licenses/gpl-2.0.html

 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,

 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */



package org.ank.food;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class ProfileProvider extends ContentProvider{

  private static final String AUTHORITY = "org.ank.food.profileprovider";
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
  private static final String DATABASE_NAME = "profiles.db";
  private static final int DATABASE_VERSION = 2;
  private static final String TABLE_NAME = "profiles";
  public static final String DEFAULT_SORT_ORDER = "name ASC";
   
  public static final String[] COLUMN_NAMES = {"_id","name","gender","age","weight",
     "height","phys_activity","prot_qouta","fat_qouta","carbo_qouta","weight_unit","height_unit"};
  public static final int ID = 0;
  public static final int NAME = 1;
  public static final int GENDER = 2;
  public static final int AGE = 3;
  public static final int WEIGHT = 4;
  public static final int HEIGHT = 5;
  public static final int PHYS_ACT = 6;
  public static final int PROT_QUOTA = 7;
  public static final int FAT_QUOTA = 8;
  public static final int CARBO_QUOTA = 9;
  public static final int WEIGHT_UNIT = 10;
  public static final int HEIGHT_UNIT = 11;
  
  
  public static final String[] COLUMN_TYPES = {"integer primary key","text","integer","integer","integer",
    "integer","integer","integer","integer","integer","integer","integer"};
   
     
  private static class DatabaseHelper extends SQLiteOpenHelper {
     
    public DatabaseHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
     }

     @Override
     public void onCreate(SQLiteDatabase db) {
       StringBuffer buf = new StringBuffer("CREATE TABLE ");
       buf.append(TABLE_NAME).append("(");
       for( int i = 0; i < COLUMN_NAMES.length-1; i++ ){
         buf.append(COLUMN_NAMES[i]).append(' ').append(COLUMN_TYPES[i]).append(", ");
       }
       buf.append(COLUMN_NAMES[COLUMN_NAMES.length-1]).append(' ').
         append(COLUMN_TYPES[COLUMN_TYPES.length-1]).append(");");
       db.execSQL( buf.toString() );
     }

     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME+";");
           onCreate(db);
     }
   }
  
   private DatabaseHelper mOpenHelper;
  
   @Override
   public int delete(Uri uri, String where, String[] whereArgs) {
     SQLiteDatabase db = mOpenHelper.getWritableDatabase();
     int a = db.delete(TABLE_NAME, where, whereArgs);
     getContext().getContentResolver().notifyChange(uri, null);
       return a;
  }

  @Override
  public String getType(Uri arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    Log.v("prof prov","inserting "+values.toString());
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(TABLE_NAME, COLUMN_NAMES[1], values);
        if (rowId >= 0) {
            Uri noteUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
  }

  @Override
  public boolean onCreate() {
    mOpenHelper = new DatabaseHelper(getContext());
        return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
    
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(TABLE_NAME);
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        
        Cursor c;
//        if (sortOrder == null)
//          c = qb.query(db, projection, selection, selectionArgs, null, null, DEFAULT_SORT_ORDER);
//        else
        c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        
        return c;
  }

  @Override
  public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int a = db.update(TABLE_NAME, values, where, whereArgs);
        getContext().getContentResolver().notifyChange(uri, null);
        return a;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.