public class TelePhoneBookProvider extends ContentProvider {
private static final String TAG = "TelePhoneBookProvider";
private static final UriMatcher s_urlMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
private static final int URL_PHONEBOOK = 1;
private static final String DATABASE_NAME = "phonebook.db";
private static final String PHONEBOOK_TABLE = "phonebook";
private static final int VERSION_CODE = 2;
private DatabaseHelper mOpenHelper;
static {
s_urlMatcher.addURI("com.sjtc.phonebook", "contact", URL_PHONEBOOK);
}
@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();
int match = s_urlMatcher.match(uri);
switch (match) {
case URL_PHONEBOOK:
qb.setTables(PHONEBOOK_TABLE);
break;
default:
break;
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = qb.query(db, projection, selection, selectionArgs, null,
null, sortOrder);
ret.setNotificationUri(getContext().getContentResolver(), uri);
return ret;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri result = null;
int match = s_urlMatcher.match(uri);
switch (match) {
case URL_PHONEBOOK:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowID = db.insert(PHONEBOOK_TABLE, null, values);
if (rowID > 0) {
result = ContentUris.withAppendedId(uri, rowID);
}
break;
default:
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return result;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int result = 0;
int match = s_urlMatcher.match(uri);
switch (match) {
case URL_PHONEBOOK:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
result = db.delete(PHONEBOOK_TABLE, selection, selectionArgs);
break;
default:
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return result;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
int match = s_urlMatcher.match(uri);
switch (match) {
case URL_PHONEBOOK:
long ret = -1;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ret = db.update(PHONEBOOK_TABLE, values, selection,
selectionArgs);
count = (int) ret;
if (ret == 0) {
long rowID = db.insert(PHONEBOOK_TABLE, null, values);
if (rowID > 0) {
count = 1;
}
}
break;
default:
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION_CODE);
}
@Override
public void onCreate(SQLiteDatabase db) {
String columns = "(_id INTEGER PRIMARY KEY,"
+ "aid INTEGER,"
+ "name TEXT,"
+ "phone TEXT,"
+ "path TEXT)";
db.execSQL("CREATE TABLE " + PHONEBOOK_TABLE + columns);
final int FIRST_VERSION = 1;
onUpgrade(db,FIRST_VERSION,VERSION_CODE);//版本升级兼容
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG,"oldVersion="+oldVersion + "--newVersion="+newVersion);
for (int i = oldVersion; i < newVersion; i++) {
switch (i) {
case 1:
upgradeToVersion2(db);
break;
default:
break;
}
}
}
private void upgradeToVersion2(SQLiteDatabase db){
Log.e(TAG,"upgradeToVersion2");
String sql = "alter table " + PHONEBOOK_TABLE + " add column localFlag integer";
db.execSQL(sql);
}
}
}