|
|
|
@ -14,48 +14,54 @@
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.model;//创建一个包,用于区别类名的命名空间
|
|
|
|
|
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 java.util.ArrayList;
|
|
|
|
|
import java.util.ArrayList;//系统的自带的可变数组的类
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Note {
|
|
|
|
|
private ContentValues mNoteDiffValues;
|
|
|
|
|
private NoteData mNoteData;
|
|
|
|
|
//mNoteDiffValues和mNoteData
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "Note";
|
|
|
|
|
/**
|
|
|
|
|
* Create a new note id for adding a new note to databases
|
|
|
|
|
* 创建一个新的笔记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);
|
|
|
|
|
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);
|
|
|
|
|
long createdTime = System.currentTimeMillis();//创建一个变量记录创建的时间
|
|
|
|
|
values.put(NoteColumns.CREATED_DATE, createdTime);//将创建日期的值付给 CREATED_DATE创建时间
|
|
|
|
|
values.put(NoteColumns.MODIFIED_DATE, createdTime);//将创建日期的值付给 MODIFIED_DATE修改时间
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);//把type的值赋为0
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);//把LOCAL_MODIFIED 本地修改 的值赋为1
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, folderId);//把文件夹id赋给PARENT_ID 父类id
|
|
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
|
|
|
|
|
|
|
|
|
|
long noteId = 0;
|
|
|
|
|
try {
|
|
|
|
|
noteId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
noteId = 0;
|
|
|
|
|
}
|
|
|
|
@ -153,7 +159,7 @@ public class Note {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setTextDataId(long id) {
|
|
|
|
|
if(id <= 0) {
|
|
|
|
|
if (id <= 0) {
|
|
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
|
|
}
|
|
|
|
|
mTextDataId = id;
|
|
|
|
@ -189,7 +195,7 @@ public class Note {
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
ContentProviderOperation.Builder builder = null;
|
|
|
|
|
|
|
|
|
|
if(mTextDataValues.size() > 0) {
|
|
|
|
|
if (mTextDataValues.size() > 0) {
|
|
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
|
|
if (mTextDataId == 0) {
|
|
|
|
|
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
|
|
|
|
@ -197,12 +203,14 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mTextDataId));
|
|
|
|
|
builder.withValues(mTextDataValues);
|
|
|
|
@ -211,7 +219,7 @@ 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);
|
|
|
|
@ -219,12 +227,14 @@ public class Note {
|
|
|
|
|
mCallDataValues);
|
|
|
|
|
try {
|
|
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Insert new call data fail with noteId" + noteId);
|
|
|
|
|
mCallDataValues.clear();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
|
|
Notes.CONTENT_DATA_URI, mCallDataId));
|
|
|
|
|
builder.withValues(mCallDataValues);
|
|
|
|
@ -239,10 +249,12 @@ public class Note {
|
|
|
|
|
Notes.AUTHORITY, operationList);
|
|
|
|
|
return (results == null || results.length == 0 || results[0] == null) ? null
|
|
|
|
|
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
return null;
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
}
|
|
|
|
|
catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|