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.
Notes-master/Note.java

301 lines
12 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.
*/
// 定义了一个名为Note的类用于表示笔记数据模型并提供与数据库交互的方法。
package net.micode.notes.model;
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;
public class Note {
// 用于存储笔记的差异值,即需要更新到数据库的字段
private ContentValues mNoteDiffValues;
// 存储笔记数据的内部类实例
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对象用于存储笔记的初始数据
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 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;
}
// 如果解析出的笔记ID不正确则抛出异常
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
}
return noteId;
}
// Note类的构造函数
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;
}
/**
* 理论上一旦数据发生变化就应该更新笔记的LOCAL_MODIFIED和MODIFIED_DATE字段。
* 为了数据安全,即使更新笔记失败,我们也应该更新笔记数据信息。
*/
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();
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false;
}
return true;
}
// 笔记数据的内部类
private class NoteData {
// 存储文本数据的ID
private long mTextDataId;
// 存储文本数据的ContentValues对象
private ContentValues mTextDataValues;
// 存储通话数据的ID
private long mCallDataId;
// 存储通话数据的ContentValues对象
private ContentValues mCallDataValues;
// 用于日志记录的标签
private static final String TAG = "NoteData";
// NoteData类的构造函数
public NoteData() {
mTextDataValues = new ContentValues();
mCallDataValues = new ContentValues();
mTextDataId = 0;
mCallDataId = 0;
}
// 检查笔记数据是否在本地被修改过
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) {
/**
* 检查参数的安全性
*/
// 检查传入的笔记ID是否有效如果笔记ID小于或等于0则抛出IllegalArgumentException异常
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
// 创建一个ArrayList来存储ContentProviderOperation对象这些对象代表要执行的数据库操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 初始化一个ContentProviderOperation.Builder对象用于构建数据库操作
ContentProviderOperation.Builder builder = null;
// 如果文本数据的ContentValues对象中有数据
if(mTextDataValues.size() > 0) {
// 将笔记ID放入文本数据的ContentValues对象中
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
// 如果文本数据ID为0表示这是一个新的文本数据需要插入新的数据项
if (mTextDataId == 0) {
// 设置文本数据的MIME类型
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
// 将文本数据插入到数据库并获取返回的URI
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mTextDataValues);
// 尝试从URI中解析出新插入的文本数据ID
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
// 如果解析失败记录错误日志并清除文本数据的ContentValues对象返回null
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());
}
// 清空文本数据的ContentValues对象准备下一次操作
mTextDataValues.clear();
}
// 如果通话数据的ContentValues对象中有数据
if(mCallDataValues.size() > 0) {
// 将笔记ID放入通话数据的ContentValues对象中
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
// 如果通话数据ID为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) {
// 如果解析失败记录错误日志并清除通话数据的ContentValues对象返回null
Log.e(TAG, "Insert new call data fail with noteId" + noteId);
mCallDataValues.clear();
return null;
}
} else {
// 如果通话数据ID不为0表示这是更新现有通话数据
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, mCallDataId));
builder.withValues(mCallDataValues);
operationList.add(builder.build());
}
// 清空通话数据的ContentValues对象准备下一次操作
mCallDataValues.clear();
}
// 如果有数据库操作需要执行
if (operationList.size() > 0) {
try {
// 执行批量数据库操作
ContentProviderResult[] results = context.getContentResolver().applyBatch(Notes.AUTHORITY, operationList);
// 如果操作成功返回更新的笔记URI否则返回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;
}
}
}