From 62927cf9dfc33ff52de2697ba66e201ac2b56696 Mon Sep 17 00:00:00 2001 From: LZ <2929718516@qq.com> Date: Thu, 28 Nov 2024 23:35:56 +0800 Subject: [PATCH] =?UTF-8?q?Note=20=E7=B1=BB=E6=8F=90=E4=BE=9B=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=AE=8C=E6=95=B4=E7=9A=84=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=88=9B=E5=BB=BA=E3=80=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=80=81=E7=AE=A1=E7=90=86=E5=92=8C=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BE=BF=E7=AD=BE=E6=95=B0=E6=8D=AE=E3=80=82=E5=AE=83=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BA=86=E4=BE=BF=E7=AD=BE=E7=9A=84=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=EF=BC=8C=E4=BB=A5=E5=8F=8A=E4=B8=8E=E4=BE=BF?= =?UTF-8?q?=E7=AD=BE=E7=9B=B8=E5=85=B3=E7=9A=84=E6=96=87=E6=9C=AC=E5=92=8C?= =?UTF-8?q?=E9=80=9A=E8=AF=9D=E6=95=B0=E6=8D=AE=EF=BC=8C=E5=B9=B6=E4=B8=94?= =?UTF-8?q?=E8=83=BD=E5=A4=9F=E5=B0=86=E8=BF=99=E4=BA=9B=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=88=B0=E5=90=8E=E7=AB=AF=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E6=8C=81=E4=B9=85=E5=8C=96=E5=92=8C=E4=B8=80=E8=87=B4=E6=80=A7?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note.java | 232 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 231 insertions(+), 1 deletion(-) diff --git a/Note.java b/Note.java index ee40f37..63b25b9 100644 --- a/Note.java +++ b/Note.java @@ -1 +1,231 @@ -mi \ No newline at end of file +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 以下是MiCode开源社区的版权声明,表明该文件的版权所有者为MiCode开源社区,并提供了网站链接。 + * + * 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 + * 该文件遵循Apache 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; +// 声明该文件属于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; + +// 定义Note类,用于管理便签的数据和操作。 +public class Note { + private ContentValues mNoteDiffValues; // 用于存储便签的差异值,用于更新操作。 + private NoteData mNoteData; // 存储便签的额外数据,如文本和通话数据。 + private static final String TAG = "Note"; // 用于日志输出的标签。 + + /** + * 创建一个新的便签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); // 设置父ID,即文件夹ID。 + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); // 插入便签到数据库。 + + long noteId = 0; + try { + noteId = Long.valueOf(uri.getPathSegments().get(1)); // 从URI中获取新创建的便签ID。 + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); // 记录日志,如果ID获取失败。 + noteId = 0; + } + if (noteId == -1) { + throw new IllegalStateException("Wrong note id:" + noteId); // 如果ID错误,抛出异常。 + } + return noteId; // 返回新创建的便签ID。 + } + + public Note() { + mNoteDiffValues = new ContentValues(); // 初始化差异值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); + } + + // 设置文本数据ID。 + public void setTextDataId(long id) { + mNoteData.setTextDataId(id); + } + + // 获取文本数据ID。 + public long getTextDataId() { + return mNoteData.mTextDataId; + } + + // 设置通话数据ID。 + public void setCallDataId(long 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; + } + + /** + * 理论上,一旦数据改变,便签应该在{@link NoteColumns#LOCAL_MODIFIED}和 + * {@link NoteColumns#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; + } + + // 定义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; + } + + // 检查是否有本地修改。 + boolean isLocalModified() { + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + + // 设置文本数据ID。 + void setTextDataId(long id) { + if(id <= 0) { + throw new IllegalArgumentException("Text data id should larger than 0"); + } + mTextDataId = id; + } + + // 设置通话数据ID。 + void setCallDataId(long 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) { + /** + * 检查安全 + */ + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + ArrayList operationList = new ArrayList(); + ContentProviderOperation.Builder builder = null; + + if(mTextDataValues.size() > 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, "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) { + mCallData \ No newline at end of file