Signed-off-by: lilium-saber <WR364999428@hotmail.com>
DUANYIQUN_BRANCH
lilium-saber 2 years ago
parent 0444a65d4e
commit b12b9a8449

@ -25,49 +25,49 @@ import android.util.Log;
import java.util.HashMap;
public class Contact {//连接类
private static HashMap<String, String> sContactCache;//建立哈希表
private static final String TAG = "Contact";//设置TAG的值为'连接'
public class Contact { //连接类
private static HashMap<String, String> sContactCache; //定义连接哈希表
private static final String TAG = "Contact"; //设置TAG的值为'连接'
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 = '+')";
+ " WHERE min_match = '+')"; //SQL语句
public static String getContact(Context context, String phoneNumber) {
public static String getContact(Context context, String phoneNumber) { //获取连接
if(sContactCache == null) {
sContactCache = new HashMap<String, String>();
sContactCache = new HashMap<String, String>(); //连接哈希表为空时创建连接哈希表
}
if(sContactCache.containsKey(phoneNumber)) {
return sContactCache.get(phoneNumber);
return sContactCache.get(phoneNumber); //连接哈希表存在电话号码时获取值
}
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber));
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将电话号码进行函数处理并代替'+'符号
Cursor cursor = context.getContentResolver().query( //用于查询的结果集
Data.CONTENT_URI, //通话记录uri
new String [] { Phone.DISPLAY_NAME },
selection,
new String[] { phoneNumber },
null);
null); //查询指定号码相关联系人名字
if (cursor != null && cursor.moveToFirst()) {
try {
String name = cursor.getString(0);
sContactCache.put(phoneNumber, name);
try { //有结果时
String name = cursor.getString(0); //获取联系人名字
sContactCache.put(phoneNumber, name); //缓存电话号码与联系人名字
return name;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, " Cursor get string error " + e.toString());
Log.e(TAG, " Cursor get string error " + e.toString()); //日志记录信息
return null;
} finally {
cursor.close();
cursor.close(); //关闭结果集
}
} else {
Log.d(TAG, "No contact matched with number:" + phoneNumber);
return null;
Log.d(TAG, "No contact matched with number:" + phoneNumber); //日志记录信息
return null; //没找到返回NULL
}
}
}

