|
|
/*
|
|
|
* 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;
|
|
|
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 {
|
|
|
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 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
|
|
|
|
|
|
// 插入新笔记到内容提供者
|
|
|
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, "获取笔记ID错误: " + e.toString());
|
|
|
noteId = 0;
|
|
|
}
|
|
|
if (noteId == -1) {
|
|
|
throw new IllegalStateException("错误的笔记ID: " + noteId);
|
|
|
}
|
|
|
return noteId;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化笔记对象
|
|
|
*/
|
|
|
public Note() {
|
|
|
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());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置文本数据的值
|
|
|
* @param key 键名
|
|
|
* @param value 值
|
|
|
*/
|
|
|
public void setTextData(String key, String value) {
|
|
|
mNoteData.setTextData(key, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置文本数据ID
|
|
|
* @param id 文本数据ID
|
|
|
*/
|
|
|
public void setTextDataId(long id) {
|
|
|
mNoteData.setTextDataId(id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取文本数据ID
|
|
|
* @return 文本数据ID
|
|
|
*/
|
|
|
public long getTextDataId() {
|
|
|
return mNoteData.mTextDataId;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置通话数据ID
|
|
|
* @param id 通话数据ID
|
|
|
*/
|
|
|
public void setCallDataId(long id) {
|
|
|
mNoteData.setCallDataId(id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置通话数据的值
|
|
|
* @param key 键名
|
|
|
* @param value 值
|
|
|
*/
|
|
|
public void setCallData(String key, String value) {
|
|
|
mNoteData.setCallData(key, value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查笔记是否有本地修改
|
|
|
* @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) {
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("错误的笔记ID: " + noteId);
|
|
|
}
|
|
|
|
|
|
if (!isLocalModified()) {
|
|
|
return true; // 没有修改,无需同步
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 理论上,一旦数据发生变化,笔记应在{@link NoteColumns#LOCAL_MODIFIED}和
|
|
|
* {@link NoteColumns#MODIFIED_DATE}上更新。为了数据安全,即使更新笔记失败,
|
|
|
* 我们也会更新笔记数据信息
|
|
|
*/
|
|
|
if (context.getContentResolver().update(
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
|
|
|
null) == 0) {
|
|
|
Log.e(TAG, "更新笔记错误,这种情况不应该发生");
|
|
|
// 不返回,继续执行
|
|
|
}
|
|
|
mNoteDiffValues.clear(); // 清除已同步的变更值
|
|
|
|
|
|
// 同步笔记数据,如果有修改且同步失败则返回false
|
|
|
if (mNoteData.isLocalModified()
|
|
|
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 内部类,用于管理笔记关联的数据(文本内容、通话记录等)
|
|
|
*/
|
|
|
private class NoteData {
|
|
|
private long mTextDataId; // 文本数据ID
|
|
|
private ContentValues mTextDataValues; // 文本数据变更值
|
|
|
private long mCallDataId; // 通话数据ID
|
|
|
private ContentValues mCallDataValues; // 通话数据变更值
|
|
|
private static final String TAG = "NoteData";
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化笔记数据对象
|
|
|
*/
|
|
|
public NoteData() {
|
|
|
mTextDataValues = new ContentValues(); // 初始化文本数据容器
|
|
|
mCallDataValues = new ContentValues(); // 初始化通话数据容器
|
|
|
mTextDataId = 0; // 初始文本数据ID为0
|
|
|
mCallDataId = 0; // 初始通话数据ID为0
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查笔记数据是否有本地修改
|
|
|
* @return 如果有修改返回true,否则返回false
|
|
|
*/
|
|
|
boolean isLocalModified() {
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置文本数据ID
|
|
|
* @param id 文本数据ID
|
|
|
*/
|
|
|
void setTextDataId(long id) {
|
|
|
if(id <= 0) {
|
|
|
throw new IllegalArgumentException("文本数据ID应大于0");
|
|
|
}
|
|
|
mTextDataId = id;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置通话数据ID
|
|
|
* @param id 通话数据ID
|
|
|
*/
|
|
|
void setCallDataId(long id) {
|
|
|
if (id <= 0) {
|
|
|
throw new IllegalArgumentException("通话数据ID应大于0");
|
|
|
}
|
|
|
mCallDataId = 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());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置文本数据的值
|
|
|
* @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());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将笔记数据推送到内容提供者
|
|
|
* @param context 应用上下文
|
|
|
* @param noteId 笔记ID
|
|
|
* @return 成功返回笔记URI,失败返回null
|
|
|
*/
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
/**
|
|
|
* 安全检查
|
|
|
*/
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("错误的笔记ID: " + noteId);
|
|
|
}
|
|
|
|
|
|
// 创建批量操作列表
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
ContentProviderOperation.Builder builder = null;
|
|
|
|
|
|
// 处理文本数据变更
|
|
|
if(mTextDataValues.size() > 0) {
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId); // 设置关联的笔记ID
|
|
|
if (mTextDataId == 0) {
|
|
|
// 新增文本数据
|
|
|
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
mTextDataValues);
|
|
|
try {
|
|
|
// 从返回的URI中获取并设置新的文本数据ID
|
|
|
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
} catch (NumberFormatException e) {
|
|
|
Log.e(TAG, "插入新文本数据失败,笔记ID: " + 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); // 设置关联的笔记ID
|
|
|
if (mCallDataId == 0) {
|
|
|
// 新增通话数据
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
mCallDataValues);
|
|
|
try {
|
|
|
// 从返回的URI中获取并设置新的通话数据ID
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
} catch (NumberFormatException e) {
|
|
|
Log.e(TAG, "插入新通话数据失败,笔记ID: " + 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(), e.getMessage()));
|
|
|
return null;
|
|
|
} catch (OperationApplicationException e) {
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
} |