diff --git a/src/Notes-master/src/net/micode/notes/data/Contact.java b/src/Notes-master/src/net/micode/notes/data/Contact.java index d97ac5d..0a9d168 100644 --- a/src/Notes-master/src/net/micode/notes/data/Contact.java +++ b/src/Notes-master/src/net/micode/notes/data/Contact.java @@ -13,38 +13,41 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + package net.micode.notes.data; - + import android.content.Context; import android.database.Cursor; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.Data; import android.telephony.PhoneNumberUtils; import android.util.Log; - + import java.util.HashMap; - + +// 联系人类,用于从设备中获取联系人信息 public class Contact { - private static HashMap sContactCache; - private static final String TAG = "Contact"; - + private static HashMap sContactCache; // 缓存联系人信息,以提高查询效率 + private static final String TAG = "Contact"; // 日志标签 + + // 查询联系人时使用的 SQL 选择条件,确保电话号码匹配且 MIME 类型为电话号码类型 private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Data.RAW_CONTACT_ID + " IN " + "(SELECT raw_contact_id " + " FROM phone_lookup" + " WHERE min_match = '+')"; - + + // 根据电话号码获取联系人姓名,首先检查缓存,然后查询数据库 public static String getContact(Context context, String phoneNumber) { if(sContactCache == null) { sContactCache = new HashMap(); } - + if(sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } - + String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); Cursor cursor = context.getContentResolver().query( @@ -53,7 +56,7 @@ public class Contact { selection, new String[] { phoneNumber }, null); - + if (cursor != null && cursor.moveToFirst()) { try { String name = cursor.getString(0); @@ -70,4 +73,4 @@ public class Contact { return null; } } -} +} \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/data/Notes.java b/src/Notes-master/src/net/micode/notes/data/Notes.java index f240604..539c53b 100644 --- a/src/Notes-master/src/net/micode/notes/data/Notes.java +++ b/src/Notes-master/src/net/micode/notes/data/Notes.java @@ -13,17 +13,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + package net.micode.notes.data; - + import android.net.Uri; + +// Notes 类定义了笔记应用中使用的常量和接口 public class Notes { public static final String AUTHORITY = "micode_notes"; public static final String TAG = "Notes"; public static final int TYPE_NOTE = 0; public static final int TYPE_FOLDER = 1; public static final int TYPE_SYSTEM = 2; - + /** * Following IDs are system folders' identifiers * {@link Notes#ID_ROOT_FOLDER } is default folder @@ -34,205 +36,206 @@ public class Notes { public static final int ID_TEMPARAY_FOLDER = -1; public static final int ID_CALL_RECORD_FOLDER = -2; public static final int ID_TRASH_FOLER = -3; - + public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; - + public static final int TYPE_WIDGET_INVALIDE = -1; public static final int TYPE_WIDGET_2X = 0; public static final int TYPE_WIDGET_4X = 1; - + + // DataConstants 类定义了不同类型的笔记数据常量 public static class DataConstants { public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; } - + /** * Uri to query all notes and folders */ public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); - + /** * Uri to query data */ public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); - + + // NoteColumns 接口定义了笔记和文件夹的公共列名 public interface NoteColumns { /** * The unique ID for a row *

Type: INTEGER (long)

