data model
DUANYIQUN_BRANCH
pycalf7qb 2 years ago
commit fb9d1d63d7

@ -25,21 +25,9 @@ import android.util.Log;
import java.util.HashMap;
<<<<<<< HEAD
<<<<<<< HEAD:app/src/main/java/net/micode/notes/data/Contact.java
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的值为'连接'
>>>>>>> 0444a65d4e44c509f57ad301a5a2b59200d62177:src/data/Contact.java
=======
public class Contact { //连接类
public class Contact { // 定义连接类
private static HashMap<String, String> sContactCache; // 定义连接哈希表
private static final String TAG = "Contact"; // 设置TAG的值为'连接'
>>>>>>> b12b9a8449498593360310c554053628b31cc8c9
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER
+ ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'"
@ -59,6 +47,7 @@ public class Contact {
String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); // 将电话号码进行函数处理并代替'+'符号
Cursor cursor = context.getContentResolver().query( // 用于查询的结果集
Data.CONTENT_URI, // 通话记录uri
new String[] { Phone.DISPLAY_NAME },

@ -17,7 +17,8 @@
package net.micode.notes.data;
import android.net.Uri;
public class 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;
@ -64,105 +65,138 @@ public class Notes {
public interface NoteColumns { // 定义便签变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ID = "_id";
/**
* The parent's id for note or folder
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String PARENT_ID = "parent_id"; // 父文件夹的id
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CREATED_DATE = "created_date"; // 表示文件夹或便签创建日期
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String MODIFIED_DATE = "modified_date"; // 便签的最新的修改日期
/**
* Alert date
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ALERTED_DATE = "alert_date"; // 提醒日期
/**
* Folder's name or text content of note
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String SNIPPET = "snippet"; // 文件夹名称或便签内容
/**
* Note's widget id
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String WIDGET_ID = "widget_id"; // 便签的widget的id
/**
* Note's widget type
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String WIDGET_TYPE = "widget_type"; // 便签widget的类型
/**
* Note's background color's id
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
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
* <P> Type: INTEGER </P>
* <P>
* Type: INTEGER
* </P>
*/
public static final String HAS_ATTACHMENT = "has_attachment";
/**
* Folder's count of notes
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String NOTES_COUNT = "notes_count"; // 文件夹内的便签数
/**
* The file type: folder or note
* <P> Type: INTEGER </P>
* <P>
* Type: INTEGER
* </P>
*/
public static final String TYPE = "type"; // 文件类型,是文件夹或便签
/**
* The last sync id
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String SYNC_ID = "sync_id"; // 最后一次同步id
/**
* Sign to indicate local modified or not
* <P> Type: INTEGER </P>
* <P>
* Type: INTEGER
* </P>
*/
public static final String LOCAL_MODIFIED = "local_modified"; // 本地修改名称标识
/**
* Original parent id before moving into temporary folder
* <P> Type : INTEGER </P>
* <P>
* Type : INTEGER
* </P>
*/
public static final String ORIGIN_PARENT_ID = "origin_parent_id"; // 移动文件时文件的前父类文件夹
/**
* The gtask id
* <P> Type : TEXT </P>
* <P>
* Type : TEXT
* </P>
*/
public static final String GTASK_ID = "gtask_id";
/**
* The version code
* <P> Type : INTEGER (long) </P>
* <P>
* Type : INTEGER (long)
* </P>
*/
public static final String VERSION = "version"; // 版本名称
}
@ -170,73 +204,94 @@ public class Notes {
public interface DataColumns { // 定义数据变量接口
/**
* The unique ID for a row
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String ID = "_id";
/**
* The MIME type of the item represented by this row.
* <P> Type: Text </P>
* <P>
* Type: Text
* </P>
*/
public static final String MIME_TYPE = "mime_type";
/**
* The reference id to note that this data belongs to
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String NOTE_ID = "note_id"; // 便签名称
/**
* Created data for note or folder
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CREATED_DATE = "created_date"; // 创建文件夹或者便签的名称
/**
* Latest modified date
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String MODIFIED_DATE = "modified_date"; // 最后修改日期
/**
* Data's content
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String CONTENT = "content"; // 便签内容数据
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P> Type: INTEGER </P>
* <P>
* Type: INTEGER
* </P>
*/
public static final String DATA1 = "data1"; // 不同类型的通用数据列,用于可能的存储与查找
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* integer data type
* <P> Type: INTEGER </P>
* <P>
* Type: INTEGER
* </P>
*/
public static final String DATA2 = "data2";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA3 = "data3";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA4 = "data4";
/**
* Generic data column, the meaning is {@link #MIMETYPE} specific, used for
* TEXT data type
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String DATA5 = "data5";
}
@ -244,7 +299,10 @@ 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> //1为列表检查模式0为普通模式
* <P>
* Type: Integer 1:check list mode 0: normal mode
* </P>
* //1为列表检查模式0为普通模式
*/
public static final String MODE = DATA1; // 模式
@ -260,13 +318,17 @@ public class Notes {
public static final class CallNote implements DataColumns {
/**
* Call date for this record
* <P> Type: INTEGER (long) </P>
* <P>
* Type: INTEGER (long)
* </P>
*/
public static final String CALL_DATE = DATA1; // 呼叫数据
/**
* Phone number for this record
* <P> Type: TEXT </P>
* <P>
* Type: TEXT
* </P>
*/
public static final String PHONE_NUMBER = DATA3; // 呼叫号码
@ -276,4 +338,5 @@ public class Notes {
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note");// 访问电话记录uri
}
}

@ -27,7 +27,7 @@ import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
//引入Notes中接口
public class NotesDatabaseHelper extends SQLiteOpenHelper { //该类为便签SQL表的定义以及操作便于进行文件夹与便签的各种操作,例如添加或删除的行为
public class NotesDatabaseHelper extends SQLiteOpenHelper { // 该类为便签SQL表的定义以及操作于进行文件夹与便签的各种操作,例如添加或删除的行为
private static final String DB_NAME = "note.db"; // 数据库名称
private static final int DB_VERSION = 4; // 数据库版本
@ -78,15 +78,14 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
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 " +
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
*/
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER increase_folder_count_on_update "+
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER increase_folder_count_on_update "
+
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
@ -97,8 +96,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Decrease folder's note count when move note from folder
*/
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER decrease_folder_count_on_update " +
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_update "
+
" AFTER UPDATE OF " + NoteColumns.PARENT_ID + " ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
@ -110,8 +109,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Increase folder's note count when insert new note to the folder
*/
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER =
"CREATE TRIGGER increase_folder_count_on_insert " +
private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = "CREATE TRIGGER increase_folder_count_on_insert "
+
" AFTER INSERT ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
@ -122,8 +121,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Decrease folder's note count when delete note from the folder
*/
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER =
"CREATE TRIGGER decrease_folder_count_on_delete " +
private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_delete "
+
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN " +
" UPDATE " + TABLE.NOTE +
@ -135,8 +134,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Update note's content when insert data with type {@link DataConstants#NOTE}
*/
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER =
"CREATE TRIGGER update_note_content_on_insert " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = "CREATE TRIGGER update_note_content_on_insert "
+
" AFTER INSERT ON " + TABLE.DATA +
" WHEN new." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
@ -146,10 +145,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" END"; // 更新便签数据时更改table表内容
/**
* Update note's content when data with {@link DataConstants#NOTE} type has changed
* Update note's content when data with {@link DataConstants#NOTE} type has
* changed
*/
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER =
"CREATE TRIGGER update_note_content_on_update " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = "CREATE TRIGGER update_note_content_on_update "
+
" AFTER UPDATE ON " + TABLE.DATA +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
@ -159,10 +159,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
" END"; // 改变便签数据时更改table表内容
/**
* Update note's content when data with {@link DataConstants#NOTE} type has deleted
* Update note's content when data with {@link DataConstants#NOTE} type has
* deleted
*/
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER =
"CREATE TRIGGER update_note_content_on_delete " +
private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = "CREATE TRIGGER update_note_content_on_delete "
+
" AFTER delete ON " + TABLE.DATA +
" WHEN old." + DataColumns.MIME_TYPE + "='" + DataConstants.NOTE + "'" +
" BEGIN" +
@ -174,8 +175,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Delete datas belong to note which has been deleted
*/
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER =
"CREATE TRIGGER delete_data_on_delete " +
private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = "CREATE TRIGGER delete_data_on_delete " +
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN" +
" DELETE FROM " + TABLE.DATA +
@ -185,8 +185,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Delete notes belong to folder which has been deleted
*/
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER =
"CREATE TRIGGER folder_delete_notes_on_delete " +
private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = "CREATE TRIGGER folder_delete_notes_on_delete "
+
" AFTER DELETE ON " + TABLE.NOTE +
" BEGIN" +
" DELETE FROM " + TABLE.NOTE +
@ -196,8 +196,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
/**
* Move notes belong to folder which has been moved to trash folder
*/
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER =
"CREATE TRIGGER folder_move_notes_on_trash " +
private static final String FOLDER_MOVE_NOTES_ON_TRASH_TRIGGER = "CREATE TRIGGER folder_move_notes_on_trash " +
" AFTER UPDATE ON " + TABLE.NOTE +
" WHEN new." + NoteColumns.PARENT_ID + "=" + Notes.ID_TRASH_FOLER +
" BEGIN" +
@ -349,6 +348,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
+ " TEXT NOT NULL DEFAULT ''");
// add a trash system folder
ContentValues values = new ContentValues();
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);

@ -16,7 +16,6 @@
package net.micode.notes.data;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
@ -34,13 +33,12 @@ 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操作相关类用于对数据库进行操作
private static final UriMatcher mMatcher; // UriMatcher类用于分辨数据操作类型用于匹配不同的uri
public class NotesProvider extends ContentProvider {
private static final UriMatcher mMatcher; //UriMatcher类用于分辨数据表
private NotesDatabaseHelper mHelper; //建立NotesDatabaseHelper类对象
private NotesDatabaseHelper mHelper; // 建立NotesDatabaseHelper类对象用于数据库操作
private static final String TAG = "NotesProvider"; //设置类名值
private static final String TAG = "NotesProvider"; // 设置类名标签,用于调试
private static final int URI_NOTE = 1; // 设置便签uri
private static final int URI_NOTE_ITEM = 2;
@ -62,7 +60,8 @@ public class NotesProvider extends ContentProvider {
} // 将不同的uri与数值相匹配
/**
* x'0A' represents the '\n' character in sqlite. For title and content in the search result,
* 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 + ","
@ -187,6 +186,7 @@ public class NotesProvider extends ContentProvider {
String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase(); // 打开数据库
boolean deleteData = false;
switch (mMatcher.match(uri)) { // uri匹配
case URI_NOTE:
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; // 设置WHERE
@ -218,6 +218,7 @@ public class NotesProvider extends ContentProvider {
default:
throw new IllegalArgumentException("Unknown URI " + uri); // 报错
}
if (count > 0) { // 表示有删除操作通知uri发生变化
if (deleteData) {
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);

@ -15,6 +15,7 @@
*/
package net.micode.notes.model;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
@ -33,88 +34,90 @@ import net.micode.notes.data.Notes.TextNote;
import java.util.ArrayList;
public class Note {// 便签类
private ContentValues mNoteDiffValues; // 用于保存便签修改值
private NoteData mNoteData; // 保存便签的数据
private static final String TAG = "Note"; // 设置TAG为Note
public class Note {
private ContentValues mNoteDiffValues;
private NoteData mNoteData;
private static final String TAG = "Note";
/**
* Create a new note id for adding a new note to databases
*/
public static synchronized long getNewNoteId(Context context, long folderId) {
public static synchronized long getNewNoteId(Context context, long folderId) { // 为一个新的便签创建id返回数据库
// Create a new note in the database
ContentValues values = new ContentValues();
long createdTime = System.currentTimeMillis();
ContentValues values = new ContentValues(); // 用于保存创建日期等字段的值
long createdTime = System.currentTimeMillis(); // 获取创建时间
values.put(NoteColumns.CREATED_DATE, createdTime);
values.put(NoteColumns.MODIFIED_DATE, createdTime);
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.PARENT_ID, folderId);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); // 将values插入数据库中并返回uri
long noteId = 0;
try {
noteId = Long.valueOf(uri.getPathSegments().get(1));
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 解析uri获取新笔记id
} catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString());
Log.e(TAG, "Get note id error :" + e.toString()); // 解析失败日志
noteId = 0;
}
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
throw new IllegalStateException("Wrong note id:" + noteId); // 获取id出错抛出异常
}
return noteId;
}
public Note() {
mNoteDiffValues = new ContentValues();
mNoteData = new NoteData();
mNoteDiffValues = new ContentValues(); // 创建新对象
mNoteData = new NoteData(); // 创建新对象
}
public void setNoteValue(String key, String value) {
mNoteDiffValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
public void setNoteValue(String key, String value) { // 设置便签的值
mNoteDiffValues.put(key, value); // key为属性values为新值
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 本地修改标识为1标识被修改
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 表示修改时间,用于服务器时间比较
}
public void setTextData(String key, String value) {
mNoteData.setTextData(key, value);
}
mNoteData.setTextData(key, value); // 添加键值对
} // 设置文本数据
public void setTextDataId(long id) {
mNoteData.setTextDataId(id);
}
} // 设置文本数据id
public long getTextDataId() {
return mNoteData.mTextDataId;
}
} // 获取文本数据id
public void setCallDataId(long id) {
mNoteData.setCallDataId(id);
}
} // 设置通讯数据id
public void setCallData(String key, String value) {
mNoteData.setCallData(key, value);
}
} // 设置通讯数据,添加键值对
public boolean isLocalModified() {
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
}
} // 检查是否被修改
public boolean syncNote(Context context, long noteId) {
public boolean syncNote(Context context, long noteId) { // 用于同步到数据库中
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出异常
}
if (!isLocalModified()) {
return true;
if (!isLocalModified()) { // 判断是否修改过
return true; // 没修改则不需要同步
}
/**
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
* In theory, once data changed, the note should be updated on
* {@link NoteColumns#LOCAL_MODIFIED} and
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails,
* we also update the
* note data info
*/
if (context.getContentResolver().update(
if (context.getContentResolver().update( // 打包并同步
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen");
@ -124,13 +127,13 @@ public class Note {
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false;
return false; // 表示同步失败
}
return true;
}
private class NoteData {
private class NoteData { // 私有内部类,用于便签数据管理
private long mTextDataId;
private ContentValues mTextDataValues;
@ -141,77 +144,76 @@ public class Note {
private static final String TAG = "NoteData";
public NoteData() {
mTextDataValues = new ContentValues();
mCallDataValues = new ContentValues();
public NoteData() { // 构造函数
mTextDataValues = new ContentValues(); // 保存便签文本
mCallDataValues = new ContentValues(); // 保存便签通讯记录
mTextDataId = 0;
mCallDataId = 0;
}
boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
}
} // 检查便签是否再本地修改
void setTextDataId(long id) {
if (id <= 0) {
throw new IllegalArgumentException("Text data id should larger than 0");
}
mTextDataId = id;
}
} // 设置便签文本标识符
void setCallDataId(long id) {
if (id <= 0) {
throw new IllegalArgumentException("Call data id should larger than 0");
}
mCallDataId = id;
}
} // 设置便签通讯标识符
void setCallData(String key, String value) {
mCallDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 更新修改状态
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改日期
} // 设置文本数据
void setTextData(String key, String value) {
void setTextData(String key, String value) { // 设置通讯数据
mTextDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
Uri pushIntoContentResolver(Context context, long noteId) {
Uri pushIntoContentResolver(Context context, long noteId) { // 将文本数据与通讯数据添加到context
/**
* Check for safety
*/
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
throw new IllegalArgumentException("Wrong note id:" + noteId); // 抛出异常
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = null;
if(mTextDataValues.size() > 0) {
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
if (mTextDataValues.size() > 0) { // 检查文本数据
mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 将便签id与数据值相关联
if (mTextDataId == 0) {
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); // 并将键值对添加到mTextDataValues中
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
mTextDataValues); // 插入新的数据行保存返回的uri
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
Log.e(TAG, "Insert new text data fail with noteId" + noteId); // 异常日志
mTextDataValues.clear();
return null;
}
} else {
} else { // 表示需要更新现有数据行
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues);
operationList.add(builder.build());
}
mTextDataValues.clear();
mTextDataValues.clear(); // 清空mTextDataValues
}
if(mCallDataValues.size() > 0) {
if (mCallDataValues.size() > 0) { // 检查通讯数据,代码内容功能同上
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
if (mCallDataId == 0) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
@ -233,7 +235,7 @@ public class Note {
mCallDataValues.clear();
}
if (operationList.size() > 0) {
if (operationList.size() > 0) { // 检查operationList对载入的操作进行批量提交
try {
ContentProviderResult[] results = context.getContentResolver().applyBatch(
Notes.AUTHORITY, operationList);

@ -31,38 +31,37 @@ import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote {
public class WorkingNote {// 表示工作便签
// Note for the working note
private Note mNote;
private Note mNote; // 表示工作便签
// Note Id
private long mNoteId;
private long mNoteId; // 便签id
// Note content
private String mContent;
private String mContent; // 便签内容
// Note mode
private int mMode;
private int mMode; // 便签模式
private long mAlertDate;
private long mAlertDate; // 提醒日期
private long mModifiedDate;
private long mModifiedDate; // 最近修改日期
private int mBgColorId;
private int mBgColorId; // 背景颜色id
private int mWidgetId;
private int mWidgetId; // 小部件id
private int mWidgetType;
private int mWidgetType; // 小部件类型
private long mFolderId;
private long mFolderId; // 所在文件夹id
private Context mContext;
private Context mContext; // 表示上下文
private static final String TAG = "WorkingNote";
private static final String TAG = "WorkingNote"; // 设置TAG
private boolean mIsDeleted;
private boolean mIsDeleted; // 表示是否被删除
private NoteSettingChangedListener mNoteSettingStatusListener;
private NoteSettingChangedListener mNoteSettingStatusListener; // 表示便签设置状态
public static final String[] DATA_PROJECTION = new String[] {
public static final String[] DATA_PROJECTION = new String[] { // 用于指定需要从数据库查询的数据列
DataColumns.ID,
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
@ -81,28 +80,28 @@ public class WorkingNote {
NoteColumns.MODIFIED_DATE
};
private static final int DATA_ID_COLUMN = 0;
private static final int DATA_ID_COLUMN = 0; // 表示查询数据的id列
private static final int DATA_CONTENT_COLUMN = 1;
private static final int DATA_CONTENT_COLUMN = 1; // 表示查询数据的内容列
private static final int DATA_MIME_TYPE_COLUMN = 2;
private static final int DATA_MIME_TYPE_COLUMN = 2; // 表示查询数据的MIME类型列
private static final int DATA_MODE_COLUMN = 3;
private static final int DATA_MODE_COLUMN = 3; // 表示查询数据的模式列
private static final int NOTE_PARENT_ID_COLUMN = 0;
private static final int NOTE_PARENT_ID_COLUMN = 0; // 表示查询便签的父文件夹id列
private static final int NOTE_ALERTED_DATE_COLUMN = 1;
private static final int NOTE_ALERTED_DATE_COLUMN = 1; // 表示便签的提醒日期列
private static final int NOTE_BG_COLOR_ID_COLUMN = 2;
private static final int NOTE_BG_COLOR_ID_COLUMN = 2; // 表示便签的背景颜色id列
private static final int NOTE_WIDGET_ID_COLUMN = 3;
private static final int NOTE_WIDGET_ID_COLUMN = 3; // 表示便签小组件id列
private static final int NOTE_WIDGET_TYPE_COLUMN = 4;
private static final int NOTE_WIDGET_TYPE_COLUMN = 4; // 表示便签小组件类型列
private static final int NOTE_MODIFIED_DATE_COLUMN = 5;
private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // 表示便签最后修改日期列
// New note construct
private WorkingNote(Context context, long folderId) {
private WorkingNote(Context context, long folderId) { // 新建便签的结构
mContext = context;
mAlertDate = 0;
mModifiedDate = System.currentTimeMillis();
@ -115,7 +114,7 @@ public class WorkingNote {
}
// Existing note construct
private WorkingNote(Context context, long noteId, long folderId) {
private WorkingNote(Context context, long noteId, long folderId) { // 存在的的便签结构
mContext = context;
mNoteId = noteId;
mFolderId = folderId;
@ -124,13 +123,13 @@ public class WorkingNote {
loadNote();
}
private void loadNote() {
private void loadNote() { // 查询并加载当前便签的详细信息
Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null);
null, null); // 返回一个Cursor对象
if (cursor != null) {
if (cursor.moveToFirst()) {
if (cursor.moveToFirst()) { // 将查询结构的列值返回当前便签
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
@ -141,70 +140,70 @@ public class WorkingNote {
cursor.close();
} else {
Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId); // 抛出异常表示未找到指定id的便签
}
loadNoteData();
loadNoteData(); // 加载便签内容和其他数据
}
private void loadNoteData() {
private void loadNoteData() { // 加载便签内容数据
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] {
DataColumns.NOTE_ID + "=?", new String[] { // 获取与便签id相对应的所有数据
String.valueOf(mNoteId)
}, null);
}, null); // 遍历Cursor对象所有数据
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) {
if (DataConstants.NOTE.equals(type)) { // 类型相对应则设置WorkingNote对象成员变量
mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
} else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN));
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); // 设置为Notes对象的成员变量
} else {
Log.d(TAG, "Wrong note type with type:" + type);
Log.d(TAG, "Wrong note type with type:" + type); // 抛出异常,错误的数据类型
}
} while (cursor.moveToNext());
}
cursor.close();
} else {
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); // 抛出异常,无查询结果
}
}
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId);
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, // 上下文对象、文件夹id、组件id、组件类型、默认背景颜色id
int widgetType, int defaultBgColorId) { // 创建空白便签
WorkingNote note = new WorkingNote(context, folderId); // 用上下文对象、父文件夹id创建WorkingNote
note.setBgColorId(defaultBgColorId);
note.setWidgetId(widgetId);
note.setWidgetType(widgetType);
return note;
return note; // 设置变量并返回
}
public static WorkingNote load(Context context, long id) {
public static WorkingNote load(Context context, long id) { // 用上下文对象和id创建新的WorkingNote对象
return new WorkingNote(context, id, 0);
}
public synchronized boolean saveNote() {
if (isWorthSaving()) {
if (!existInDatabase()) {
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId);
public synchronized boolean saveNote() { // 保存便签
if (isWorthSaving()) { // 判断是否保存
if (!existInDatabase()) { // 是否存在与数据库
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { // 不存在则获取id
Log.e(TAG, "Create new note fail with id:" + mNoteId); // 日志记录
return false;
}
}
mNote.syncNote(mContext, mNoteId);
mNote.syncNote(mContext, mNoteId); // 同步到数据库中
/**
* Update widget content if there exist any widget of this note
*/
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID // 诺该便签与组件关联
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
mNoteSettingStatusListener.onWidgetChanged(); // 更新组件
}
return true;
} else {
@ -214,155 +213,156 @@ public class WorkingNote {
public boolean existInDatabase() {
return mNoteId > 0;
}
} // 判断便签是否存在数据库
private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) // 不存在于数据库且为空,或者没本地修改
|| (existInDatabase() && !mNote.isLocalModified())) {
return false;
} else {
return true;
}
}
} // 判断是否值得保存
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l;
}
} // 设置便签状态监视器
public void setAlertDate(long date, boolean set) {
if (date != mAlertDate) {
mAlertDate = date;
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate));
public void setAlertDate(long date, boolean set) { // 设置提醒
if (date != mAlertDate) { // 判断传入是否相同
mAlertDate = date; // 更新
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); // 更新数据库
}
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set);
mNoteSettingStatusListener.onClockAlertChanged(date, set); // 调用监听器
}
}
public void markDeleted(boolean mark) {
public void markDeleted(boolean mark) { // 标记删除标志
mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
mNoteSettingStatusListener.onWidgetChanged(); // 已删除则更新组件
}
}
public void setBgColorId(int id) {
if (id != mBgColorId) {
mBgColorId = id;
public void setBgColorId(int id) { // 设置背景颜色
if (id != mBgColorId) { // 检查
mBgColorId = id; // 更新设置
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onBackgroundColorChanged();
mNoteSettingStatusListener.onBackgroundColorChanged(); // 通知监听器
}
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); // 更新数据库
}
}
public void setCheckListMode(int mode) {
if (mMode != mode) {
public void setCheckListMode(int mode) { // 设置便签的检查清单模式
if (mMode != mode) { // 检查
if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); // 通知
}
mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); // 更新数据库
}
}
public void setWidgetType(int type) {
public void setWidgetType(int type) { // 设置组件类型
if (type != mWidgetType) {
mWidgetType = type;
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); // 更新数据库
}
}
public void setWidgetId(int id) {
public void setWidgetId(int id) { // 设置组件id
if (id != mWidgetId) {
mWidgetId = id;
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); // 更新数据库
}
}
public void setWorkingText(String text) {
public void setWorkingText(String text) { // 设置文本
if (!TextUtils.equals(mContent, text)) {
mContent = text;
mNote.setTextData(DataColumns.CONTENT, mContent);
mNote.setTextData(DataColumns.CONTENT, mContent); // 更新
}
}
public void convertToCallNote(String phoneNumber, long callDate) {
public void convertToCallNote(String phoneNumber, long callDate) { // 转换为呼叫类型便签
mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate));
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber);
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER));
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); // 修改父文件夹id为呼叫记录id
}
public boolean hasClockAlert() {
return (mAlertDate > 0 ? true : false);
}
} // 是否设置提醒数据
public String getContent() {
return mContent;
}
} // 获取内容
public long getAlertDate() {
return mAlertDate;
}
} // 获取提醒时间
public long getModifiedDate() {
return mModifiedDate;
}
} // 获取最后修改时间
public int getBgColorResId() {
return NoteBgResources.getNoteBgResource(mBgColorId);
}
} // 获取背景资源id
public int getBgColorId() {
return mBgColorId;
}
} // 获取背景颜色id
public int getTitleBgResId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
}
} // 获取标题栏背景颜色资源id
public int getCheckListMode() {
return mMode;
}
} // 获取检查清单模式
public long getNoteId() {
return mNoteId;
}
} // 获取便签id
public long getFolderId() {
return mFolderId;
}
} // 获取父文件夹id
public int getWidgetId() {
return mWidgetId;
}
} // 获取组件id
public int getWidgetType() {
return mWidgetType;
}
} // 获取组件类型
public interface NoteSettingChangedListener {
public interface NoteSettingChangedListener { // 接口
/**
* Called when the background color of current note has just changed
*/
void onBackgroundColorChanged();
void onBackgroundColorChanged(); // 便签背景颜色变化时启动
/**
* Called when user set clock
*/
void onClockAlertChanged(long date, boolean set);
void onClockAlertChanged(long date, boolean set); // 设置提醒时间时启动
/**
* Call when user create note from widget
*/
void onWidgetChanged();
void onWidgetChanged(); // 创建组件时启动
/**
* Call when switch between check list mode and normal mode
*
* @param oldMode is previous mode before change
* @param newMode is new mode
*/
void onCheckListModeChanged(int oldMode, int newMode);
void onCheckListModeChanged(int oldMode, int newMode); // 清单模式转为笔记模式时启动
}
}

Loading…
Cancel
Save