|
|
/*
|
|
|
* 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,用于在数据库中添加新的笔记
|
|
|
*/
|
|
|
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 {
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1)); // 获取新笔记的ID
|
|
|
} 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; // 返回新创建的笔记ID
|
|
|
}
|
|
|
|
|
|
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); // 将文本数据传递给NoteData对象
|
|
|
}
|
|
|
|
|
|
// 设置文本数据ID
|
|
|
public void setTextDataId(long id) {
|
|
|
mNoteData.setTextDataId(id); // 设置文本数据的ID
|
|
|
}
|
|
|
|
|
|
// 获取文本数据ID
|
|
|
public long getTextDataId() {
|
|
|
return mNoteData.mTextDataId; // 返回文本数据的ID
|
|
|
}
|
|
|
|
|
|
// 设置通话数据ID
|
|
|
public void setCallDataId(long id) {
|
|
|
mNoteData.setCallDataId(id); // 设置通话数据的ID
|
|
|
}
|
|
|
|
|
|
// 设置通话数据
|
|
|
public void setCallData(String key, String value) {
|
|
|
mNoteData.setCallData(key, value); // 将通话数据传递给NoteData对象
|
|
|
}
|
|
|
|
|
|
// 判断笔记是否本地已修改
|
|
|
public boolean isLocalModified() {
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); // 如果修改数据不为空或者NoteData已修改,则返回true
|
|
|
}
|
|
|
|
|
|
// 同步笔记数据到ContentProvider
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId); // 检查笔记ID是否合法
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
}
|
|
|
mNoteDiffValues.clear(); // 清空修改数据
|
|
|
|
|
|
// 如果有本地修改的内容,推送到ContentResolver
|
|
|
if (mNoteData.isLocalModified()
|
|
|
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
|
|
|
return false; // 如果推送失败,返回false
|
|
|
}
|
|
|
|
|
|
return true; // 同步成功,返回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;
|
|
|
mCallDataId = 0;
|
|
|
}
|
|
|
|
|
|
// 判断笔记内容是否本地已修改
|
|
|
boolean isLocalModified() {
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; // 如果文本或通话数据有修改,返回true
|
|
|
}
|
|
|
|
|
|
// 设置文本数据的ID
|
|
|
void setTextDataId(long id) {
|
|
|
if(id <= 0) {
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0"); // 检查ID是否合法
|
|
|
}
|
|
|
mTextDataId = id; // 设置文本数据ID
|
|
|
}
|
|
|
|
|
|
// 设置通话数据的ID
|
|
|
void setCallDataId(long id) {
|
|
|
if (id <= 0) {
|
|
|
throw new IllegalArgumentException("Call data id should larger than 0"); // 检查ID是否合法
|
|
|
}
|
|
|
mCallDataId = id; // 设置通话数据ID
|
|
|
}
|
|
|
|
|
|
// 设置通话数据
|
|
|
void setCallData(String key, String value) {
|
|
|
mCallDataValues.put(key, value); // 将通话数据放入ContentValues
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记本地已修改
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
|
|
|
}
|
|
|
|
|
|
// 设置文本数据
|
|
|
void setTextData(String key, String value) {
|
|
|
mTextDataValues.put(key, value); // 将文本数据放入ContentValues
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记本地已修改
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); // 更新修改时间
|
|
|
}
|
|
|
|
|
|
// 推送内容到ContentResolver
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId); // 检查笔记ID是否合法
|
|
|
}
|
|
|
|
|
|
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); // 设置MIME类型
|
|
|
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 {
|
|
|
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); // 设置MIME类型
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mCallDataValues); // 插入数据
|
|
|
try {
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); // 获取通话数据ID
|
|
|
} 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); // 返回URI
|
|
|
} 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;
|
|
|
}
|
|
|
}
|
|
|
}
|