You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MINotes/Note.java

292 lines
13 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.model;
// 导入Android框架中用于内容提供者操作的相关类
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
// 导入项目中定义的与笔记数据相关的类
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;
import java.util.ArrayList;
// 定义Note类用于处理笔记的创建、更新和同步
public class Note {
// 用于存储笔记数据变化的ContentValues对象
private ContentValues mNoteDiffValues;
// 内部类NoteData的实例用于存储笔记的文本和呼叫数据
private NoteData mNoteData;
// 用于日志记录的标签
private static final String TAG = "Note";
/**
* 静态同步方法用于创建一个新的笔记ID并将新笔记添加到数据库中。
*
* @param context 上下文对象,用于访问数据库
* @param folderId 笔记所属文件夹的ID
* @return 新创建的笔记ID
*/
public static synchronized long getNewNoteId(Context context, long folderId) {
// 创建一个新的笔记条目在数据库中,并设置相关属性
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); // 父ID即文件夹ID
// 将新笔记插入数据库并获取返回的URI
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
long noteId = 0;
try {
// 从URI中解析出新创建的笔记ID
noteId = Long.valueOf(uri.getPathSegments().get(1));
} catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString());
noteId = 0;
}
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
}
return noteId;
}
// Note类的构造函数初始化ContentValues对象和NoteData对象
public Note() {
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 setTextData(String key, String value) {
mNoteData.setTextData(key, value);
}
// 设置笔记的文本数据ID
public void setTextDataId(long id) {
mNoteData.setTextDataId(id);
}
// 获取笔记的文本数据ID
public long getTextDataId() {
return mNoteData.mTextDataId;
}
// 设置笔记的呼叫数据ID
public void setCallDataId(long 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);
}
if (!isLocalModified()) {
return true; // 如果没有本地修改直接返回true
}
// 更新笔记数据,如果更新失败,则记录错误日志
if (context.getContentResolver().update(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen");
// 更新失败,继续执行
}
mNoteDiffValues.clear(); // 清除笔记差异值
// 如果NoteData有本地修改尝试将其推送到内容解析器
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false; // 如果推送失败返回false
}
return true; // 同步成功返回true
}
// 内部类NoteData用于存储笔记的文本数据和呼叫数据
private class NoteData {
private long mTextDataId; // 笔记文本数据的ID
private ContentValues mTextDataValues; // 笔记文本数据的ContentValues对象
private long mCallDataId; // 笔记呼叫数据的ID
private ContentValues mCallDataValues; // 笔记呼叫数据的ContentValues对象
private static final String TAG = "NoteData"; // 用于日志记录的标签
public NoteData() {
mTextDataValues = new ContentValues(); // 初始化文本数据的ContentValues对象
mCallDataValues = new ContentValues(); // 初始化呼叫数据的ContentValues对象
mTextDataId = 0; // 初始化文本数据ID
mCallDataId = 0; // 初始化呼叫数据ID
}
// 检查笔记数据是否在本地被修改过
boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
}
// 设置笔记的文本数据ID
void setTextDataId(long id) {
if(id <= 0) {
throw new IllegalArgumentException("Text data id should larger than 0");
}
mTextDataId = id;
}
// 设置笔记的呼叫数据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()); // 更新修改日期
}
// 设置笔记的文本数据
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) {
// 检查noteId是否有效
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) {
// 如果文本数据ID为0表示是新数据需要插入
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); // 设置文本数据ID
} catch (NumberFormatException e) {
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
mTextDataValues.clear();
return null;
}
} else {
// 如果文本数据ID不为0表示是更新数据
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues);
operationList.add(builder.build());
}
mTextDataValues.clear();
}
// 检查是否有呼叫数据需要更新。
if(mCallDataValues.size() > 0) {
// 如果有数据需要更新将笔记ID放入数据值中。
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
// 如果mCallDataId为0表示这是一个新的呼叫数据记录需要插入操作。
if (mCallDataId == 0) {
// 设置MIME类型为呼叫记录的类型。
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
// 插入新的呼叫数据记录到内容提供者并获取返回的URI。
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mCallDataValues);
// 尝试从返回的URI中解析出新的呼叫数据ID。
try {
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
// 如果解析失败记录错误日志并清除数据值返回null。
Log.e(TAG, "Insert new call data fail with noteId" + noteId);
mCallDataValues.clear();
return null;
}
} else {
// 如果mCallDataId不为0表示是更新已有的呼叫数据记录。
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mCallDataId));
// 设置更新的数据值。
builder.withValues(mCallDataValues);
// 将更新操作添加到操作列表中。
operationList.add(builder.build());
}
// 清除数据值,为下一次更新做准备。
mCallDataValues.clear();
}
// 检查操作列表是否有内容需要执行。
if (operationList.size() > 0) {
try {
// 应用批量操作。
ContentProviderResult[] results = context.getContentResolver().applyBatch(
Notes.AUTHORITY, operationList);
// 如果操作结果为空或者结果数组长度为0或者第一个结果为null返回null。
return (results == null || results.length == 0 || results[0] == null) ? null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
} catch (RemoteException e) {
// 如果发生远程异常记录错误日志并返回null。
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
} catch (OperationApplicationException e) {
// 如果操作应用异常记录错误日志并返回null。
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
}
}
// 如果操作列表为空返回null。
return null;
}
}
}