@ -17,9 +17,9 @@
package net.micode.notes.data;
import android.net.Uri;
public class Notes {
public static final String AUTHORITY = "micode_notes";
public static final String TAG = "Notes";
public class Notes { //定义类,为模块的其他类提供变量定义
public static final String AUTHORITY = "micode_notes"; //设置AUTHORITY
public static final String TAG = "Notes"; //设置TAG
public static final int TYPE_NOTE = 0;
public static final int TYPE_FOLDER = 1;
public static final int TYPE_SYSTEM = 2;
@ -46,7 +46,7 @@ public class Notes {
public static final int TYPE_WIDGET_2X = 0;
public static final int TYPE_WIDGET_4X = 1;
public static class DataConstants {
public static class DataConstants { //获取记录
public static final String NOTE = TextNote.CONTENT_ITEM_TYPE;
public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE;
}
@ -54,14 +54,14 @@ public class Notes {
/**
* Uri to query all notes and folders
*/
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");
public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note");//查询所有笔记与文件的uri
/**
* Uri to query data
*/
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");
public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data");//查询数据的uri
public interface NoteColumns {
public interface NoteColumns { //定义便签变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
@ -72,50 +72,50 @@ public class Notes {
* The parent's id for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String PARENT_ID = "parent_id";
public static final String PARENT_ID = "parent_id"; //父文件夹的id
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String CREATED_DATE = "created_date";
public static final String CREATED_DATE = "created_date"; //表示文件夹或便签创建日期
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
*/
public static final String MODIFIED_DATE = "modified_date";
public static final String MODIFIED_DATE = "modified_date"; //便签的最新的修改日期
/**
* Alert date
* <P> Type: INTEGER (long) </P>
*/
public static final String ALERTED_DATE = "alert_date";
public static final String ALERTED_DATE = "alert_date"; //提醒日期
/**
* Folder's name or text content of note
* <P> Type: TEXT </P>
*/
public static final String SNIPPET = "snippet";
public static final String SNIPPET = "snippet"; //文件夹名称或便签内容
/**
* Note's widget id
* <P> Type: INTEGER (long) </P>
*/
public static final String WIDGET_ID = "widget_id";
public static final String WIDGET_ID = "widget_id"; //便签的widget的id
/**
* Note's widget type
* <P> Type: INTEGER (long) </P>
*/
public static final String WIDGET_TYPE = "widget_type";
public static final String WIDGET_TYPE = "widget_type"; //便签widget的类型
/**
* Note's background color's id
* <P> Type: INTEGER (long) </P>
*/
public static final String BG_COLOR_ID = "bg_color_id";
public static final String BG_COLOR_ID = "bg_color_id"; //背景颜色代号
/**
* For text note, it doesn't has attachment, for multi-media
@ -128,31 +128,31 @@ public class Notes {
* Folder's count of notes
* <P> Type: INTEGER (long) </P>
*/
public static final String NOTES_COUNT = "notes_count";
public static final String NOTES_COUNT = "notes_count"; //文件夹内的便签数
/**
* The file type: folder or note
* <P> Type: INTEGER </P>
*/
public static final String TYPE = "type";
public static final String TYPE = "type"; //文件类型,是文件夹或便签
/**
* The last sync id
* <P> Type: INTEGER (long) </P>
*/
public static final String SYNC_ID = "sync_id";
public static final String SYNC_ID = "sync_id"; //最后一次同步id
/**
* Sign to indicate local modified or not
* <P> Type: INTEGER </P>
*/
public static final String LOCAL_MODIFIED = "local_modified";
public static final String LOCAL_MODIFIED = "local_modified"; //本地修改名称标识
/**
* Original parent id before moving into temporary folder
* <P> Type : INTEGER </P>
*/
public static final String ORIGIN_PARENT_ID = "origin_parent_id";
public static final String ORIGIN_PARENT_ID = "origin_parent_id"; //移动文件时文件的前父类文件夹
/**
* The gtask id
@ -164,10 +164,10 @@ public class Notes {
* The version code
* <P> Type : INTEGER (long) </P>
*/
public static final String VERSION = "version";
public static final String VERSION = "version"; //版本名称
}
public interface DataColumns {
public interface DataColumns { //定义数据变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
@ -184,25 +184,25 @@ public class Notes {
* The reference id to note that this data belongs to
* <P> Type: INTEGER (long) </P>
*/
public static final String NOTE_ID = "note_id";
public static final String NOTE_ID = "note_id"; //便签名称
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
*/
public static final String CREATED_DATE = "created_date";
public static final String CREATED_DATE = "created_date"; //创建文件夹或者便签的名称
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
*/
public static final String MODIFIED_DATE = "modified_date";
public static final String MODIFIED_DATE = "modified_date"; //最后修改日期
/**
* Data's content
* <P> Type: TEXT </P>
*/
public static final String CONTENT = "content";
public static final String CONTENT = "content"; //便签内容数据
/**
@ -210,7 +210,7 @@ public class Notes {
* integer data type
* <P> Type: INTEGER </P>
*/
public static final String DATA1 = "data1";
public static final String DATA1 = "data1"; //不同类型的通用数据列,用于可能的存储与查找
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
@ -244,17 +244,17 @@ public class Notes {
public static final class TextNote implements DataColumns {
/**
* Mode to indicate the text in check list mode or not
* <P> Type: Integer 1:check list mode 0: normal mode </P>
* <P> Type: Integer 1:check list mode 0: normal mode </P> //1为列表检查模式0为普通模式
*/
public static final String MODE = DATA1;
public static final String MODE = DATA1; //模式
public static final int MODE_CHECK_LIST = 1;
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");
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note");//访问数据表uri
}
public static final class CallNote implements DataColumns {
@ -262,18 +262,18 @@ public class Notes {
* Call date for this record
* <P> Type: INTEGER (long) </P>
*/
public static final String CALL_DATE = DATA1;
public static final String CALL_DATE = DATA1; //呼叫数据
/**
* Phone number for this record
* <P> Type: TEXT </P>
*/
public static final String PHONE_NUMBER = DATA3;
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");
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");//访问电话记录uri
}
}

@ -26,23 +26,23 @@ import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
//引入Notes中接口
public class NotesDatabaseHelper extends SQLiteOpenHelper { //该类为便签SQL表的定义以及操作便于进行文件夹与便签的各种操作例如添加或删除的行为
private static final String DB_NAME = "note.db"; //数据库名称
public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "note.db";
private static final int DB_VERSION = 4; //数据库版本
private static final int DB_VERSION = 4;
public interface TABLE { //定义一个数据库表接口
public static final String NOTE = "note"; //便签名
public interface TABLE {
public static final String NOTE = "note";
public static final String DATA = "data";
public static final String DATA = "data"; //便签数据
}
private static final String TAG = "NotesDatabaseHelper";
private static final String TAG = "NotesDatabaseHelper"; //日志类型
private static NotesDatabaseHelper mInstance;
private static NotesDatabaseHelper mInstance; //创建NotesDatabaseHelper类对象
private static final String CREATE_NOTE_TABLE_SQL =
private static final String CREATE_NOTE_TABLE_SQL = //SQL语句创建便签表包含ID、最后修改日期、背景颜色、便签数等在Notes类中的内容
"CREATE TABLE " + TABLE.NOTE + "(" +
NoteColumns.ID + " INTEGER PRIMARY KEY," +
NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," +
@ -63,7 +63,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" +
")";
private static final String CREATE_DATA_TABLE_SQL =
private static final String CREATE_DATA_TABLE_SQL = //创建数据表的SQL语句
"CREATE TABLE " + TABLE.DATA + "(" +
DataColumns.ID + " INTEGER PRIMARY KEY," +
DataColumns.MIME_TYPE + " TEXT NOT NULL," +
@ -80,7 +80,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
"CREATE INDEX IF NOT EXISTS note_id_index ON " +
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";
TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; //创建查询操作表索引,用于查找
/**
* Increase folder's note count when move note to the folder
@ -92,7 +92,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END";
" END"; //创建触发器,方便进行操作,当移动便签时统计新的父文件夹内便签数量
/**
* Decrease folder's note count when move note from folder
@ -105,7 +105,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0" + ";" +
" END";
" END"; //与上一个方法作用相反,统计旧的父文件夹便签数量
/**
* Increase folder's note count when insert new note to the folder
@ -117,7 +117,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + " + 1" +
" WHERE " + NoteColumns.ID + "=new." + NoteColumns.PARENT_ID + ";" +
" END";
" END"; //创建便签时增加统计数量
/**
* Decrease folder's note count when delete note from the folder
@ -130,7 +130,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" SET " + NoteColumns.NOTES_COUNT + "=" + NoteColumns.NOTES_COUNT + "-1" +
" WHERE " + NoteColumns.ID + "=old." + NoteColumns.PARENT_ID +
" AND " + NoteColumns.NOTES_COUNT + ">0;" +
" END";
" END"; //删除时减少数量
/**
* Update note's content when insert data with type {@link DataConstants#NOTE}
@ -143,7 +143,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END";
" END"; //更新便签数据时更改table表内容
/**
* Update note's content when data with {@link DataConstants#NOTE} type has changed
@ -156,7 +156,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=new." + DataColumns.CONTENT +
" WHERE " + NoteColumns.ID + "=new." + DataColumns.NOTE_ID + ";" +
" END";
" END"; //改变便签数据时更改table表内容
/**
* Update note's content when data with {@link DataConstants#NOTE} type has deleted
@ -169,7 +169,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.SNIPPET + "=''" +
" WHERE " + NoteColumns.ID + "=old." + DataColumns.NOTE_ID + ";" +
" END";
" END"; //删除便签数据时改变table表内容
/**
* Delete datas belong to note which has been deleted
@ -180,7 +180,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" BEGIN" +
" DELETE FROM " + TABLE.DATA +
" WHERE " + DataColumns.NOTE_ID + "=old." + NoteColumns.ID + ";" +
" END";
" END"; //删除便签时删除便签内容
/**
* Delete notes belong to folder which has been deleted
@ -191,7 +191,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" BEGIN" +
" DELETE FROM " + TABLE.NOTE +
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END";
" END"; //删除文件夹时删除内部的便签
/**
* Move notes belong to folder which has been moved to trash folder
@ -204,20 +204,20 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" UPDATE " + TABLE.NOTE +
" SET " + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" WHERE " + NoteColumns.PARENT_ID + "=old." + NoteColumns.ID + ";" +
" END";
" 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");
reCreateNoteTableTriggers(db); //重新创建触发器
createSystemFolder(db); //创建系统文件夹
Log.d(TAG, "note table has been created"); //创建一个新的便签表,返回日志
}
private void reCreateNoteTableTriggers(SQLiteDatabase db) {
private void reCreateNoteTableTriggers(SQLiteDatabase db) { //数据库对象DB
db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update");
db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_delete");
@ -233,17 +233,17 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL(NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER);
db.execSQL(FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER);
db.execSQL(FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER);
}
} //用于执行SQL语句运行重新创建便签表触发器函数
private void createSystemFolder(SQLiteDatabase db) {
ContentValues values = new ContentValues();
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);
db.insert(TABLE.NOTE, null, values); //为通讯便签创建记录文件夹,并添加键值对
/**
* root folder which is default folder
@ -251,7 +251,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_ROOT_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
db.insert(TABLE.NOTE, null, values); //创建根文件夹,是默认的、最终的父文件夹
/**
* temporary folder which is used for moving note
@ -259,7 +259,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_TEMPARAY_FOLDER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
db.insert(TABLE.NOTE, null, values); //为移动中的便签创建的临时文件夹
/**
* create trash folder
@ -267,24 +267,24 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.clear();
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
db.insert(TABLE.NOTE, null, values); //创建垃圾桶
}
public void createDataTable(SQLiteDatabase db) {
db.execSQL(CREATE_DATA_TABLE_SQL);
public void createDataTable(SQLiteDatabase db) { //创建数据表
db.execSQL(CREATE_DATA_TABLE_SQL); //SQL输入保存的语句
reCreateDataTableTriggers(db);
db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
Log.d(TAG, "data table has been created");
Log.d(TAG, "data table has been created"); //日志记录
}
private void reCreateDataTableTriggers(SQLiteDatabase db) {
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("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);
db.execSQL(DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER); //重新创建被删除的触发器
}
static synchronized NotesDatabaseHelper getInstance(Context context) {
@ -292,16 +292,16 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
mInstance = new NotesDatabaseHelper(context);
}
return mInstance;
}
} //静态同步方法用于返回NotesDatabaseHelper的对象实例mInstance
@Override
public void onCreate(SQLiteDatabase db) {
createNoteTable(db);
createDataTable(db);
}
createNoteTable(db); //创建便签表
createDataTable(db); //创建数据表
} //用于初始化表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //用于检查并升级数据库版本
boolean reCreateTriggers = false;
boolean skipV2 = false;
@ -309,31 +309,31 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
upgradeToV2(db);
skipV2 = true; // this upgrade including the upgrade from v2 to v3
oldVersion++;
}
} //检测到版本为1升级版本
if (oldVersion == 2 && !skipV2) {
upgradeToV3(db);
reCreateTriggers = true;
oldVersion++;
reCreateTriggers = true; //重建触发器标志
oldVersion++; //检测到版本为2升级版本
}
if (oldVersion == 3) {
upgradeToV4(db);
oldVersion++;
}
} //检测到版本为3升级版本
if (reCreateTriggers) {
reCreateNoteTableTriggers(db);
reCreateDataTableTriggers(db);
}
} //进行触发器的重建
if (oldVersion != newVersion) {
throw new IllegalStateException("Upgrade notes database to version " + newVersion
+ "fails");
+ "fails"); //版本不匹配则抛出异常
}
}
private void upgradeToV2(SQLiteDatabase db) {
private void upgradeToV2(SQLiteDatabase db) { //升级版本并重建表
db.execSQL("DROP TABLE IF EXISTS " + TABLE.NOTE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE.DATA);
createNoteTable(db);
@ -353,10 +353,10 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
db.insert(TABLE.NOTE, null, values);
}
} //删除不用的触发器,并添加不为空的新列,添加键值对
private void upgradeToV4(SQLiteDatabase db) {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.VERSION
+ " INTEGER NOT NULL DEFAULT 0");
}
} //添加新的不为空的列
}

@ -36,22 +36,22 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE;
public class NotesProvider extends ContentProvider {
private static final UriMatcher mMatcher;
private static final UriMatcher mMatcher; //UriMatcher类用于分辨数据表
private NotesDatabaseHelper mHelper;
private NotesDatabaseHelper mHelper; //建立NotesDatabaseHelper类对象
private static final String TAG = "NotesProvider";
private static final String TAG = "NotesProvider"; //设置类名值
private static final int URI_NOTE = 1;
private static final int URI_NOTE = 1; //设置便签uri
private static final int URI_NOTE_ITEM = 2;
private static final int URI_DATA = 3;
private static final int URI_DATA = 3; //设置数据uri
private static final int URI_DATA_ITEM = 4;
private static final int URI_SEARCH = 5;
private static final int URI_SEARCH = 5; //设置搜索uri
private static final int URI_SEARCH_SUGGEST = 6;
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_ITEM);
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
@ -59,48 +59,48 @@ public class NotesProvider extends ContentProvider {
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);
}
} //将不同的uri与数值相匹配
/**
* 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.
*/
*/ //用0A代表\n为搜索结果换行
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;
+ "'" + 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;
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; //检索便签的sql语句在列表中搜寻。使用Notes类中的接口与变量
@Override
public boolean onCreate() {
mHelper = NotesDatabaseHelper.getInstance(getContext());
return true;
return true; //创建NotesDatabaseHelper类并返回成果标识
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
Cursor c = null;
SQLiteDatabase db = mHelper.getReadableDatabase();
String sortOrder) { //负责查询数据接受uri、返回烈、选择、排列方式
Cursor c = null; //创建空游标,表示数据库操作结果集合
SQLiteDatabase db = mHelper.getReadableDatabase(); //获取数据库对象
String id = null;
switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { //确定uri类型
case URI_NOTE:
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
sortOrder);
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;
break; //传参对id进行查询
case URI_DATA:
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
sortOrder);
@ -112,13 +112,13 @@ public class NotesProvider extends ContentProvider {
break;
case URI_SEARCH:
case URI_SEARCH_SUGGEST:
if (sortOrder != null || projection != null) {
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 (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { //提取uri中搜索模式
if (uri.getPathSegments().size() > 1) {
searchString = uri.getPathSegments().get(1);
}
@ -133,43 +133,43 @@ public class NotesProvider extends ContentProvider {
try {
searchString = String.format("%%%s%%", searchString);
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
new String[] { searchString });
new String[] { searchString }); //在Notes表进行搜索
} 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); //抛出异常
}
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri); //在游标查询的数据变化时进行通知
} //通知uri是标识查询结果的urisetNotificationUri方法可注册通知uri与对象c相关联
return c;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = mHelper.getWritableDatabase();
public Uri insert(Uri uri, ContentValues values) { //uri表示要插入的urivalues表示要插入的数据
SQLiteDatabase db = mHelper.getWritableDatabase(); //打开或创建一个数据库
long dataId = 0, noteId = 0, insertedId = 0;
switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { //匹配uri
case URI_NOTE:
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
insertedId = noteId = db.insert(TABLE.NOTE, null, values); //将数据插入到Notes表中
break;
case URI_DATA:
if (values.containsKey(DataColumns.NOTE_ID)) {
noteId = values.getAsLong(DataColumns.NOTE_ID);
if (values.containsKey(DataColumns.NOTE_ID)) { //检查values是否含有NOTE_ID内容返回bool值
noteId = values.getAsLong(DataColumns.NOTE_ID); //关联noteId
} 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); //数据库插入新行的id返回到两个变量中
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
// Notify the note uri
if (noteId > 0) {
if (noteId > 0) { //不为零表示插入成功,
getContext().getContentResolver().notifyChange(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); //通知uri发生变化
}
// Notify the data uri
@ -178,22 +178,22 @@ public class NotesProvider extends ContentProvider {
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
}
return ContentUris.withAppendedId(uri, insertedId);
return ContentUris.withAppendedId(uri, insertedId); //返回新的uri表示插入位置
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
public int delete(Uri uri, String selection, String[] selectionArgs) { //表示要删除的uri、SQL中的WHERE与参数
int count = 0; //表示删除记录数
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase();
SQLiteDatabase db = mHelper.getWritableDatabase(); //打开数据库
boolean deleteData = false;
switch (mMatcher.match(uri)) {
switch (mMatcher.match(uri)) { //uri匹配
case URI_NOTE:
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
count = db.delete(TABLE.NOTE, selection, selectionArgs);
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; //设置WHERE
count = db.delete(TABLE.NOTE, selection, selectionArgs); //删除,并计数
break;
case URI_NOTE_ITEM:
id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); //获取id
/**
* ID that smaller than 0 is system folder which is not allowed to
* trash
@ -204,45 +204,45 @@ public class NotesProvider extends ContentProvider {
}
count = db.delete(TABLE.NOTE,
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
break;
break; //删除指定id并计数
case URI_DATA:
count = db.delete(TABLE.DATA, selection, selectionArgs);
count = db.delete(TABLE.DATA, selection, selectionArgs); //删除,并计数
deleteData = true;
break;
case URI_DATA_ITEM:
id = uri.getPathSegments().get(1);
id = uri.getPathSegments().get(1); //获取id
count = db.delete(TABLE.DATA,
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
deleteData = true;
deleteData = true; //删除指定id并计数
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); //报错
}
if (count > 0) {
if (count > 0) { //表示有删除操作通知uri发生变化
if (deleteData) {
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
}
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
return count; //返回删除的记录数
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { //表示更新的uri、values为新的列值、where以及参数
int count = 0; //更新条数
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase();
boolean updateData = false;
switch (mMatcher.match(uri)) {
SQLiteDatabase db = mHelper.getWritableDatabase(); //打开数据库
boolean updateData = false; //设置数据表更新表示
switch (mMatcher.match(uri)) { //匹配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); //提取id
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
+ parseSelection(selection), selectionArgs);
+ parseSelection(selection), selectionArgs); //增加指定的id条件
break;
case URI_DATA:
count = db.update(TABLE.DATA, values, selection, selectionArgs);
@ -255,51 +255,50 @@ public class NotesProvider extends ContentProvider {
updateData = true;
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
throw new IllegalArgumentException("Unknown URI " + uri); //抛出异常
}
if (count > 0) {
if (updateData) {
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);//通知指定uri数据更改
}
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null); //通知uri数据更改
}
return count;
}
private String parseSelection(String selection) {
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
private String parseSelection(String selection) { //检查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);
sql.append("UPDATE ");
sql.append(TABLE.NOTE);
sql.append(" SET ");
sql.append(NoteColumns.VERSION);
sql.append("=" + NoteColumns.VERSION + "+1 ");
sql.append("=" + NoteColumns.VERSION + "+1 "); //升级便签版本号
if (id > 0 || !TextUtils.isEmpty(selection)) {
if (id > 0 || !TextUtils.isEmpty(selection)) { //selection不为空或存在id则增加where条件
sql.append(" WHERE ");
}
if (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);
String selectString = id > 0 ? parseSelection(selection) : selection; //parseSelection将传入的参数中的占位符替换为实际的值
for (String args : selectionArgs) { //
selectString = selectString.replaceFirst("\\?", args); //replaceFirst选择参数数组中的值替换语句中的占位符
}
sql.append(selectString);
}
mHelper.getWritableDatabase().execSQL(sql.toString());
mHelper.getWritableDatabase().execSQL(sql.toString()); //执行SQL语句
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
public String getType(Uri uri) { //获取指定uri的MIME类型
// TODO Auto-generated method stub //表示该方法没有实现
return null;
}
}

Loading…
Cancel
Save