注释代码

suhongyuan
shy 1 year ago
parent aa531424ca
commit 83022bc33d

@ -0,0 +1,267 @@
/*
* 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 = 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()); // 获取 ID 失败
noteId = 0;
}
// 如果获取到的笔记 ID 无效,则抛出异常
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
}
return noteId;
}
// 构造函数,初始化数据成员
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); // 检查笔记 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(); // 清除更新的内容
// 如果笔记数据有修改,尝试同步文本和通话数据
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
}
// 判断文本数据或通话数据是否有修改
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"); // 检查 ID 是否有效
}
mTextDataId = id;
}
// 设置通话数据的 ID
void setCallDataId(long id) {
if (id <= 0) {
throw new IllegalArgumentException("Call data id should larger than 0"); // 检查 ID 是否有效
}
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()); // 设置修改时间
}
// 将数据保存到 ContentProvider 中
Uri pushIntoContentResolver(Context context, long noteId) {
// 检查 noteId 是否有效
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); // 操作列表
ContentProviderOperation.Builder builder = null;
// 如果文本数据有修改,进行保存或更新
if(mTextDataValues.size() > 0) {
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
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);
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);
} 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; // 没有操作时返回 null
}
}
}
Loading…
Cancel
Save