*/ public static final String ID = "_id"; - + /** * The parent's id for note or folder *

Type: INTEGER (long)

*/ public static final String PARENT_ID = "parent_id"; - + /** * Created data for note or folder *

Type: INTEGER (long)

*/ public static final String CREATED_DATE = "created_date"; - + /** * Latest modified date *

Type: INTEGER (long)

*/ public static final String MODIFIED_DATE = "modified_date"; - - + /** * Alert date *

Type: INTEGER (long)

*/ public static final String ALERTED_DATE = "alert_date"; - + /** * Folder's name or text content of note *

Type: TEXT

*/ public static final String SNIPPET = "snippet"; - + /** * Note's widget id *

Type: INTEGER (long)

*/ public static final String WIDGET_ID = "widget_id"; - + /** * Note's widget type *

Type: INTEGER (long)

*/ public static final String WIDGET_TYPE = "widget_type"; - + /** * Note's background color's id *

Type: INTEGER (long)

*/ public static final String BG_COLOR_ID = "bg_color_id"; - + /** * For text note, it doesn't has attachment, for multi-media * note, it has at least one attachment *

Type: INTEGER

*/ public static final String HAS_ATTACHMENT = "has_attachment"; - + /** * Folder's count of notes *

Type: INTEGER (long)

*/ public static final String NOTES_COUNT = "notes_count"; - + /** * The file type: folder or note *

Type: INTEGER

*/ public static final String TYPE = "type"; - + /** * The last sync id *

Type: INTEGER (long)

*/ public static final String SYNC_ID = "sync_id"; - + /** * Sign to indicate local modified or not *

Type: INTEGER

*/ public static final String LOCAL_MODIFIED = "local_modified"; - + /** * Original parent id before moving into temporary folder *

Type : INTEGER

*/ public static final String ORIGIN_PARENT_ID = "origin_parent_id"; - + /** * The gtask id *

Type : TEXT

*/ public static final String GTASK_ID = "gtask_id"; - + /** * The version code *

Type : INTEGER (long)

*/ public static final String VERSION = "version"; } - + + // DataColumns 接口定义了笔记数据的公共列名 public interface DataColumns { /** * The unique ID for a row *

Type: INTEGER (long)

*/ public static final String ID = "_id"; - + /** * The MIME type of the item represented by this row. *

Type: Text

*/ public static final String MIME_TYPE = "mime_type"; - + /** * The reference id to note that this data belongs to *

Type: INTEGER (long)

*/ public static final String NOTE_ID = "note_id"; - + /** * Created data for note or folder *

Type: INTEGER (long)

*/ public static final String CREATED_DATE = "created_date"; - + /** * Latest modified date *

Type: INTEGER (long)

*/ public static final String MODIFIED_DATE = "modified_date"; - + /** * Data's content *

Type: TEXT

*/ public static final String CONTENT = "content"; - - + /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * integer data type *

Type: INTEGER

*/ public static final String DATA1 = "data1"; - + /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * integer data type *

Type: INTEGER

*/ public static final String DATA2 = "data2"; - + /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * TEXT data type *

Type: TEXT

*/ public static final String DATA3 = "data3"; - + /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * TEXT data type *

Type: TEXT

*/ public static final String DATA4 = "data4"; - + /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * TEXT data type @@ -240,40 +243,42 @@ public class Notes { */ public static final String DATA5 = "data5"; } - + + // TextNote 类定义了文本笔记的相关信息和 URI public static final class TextNote implements DataColumns { /** * Mode to indicate the text in check list mode or not *

Type: Integer 1:check list mode 0: normal mode

*/ public static final String MODE = DATA1; - + public static final int MODE_CHECK_LIST = 1; - + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; - + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; - + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); } - + + // CallNote 类定义了通话记录相关的信息和 URI public static final class CallNote implements DataColumns { /** * Call date for this record *

Type: INTEGER (long)

