package com.example.qunzheng.todolist.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils; /**
* Created by QunZheng on 2015/3/9.
*/
public class EarthquakeProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.qunzheng.earthquakeprovider/earthquakes");
public static final String KEY_ID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_DETAILS = "details";
public static final String KEY_SUMMARY = "summary";
public static final String KEY_LOCATION_LAT = "latitude";
public static final String KEY_LOCATION_LNG = "longitude";
public static final String KEY_LINK = "link";
public static final String KEY_MAGNITUDE = "magnitude"; private EarthquakeDatabaseHelper sqLiteOpenHelper = null; private static final int ALL_ROWS = 1;
private static final int SINGLE_ROW = 2; private static UriMatcher matcher = null; static {
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("com.qunzheng.earthquakeprovider","earthquakes",ALL_ROWS);
matcher.addURI("com.qunzheng.earthquakeprovider","earthquakes/#",SINGLE_ROW);
} @Override
public boolean onCreate() {
sqLiteOpenHelper = new EarthquakeDatabaseHelper(getContext(), EarthquakeDatabaseHelper.DATABASE_NAME, null, EarthquakeDatabaseHelper.DATABASE_VERSION);
return true;
} @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase(); SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(EarthquakeDatabaseHelper.DATABASE_TABLE);
switch (matcher.match(uri)) {
case SINGLE_ROW:
String rowId = uri.getPathSegments().get(1);
queryBuilder.appendWhere(KEY_ID + "=" + rowId);
break;
} String orderBy;
if (TextUtils.isEmpty(sortOrder)) {
orderBy = KEY_DATE;
}else{
orderBy = sortOrder;
}
return queryBuilder.query(db,projection,selection,selectionArgs,null,null,orderBy);
} @Override
public String getType(Uri uri) {
switch (matcher.match(uri)) {
case ALL_ROWS:
return "vnd.android.cursor.dir/vnd.qunzheng.earthquakes";
case SINGLE_ROW:
return "vnd.android.cursor.item/vnd.qunzheng.earthquakes";
default:
throw new IllegalArgumentException("Unsupport Uri:" + uri);
}
} @Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase(); long id = db.insert(EarthquakeDatabaseHelper.DATABASE_TABLE, null, values); //数据插入成功
if (id > -1) {
Uri resultUri = ContentUris.withAppendedId(CONTENT_URI, id); getContext().getContentResolver().notifyChange(resultUri, null);
return resultUri;
}else{ }
return null;
} @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase(); switch (matcher.match(uri)) {
case SINGLE_ROW:
String id = uri.getPathSegments().get(1);
selection = KEY_ID + "=" + id +
(TextUtils.isEmpty(selection) ? "" : (" and ("+ selection +")"));
break;
} if (selection == null) {
selection = "1";
} int resultRows = db.delete(EarthquakeDatabaseHelper.DATABASE_TABLE, selection, selectionArgs); getContext().getContentResolver().notifyChange(uri, null);
return resultRows;
} @Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = sqLiteOpenHelper.getReadableDatabase(); switch (matcher.match(uri)) {
case SINGLE_ROW:
String id = uri.getPathSegments().get(1);
selection = KEY_ID + "=" + id +
(TextUtils.isEmpty(selection) ? "" : (" and ("+ selection +")"));
break;
} int resultRows = db.update(EarthquakeDatabaseHelper.DATABASE_TABLE, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return resultRows;
} private static class EarthquakeDatabaseHelper extends SQLiteOpenHelper{ public static final String DATABASE_NAME = "earthquake.db";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE = "earthQuake"; private static final String TABLE_CREATE = "create table " +
DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
KEY_DATE + " INTEGER, " +
KEY_LINK + " text, " +
KEY_LOCATION_LAT + " FLOAT, " +
KEY_LOCATION_LNG + " FLOAT, " +
KEY_SUMMARY + " TEXT, " +
KEY_MAGNITUDE + " FLOAT, " +
KEY_DETAILS + " text)"; public EarthquakeDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
}

  

05-11 17:27