张鑫朗
6p0 2 days ago
parent a5d5f7ba90
commit dd10662994

@ -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<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
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(),
Loading…
Cancel
Save