From 070f9b7202891853f57c7200ff820543c5b4eac1 Mon Sep 17 00:00:00 2001 From: Maike Date: Sun, 5 May 2024 14:47:11 +0800 Subject: [PATCH] xdx --- src/net/micode/notes/tool/BackupUtils.java | 370 ++++++++++++++++++ src/net/micode/notes/tool/DataUtils.java | 363 +++++++++++++++++ .../micode/notes/tool/GTaskStringUtils.java | 78 ++++ src/net/micode/notes/tool/ResourceParser.java | 235 +++++++++++ src/net/micode/notes/tool/tool.iml | 11 + 5 files changed, 1057 insertions(+) create mode 100644 src/net/micode/notes/tool/BackupUtils.java create mode 100644 src/net/micode/notes/tool/DataUtils.java create mode 100644 src/net/micode/notes/tool/GTaskStringUtils.java create mode 100644 src/net/micode/notes/tool/ResourceParser.java create mode 100644 src/net/micode/notes/tool/tool.iml diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java new file mode 100644 index 0000000..4060167 --- /dev/null +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -0,0 +1,370 @@ +/* + * 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;//用于管理笔记数据和资源n +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;//在JAVA中实现操作功能的类 +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +//笔记备份工具类 +public class BackupUtils { + private static final String TAG = "BackupUtils"; + // Singleton stuff 单例模式 + private static BackupUtils sInstance; + + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + // 备份和恢复功能 + //定义备份或恢复数据时可能遇到的状态码。 + +public class BackupState { + + //当前SD未挂载 + + public static final int STATE_SD_CARD_UNMOUNTED = 0; + + //备份文件不存在 + + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + + // 数据格式不正确,可能被其他程序更改 + + public static final int STATE_DATA_DESTROYED = 2; + + //运行时异常导致恢复或备份失败 + + public static final int STATE_SYSTEM_ERROR = 3; + + // 备份或恢复成功 + + public static final int STATE_SUCCESS = 4; + + // 用于导出文件的目录 + private TextExport mTextExport; +} + + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + public int exportToText() { + return mTextExport.exportToText(); + } + + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + public String getExportedTextFileDir() { + return mTextExport.mFileDirectory; + } + // 笔记备份类 + private static class TextExport { + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, + NoteColumns.MODIFIED_DATE, + NoteColumns.SNIPPET, + NoteColumns.TYPE + }; + //定义Note数据表中各列对应的索引常量。 + 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, + DataColumns.DATA1, + DataColumns.DATA2, + DataColumns.DATA3, + 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; + + private static final int DATA_COLUMN_PHONE_NUMBER = 4; + + 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 Context mContext; + private String mFileName; + private String mFileDirectory; + // 备份和恢复功能 + public TextExport(Context context) { + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + mFileName = ""; + mFileDirectory = ""; + } + //同上 + private String getFormat(int id) { + return TEXT_FORMAT[id]; + } + + // 导出笔记到文本 + private void exportFolderToText(String folderId, PrintStream ps) { + // Query notes belong to this folder + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { + folderId + }, null); + // 查询笔记 + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // Print note's last modified date + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // Query data belong to this note + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); + } + } + // 导出笔记到文本 + private void exportNoteToText(String noteId, PrintStream ps) { + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { + noteId + }, null); + // 查询笔记数据 + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do {// 开始遍历笔记数据 + // 获取当前行的MIME类型 + 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)); + } + // 获取并打印通话日期 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + // 获取并打印附件位置(如果存在) + 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()); + } + // 关闭数据游标 + dataCursor.close(); + } + // print a line separator between note + + try { + ps.write(new byte[] { + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } + // 导出笔记到文本 + public int exportToText() { + if (!externalStorageAvailable()) { + Log.d(TAG, "Media was not mounted"); + return STATE_SD_CARD_UNMOUONTED; + } + // 错误输出 + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + Log.e(TAG, "get print stream error"); + return STATE_SYSTEM_ERROR; + } + // First export folder and its notes + + // 查询文件夹 + Cursor folderCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + // 遍历文件夹 + if (folderCursor != null) { + 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()); + } + // 关闭文件夹游标 + folderCursor.close(); + } + + // Export trash folder + + // Export notes in root's folder + 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) { + if (noteCursor.moveToFirst()) { + do { + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // Query data belong to this note + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext()); + } + noteCursor.close(); + } + ps.close(); + + return STATE_SUCCESS; + } + + /** + * Get a print stream pointed to the file {@generateExportedTextFile} + */ + // 获取打印流 + private PrintStream getExportToTextPrintStream() { + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + Log.e(TAG, "create file to exported failed"); + return null; + } + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); + + PrintStream ps = null; + // 创建文件输出流 + try { + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + * Generate the text file to store imported data + */ + + // 生成文件 + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + StringBuilder sb = new StringBuilder(); + sb.append(Environment.getExternalStorageDirectory()); + sb.append(context.getString(filePathResId)); + File filedir = new File(sb.toString()); + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); + File file = new File(sb.toString()); + // 创建文件 + try { + // 检查目录是否存在,不存在则创建目录 + if (!filedir.exists()) { + filedir.mkdir(); + } + // 检查文件是否存在,不存在则创建文件 + if (!file.exists()) { + file.createNewFile(); + } + return file; + } catch (SecurityException e) { + // 处理安全异常 + e.printStackTrace(); + } catch (IOException e) { + // 处理IO异常 + e.printStackTrace(); + } + + // 遇到异常返回null + return null; + } +} + + diff --git a/src/net/micode/notes/tool/DataUtils.java b/src/net/micode/notes/tool/DataUtils.java new file mode 100644 index 0000000..df43ed3 --- /dev/null +++ b/src/net/micode/notes/tool/DataUtils.java @@ -0,0 +1,363 @@ +/* + * 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. + */ +//与backup部分库类似,不再赘述 +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; + + +public class DataUtils { + public static final String TAG = "DataUtils"; + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查提供的ID集合是否为null + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + // 检查ID集合是否为空 + if (ids.size() == 0) { + Log.d(TAG, "no id is in the hashset"); + return true; + } + // TODO: 实现根据提供的ids批量删除笔记的逻辑 + return false; + } +} + + public boolean deleteNotes(long[] ids) { + // 创建一个内容提供者操作的列表,用于存储删除操作 + ArrayList operationList = new ArrayList(); + + // 遍历所有给定的ID + for (long id : ids) { + // 检查ID是否为系统根文件夹ID,如果是则跳过删除操作 + if(id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 为每个非系统根文件夹ID构建一个删除操作,并添加到操作列表中 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + operationList.add(builder.build()); + } + + try { + // 执行内容提供者操作列表中的所有操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + + // 检查删除操作的结果,如果没有成功删除任何笔记,则返回false + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 所有删除操作执行成功,返回true + return true; + } catch (RemoteException e) { + // 处理执行删除操作时可能出现的远程异常 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 处理执行删除操作时可能出现的操作应用异常 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 如果有任何异常发生,则返回false + 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(); + values.put(NoteColumns.PARENT_ID, desFolderId); // 设置新的父文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 记录原始父文件夹ID + 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 如果移动操作成功执行,则返回true;否则返回false。 + */ +public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, + long folderId) { + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; // 如果ID集为空,视为操作成功 + } + + // 准备批量操作 + ArrayList operationList = new ArrayList(); + for (long id : ids) { + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置新的父文件夹ID + 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, "delete notes failed, ids:" + ids.toString()); + return false; // 如果没有成功的结果,返回false + } + return true; // 批量操作成功 + } catch (RemoteException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + return false; // 捕获异常,返回失败 +} + + /** + * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + */ + 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) { + // 如果查询结果不为空,则尝试获取计数结果 + if(cursor.moveToFirst()) { + try { + count = cursor.getInt(0); // 获取查询结果中的计数 + } catch (IndexOutOfBoundsException e) { + // 处理可能的索引越界异常 + Log.e(TAG, "get folder count failed:" + e.toString()); + } finally { + // 无论是否成功,最后都关闭Cursor + cursor.close(); + } + } + } + return count; // 返回文件夹数量 + } + + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 根据noteId和类型查询数据库,排除父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) { + // 检查查询结果是否有记录 + if (cursor.getCount() > 0) { + exist = true; + } + // 关闭Cursor + cursor.close(); + } + return exist; + } + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 使用ContentUris和noteId构建查询URI,并执行查询 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); + + boolean exist = false; + if (cursor != null) { + // 如果查询结果不为空,检查是否有记录匹配 + if (cursor.getCount() > 0) { + exist = true; + } + // 关闭Cursor + cursor.close(); + } + return exist; +} + + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 使用ContentUris和dataId构建查询URI,并执行查询 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), + null, null, null, null); + + boolean exist = false; + if (cursor != null) { + // 如果查询结果不为空,检查是否有记录匹配 + if (cursor.getCount() > 0) { + exist = true; + } + // 关闭Cursor + cursor.close(); + } + return exist; +} + + 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) { + // 如果查询结果不为空,检查是否有记录匹配 + if(cursor.getCount() > 0) { + exist = true; + } + // 关闭Cursor + cursor.close(); + } + return exist; +} + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 根据指定的文件夹ID查询笔记小部件的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) { + // 如果查询结果不为空,则遍历结果并填充集合 + if (c.moveToFirst()) { + set = new HashSet(); + do { + try { + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); // 获取小部件ID + widget.widgetType = c.getInt(1); // 获取小部件类型 + set.add(widget); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + c.close(); // 关闭游标 + } + return set; +} + + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 根据笔记ID和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, "Get call number fails " + e.toString()); + } finally { + cursor.close(); // 关闭游标 + } + } + return ""; // 如果未查询到电话号码,则返回空字符串 +} + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 构造查询语句,查询特定电话号码、通话日期和MIME类型的注释ID + 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); + + if (cursor != null) { + if (cursor.moveToFirst()) { + try { + // 如果查询结果存在,返回第一行的注释ID + return cursor.getLong(0); + } catch (IndexOutOfBoundsException e) { + // 处理索引越界异常 + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + // 关闭游标 + cursor.close(); + } + // 如果查询无结果,返回0 + return 0; +} + + + public static String getSnippetById(ContentResolver resolver, long noteId) { + // 构造查询语句,查询特定ID的注释的摘要 + 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 = ""; + if (cursor.moveToFirst()) { + // 如果查询结果存在,获取摘要 + snippet = cursor.getString(0); + } + // 关闭游标 + cursor.close(); + return snippet; + } + // 如果查询无结果,抛出异常 + throw new IllegalArgumentException("Note is not found with id: " + noteId); +} + + + + 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/net/micode/notes/tool/GTaskStringUtils.java b/src/net/micode/notes/tool/GTaskStringUtils.java new file mode 100644 index 0000000..3b31dda --- /dev/null +++ b/src/net/micode/notes/tool/GTaskStringUtils.java @@ -0,0 +1,78 @@ +/* + * 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; + +/** + * GTaskStringUtils 是一个工具类,提供了一系列与 Google 任务 API 交互时使用的静态常量字符串。 + * 这些常量涵盖了 JSON 字段名称、操作类型以及其他标识符,用于 Google 任务框架内的任务同步与操作。 + */ +public class GTaskStringUtils { + + // 一组与 Google 任务 API 请求和响应中使用的 JSON 字段名称相关的常量。 + public final static String GTASK_JSON_ACTION_ID = "action_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"; + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + 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"; + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_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_ENTITY_DELTA = "entity_delta"; + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + public final static String GTASK_JSON_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"; + 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"; + public final static String GTASK_JSON_NOTES = "notes"; + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_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"; + + // 特定于某个应用或系统的常量 + 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"; + 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/net/micode/notes/tool/ResourceParser.java b/src/net/micode/notes/tool/ResourceParser.java new file mode 100644 index 0000000..244634e --- /dev/null +++ b/src/net/micode/notes/tool/ResourceParser.java @@ -0,0 +1,235 @@ +/* + * 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; + +/** + * ResourceParser 类设计用于便捷地访问与笔记背景及文本样式相关的资源标识符。 + * 它封装了表示各种颜色、文本大小的静态常量,并为不同的笔记编辑场景提供了对drawable资源的访问。 + */ +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; + + /** + * NoteBgResources 内部类封装了与编辑笔记背景相关的drawable资源数组。 + * 分别为普通编辑背景(BG_EDIT_RESOURCES)和标题编辑背景(BG_EDIT_TITLE_RESOURCES), + * 对应于已定义的不同颜色常量,便于在实际应用中根据颜色选择相应的资源。 + */ + public static class NoteBgResources { + // 编辑笔记背景时使用的drawable资源数组,与颜色常量一一对应。 + 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 + }; + + // 编辑笔记标题背景时使用的drawable资源数组,同样与颜色常量一一对应。 + 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,用于索引背景资源数组。 + * @return 返回对应id的背景资源。 + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 根据提供的id获取注释标题背景资源。 + * @param id 注释标题的id,用于索引标题背景资源数组。 + * @return 返回对应id的标题背景资源。 + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + + /** + * 获取默认背景资源id。 + * 根据用户设置是否设置了自定义背景颜色来决定返回随机背景资源或默认背景资源。 + * @param context 上下文对象,用于访问SharedPreferences。 + * @return 返回背景资源的id。 + */ + public static int getDefaultBgId(Context context) { + // 检查用户是否设置了自定义背景颜色 + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( + NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 如果设置了,返回一个随机的背景资源 + return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); + } else { + // 如果未设置,返回默认背景资源 + 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,获取对应的笔记背景资源的第一个资源。 + +public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; +} + +//根据提供的ID,获取对应的笔记背景资源的最后一个资源。 +public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; +} + +//根据提供的ID,获取对应的笔记背景资源的单个资源。 +public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; +} + +//根据提供的ID,获取对应的笔记背景资源的普通资源。 +public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; +} + + +//获取文件夹背景资源的ID。 +public static int getFolderBgRes() { + return R.drawable.list_folder; +} + +//这个内部类提供了一系列的Widget背景资源。 + +public static class WidgetBgResources { + + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 提供4x尺寸的Widget背景资源。 + + private final static int [] BG_4X_RESOURCES = new int [] { + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + //获取指定索引的4x Widget背景资源的ID。 +c 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? +} + + public class ResourceParser { + + // HACKME: 修复了存储资源ID到共享偏好中的bug。如果ID大于资源数组的长度, + // 则返回默认字体大小BG_DEFAULT_FONT_SIZE。 + public static int getTexAppearanceResource(int id) { + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + // 获取TEXTAPPEARANCE_RESOURCES数组的大小。 + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/tool.iml b/src/net/micode/notes/tool/tool.iml new file mode 100644 index 0000000..af609a1 --- /dev/null +++ b/src/net/micode/notes/tool/tool.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file