AndroidFoundProvider.java :  » App » ownerinfo » com » noamwolf » android » androidfound » Android Open Source

Android Open Source » App » ownerinfo 
ownerinfo » com » noamwolf » android » androidfound » AndroidFoundProvider.java
package com.noamwolf.android.androidfound;

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;

import java.util.HashMap;

/**
 * Table structure for storing owner information will be:
 * 
 * tblOwner
 *   name
 *   companyName
 * 
 * @author nwolf
 */
public class AndroidFoundProvider extends ContentProvider {

  private static final String TAG = "AndroidFoundProvider";
  private static final String DATABASE_NAME = "nw_android_found.db";
  private static final int DATABASE_VERSION = 2;
  protected static final String OWNER_TABLE_NAME = "owner";
  
  private static HashMap<String, String> columnMap; 
  private DatabaseHelper openHelper;
  
  /**
   * This class helps open, create, and upgrade the database file.
   */
  private static class DatabaseHelper extends SQLiteOpenHelper {

      DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + OWNER_TABLE_NAME + " ("
            + Owner.Columns._ID + " INTEGER PRIMARY KEY,"
            + Owner.Columns.NAME + " TEXT,"
            + Owner.Columns.COMPANY_NAME + " TEXT,"
            + Owner.Columns.EMAIL + " TEXT,"
            + Owner.Columns.PHONE_NUMBER + " TEXT,"
            + Owner.Columns.OTHER_CONTACT_NAME + " TEXT,"
            + Owner.Columns.OTHER_CONTACT_NUMBER + " TEXT,"
            + Owner.Columns.IMAGE_URI + " TEXT"
            + ");");
        
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + OWNER_TABLE_NAME);
        onCreate(db);
      }
  }
  
  @Override
  public int delete(Uri arg0, String arg1, String[] arg2) {
    return 0;
  }

  @Override
  public String getType(Uri uri) {
    return Owner.Columns.CONTENT_TYPE;
  }

  @Override
  public Uri insert(Uri uri, ContentValues initialValues) {
    
    ContentValues values;
    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }
    
    SQLiteDatabase db = openHelper.getWritableDatabase();
    long rowId = db.insert(OWNER_TABLE_NAME, Owner.Columns.NAME, values);
    if (rowId > 0) {
        Uri noteUri = ContentUris.withAppendedId(Owner.Columns.CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(noteUri, null);
        return noteUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
  }

  @Override
  public boolean onCreate() {
    openHelper = 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(OWNER_TABLE_NAME);
    qb.setProjectionMap(columnMap);
    
    // Get the database and run the query
    SQLiteDatabase db = openHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);

    // Tell the cursor what uri to watch, so it knows when its source data changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
  }

  @Override
  public int update(Uri uri, ContentValues values, String whereClause,
      String[] whereArgs) {
    SQLiteDatabase db = openHelper.getWritableDatabase();
    int count;
    count = db.update(OWNER_TABLE_NAME, values, whereClause, whereArgs);
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
  }
}
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.