package
secondriver.xprovider;
import
android.content.*;
import
android.content.pm.ProviderInfo;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteException;
import
android.database.sqlite.SQLiteOpenHelper;
import
android.database.sqlite.SQLiteQueryBuilder;
import
android.net.Uri;
import
android.provider.BaseColumns;
import
android.text.TextUtils;
import
android.util.Log;
import
java.text.SimpleDateFormat;
import
java.util.Date;
/**
* Author : secondriver
* Date : 2015/11/4
*/
public
class
XProvider
extends
ContentProvider {
private
static
final
String TAG =
"XProvider"
;
public
static
final
String AUTHORITY =
"secondriver.xprovider.X_PROVIDER"
;
public
static
final
Uri AUTHORITY_URI = Uri.parse(
"content://"
+ AUTHORITY);
public
static
UriMatcher uriMatcher;
static
{
uriMatcher =
new
UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,
"note/#"
, Note.NOTE_ITEM);
uriMatcher.addURI(AUTHORITY,
"note"
, Note.NOTE_DIR);
}
private
XSQLiteHelper helper;
@Override
public
boolean
onCreate() {
helper = XSQLiteHelper.getXsqLiteHelper(getContext());
return
true
;
}
@Override
public
Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder builder =
new
SQLiteQueryBuilder();
switch
(uriMatcher.match(uri)) {
case
Note.NOTE_DIR:
break
;
case
Note.NOTE_ITEM:
builder.appendWhere(Note.ID_COLUMN +
"="
+ uri.getPathSegments().get(
1
));
break
;
default
:
throw
new
IllegalArgumentException(
"Unsupported URI :"
+ uri);
}
builder.setTables(Note.TABLE_NAME);
Cursor cursor = builder.query(helper.getReadableDatabase(), projection, selection, selectionArgs,
null
,
null
, sortOrder);
return
cursor;
}
@Override
public
String getType(Uri uri) {
switch
(uriMatcher.match(uri)) {
case
Note.NOTE_ITEM:
return
Note.CONTENT_ITEM_TYPE;
case
Note.NOTE_DIR:
return
Note.CONTENT_DIR_TYPE;
default
:
throw
new
IllegalArgumentException(
"Unsupported URI :"
+ uri);
}
}
@Override
public
Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = helper.getWritableDatabase();
switch
(uriMatcher.match(uri)) {
case
Note.NOTE_DIR:
long
noteId = db.insert(Note.TABLE_NAME,
null
, values);
if
(noteId >
0
) {
Uri newUri = ContentUris.withAppendedId(uri, noteId);
getContext().getContentResolver().notifyChange(newUri,
null
);
return
newUri;
}
else
{
return
null
;
}
default
:
throw
new
IllegalArgumentException(
"Unsupported URI :"
+ uri);
}
}
@Override
public
int
delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
int
effectRows =
0
;
switch
(uriMatcher.match(uri)) {
case
Note.NOTE_DIR:
effectRows = db.delete(Note.TABLE_NAME, selection, selectionArgs);
break
;
case
Note.NOTE_ITEM:
long
id = ContentUris.parseId(uri);
String whereClause = Note.ID_COLUMN +
"="
+ String.valueOf(id);
if
(!TextUtils.isEmpty(selection)) {
whereClause = whereClause +
" AND "
+ selection;
}
effectRows = db.delete(Note.TABLE_NAME, whereClause, selectionArgs);
break
;
default
:
throw
new
IllegalArgumentException(
"Unsupported URI :"
+ uri);
}
getContext().getContentResolver().notifyChange(uri,
null
);
return
effectRows;
}
@Override
public
int
update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
int
effectRows =
0
;
switch
(uriMatcher.match(uri)) {
case
Note.NOTE_DIR:
effectRows = db.update(Note.TABLE_NAME, values, selection, selectionArgs);
break
;
case
Note.NOTE_ITEM:
long
nodeId = ContentUris.parseId(uri);
String whereClause = Note.ID_COLUMN +
"="
+ String.valueOf(nodeId);
if
(!TextUtils.isEmpty(selection)) {
whereClause = whereClause +
" AND "
+ selection;
}
effectRows = db.update(Note.TABLE_NAME, values, whereClause, selectionArgs);
break
;
default
:
throw
new
IllegalArgumentException(
"Unsupported URI :"
+ uri);
}
return
effectRows;
}
public
static
class
XSQLiteHelper
extends
SQLiteOpenHelper {
public
static
final
String DATA_BASE_NAME =
"xprovider.sqlite"
;
public
static
final
int
DATA_BASE_VERSION =
1
;
public
static
volatile
XSQLiteHelper xsqLiteHelper;
public
static
XSQLiteHelper getXsqLiteHelper(Context context) {
if
(
null
== xsqLiteHelper) {
synchronized
(XSQLiteHelper.
class
) {
if
(
null
== xsqLiteHelper) {
xsqLiteHelper =
new
XSQLiteHelper(context, DATA_BASE_NAME,
null
, DATA_BASE_VERSION);
}
}
}
return
xsqLiteHelper;
}
public
XSQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int
version) {
super
(context, name, factory, version);
}
@Override
public
void
onCreate(SQLiteDatabase db) {
db.beginTransaction();
try
{
db.execSQL(
new
StringBuilder(
"create table if not exists "
)
.append(Note.TABLE_NAME)
.append(
"("
)
.append(Note.ID_COLUMN)
.append(
" integer primary key autoincrement, "
)
.append(Note.CONTENT_COLUMN)
.append(
" varchar, "
)
.append(Note.CREATED_COLUMN)
.append(
" varchar,"
)
.append(Note.FLAG_COLUMN)
.append(
" varchar,"
)
.append(Note.STATUS_COLUMN)
.append(
" varchar"
)
.append(
")"
)
.toString()
);
SimpleDateFormat simpleDateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
for
(
int
i =
0
; i <
50
; i++) {
ContentValues cv =
new
ContentValues();
cv.put(Note.CONTENT_COLUMN,
"Note 内容 "
+ i);
cv.put(Note.CREATED_COLUMN, simpleDateFormat.format(
new
Date()));
cv.put(Note.FLAG_COLUMN, i);
cv.put(Note.STATUS_COLUMN, i);
db.insert(Note.TABLE_NAME,
null
, cv);
}
db.setTransactionSuccessful();
}
catch
(SQLiteException e) {
Log.e(TAG, e.getMessage());
}
finally
{
db.endTransaction();
}
}
@Override
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
}
}
}