|
|
/*
|
|
|
* 单个便签项
|
|
|
* 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;//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;//导入Java,util,ArrayList
|
|
|
|
|
|
|
|
|
public class Note {//定义note类,处理单个便签
|
|
|
private ContentValues mNoteDiffValues;//声明一个ContentValues私有变量ContentValues,用来存储note与上次修改后的改动
|
|
|
private NoteData mNoteData;//声明一个私有变量NoteData,用来记录单个便签的基本信息
|
|
|
private static final String TAG = "Note";//设置软件标签
|
|
|
/**
|
|
|
* Create a new note id for adding a new note to databases
|
|
|
*/
|
|
|
public static synchronized long getNewNoteId(Context context, long folderId) {
|
|
|
//Create a new note in the database
|
|
|
/*
|
|
|
* 作用:为新便签在数据库里边创建一个新的ID
|
|
|
* 实现:创建一个内容集合,存入创建便签的时间,最后修改的时间,再根据当前便签所处的上下文和文件夹的ID,从内容解析器中得到的路径uri里面
|
|
|
* 获取新的便签的ID。最后判断ID是否合法,然后返回
|
|
|
* 参数:@context:便签所处的上下文
|
|
|
* @folderId:所处的便签文件夹ID
|
|
|
* @return:新创建便签的ID
|
|
|
*/
|
|
|
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);
|
|
|
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
return noteId;
|
|
|
}
|
|
|
|
|
|
public Note() {//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容
|
|
|
mNoteDiffValues = new ContentValues();
|
|
|
mNoteData = new NoteData();
|
|
|
}
|
|
|
|
|
|
public void setNoteValue(String key, String value) {
|
|
|
/*
|
|
|
* 作用:设置便签的属性
|
|
|
* 实现:通过传入key,指定修改便签的属性;传入value,指定修改的值,通过属性管理器mNoteDiffValues写入。最后,改变最后修改时间
|
|
|
* 参数:@key:要修改的属性
|
|
|
* @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) {//设置文本数据的ID
|
|
|
mNoteData.setTextDataId(id);
|
|
|
}
|
|
|
|
|
|
public long getTextDataId() {//获取文本数据的ID
|
|
|
return mNoteData.mTextDataId;
|
|
|
}
|
|
|
|
|
|
public void setCallDataId(long id) {//设置电话号码数据的ID
|
|
|
mNoteData.setCallDataId(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
|
|
|
*/
|
|
|
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() {//NoteData的构造函数,初始化四个变量
|
|
|
mTextDataValues = new ContentValues();
|
|
|
mCallDataValues = new ContentValues();
|
|
|
mTextDataId = 0;
|
|
|
mCallDataId = 0;
|
|
|
}
|
|
|
|
|
|
boolean isLocalModified() {//判断是否本地修改
|
|
|
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
|
|
|
}
|
|
|
|
|
|
void setTextDataId(long id) {//设置文本数据的ID
|
|
|
if(id <= 0) {
|
|
|
throw new IllegalArgumentException("Text data id should larger than 0");
|
|
|
}
|
|
|
mTextDataId = 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) {//设置电话号码数据内容,并且保存修改时间
|
|
|
mCallDataValues.put(key, value);
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
|
|
|
}
|
|
|
|
|
|
void setTextData(String key, String value) {//设置文本数据
|
|
|
mTextDataValues.put(key, value);
|
|
|
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 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) {//把文本数据存入DataColumns
|
|
|
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
if (mTextDataId == 0) {//文本数据ID为零,意味着这个id是新建默认的id
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
if(mCallDataValues.size() > 0) {//电话号码同步
|
|
|
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
|
|
|
if (mCallDataId == 0) {//将电话号码的id设定为uri提供的id
|
|
|
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 {//当电话号码不为新建时,更新电话号码ID
|
|
|
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
|
|
|
Notes.CONTENT_DATA_URI, mCallDataId));
|
|
|
builder.withValues(mCallDataValues);
|
|
|
operationList.add(builder.build());
|
|
|
}
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|