Signed-off-by: lilium-saber <WR364999428@hotmail.com>
DUANYIQUN_BRANCH
lilium-saber 2 years ago
parent b12b9a8449
commit 57dbac7bbf

@ -47,6 +47,7 @@ public class Contact {
String selection = CALLER_ID_SELECTION.replace("+", String selection = CALLER_ID_SELECTION.replace("+",
PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将电话号码进行函数处理并代替'+'符号 PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将电话号码进行函数处理并代替'+'符号
Cursor cursor = context.getContentResolver().query( //用于查询的结果集 Cursor cursor = context.getContentResolver().query( //用于查询的结果集
Data.CONTENT_URI, //通话记录uri Data.CONTENT_URI, //通话记录uri
new String [] { Phone.DISPLAY_NAME }, new String [] { Phone.DISPLAY_NAME },

@ -26,7 +26,7 @@ import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns;
//引入Notes中接 //引入Notes中接
public class NotesDatabaseHelper extends SQLiteOpenHelper { //该类为便签SQL表的定义以及操作便于进行文件夹与便签的各种操作例如添加或删除的行为 public class NotesDatabaseHelper extends SQLiteOpenHelper { //该类为便签SQL表的定义以及操作便于进行文件夹与便签的各种操作例如添加或删除的行为
private static final String DB_NAME = "note.db"; //数据库名称 private static final String DB_NAME = "note.db"; //数据库名称
@ -349,6 +349,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.GTASK_ID
+ " TEXT NOT NULL DEFAULT ''"); + " TEXT NOT NULL DEFAULT ''");
// add a trash system folder // add a trash system folder
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER); values.put(NoteColumns.ID, Notes.ID_TRASH_FOLER);
values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); values.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);

