|
|
|
@ -43,12 +43,12 @@ public class Note {
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized long getNewNoteId(Context context, long folderId) {
|
|
|
|
|
// Create a new note in the database
|
|
|
|
|
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);
|
|
|
|
|
ContentValues values = new ContentValues();//新建values来保存信息
|
|
|
|
|
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);
|
|
|
|
|
//ContentResolver()主要是实现外部应用对ContentProvider中的数据
|
|
|
|
@ -57,6 +57,9 @@ public class Note {
|
|
|
|
|
long noteId = 0;
|
|
|
|
|
try {
|
|
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
// uri.getPathSegments().get(1) 的作用是从 Uri 对象中获取路径的分段,并返回索引为 1 的分段内容。
|
|
|
|
|
// 在这段代码中,通过调用 uri.getPathSegments() 方法获取到一个 List<String> 对象,其中包含了 Uri 路径的各个分段内容。而后使用 get(1) 方法获取到索引为 1 的分段内容。
|
|
|
|
|
// 根据代码的上下文,可以推测索引为 1 的分段内容代表新生成的笔记的 ID 号。因此,uri.getPathSegments().get(1) 的作用就是从 Uri 中解析出新生成的笔记的 ID 号,并将其作为一个长整型值返回给变量 noteId。
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
noteId = 0;
|
|
|
|
@ -64,7 +67,7 @@ public class Note {
|
|
|
|
|
if (noteId == -1) {
|
|
|
|
|
throw new IllegalStateException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
|
return noteId;
|
|
|
|
|
return noteId;//生成的新笔记的 ID 号
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Note() {
|
|
|
|
@ -72,10 +75,10 @@ public class Note {
|
|
|
|
|
mNoteData = new NoteData();
|
|
|
|
|
}//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容
|
|
|
|
|
|
|
|
|
|
public void setNoteValue(String key, String value) {
|
|
|
|
|
mNoteDiffValues.put(key, value);
|
|
|
|
|
public void setNoteValue(String key, String value) {//接受两个参数:key 和 value,分别表示属性的键和值。
|
|
|
|
|
mNoteDiffValues.put(key, value);//将 key 和 value 存储到 mNoteDiffValues 对象中
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());//修改时间为当前时间
|
|
|
|
|
}//设置数据库表格的标签属性数据
|
|
|
|
|
|
|
|
|
|
public void setTextData(String key, String value) {
|
|
|
|
@ -117,12 +120,12 @@ public class Note {
|
|
|
|
|
* {@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) {
|
|
|
|
|
null) == 0) {//笔记的 Uri,通过 ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId) 获取到特定 ID 的笔记的 Uri。
|
|
|
|
|
Log.e(TAG, "Update note error, should not happen");
|
|
|
|
|
// Do not return, fall through
|
|
|
|
|
}
|
|
|
|
|
}//如果更新操作返回的结果为 0(表示没有进行更新操作),则记录错误日志 "Update note error, should not happen",并且在注释中说明不要返回,而是继续执行后面的代码。
|
|
|
|
|
mNoteDiffValues.clear();
|
|
|
|
|
|
|
|
|
|
if (mNoteData.isLocalModified()
|
|
|
|
@ -194,11 +197,13 @@ public class Note {
|
|
|
|
|
ContentProviderOperation.Builder builder = null;//数据库的操作列表
|
|
|
|
|
|
|
|
|
|
if(mTextDataValues.size() > 0) {//有文本数据
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mTextDataId == 0) {
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);//如果有文本数据,首先将笔记的 ID(noteId)存储到 mTextDataValues 中
|
|
|
|
|
if (mTextDataId == 0) {//如果是 0,表示当前没有对应的文本数据记录,需要进行插入操作。
|
|
|
|
|
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
|
|
|
|
|
//在插入操作中,设置 mTextDataValues 的 MIME 类型为 TextNote.CONTENT_ITEM_TYPE,表示文本类型的数据。
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
|
|
mTextDataValues);
|
|
|
|
|
mTextDataValues);//通过 context.getContentResolver().insert() 方法将 mTextDataValues 插入到 DataColumns 表中,并返回插入数据的 Uri。
|
|
|
|
|
//尝试从插入返回的 Uri 中获取新插入数据的 ID,并将其转换为 Long 类型,并存储到 mTextDataId 中。若转换失败,记录错误日志并清空 mTextDataValues,然后返回 null。
|
|
|
|
|
try {
|
|
|
|
|
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
@ -206,21 +211,25 @@ public class Note {
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else {//如果 mTextDataId 不为 0,则表示已存在对应的文本数据记录,需要进行更新操作。
|
|
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mTextDataId));
|
|
|
|
|
Notes.CONTENT_DATA_URI, mTextDataId));//通过 ContentUris.withAppendedId() 获取特定 ID 的文本数据记录的 Uri,并设置更新值为 mTextDataValues。
|
|
|
|
|
builder.withValues(mTextDataValues);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
operationList.add(builder.build());//将更新操作添加到 operationList 中。
|
|
|
|
|
}
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
mTextDataValues.clear();//清空 mTextDataValues。
|
|
|
|
|
}//把文本数据存入DataColumns
|
|
|
|
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {//有电话号码
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mCallDataId == 0) {
|
|
|
|
|
if(mCallDataValues.size() > 0) {//有电话号码。用于将电话号码数据存储到数据库的 DataColumns 表中
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);//如果有电话号码数据,首先将笔记的 ID(noteId)存储到 mCallDataValues 中。
|
|
|
|
|
if (mCallDataId == 0) {//判断 mCallDataId 是否为 0。如果是 0,表示当前没有对应的电话号码数据记录,需要进行插入操作。
|
|
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
|
|
mCallDataValues);
|
|
|
|
|
//在插入操作中,设置 mCallDataValues 的 MIME 类型为 CallNote.CONTENT_ITEM_TYPE,表示电话号码类型的数据。
|
|
|
|
|
// 然后通过 context.getContentResolver().insert() 方法将 mCallDataValues 插入到 DataColumns 表中,并返回插入数据的 Uri。
|
|
|
|
|
|
|
|
|
|
//尝试从插入返回的 Uri 中获取新插入数据的 ID,并将其转换为 Long 类型,并存储到 mCallDataId 中。若转换失败,记录错误日志并清空 mCallDataValues,然后返回 null。
|
|
|
|
|
try {
|
|
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
@ -228,16 +237,19 @@ public class Note {
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else {//如果 mCallDataId 不为 0,则表示已存在对应的电话号码数据记录,需要进行更新操作。
|
|
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mCallDataId));
|
|
|
|
|
Notes.CONTENT_DATA_URI, mCallDataId));//创建一个 ContentProviderOperation.Builder 对象,通过 ContentUris.withAppendedId() 获取特定 ID 的电话号码数据记录的 Uri,并设置更新值为 mCallDataValues。
|
|
|
|
|
builder.withValues(mCallDataValues);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
operationList.add(builder.build());////将更新操作添加到 operationList 中。
|
|
|
|
|
}
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
mCallDataValues.clear();//清空 mCallDataValues。
|
|
|
|
|
}//把电话号码数据存入DataColumns
|
|
|
|
|
//将电话号码数据与笔记关联起来,方便后续读取和展示。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (operationList.size() > 0) {//总长大于0
|
|
|
|
|
// 在 try-catch 块中,捕获可能发生的 RemoteException 和 OperationApplicationException 异常。
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = context.getContentResolver().applyBatch(
|
|
|
|
|
Notes.AUTHORITY, operationList);
|
|
|
|
|