|
|
|
@ -33,15 +33,14 @@ 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*1. 便签信息提供类,该类继承于ContentProvider类,由此可见该类主要用于数据的提供的功能,供其他模块实现,相当于对便签信息的管理,该类定义了相关的UriMathcher用于匹配Uri,使数据管理更加安全,并定义了一些列的方法,对该软件创建的数据库进行管理,例如,OnCreate, query,insert,update, parseSelection等方法,用于对被选中的数据对象进行相关的删除,插入,更新等操作,实现对数据库的管理,为上层的使用提供了一个较好的接口,对数据库信息进行抽象。*/
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
private static final UriMatcher mMatcher;//UriMatcher是Android提供的用来操作Uri的工具类
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
private NotesDatabaseHelper mHelper;//创建数据库帮助类实体对象
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesProvider";//该类的自定义标签
|
|
|
|
|
//1-6都是uri资源的id
|
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
@ -49,22 +48,25 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
|
|
|
|
//将以下这些变量放在static模块中,只初始化一次,提高运行速度以及减少空间浪费。将各个Uri与其代号关联在一起
|
|
|
|
|
static {
|
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);//创建uri工具类实例对象
|
|
|
|
|
//注册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);
|
|
|
|
|
//SUGGEST_URI_PATH_QUERY 并不属于URI的一部分,而应是用于指向此路径的常量
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* x'0A' represents the '\n' character in sqlite. For title and content in the search result,
|
|
|
|
|
* we will trim '\n' and white space in order to show more information.
|
|
|
|
|
* we will trim (装饰,整理)'\n' and white space in order to show more information.
|
|
|
|
|
*/
|
|
|
|
|
//声明 NOTES_SEARCH_PROJECTION,便签搜索映射,相当于搜索的信息
|
|
|
|
|
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 + ","
|
|
|
|
@ -72,116 +74,124 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
+ 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;
|
|
|
|
|
|
|
|
|
|
//note搜索查询
|
|
|
|
|
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
|
|
|
|
|
//创建一个便签数据库助手mHelper
|
|
|
|
|
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();
|
|
|
|
|
Cursor c = null;//函数返回对象
|
|
|
|
|
/*getReadableDatabase()方法是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。*/
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();//向数据库读操作
|
|
|
|
|
String id = null;
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
case URI_NOTE://匹配到标签的uri
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
sortOrder);//查询的表返回列行如何分组过滤条件,数据库查询语句
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
case URI_NOTE_ITEM://匹配到标签条目
|
|
|
|
|
id = uri.getPathSegments().get(1);//匹配到列表中便签项的路径中的1号字符
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);//查询的表返回列行如何分组过滤条件,数据库查询语句
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
case URI_DATA://匹配到数据的uri
|
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
sortOrder);//查询的表返回列行如何分组过滤条件,数据库查询语句
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
case URI_DATA_ITEM://匹配到数据条目
|
|
|
|
|
id = uri.getPathSegments().get(1);//匹配到列表中数据项的路径中的1号字符
|
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);//数据库查询语句
|
|
|
|
|
break;
|
|
|
|
|
case URI_SEARCH:
|
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
case URI_SEARCH://匹配到搜索
|
|
|
|
|
case URI_SEARCH_SUGGEST://匹配到搜索建议
|
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
|
//如果sortOrder或projection不为空抛出错误
|
|
|
|
|
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 (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {//匹配到搜索建议
|
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
|
//如果如果该路径数组大小大于一则取第一个元素
|
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//小于1则取第零个元素
|
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
|
//如果路径为空则返回空
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
/*通过上文得到的搜索字符串,进行格式加工,而后通过rawQuery()直接在“NOTES_SNIPPET_SEARCH_QUERY”数据库中搜索ID=searchString的对应信息串。*/
|
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
|
/*cursor = db.rawQuery("select name from *** where id=?", new String[]{"1"});*/
|
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
|
new String[] { searchString });
|
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());//出错抛出异常
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);//无法匹配上述的uri,抛出错误
|
|
|
|
|
}
|
|
|
|
|
if (c != null) {
|
|
|
|
|
//若getContentResolver发生变化,就接收通知
|
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
//插入一个新的uri
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
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);
|
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);//向note表插入一个uri
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {//如果数据包含主键
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);//转换为long型
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());//信息不含主键抛出异常错误
|
|
|
|
|
}
|
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);//向数据表插入一个uri
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);//不匹配的uri
|
|
|
|
|
}
|
|
|
|
|
// Notify the note uri
|
|
|
|
|
// Notify the note uri(报告note id)
|
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
|
// Notify the data uri(报告data id)
|
|
|
|
|
if (dataId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);//返回插入uri的路径
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
//删除一个uri
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
@ -189,8 +199,8 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
boolean deleteData = false;
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";//要删除的note的信息
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);//删除且返回数量
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
@ -200,11 +210,13 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
*/
|
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
// ID小于0表示是系统文件夹,不能删除
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);//删除且返回数量
|
|
|
|
|
break;
|
|
|
|
|
//以下两个与上两个相同
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
deleteData = true;
|
|
|
|
@ -216,34 +228,35 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);//不匹配的uri
|
|
|
|
|
}
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (deleteData) {
|
|
|
|
|
if (deleteData) {//如果真的删除了数据,则要求监听器对观察者发送通知
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);//对所有修改进行通知
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
return count;//返回删除数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
//更新一个uri
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
int count = 0;//更新数量
|
|
|
|
|
String id = null;
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean updateData = false;
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);//先升级版本再更新
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);//更新且返回数量
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
id = uri.getPathSegments().get(1);//获取该item的id
|
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
//与上述两个相同
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
|
updateData = true;
|
|
|
|
@ -255,23 +268,24 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);//不匹配uri
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (updateData) {
|
|
|
|
|
if (updateData) {//如果对数据进行了更新,则要求监听器对观察者发送通知
|
|
|
|
|
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) {
|
|
|
|
|
//如果selection不是空字符,那么返回" AND (" + selection + ')'如果是空字符,返回""
|
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
|
//在数据库中升级版本
|
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
@ -282,22 +296,23 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
}
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
if (id > 0) {//id>0就将其写入数据库
|
|
|
|
|
sql.append(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) {
|
|
|
|
|
public String getType(Uri uri) {//获取uri的数据类型,实际永远返回null
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|