diff --git a/src/net/micode/notes/model/Note.java b/src/net/micode/notes/model/Note.java index 6706cf6..c1e0b37 100644 --- a/src/net/micode/notes/model/Note.java +++ b/src/net/micode/notes/model/Note.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -14,6 +14,8 @@ * limitations under the License. */ +// 定义了一个名为Note的类,用于处理便签数据。 + package net.micode.notes.model; import android.content.ContentProviderOperation; import android.content.ContentProviderResult; @@ -33,16 +35,16 @@ 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"; + private ContentValues mNoteDiffValues; // 用于存储便签数据变化的ContentValues对象 + private NoteData mNoteData; // 用于存储便签数据的私有类NoteData的对象 + private static final String TAG = "Note"; // 用于日志标记的TAG + /** * Create a new note id for adding a new note to databases */ public static synchronized long getNewNoteId(Context context, long folderId) { - // Create a new note in the database + // 创建一个新的便签ID,用于将新的便签添加到数据库中 ContentValues values = new ContentValues(); long createdTime = System.currentTimeMillis(); values.put(NoteColumns.CREATED_DATE, createdTime); @@ -71,36 +73,44 @@ public class Note { } 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 setTextData(String key, String value) { + // 设置文本数据 mNoteData.setTextData(key, value); } public void setTextDataId(long id) { + // 设置文本数据ID mNoteData.setTextDataId(id); } public long getTextDataId() { + // 获取文本数据ID return mNoteData.mTextDataId; } public void setCallDataId(long id) { + // 设置通话数据ID mNoteData.setCallDataId(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) { + // 同步便签数据到数据库 if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); } @@ -109,11 +119,7 @@ public class Note { 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 - * note data info - */ + // 更新便签数据 if (context.getContentResolver().update( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, null) == 0) { @@ -122,6 +128,7 @@ public class Note { } mNoteDiffValues.clear(); + // 更新便签数据信息 if (mNoteData.isLocalModified() && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { return false; @@ -132,13 +139,9 @@ public class Note { private class NoteData { private long mTextDataId; - private ContentValues mTextDataValues; - private long mCallDataId; - private ContentValues mCallDataValues; - private static final String TAG = "NoteData"; public NoteData() { @@ -149,10 +152,12 @@ public class Note { } boolean isLocalModified() { + // 检查是否有本地修改 return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; } void setTextDataId(long id) { + // 设置文本数据ID if(id <= 0) { throw new IllegalArgumentException("Text data id should larger than 0"); } @@ -160,6 +165,7 @@ public class Note { } void setCallDataId(long id) { + // 设置通话数据ID if (id <= 0) { throw new IllegalArgumentException("Call data id should larger than 0"); } @@ -167,32 +173,29 @@ public class Note { } void setCallData(String key, String value) { + // 设置通话数据 mCallDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } 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) { - /** - * Check for safety - */ - if (noteId <= 0) { - throw new IllegalArgumentException("Wrong note id:" + noteId); - } - + // 将便签数据推送到内容解析器 ArrayList operationList = new ArrayList(); ContentProviderOperation.Builder builder = null; if(mTextDataValues.size() > 0) { + // 处理文本数据 mTextDataValues.put(DataColumns.NOTE_ID, noteId); if (mTextDataId == 0) { - mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + // 插入新的文本数据 Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mTextDataValues); try { @@ -203,6 +206,7 @@ public class Note { return null; } } else { + // 更新文本数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mTextDataId)); builder.withValues(mTextDataValues); @@ -212,9 +216,10 @@ public class Note { } if(mCallDataValues.size() > 0) { + // 处理通话数据 mCallDataValues.put(DataColumns.NOTE_ID, noteId); if (mCallDataId == 0) { - mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); + // 插入新的通话数据 Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mCallDataValues); try { @@ -225,6 +230,7 @@ public class Note { return null; } } else { + // 更新通话数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mCallDataId)); builder.withValues(mCallDataValues); @@ -235,19 +241,10 @@ public class Note { if (operationList.size() > 0) { try { + // 应用批量操作 ContentProviderResult[] results = context.getContentResolver().applyBatch( Notes.AUTHORITY, operationList); return (results == null || results.length == 0 || results[0] == null) ? null : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); } catch (RemoteException e) { - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); - return null; - } catch (OperationApplicationException e) { - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); - return null; - } - } - return null; - } - } -} + Log.e(TAG, String.format("%s: %s", e.toString(), \ No newline at end of file