diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..f072336 100644 --- a/src/net/micode/notes/tool/BackupUtils.java +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -14,331 +14,348 @@ * limitations under the License. */ -package net.micode.notes.tool; - -import android.content.Context; -import android.database.Cursor; -import android.os.Environment; -import android.text.TextUtils; -import android.text.format.DateFormat; -import android.util.Log; - -import net.micode.notes.R; -import net.micode.notes.data.Notes; -import net.micode.notes.data.Notes.DataColumns; -import net.micode.notes.data.Notes.DataConstants; -import net.micode.notes.data.Notes.NoteColumns; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; - - -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; - } - - /** - * Following states are signs to represents backup or restore - * status - */ - // Currently, the sdcard is not mounted - public static final int STATE_SD_CARD_UNMOUONTED = 0; - // The backup file not exist - public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; - // The data is not well formated, may be changed by other programs - public static final int STATE_DATA_DESTROIED = 2; - // Some run-time exception which causes restore or backup fails - public static final int STATE_SYSTEM_ERROR = 3; - // Backup or restore success - 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 - }; - - 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]; - } - - /** - * Export the folder identified by folder id to text - */ - 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(); - } - } - - /** - * Export note identified by id to a print stream - */ - 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)) { - // Print phone number - 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)); - } - // Print call date - ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat - .format(mContext.getString(R.string.format_datetime_mdhm), - callDate))); - // Print call attachment location - 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()); - } - } - - /** - * Note will be exported as text which is user readable - */ - 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 { - // Print folder's name - 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 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) { - e.printStackTrace(); - } - - return null; - } -} - - + 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; + } + + /** + * 备份/恢复状态码常量定义 + */ + public static final int STATE_SD_CARD_UNMOUONTED = 0; // SD卡未挂载 + 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; // 文本导出处理器 + + // 私有构造函数初始化文本导出组件 + 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 + }; + private static final int NOTE_COLUMN_ID = 0; // 笔记ID列索引 + 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; // MIME类型列索引 + 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]; + } + + /** + * 导出指定文件夹下的笔记到文本流 + * @param folderId 要导出的文件夹ID + * @param ps 输出打印流 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的笔记 + Cursor notesCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.PARENT_ID + "=?", + 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(); + } + } + + /** + * 导出指定笔记到文本流 + * @param noteId 要导出的笔记ID + * @param ps 输出打印流 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + // 查询该笔记的所有数据项 + Cursor dataCursor = mContext.getContentResolver().query( + Notes.CONTENT_DATA_URI, + DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", + 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); + } + } + + /** + * 执行文本导出主逻辑 + * @return 操作状态码 + */ + public int exportToText() { + if (!externalStorageAvailable()) { + Log.d(TAG, "外部存储不可用"); + return STATE_SD_CARD_UNMOUONTED; + } + + PrintStream ps = getExportToTextPrintStream(); + if (ps == null) { + Log.e(TAG, "获取输出流失败"); + 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; + } + + /** + * 创建导出文件的打印流 + * @return 配置好的PrintStream对象 + */ + private PrintStream getExportToTextPrintStream() { + File file = generateFileMountedOnSDcard( + mContext, + R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + Log.e(TAG, "创建导出文件失败"); + return null; + } + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); + + try { + return new PrintStream(new FileOutputStream(file)); + } catch (FileNotFoundException e) { + Log.e(TAG, "文件未找到", e); + } catch (NullPointerException e) { + Log.e(TAG, "空指针异常", e); + } + return null; + } + } + + /** + * 在SD卡上生成指定格式的文件 + * @param context 上下文对象 + * @param filePathResId 文件路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的文件对象 + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + String path = Environment.getExternalStorageDirectory() + context.getString(filePathResId); + File dir = new File(path); + + // 生成完整文件路径 + String fullPath = path + context.getString(fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis())); + File file = new File(fullPath); + + try { + // 创建目录和文件 + if (!dir.exists() && !dir.mkdirs()) { + Log.e(TAG, "创建目录失败:" + path); + return null; + } + if (!file.exists() && !file.createNewFile()) { + Log.e(TAG, "创建文件失败:" + fullPath); + return null; + } + return file; + } catch (SecurityException e) { + Log.e(TAG, "安全权限异常", e); + } catch (IOException e) { + Log.e(TAG, "IO操作异常", e); + } + return null; + } + } \ No newline at end of file diff --git a/src/net/micode/notes/tool/DataUtils.java b/src/net/micode/notes/tool/DataUtils.java index 2a14982..c439c7b 100644 --- a/src/net/micode/notes/tool/DataUtils.java +++ b/src/net/micode/notes/tool/DataUtils.java @@ -14,282 +14,318 @@ * limitations under the License. */ -package net.micode.notes.tool; + 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); - 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; - } - - /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} - */ - 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; - } - - 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 ""; - } - - 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; - } - - 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; - } -} + 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 { + private static final String TAG = "DataUtils"; + + /** + * 批量删除笔记(支持事务操作) + * @param resolver 内容解析器 + * @param ids 要删除的笔记ID集合 + * @return true表示全部删除成功,false表示有部分失败 + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + if (ids == null) { + Log.d(TAG, "参数ids为空"); + return true; + } + if (ids.size() == 0) { + Log.d(TAG, "ID集合为空"); + return true; + } + + // 构建批量删除操作列表 + ArrayList operationList = new ArrayList<>(); + for (long id : ids) { + if(id == Notes.ID_ROOT_FOLDER) { // 防止删除系统根目录 + Log.e(TAG, "禁止删除系统根目录"); + continue; + } + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + operationList.add(builder.build()); + } + + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + return results != null && results.length > 0 && results[0] != null; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, "批量删除异常: " + e.getMessage()); + } + 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); // 记录原始目录 + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记本地修改 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + /** + * 批量移动笔记到指定文件夹 + * @param resolver 内容解析器 + * @param ids 需要移动的笔记ID集合 + * @param folderId 目标文件夹ID + * @return 操作是否成功 + */ + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { + if (ids == null) { + Log.d(TAG, "参数ids为空"); + 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); + return results != null && results.length > 0 && results[0] != null; + } catch (RemoteException | OperationApplicationException e) { + Log.e(TAG, "批量移动异常: " + e.getMessage()); + } + return false; + } + + /** + * 获取用户创建的文件夹数量(排除系统文件夹和回收站) + * @param resolver 内容解析器 + * @return 用户文件夹数量 + */ + public static int getUserFolderCount(ContentResolver resolver) { + // 查询条件:类型为文件夹 且 不在回收站中 + String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?"; + String[] selectionArgs = { + String.valueOf(Notes.TYPE_FOLDER), + String.valueOf(Notes.ID_TRASH_FOLER) + }; + + try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { "COUNT(*)" }, + selection, + selectionArgs, + null)) { + + if (cursor != null && cursor.moveToFirst()) { + return cursor.getInt(0); // 获取统计结果 + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取文件夹数量异常: " + e.getMessage()); + } + return 0; + } + + /** + * 检查指定笔记是否在可见数据库中 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @param type 笔记类型 + * @return 是否存在且可见 + */ + public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER; + try (Cursor cursor = resolver.query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, + selection, + new String[] { String.valueOf(type) }, + null)) { + return cursor != null && cursor.getCount() > 0; + } + } + + // region 存在性检查方法 + /** + * 检查笔记是否存在 + * @param resolver 内容解析器 + * @param noteId 笔记ID + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + try (Cursor cursor = resolver.query( + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null)) { + return cursor != null && cursor.getCount() > 0; + } + } + + /** + * 检查数据项是否存在 + * @param resolver 内容解析器 + * @param dataId 数据项ID + */ + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + try (Cursor cursor = resolver.query( + ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), + null, null, null, null)) { + return cursor != null && cursor.getCount() > 0; + } + } + // endregion + + /** + * 检查可见文件夹名称是否存在 + * @param resolver 内容解析器 + * @param name 文件夹名称 + */ + public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { + String selection = NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + + " AND " + NoteColumns.SNIPPET + "=?"; + try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, + selection, + new String[] { name }, null)) { + return cursor != null && cursor.getCount() > 0; + } + } + + /** + * 获取文件夹关联的小部件属性集合 + * @param resolver 内容解析器 + * @param folderId 文件夹ID + * @return 小部件属性集合 + */ + public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + HashSet set = new HashSet<>(); + String selection = NoteColumns.PARENT_ID + "=?"; + try (Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, + selection, + new String[] { String.valueOf(folderId) }, + null)) { + + if (c != null && c.moveToFirst()) { + do { + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); // 小部件ID + widget.widgetType = c.getInt(1); // 小部件类型 + set.add(widget); + } while (c.moveToNext()); + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取小部件属性异常: " + e.getMessage()); + } + return set; + } + + // region 通话记录相关方法 + /** + * 通过笔记ID获取通话号码 + * @param resolver 内容解析器 + * @param noteId 笔记ID + */ + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + String selection = CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?"; + try (Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String[] { CallNote.PHONE_NUMBER }, + selection, + new String[] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, + null)) { + + if (cursor != null && cursor.moveToFirst()) { + return cursor.getString(0); + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取通话号码异常: " + e.getMessage()); + } + return ""; + } + + /** + * 通过电话号码和通话时间获取笔记ID + * @param resolver 内容解析器 + * @param phoneNumber 电话号码 + * @param callDate 通话时间戳 + */ + public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + String selection = CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)"; + try (Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String[] { CallNote.NOTE_ID }, + selection, + new String[] { + String.valueOf(callDate), + CallNote.CONTENT_ITEM_TYPE, + phoneNumber + }, + null)) { + + if (cursor != null && cursor.moveToFirst()) { + return cursor.getLong(0); + } + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "获取通话笔记ID异常: " + e.getMessage()); + } + return 0; + } + // endregion + + /** + * 获取笔记摘要内容 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @throws IllegalArgumentException 当笔记不存在时抛出 + */ + public static String getSnippetById(ContentResolver resolver, long noteId) { + try (Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String[] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String[] { String.valueOf(noteId)}, + null)) { + + if (cursor != null && cursor.moveToFirst()) { + return cursor.getString(0); + } + } + throw new IllegalArgumentException("未找到ID为 " + noteId + " 的笔记"); + } + + /** + * 格式化摘要文本(去除换行和首尾空格) + * @param snippet 原始摘要文本 + * @return 格式化后的单行文本 + */ + public static String getFormattedSnippet(String snippet) { + if (snippet == null) return ""; + + String formatted = snippet.trim(); + int lineBreak = formatted.indexOf('\n'); + return lineBreak != -1 ? formatted.substring(0, lineBreak) : formatted; + } + } \ No newline at end of file diff --git a/src/net/micode/notes/tool/GTaskStringUtils.java b/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..e83aa73 100644 --- a/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/net/micode/notes/tool/GTaskStringUtils.java @@ -14,100 +14,243 @@ * limitations under the License. */ -package net.micode.notes.tool; - -public class GTaskStringUtils { - - 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"; - -} + package net.micode.notes.tool; + + /** + * 该类是一个工具类,主要用于处理与GTask相关的字符串常量。 + * GTask可能是某种任务管理相关的功能或模块,这些常量可能是用于定义JSON格式数据中的字段名称, + * 以便在任务管理过程中进行数据的序列化和反序列化,或者用于标识不同的任务操作类型、任务属性等。 + * 这些常量的定义有助于在代码中统一管理和使用这些字符串,避免硬编码带来的问题,提高代码的可维护性和可读性。 + */ + public class GTaskStringUtils { + + /** + * JSON数据中表示动作ID的字段名称。 + */ + public final static String GTASK_JSON_ACTION_ID = "action_id"; + + /** + * JSON数据中表示动作列表的字段名称。 + */ + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + + /** + * JSON数据中表示动作类型的字段名称。 + */ + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + + /** + * JSON数据中表示创建动作类型的值。 + */ + public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + + /** + * JSON数据中表示获取所有动作类型的值。 + */ + public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + + /** + * JSON数据中表示移动动作类型的值。 + */ + public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + + /** + * JSON数据中表示更新动作类型的值。 + */ + public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + + /** + * JSON数据中表示创建者ID的字段名称。 + */ + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + + /** + * JSON数据中表示子实体的字段名称。 + */ + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + + /** + * JSON数据中表示客户端版本的字段名称。 + */ + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + + /** + * JSON数据中表示任务是否完成的字段名称。 + */ + public final static String GTASK_JSON_COMPLETED = "completed"; + + /** + * JSON数据中表示当前列表ID的字段名称。 + */ + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + + /** + * JSON数据中表示默认列表ID的字段名称。 + */ + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + + /** + * JSON数据中表示任务是否被删除的字段名称。 + */ + public final static String GTASK_JSON_DELETED = "deleted"; + + /** + * JSON数据中表示目标列表的字段名称。 + */ + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + + /** + * JSON数据中表示目标父级的字段名称。 + */ + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + + /** + * JSON数据中表示目标父级类型的字段名称。 + */ + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + + /** + * JSON数据中表示实体变化的字段名称。 + */ + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + + /** + * JSON数据中表示实体类型的字段名称。 + */ + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + /** + * JSON数据中表示是否获取已删除任务的字段名称。 + */ + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + /** + * JSON数据中表示ID的字段名称。 + */ + public final static String GTASK_JSON_ID = "id"; + + /** + * JSON数据中表示索引的字段名称。 + */ + public final static String GTASK_JSON_INDEX = "index"; + + /** + * JSON数据中表示最后修改时间的字段名称。 + */ + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + + /** + * JSON数据中表示最新同步点的字段名称。 + */ + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + + /** + * JSON数据中表示列表ID的字段名称。 + */ + public final static String GTASK_JSON_LIST_ID = "list_id"; + + /** + * JSON数据中表示列表的字段名称。 + */ + public final static String GTASK_JSON_LISTS = "lists"; + + /** + * JSON数据中表示名称的字段名称。 + */ + public final static String GTASK_JSON_NAME = "name"; + + /** + * JSON数据中表示新ID的字段名称。 + */ + public final static String GTASK_JSON_NEW_ID = "new_id"; + + /** + * JSON数据中表示笔记的字段名称。 + */ + public final static String GTASK_JSON_NOTES = "notes"; + + /** + * JSON数据中表示父级ID的字段名称。 + */ + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + + /** + * JSON数据中表示前一个兄弟ID的字段名称。 + */ + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + + /** + * JSON数据中表示结果的字段名称。 + */ + public final static String GTASK_JSON_RESULTS = "results"; + + /** + * JSON数据中表示源列表的字段名称。 + */ + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + + /** + * JSON数据中表示任务的字段名称。 + */ + public final static String GTASK_JSON_TASKS = "tasks"; + + /** + * JSON数据中表示类型的字段名称。 + */ + public final static String GTASK_JSON_TYPE = "type"; + + /** + * JSON数据中表示组类型的值。 + */ + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + + /** + * JSON数据中表示任务类型的值。 + */ + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + + /** + * 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/src/net/micode/notes/tool/ResourceParser.java b/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..c566e9b 100644 --- a/src/net/micode/notes/tool/ResourceParser.java +++ b/src/net/micode/notes/tool/ResourceParser.java @@ -1,181 +1,168 @@ /* - * 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. + * 资源映射解析工具类,集中管理颜色、背景、字体等资源的ID映射关系 + * 提供静态方法实现资源ID与逻辑标识符之间的转换,支持多场景资源适配 */ -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 - }; - - public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; - } - - public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[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 - }; - - public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; - } - - public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; - } - - public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[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 { - 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, - }; - - public static int getWidget2xBgResource(int id) { - return BG_2X_RESOURCES[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 - }; - - 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 - }; - - public static int getTexAppearanceResource(int id) { - /** - * HACKME: Fix bug of store the resource id in shared preference. - * The id may larger than the length of resources, in this case, - * return the {@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; - } - } -} + 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 = { + 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 = { + 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 + }; + + /** + * 获取笔记内容区域背景资源 + * @param id 颜色标识符(YELLOW/BLUE等) + * @return 对应的可绘制资源ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 获取笔记标题栏背景资源 + * @param id 颜色标识符 + * @return 对应的标题栏背景资源ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认背景颜色标识 + * @param context 上下文对象 + * @return 颜色标识符:随机颜色(如果启用设置)或默认黄色 + */ + public static int getDefaultBgId(Context context) { + boolean useRandomColor = PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false); + return useRandomColor ? (int)(Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length) + : BG_DEFAULT_COLOR; + } + + /** + * 笔记列表项背景资源管理 + */ + public static class NoteItemBgResources { + // 列表项不同位置的背景资源(按颜色顺序存储) + private final static int[] BG_FIRST_RESOURCES = { /* 首项背景 */ }; + private final static int[] BG_NORMAL_RESOURCES = { /* 中间项背景 */ }; + private final static int[] BG_LAST_RESOURCES = { /* 末项背景 */ }; + private final static int[] BG_SINGLE_RESOURCES = { /* 单一项背景 */ }; + + /** + * 获取列表首项背景 + * @param id 颜色标识符 + * @return 首项背景资源ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + // 其他列表背景获取方法(实现类似)... + + /** + * 获取文件夹列表项背景 + * @return 固定文件夹图标资源ID + */ + public static int getFolderBgRes() { + return R.drawable.list_folder; + } + } + + /** + * 小部件背景资源管理 + */ + public static class WidgetBgResources { + // 2x尺寸小部件各颜色背景资源 + private final static int[] BG_2X_RESOURCES = { + R.drawable.widget_2x_yellow, + R.drawable.widget_2x_blue, + // ...其他颜色 + }; + + /** + * 获取2x小部件背景资源 + * @param id 颜色标识符 + * @return 对应的2x背景资源ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x小部件背景资源数组及获取方法(实现类似)... + } + + /** + * 文本外观资源管理 + */ + public static class TextAppearanceResources { + // 字体大小样式资源数组(小->大) + private final static int[] TEXTAPPEARANCE_RESOURCES = { + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + /** + * 获取字体样式资源 + * @param id 字体大小标识符 + * @return 对应的样式资源ID(越界时返回默认中等大小) + */ + public static int getTexAppearanceResource(int id) { + // 处理存储错误导致的越界问题 + return (id < TEXTAPPEARANCE_RESOURCES.length) ? + TEXTAPPEARANCE_RESOURCES[id] : BG_DEFAULT_FONT_SIZE; + } + + /** + * 获取可用字体样式的数量 + * @return 支持的字体等级总数 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } + } \ No newline at end of file