|
|
import android.content.ContentProvider;
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
|
import android.database.Cursor;
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
import android.net.Uri;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.app.SearchManager;
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
|
// 定义 NotesProvider 类
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
// UriMatcher 用于匹配 Uri
|
|
|
private static final UriMatcher mMatcher;
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
private static final int URI_NOTE = 1;
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
private static final int URI_DATA = 3;
|
|
|
private static final int URI_DATA_ITEM = 4;
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
static {
|
|
|
// 创建 UriMatcher 时,调用 UriMatcher(UriMatcher.NO_MATCH) 表示不匹配任何路径的返回码
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
// 把需要匹配 Uri 路径全部给注册上
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
|
|
|
}
|
|
|
|
|
|
// 声明 NOTES_SEARCH_PROJECTION
|
|
|
private static final String NOTES_SEARCH_PROJECTION = "……";
|
|
|
// 声明 NOTES_SNIPPET_SEARCH_QUERY
|
|
|
private static String NOTES_SNIPPET_SEARCH_QUERY = "……";
|
|
|
|
|
|
// 定义 mHelper 初始化方法
|
|
|
@Override
|
|
|
public boolean onCreate() {
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
// 查询 uri 在数据库中对应的位置
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
String sortOrder) {
|
|
|
Cursor c = null;
|
|
|
// 获取可读数据库
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
String id = null;
|
|
|
// 匹配查找 uri
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
// 对于不同的匹配值,在数据库中查找相应的条目
|
|
|
case URI_NOTE:
|
|
|
c = db.query(NotesDatabaseHelper.TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
sortOrder);
|
|
|
break;
|
|
|
// 这里省略原代码中 query 方法里的省略部分
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
// 不合法的参数异常
|
|
|
throw new IllegalArgumentException(
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
}
|
|
|
String searchString = null;
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
// getPathSegments() 方法得到一个 String 的 List,
|
|
|
// 在 uri.getPathSegments().get(1) 为第 2 个元素
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
}
|
|
|
}
|
|
|
// 这里省略原代码中 case URI_SEARCH_SUGGEST 里的省略部分
|
|
|
break;
|
|
|
}
|
|
|
return c;
|
|
|
}
|
|
|
|
|
|
// 将字符串解析成规定格式
|
|
|
private String parseSelection(String selection) {
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
}
|
|
|
|
|
|
// 更新一个 uri
|
|
|
@Override
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
int count = 0;
|
|
|
String id = null;
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
boolean updateData = false;
|
|
|
// 这里省略原代码中 update 方法里的省略部分
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
// 增加一个 noteVersion
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
sql.append("UPDATE ");
|
|
|
sql.append(NotesDatabaseHelper.TABLE.NOTE);
|
|
|
sql.append(" SET ");
|
|
|
sql.append(Notes.NoteColumns.VERSION);
|
|
|
sql.append("=" + Notes.NoteColumns.VERSION + "+1 ");
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
sql.append(" WHERE ");
|
|
|
}
|
|
|
if (id > 0) {
|
|
|
sql.append(Notes.NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
}
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
for (String args : selectionArgs) {
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
}
|
|
|
sql.append(selectString);
|
|
|
}
|
|
|
// execSQL() 方法可以执行 insert、delete、update 和 CREATE TABLE 之类有更改行为的 SQL 语句
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getType(Uri uri) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
// 这里可以添加插入数据的逻辑
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
// 这里可以添加删除数据的逻辑
|
|
|
return 0;
|
|
|
}
|
|
|
} |