You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
8.5 KiB
212 lines
8.5 KiB
|
|
/*
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package net.micode.notes.data;
|
|
|
|
import android.app.SearchManager;
|
|
import android.content.ContentProvider;
|
|
import android.content.ContentUris;
|
|
import android.content.ContentValues;
|
|
import android.content.Intent;
|
|
import android.content.UriMatcher;
|
|
import android.database.Cursor;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.net.Uri;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import net.micode.notes.R;
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
// URI Matcher 用于将 URI 映射到相应的操作
|
|
private static final UriMatcher mMatcher;
|
|
|
|
// 数据库帮助类实例
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
// 日志标签
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
// 定义常量用于 URI 匹配
|
|
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;
|
|
|
|
// 静态代码块,初始化 URI Matcher
|
|
static {
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
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); // 匹配搜索建议
|
|
}
|
|
|
|
// 搜索结果的投影(投影是查询中返回的列的集合)
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + ","
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
|
|
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
// 搜索的 SQL 查询
|
|
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
|
|
+ " FROM " + TABLE.NOTE
|
|
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
@Override
|
|
public boolean onCreate() {
|
|
// 初始化 NotesDatabaseHelper 实例
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 查询操作,支持不同 URI 对应不同的查询行为。
|
|
*
|
|
* @param uri 查询 URI
|
|
* @param projection 要查询的列
|
|
* @param selection 查询条件
|
|
* @param selectionArgs 查询条件的参数
|
|
* @param sortOrder 排序方式
|
|
* @return 查询结果的 Cursor
|
|
*/
|
|
@Override
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
String sortOrder) {
|
|
Cursor c = null;
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
String id = null;
|
|
switch (mMatcher.match(uri)) {
|
|
case URI_NOTE:
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
sortOrder);
|
|
break;
|
|
case URI_NOTE_ITEM:
|
|
id = uri.getPathSegments().get(1);
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
break;
|
|
case URI_DATA:
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
sortOrder);
|
|
break;
|
|
case URI_DATA_ITEM:
|
|
id = uri.getPathSegments().get(1);
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
break;
|
|
case URI_SEARCH:
|
|
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) {
|
|
searchString = uri.getPathSegments().get(1);
|
|
}
|
|
} else {
|
|
searchString = uri.getQueryParameter("pattern");
|
|
}
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
searchString = String.format("%%%s%%", searchString);
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
new String[] { searchString });
|
|
} catch (IllegalStateException ex) {
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
}
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
}
|
|
if (c != null) {
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
}
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* 插入操作,支持 URI 匹配后将数据插入到相应表中。
|
|
*
|
|
* @param uri 插入 URI
|
|
* @param values 插入的数据
|
|
* @return 插入后的 URI
|
|
*/
|
|
@Override
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
switch (mMatcher.match(uri)) {
|
|
case URI_NOTE:
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
break;
|
|
case URI_DATA:
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
} else {
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
}
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
}
|
|
// 通知 URI 的变化
|
|
if (noteId > 0) {
|
|
getContext().getContentResolver().notifyChange(
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
}
|
|
|
|
if (dataId > 0) {
|
|
getContext().getContentResolver().notifyChange(
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
}
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
}
|
|
|
|
/**
|
|
* 删除操作,支持 URI 匹配后从相应表中删除数据。
|
|
*
|
|
* @param uri 删除的 URI
|
|
* @param selection 删除条件
|
|
* @param selectionArgs 删除条件的参数
|
|
* @return 删除的行数
|
|
*/
|
|
|