diff --git a/src/Model/Note.java b/src/Model/Note.java deleted file mode 100644 index f37396d..0000000 --- a/src/Model/Note.java +++ /dev/null @@ -1,390 +0,0 @@ -/* - * 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 对象,用于存储要插入数据库的笔记信息 - ContentValues values = new ContentValues(); - // 获取当前时间作为笔记的创建时间 - long createdTime = System.currentTimeMillis(); - // 将创建时间添加到 ContentValues 中 - 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); - - long noteId = 0; - try { - // 从 URI 中提取笔记的 ID - noteId = Long.valueOf(uri.getPathSegments().get(1)); - } catch (NumberFormatException e) { - // 若提取 ID 时出现异常,记录错误日志 - Log.e(TAG, "Get note id error :" + e.toString()); - noteId = 0; - } - if (noteId == -1) { - // 若笔记 ID 为 -1,抛出异常表示笔记 ID 错误 - throw new IllegalStateException("Wrong note 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) { - // 若笔记 ID 不合法,抛出异常 - throw new IllegalArgumentException("Wrong note id:" + noteId); - } - - if (!isLocalModified()) { - // 若笔记未被修改,直接返回 true - return true; - } - - /** - * 理论上,一旦数据发生变化,笔记的 LOCAL_MODIFIED 和 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"); - // 不返回,继续执行后续操作 - } - // 清空笔记的差异值 - mNoteDiffValues.clear(); - - if (mNoteData.isLocalModified() - && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { - // 若笔记数据被修改且同步失败,返回 false - return false; - } - - return true; - } - - /** - * 内部类,用于管理笔记的数据,包括文本数据和通话数据。 - */ - private class NoteData { - // 文本数据的 ID - private long mTextDataId; - // 存储文本数据的 ContentValues 对象 - private ContentValues mTextDataValues; - // 通话数据的 ID - private long mCallDataId; - // 存储通话数据的 ContentValues 对象 - private ContentValues mCallDataValues; - // 日志标签,用于在日志中标识该内部类的信息 - private static final String TAG = "NoteData"; - - /** - * 构造函数,初始化文本数据和通话数据的 ContentValues 对象,以及 ID 为 0。 - */ - 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 文本数据的 ID - */ - void setTextDataId(long id) { - if(id <= 0) { - // 若文本数据 ID 不合法,抛出异常 - throw new IllegalArgumentException("Text data id should larger than 0"); - } - mTextDataId = id; - } - - /** - * 设置笔记的通话数据的 ID。 - * - * @param id 通话数据的 ID - */ - void setCallDataId(long id) { - if (id <= 0) { - // 若通话数据 ID 不合法,抛出异常 - throw new IllegalArgumentException("Call data id should larger than 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) { - /** - * 进行安全检查,确保笔记 ID 合法。 - */ - if (noteId <= 0) { - // 若笔记 ID 不合法,抛出异常 - throw new IllegalArgumentException("Wrong note id:" + noteId); - } - - // 创建一个 ContentProviderOperation 列表,用于批量操作 - ArrayList operationList = new ArrayList(); - // ContentProviderOperation 构建器 - ContentProviderOperation.Builder builder = null; - - if(mTextDataValues.size() > 0) { - // 若文本数据有修改 - mTextDataValues.put(DataColumns.NOTE_ID, noteId); - if (mTextDataId == 0) { - // 若文本数据 ID 为 0,说明是新的文本数据 - mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); - // 插入新的文本数据 - Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, - mTextDataValues); - try { - // 获取插入后的文本数据 ID - setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); - } catch (NumberFormatException e) { - // 若获取 ID 失败,记录错误日志并清空文本数据 - Log.e(TAG, "Insert new text data fail with noteId" + noteId); - mTextDataValues.clear(); - return null; - } - } else { - // 若文本数据 ID 不为 0,说明是更新已有的文本数据 - builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( - Notes.CONTENT_DATA_URI, mTextDataId)); - builder.withValues(mTextDataValues); - operationList.add(builder.build()); - } - // 清空文本数据的 ContentValues 对象 - mTextDataValues.clear(); - } - - if(mCallDataValues.size() > 0) { - // 若通话数据有修改 - mCallDataValues.put(DataColumns.NOTE_ID, noteId); - if (mCallDataId == 0) { - // 若通话数据 ID 为 0,说明是新的通话数据 - mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); - // 插入新的通话数据 - Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, - mCallDataValues); - try { - // 获取插入后的通话数据 ID - setCallDataId(Long.valueOf(uri.getPathSegments().get(1))); - } catch (NumberFormatException e) { - // 若获取 ID 失败,记录错误日志并清空通话数据 - Log.e(TAG, "Insert new call data fail with noteId" + noteId); - mCallDataValues.clear(); - return null; - } - } else { - // 若通话数据 ID 不为 0,说明是更新已有的通话数据 - builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( - Notes.CONTENT_DATA_URI, mCallDataId)); - builder.withValues(mCallDataValues); - operationList.add(builder.build()); - } - // 清空通话数据的 ContentValues 对象 - mCallDataValues.clear(); - } - - if (operationList.size() > 0) { - try { - // 批量执行 ContentProviderOperation 列表中的操作 - 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; - } - } -} \ No newline at end of file diff --git a/src/Model/WorkingNote.java b/src/Model/WorkingNote.java deleted file mode 100644 index a48957a..0000000 --- a/src/Model/WorkingNote.java +++ /dev/null @@ -1,523 +0,0 @@ -/* - * 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.appwidget.AppWidgetManager; -import android.content.ContentUris; -import android.content.Context; -import android.database.Cursor; -import android.text.TextUtils; -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.DataConstants; -import net.micode.notes.data.Notes.NoteColumns; -import net.micode.notes.data.Notes.TextNote; -import net.micode.notes.tool.ResourceParser.NoteBgResources; - -/** - * WorkingNote 类用于管理便签的创建、加载、保存等操作。 - * 它封装了便签的各种属性和操作,包括便签的内容、模式、背景颜色、提醒日期等。 - */ -public class WorkingNote { - // 便签对象,用于管理便签的各种数据 - private Note mNote; - // 便签的 ID - private long mNoteId; - // 便签的内容 - private String mContent; - // 便签的模式 - private int mMode; - // 便签的提醒日期 - private long mAlertDate; - // 便签的修改日期 - private long mModifiedDate; - // 便签的背景颜色 ID - private int mBgColorId; - // 便签小部件的 ID - private int mWidgetId; - // 便签小部件的类型 - private int mWidgetType; - // 便签所在文件夹的 ID - private long mFolderId; - // 上下文对象,用于访问系统资源和执行操作 - private Context mContext; - // 日志标签,用于调试日志输出 - private static final String TAG = "WorkingNote"; - // 标记便签是否已被删除 - private boolean mIsDeleted; - // 便签设置更改监听器,用于监听便签设置的变化 - private NoteSettingChangedListener mNoteSettingStatusListener; - - // 数据查询的投影列,用于从数据库中查询数据 - public static final String[] DATA_PROJECTION = new String[] { - DataColumns.ID, - DataColumns.CONTENT, - DataColumns.MIME_TYPE, - DataColumns.DATA1, - DataColumns.DATA2, - DataColumns.DATA3, - DataColumns.DATA4, - }; - - // 便签查询的投影列,用于从数据库中查询便签信息 - public static final String[] NOTE_PROJECTION = new String[] { - NoteColumns.PARENT_ID, - NoteColumns.ALERTED_DATE, - NoteColumns.BG_COLOR_ID, - NoteColumns.WIDGET_ID, - NoteColumns.WIDGET_TYPE, - NoteColumns.MODIFIED_DATE - }; - - // 数据 ID 列的索引 - private static final int DATA_ID_COLUMN = 0; - // 数据内容列的索引 - private static final int DATA_CONTENT_COLUMN = 1; - // 数据 MIME 类型列的索引 - private static final int DATA_MIME_TYPE_COLUMN = 2; - // 数据模式列的索引 - private static final int DATA_MODE_COLUMN = 3; - // 便签父文件夹 ID 列的索引 - private static final int NOTE_PARENT_ID_COLUMN = 0; - // 便签提醒日期列的索引 - private static final int NOTE_ALERTED_DATE_COLUMN = 1; - // 便签背景颜色 ID 列的索引 - private static final int NOTE_BG_COLOR_ID_COLUMN = 2; - // 便签小部件 ID 列的索引 - private static final int NOTE_WIDGET_ID_COLUMN = 3; - // 便签小部件类型列的索引 - private static final int NOTE_WIDGET_TYPE_COLUMN = 4; - // 便签修改日期列的索引 - private static final int NOTE_MODIFIED_DATE_COLUMN = 5; - - /** - * 新建便签的构造函数。 - * - * @param context 上下文对象 - * @param folderId 便签所在文件夹的 ID - */ - private WorkingNote(Context context, long folderId) { - mContext = context; - // 初始化提醒日期为 0,表示没有提醒 - mAlertDate = 0; - // 初始化修改日期为当前时间 - mModifiedDate = System.currentTimeMillis(); - mFolderId = folderId; - mNote = new Note(); - // 初始化便签 ID 为 0,表示新便签还未分配 ID - mNoteId = 0; - // 标记便签未被删除 - mIsDeleted = false; - // 初始化便签模式为 0 - mMode = 0; - // 初始化小部件类型为无效类型 - mWidgetType = Notes.TYPE_WIDGET_INVALIDE; - } - - /** - * 加载现有便签的构造函数。 - * - * @param context 上下文对象 - * @param noteId 便签的 ID - * @param folderId 便签所在文件夹的 ID - */ - private WorkingNote(Context context, long noteId, long folderId) { - mContext = context; - mNoteId = noteId; - mFolderId = folderId; - // 标记便签未被删除 - mIsDeleted = false; - mNote = new Note(); - // 加载便签信息 - loadNote(); - } - - /** - * 加载便签的基本信息,包括父文件夹 ID、背景颜色 ID、小部件 ID 等。 - */ - private void loadNote() { - // 查询便签信息 - Cursor cursor = mContext.getContentResolver().query( - ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, - null, null); - - if (cursor != null) { - if (cursor.moveToFirst()) { - // 获取便签所在文件夹的 ID - mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); - // 获取便签的背景颜色 ID - mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); - // 获取便签小部件的 ID - mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); - // 获取便签小部件的类型 - mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); - // 获取便签的提醒日期 - mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); - // 获取便签的修改日期 - mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); - } - // 关闭游标 - cursor.close(); - } else { - // 记录错误日志 - Log.e(TAG, "No note with id:" + mNoteId); - // 抛出异常,表示找不到指定 ID 的便签 - throw new IllegalArgumentException("Unable to find note with id " + mNoteId); - } - // 加载便签的数据 - loadNoteData(); - } - - /** - * 加载便签的数据,包括便签内容和模式等。 - */ - private void loadNoteData() { - // 查询便签的数据 - Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, - DataColumns.NOTE_ID + "=?", new String[] { - String.valueOf(mNoteId) - }, null); - - if (cursor != null) { - if (cursor.moveToFirst()) { - do { - // 获取数据的 MIME 类型 - String type = cursor.getString(DATA_MIME_TYPE_COLUMN); - if (DataConstants.NOTE.equals(type)) { - // 如果是普通便签类型,获取便签内容和模式 - mContent = cursor.getString(DATA_CONTENT_COLUMN); - mMode = cursor.getInt(DATA_MODE_COLUMN); - // 设置便签的文本数据 ID - mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); - } else if (DataConstants.CALL_NOTE.equals(type)) { - // 如果是通话便签类型,设置通话数据 ID - mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); - } else { - // 记录日志,表示遇到错误的便签类型 - Log.d(TAG, "Wrong note type with type:" + type); - } - } while (cursor.moveToNext()); - } - // 关闭游标 - cursor.close(); - } else { - // 记录错误日志 - Log.e(TAG, "No data with id:" + mNoteId); - // 抛出异常,表示找不到指定 ID 的便签数据 - throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); - } - } - - /** - * 创建一个空的便签。 - * - * @param context 上下文对象 - * @param folderId 便签所在文件夹的 ID - * @param widgetId 便签小部件的 ID - * @param widgetType 便签小部件的类型 - * @param defaultBgColorId 便签的默认背景颜色 ID - * @return 新创建的便签对象 - */ - public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, - int widgetType, int defaultBgColorId) { - // 创建一个新的便签对象 - WorkingNote note = new WorkingNote(context, folderId); - // 设置便签的背景颜色 ID - note.setBgColorId(defaultBgColorId); - // 设置便签小部件的 ID - note.setWidgetId(widgetId); - // 设置便签小部件的类型 - note.setWidgetType(widgetType); - return note; - } - - /** - * 加载指定 ID 的便签。 - * - * @param context 上下文对象 - * @param id 便签的 ID - * @return 加载的便签对象 - */ - public static WorkingNote load(Context context, long id) { - return new WorkingNote(context, id, 0); - } - - /** - * 保存便签。 - * - * @return 如果保存成功返回 true,否则返回 false - */ - public synchronized boolean saveNote() { - if (isWorthSaving()) { - if (!existInDatabase()) { - // 如果便签不在数据库中,创建一个新的便签 ID - if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { - // 记录错误日志 - Log.e(TAG, "Create new note fail with id:" + mNoteId); - return false; - } - } - // 同步便签数据到数据库 - mNote.syncNote(mContext, mNoteId); - - /** - * 如果存在该便签的小部件,更新小部件的内容 - */ - if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID - && mWidgetType != Notes.TYPE_WIDGET_INVALIDE - && mNoteSettingStatusListener != null) { - // 通知监听器小部件内容发生变化 - mNoteSettingStatusListener.onWidgetChanged(); - } - return true; - } else { - return false; - } - } - - /** - * 判断便签是否存在于数据库中。 - * - * @return 如果存在返回 true,否则返回 false - */ - public boolean existInDatabase() { - return mNoteId > 0; - } - - /** - * 判断便签是否值得保存。 - * - * @return 如果值得保存返回 true,否则返回 false - */ - private boolean isWorthSaving() { - if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) - || (existInDatabase() && !mNote.isLocalModified())) { - return false; - } else { - return true; - } - } - - /** - * 设置便签设置更改监听器。 - * - * @param l 监听器对象 - */ - public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { - mNoteSettingStatusListener = l; - } - - /** - * 设置便签的提醒日期。 - * - * @param date 提醒日期 - * @param set 是否设置提醒 - */ - public void setAlertDate(long date, boolean set) { - if (date != mAlertDate) { - mAlertDate = date; - // 设置便签的提醒日期 - mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); - } - if (mNoteSettingStatusListener != null) { - // 通知监听器提醒日期发生变化 - mNoteSettingStatusListener.onClockAlertChanged(date, set); - } - } - - /** - * 标记便签是否已被删除。 - * - * @param mark 如果为 true 表示标记为已删除,否则为未删除 - */ - public void markDeleted(boolean mark) { - mIsDeleted = mark; - if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID - && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { - // 通知监听器小部件内容发生变化 - mNoteSettingStatusListener.onWidgetChanged(); - } - } - - /** - * 设置便签的背景颜色 ID。 - * - * @param id 背景颜色 ID - */ - public void setBgColorId(int id) { - if (id != mBgColorId) { - mBgColorId = id; - if (mNoteSettingStatusListener != null) { - // 通知监听器背景颜色发生变化 - mNoteSettingStatusListener.onBackgroundColorChanged(); - } - // 设置便签的背景颜色 ID - mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); - } - } - - /** - * 设置便签的检查列表模式。 - * - * @param mode 检查列表模式 - */ - public void setCheckListMode(int mode) { - if (mMode != mode) { - if (mNoteSettingStatusListener != null) { - // 通知监听器检查列表模式发生变化 - mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); - } - mMode = mode; - // 设置便签的文本数据模式 - mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); - } - } - - /** - * 设置便签小部件的类型。 - * - * @param type 小部件类型 - */ - public void setWidgetType(int type) { - if (type != mWidgetType) { - mWidgetType = type; - // 设置便签的小部件类型 - mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); - } - } - - /** - * 设置便签小部件的 ID。 - * - * @param id 小部件 ID - */ - public void setWidgetId(int id) { - if (id != mWidgetId) { - mWidgetId = id; - // 设置便签的小部件 ID - mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); - } - } - - /** - * 设置便签的文本内容。 - * - * @param text 文本内容 - */ - public void setWorkingText(String text) { - if (!TextUtils.equals(mContent, text)) { - mContent = text; - // 设置便签的文本数据内容 - mNote.setTextData(DataColumns.CONTENT, mContent); - } - } - - /** - * 将便签转换为通话便签。 - * - * @param phoneNumber 电话号码 - * @param callDate 通话日期 - */ - public void convertToCallNote(String phoneNumber, long callDate) { - // 设置通话便签的通话日期 - mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); - // 设置通话便签的电话号码 - mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); - // 设置便签的父文件夹 ID 为通话记录文件夹的 ID - mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); - } - - /** - * 判断便签是否设置了提醒。 - * - * @return 如果设置了提醒返回 true,否则返回 false - */ - public boolean hasClockAlert() { - return (mAlertDate > 0 ? true : false); - } - - public String getContent() { - return mContent; - } - - public long getAlertDate() { - return mAlertDate; - } - - public long getModifiedDate() { - return mModifiedDate; - } - - public int getBgColorResId() { - return NoteBgResources.getNoteBgResource(mBgColorId); - } - - public int getBgColorId() { - return mBgColorId; - } - - public int getTitleBgResId() { - return NoteBgResources.getNoteTitleBgResource(mBgColorId); - } - - public int getCheckListMode() { - return mMode; - } - - public long getNoteId() { - return mNoteId; - } - - public long getFolderId() { - return mFolderId; - } - - public int getWidgetId() { - return mWidgetId; - } - - public int getWidgetType() { - return mWidgetType; - } - - public interface NoteSettingChangedListener { - /** - * Called when the background color of current note has just changed - */ - void onBackgroundColorChanged(); - - /** - * Called when user set clock - */ - void onClockAlertChanged(long date, boolean set); - - /** - * Call when user create note from widget - */ - void onWidgetChanged(); - - /** - * Call when switch between check list mode and normal mode - * @param oldMode is previous mode before change - * @param newMode is new mode - */ - void onCheckListModeChanged(int oldMode, int newMode); - } -} diff --git a/src/Widget/NoteWidgetProvider.java b/src/Widget/NoteWidgetProvider.java deleted file mode 100644 index bac6ff6..0000000 --- a/src/Widget/NoteWidgetProvider.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * 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.widget; - -import android.app.PendingIntent; -import android.appwidget.AppWidgetManager; -import android.appwidget.AppWidgetProvider; -import android.content.ContentValues; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.util.Log; -import android.widget.RemoteViews; - -import net.micode.notes.R; -import net.micode.notes.data.Notes; -import net.micode.notes.data.Notes.NoteColumns; -import net.micode.notes.tool.ResourceParser; -import net.micode.notes.ui.NoteEditActivity; -import net.micode.notes.ui.NotesListActivity; - -/** - * 抽象类 NoteWidgetProvider,继承自 AppWidgetProvider,用于处理笔记小部件的相关操作。 - * 提供了小部件删除、更新等功能,并且定义了一些抽象方法供子类实现特定的资源获取。 - */ -public abstract class NoteWidgetProvider extends AppWidgetProvider { - // 查询笔记信息时使用的投影,指定要查询的列 - public static final String [] PROJECTION = new String [] { - NoteColumns.ID, - NoteColumns.BG_COLOR_ID, - NoteColumns.SNIPPET - }; - - // 笔记 ID 在投影数组中的索引 - public static final int COLUMN_ID = 0; - // 笔记背景颜色 ID 在投影数组中的索引 - public static final int COLUMN_BG_COLOR_ID = 1; - // 笔记摘要在投影数组中的索引 - public static final int COLUMN_SNIPPET = 2; - - // 日志标签,用于在日志中标识该类的相关信息 - private static final String TAG = "NoteWidgetProvider"; - - /** - * 当小部件被删除时调用此方法。 - * 该方法会将对应笔记的小部件 ID 设置为无效值。 - * - * @param context 上下文对象 - * @param appWidgetIds 被删除的小部件 ID 数组 - */ - @Override - public void onDeleted(Context context, int[] appWidgetIds) { - // 创建 ContentValues 对象,用于存储要更新的字段和值 - ContentValues values = new ContentValues(); - // 将笔记的小部件 ID 设置为无效值 - values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); - // 遍历被删除的小部件 ID 数组 - for (int i = 0; i < appWidgetIds.length; i++) { - // 更新笔记数据库中对应小部件 ID 的笔记记录 - context.getContentResolver().update(Notes.CONTENT_NOTE_URI, - values, - NoteColumns.WIDGET_ID + "=?", - new String[] { String.valueOf(appWidgetIds[i])}); - } - } - - /** - * 根据小部件 ID 获取笔记小部件的信息。 - * - * @param context 上下文对象 - * @param widgetId 小部件 ID - * @return 包含笔记信息的游标,如果查询失败则返回 null - */ - private Cursor getNoteWidgetInfo(Context context, int widgetId) { - // 查询笔记数据库,获取指定小部件 ID 且不在回收站的笔记信息 - return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, - PROJECTION, - NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", - new String[] { String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER) }, - null); - } - - /** - * 更新小部件的方法,默认不开启隐私模式。 - * - * @param context 上下文对象 - * @param appWidgetManager 小部件管理器 - * @param appWidgetIds 要更新的小部件 ID 数组 - */ - protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - // 调用带隐私模式参数的更新方法,隐私模式为 false - update(context, appWidgetManager, appWidgetIds, false); - } - - /** - * 更新小部件的具体实现方法。 - * 根据小部件 ID 获取笔记信息,设置小部件的背景、文本和点击事件等。 - * - * @param context 上下文对象 - * @param appWidgetManager 小部件管理器 - * @param appWidgetIds 要更新的小部件 ID 数组 - * @param privacyMode 是否开启隐私模式 - */ - private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, - boolean privacyMode) { - // 遍历要更新的小部件 ID 数组 - for (int i = 0; i < appWidgetIds.length; i++) { - // 检查小部件 ID 是否有效 - if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { - // 获取默认的背景颜色 ID - int bgId = ResourceParser.getDefaultBgId(context); - // 初始化笔记摘要为空字符串 - String snippet = ""; - // 创建一个启动笔记编辑活动的意图 - Intent intent = new Intent(context, NoteEditActivity.class); - // 设置意图标志,确保活动以单顶模式启动 - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - // 将小部件 ID 作为额外数据添加到意图中 - intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); - // 将小部件类型作为额外数据添加到意图中 - intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); - - // 获取指定小部件 ID 的笔记信息游标 - Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); - if (c != null && c.moveToFirst()) { - // 检查是否存在多条具有相同小部件 ID 的笔记记录 - if (c.getCount() > 1) { - // 记录错误日志 - Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); - // 关闭游标 - c.close(); - return; - } - // 从游标中获取笔记摘要 - snippet = c.getString(COLUMN_SNIPPET); - // 从游标中获取笔记背景颜色 ID - bgId = c.getInt(COLUMN_BG_COLOR_ID); - // 将笔记 ID 作为额外数据添加到意图中 - intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); - // 设置意图的动作类型为查看 - intent.setAction(Intent.ACTION_VIEW); - } else { - // 如果没有找到笔记信息,设置默认的提示信息 - snippet = context.getResources().getString(R.string.widget_havenot_content); - // 设置意图的动作类型为插入或编辑 - intent.setAction(Intent.ACTION_INSERT_OR_EDIT); - } - - // 关闭游标 - if (c != null) { - c.close(); - } - - // 创建 RemoteViews 对象,用于设置小部件的布局 - RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); - // 设置小部件的背景图片资源 - rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); - // 将背景颜色 ID 作为额外数据添加到意图中 - intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); - - /** - * 生成用于启动小部件宿主的待定意图 - */ - PendingIntent pendingIntent = null; - if (privacyMode) { - // 如果开启隐私模式,设置小部件文本为隐私模式提示信息 - rv.setTextViewText(R.id.widget_text, - context.getString(R.string.widget_under_visit_mode)); - // 创建启动笔记列表活动的待定意图 - pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( - context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); - } else { - // 如果未开启隐私模式,设置小部件文本为笔记摘要 - rv.setTextViewText(R.id.widget_text, snippet); - // 创建启动笔记编辑活动的待定意图 - pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, - PendingIntent.FLAG_UPDATE_CURRENT); - } - - // 设置小部件文本的点击事件为待定意图 - rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); - // 更新指定小部件 ID 的小部件布局 - appWidgetManager.updateAppWidget(appWidgetIds[i], rv); - } - } - } - - /** - * 抽象方法,用于获取指定背景颜色 ID 对应的背景资源 ID。 - * 子类需要实现该方法来提供具体的背景资源。 - * - * @param bgId 背景颜色 ID - * @return 背景资源 ID - */ - protected abstract int getBgResourceId(int bgId); - - /** - * 抽象方法,用于获取小部件的布局 ID。 - * 子类需要实现该方法来提供具体的布局资源。 - * - * @return 布局 ID - */ - protected abstract int getLayoutId(); - - /** - * 抽象方法,用于获取小部件的类型。 - * 子类需要实现该方法来提供具体的小部件类型。 - * - * @return 小部件类型 - */ - protected abstract int getWidgetType(); -} \ No newline at end of file diff --git a/src/Widget/NoteWidgetProvider_2x.java b/src/Widget/NoteWidgetProvider_2x.java deleted file mode 100644 index 904490b..0000000 --- a/src/Widget/NoteWidgetProvider_2x.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.widget; - -import android.appwidget.AppWidgetManager; -import android.content.Context; - -import net.micode.notes.R; -import net.micode.notes.data.Notes; -import net.micode.notes.tool.ResourceParser; - -/** - * NoteWidgetProvider_2x 类继承自 NoteWidgetProvider,用于处理 2x 尺寸的笔记小部件。 - * 它重写了父类的一些抽象方法,以提供适合 2x 小部件的布局、背景资源和小部件类型。 - */ -public class NoteWidgetProvider_2x extends NoteWidgetProvider { - - /** - * 当小部件需要更新时调用此方法。 - * 调用父类的 update 方法来完成小部件的更新操作。 - * - * @param context 上下文对象,用于获取资源和执行相关操作 - * @param appWidgetManager 小部件管理器,用于管理和更新小部件 - * @param appWidgetIds 要更新的小部件的 ID 数组 - */ - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - // 调用父类的 update 方法进行小部件更新 - super.update(context, appWidgetManager, appWidgetIds); - } - - /** - * 获取 2x 小部件的布局资源 ID。 - * - * @return 2x 小部件的布局资源 ID,即 R.layout.widget_2x - */ - @Override - protected int getLayoutId() { - return R.layout.widget_2x; - } - - /** - * 根据背景颜色 ID 获取 2x 小部件的背景资源 ID。 - * 调用 ResourceParser 类中的 WidgetBgResources 内部类的方法来获取相应的背景资源。 - * - * @param bgId 背景颜色 ID,用于指定要获取的背景颜色 - * @return 2x 小部件的背景资源 ID - */ - @Override - protected int getBgResourceId(int bgId) { - return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); - } - - /** - * 获取 2x 小部件的类型。 - * - * @return 2x 小部件的类型,即 Notes.TYPE_WIDGET_2X - */ - @Override - protected int getWidgetType() { - return Notes.TYPE_WIDGET_2X; - } -} \ No newline at end of file diff --git a/src/Widget/NoteWidgetProvider_4x.java b/src/Widget/NoteWidgetProvider_4x.java deleted file mode 100644 index ae17300..0000000 --- a/src/Widget/NoteWidgetProvider_4x.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.widget; - -import android.appwidget.AppWidgetManager; -import android.content.Context; - -import net.micode.notes.R; -import net.micode.notes.data.Notes; -import net.micode.notes.tool.ResourceParser; - -/** - * NoteWidgetProvider_4x 类继承自 NoteWidgetProvider,用于处理 4x 尺寸的笔记小部件。 - * 它重写了父类的一些方法,以提供适合 4x 小部件的布局、背景资源和小部件类型等信息。 - */ -public class NoteWidgetProvider_4x extends NoteWidgetProvider { - - /** - * 当小部件需要更新时调用此方法。 - * 调用父类的 update 方法来完成小部件的更新操作。 - * - * @param context 上下文对象,用于获取系统服务和资源等 - * @param appWidgetManager 小部件管理器,用于管理和更新小部件 - * @param appWidgetIds 要更新的小部件的 ID 数组 - */ - @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { - // 调用父类的 update 方法进行小部件更新 - super.update(context, appWidgetManager, appWidgetIds); - } - - /** - * 获取 4x 小部件的布局资源 ID。 - * - * @return 4x 小部件的布局资源 ID,即 R.layout.widget_4x - */ - protected int getLayoutId() { - return R.layout.widget_4x; - } - - /** - * 根据背景颜色 ID 获取 4x 小部件的背景资源 ID。 - * 借助 ResourceParser 类中的 WidgetBgResources 内部类来获取对应背景资源。 - * - * @param bgId 背景颜色 ID,代表不同的背景颜色 - * @return 4x 小部件对应背景颜色的背景资源 ID - */ - @Override - protected int getBgResourceId(int bgId) { - return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); - } - - /** - * 获取 4x 小部件的类型。 - * - * @return 4x 小部件的类型,即 Notes.TYPE_WIDGET_4X - */ - @Override - protected int getWidgetType() { - return Notes.TYPE_WIDGET_4X; - } -} \ No newline at end of file diff --git a/src/项目原代码.txt b/src/项目原代码.txt deleted file mode 100644 index 5f25df6..0000000 --- a/src/项目原代码.txt +++ /dev/null @@ -1 +0,0 @@ -assdjgyuguyhgnb6 \ No newline at end of file