From 73c0f941b3efbcaf7d246db195e307791f80e087 Mon Sep 17 00:00:00 2001 From: liuwei <3316506926@qq.com> Date: Tue, 31 Dec 2024 17:16:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- t/b.txt | 351 ++++++++++++++++++++++++++++++++++++++++++++++++++++ t/d.txt | 377 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ t/g.txt | 113 +++++++++++++++++ t/r.txt | 219 ++++++++++++++++++++++++++++++++ 4 files changed, 1060 insertions(+) create mode 100644 t/b.txt create mode 100644 t/d.txt create mode 100644 t/g.txt create mode 100644 t/r.txt diff --git a/t/b.txt b/t/b.txt new file mode 100644 index 0000000..d4433a7 --- /dev/null +++ b/t/b.txt @@ -0,0 +1,351 @@ +/* + * Copyright (c) 2010 - 2011, The MiCode Open Source Community (www.micode.net) + * + * 本代码遵循Apache许可证2.0版(“许可证”); + * 除非遵守许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证副本: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,依据许可证分发的软件 + * 按“原样”分发,不附带任何明示或暗示的保证或条件。 + * 请参阅许可证,了解具体的权限和限制。 + */ + +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; + +// BackupUtils类提供了将笔记数据备份为文本文件的功能,包括检查外部存储状态、导出数据等操作 +public class BackupUtils { + // 日志标签 + private static final String TAG = "BackupUtils"; + // 单例实例 + private static BackupUtils sInstance; + + // 获取BackupUtils单例实例的方法 + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 以下状态表示备份或恢复的状态 + */ + // 当前SD卡未挂载 + 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 TextExport mTextExport; + + // BackupUtils的构造函数,初始化TextExport对象 + 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_PROJECTION中笔记ID列的索引 + private static final int NOTE_COLUMN_ID = 0; + // NOTE_PROJECTION中笔记修改日期列的索引 + private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + // NOTE_PROJECTION中笔记摘要列的索引 + 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, + }; + + // DATA_PROJECTION中内容列的索引 + private static final int DATA_COLUMN_CONTENT = 0; + // DATA_PROJECTION中MIME类型列的索引 + private static final int DATA_COLUMN_MIME_TYPE = 1; + // DATA_PROJECTION中通话日期列的索引 + private static final int DATA_COLUMN_CALL_DATE = 2; + // DATA_PROJECTION中电话号码列的索引 + 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; + + // TextExport的构造函数,初始化上下文和文本格式数组 + 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]; + } + + /** + * 将指定文件夹ID标识的文件夹导出为文本 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的笔记 + 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 { + // 打印笔记的最后修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 查询属于该笔记的数据 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); + } + } + + /** + * 将指定ID标识的笔记导出到打印流 + */ + 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 { + 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(); + } + // 在笔记之间打印一行分隔符 + 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; + } + // 首先导出文件夹及其笔记 + 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(); + } + + // 导出根文件夹中的笔记 + 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)))); + // 查询属于该笔记的数据 + String noteId = noteCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (noteCursor.moveToNext()); + } + noteCursor.close(); + } + ps.close(); + + return STATE_SUCCESS; + } + + /** + * 获取指向{@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; + } + } + + /** + * 生成用于存储导入数据的文本文件 + */ + 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) { + e.printStackTrace(); + } + + return null; + } +} \ No newline at end of file diff --git a/t/d.txt b/t/d.txt new file mode 100644 index 0000000..6ef8308 --- /dev/null +++ b/t/d.txt @@ -0,0 +1,377 @@ +/* + * Copyright (c) 2010 - 2011, The MiCode Open Source Community (www.micode.net) + * + * 本代码遵循Apache许可证2.0版(“许可证”); + * 除非遵守许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证副本: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,依据许可证分发的软件 + * 按“原样”分发,不附带任何明示或暗示的保证或条件。 + * 请参阅许可证,了解具体的权限和限制。 + */ + +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; + +// DataUtils类提供了一系列用于操作笔记数据的工具方法,包括批量删除、移动笔记、查询数据等功能 +public class DataUtils { + // 日志标签 + public static final String TAG = "DataUtils"; + + /** + * 批量删除笔记的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param ids 包含要删除笔记ID的HashSet + * @return 如果删除成功返回true,否则返回false + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + if (ids.size() == 0) { + Log.d(TAG, "no id is in the hashset"); + return true; + } + + ArrayList operationList = new ArrayList(); + for (long id : ids) { + if (id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 构建删除操作 + 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, "delete notes failed, ids:" + ids.toString()); + return 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; + } + + /** + * 将笔记移动到指定文件夹的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @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); + 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 ContentResolver对象,用于与内容提供者进行交互 + * @param ids 包含要移动笔记ID的HashSet + * @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; + } + + 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); + 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; + } + 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; + } + + /** + * 获取用户文件夹数量(不包括系统文件夹)的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @return 用户文件夹的数量 + */ + public static int getUserFolderCount(ContentResolver resolver) { + 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.close(); + } + } + } + return count; + } + + /** + * 检查笔记在数据库中是否可见(根据类型和不在回收站)的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param noteId 要检查的笔记ID + * @param type 笔记类型 + * @return 如果笔记可见返回true,否则返回false + */ + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + 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.close(); + } + return exist; + } + + /** + * 检查笔记是否存在于笔记数据库中的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param noteId 要检查的笔记ID + * @return 如果笔记存在返回true,否则返回false + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + 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.close(); + } + return exist; + } + + /** + * 检查数据是否存在于数据数据库中的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param dataId 要检查的数据ID + * @return 如果数据存在返回true,否则返回false + */ + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + 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.close(); + } + return exist; + } + + /** + * 检查可见文件夹名称是否已存在的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param name 要检查的文件夹名称 + * @return 如果文件夹名称已存在返回true,否则返回false + */ + 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.close(); + } + return exist; + } + + /** + * 获取指定文件夹下笔记的小部件属性的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param folderId 文件夹ID + * @return 包含小部件属性的HashSet,如果没有则返回null + */ + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + 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); + widget.widgetType = c.getInt(1); + set.add(widget); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + c.close(); + } + return set; + } + + /** + * 根据笔记ID获取通话号码的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param noteId 笔记ID + * @return 通话号码,如果未找到则返回空字符串 + */ + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + 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 ""; + } + + /** + * 根据电话号码和通话日期获取笔记ID的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + * @return 笔记ID,如果未找到则返回0 + */ + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + 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 { + return cursor.getLong(0); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + cursor.close(); + } + return 0; + } + + /** + * 根据笔记ID获取摘要的方法 + * @param resolver ContentResolver对象,用于与内容提供者进行交互 + * @param noteId 笔记ID + * @return 摘要,如果未找到则抛出IllegalArgumentException + */ + 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 = ""; + if (cursor.moveToFirst()) { + snippet = cursor.getString(0); + } + cursor.close(); + return snippet; + } + throw new IllegalArgumentException("Note is not found with 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/t/g.txt b/t/g.txt new file mode 100644 index 0000000..95aeb9e --- /dev/null +++ b/t/g.txt @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2010 - 2011, The MiCode Open Source Community (www.micode.net) + * + * 本代码遵循Apache许可证2.0版(“许可证”); + * 除非遵守许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证副本: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,依据许可证分发的软件 + * 按“原样”分发,不附带任何明示或暗示的保证或条件。 + * 请参阅许可证,了解具体的权限和限制。 + */ + +package net.micode.notes.tool; + +// GTaskStringUtils类定义了一系列与Google Tasks(可能是相关功能或数据交互)相关的字符串常量 +public class GTaskStringUtils { + + // Google Tasks JSON数据中,用于标识操作ID的键 + public final static String GTASK_JSON_ACTION_ID = "action_id"; + // Google Tasks JSON数据中,用于标识操作列表的键 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + // Google Tasks JSON数据中,用于标识操作类型的键 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + // Google Tasks操作类型:创建 + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + // Google Tasks操作类型:获取所有 + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + // Google Tasks操作类型:移动 + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + // Google Tasks操作类型:更新 + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + // Google Tasks JSON数据中,用于标识创建者ID的键 + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + // Google Tasks JSON数据中,用于标识子实体的键 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + // Google Tasks JSON数据中,用于标识客户端版本的键 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + // Google Tasks JSON数据中,用于标识任务是否完成的键 + public final static String GTASK_JSON_COMPLETED = "completed"; + // Google Tasks JSON数据中,用于标识当前列表ID的键 + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + // Google Tasks JSON数据中,用于标识默认列表ID的键 + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + // Google Tasks JSON数据中,用于标识是否删除的键 + public final static String GTASK_JSON_DELETED = "deleted"; + // Google Tasks JSON数据中,用于标识目标列表的键 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + // Google Tasks JSON数据中,用于标识目标父级的键 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + // Google Tasks JSON数据中,用于标识目标父级类型的键 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + // Google Tasks JSON数据中,用于标识实体增量的键 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + // Google Tasks JSON数据中,用于标识实体类型的键 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + // Google Tasks JSON数据中,用于标识获取已删除项的键 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + // Google Tasks JSON数据中,用于标识ID的键 + public final static String GTASK_JSON_ID = "id"; + // Google Tasks JSON数据中,用于标识索引的键 + public final static String GTASK_JSON_INDEX = "index"; + // Google Tasks JSON数据中,用于标识最后修改时间的键 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + // Google Tasks JSON数据中,用于标识最新同步点的键 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + // Google Tasks JSON数据中,用于标识列表ID的键 + public final static String GTASK_JSON_LIST_ID = "list_id"; + // Google Tasks JSON数据中,用于标识列表集合的键 + public final static String GTASK_JSON_LISTS = "lists"; + // Google Tasks JSON数据中,用于标识名称的键 + public final static String GTASK_JSON_NAME = "name"; + // Google Tasks JSON数据中,用于标识新ID的键 + public final static String GTASK_JSON_NEW_ID = "new_id"; + // Google Tasks JSON数据中,用于标识备注的键 + public final static String GTASK_JSON_NOTES = "notes"; + // Google Tasks JSON数据中,用于标识父级ID的键 + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + // Google Tasks JSON数据中,用于标识前一个兄弟ID的键 + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + // Google Tasks JSON数据中,用于标识结果的键 + public final static String GTASK_JSON_RESULTS = "results"; + // Google Tasks JSON数据中,用于标识源列表的键 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + // Google Tasks JSON数据中,用于标识任务集合的键 + public final static String GTASK_JSON_TASKS = "tasks"; + // Google Tasks JSON数据中,用于标识类型的键 + public final static String GTASK_JSON_TYPE = "type"; + // Google Tasks实体类型:组 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + // Google Tasks实体类型:任务 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + // Google Tasks JSON数据中,用于标识用户的键 + public final static String GTASK_JSON_USER = "user"; + + // MIUI文件夹前缀 + 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"; + // 元数据头部:Google Tasks ID + 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/t/r.txt b/t/r.txt new file mode 100644 index 0000000..4ccfbfc --- /dev/null +++ b/t/r.txt @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2010 - 2011, The MiCode Open Source Community (www.micode.net) + * + * 本代码遵循Apache许可证2.0版(“许可证”); + * 除非遵守许可证,否则您不得使用此文件。 + * 您可以在以下网址获取许可证副本: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 除非适用法律要求或书面同意,依据许可证分发的软件 + * 按“原样”分发,不附带任何明示或暗示的保证或条件。 + * 请参阅许可证,了解具体的权限和限制。 + */ + +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类用于解析和管理与笔记应用相关的各种资源,如背景颜色、字体大小、背景图片等资源 +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内部类用于管理笔记编辑界面的背景资源 + public static class NoteBgResources { + // 存储笔记编辑背景图片资源ID的数组 + 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 + }; + + // 存储笔记编辑标题背景图片资源ID的数组 + 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获取对应的笔记编辑背景资源ID + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + // 根据传入的颜色ID获取对应的笔记编辑标题背景资源ID + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + // 获取默认背景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; + } + } + + // NoteItemBgResources内部类用于管理笔记列表项的背景资源 + public static class NoteItemBgResources { + // 存储笔记列表项首项背景图片资源ID的数组 + 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 + }; + + // 存储笔记列表项中间项背景图片资源ID的数组 + 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 + }; + + // 存储笔记列表项末项背景图片资源ID的数组 + 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, + }; + + // 存储笔记列表项单项背景图片资源ID的数组 + 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获取对应的笔记列表项首项背景资源ID + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + // 根据传入的颜色ID获取对应的笔记列表项末项背景资源ID + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + // 根据传入的颜色ID获取对应的笔记列表项单项背景资源ID + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + // 根据传入的颜色ID获取对应的笔记列表项中间项背景资源ID + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_RESOURCES[id]; + } + + // 获取文件夹背景资源ID + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + // WidgetBgResources内部类用于管理小部件的背景资源 + public static class WidgetBgResources { + // 存储2x小部件背景图片资源ID的数组 + private final static int[] BG_2X_RESOURCES = new int[]{ + R.drawable.widget_2x_yellow, + R.drawable.widget_2x_blue, + R.drawable.widget_2x_white, + R.drawable.widget_2x_green, + R.drawable.widget_2x_red, + }; + + // 根据传入的颜色ID获取对应的2x小部件背景资源ID + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 存储4x小部件背景图片资源ID的数组 + 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 + }; + + // 根据传入的颜色ID获取对应的4x小部件背景资源ID + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + // TextAppearanceResources内部类用于管理文本外观资源 + public static class TextAppearanceResources { + // 存储文本外观样式资源ID的数组 + private final static int[] TEXTAPPEARANCE_RESOURCES = new int[]{ + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + // 根据传入的ID获取对应的文本外观资源ID,处理ID越界情况 + public static int getTexAppearanceResource(int id) { + /** + * 修复在共享偏好设置中存储资源ID的错误。 + * ID可能大于资源数组的长度,在这种情况下, + * 返回{@link ResourceParser#BG_DEFAULT_FONT_SIZE} + */ + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + // 获取文本外观资源数组的长度 + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} \ No newline at end of file