diff --git a/src/崔玉涛/BackupUtils.java.txt b/src/崔玉涛/BackupUtils.java.txt new file mode 100644 index 0000000..f30300d --- /dev/null +++ b/src/崔玉涛/BackupUtils.java.txt @@ -0,0 +1,424 @@ +/* + * 版权声明,代码遵循 Apache License 2.0 开源协议 + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.tool; + +import android.content.Context; +import android.database.Cursor; +import android.os.Environment; +import android.text.TextUtils; +import android.text.format.DateFormat; +import android.util.Log; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.DataColumns; +import net.micode.notes.data.Notes.DataConstants; +import net.micode.notes.data.Notes.NoteColumns; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +/** + * 备份工具类,用于将笔记数据导出为文本文件 + */ +public class BackupUtils { + private static final String TAG = "BackupUtils"; + + // 单例模式实现 + private static BackupUtils sInstance; + + /** + * 获取BackupUtils单例 + * @param context 上下文对象 + * @return 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; + + /** + * 私有构造函数 + * @param context 上下文对象 + */ + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + /** + * 检查外部存储是否可用 + * @return 外部存储是否可用 + */ + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + /** + * 导出笔记为文本文件 + * @return 导出状态 + */ + public int exportToText() { + return mTextExport.exportToText(); + } + + /** + * 获取导出的文本文件名 + * @return 文件名 + */ + public String getExportedTextFileName() { + return mTextExport.mFileName; + } + + /** + * 获取导出的文本文件目录 + * @return 文件目录 + */ + 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; + + /** + * 构造函数 + * @param context 上下文对象 + */ + public TextExport(Context context) { + // 从资源文件中获取文本格式 + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); + mContext = context; + mFileName = ""; + mFileDirectory = ""; + } + + /** + * 获取指定格式 + * @param id 格式ID + * @return 格式字符串 + */ + private String getFormat(int id) { + return TEXT_FORMAT[id]; + } + + /** + * 导出文件夹内容到文本 + * @param folderId 文件夹ID + * @param ps 打印流 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于该文件夹的笔记 + Cursor notesCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.PARENT_ID + "=?", + 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.toString()); + } + } + + /** + * 将笔记导出为可读文本 + * @return 导出状态 + */ + 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; + } + + /** + * 获取导出文本的打印流 + * @return 打印流 + */ + 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卡上生成文件 + * @param context 上下文对象 + * @param filePathResId 文件路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的文件对象 + */ + 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/src/崔玉涛/DataUtils.java.txt b/src/崔玉涛/DataUtils.java.txt new file mode 100644 index 0000000..93a8811 --- /dev/null +++ b/src/崔玉涛/DataUtils.java.txt @@ -0,0 +1,401 @@ +/* + * 版权声明,代码遵循 Apache License 2.0 开源协议 + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.tool; + +import android.content.ContentProviderOperation; +import android.content.ContentProviderResult; +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.OperationApplicationException; +import android.database.Cursor; +import android.os.RemoteException; +import android.util.Log; + +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.CallNote; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; + +import java.util.ArrayList; +import java.util.HashSet; + +/** + * 数据工具类,提供对便签数据库的各种操作 + */ +public class DataUtils { + // 日志标签 + public static final String TAG = "DataUtils"; + + /** + * 批量删除便签 + * @param resolver ContentResolver对象 + * @param ids 要删除的便签ID集合 + * @return 删除是否成功 + */ + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查ID集合是否为空 + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + // 检查ID集合是否为空集合 + if (ids.size() == 0) { + Log.d(TAG, "no id is in the hashset"); + return true; + } + + // 创建批量操作列表 + 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); // 设置新的父文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 记录原始父文件夹ID + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + // 执行更新 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + } + + /** + * 批量移动便签到指定文件夹 + * @param resolver ContentResolver对象 + * @param ids 要移动的便签ID集合 + * @param folderId 目标文件夹ID + * @return 移动是否成功 + */ + public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, + long folderId) { + // 检查ID集合是否为空 + 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); // 设置新的父文件夹ID + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 + operationList.add(builder.build()); + } + + try { + // 执行批量操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查操作结果 + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + 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 是否可见 + */ + 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 是否存在 + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 查询指定ID的便签 + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), + null, null, null, null); + + boolean exist = false; + if (cursor != null) { + if (cursor.getCount() > 0) { + exist = true; + } + cursor.close(); + } + return exist; + } + + /** + * 检查数据项是否存在于数据库中 + * @param resolver ContentResolver对象 + * @param dataId 数据项ID + * @return 是否存在 + */ + public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询指定ID的数据项 + 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 是否存在 + */ + 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 小部件属性集合 + */ + 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); // 小部件ID + 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 + */ + 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); // 返回便签ID + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + cursor.close(); + } + return 0; // 默认返回0 + } + + /** + * 通过便签ID获取内容摘要 + * @param resolver ContentResolver对象 + * @param noteId 便签ID + * @return 内容摘要 + * @throws 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/src/崔玉涛/GTaskStringUtils.java.txt b/src/崔玉涛/GTaskStringUtils.java.txt new file mode 100644 index 0000000..99cd1fc --- /dev/null +++ b/src/崔玉涛/GTaskStringUtils.java.txt @@ -0,0 +1,169 @@ +/* + * 版权声明,代码遵循 Apache License 2.0 开源协议 + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.tool; + +/** + * Google Tasks 相关字符串常量工具类 + * 包含与Google Tasks API交互时使用的JSON字段名和常量值 + */ +public class GTaskStringUtils { + + // ================ JSON字段名常量 ================ + + /** 操作ID字段 */ + 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"; + + /** 创建者ID字段 */ + 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"; + + /** 当前列表ID字段 */ + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + + /** 默认列表ID字段 */ + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + + /** 删除状态字段 */ + 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"; + + /** ID字段 */ + 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"; + + /** 列表ID字段 */ + 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"; + + /** 新ID字段 */ + public final static String GTASK_JSON_NEW_ID = "new_id"; + + /** 笔记字段 */ + public final static String GTASK_JSON_NOTES = "notes"; + + /** 父ID字段 */ + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + + /** 前一个兄弟节点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"; + + // ================ 文件夹相关常量 ================ + + /** 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/崔玉涛/ResourceParser.java.txt b/src/崔玉涛/ResourceParser.java.txt new file mode 100644 index 0000000..e71ed64 --- /dev/null +++ b/src/崔玉涛/ResourceParser.java.txt @@ -0,0 +1,573 @@ +/* + * 版权声明,代码遵循 Apache License 2.0 开源协议 + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.tool; + +import android.content.Context; +import android.preference.PreferenceManager; + +import net.micode.notes.R; +import net.micode.notes.ui.NotesPreferenceActivity; + +/** + * 资源解析工具类 + * 用于管理和获取应用中的各种资源ID,包括背景、文字样式等 + */ +public class ResourceParser { + + // ================ 颜色常量 ================ + + /** 黄色背景标识 */ + public static final int YELLOW = 0; + + /** 蓝色背景标识 */ + public static final int BLUE = 1; + + /** 白色背景标识 */ + public static final int WHITE = 2; + + /** 绿色背景标识 */ + public static final int GREEN = 3; + + /** 红色背景标识 */ + public static final int RED = 4; + + /** 默认背景颜色(黄色) */ + public static final int BG_DEFAULT_COLOR = YELLOW; + + // ================ 字体大小常量 ================ + + /** 小号字体 */ + public static final int TEXT_SMALL = 0; + + /** 中号字体 */ + public static final int TEXT_MEDIUM = 1; + + /** 大号字体 */ + public static final int TEXT_LARGE = 2; + + /** 超大号字体 */ + public static final int TEXT_SUPER = 3; + + /** 默认字体大小(中号) */ + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + + /** + * 笔记背景资源类 + */ + public static class NoteBgResources { + // 编辑界面背景资源数组 + private final static int[] BG_EDIT_RESOURCES = new int[]{ + R.drawable.edit_yellow, + R.drawable.edit_blue, + R.drawable.edit_white, + R.drawable.edit_green, + R.drawable.edit_red + }; + + // 编辑界面标题背景资源数组 + private final static int[] BG_EDIT_TITLE_RESOURCES = new int[]{ + R.drawable.edit_title_yellow, + R.drawable.edit_title_blue, + R.drawable.edit_title_white, + R.drawable.edit_title_green, + R.drawable.edit_title_red + }; + + /** + * 获取笔记背景资源ID + * @param id 颜色标识(YELLOW, BLUE等) + * @return 对应的背景资源ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 获取笔记标题背景资源ID + * @param id 颜色标识(YELLOW, BLUE等) + * @return 对应的标题背景资源ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认背景ID + * @param context 上下文对象 + * @return 背景ID(如果设置了随机背景则返回随机ID,否则返回默认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 + }; + + /** + * 获取列表第一项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + /** + * 获取列表最后一项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + /** + * 获取列表单项背景资源(当列表只有一项时) + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + /** + * 获取列表中间项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_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 = 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, + }; + + /** + * 获取2x小部件背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x小部件背景资源数组 + private final static int[] BG_4X_RESOURCES = new int[]{ + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + /** + * 获取4x小部件背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + /** + * 文字外观资源类 + */ + public static class TextAppearanceResources { + // 文字外观资源数组 + private final static int[] TEXTAPPEARANCE_RESOURCES = new int[]{ + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + /** + * 获取文字外观资源 + * @param id 字体大小标识 + * @return 对应的文字外观资源ID + * + * 注意:修复了在SharedPreference中存储资源ID时的bug。 + * 如果ID大于资源数组长度,则返回默认字体大小 + */ + public static int getTexAppearanceResource(int id) { + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + /** + * 获取文字外观资源数量 + * @return 可用的文字外观资源数量 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +}/* + * 版权声明,代码遵循 Apache License 2.0 开源协议 + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.micode.notes.tool; + +import android.content.Context; +import android.preference.PreferenceManager; + +import net.micode.notes.R; +import net.micode.notes.ui.NotesPreferenceActivity; + +/** + * 资源解析工具类 + * 用于管理和获取应用中的各种资源ID,包括背景、文字样式等 + */ +public class ResourceParser { + + // ================ 颜色常量 ================ + + /** 黄色背景标识 */ + public static final int YELLOW = 0; + + /** 蓝色背景标识 */ + public static final int BLUE = 1; + + /** 白色背景标识 */ + public static final int WHITE = 2; + + /** 绿色背景标识 */ + public static final int GREEN = 3; + + /** 红色背景标识 */ + public static final int RED = 4; + + /** 默认背景颜色(黄色) */ + public static final int BG_DEFAULT_COLOR = YELLOW; + + // ================ 字体大小常量 ================ + + /** 小号字体 */ + public static final int TEXT_SMALL = 0; + + /** 中号字体 */ + public static final int TEXT_MEDIUM = 1; + + /** 大号字体 */ + public static final int TEXT_LARGE = 2; + + /** 超大号字体 */ + public static final int TEXT_SUPER = 3; + + /** 默认字体大小(中号) */ + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + + /** + * 笔记背景资源类 + */ + public static class NoteBgResources { + // 编辑界面背景资源数组 + private final static int[] BG_EDIT_RESOURCES = new int[]{ + R.drawable.edit_yellow, + R.drawable.edit_blue, + R.drawable.edit_white, + R.drawable.edit_green, + R.drawable.edit_red + }; + + // 编辑界面标题背景资源数组 + private final static int[] BG_EDIT_TITLE_RESOURCES = new int[]{ + R.drawable.edit_title_yellow, + R.drawable.edit_title_blue, + R.drawable.edit_title_white, + R.drawable.edit_title_green, + R.drawable.edit_title_red + }; + + /** + * 获取笔记背景资源ID + * @param id 颜色标识(YELLOW, BLUE等) + * @return 对应的背景资源ID + */ + public static int getNoteBgResource(int id) { + return BG_EDIT_RESOURCES[id]; + } + + /** + * 获取笔记标题背景资源ID + * @param id 颜色标识(YELLOW, BLUE等) + * @return 对应的标题背景资源ID + */ + public static int getNoteTitleBgResource(int id) { + return BG_EDIT_TITLE_RESOURCES[id]; + } + } + + /** + * 获取默认背景ID + * @param context 上下文对象 + * @return 背景ID(如果设置了随机背景则返回随机ID,否则返回默认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 + }; + + /** + * 获取列表第一项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgFirstRes(int id) { + return BG_FIRST_RESOURCES[id]; + } + + /** + * 获取列表最后一项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgLastRes(int id) { + return BG_LAST_RESOURCES[id]; + } + + /** + * 获取列表单项背景资源(当列表只有一项时) + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgSingleRes(int id) { + return BG_SINGLE_RESOURCES[id]; + } + + /** + * 获取列表中间项背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getNoteBgNormalRes(int id) { + return BG_NORMAL_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 = 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, + }; + + /** + * 获取2x小部件背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getWidget2xBgResource(int id) { + return BG_2X_RESOURCES[id]; + } + + // 4x小部件背景资源数组 + private final static int[] BG_4X_RESOURCES = new int[]{ + R.drawable.widget_4x_yellow, + R.drawable.widget_4x_blue, + R.drawable.widget_4x_white, + R.drawable.widget_4x_green, + R.drawable.widget_4x_red + }; + + /** + * 获取4x小部件背景资源 + * @param id 颜色标识 + * @return 对应的背景资源ID + */ + public static int getWidget4xBgResource(int id) { + return BG_4X_RESOURCES[id]; + } + } + + /** + * 文字外观资源类 + */ + public static class TextAppearanceResources { + // 文字外观资源数组 + private final static int[] TEXTAPPEARANCE_RESOURCES = new int[]{ + R.style.TextAppearanceNormal, + R.style.TextAppearanceMedium, + R.style.TextAppearanceLarge, + R.style.TextAppearanceSuper + }; + + /** + * 获取文字外观资源 + * @param id 字体大小标识 + * @return 对应的文字外观资源ID + * + * 注意:修复了在SharedPreference中存储资源ID时的bug。 + * 如果ID大于资源数组长度,则返回默认字体大小 + */ + public static int getTexAppearanceResource(int id) { + if (id >= TEXTAPPEARANCE_RESOURCES.length) { + return BG_DEFAULT_FONT_SIZE; + } + return TEXTAPPEARANCE_RESOURCES[id]; + } + + /** + * 获取文字外观资源数量 + * @return 可用的文字外观资源数量 + */ + public static int getResourcesSize() { + return TEXTAPPEARANCE_RESOURCES.length; + } + } +} \ No newline at end of file