|
|
|
@ -15,15 +15,15 @@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
@ -49,16 +49,17 @@ 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 {
|
|
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
} 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);
|
|
|
|
|
}
|
|
|
|
@ -68,37 +69,37 @@ public class Note {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}//设置数据库表格的标签文本内容的数据
|
|
|
|
|
|
|
|
|
|
public void setTextDataId(long id) {
|
|
|
|
|
mNoteData.setTextDataId(id);
|
|
|
|
|
}
|
|
|
|
|
}//设置文本数据的ID
|
|
|
|
|
|
|
|
|
|
public long getTextDataId() {
|
|
|
|
|
return mNoteData.mTextDataId;
|
|
|
|
|
}
|
|
|
|
|
}//得到文本数据的ID
|
|
|
|
|
|
|
|
|
|
public void setCallDataId(long id) {
|
|
|
|
|
mNoteData.setCallDataId(id);
|
|
|
|
|
}
|
|
|
|
|
}//设置电话号码数据的ID
|
|
|
|
|
|
|
|
|
|
public void setCallData(String key, String value) {
|
|
|
|
|
mNoteData.setCallData(key, value);
|
|
|
|
|
}
|
|
|
|
|
}//得到电话号码数据的ID
|
|
|
|
|
|
|
|
|
|
public boolean isLocalModified() {
|
|
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
|
|
|
|
|
}
|
|
|
|
|
}//判断是否是本地修改
|
|
|
|
|
|
|
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
@ -128,16 +129,16 @@ 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";
|
|
|
|
|
|
|
|
|
@ -147,26 +148,25 @@ public class Note {
|
|
|
|
|
mTextDataId = 0;
|
|
|
|
|
mCallDataId = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean isLocalModified() {
|
|
|
|
|
boolean isLocalModified() {//判断当前对象是否有本地修改
|
|
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setTextDataId(long id) {
|
|
|
|
|
void setTextDataId(long id) {//设置文本数据的ID
|
|
|
|
|
if(id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
|
|
}
|
|
|
|
|
mTextDataId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setCallDataId(long id) {
|
|
|
|
|
void setCallDataId(long id) {//设置电话号码数据的ID
|
|
|
|
|
if (id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Call data id should larger than 0");
|
|
|
|
|
}
|
|
|
|
|
mCallDataId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setCallData(String key, String value) {
|
|
|
|
|
void setCallData(String key, String value) {////得到电话号码数据的ID
|
|
|
|
|
mCallDataValues.put(key, value);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
|
@ -177,17 +177,17 @@ 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) {
|
|
|
|
|
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) {
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
@ -197,7 +197,7 @@ public class Note {
|
|
|
|
|
mTextDataValues);
|
|
|
|
|
try {
|
|
|
|
|
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
} catch (NumberFormatException e) {//出错则清空
|
|
|
|
|
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
@ -209,7 +209,7 @@ public class Note {
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
mTextDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
}//把文本数据存入DataColumns
|
|
|
|
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {
|
|
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
@ -231,7 +231,7 @@ public class Note {
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
}
|
|
|
|
|
}//把电话号码数据存入DataColumns
|
|
|
|
|
|
|
|
|
|
if (operationList.size() > 0) {
|
|
|
|
|
try {
|
|
|
|
@ -246,7 +246,7 @@ public class Note {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}//存储过程中的异常处理
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|