|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
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;//系统的自带的可变数组的类
|
|
|
|
|
|
|
|
|
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);//将创建日期的值付给 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);
|
|
|
//getContentResolver()函数是实现外部应用对于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;
|
|
|
}//异常处理
|
|
|
|
|
|
if (noteId == -1) {
|
|
|
throw new IllegalStateException("Wrong note id:" + noteId);
|
|
|
} // 如果发生错误,输出错误ID
|
|
|
return noteId;
|
|
|
}
|
|
|
|
|
|
public Note() {
|
|
|
mNoteDiffValues = new ContentValues(); //定义一个新变量,存储便签属性
|
|
|
mNoteData = new NoteData();//定义一个新变量,存出便签内容
|
|
|
}
|
|
|
|
|
|
public void setNoteValue(String key, String value) {
|
|
|
mNoteDiffValues.put(key, value); //设置便签属性传入ket和value
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //设置新创建的便签更改次数为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); //设置电话的内容和数据
|
|
|
}
|
|
|
|
|
|
public boolean isLocalModified() {
|
|
|
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); //
|
|
|
}//判断是否为本地产生的修改
|
|
|
|
|
|
public boolean syncNote(Context context, long noteId) {
|
|
|
if (noteId <= 0) {
|
|
|
throw new IllegalArgumentException("Wrong note id:" + noteId);
|
|
|
} //报告错误信息
|
|
|
|
|
|
if (!isLocalModified()) {
|
|
|
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
|
|
|
*/
|
|
|
|
|
|
/*
|
|
|
理论上,一旦数据发生变化,应在{@link NoteColumns#LOCAL_MODIFIED}和
|
|
|
{@link NotesColumns#MODIFIED_DATE}上更新注释。为了数据安全,
|
|
|
虽然更新笔记失败,但我们也会更新笔记数据信息
|
|
|
*/
|
|
|
|
|
|
if (context.getContentResolver().update(
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
|
|
|
null) == 0) {
|
|
|
Log.e(TAG, "Update note error, should not happen");
|
|
|
// Do not return, fall through
|
|
|
}
|
|
|
mNoteDiffValues.clear();
|
|
|
|
|
|
if (mNoteData.isLocalModified()
|
|
|
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}//用于判断便签是否正确同步
|
|
|
|
|
|
private class NoteData {
|
|
|
private long mTextDataId; //定义便签数据的文件数据ID
|
|
|
|
|
|
private ContentValues mTextDataValues; //定义文件数据
|
|
|
|
|
|
private long mCallDataId;//定义便签电话ID
|
|
|
|
|
|
private ContentValues mCallDataValues;//定义便签电话数据
|
|
|
|
|
|
private static final String TAG = "NoteData";
|
|
|
|
|
|
public NoteData() {
|
|
|
mTextDataValues = new ContentValues();
|
|
|
mCallDataValues = new ContentValues();
|
|
|
mTextDataId = 0;
|
|
|
mCallDataId = 0;
|
|
|
} //构造函数,定义文件数据ID和电话数据ID为0,创建便签电话数据和文件数据类
|
|
|
|
|
|
boolean isLocalModified() {
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
}//判断是否被本地修改,返回是否修改文本数据或者电话数据
|
|
|
|
|
|
void setTextDataId(long id) {
|
|
|
if (id <= 0) {
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
} //为文本数据ID的定义提供错误判断
|
|
|
mTextDataId = id; //设置文本数据ID
|
|
|
}
|
|
|
|
|
|
void setCallDataId(long id) {
|
|
|
if (id <= 0) {
|
|
|
throw new IllegalArgumentException("Call data id should larger than 0");
|
|
|
}//为电话数据ID的定义提供错误判断
|
|
|
mCallDataId = id;//设置电话数据ID
|
|
|
}
|
|
|
|
|
|
void setCallData(String key, String value) {
|
|
|
mCallDataValues.put(key, value); //设置电话数据的数据和内容
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); //设置电话数据的本地修改为1
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); //设置修改时间为本地时间
|
|
|
}
|
|
|
|
|
|
void setTextData(String key, String value) {
|
|
|
mTextDataValues.put(key, value); //设置文本数据
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);//设置文本数据的本地修改为1
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());//设置修改时间为本地时间
|
|
|
}
|
|
|
|
|
|
Uri pushIntoContentResolver(Context context, long noteId) {
|
|
|
|
|
|
/*
|
|
|
*下面函数的作用是将新的数据通过Uri的操作存储到数据库
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 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) { //如果文本数据的值不为0
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId); //存入note数据ID
|
|
|
if (mTextDataId == 0) {
|
|
|
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
mTextDataValues);
|
|
|
try {
|
|
|
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
}
|
|
|
catch (NumberFormatException e) {
|
|
|
Log.e(TAG, "Insert new text data fail with noteId" + noteId);
|
|
|
mTextDataValues.clear();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
Notes.CONTENT_DATA_URI, mTextDataId));
|
|
|
builder.withValues(mTextDataValues);
|
|
|
operationList.add(builder.build());
|
|
|
}
|
|
|
mTextDataValues.clear();
|
|
|
}//把文本数据存入到DataColumns
|
|
|
|
|
|
if (mCallDataValues.size() > 0) {
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
if (mCallDataId == 0) {
|
|
|
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
|
|
|
mCallDataValues);
|
|
|
try {
|
|
|
setCallDataId(Long.valueOf(uri.getPathSegments().get(1)));
|
|
|
}
|
|
|
catch (NumberFormatException e) {
|
|
|
Log.e(TAG, "Insert new call data fail with noteId" + noteId);
|
|
|
mCallDataValues.clear();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
Notes.CONTENT_DATA_URI, mCallDataId));
|
|
|
builder.withValues(mCallDataValues);
|
|
|
operationList.add(builder.build());
|
|
|
}
|
|
|
mCallDataValues.clear();
|
|
|
}//把电话数据存入到DataColumns
|
|
|
|
|
|
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;
|
|
|
}//存储过程中对于异常情况的处理
|
|
|
}
|
|
|
}
|