/* * 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; 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; private static final String TAG = "Note"; /** * 为数据库中添加新笔记生成一个新的笔记ID * @param context 上下文对象,用于访问应用程序的资源和其他内容提供者 * @param folderId 文件夹ID,表示新笔记将被添加到的文件夹 * @return 新创建的笔记的ID */ public static synchronized long getNewNoteId(Context context, long folderId) { 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, "获取笔记ID错误 :" + e.toString()); noteId = 0; } if (noteId == -1) { throw new IllegalStateException("错误的笔记ID:" + noteId); } return noteId; } public Note() { mNoteDiffValues = new ContentValues(); mNoteData = new NoteData(); } /** * 设置笔记的值 * @param key 设置的字段名 * @param 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()); } /** * 设置文本数据 * @param key 设置的字段名 * @param value 设置的字段值 */ public void setTextData(String key, String value) { mNoteData.setTextData(key, value); } /** * 设置文本数据ID * @param id 文本数据的ID */ public void setTextDataId(long id) { mNoteData.setTextDataId(id); } /** * 获取文本数据ID * @return 文本数据的ID */ public long getTextDataId() { return mNoteData.mTextDataId; } /** * 设置通话数据ID * @param id 通话数据的ID */ public void setCallDataId(long id) { mNoteData.setCallDataId(id); } /** * 设置通话数据 * @param key 设置的字段名 * @param value 设置的字段值 */ public void setCallData(String key, String value) { mNoteData.setCallData(key, value); } /** * 检查笔记是否被本地修改 * @return 如果笔记被本地修改则返回true,否则返回false */ public boolean isLocalModified() { return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); } /** * 同步笔记到数据库 * * @param context 上下文对象,用于访问应用程序的资源和其他内容提供者 * @param noteId 需要同步的笔记ID * @return 如果同步成功则返回true,否则返回false */ public boolean syncNote(Context context, long noteId) { if (noteId <= 0) { throw new IllegalArgumentException("错误的笔记ID:" + noteId);//记录更新错误的笔记 } if (!isLocalModified()) { return true; } if (context.getContentResolver().update( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, null) == 0) { Log.e(TAG, "更新笔记错误,不应该发生"); // 不返回,继续执行 } mNoteDiffValues.clear(); if (mNoteData.isLocalModified() && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { return false; } return true; } /** * 内部类NoteData,管理笔记的文本数据和通话数据 **/ private class NoteData { private long mTextDataId; private ContentValues mTextDataValues; private long mCallDataId; private ContentValues mCallDataValues; private static final String TAG = "NoteData"; public NoteData() { mTextDataValues = new ContentValues(); mCallDataValues = new ContentValues(); mTextDataId = 0; mCallDataId = 0; } /** * 检查数据是否被本地修改 * @return 如果数据被本地修改则返回true,否则返回false */ boolean isLocalModified() { return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; } /** * 设置文本数据ID * @param文本数据的ID */ void setTextDataId(long id) { if (id <= 0) { throw new IllegalArgumentException("文本数据ID应该大于0"); } mTextDataId = id; } /** * 设置通话数据ID * @param id 通话数据的ID */ void setCallDataId(long id) { if (id <= 0) { throw new IllegalArgumentException("通话数据ID应该大于0"); } mCallDataId = id; } /** * 设置通话数据的字段 * @param key 设置的字段名 * @param value 设置的字段值 */ void setCallData(String key, String value) {//字段名和字段值 mCallDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } /** * 设置文本数据 * * @param key 设置的字段名 * @param value 设置的字段值 */ void setTextData(String key, String value) { mTextDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } /** * 将数据推送 * @param context 上下文对象,用于访问应用程序的资源和其他内容提供者 * @param noteId 笔记的ID * @return 如果推送成功则返回Uri,否则返回null */ Uri pushIntoContentResolver(Context context, long noteId) { /** * 安全性检查 */ if (noteId <= 0) { throw new IllegalArgumentException("错误的笔记ID:" + noteId); //笔记ID } ArrayList operationList = new ArrayList(); ContentProviderOperation.Builder builder = null;//推送失败返回null if (mTextDataValues.size() > 0) {//字段值大于0,插入文本呢数据 mTextDataValues.put(DataColumns.NOTE_ID, noteId); 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, "插入新的文本数据失败,笔记ID" + 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) {//字段值大于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, "插入新的通话数据失败,笔记ID" + 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(); } 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; } } }