From bd1185bb422864a8d6adfeb8a0297ad9d4012010 Mon Sep 17 00:00:00 2001 From: baiguli <2169927768@qq.com> Date: Tue, 20 May 2025 10:09:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BackupUtils.java | 427 ++++++++++++++++++++++++++++++++++++++ src/DataUtils.java | 392 ++++++++++++++++++++++++++++++++++ src/GTaskStringUtils.java | 87 ++++++++ src/ResourceParser.java | 273 ++++++++++++++++++++++++ 4 files changed, 1179 insertions(+) create mode 100644 src/BackupUtils.java create mode 100644 src/DataUtils.java create mode 100644 src/GTaskStringUtils.java create mode 100644 src/ResourceParser.java diff --git a/src/BackupUtils.java b/src/BackupUtils.java new file mode 100644 index 0000000..4e9c724 --- /dev/null +++ b/src/BackupUtils.java @@ -0,0 +1,427 @@ +/* + * 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.tool; + +import android.content.Context; +import android.database.Cursor; +import android.os.Environment; +import android.text.TextUtils; +import android.text.format.DateFormat; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +/** + * 笔记备份工具类,提供将笔记数据导出为文本文件的功能 + * 支持导出文件夹、普通笔记和通话记录笔记到SD卡指定路径 + */ +public class BackupUtils { + private static final String TAG = "BackupUtils"; + // 单例实例 + private static BackupUtils sInstance; + + /** + * 获取单例实例 + * @param context 应用上下文 + * @return BackupUtils实例 + */ + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 备份/恢复状态码定义 + * STATE_SD_CARD_UNMOUONTED: SD卡未挂载 + * STATE_BACKUP_FILE_NOT_EXIST: 备份文件不存在 + * STATE_DATA_DESTROIED: 数据损坏 + * STATE_SYSTEM_ERROR: 系统错误 + * STATE_SUCCESS: 操作成功 + */ + public static final int STATE_SD_CARD_UNMOUONTED = 0; + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + public static final int STATE_DATA_DESTROIED = 2; + public static final int STATE_SYSTEM_ERROR = 3; + public static final int STATE_SUCCESS = 4; + + private final TextExport mTextExport; // 文本导出模块 + + /** + * 构造方法,初始化文本导出模块 + * @param context 应用上下文 + */ + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + /** + * 检查外部存储是否可用(已挂载) + * @return true if SD卡可用 + */ + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + /** + * 执行笔记导出操作 + * @return 状态码(参考STATE_*常量) + */ + public int exportToText() { + return mTextExport.exportToText(); + } + + /** + * 获取导出的文本文件名 + * @return 文件名(如"notes_20231001.txt") + */ + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + /** + * 获取导出文件的存储目录 + * @return 目录路径(如"/sdcard/notes_backup/") + */ + public String getExportedTextFileDir() { + return mTextExport.mFileDirectory; + } + + /** + * 文本导出核心实现类 + */ + private static class TextExport { + // 笔记查询投影(仅获取必要字段) + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, // 笔记ID + NoteColumns.MODIFIED_DATE, // 最后修改时间 + NoteColumns.SNIPPET, // 摘要(文件夹名称/笔记标题) + NoteColumns.TYPE // 类型(文件夹/笔记) + }; + // 投影字段索引 + private static final int NOTE_COLUMN_ID = 0; + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + private static final int NOTE_COLUMN_SNIPPET = 2; + + // 笔记内容数据查询投影 + private static final String[] DATA_PROJECTION = { + DataColumns.CONTENT, // 内容(文本/位置信息) + DataColumns.MIME_TYPE, // MIME类型(区分普通笔记/通话记录) + DataColumns.DATA1, // 预留字段1(通话记录中为日期?) + DataColumns.DATA2, // 预留字段2 + DataColumns.DATA3, // 预留字段3 + DataColumns.DATA4, // 电话号码(通话记录专用) + }; + // 数据投影字段索引 + private static final int DATA_COLUMN_CONTENT = 0; + private static final int DATA_COLUMN_MIME_TYPE = 1; + private static final int DATA_COLUMN_CALL_DATE = 2; // DATA1的别名(通话时间) + private static final int DATA_COLUMN_PHONE_NUMBER = 4; // DATA4的别名(电话号码) + + // 导出格式模板(对应res/array/format_for_exported_note.xml) + private final String[] TEXT_FORMAT; + // 格式模板索引 + private static final int FORMAT_FOLDER_NAME = 0; // 文件夹名称格式 + private static final int FORMAT_NOTE_DATE = 1; // 笔记日期格式 + private static final int FORMAT_NOTE_CONTENT = 2; // 笔记内容格式 + + private final Context mContext; + private String mFileName; // 导出文件名 + private String mFileDirectory; // 导出文件目录 + + /** + * 构造方法,加载格式模板 + * @param context 应用上下文 + */ + public TextExport(Context context) { + // 从资源文件获取导出格式模板 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + } + + /** + * 获取指定索引的格式模板 + * @param id 格式索引(FORMAT_*常量) + * @return 格式字符串 + */ + private String getFormat(int id) { + return TEXT_FORMAT[id]; + } + + /** + * 将指定文件夹及其子笔记导出到文本流 + * @param folderId 文件夹ID + * @param ps 输出流 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询该文件夹下的所有笔记 + Cursor notesCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.PARENT_ID + "=?", // 条件:父级ID为当前文件夹ID + new String[]{folderId}, + null + ); + + if (notesCursor != null) { + try { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记最后修改时间 + String dateStr = DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), // 格式:MM月dd日 HH:mm + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE) + ).toString(); + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), dateStr)); + + // 导出笔记内容 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + } finally { + notesCursor.close(); // 释放游标资源 + } + } + } + + /** + * 将指定笔记内容导出到文本流 + * @param noteId 笔记ID + * @param ps 输出流 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + // 查询笔记对应的内容数据(如文本内容、通话记录) + Cursor dataCursor = mContext.getContentResolver().query( + Notes.CONTENT_DATA_URI, + DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", // 条件:属于该笔记ID + new String[]{noteId}, + null + ); + + if (dataCursor != null) { + try { + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { + // 处理通话记录笔记 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + String location = dataCursor.getString(DATA_COLUMN_CONTENT); // 通话位置或备注 + + // 打印电话号码 + if (!TextUtils.isEmpty(phoneNumber)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); + } + // 打印通话时间 + String callDateStr = DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + callDate + ).toString(); + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), callDateStr)); + // 打印位置信息 + if (!TextUtils.isEmpty(location)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { + // 处理普通文本笔记 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), content)); + } + } + } while (dataCursor.moveToNext()); + } + } finally { + dataCursor.close(); // 释放游标资源 + } + } + // 打印分隔符(换行符+特殊字符,可能用于区分笔记) + try { + ps.write(new byte[]{Character.LINE_SEPARATOR, Character.LETTER_NUMBER}); + } catch (IOException e) { + Log.e(TAG, "写入分隔符失败:" + e.getMessage()); + } + } + + /** + * 执行完整的导出流程 + * @return 导出状态码 + */ + public int exportToText() { + // 检查SD卡是否可用 + if (!externalStorageAvailable()) { + Log.d(TAG, "SD卡未挂载,无法导出"); + return STATE_SD_CARD_UNMOUONTED; + } + + // 获取输出流 + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + Log.e(TAG, "创建输出流失败"); + return STATE_SYSTEM_ERROR; + } + + // 导出文件夹及其笔记 + String whereClause = "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER; // 排除回收站,包含通话记录文件夹 + + Cursor folderCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + whereClause, + null, + null + ); + + if (folderCursor != null) { + try { + if (folderCursor.moveToFirst()) { + do { + String folderName; + // 处理特殊文件夹:通话记录文件夹使用固定名称 + if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { + folderName = mContext.getString(R.string.call_record_folder_name); + } else { + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); // 使用文件夹摘要作为名称 + } + // 打印文件夹名称 + if (!TextUtils.isEmpty(folderName)) { + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + } + String folderId = folderCursor.getString(NOTE_COLUMN_ID); + exportFolderToText(folderId, ps); // 导出文件夹下的笔记 + } while (folderCursor.moveToNext()); + } + } finally { + folderCursor.close(); + } + } + + // 导出根目录下的普通笔记(父级ID=0) + Cursor noteCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", + null, + null + ); + + if (noteCursor != null) { + try { + if (noteCursor.moveToFirst()) { + do { + String dateStr = DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE) + ).toString(); + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), dateStr)); + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); // 导出笔记内容 + } while (noteCursor.moveToNext()); + } + } finally { + noteCursor.close(); + } + } + + ps.close(); // 关闭输出流 + return STATE_SUCCESS; + } + + /** + * 获取导出文件的打印流 + * @return PrintStream实例,失败时返回null + */ + private PrintStream getExportToTextPrintStream() { + // 生成文件路径(如/sdcard/notes_backup/notes_20231001.txt) + File file = generateFileMountedOnSDcard( + mContext, + R.string.file_path, // 目录路径(如"/notes_backup/") + R.string.file_name_txt_format // 文件名格式(如"notes_%s.txt") + ); + if (file == null) { + return null; + } + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); // 存储目录路径 + + // 创建文件输出流 + try { + FileOutputStream fos = new FileOutputStream(file); + return new PrintStream(fos); + } catch (FileNotFoundException e) { + Log.e(TAG, "文件未找到:" + e.getMessage()); + } catch (NullPointerException e) { + Log.e(TAG, "空指针异常:" + e.getMessage()); + } + return null; + } + } + + /** + * 在SD卡生成备份文件 + * @param context 应用上下文 + * @param filePathResId 目录路径资源ID(如R.string.file_path) + * @param fileNameFormatResId 文件名格式资源ID(如R.string.file_name_txt_format) + * @return 生成的文件对象,失败时返回null + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + // 构建文件路径:SD卡根目录 + 自定义目录 + 文件名 + StringBuilder sb = new StringBuilder(); + sb.append(Environment.getExternalStorageDirectory()); // SD卡根目录 + sb.append(context.getString(filePathResId)); // 自定义目录(如"/notes_backup/") + File filedir = new File(sb.toString()); // 目录对象 + + // 构建文件名:格式字符串 + 当前日期(如"notes_20231001.txt") + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), System.currentTimeMillis()) // 日期格式:yyyyMMdd + )); + File file = new File(sb.toString()); // 文件对象 + + // 创建目录和文件(若不存在) + try { + if (!filedir.exists() && !filedir.mkdirs()) { + Log.e(TAG, "创建目录失败:" + filedir.getPath()); + return null; + } + if (!file.exists() && !file.createNewFile()) { + Log.e(TAG, "创建文件失败:" + file.getPath()); + return null; + } + return file; + } catch (SecurityException | IOException e) { + Log.e(TAG, "文件操作失败:" + e.getMessage()); + return null; + } + } +} \ No newline at end of file diff --git a/src/DataUtils.java b/src/DataUtils.java new file mode 100644 index 0000000..f29008e --- /dev/null +++ b/src/DataUtils.java @@ -0,0 +1,392 @@ +/* + * 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.tool; + +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.OperationApplicationException; +import android.database.Cursor; +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.NoteColumns; +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; + +import java.util.ArrayList; +import java.util.HashSet; + +/** + * 数据操作工具类,提供笔记数据的批量删除、移动、查询等功能 + * 涉及内容提供者(ContentProvider)的操作,用于管理笔记数据库 + */ +public class DataUtils { + public static final String TAG = "DataUtils"; + + /** + * 批量删除笔记 + * @param resolver 内容解析器 + * @param ids 待删除的笔记ID集合(跳过系统根文件夹) + * @return 删除是否成功 + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + if (ids == null) { + Log.d(TAG, "待删除的ID集合为空"); + return true; + } + if (ids.size() == 0) { + Log.d(TAG, "ID集合中无有效数据"); + return true; + } + + ArrayList operationList = new ArrayList<>(); + for (long id : ids) { + // 跳过系统根文件夹(禁止删除) + if (id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "尝试删除系统根文件夹,已忽略"); + continue; + } + // 创建删除操作:根据ID构建删除URI + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + operationList.add(builder.build()); + } + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果 + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "删除失败,ID集合:" + ids.toString()); + return false; + } + return true; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, "批量删除异常:" + e.getMessage()); + e.printStackTrace(); + } + return false; + } + + /** + * 将单个笔记移动到目标文件夹 + * @param resolver 内容解析器 + * @param id 待移动的笔记ID + * @param srcFolderId 源文件夹ID(记录原始位置) + * @param desFolderId 目标文件夹ID + */ + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + ContentValues values = new ContentValues(); + // 更新父级ID为目标文件夹ID + values.put(NoteColumns.PARENT_ID, desFolderId); + // 记录原始父级ID(用于同步或回滚) + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); + // 标记本地修改(可能用于同步逻辑) + values.put(NoteColumns.LOCAL_MODIFIED, 1); + // 执行更新操作 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + /** + * 批量移动笔记到目标文件夹 + * @param resolver 内容解析器 + * @param ids 待移动的笔记ID集合 + * @param folderId 目标文件夹ID + * @return 移动是否成功 + */ + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { + if (ids == null) { + Log.d(TAG, "待移动的ID集合为空"); + return true; + } + + ArrayList operationList = new ArrayList<>(); + for (long id : ids) { + // 创建更新操作:设置父级ID并标记本地修改 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + builder.withValue(NoteColumns.PARENT_ID, folderId); + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + operationList.add(builder.build()); + } + + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果 + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "移动失败,ID集合:" + ids.toString()); + return false; + } + return true; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, "批量移动异常:" + e.getMessage()); + e.printStackTrace(); + } + return false; + } + + /** + * 获取用户创建的文件夹数量(排除系统文件夹和回收站) + * @param resolver 内容解析器 + * @return 文件夹数量 + */ + public static int getUserFolderCount(ContentResolver resolver) { + // 查询条件:类型为文件夹,且父级ID不是回收站ID + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String[]{"COUNT(*)"}, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", + new String[]{String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, + null); + + int count = 0; + if (cursor != null) { + try { + if (cursor.moveToFirst()) { + count = cursor.getInt(0); // 获取计数结果 + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "查询文件夹数量失败:" + e.getMessage()); + } finally { + cursor.close(); // 关闭游标释放资源 + } + } + return count; + } + + /** + * 检查笔记是否在数据库中可见(非回收站) + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @param type 笔记类型(如Notes.TYPE_NOTE或Notes.TYPE_FOLDER) + * @return 是否可见(存在且不在回收站) + */ + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 查询条件:类型匹配且父级ID不是回收站ID + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, + new String[]{String.valueOf(type)}, + null); + + boolean exist = false; + if (cursor != null) { + exist = cursor.getCount() > 0; // 存在有效记录则可见 + cursor.close(); + } + return exist; + } + + /** + * 检查笔记是否存在于数据库中(不判断是否在回收站) + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 是否存在 + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 直接查询笔记ID是否存在 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); + + boolean exist = false; + if (cursor != null) { + exist = cursor.getCount() > 0; + cursor.close(); + } + return exist; + } + + /** + * 检查数据项是否存在于数据库中(如笔记内容数据) + * @param resolver 内容解析器 + * @param dataId 数据项ID + * @return 是否存在 + */ + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询数据项URI对应的记录 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), + null, null, null, null); + + boolean exist = false; + if (cursor != null) { + exist = cursor.getCount() > 0; + cursor.close(); + } + return exist; + } + + /** + * 检查文件夹名称是否已存在(非回收站的可见文件夹) + * @param resolver 内容解析器 + * @param name 文件夹名称 + * @return 是否存在同名文件夹 + */ + public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { + // 查询条件:类型为文件夹,不在回收站,且名称匹配 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.SNIPPET + "=?", + new String[]{name}, null); + + boolean exist = false; + if (cursor != null) { + exist = cursor.getCount() > 0; + cursor.close(); + } + return exist; + } + + /** + * 获取文件夹关联的桌面小部件属性集合 + * @param resolver 内容解析器 + * @param folderId 文件夹ID + * @return 小部件属性集合(AppWidgetAttribute) + */ + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 查询该文件夹下所有笔记的小部件ID和类型 + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, + new String[]{NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE}, + NoteColumns.PARENT_ID + "=?", + new String[]{String.valueOf(folderId)}, + null); + + HashSet set = null; + if (c != null) { + set = new HashSet<>(); + try { + if (c.moveToFirst()) { + do { + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); // 小部件ID + widget.widgetType = c.getInt(1); // 小部件类型 + set.add(widget); + } while (c.moveToNext()); + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "解析小部件数据失败:" + e.getMessage()); + } finally { + c.close(); + } + } + return set; + } + + /** + * 根据笔记ID获取通话记录中的电话号码 + * @param resolver 内容解析器 + * @param noteId 通话笔记ID + * @return 电话号码(空字符串表示未找到) + */ + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询通话笔记对应的电话号码(通过MIME类型过滤) + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String[]{CallNote.PHONE_NUMBER}, + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", + new String[]{String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE}, + null); + + if (cursor != null && cursor.moveToFirst()) { + try { + return cursor.getString(0); // 获取电话号码 + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取电话号码失败:" + e.getMessage()); + } finally { + cursor.close(); + } + } + return ""; + } + + /** + * 根据电话号码和通话时间获取对应的笔记ID + * @param resolver 内容解析器 + * @param phoneNumber 电话号码 + * @param callDate 通话时间戳 + * @return 笔记ID(0表示未找到) + */ + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 使用PHONE_NUMBERS_EQUAL函数进行模糊匹配(处理格式差异) + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String[]{CallNote.NOTE_ID}, + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", + new String[]{String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber}, + null); + + long noteId = 0; + if (cursor != null) { + try { + if (cursor.moveToFirst()) { + noteId = cursor.getLong(0); // 获取笔记ID + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取通话笔记ID失败:" + e.getMessage()); + } finally { + cursor.close(); + } + } + return noteId; + } + + /** + * 根据笔记ID获取摘要内容 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 摘要内容(若不存在则抛出异常) + */ + public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询笔记的摘要字段 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String[]{NoteColumns.SNIPPET}, + NoteColumns.ID + "=?", + new String[]{String.valueOf(noteId)}, + null); + + if (cursor != null) { + String snippet = ""; + try { + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); // 获取摘要 + } + } finally { + cursor.close(); + } + return snippet; + } + throw new IllegalArgumentException("未找到ID为 " + noteId + " 的笔记"); + } + + /** + * 格式化摘要内容:截断换行符前的内容并去首尾空格 + * @param snippet 原始摘要 + * @return 格式化后的摘要 + */ + public static String getFormattedSnippet(String snippet) { + if (snippet != null) { + snippet = snippet.trim(); // 去除首尾空格 + int index = snippet.indexOf('\n'); + if (index != -1) { + snippet = snippet.substring(0, index); // 截断到第一个换行符 + } + } + return snippet; + } +} \ No newline at end of file diff --git a/src/GTaskStringUtils.java b/src/GTaskStringUtils.java new file mode 100644 index 0000000..5749938 --- /dev/null +++ b/src/GTaskStringUtils.java @@ -0,0 +1,87 @@ +/* + * 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.tool; + +/** + * Google Tasks同步相关的字符串常量工具类 + * 定义了与Google Tasks API交互时使用的JSON字段名、文件夹名称和元数据标识 + */ +public class GTaskStringUtils { + + // --------------- JSON请求/响应字段名常量 --------------- + + // 操作相关字段 + public final static String GTASK_JSON_ACTION_ID = "action_id"; // 操作ID + public final static String GTASK_JSON_ACTION_LIST = "action_list"; // 操作列表 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; // 操作类型 + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; // 创建操作 + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; // 获取所有操作 + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; // 移动操作 + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; // 更新操作 + + // 实体相关字段 + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; // 创建者ID + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; // 子实体 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; // 实体变更 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; // 实体类型 + + // 任务/列表属性字段 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version";// 客户端版本 + public final static String GTASK_JSON_COMPLETED = "completed"; // 完成状态 + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";// 当前列表ID + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";// 默认列表ID + public final static String GTASK_JSON_DELETED = "deleted"; // 删除状态 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; // 目标列表 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; // 目标父级 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";// 目标父级类型 + public final static String GTASK_JSON_ID = "id"; // ID + public final static String GTASK_JSON_INDEX = "index"; // 索引位置 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; // 最后修改时间 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";// 最新同步点 + public final static String GTASK_JSON_LIST_ID = "list_id"; // 列表ID + public final static String GTASK_JSON_LISTS = "lists"; // 列表集合 + public final static String GTASK_JSON_NAME = "name"; // 名称 + public final static String GTASK_JSON_NEW_ID = "new_id"; // 新ID + public final static String GTASK_JSON_NOTES = "notes"; // 备注 + public final static String GTASK_JSON_PARENT_ID = "parent_id"; // 父级ID + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";// 前一个兄弟节点ID + public final static String GTASK_JSON_RESULTS = "results"; // 结果集 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; // 源列表 + public final static String GTASK_JSON_TASKS = "tasks"; // 任务集合 + public final static String GTASK_JSON_TYPE = "type"; // 类型 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; // 组类型 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; // 任务类型 + public final static String GTASK_JSON_USER = "user"; // 用户 + + // --------------- 特殊文件夹和元数据相关常量 --------------- + + // MIUI笔记文件夹前缀,用于标识由MIUI笔记创建的Google Tasks文件夹 + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + + // 默认文件夹名称 + public final static String FOLDER_DEFAULT = "Default"; // 默认任务列表 + public final static String FOLDER_CALL_NOTE = "Call_Note"; // 通话记录笔记 + public final static String FOLDER_META = "METADATA"; // 元数据文件夹 + + // 元数据头部标识 + public final static String META_HEAD_GTASK_ID = "meta_gid"; // Google Tasks ID元数据 + public final static String META_HEAD_NOTE = "meta_note"; // 笔记元数据 + public final static String META_HEAD_DATA = "meta_data"; // 数据元数据 + + // 元数据笔记名称,提示用户不要更新或删除 + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; +} \ No newline at end of file diff --git a/src/ResourceParser.java b/src/ResourceParser.java new file mode 100644 index 0000000..6e972b7 --- /dev/null +++ b/src/ResourceParser.java @@ -0,0 +1,273 @@ +/* + * 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.tool; + +import android.content.Context; +import android.preference.PreferenceManager; + +import net.micode.notes.R; +import net.micode.notes.ui.NotesPreferenceActivity; + +/** + * 资源解析工具类,用于管理和获取笔记应用中的各种资源 + * 包括背景色、字体大小、各种视图的背景资源等 + */ +public class ResourceParser { + + // 笔记背景颜色常量定义 + public static final int YELLOW = 0; // 黄色背景 + public static final int BLUE = 1; // 蓝色背景 + public static final int WHITE = 2; // 白色背景 + public static final int GREEN = 3; // 绿色背景 + public static final int RED = 4; // 红色背景 + + public static final int BG_DEFAULT_COLOR = YELLOW; // 默认背景颜色为黄色 + + // 字体大小常量定义 + public static final int TEXT_SMALL = 0; // 小号字体 + public static final int TEXT_MEDIUM = 1; // 中号字体(默认) + public static final int TEXT_LARGE = 2; // 大号字体 + public static final int TEXT_SUPER = 3; // 超大号字体 + + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; // 默认字体大小为中号 + + /** + * 笔记编辑界面背景资源管理类 + * 提供获取不同颜色笔记编辑界面背景和标题栏背景资源的方法 + */ + public static class NoteBgResources { + // 笔记编辑界面背景资源数组,按颜色索引 + private final static int [] BG_EDIT_RESOURCES = new int [] { + R.drawable.edit_yellow, // 黄色背景 + R.drawable.edit_blue, // 蓝色背景 + R.drawable.edit_white, // 白色背景 + R.drawable.edit_green, // 绿色背景 + R.drawable.edit_red // 红色背景 + }; + + // 笔记编辑界面标题栏背景资源数组,按颜色索引 + private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { + R.drawable.edit_title_yellow, // 黄色标题栏 + R.drawable.edit_title_blue, // 蓝色标题栏 + R.drawable.edit_title_white, // 白色标题栏 + R.drawable.edit_title_green, // 绿色标题栏 + R.drawable.edit_title_red // 红色标题栏 + }; + + /** + * 根据颜色ID获取笔记编辑界面背景资源 + * @param id 颜色ID (0-4) + * @return 对应的背景资源ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 根据颜色ID获取笔记编辑界面标题栏背景资源 + * @param id 颜色ID (0-4) + * @return 对应的标题栏背景资源ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认笔记背景颜色ID + * 根据用户设置决定是返回随机颜色还是默认颜色 + * @param context 应用上下文 + * @return 背景颜色ID + */ + public static int getDefaultBgId(Context context) { + // 检查用户是否开启了"随机背景色"设置 + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 随机生成一个颜色ID + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + // 返回默认颜色ID + return BG_DEFAULT_COLOR; + } + } + + /** + * 笔记列表项背景资源管理类 + * 提供获取不同位置(首项、中间项、末项、单项)和不同颜色的笔记列表项背景资源 + */ + public static class NoteItemBgResources { + // 列表首项背景资源数组,按颜色索引 + private final static int [] BG_FIRST_RESOURCES = new int [] { + R.drawable.list_yellow_up, // 黄色首项背景 + R.drawable.list_blue_up, // 蓝色首项背景 + R.drawable.list_white_up, // 白色首项背景 + R.drawable.list_green_up, // 绿色首项背景 + R.drawable.list_red_up // 红色首项背景 + }; + + // 列表中间项背景资源数组,按颜色索引 + private final static int [] BG_NORMAL_RESOURCES = new int [] { + R.drawable.list_yellow_middle, // 黄色中间项背景 + R.drawable.list_blue_middle, // 蓝色中间项背景 + R.drawable.list_white_middle, // 白色中间项背景 + R.drawable.list_green_middle, // 绿色中间项背景 + R.drawable.list_red_middle // 红色中间项背景 + }; + + // 列表末项背景资源数组,按颜色索引 + private final static int [] BG_LAST_RESOURCES = new int [] { + R.drawable.list_yellow_down, // 黄色末项背景 + R.drawable.list_blue_down, // 蓝色末项背景 + R.drawable.list_white_down, // 白色末项背景 + R.drawable.list_green_down, // 绿色末项背景 + R.drawable.list_red_down, // 红色末项背景 + }; + + // 列表单项(只有一项)背景资源数组,按颜色索引 + private final static int [] BG_SINGLE_RESOURCES = new int [] { + R.drawable.list_yellow_single, // 黄色单项背景 + R.drawable.list_blue_single, // 蓝色单项背景 + R.drawable.list_white_single, // 白色单项背景 + R.drawable.list_green_single, // 绿色单项背景 + R.drawable.list_red_single // 红色单项背景 + }; + + /** + * 根据颜色ID获取列表首项背景资源 + * @param id 颜色ID (0-4) + * @return 对应的首项背景资源ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + /** + * 根据颜色ID获取列表末项背景资源 + * @param id 颜色ID (0-4) + * @return 对应的末项背景资源ID + */ + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + /** + * 根据颜色ID获取列表单项背景资源 + * @param id 颜色ID (0-4) + * @return 对应的单项背景资源ID + */ + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + /** + * 根据颜色ID获取列表中间项背景资源 + * @param id 颜色ID (0-4) + * @return 对应的中间项背景资源ID + */ + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; + } + + /** + * 获取文件夹背景资源 + * @return 文件夹背景资源ID + */ + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + /** + * 桌面小部件背景资源管理类 + * 提供获取不同尺寸(2x和4x)和不同颜色的小部件背景资源 + */ + public static class WidgetBgResources { + // 2x尺寸小部件背景资源数组,按颜色索引 + private final static int [] BG_2X_RESOURCES = new int [] { + R.drawable.widget_2x_yellow, // 黄色2x小部件背景 + R.drawable.widget_2x_blue, // 蓝色2x小部件背景 + R.drawable.widget_2x_white, // 白色2x小部件背景 + R.drawable.widget_2x_green, // 绿色2x小部件背景 + R.drawable.widget_2x_red, // 红色2x小部件背景 + }; + + /** + * 根据颜色ID获取2x尺寸小部件背景资源 + * @param id 颜色ID (0-4) + * @return 对应的2x小部件背景资源ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x尺寸小部件背景资源数组,按颜色索引 + private final static int [] BG_4X_RESOURCES = new int [] { + R.drawable.widget_4x_yellow, // 黄色4x小部件背景 + R.drawable.widget_4x_blue, // 蓝色4x小部件背景 + R.drawable.widget_4x_white, // 白色4x小部件背景 + R.drawable.widget_4x_green, // 绿色4x小部件背景 + R.drawable.widget_4x_red // 红色4x小部件背景 + }; + + /** + * 根据颜色ID获取4x尺寸小部件背景资源 + * @param id 颜色ID (0-4) + * @return 对应的4x小部件背景资源ID + */ + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + /** + * 文本样式资源管理类 + * 提供获取不同字体大小的文本样式资源 + */ + public static class TextAppearanceResources { + // 文本样式资源数组,按字体大小索引 + private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { + R.style.TextAppearanceNormal, // 小号字体样式 + R.style.TextAppearanceMedium, // 中号字体样式 + R.style.TextAppearanceLarge, // 大号字体样式 + R.style.TextAppearanceSuper // 超大号字体样式 + }; + + /** + * 根据字体大小ID获取文本样式资源 + * @param id 字体大小ID (0-3) + * @return 对应的文本样式资源ID + */ + public static int getTexAppearanceResource(int id) { + /** + * HACKME: 修复在共享偏好中存储资源ID的问题 + * 如果ID大于资源数组长度,返回默认字体大小对应的资源ID + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + /** + * 获取文本样式资源数组的长度 + * @return 资源数组长度 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} \ No newline at end of file