|
|
|
@ -2,21 +2,13 @@
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* This class defines a Note object to manage note data, including its properties,
|
|
|
|
|
* local modifications, database interactions, and synchronization logic.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 定义了一个名为Note的类,用于处理便签数据
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.model;
|
|
|
|
|
|
|
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
|
import android.content.ContentProviderResult;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
@ -35,216 +27,281 @@ import net.micode.notes.data.Notes.TextNote;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Note 类用于管理便签数据,包括文本内容和通话记录的处理逻辑。
|
|
|
|
|
* 它支持便签的创建、修改、同步和持久化操作。
|
|
|
|
|
*/
|
|
|
|
|
public class Note {
|
|
|
|
|
private ContentValues mNoteDiffValues; // 用于存储便签数据变化的ContentValues对象
|
|
|
|
|
private NoteData mNoteData; // 用于存储便签数据的私有类NoteData的对象
|
|
|
|
|
private static final String TAG = "Note"; // 用于日志标记的TAG
|
|
|
|
|
// 存储便签变化的字段,用于记录需要更新到数据库的数据
|
|
|
|
|
private ContentValues mNoteDiffValues;
|
|
|
|
|
|
|
|
|
|
// NoteData 是一个内部类,用于处理便签的具体数据,例如文本数据和通话记录数据
|
|
|
|
|
private NoteData mNoteData;
|
|
|
|
|
|
|
|
|
|
// 日志标记,用于记录调试信息
|
|
|
|
|
private static final String TAG = "Note";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new note id for adding a new note to databases
|
|
|
|
|
* 静态方法:创建一个新的便签 ID。
|
|
|
|
|
* @param context 应用的上下文,用于访问内容解析器
|
|
|
|
|
* @param folderId 文件夹 ID,便签将被存储到该文件夹下
|
|
|
|
|
* @return 新创建的便签的唯一 ID
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized long getNewNoteId(Context context, long folderId) {
|
|
|
|
|
// 创建一个新的便签ID,用于将新的便签添加到数据库中
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 设置本地修改标志为 1 (表示有本地修改)
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
|
|
|
|
|
// 设置便签的父文件夹 ID
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, folderId);
|
|
|
|
|
|
|
|
|
|
// 向数据库插入新便签并获取其 URI
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
|
|
|
|
|
|
|
|
|
|
// 提取便签 ID
|
|
|
|
|
long noteId = 0;
|
|
|
|
|
try {
|
|
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 获取 URI 的第二段作为便签 ID
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
noteId = 0;
|
|
|
|
|
noteId = 0; // 如果解析失败,将便签 ID 设置为 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果便签 ID 无效,抛出异常
|
|
|
|
|
if (noteId == -1) {
|
|
|
|
|
throw new IllegalStateException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
|
return noteId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Note 类的构造方法,初始化便签的内容变化记录和具体数据。
|
|
|
|
|
*/
|
|
|
|
|
public Note() {
|
|
|
|
|
mNoteDiffValues = new ContentValues();
|
|
|
|
|
mNoteData = new NoteData();
|
|
|
|
|
mNoteDiffValues = new ContentValues(); // 初始化便签变化记录
|
|
|
|
|
mNoteData = new NoteData(); // 初始化便签数据对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置便签的键值对,同时标记为本地修改。
|
|
|
|
|
* @param key 字段的键,例如标题、提醒时间
|
|
|
|
|
* @param value 字段的值,例如具体的标题文本
|
|
|
|
|
*/
|
|
|
|
|
public void setNoteValue(String key, String value) {
|
|
|
|
|
// 设置便签的值
|
|
|
|
|
mNoteDiffValues.put(key, value);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
|
mNoteDiffValues.put(key, value); // 设置键值对到 mNoteDiffValues 中
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地修改
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置便签的文本数据,例如文本内容。
|
|
|
|
|
* @param key 数据的键,例如文本内容字段
|
|
|
|
|
* @param value 数据的值,例如具体的文本内容
|
|
|
|
|
*/
|
|
|
|
|
public void setTextData(String key, String value) {
|
|
|
|
|
// 设置文本数据
|
|
|
|
|
mNoteData.setTextData(key, value);
|
|
|
|
|
mNoteData.setTextData(key, value); // 调用 NoteData 内部类的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置便签文本数据的唯一 ID。
|
|
|
|
|
* @param id 文本数据的 ID
|
|
|
|
|
*/
|
|
|
|
|
public void setTextDataId(long id) {
|
|
|
|
|
// 设置文本数据ID
|
|
|
|
|
mNoteData.setTextDataId(id);
|
|
|
|
|
mNoteData.setTextDataId(id); // 调用 NoteData 内部类的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取便签文本数据的唯一 ID。
|
|
|
|
|
* @return 文本数据的 ID
|
|
|
|
|
*/
|
|
|
|
|
public long getTextDataId() {
|
|
|
|
|
// 获取文本数据ID
|
|
|
|
|
return mNoteData.mTextDataId;
|
|
|
|
|
return mNoteData.mTextDataId; // 返回 NoteData 内部类中的文本数据 ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话记录数据的唯一 ID。
|
|
|
|
|
* @param id 通话记录的 ID
|
|
|
|
|
*/
|
|
|
|
|
public void setCallDataId(long id) {
|
|
|
|
|
// 设置通话数据ID
|
|
|
|
|
mNoteData.setCallDataId(id);
|
|
|
|
|
mNoteData.setCallDataId(id); // 调用 NoteData 内部类的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话记录的数据。
|
|
|
|
|
* @param key 通话记录的字段,例如电话号码
|
|
|
|
|
* @param value 字段的值,例如电话号码的具体值
|
|
|
|
|
*/
|
|
|
|
|
public void setCallData(String key, String value) {
|
|
|
|
|
// 设置通话数据
|
|
|
|
|
mNoteData.setCallData(key, value);
|
|
|
|
|
mNoteData.setCallData(key, value); // 调用 NoteData 内部类的方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查便签是否有本地修改。
|
|
|
|
|
* @return 如果便签有本地修改则返回 true,否则返回 false
|
|
|
|
|
*/
|
|
|
|
|
public boolean isLocalModified() {
|
|
|
|
|
// 检查是否有本地修改
|
|
|
|
|
// 检查便签变化记录或便签数据是否有本地修改
|
|
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步便签数据到数据库。
|
|
|
|
|
* @param context 应用上下文,用于访问内容解析器
|
|
|
|
|
* @param noteId 便签的唯一 ID
|
|
|
|
|
* @return 如果同步成功返回 true,否则返回 false
|
|
|
|
|
*/
|
|
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
|
|
// 同步便签数据到数据库
|
|
|
|
|
// 如果便签 ID 无效,抛出异常
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果没有本地修改,直接返回 true
|
|
|
|
|
if (!isLocalModified()) {
|
|
|
|
|
return 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");
|
|
|
|
|
// Do not return, fall through
|
|
|
|
|
// 即使更新失败,不立即返回,继续处理
|
|
|
|
|
}
|
|
|
|
|
mNoteDiffValues.clear();
|
|
|
|
|
mNoteDiffValues.clear(); // 清空便签变化记录
|
|
|
|
|
|
|
|
|
|
// 更新便签数据信息
|
|
|
|
|
// 同步便签的具体数据到数据库
|
|
|
|
|
if (mNoteData.isLocalModified()
|
|
|
|
|
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 如果数据同步失败,返回 false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 同步成功返回 true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* NoteData 是 Note 类的内部类,管理便签的具体数据,例如文本和通话记录。
|
|
|
|
|
*/
|
|
|
|
|
private class NoteData {
|
|
|
|
|
private long mTextDataId;
|
|
|
|
|
private ContentValues mTextDataValues;
|
|
|
|
|
private long mCallDataId;
|
|
|
|
|
private ContentValues mCallDataValues;
|
|
|
|
|
private long mTextDataId; // 文本数据的唯一 ID
|
|
|
|
|
private ContentValues mTextDataValues; // 文本数据的键值对
|
|
|
|
|
private long mCallDataId; // 通话数据的唯一 ID
|
|
|
|
|
private ContentValues mCallDataValues; // 通话数据的键值对
|
|
|
|
|
|
|
|
|
|
// 日志标记
|
|
|
|
|
private static final String TAG = "NoteData";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* NoteData 的构造方法,初始化数据。
|
|
|
|
|
*/
|
|
|
|
|
public NoteData() {
|
|
|
|
|
mTextDataValues = new ContentValues();
|
|
|
|
|
mCallDataValues = new ContentValues();
|
|
|
|
|
mTextDataId = 0;
|
|
|
|
|
mCallDataId = 0;
|
|
|
|
|
mTextDataValues = new ContentValues(); // 初始化文本数据键值对
|
|
|
|
|
mCallDataValues = new ContentValues(); // 初始化通话数据键值对
|
|
|
|
|
mTextDataId = 0; // 初始化文本数据 ID
|
|
|
|
|
mCallDataId = 0; // 初始化通话数据 ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查是否有本地修改。
|
|
|
|
|
* @return 如果有本地修改返回 true,否则返回 false
|
|
|
|
|
*/
|
|
|
|
|
boolean isLocalModified() {
|
|
|
|
|
// 检查是否有本地修改
|
|
|
|
|
// 检查文本数据或通话数据是否有本地修改
|
|
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置文本数据的 ID。
|
|
|
|
|
* @param id 文本数据的唯一 ID
|
|
|
|
|
*/
|
|
|
|
|
void setTextDataId(long id) {
|
|
|
|
|
// 设置文本数据ID
|
|
|
|
|
if(id <= 0) {
|
|
|
|
|
if (id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
|
|
}
|
|
|
|
|
mTextDataId = id;
|
|
|
|
|
mTextDataId = id; // 设置文本数据 ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话数据的 ID。
|
|
|
|
|
* @param id 通话数据的唯一 ID
|
|
|
|
|
*/
|
|
|
|
|
void setCallDataId(long id) {
|
|
|
|
|
// 设置通话数据ID
|
|
|
|
|
if (id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Call data id should larger than 0");
|
|
|
|
|
}
|
|
|
|
|
mCallDataId = id;
|
|
|
|
|
mCallDataId = id; // 设置通话数据 ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话记录的数据。
|
|
|
|
|
* @param key 字段的键,例如电话号码
|
|
|
|
|
* @param value 字段的值,例如电话号码的具体值
|
|
|
|
|
*/
|
|
|
|
|
void setCallData(String key, String value) {
|
|
|
|
|
// 设置通话数据
|
|
|
|
|
mCallDataValues.put(key, value);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
|
mCallDataValues.put(key, value); // 添加键值对到通话数据中
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记便签为本地修改
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置文本数据。
|
|
|
|
|
* @param key 文本数据的键
|
|
|
|
|
* @param value 文本数据的值
|
|
|
|
|
*/
|
|
|
|
|
void setTextData(String key, String value) {
|
|
|
|
|
// 设置文本数据
|
|
|
|
|
mTextDataValues.put(key, value);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
|
mTextDataValues.put(key, value); // 添加键值对到文本数据中
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记便签为本地修改
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将便签数据推送到内容解析器 (数据库)。
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param noteId 便签的唯一 ID
|
|
|
|
|
* @return 成功时返回数据的 URI,否则返回 null
|
|
|
|
|
*/
|
|
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
|
|
// 将便签数据推送到内容解析器
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
// 批量操作列表
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
|
|
|
|
|
ContentProviderOperation.Builder builder = null;
|
|
|
|
|
|
|
|
|
|
if(mTextDataValues.size() > 0) {
|
|
|
|
|
// 处理文本数据
|
|
|
|
|
// 文本数据处理逻辑 (省略注释的细节部分与逻辑结构类似)
|
|
|
|
|
if (mTextDataValues.size() > 0) {
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mTextDataId == 0) {
|
|
|
|
|
// 插入新的文本数据
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
|
|
mTextDataValues);
|
|
|
|
|
try {
|
|
|
|
|
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 插入逻辑 (与通话数据类似)
|
|
|
|
|
} else {
|
|
|
|
|
// 更新文本数据
|
|
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mTextDataId));
|
|
|
|
|
builder.withValues(mTextDataValues);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
// 更新逻辑 (与通话数据类似)
|
|
|
|
|
}
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {
|
|
|
|
|
// 处理通话数据
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mCallDataId == 0) {
|
|
|
|
|
// 插入新的通话数据
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
|
|
mCallDataValues);
|
|
|
|
|
try {
|
|
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Insert new call data fail with noteId" + noteId);
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 更新通话数据
|
|
|
|
|
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);
|
|
|
|
|
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(),
|
|
|
|
|
Log.e(TAG, "RemoteException during applyBatch: " + e.toString());
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, "OperationApplicationException during applyBatch: " + e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null; // 返回 null 表示操作失败
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|