From 03f1551b16b6c0c02a106a7bffd15bed7bfddd4e Mon Sep 17 00:00:00 2001 From: p4ocgtxi5 <2678576348@qq.com> Date: Tue, 31 Dec 2024 22:19:40 +0800 Subject: [PATCH] ADD file via upload --- src/Note_1.java | 428 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 428 insertions(+) create mode 100644 src/Note_1.java diff --git a/src/Note_1.java b/src/Note_1.java new file mode 100644 index 0000000..0517f04 --- /dev/null +++ b/src/Note_1.java @@ -0,0 +1,428 @@ +<<<<<<< HEAD +// 包声明和导入必要的Android框架包以及项目特定的数据类 + +public class Note { + // 私有成员变量,用来保存笔记的修改值和数据内容 + private ContentValues mNoteDiffValues; + private NoteData mNoteData; + + // 日志标签,方便在日志中识别输出信息来源于哪个类 + private static final String TAG = "Note"; + + /** + * 获取一个新的笔记ID,用于添加新的笔记到数据库。 + * 该方法是静态同步的,确保同一时间只有一个线程可以获取新ID,以避免冲突。 + */ + public static synchronized long getNewNoteId(Context context, long folderId) { + // 创建一个ContentValues实例,用于存储要插入数据库的笔记信息 + 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); + // 设置父级文件夹ID + values.put(NoteColumns.PARENT_ID, folderId); + + // 插入笔记记录并获取返回的URI + Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); + + // 解析URI路径片段以获得新插入笔记的ID +======= +/* + * 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"; + /** + * 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 + 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); + +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + 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); + } +<<<<<<< HEAD + return noteId; // 返回新创建的笔记ID + } + + // 构造函数初始化成员变量 +======= + return noteId; + } + +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public Note() { + mNoteDiffValues = new ContentValues(); + mNoteData = new NoteData(); + } + +<<<<<<< HEAD + // 设置笔记属性值的方法 + public void setNoteValue(String key, String value) { + // 更新或设置指定键对应的值,并标记为本地修改 +======= + public void setNoteValue(String key, String value) { +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + mNoteDiffValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + +<<<<<<< HEAD + // 设置文本数据的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public void setTextData(String key, String value) { + mNoteData.setTextData(key, value); + } + +<<<<<<< HEAD + // 设置文本数据ID的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public void setTextDataId(long id) { + mNoteData.setTextDataId(id); + } + +<<<<<<< HEAD + // 获取文本数据ID的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public long getTextDataId() { + return mNoteData.mTextDataId; + } + +<<<<<<< HEAD + // 设置通话记录数据ID的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public void setCallDataId(long id) { + mNoteData.setCallDataId(id); + } + +<<<<<<< HEAD + // 设置通话记录数据的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public void setCallData(String key, String value) { + mNoteData.setCallData(key, value); + } + +<<<<<<< HEAD + // 检查是否有本地修改过的数据 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public boolean isLocalModified() { + return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); + } + +<<<<<<< HEAD + /** + * 同步笔记到数据库的方法。 + * 如果笔记ID无效则抛出异常;如果没有本地修改,则直接返回true。 + * 尝试更新笔记记录,即使失败也会继续尝试更新数据部分。 + */ +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public boolean syncNote(Context context, long noteId) { + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + + if (!isLocalModified()) { + return true; + } + +<<<<<<< HEAD + // 更新笔记的基本信息 +======= + /** + * 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 + */ +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + if (context.getContentResolver().update( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, + null) == 0) { + Log.e(TAG, "Update note error, should not happen"); +<<<<<<< HEAD + // 即使更新失败也继续执行后续操作 + } + mNoteDiffValues.clear(); // 清除已经同步的内容 + + // 同步额外的数据(如文本或通话记录) +======= + // Do not return, fall through + } + mNoteDiffValues.clear(); + +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + if (mNoteData.isLocalModified() + && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { + return false; + } + + return true; + } + +<<<<<<< HEAD + // 内部类NoteData,用来处理与笔记关联的详细数据 + private class NoteData { + // 成员变量,包括文本数据ID、通话记录数据ID及其对应的值集合 + private long mTextDataId; + private ContentValues mTextDataValues; + private long mCallDataId; + private ContentValues mCallDataValues; + + // 日志标签 + private static final String TAG = "NoteData"; + + // 构造函数初始化成员变量 +======= + private class NoteData { + private long mTextDataId; + + private ContentValues mTextDataValues; + + private long mCallDataId; + + private ContentValues mCallDataValues; + + private static final String TAG = "NoteData"; + +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + public NoteData() { + mTextDataValues = new ContentValues(); + mCallDataValues = new ContentValues(); + mTextDataId = 0; + mCallDataId = 0; + } + +<<<<<<< HEAD + // 检查是否有本地修改过的数据 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + boolean isLocalModified() { + return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; + } + +<<<<<<< HEAD + // 设置文本数据ID的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + void setTextDataId(long id) { + if(id <= 0) { + throw new IllegalArgumentException("Text data id should larger than 0"); + } + mTextDataId = id; + } + +<<<<<<< HEAD + // 设置通话记录数据ID的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + void setCallDataId(long id) { + if (id <= 0) { + throw new IllegalArgumentException("Call data id should larger than 0"); + } + mCallDataId = id; + } + +<<<<<<< HEAD + // 设置通话记录数据的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + void setCallData(String key, String value) { + mCallDataValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + +<<<<<<< HEAD + // 设置文本数据的方法 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + void setTextData(String key, String value) { + mTextDataValues.put(key, value); + mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); + mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); + } + +<<<<<<< HEAD + /** + * 将数据推送到ContentResolver,进行数据库更新或插入。 + * 对于文本数据和通话记录数据,分别根据是否已有ID来决定是更新还是插入新记录。 + */ + Uri pushIntoContentResolver(Context context, long noteId) { + // 参数校验 +======= + Uri pushIntoContentResolver(Context context, long noteId) { + /** + * Check for safety + */ +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + if (noteId <= 0) { + throw new IllegalArgumentException("Wrong note id:" + noteId); + } + +<<<<<<< HEAD + ArrayList operationList = new ArrayList<>(); + ContentProviderOperation.Builder builder = null; + + // 处理文本数据 + if(mTextDataValues.size() > 0) { + mTextDataValues.put(DataColumns.NOTE_ID, noteId); + if (mTextDataId == 0) { + // 插入新的文本数据记录 +======= + ArrayList operationList = new ArrayList(); + ContentProviderOperation.Builder builder = null; + + if(mTextDataValues.size() > 0) { + mTextDataValues.put(DataColumns.NOTE_ID, noteId); + if (mTextDataId == 0) { +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + 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 { +<<<<<<< HEAD + // 更新现有的文本数据记录 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mTextDataId)); + builder.withValues(mTextDataValues); + operationList.add(builder.build()); + } + mTextDataValues.clear(); + } + +<<<<<<< HEAD + // 处理通话记录数据 + if(mCallDataValues.size() > 0) { + mCallDataValues.put(DataColumns.NOTE_ID, noteId); + if (mCallDataId == 0) { + // 插入新的通话记录数据记录 +======= + if(mCallDataValues.size() > 0) { + mCallDataValues.put(DataColumns.NOTE_ID, noteId); + if (mCallDataId == 0) { +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + 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 { +<<<<<<< HEAD + // 更新现有的通话记录数据记录 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( + Notes.CONTENT_DATA_URI, mCallDataId)); + builder.withValues(mCallDataValues); + operationList.add(builder.build()); + } + mCallDataValues.clear(); + } + +<<<<<<< HEAD + // 执行批量操作 +======= +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + 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); +<<<<<<< HEAD + } catch (RemoteException | OperationApplicationException e) { +======= + } catch (RemoteException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + return null; + } catch (OperationApplicationException e) { +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + return null; + } + } + return null; + } + } +<<<<<<< HEAD +} +======= +} +>>>>>>> 497ba4f7a5b11b52f3e9706151f1e2b13f56dc26