|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
public class Note {
|
|
|
// 用于存储笔记基本信息的变更值(如标题、修改时间等)
|
|
|
private ContentValues mNoteDiffValues;
|
|
|
// 存储笔记的具体内容数据(文本或通话记录)
|
|
|
private NoteData mNoteData;
|
|
|
private static final String TAG = "Note"; // 日志标签
|
|
|
|
|
|
/**
|
|
|
* 在指定文件夹下创建新笔记并返回ID
|
|
|
* @param context 上下文环境
|
|
|
* @param folderId 父文件夹ID
|
|
|
* @return 新创建的笔记ID (0表示创建失败)
|
|
|
*/
|
|
|
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 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;
|
|
|
}
|
|
|
|
|
|
// 构造函数初始化数据容器
|
|
|
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(用于更新现有文本数据)
|
|
|
public void setTextDataId(long id) {
|
|
|
mNoteData.setTextDataId(id);
|
|
|
}
|
|
|
|
|
|
// 获取文本数据ID
|
|
|
public long getTextDataId() {
|
|
|
return mNoteData.mTextDataId;
|
|
|
}
|
|
|
|
|
|
// 设置通话记录数据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表示存在未同步修改
|
|
|
*/
|
|
|
public boolean isLocalModified() {
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 同步笔记数据到数据库
|
|
|
* @param context 上下文环境
|
|
|
* @param noteId 要同步的笔记ID
|
|
|
* @return true表示同步成功
|
|
|
*/
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
|
|
}
|
|
|
|
|
|
// 无修改时直接返回成功
|
|
|
if (!isLocalModified()) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/* 优先更新笔记基本信息 */
|
|
|
int updateCount = context.getContentResolver().update(
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
mNoteDiffValues,
|
|
|
null,
|
|
|
null);
|
|
|
|
|
|
if (updateCount == 0) {
|
|
|
Log.e(TAG, "Update note error, should not happen");
|
|
|
// 继续尝试保存内容数据(不中断流程)
|
|
|
}
|
|
|
mNoteDiffValues.clear(); // 清空已提交的变更
|
|
|
|
|
|
/* 更新笔记内容数据(文本/通话记录)*/
|
|
|
if (mNoteData.isLocalModified()) {
|
|
|
Uri resultUri = mNoteData.pushIntoContentResolver(context, noteId);
|
|
|
if (resultUri == 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; // 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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置通话记录数据
|
|
|
* @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("Wrong note id:" + noteId);
|
|
|
}
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<>();
|
|
|
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, "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) {
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
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);
|
|
|
// 返回笔记URI作为操作成功标识
|
|
|
return (results == null || results.length == 0 || results[0] == null) ? null
|
|
|
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
|
|
|
} catch (RemoteException | OperationApplicationException e) {
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
return null; // 无需要执行的操作
|
|
|
}
|
|
|
}
|
|
|
}
|