|
|
|
@ -13,8 +13,10 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
/*这个类主要表示一个笔记,包含笔记的内容、修改值和数据。它提供了方法来创建新的笔记ID、设置笔记值、同步笔记到数据库等。内部类`NoteData`用于封装笔记的数据,包括文本数据和通话数据。*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.model;
|
|
|
|
|
|
|
|
|
|
import android.content.ContentProviderOperation;
|
|
|
|
|
import android.content.ContentProviderResult;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
@ -33,16 +35,25 @@ import net.micode.notes.data.Notes.TextNote;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Note类表示一个笔记,包含笔记的内容、修改值和数据。
|
|
|
|
|
*/
|
|
|
|
|
public class Note {
|
|
|
|
|
// 笔记的差异值,用于记录笔记的变更
|
|
|
|
|
private ContentValues mNoteDiffValues;
|
|
|
|
|
// 笔记数据的封装
|
|
|
|
|
private NoteData mNoteData;
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = "Note";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new note id for adding a new note to databases
|
|
|
|
|
* 为新笔记创建一个新的笔记ID。
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param folderId 笔记所属的文件夹ID
|
|
|
|
|
* @return 新创建的笔记ID
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized long getNewNoteId(Context context, long folderId) {
|
|
|
|
|
// Create a new note in the database
|
|
|
|
|
// 在数据库中创建一个新的笔记
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
long createdTime = System.currentTimeMillis();
|
|
|
|
|
values.put(NoteColumns.CREATED_DATE, createdTime);
|
|
|
|
@ -65,41 +76,81 @@ public class Note {
|
|
|
|
|
return noteId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Note类的构造函数,初始化笔记的差异值和数据。
|
|
|
|
|
*/
|
|
|
|
|
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。
|
|
|
|
|
* @param id 文本数据ID
|
|
|
|
|
*/
|
|
|
|
|
public void setTextDataId(long id) {
|
|
|
|
|
mNoteData.setTextDataId(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取文本数据ID。
|
|
|
|
|
* @return 文本数据ID
|
|
|
|
|
*/
|
|
|
|
|
public long getTextDataId() {
|
|
|
|
|
return mNoteData.mTextDataId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话数据ID。
|
|
|
|
|
* @param id 通话数据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);
|
|
|
|
@ -109,11 +160,7 @@ public class Note {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and
|
|
|
|
|
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
|
|
|
|
|
* note data info
|
|
|
|
|
*/
|
|
|
|
|
// 更新笔记信息
|
|
|
|
|
if (context.getContentResolver().update(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
|
|
|
|
|
null) == 0) {
|
|
|
|
@ -122,6 +169,7 @@ public class Note {
|
|
|
|
|
}
|
|
|
|
|
mNoteDiffValues.clear();
|
|
|
|
|
|
|
|
|
|
// 更新笔记数据
|
|
|
|
|
if (mNoteData.isLocalModified()
|
|
|
|
|
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
|
|
|
|
|
return false;
|
|
|
|
@ -130,17 +178,24 @@ public class Note {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 笔记数据的内部类。
|
|
|
|
|
*/
|
|
|
|
|
private class NoteData {
|
|
|
|
|
// 文本数据ID
|
|
|
|
|
private long mTextDataId;
|
|
|
|
|
|
|
|
|
|
// 文本数据值
|
|
|
|
|
private ContentValues mTextDataValues;
|
|
|
|
|
|
|
|
|
|
// 通话数据ID
|
|
|
|
|
private long mCallDataId;
|
|
|
|
|
|
|
|
|
|
// 通话数据值
|
|
|
|
|
private ContentValues mCallDataValues;
|
|
|
|
|
|
|
|
|
|
// 日志标签
|
|
|
|
|
private static final String TAG = "NoteData";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* NoteData类的构造函数,初始化文本数据和通话数据。
|
|
|
|
|
*/
|
|
|
|
|
public NoteData() {
|
|
|
|
|
mTextDataValues = new ContentValues();
|
|
|
|
|
mCallDataValues = new ContentValues();
|
|
|
|
@ -148,10 +203,18 @@ public class Note {
|
|
|
|
|
mCallDataId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查笔记数据是否本地修改过。
|
|
|
|
|
* @return 如果文本数据或通话数据本地修改过,返回true。
|
|
|
|
|
*/
|
|
|
|
|
boolean isLocalModified() {
|
|
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置文本数据ID。
|
|
|
|
|
* @param id 文本数据ID
|
|
|
|
|
*/
|
|
|
|
|
void setTextDataId(long id) {
|
|
|
|
|
if(id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
|
@ -159,6 +222,10 @@ public class Note {
|
|
|
|
|
mTextDataId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置通话数据ID。
|
|
|
|
|
* @param id 通话数据ID
|
|
|
|
|
*/
|
|
|
|
|
void setCallDataId(long id) {
|
|
|
|
|
if (id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Call data id should larger than 0");
|
|
|
|
@ -166,29 +233,45 @@ public class Note {
|
|
|
|
|
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。
|
|
|
|
|
*/
|
|
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
|
|
/**
|
|
|
|
|
* Check for safety
|
|
|
|
|
*/
|
|
|
|
|
// 检查参数
|
|
|
|
|
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) {
|
|
|
|
@ -211,7 +294,8 @@ public class Note {
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {
|
|
|
|
|
// 推送通话数据
|
|
|
|
|
if(mCallDataValues.size() > 0) {
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mCallDataId == 0) {
|
|
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
|
@ -232,22 +316,21 @@ public class Note {
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
// 执行操作
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|