|
|
|
@ -15,28 +15,28 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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 android.content.ContentProviderOperation;//批量的更新、插入、删除数据。
|
|
|
|
|
import android.content.ContentProviderResult;//操作的结果
|
|
|
|
|
import android.content.ContentUris;//添加和获取Uri后面的ID
|
|
|
|
|
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 net.micode.notes.data.Notes;//导入Note
|
|
|
|
|
import net.micode.notes.data.Notes.CallNote;//导入Notes.CallNote
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;//导入Notes.DataColumns
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;//导入Notes.NoteColumns
|
|
|
|
|
import net.micode.notes.data.Notes.TextNote;//导入Notes.TextNote
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.ArrayList;//输入Scanner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Note {
|
|
|
|
|
private ContentValues mNoteDiffValues;
|
|
|
|
|
private NoteData mNoteData;
|
|
|
|
|
private ContentValues mNoteDiffValues;//属性
|
|
|
|
|
private NoteData mNoteData;//属性
|
|
|
|
|
private static final String TAG = "Note";
|
|
|
|
|
/**
|
|
|
|
|
* Create a new note id for adding a new note to databases
|
|
|
|
@ -49,8 +49,10 @@ public class Note {
|
|
|
|
|
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);
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, folderId);//将数据写入数据库表格
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
|
|
|
|
|
//ContentResolver()主要是实现外部应用对ContentProvider中的数据
|
|
|
|
|
//进行添加、删除、修改和查询操作
|
|
|
|
|
|
|
|
|
|
long noteId = 0;
|
|
|
|
|
try {
|
|
|
|
@ -58,49 +60,49 @@ public class Note {
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
noteId = 0;
|
|
|
|
|
}
|
|
|
|
|
}//try-catch异常处理
|
|
|
|
|
if (noteId == -1) {
|
|
|
|
|
throw new IllegalStateException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
|
return noteId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Note() {
|
|
|
|
|
public Note() {//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容
|
|
|
|
|
mNoteDiffValues = new ContentValues();
|
|
|
|
|
mNoteData = new NoteData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNoteValue(String key, String 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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTextData(String key, String value) {
|
|
|
|
|
public void setTextData(String key, String value) {//设置数据库表格的标签文本内容的数据
|
|
|
|
|
mNoteData.setTextData(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTextDataId(long id) {
|
|
|
|
|
public void setTextDataId(long id) {//设置文本数据的ID
|
|
|
|
|
mNoteData.setTextDataId(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getTextDataId() {
|
|
|
|
|
public long getTextDataId() {//得到文本数据的ID
|
|
|
|
|
return mNoteData.mTextDataId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCallDataId(long id) {
|
|
|
|
|
public void setCallDataId(long id) {//设置电话号码数据的ID
|
|
|
|
|
mNoteData.setCallDataId(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCallData(String key, String value) {
|
|
|
|
|
public void setCallData(String key, String value) {//得到电话号码数据的ID
|
|
|
|
|
mNoteData.setCallData(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isLocalModified() {
|
|
|
|
|
public boolean isLocalModified() {//判断是否是本地修改
|
|
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
|
|
public boolean syncNote(Context context, long noteId) {//判断数据是否同步
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
@ -130,17 +132,17 @@ public class Note {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NoteData {
|
|
|
|
|
private class NoteData {//定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据
|
|
|
|
|
private long mTextDataId;
|
|
|
|
|
|
|
|
|
|
private ContentValues mTextDataValues;
|
|
|
|
|
private ContentValues mTextDataValues;//文本数据
|
|
|
|
|
|
|
|
|
|
private long mCallDataId;
|
|
|
|
|
|
|
|
|
|
private ContentValues mCallDataValues;
|
|
|
|
|
private ContentValues mCallDataValues;//电话号码数据
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NoteData";
|
|
|
|
|
|
|
|
|
|
//下面是上述几个函数的具体实现
|
|
|
|
|
public NoteData() {
|
|
|
|
|
mTextDataValues = new ContentValues();
|
|
|
|
|
mCallDataValues = new ContentValues();
|
|
|
|
@ -177,19 +179,19 @@ public class Note {
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//下面函数的作用是将新的数据通过Uri的操作存储到数据库
|
|
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
|
|
/**
|
|
|
|
|
* Check for safety
|
|
|
|
|
*/
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
if (noteId <= 0) {//判断数据是否合法
|
|
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
ContentProviderOperation.Builder builder = null;
|
|
|
|
|
ContentProviderOperation.Builder builder = null;//数据库的操作列表
|
|
|
|
|
|
|
|
|
|
if(mTextDataValues.size() > 0) {
|
|
|
|
|
if(mTextDataValues.size() > 0) {//把文本数据存入DataColumns
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mTextDataId == 0) {
|
|
|
|
|
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
|
|
|
|
@ -211,7 +213,7 @@ public class Note {
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {
|
|
|
|
|
if(mCallDataValues.size() > 0) {//把电话号码数据存入DataColumns
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mCallDataId == 0) {
|
|
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
|
@ -233,7 +235,7 @@ public class Note {
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (operationList.size() > 0) {
|
|
|
|
|
if (operationList.size() > 0) {//存储过程中的异常处理
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = context.getContentResolver().applyBatch(
|
|
|
|
|
Notes.AUTHORITY, operationList);
|
|
|
|
|