2009-10-22 14:26:02 +02:00
/ *
2010-01-22 15:21:33 +01:00
* Copyright ( C ) 2008 Google Inc .
2009-10-22 14:26:02 +02:00
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
*
* 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 com.openwide.android ;
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.provider.Contacts.Phones ;
2010-01-25 17:58:12 +01:00
import android.provider.ContactsContract.CommonDataKinds.Phone ;
2009-10-22 14:26:02 +02:00
import android.util.Log ;
/ * *
* Simple groups database access helper class . Defines the basic CRUD operations
2010-01-22 15:21:33 +01:00
* for the group add example , and gives the ability to list all groups as well as
2009-10-22 14:26:02 +02:00
* retrieve or modify a specific group .
*
* /
public class GroupsDbAdapter {
public static final String KEY_GROUP_NAME = " name " ;
public static final String KEY_GROUP_ROWID = " _id " ;
public static final String KEY_GROUP_TO_PHONE_ROWID = " _id " ;
public static final String KEY_GROUP_TO_PHONE_GROUPID = " gid " ;
public static final String KEY_GROUP_TO_PHONE_PHONEID = " pid " ;
private static final String TAG = " groupsDbAdapter " ;
private DatabaseHelper mDbHelper ;
private SQLiteDatabase mDb ;
/ * *
* Database creation sql statement
* /
private static final String DATABASE_GROUP_CREATE =
" create table groups (_id integer primary key autoincrement, "
+ " name text not null); " ;
private static final String DATABASE_GROUP_TO_PHONE_CREATE =
" create table group_TO_PHONE (_id integer primary key autoincrement, "
+ " gid integer not null, pid integer not null); " ;
private static final String DATABASE_NAME = " data " ;
private static final String DATABASE_GROUP_TABLE = " groups " ;
private static final String DATABASE_GROUP_TO_PHONE_TABLE = " group_TO_PHONE " ;
2010-04-07 11:03:09 +02:00
private static final int DATABASE_VERSION = 3 ;
2009-10-22 14:26:02 +02:00
private final Context mCtx ;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper ( Context context ) {
super ( context , DATABASE_NAME , null , DATABASE_VERSION ) ;
}
@Override
public void onCreate ( SQLiteDatabase db ) {
db . execSQL ( DATABASE_GROUP_CREATE ) ;
db . execSQL ( DATABASE_GROUP_TO_PHONE_CREATE ) ;
}
@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 groups " ) ;
db . execSQL ( " DROP TABLE IF EXISTS group_TO_PHONE " ) ;
onCreate ( db ) ;
}
}
/ * *
* Constructor - takes the context to allow the database to be
* opened / created
*
* @param ctx the Context within which to work
* /
public GroupsDbAdapter ( Context ctx ) {
this . mCtx = ctx ;
}
/ * *
* Open the groups database . If it cannot be opened , try to create a new
* instance of the database . If it cannot be created , throw an exception to
* signal the failure
*
* @return this ( self reference , allowing this to be chained in an
* initialization call )
* @throws SQLException if the database could be neither opened or created
* /
public GroupsDbAdapter open ( ) throws SQLException {
mDbHelper = new DatabaseHelper ( mCtx ) ;
mDb = mDbHelper . getWritableDatabase ( ) ;
return this ;
}
public void close ( ) {
mDbHelper . close ( ) ;
}
/ * *
* Create a new group using the name provided . If the group is
* successfully created return the new rowId for that group , otherwise return
* a - 1 to indicate failure .
*
* @param name the name of the group
* /
public long createGroup ( String name ) {
ContentValues initialValues = new ContentValues ( ) ;
initialValues . put ( KEY_GROUP_NAME , name ) ;
return mDb . insert ( DATABASE_GROUP_TABLE , null , initialValues ) ;
}
/ * *
* Delete the group with the given rowId
*
* @param rowId id of group to delete
* @return true if deleted , false otherwise
* /
public boolean deleteGroup ( long rowId ) {
return mDb . delete ( DATABASE_GROUP_TABLE , KEY_GROUP_ROWID + " = " + rowId , null ) > 0 & & mDb . delete ( DATABASE_GROUP_TO_PHONE_TABLE , KEY_GROUP_TO_PHONE_GROUPID + " = " + rowId , null ) > 0 ;
}
/ * *
* Return a Cursor over the list of all groups in the database
*
* @return Cursor over all groups
* /
public Cursor fetchAllGroups ( ) {
return mDb . query ( DATABASE_GROUP_TABLE , new String [ ] { KEY_GROUP_ROWID , KEY_GROUP_NAME } , null , null , null , null , KEY_GROUP_NAME ) ;
}
/ * *
* Return a Cursor positioned at the group that matches the given rowId
*
* @param rowId id of group to retrieve
* @return Cursor positioned to matching group , if found
* @throws SQLException if group could not be found / retrieved
* /
public Cursor fetchGroup ( long rowId ) throws SQLException {
Cursor mCursor =
mDb . query ( true , DATABASE_GROUP_TABLE , new String [ ] { KEY_GROUP_ROWID , KEY_GROUP_NAME } , KEY_GROUP_ROWID + " = " + rowId , null ,
null , null , null , null ) ;
if ( mCursor ! = null ) {
mCursor . moveToFirst ( ) ;
}
return mCursor ;
}
/ * *
* @param rowId id of group to update
* @param name value to set group name to
* /
public boolean updateGroup ( long rowId , String name ) {
ContentValues args = new ContentValues ( ) ;
args . put ( KEY_GROUP_NAME , name ) ;
return mDb . update ( DATABASE_GROUP_TABLE , args , KEY_GROUP_ROWID + " = " + rowId , null ) > 0 ;
}
public Cursor fetchPhonesFromGroup ( long groupId ) {
Cursor mCursor = mDb . query ( true , DATABASE_GROUP_TO_PHONE_TABLE , new String [ ] { KEY_GROUP_TO_PHONE_PHONEID } , KEY_GROUP_TO_PHONE_GROUPID + " = " + groupId , null , null , null , null , null ) ;
Cursor userCursor = null ;
int phoneIdIdx = mCursor . getColumnIndex ( KEY_GROUP_TO_PHONE_PHONEID ) ;
if ( mCursor ! = null ) {
userCursor = mCtx . getContentResolver ( ) . query ( Phones . CONTENT_URI , null , " Phones. " + Phones . _ID + " IN " + cursorToStringList ( mCursor , phoneIdIdx ) , null , Phones . NAME ) ;
}
return userCursor ;
}
2010-01-25 17:58:12 +01:00
2009-10-22 14:26:02 +02:00
public long addPhoneToGroup ( long groupId , long phoneId ) {
ContentValues initialValues = new ContentValues ( ) ;
initialValues . put ( KEY_GROUP_TO_PHONE_GROUPID , groupId ) ;
initialValues . put ( KEY_GROUP_TO_PHONE_PHONEID , phoneId ) ;
return mDb . insert ( DATABASE_GROUP_TO_PHONE_TABLE , null , initialValues ) ;
}
public boolean removePhoneToGroup ( long groupId , long phoneId ) {
return mDb . delete ( DATABASE_GROUP_TO_PHONE_TABLE , KEY_GROUP_TO_PHONE_GROUPID + " = " + groupId + " AND " + KEY_GROUP_TO_PHONE_PHONEID + " = " + phoneId , null ) > 0 ;
}
public String cursorToStringList ( Cursor cursor , int columnIdx ) {
cursor . moveToFirst ( ) ;
String list = " ( " ;
while ( ! cursor . isAfterLast ( ) ) {
list + = cursor . getString ( columnIdx ) ;
if ( ! cursor . isLast ( ) ) {
list + = " , " ;
}
cursor . moveToNext ( ) ;
}
list + = " ) " ;
return list ;
}
}