sfh 8 months ago
parent 977135461c
commit c7d559d6b5

@ -0,0 +1,265 @@
/*
* 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;
}
}
}

@ -0,0 +1,254 @@
/*
* 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.appwidget.AppWidgetManager;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
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.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;
import net.micode.notes.tool.ResourceParser.NoteBgResources;
/**
* WorkingNote
*
*/
public class WorkingNote {
private Note mNote; // 代表笔记的 Note 对象
private long mNoteId; // 笔记的 ID
private String mContent; // 笔记的内容(文本)
private int mMode; // 笔记的模式(例如:清单模式)
private long mAlertDate; // 提醒时间
private long mModifiedDate; // 最后修改时间
private int mBgColorId; // 背景色 ID
private int mWidgetId; // 小部件 ID
private int mWidgetType; // 小部件类型
private long mFolderId; // 文件夹 ID
private Context mContext; // 上下文
private boolean mIsDeleted; // 是否被标记为已删除
private NoteSettingChangedListener mNoteSettingStatusListener; // 设置变更监听器
private static final String TAG = "WorkingNote"; // 日志标签
// 数据查询的列名数组
public static final String[] DATA_PROJECTION = new String[] {
DataColumns.ID, DataColumns.CONTENT, DataColumns.MIME_TYPE, DataColumns.DATA1,
DataColumns.DATA2, DataColumns.DATA3, DataColumns.DATA4
};
// 笔记数据的查询列名
public static final String[] NOTE_PROJECTION = new String[] {
NoteColumns.PARENT_ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.MODIFIED_DATE
};
// 各列的索引常量
private static final int DATA_ID_COLUMN = 0;
private static final int DATA_CONTENT_COLUMN = 1;
private static final int DATA_MIME_TYPE_COLUMN = 2;
private static final int DATA_MODE_COLUMN = 3;
private static final int NOTE_PARENT_ID_COLUMN = 0;
private static final int NOTE_ALERTED_DATE_COLUMN = 1;
private static final int NOTE_BG_COLOR_ID_COLUMN = 2;
private static final int NOTE_WIDGET_ID_COLUMN = 3;
private static final int NOTE_WIDGET_TYPE_COLUMN = 4;
private static final int NOTE_MODIFIED_DATE_COLUMN = 5;
/**
*
*/
private WorkingNote(Context context, long folderId) {
mContext = context;
mAlertDate = 0;
mModifiedDate = System.currentTimeMillis();
mFolderId = folderId;
mNote = new Note(); // 创建一个新的 Note 对象
mNoteId = 0;
mIsDeleted = false;
mMode = 0;
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 默认无效的小部件类型
}
/**
*
*/
private WorkingNote(Context context, long noteId, long folderId) {
mContext = context;
mNoteId = noteId;
mFolderId = folderId;
mIsDeleted = false;
mNote = new Note();
loadNote(); // 加载笔记数据
}
/**
*
*/
private void loadNote() {
// 从笔记内容 URI 中查询笔记的基本信息
Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
// 获取笔记的基本信息
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN);
mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN);
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN);
}
cursor.close();
} else {
Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
}
loadNoteData(); // 加载笔记的内容数据
}
/**
*
*/
private void loadNoteData() {
// 查询笔记的数据内容
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] {
String.valueOf(mNoteId)
}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) {
mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); // 设置文本数据ID
} else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); // 设置通话数据ID
} else {
Log.d(TAG, "Wrong note type with type:" + type);
}
} while (cursor.moveToNext());
}
cursor.close();
} else {
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
}
}
/**
*
*/
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId);
note.setBgColorId(defaultBgColorId); // 设置背景色
note.setWidgetId(widgetId); // 设置小部件ID
note.setWidgetType(widgetType); // 设置小部件类型
return note;
}
/**
* ID
*/
public static WorkingNote load(Context context, long id) {
return new WorkingNote(context, id, 0);
}
/**
*
*/
public synchronized boolean saveNote() {
if (isWorthSaving()) { // 如果笔记值得保存
if (!existInDatabase()) { // 如果笔记不存在数据库
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId);
return false;
}
}
mNote.syncNote(mContext, mNoteId); // 同步笔记内容到数据库
// 如果笔记关联了小部件,更新小部件内容
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
}
return true;
} else {
return false;
}
}
/**
*
*/
public boolean existInDatabase() {
return mNoteId > 0;
}
/**
*
*/
private boolean isWorthSaving() {
return !(mIsDeleted || (TextUtils.isEmpty(mContent) && !existInDatabase())
|| (existInDatabase() && !mNote.isLocalModified())); // 判断笔记是否被删除或没有修改
}
// 其他设置方法(设置提醒时间、背景色、小部件等)
public void setAlertDate(long date, boolean set) { ... }
public void markDeleted(boolean mark) { ... }
public void setBgColorId(int id) { ... }
public void setCheckListMode(int mode) { ... }
public void setWidgetType(int type) { ... }
public void setWidgetId(int id) { ... }
public void setWorkingText(String text) { ... }
// 获取笔记内容、提醒时间、修改时间等信息
public String getContent() { return mContent; }
public long getAlertDate() { return mAlertDate; }
public long getModifiedDate() { return mModifiedDate; }
public int getBgColorResId() { return NoteBgResources.getNoteBgResource(mBgColorId); }
public int getBgColorId() { return mBgColorId; }
public int getCheckListMode() { return mMode; }
public long getNoteId() { return mNoteId; }
public long getFolderId() { return mFolderId; }
public int getWidgetId() { return mWidgetId; }
public int getWidgetType() { return mWidgetType; }
// 设置变更监听接口
public interface NoteSettingChangedListener {
void onBackgroundColorChanged(); // 背景色变更时调用
void onClockAlertChanged(long date, boolean set); // 提醒时间变更时调用
void onWidgetChanged(); // 小部件变更时调用
void onCheckListModeChanged(int oldMode, int newMode); // 清单模式切换时调用
}
}
Loading…
Cancel
Save