@ -187,6 +187,7 @@ public class NotesProvider extends ContentProvider {
String id = null; String id = null;
SQLiteDatabase db = mHelper.getWritableDatabase(); //打开数据库 SQLiteDatabase db = mHelper.getWritableDatabase(); //打开数据库
boolean deleteData = false; boolean deleteData = false;
switch (mMatcher.match(uri)) { //uri匹配 switch (mMatcher.match(uri)) { //uri匹配
case URI_NOTE: case URI_NOTE:
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; //设置WHERE selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; //设置WHERE
@ -218,6 +219,7 @@ public class NotesProvider extends ContentProvider {
default: default:
throw new IllegalArgumentException("Unknown URI " + uri); //报错 throw new IllegalArgumentException("Unknown URI " + uri); //报错
} }
if (count > 0) { //表示有删除操作通知uri发生变化 if (count > 0) { //表示有删除操作通知uri发生变化
if (deleteData) { if (deleteData) {
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);

@ -35,78 +35,78 @@ import java.util.ArrayList;
public class Note { public class Note {
private ContentValues mNoteDiffValues; private ContentValues mNoteDiffValues; //用于保存便签修改值
private NoteData mNoteData; private NoteData mNoteData; //保存便签的数据
private static final String TAG = "Note"; private static final String TAG = "Note"; //设置TAG为Note
/** /**
* Create a new note id for adding a new note to databases * Create a new note id for adding a new note to databases
*/ */
public static synchronized long getNewNoteId(Context context, long folderId) { public static synchronized long getNewNoteId(Context context, long folderId) { //为一个新的便签创建id返回数据库
// Create a new note in the database // Create a new note in the database
ContentValues values = new ContentValues(); ContentValues values = new ContentValues(); //用于保存创建日期等字段的值
long createdTime = System.currentTimeMillis(); long createdTime = System.currentTimeMillis(); //获取创建时间
values.put(NoteColumns.CREATED_DATE, createdTime); values.put(NoteColumns.CREATED_DATE, createdTime);
values.put(NoteColumns.MODIFIED_DATE, createdTime); values.put(NoteColumns.MODIFIED_DATE, createdTime);
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
values.put(NoteColumns.LOCAL_MODIFIED, 1); values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.PARENT_ID, folderId); values.put(NoteColumns.PARENT_ID, folderId);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); //将values插入数据库中并返回uri
long noteId = 0; long noteId = 0;
try { try {
noteId = Long.valueOf(uri.getPathSegments().get(1)); noteId = Long.valueOf(uri.getPathSegments().get(1)); //解析uri获取新笔记id
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString()); Log.e(TAG, "Get note id error :" + e.toString()); //解析失败日志
noteId = 0; noteId = 0;
} }
if (noteId == -1) { if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId); throw new IllegalStateException("Wrong note id:" + noteId); //获取id出错抛出异常
} }
return noteId; return noteId;
} }
public Note() { public Note() {
mNoteDiffValues = new ContentValues(); mNoteDiffValues = new ContentValues(); //创建新对象
mNoteData = new NoteData(); mNoteData = new NoteData(); //创建新对象
} }
public void setNoteValue(String key, String value) { public void setNoteValue(String key, String value) { //设置便签的值
mNoteDiffValues.put(key, value); mNoteDiffValues.put(key, value); //key为属性values为新值
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //本地修改标识为1标识被修改
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); //表示修改时间,用于服务器时间比较
} }
public void setTextData(String key, String value) { public void setTextData(String key, String value) {
mNoteData.setTextData(key, value); mNoteData.setTextData(key, value); //添加键值对
} } //设置文本数据
public void setTextDataId(long id) { public void setTextDataId(long id) {
mNoteData.setTextDataId(id); mNoteData.setTextDataId(id);
} } //设置文本数据id
public long getTextDataId() { public long getTextDataId() {
return mNoteData.mTextDataId; return mNoteData.mTextDataId;
} } //获取文本数据id
public void setCallDataId(long id) { public void setCallDataId(long id) {
mNoteData.setCallDataId(id); mNoteData.setCallDataId(id);
} } //设置通讯数据id
public void setCallData(String key, String value) { public void setCallData(String key, String value) {
mNoteData.setCallData(key, value); mNoteData.setCallData(key, value);
} } //设置通讯数据,添加键值对
public boolean isLocalModified() { public boolean isLocalModified() {
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
} } //检查是否被修改
public boolean syncNote(Context context, long noteId) { public boolean syncNote(Context context, long noteId) { //用于同步到数据库中
if (noteId <= 0) { if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId); throw new IllegalArgumentException("Wrong note id:" + noteId); //抛出异常
} }
if (!isLocalModified()) { if (!isLocalModified()) { //判断是否修改过
return true; return true; //没修改则不需要同步
} }
/** /**
@ -114,7 +114,7 @@ public class Note {
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the * {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
* note data info * note data info
*/ */
if (context.getContentResolver().update( if (context.getContentResolver().update( //打包并同步
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) { null) == 0) {
Log.e(TAG, "Update note error, should not happen"); Log.e(TAG, "Update note error, should not happen");
@ -124,13 +124,13 @@ public class Note {
if (mNoteData.isLocalModified() if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) { && (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false; return false; //表示同步失败
} }
return true; return true;
} }
private class NoteData { private class NoteData { //私有内部类,用于便签数据管理
private long mTextDataId; private long mTextDataId;
private ContentValues mTextDataValues; private ContentValues mTextDataValues;
@ -141,77 +141,76 @@ public class Note {
private static final String TAG = "NoteData"; private static final String TAG = "NoteData";
public NoteData() { public NoteData() { //构造函数
mTextDataValues = new ContentValues(); mTextDataValues = new ContentValues(); //保存便签文本
mCallDataValues = new ContentValues(); mCallDataValues = new ContentValues(); //保存便签通讯记录
mTextDataId = 0; mTextDataId = 0;
mCallDataId = 0; mCallDataId = 0;
} }
boolean isLocalModified() { boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
} } //检查便签是否再本地修改
void setTextDataId(long id) { void setTextDataId(long id) {
if(id <= 0) { if(id <= 0) {
throw new IllegalArgumentException("Text data id should larger than 0"); throw new IllegalArgumentException("Text data id should larger than 0");
} }
mTextDataId = id; mTextDataId = id;
} } //设置便签文本标识符
void setCallDataId(long id) { void setCallDataId(long id) {
if (id <= 0) { if (id <= 0) {
throw new IllegalArgumentException("Call data id should larger than 0"); throw new IllegalArgumentException("Call data id should larger than 0");
} }
mCallDataId = id; mCallDataId = id;
} } //设置便签通讯标识符
void setCallData(String key, String value) { void setCallData(String key, String value) {
mCallDataValues.put(key, value); mCallDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //更新修改状态
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); //更新修改日期
} } //设置文本数据
void setTextData(String key, String value) { void setTextData(String key, String value) { //设置通讯数据
mTextDataValues.put(key, value); mTextDataValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
} }
Uri pushIntoContentResolver(Context context, long noteId) { Uri pushIntoContentResolver(Context context, long noteId) { //将文本数据与通讯数据添加到context
/** /**
* Check for safety * Check for safety
*/ */
if (noteId <= 0) { if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId); throw new IllegalArgumentException("Wrong note id:" + noteId); //抛出异常
} }
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = null; ContentProviderOperation.Builder builder = null;
if(mTextDataValues.size() > 0) { if(mTextDataValues.size() > 0) { //检查文本数据
mTextDataValues.put(DataColumns.NOTE_ID, noteId); mTextDataValues.put(DataColumns.NOTE_ID, noteId); //将便签id与数据值相关联
if (mTextDataId == 0) { if (mTextDataId == 0) {
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); //并将键值对添加到mTextDataValues中
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues); mTextDataValues); //插入新的数据行保存返回的uri
try { try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(TAG, "Insert new text data fail with noteId" + noteId); Log.e(TAG, "Insert new text data fail with noteId" + noteId); //异常日志
mTextDataValues.clear(); mTextDataValues.clear();
return null; return null;
} }
} else { } else { //表示需要更新现有数据行
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId)); Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues); builder.withValues(mTextDataValues);
operationList.add(builder.build()); operationList.add(builder.build());
} }
mTextDataValues.clear(); mTextDataValues.clear(); //清空mTextDataValues
} }
if(mCallDataValues.size() > 0) { //检查通讯数据,代码内容功能同上
if(mCallDataValues.size() > 0) {
mCallDataValues.put(DataColumns.NOTE_ID, noteId); mCallDataValues.put(DataColumns.NOTE_ID, noteId);
if (mCallDataId == 0) { if (mCallDataId == 0) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
@ -233,7 +232,7 @@ public class Note {
mCallDataValues.clear(); mCallDataValues.clear();
} }
if (operationList.size() > 0) { if (operationList.size() > 0) { //检查operationList对载入的操作进行批量提交
try { try {
ContentProviderResult[] results = context.getContentResolver().applyBatch( ContentProviderResult[] results = context.getContentResolver().applyBatch(
Notes.AUTHORITY, operationList); Notes.AUTHORITY, operationList);

@ -34,35 +34,35 @@ import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote { public class WorkingNote {
// Note for the working note // Note for the working note
private Note mNote; private Note mNote; //表示工作便签
// Note Id // Note Id
private long mNoteId; private long mNoteId; //便签id
// Note content // Note content
private String mContent; private String mContent; //便签内容
// Note mode // Note mode
private int mMode; private int mMode; //便签模式
private long mAlertDate; private long mAlertDate; //提醒日期
private long mModifiedDate; private long mModifiedDate; //最近修改日期
private int mBgColorId; private int mBgColorId; //背景颜色id
private int mWidgetId; private int mWidgetId; //小部件id
private int mWidgetType; private int mWidgetType; //小部件类型
private long mFolderId; private long mFolderId; //所在文件夹id
private Context mContext; private Context mContext; //表示上下文
private static final String TAG = "WorkingNote"; private static final String TAG = "WorkingNote"; //设置TAG
private boolean mIsDeleted; private boolean mIsDeleted; //表示是否被删除
private NoteSettingChangedListener mNoteSettingStatusListener; private NoteSettingChangedListener mNoteSettingStatusListener; //表示便签设置状态
public static final String[] DATA_PROJECTION = new String[] { public static final String[] DATA_PROJECTION = new String[] { //用于指定需要从数据库查询的数据列
DataColumns.ID, DataColumns.ID,
DataColumns.CONTENT, DataColumns.CONTENT,
DataColumns.MIME_TYPE, DataColumns.MIME_TYPE,
@ -81,28 +81,28 @@ public class WorkingNote {
NoteColumns.MODIFIED_DATE NoteColumns.MODIFIED_DATE
}; };
private static final int DATA_ID_COLUMN = 0; private static final int DATA_ID_COLUMN = 0; //表示查询数据的id列
private static final int DATA_CONTENT_COLUMN = 1; private static final int DATA_CONTENT_COLUMN = 1; //表示查询数据的内容列
private static final int DATA_MIME_TYPE_COLUMN = 2; private static final int DATA_MIME_TYPE_COLUMN = 2; //表示查询数据的MIME类型列
private static final int DATA_MODE_COLUMN = 3; private static final int DATA_MODE_COLUMN = 3; //表示查询数据的模式列
private static final int NOTE_PARENT_ID_COLUMN = 0; private static final int NOTE_PARENT_ID_COLUMN = 0; //表示查询便签的父文件夹id列
private static final int NOTE_ALERTED_DATE_COLUMN = 1; private static final int NOTE_ALERTED_DATE_COLUMN = 1; //表示便签的提醒日期列
private static final int NOTE_BG_COLOR_ID_COLUMN = 2; private static final int NOTE_BG_COLOR_ID_COLUMN = 2; //表示便签的背景颜色id列
private static final int NOTE_WIDGET_ID_COLUMN = 3; private static final int NOTE_WIDGET_ID_COLUMN = 3; //表示便签小组件id列
private static final int NOTE_WIDGET_TYPE_COLUMN = 4; private static final int NOTE_WIDGET_TYPE_COLUMN = 4; //表示便签小组件类型列
private static final int NOTE_MODIFIED_DATE_COLUMN = 5; private static final int NOTE_MODIFIED_DATE_COLUMN = 5; //表示便签最后修改日期列
// New note construct // New note construct
private WorkingNote(Context context, long folderId) { private WorkingNote(Context context, long folderId) { //新建便签的结构
mContext = context; mContext = context;
mAlertDate = 0; mAlertDate = 0;
mModifiedDate = System.currentTimeMillis(); mModifiedDate = System.currentTimeMillis();
@ -115,7 +115,7 @@ public class WorkingNote {
} }
// Existing note construct // Existing note construct
private WorkingNote(Context context, long noteId, long folderId) { private WorkingNote(Context context, long noteId, long folderId) { //存在的的便签结构
mContext = context; mContext = context;
mNoteId = noteId; mNoteId = noteId;
mFolderId = folderId; mFolderId = folderId;
@ -124,13 +124,13 @@ public class WorkingNote {
loadNote(); loadNote();
} }
private void loadNote() { private void loadNote() { //查询并加载当前便签的详细信息
Cursor cursor = mContext.getContentResolver().query( Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null); null, null); //返回一个Cursor对象
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) { //将查询结构的列值返回当前便签
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
@ -141,70 +141,70 @@ public class WorkingNote {
cursor.close(); cursor.close();
} else { } else {
Log.e(TAG, "No note with id:" + mNoteId); Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId); throw new IllegalArgumentException("Unable to find note with id " + mNoteId); //抛出异常表示未找到指定id的便签
} }
loadNoteData(); loadNoteData(); //加载便签内容和其他数据
} }
private void loadNoteData() { private void loadNoteData() { //加载便签内容数据
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] { DataColumns.NOTE_ID + "=?", new String[] { //获取与便签id相对应的所有数据
String.valueOf(mNoteId) String.valueOf(mNoteId)
}, null); }, null); //遍历Cursor对象所有数据
if (cursor != null) { if (cursor != null) {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN); String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) { if (DataConstants.NOTE.equals(type)) { //类型相对应则设置WorkingNote对象成员变量
mContent = cursor.getString(DATA_CONTENT_COLUMN); mContent = cursor.getString(DATA_CONTENT_COLUMN);
mMode = cursor.getInt(DATA_MODE_COLUMN); mMode = cursor.getInt(DATA_MODE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
} else if (DataConstants.CALL_NOTE.equals(type)) { } else if (DataConstants.CALL_NOTE.equals(type)) {
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); //设置为Notes对象的成员变量
} else { } else {
Log.d(TAG, "Wrong note type with type:" + type); Log.d(TAG, "Wrong note type with type:" + type); //抛出异常,错误的数据类型
} }
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
} else { } else {
Log.e(TAG, "No data with id:" + mNoteId); Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's 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, public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, //上下文对象、文件夹id、组件id、组件类型、默认背景颜色id
int widgetType, int defaultBgColorId) { int widgetType, int defaultBgColorId) { //创建空白便签
WorkingNote note = new WorkingNote(context, folderId); WorkingNote note = new WorkingNote(context, folderId); //用上下文对象、父文件夹id创建WorkingNote
note.setBgColorId(defaultBgColorId); note.setBgColorId(defaultBgColorId);
note.setWidgetId(widgetId); note.setWidgetId(widgetId);
note.setWidgetType(widgetType); note.setWidgetType(widgetType);
return note; return note; //设置变量并返回
} }
public static WorkingNote load(Context context, long id) { public static WorkingNote load(Context context, long id) { //用上下文对象和id创建新的WorkingNote对象
return new WorkingNote(context, id, 0); return new WorkingNote(context, id, 0);
} }
public synchronized boolean saveNote() { public synchronized boolean saveNote() { //保存便签
if (isWorthSaving()) { if (isWorthSaving()) { //判断是否保存
if (!existInDatabase()) { if (!existInDatabase()) { //是否存在与数据库
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { //不存在则获取id
Log.e(TAG, "Create new note fail with id:" + mNoteId); Log.e(TAG, "Create new note fail with id:" + mNoteId); //日志记录
return false; return false;
} }
} }
mNote.syncNote(mContext, mNoteId); mNote.syncNote(mContext, mNoteId); //同步到数据库中
/** /**
* Update widget content if there exist any widget of this note * Update widget content if there exist any widget of this note
*/ */
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID //诺该便签与组件关联
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mWidgetType != Notes.TYPE_WIDGET_INVALIDE
&& mNoteSettingStatusListener != null) { && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged(); mNoteSettingStatusListener.onWidgetChanged(); //更新组件
} }
return true; return true;
} else { } else {
@ -214,155 +214,155 @@ public class WorkingNote {
public boolean existInDatabase() { public boolean existInDatabase() {
return mNoteId > 0; return mNoteId > 0;
} } //判断便签是否存在数据库
private boolean isWorthSaving() { private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) //不存在于数据库且为空,或者没本地修改
|| (existInDatabase() && !mNote.isLocalModified())) { || (existInDatabase() && !mNote.isLocalModified())) {
return false; return false;
} else { } else {
return true; return true;
} }
} } //判断是否值得保存
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l; mNoteSettingStatusListener = l;
} } //设置便签状态监视器
public void setAlertDate(long date, boolean set) { public void setAlertDate(long date, boolean set) { //设置提醒
if (date != mAlertDate) { if (date != mAlertDate) { //判断传入是否相同
mAlertDate = date; mAlertDate = date; //更新
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); //更新数据库
} }
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set); mNoteSettingStatusListener.onClockAlertChanged(date, set); //调用监听器
} }
} }
public void markDeleted(boolean mark) { public void markDeleted(boolean mark) { //标记删除标志
mIsDeleted = mark; mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged(); mNoteSettingStatusListener.onWidgetChanged(); //已删除则更新组件
} }
} }
public void setBgColorId(int id) { public void setBgColorId(int id) { //设置背景颜色
if (id != mBgColorId) { if (id != mBgColorId) { //检查
mBgColorId = id; mBgColorId = id; //更新设置
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onBackgroundColorChanged(); mNoteSettingStatusListener.onBackgroundColorChanged(); //通知监听器
} }
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); //更新数据库
} }
} }
public void setCheckListMode(int mode) { public void setCheckListMode(int mode) { //设置便签的检查清单模式
if (mMode != mode) { if (mMode != mode) { //检查
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); //通知
} }
mMode = mode; mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); //更新数据库
} }
} }
public void setWidgetType(int type) { public void setWidgetType(int type) { //设置组件类型
if (type != mWidgetType) { if (type != mWidgetType) {
mWidgetType = type; mWidgetType = type;
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); //更新数据库
} }
} }
public void setWidgetId(int id) { public void setWidgetId(int id) { //设置组件id
if (id != mWidgetId) { if (id != mWidgetId) {
mWidgetId = id; mWidgetId = id;
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); //更新数据库
} }
} }
public void setWorkingText(String text) { public void setWorkingText(String text) { //设置文本
if (!TextUtils.equals(mContent, text)) { if (!TextUtils.equals(mContent, text)) {
mContent = text; mContent = text;
mNote.setTextData(DataColumns.CONTENT, mContent); mNote.setTextData(DataColumns.CONTENT, mContent); //更新
} }
} }
public void convertToCallNote(String phoneNumber, long callDate) { public void convertToCallNote(String phoneNumber, long callDate) { //转换为呼叫类型便签
mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate));
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber);
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); //修改父文件夹id为呼叫记录id
} }
public boolean hasClockAlert() { public boolean hasClockAlert() {
return (mAlertDate > 0 ? true : false); return (mAlertDate > 0 ? true : false);
} } //是否设置提醒数据
public String getContent() { public String getContent() {
return mContent; return mContent;
} } //获取内容
public long getAlertDate() { public long getAlertDate() {
return mAlertDate; return mAlertDate;
} } //获取提醒时间
public long getModifiedDate() { public long getModifiedDate() {
return mModifiedDate; return mModifiedDate;
} } //获取最后修改时间
public int getBgColorResId() { public int getBgColorResId() {
return NoteBgResources.getNoteBgResource(mBgColorId); return NoteBgResources.getNoteBgResource(mBgColorId);
} } //获取背景资源id
public int getBgColorId() { public int getBgColorId() {
return mBgColorId; return mBgColorId;
} } //获取背景颜色id
public int getTitleBgResId() { public int getTitleBgResId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId); return NoteBgResources.getNoteTitleBgResource(mBgColorId);
} } //获取标题栏背景颜色资源id
public int getCheckListMode() { public int getCheckListMode() {
return mMode; return mMode;
} } //获取检查清单模式
public long getNoteId() { public long getNoteId() {
return mNoteId; return mNoteId;
} } //获取便签id
public long getFolderId() { public long getFolderId() {
return mFolderId; return mFolderId;
} } //获取父文件夹id
public int getWidgetId() { public int getWidgetId() {
return mWidgetId; return mWidgetId;
} } //获取组件id
public int getWidgetType() { public int getWidgetType() {
return mWidgetType; return mWidgetType;
} } //获取组件类型
public interface NoteSettingChangedListener { public interface NoteSettingChangedListener { //接口
/** /**
* Called when the background color of current note has just changed * Called when the background color of current note has just changed
*/ */
void onBackgroundColorChanged(); void onBackgroundColorChanged(); //便签背景颜色变化时启动
/** /**
* Called when user set clock * Called when user set clock
*/ */
void onClockAlertChanged(long date, boolean set); void onClockAlertChanged(long date, boolean set); //设置提醒时间时启动
/** /**
* Call when user create note from widget * Call when user create note from widget
*/ */
void onWidgetChanged(); void onWidgetChanged(); //创建组件时启动
/** /**
* Call when switch between check list mode and normal mode * Call when switch between check list mode and normal mode
* @param oldMode is previous mode before change * @param oldMode is previous mode before change
* @param newMode is new mode * @param newMode is new mode
*/ */
void onCheckListModeChanged(int oldMode, int newMode); void onCheckListModeChanged(int oldMode, int newMode); //清单模式转为笔记模式时启动
} }
} }

Loading…
Cancel
Save