|
|
@ -36,12 +36,16 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
// 创建一个UriMatcher对象,用于匹配URI
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个NotesDatabaseHelper对象,用于操作数据库
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个TAG,用于标记日志
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个URI码,用于表示不同的URI
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
@ -50,6 +54,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 静态初始化,创建一个UriMatcher对象,用于匹配URI
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
@ -81,6 +86,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
|
|
|
// 获取NotesDatabaseHelper实例
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -88,99 +94,136 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
String sortOrder) {
|
|
|
|
String sortOrder) {
|
|
|
|
|
|
|
|
// 初始化游标
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
|
|
|
|
// 获取可读的数据库
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
|
|
|
// 初始化id
|
|
|
|
String id = null;
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
// 根据uri匹配模式
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
|
|
|
// 查询note表
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 查询note表,并设置查询条件
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
sortOrder);
|
|
|
|
sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 查询note表中的某一行
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
// 获取id
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
// 查询note表,并设置查询条件
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 查询data表
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
// 查询data表,并设置查询条件
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
sortOrder);
|
|
|
|
sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 查询data表中的某一行
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
// 获取id
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
// 查询data表,并设置查询条件
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 查询搜索建议
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
|
|
|
// 检查排序,查询条件,查询参数是否有效
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化搜索字符串
|
|
|
|
String searchString = null;
|
|
|
|
String searchString = null;
|
|
|
|
|
|
|
|
// 根据uri匹配模式,获取搜索字符串
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
|
|
|
|
// 如果uri中包含路径段,则获取路径段
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 从uri中获取查询参数
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果搜索字符串为空,则返回空游标
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 将搜索字符串格式化
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
|
|
|
|
// 查询note表,并设置查询条件
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
new String[] { searchString });
|
|
|
|
new String[] { searchString });
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 未知uri
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果游标不为空,则设置通知uri
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回游标
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
|
|
|
// 获取可写的数据库
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
// 定义变量
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
|
|
|
|
// 根据uri匹配
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
|
|
|
// 匹配到note uri
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 插入note
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 匹配到data uri
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
// 判断note id是否存在
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 插入data
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 匹配到其他uri
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Notify the note uri
|
|
|
|
// Notify the note uri
|
|
|
|
|
|
|
|
// 通知note uri
|
|
|
|
if (noteId > 0) {
|
|
|
|
if (noteId > 0) {
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
// Notify the data uri
|
|
|
|
|
|
|
|
// 通知data uri
|
|
|
|
if (dataId > 0) {
|
|
|
|
if (dataId > 0) {
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 返回插入的id
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
@ -235,21 +278,27 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
boolean updateData = false;
|
|
|
|
boolean updateData = false;
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 增加版本号
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
// 获取id
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
// 增加版本号
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
// 更新数据
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
// 获取id
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
// 更新数据
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
@ -260,18 +309,22 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
if (updateData) {
|
|
|
|
if (updateData) {
|
|
|
|
|
|
|
|
// 通知更新数据
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 通知更新笔记
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
|
|
|
|
// 解析选择语句,返回一个字符串
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
|
|
|
|
// 增加笔记的版本
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
@ -299,7 +352,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
// 获取URI的类型
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|