*/ public static final String CALL_DATE = DATA1; - + /** * Phone number for this record *

Type: TEXT

*/ public static final String PHONE_NUMBER = DATA3; - + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; - + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; - + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); } -} +} \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java b/src/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java index ffe5d57..eb9d445 100644 --- a/src/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java +++ b/src/Notes-master/src/net/micode/notes/data/NotesDatabaseHelper.java @@ -1,47 +1,31 @@ -/* - * 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.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; - + import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.NoteColumns; - - + +// NotesDatabaseHelper 类用于管理笔记应用的 SQLite 数据库 public class NotesDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "note.db"; - + private static final int DB_VERSION = 4; - + public interface TABLE { public static final String NOTE = "note"; - + public static final String DATA = "data"; } - + private static final String TAG = "NotesDatabaseHelper"; - + private static NotesDatabaseHelper mInstance; - + private static final String CREATE_NOTE_TABLE_SQL = "CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.ID + " INTEGER PRIMARY KEY," + @@ -62,7 +46,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + ")"; - + private static final String CREATE_DATA_TABLE_SQL = "CREATE TABLE " + TABLE.DATA + "(" + DataColumns.ID + " INTEGER PRIMARY KEY," + @@ -77,11 +61,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + ")"; - + private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = "CREATE INDEX IF NOT EXISTS note_id_index ON " + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; - + /** * Increase folder's note count when move note to the folder */ @@ -93,7 +77,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + " END"; - + /** * Decrease folder's note count when move note from folder */ @@ -106,7 +90,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + " AND " + NoteColumns.NOTES_COUNT + ">0" + ";" + " END"; - + /** * Increase folder's note count when insert new note to the folder */ @@ -118,7 +102,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" + " WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" + " END"; - + /** * Decrease folder's note count when delete note from the folder */ @@ -131,7 +115,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID + " AND " + NoteColumns.NOTES_COUNT + ">0;" + " END"; - + /** * Update note's content when insert data with type {@link DataConstants#NOTE} */ @@ -144,7 +128,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + " END"; - + /** * Update note's content when data with {@link DataConstants#NOTE} type has changed */ @@ -157,7 +141,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT + " WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" + " END"; - + /** * Update note's content when data with {@link DataConstants#NOTE} type has deleted */ @@ -170,7 +154,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.SNIPPET + "=''" + " WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" + " END"; - + /** * Delete datas belong to note which has been deleted */ @@ -181,7 +165,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " DELETE FROM " + TABLE.DATA + " WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" + " END"; - + /** * Delete notes belong to folder which has been deleted */ @@ -192,7 +176,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " DELETE FROM " + TABLE.NOTE + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " END"; - + /** * Move notes belong to folder which has been moved to trash folder */ @@ -205,18 +189,21 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { " SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER + " WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" + " END"; - + + // 构造函数,初始化数据库名称和版本 public NotesDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } - + + // 创建笔记表及其触发器,并插入系统文件夹 public void createNoteTable(SQLiteDatabase db) { db.execSQL(CREATE_NOTE_TABLE_SQL); reCreateNoteTableTriggers(db); createSystemFolder(db); Log.d(TAG, "note table has been created"); } - + + // 重新创建笔记表的触发器 private void reCreateNoteTableTriggers(SQLiteDatabase db) { db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update"); @@ -225,7 +212,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS folder_delete_notes_on_delete"); db.execSQL("DROP TRIGGER IF EXISTS folder_move_notes_on_trash"); - + db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER); db.execSQL(NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER); @@ -234,17 +221,18 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER); db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER); } - + + // 创建系统文件夹 private void createSystemFolder(SQLiteDatabase db) { ContentValues values = new ContentValues(); - + /** * call record foler for call notes */ values.put(NoteColumns.ID, Notes.ID_CALL_RECORD_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - + /** * root folder which is default folder */ @@ -252,7 +240,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - + /** * temporary folder which is used for moving note */ @@ -260,7 +248,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); - + /** * create trash folder */ @@ -269,77 +257,84 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); } - + + // 创建数据表及其触发器,并创建索引 public void createDataTable(SQLiteDatabase db) { db.execSQL(CREATE_DATA_TABLE_SQL); reCreateDataTableTriggers(db); db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL); Log.d(TAG, "data table has been created"); } - + + // 重新创建数据表的触发器 private void reCreateDataTableTriggers(SQLiteDatabase db) { db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_insert"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_update"); db.execSQL("DROP TRIGGER IF EXISTS update_note_content_on_delete"); - + db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER); db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); } - + + // 获取 NotesDatabaseHelper 的单例实例 static synchronized NotesDatabaseHelper getInstance(Context context) { if (mInstance == null) { mInstance = new NotesDatabaseHelper(context); } return mInstance; } - + + // 创建数据库时调用,初始化数据库结构 @Override public void onCreate(SQLiteDatabase db) { createNoteTable(db); createDataTable(db); } - + + // 数据库版本升级时调用,处理数据库结构的变更 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { boolean reCreateTriggers = false; boolean skipV2 = false; - + if (oldVersion == 1) { upgradeToV2(db); skipV2 = true; // this upgrade including the upgrade from v2 to v3 oldVersion++; } - + if (oldVersion == 2 && !skipV2) { upgradeToV3(db); reCreateTriggers = true; oldVersion++; } - + if (oldVersion == 3) { upgradeToV4(db); oldVersion++; } - + if (reCreateTriggers) { reCreateNoteTableTriggers(db); reCreateDataTableTriggers(db); } - + if (oldVersion != newVersion) { throw new IllegalStateException("Upgrade notes database to version " + newVersion + "fails"); } } - + + // 从版本 1 升级到版本 2,重新创建表结构 private void upgradeToV2(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE); db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA); createNoteTable(db); createDataTable(db); } - + + // 从版本 2 升级到版本 3,添加系统回收站文件夹 private void upgradeToV3(SQLiteDatabase db) { // drop unused triggers db.execSQL("DROP TRIGGER IF EXISTS update_note_modified_date_on_insert"); @@ -354,9 +349,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); db.insert(TABLE.NOTE, null, values); } - + + // 从版本 3 升级到版本 4,添加版本控制字段 private void upgradeToV4(SQLiteDatabase db) { db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0"); } -} +} \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/data/NotesProvider.java b/src/Notes-master/src/net/micode/notes/data/NotesProvider.java index edb0a60..fb30add 100644 --- a/src/Notes-master/src/net/micode/notes/data/NotesProvider.java +++ b/src/Notes-master/src/net/micode/notes/data/NotesProvider.java @@ -13,10 +13,9 @@ * 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; @@ -28,28 +27,28 @@ 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; - - + +// NotesProvider 类继承自 ContentProvider,负责处理与笔记数据相关的数据库操作 public class NotesProvider extends ContentProvider { 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 { mMatcher = new UriMatcher(UriMatcher.NO_MATCH); mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); @@ -60,7 +59,7 @@ public class NotesProvider extends ContentProvider { 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. @@ -72,19 +71,21 @@ 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; - + 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; - + + // 初始化 ContentProvider,获取数据库帮助类实例 @Override public boolean onCreate() { mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } - + + // 根据 URI 查询数据,返回 Cursor 对象 @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { @@ -116,7 +117,7 @@ public class NotesProvider extends ContentProvider { 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) { @@ -125,11 +126,11 @@ public class NotesProvider extends ContentProvider { } else { searchString = uri.getQueryParameter("pattern"); } - + if (TextUtils.isEmpty(searchString)) { return null; } - + try { searchString = String.format("%%%s%%", searchString); c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, @@ -146,7 +147,8 @@ public class NotesProvider extends ContentProvider { } return c; } - + + // 插入新的笔记或笔记数据到数据库中 @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mHelper.getWritableDatabase(); @@ -171,16 +173,17 @@ public class NotesProvider extends ContentProvider { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); } - + // Notify the data uri if (dataId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); } - + return ContentUris.withAppendedId(uri, insertedId); } - + + // 根据 URI 删除笔记或笔记数据 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; @@ -226,7 +229,8 @@ public class NotesProvider extends ContentProvider { } return count; } - + + // 根据 URI 更新笔记或笔记数据 @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; @@ -257,7 +261,7 @@ public class NotesProvider extends ContentProvider { default: throw new IllegalArgumentException("Unknown URI " + uri); } - + if (count > 0) { if (updateData) { getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); @@ -266,11 +270,13 @@ public class NotesProvider extends ContentProvider { } return count; } - + + // 处理查询条件字符串 private String parseSelection(String selection) { return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } - + + // 增加笔记的版本号 private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { StringBuilder sql = new StringBuilder(120); sql.append("UPDATE "); @@ -278,7 +284,7 @@ public class NotesProvider extends ContentProvider { sql.append(" SET "); sql.append(NoteColumns.VERSION); sql.append("=" + NoteColumns.VERSION + "+1 "); - + if (id > 0 || !TextUtils.isEmpty(selection)) { sql.append(" WHERE "); } @@ -292,14 +298,15 @@ public class NotesProvider extends ContentProvider { } sql.append(selectString); } - + mHelper.getWritableDatabase().execSQL(sql.toString()); } - + + // 返回与给定 URI 对应的 MIME 类型 @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } - -} + +} \ No newline at end of file