From 239a8760947640b74fa65746083b1c22e4aa9ee9 Mon Sep 17 00:00:00 2001 From: yolo <2915594363@qq.com> Date: Thu, 12 Dec 2024 20:10:46 +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 --- tool/BackupUtils.java | 319 +++++++++++++++++++++++++++++++++++++ tool/DataUtils.java | 289 +++++++++++++++++++++++++++++++++ tool/GTaskStringUtils.java | 99 ++++++++++++ tool/ResourceParser.java | 189 ++++++++++++++++++++++ 4 files changed, 896 insertions(+) create mode 100644 tool/BackupUtils.java create mode 100644 tool/DataUtils.java create mode 100644 tool/GTaskStringUtils.java create mode 100644 tool/ResourceParser.java diff --git a/tool/BackupUtils.java b/tool/BackupUtils.java new file mode 100644 index 0000000..46e58af --- /dev/null +++ b/tool/BackupUtils.java @@ -0,0 +1,319 @@ +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; + +// 备份工具类,用于将笔记数据导出为文本文件 +public class BackupUtils { + private static final String TAG = "BackupUtils"; + + // 单例模式相关 + private static BackupUtils sInstance; + + // 获取单例实例 + 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; + + // 私有构造函数,初始化TextExport实例 + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + // 检查外部存储是否可用 + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + // 导出笔记数据为文本文件,调用TextExport的exportToText方法 + public int exportToText() { + return mTextExport.exportToText(); + } + + // 获取导出的文本文件名 + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + // 获取导出的文本文件目录 + public String getExportedTextFileDir() { + return mTextExport.mFileDirectory; + } + + // 内部类,用于处理文本导出的具体操作 + private static class TextExport { + // 查询笔记的投影,包括ID、修改日期、片段和类型 + private static final String[] NOTE_PROJECTION = { + NoteColumns.ID, + NoteColumns.MODIFIED_DATE, + NoteColumns.SNIPPET, + NoteColumns.TYPE + }; + // 笔记投影中ID列的索引 + 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; + + // 查询数据的投影,包括内容、MIME类型、数据1、数据2、数据3、数据4 + 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; + // 数据投影中MIME类型列的索引 + private static final int DATA_COLUMN_MIME_TYPE = 1; + // 数据投影中通话日期列的索引(假设为DATA1) + private static final int DATA_COLUMN_CALL_DATE = 2; + // 数据投影中电话号码列的索引(假设为DATA3) + 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]; + } + + // 将指定文件夹及其笔记导出为文本,打印到PrintStream + 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(); + } + } + + // 将指定笔记导出为文本,打印到PrintStream + 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; + } + + // 获取指向导出文本文件的PrintStream + 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; + } + } + + // 在SD卡上生成用于存储导入数据的文本文件 + 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/tool/DataUtils.java b/tool/DataUtils.java new file mode 100644 index 0000000..76e7f22 --- /dev/null +++ b/tool/DataUtils.java @@ -0,0 +1,289 @@ +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) { + 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; + } + + // 将笔记移动到指定文件夹的方法 + 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); + // 更新笔记的父文件夹ID等信息 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + // 批量将笔记移动到指定文件夹的方法 + 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; + } + + // 获取除系统文件夹外的所有文件夹数量的方法 + 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; + } + + // 检查笔记是否在笔记数据库中可见(非回收站且类型匹配)的方法 + 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; + } + + // 检查笔记是否存在于笔记数据库中的方法 + 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; + } + + // 检查数据是否存在于数据数据库中的方法 + 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; + } + + // 检查文件夹名称是否可见(非回收站且名称唯一)的方法 + 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; + } + + // 获取指定文件夹中笔记的小部件属性集合的方法 + 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获取通话号码的方法 + 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的方法 + 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获取笔记片段的方法 + 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); + } + + // 格式化笔记片段的方法,去除首尾空格和换行符 + 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/tool/GTaskStringUtils.java b/tool/GTaskStringUtils.java new file mode 100644 index 0000000..1c72382 --- /dev/null +++ b/tool/GTaskStringUtils.java @@ -0,0 +1,99 @@ +package net.micode.notes.tool; + +// 用于处理与GTask相关的字符串常量的工具类 +public class GTaskStringUtils { + + // GTask JSON中动作ID的键 + public final static String GTASK_JSON_ACTION_ID = "action_id"; + // GTask JSON中动作列表的键 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + // GTask JSON中动作类型的键 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + // GTask JSON中创建动作类型的值 + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + // GTask JSON中获取所有动作类型的值 + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + // GTask JSON中移动动作类型的值 + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + // GTask JSON中更新动作类型的值 + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + // GTask JSON中创建者ID的键 + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + // GTask JSON中子实体的键 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + // GTask JSON中客户端版本的键 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + // GTask JSON中完成状态的键 + public final static String GTASK_JSON_COMPLETED = "completed"; + // GTask JSON中当前列表ID的键 + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + // GTask JSON中默认列表ID的键 + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + // GTask JSON中已删除状态的键 + public final static String GTASK_JSON_DELETED = "deleted"; + // GTask JSON中目标列表的键 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + // GTask JSON中目标父级的键 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + // GTask JSON中目标父级类型的键 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + // GTask JSON中实体增量的键 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + // GTask JSON中实体类型的键 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + // GTask JSON中获取已删除项的键 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + // GTask JSON中ID的键 + public final static String GTASK_JSON_ID = "id"; + // GTask JSON中索引的键 + public final static String GTASK_JSON_INDEX = "index"; + // GTask JSON中最后修改时间的键 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + // GTask JSON中最新同步点的键 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + // GTask JSON中列表ID的键 + public final static String GTASK_JSON_LIST_ID = "list_id"; + // GTask JSON中列表的键 + public final static String GTASK_JSON_LISTS = "lists"; + // GTask JSON中名称的键 + public final static String GTASK_JSON_NAME = "name"; + // GTask JSON中新ID的键 + public final static String GTASK_JSON_NEW_ID = "new_id"; + // GTask JSON中笔记的键 + public final static String GTASK_JSON_NOTES = "notes"; + // GTask JSON中父级ID的键 + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + // GTask JSON中前一个兄弟ID的键 + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + // GTask JSON中结果的键 + public final static String GTASK_JSON_RESULTS = "results"; + // GTask JSON中源列表的键 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + // GTask JSON中任务的键 + public final static String GTASK_JSON_TASKS = "tasks"; + // GTask JSON中类型的键 + public final static String GTASK_JSON_TYPE = "type"; + // GTask JSON中组类型的值 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + // GTask JSON中任务类型的值 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + // GTask 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"; + // 元数据中GTask 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/tool/ResourceParser.java b/tool/ResourceParser.java new file mode 100644 index 0000000..996def4 --- /dev/null +++ b/tool/ResourceParser.java @@ -0,0 +1,189 @@ +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获取笔记编辑背景资源 + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[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; + } + } + + // 内部类,用于管理笔记列表项背景资源 + 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]; + } + + // 获取文件夹背景资源 + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + // 内部类,用于管理小部件背景资源 + public static class WidgetBgResources { + // 2x小部件的背景资源数组 + 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, + }; + // 4x小部件的背景资源数组 + 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获取2x小部件的背景资源 + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 根据ID获取4x小部件的背景资源 + 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获取文本外观资源 + public static int getTexAppearanceResource(int id) { + /** + * HACKME: 修复在共享偏好设置中存储资源ID的问题。 + * 如果ID大于资源数组的长度,则返回默认字体大小。 + */ + 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