diff --git a/Desktop/XIAOMINotes/tool/BackupUtils.java b/Desktop/XIAOMINotes/tool/BackupUtils.java new file mode 100644 index 0000000..cf2a0b4 --- /dev/null +++ b/Desktop/XIAOMINotes/tool/BackupUtils.java @@ -0,0 +1,348 @@ +/* + * 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; + + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 以下状态表示备份或还原的状态 + */ + // 当前,SD卡未挂载 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + // 数据格式不正确,可能已被其他程序更改 + public static final int STATE_DATA_DESTROIED = 2; + // 出现运行时异常导致备份或还原失败 + public static final int STATE_SYSTEM_ERROR = 3; + // 备份或还原成功 + + public static final int STATE_SUCCESS = 4; + + private TextExport mTextExport; // 声明一个私有的TextExport对象mTextExport。 + + // 构造方法,接受一个Context参数,用于初始化mTextExport对象。 + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + // 静态方法,检查外部存储是否可用。 + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + // 公共方法,调用mTextExport对象的exportToText方法,返回int值。 + 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]; + } + + /** + * 将由文件夹ID标识的文件夹导出为文本 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于此文件夹的笔记 + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { + folderId + }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记的最后修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 查询属于此笔记的数据 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); + } + } + + /** + * 将由ID标识的笔记导出到打印流中 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { + noteId + }, null); + + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { + // 打印电话号码 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + String location = dataCursor.getString(DATA_COLUMN_CONTENT); + + if (!TextUtils.isEmpty(phoneNumber)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + phoneNumber)); + } + // 打印通话日期 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + // 打印通话附件位置 + if (!TextUtils.isEmpty(location)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + content)); + } + } + } while (dataCursor.moveToNext()); + } + dataCursor.close(); + } + // 在笔记之间打印一行分隔符 + try { + ps.write(new byte[] { + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } + + /** + * 将笔记导出为文本,以便用户阅读 + */ + public int exportToText() { + if (!externalStorageAvailable()) { + Log.d(TAG, "存储设备未挂载"); + 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)))); + // 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; + } + + /** + * *获取一个指向文件{@generateExportedTextFile}的打印流 + */ + private PrintStream getExportToTextPrintStream() { + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + Log.e(TAG, "create file to exported failed"); + return null; + } + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); + PrintStream ps = null; + try { + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + *生成用于存储导入数据的文本文件 + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + StringBuilder sb = new StringBuilder(); + sb.append(Environment.getExternalStorageDirectory()); + sb.append(context.getString(filePathResId)); + File filedir = new File(sb.toString()); + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); + File file = new File(sb.toString()); + + try { + if (!filedir.exists()) { + filedir.mkdir(); + } + if (!file.exists()) { + file.createNewFile(); + } + return file; + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } +} + + diff --git a/Desktop/XIAOMINotes/tool/DataUtils.java b/Desktop/XIAOMINotes/tool/DataUtils.java new file mode 100644 index 0000000..01148c4 --- /dev/null +++ b/Desktop/XIAOMINotes/tool/DataUtils.java @@ -0,0 +1,386 @@ +/* + * 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"; + + // 批量删除笔记的方法,接受ContentResolver对象和一组笔记的ID集合 + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查ID集合是否为null,如果为null,记录日志并返回true + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + // 检查ID集合是否为空,如果为空,记录日志并返回true + if(ids.size() == 0) { + Log.d(TAG, "no id is in the hashset"); + return true; + } + + // 创建一个ContentProviderOperation列表,用于存储删除操作 + ArrayList operationList = new ArrayList(); + + // 遍历ID集合,为每个ID创建一个删除操作 + for (long id : ids) { + // 如果ID是根文件夹ID,记录错误日志并跳过删除操作 + if(id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 创建删除操作的Builder对象 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 将构建好的删除操作添加到操作列表中 + operationList.add(builder.build()); + } + + try { + // 执行批量操作,应用删除操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查删除结果,如果结果为空、长度为0或第一个结果为null,记录日志并返回false + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 删除成功,返回true + return true; + } catch (RemoteException e) { + // 捕获RemoteException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获OperationApplicationException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 如果发生异常,返回false + return false; + } + + + // 将笔记移动到指定文件夹 + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + // 创建一个ContentValues对象,用于存储更新的值 + ContentValues values = new ContentValues(); + // 将目标文件夹ID设置为新的父文件夹ID + values.put(NoteColumns.PARENT_ID, desFolderId); + // 将原始父文件夹ID设置为源文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); + // 将LOCAL_MODIFIED标记设置为1,表示已修改 + 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) { + // 检查ID集合是否为空,如果为空,记录日志并返回true + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + + // 创建一个操作列表,用于存储更新操作 + ArrayList operationList = new ArrayList(); + // 遍历ID集合,为每个ID创建一个更新操作 + for (long id : ids) { + // 创建更新操作的Builder对象 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 设置更新的值,将目标文件夹ID设置为新的父文件夹ID,将LOCAL_MODIFIED标记设置为1 + builder.withValue(NoteColumns.PARENT_ID, folderId); + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + // 将更新操作添加到操作列表中 + operationList.add(builder.build()); + } + + try { + // 应用批量更新操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查更新结果,如果结果为空、长度为0或第一个结果为null,记录日志并返回false + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 更新成功,返回true + return true; + } catch (RemoteException e) { + // 捕获RemoteException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获OperationApplicationException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 更新失败,返回false + 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) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + 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()) { + // 如果查询结果不为空,创建一个HashSet对象,用于存储小部件信息 + set = new HashSet(); + do { + try { + // 遍历查询结果,为每个笔记创建一个小部件属性对象,并将其添加到HashSet中 + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); + widget.widgetType = c.getInt(1); + set.add(widget); + } catch (IndexOutOfBoundsException e) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + // 关闭游标 + c.close(); + } + // 返回文件夹中所有笔记的小部件信息集合 + return set; + } + + // 根据笔记ID获取通话记录号码 + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询指定笔记ID的通话记录号码 + 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) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, "Get call number fails " + e.toString()); + } finally { + // 关闭游标 + cursor.close(); + } + } + // 如果查询结果为空或发生异常,返回空字符串 + return ""; + } + + + // 根据通话日期和电话号码查询通话记录的笔记ID + public static long getCallNoteIdByCallDateAndPhoneNumber(ContentResolver resolver, long callDate, String phoneNumber) { + // 查询通话日期和电话号码匹配的通话记录的笔记ID + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.NOTE_ID }, + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, + null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + try { + // 如果查询结果不为空,获取通话记录的笔记ID并返回 + return cursor.getLong(0); + } catch (IndexOutOfBoundsException e) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + // 关闭游标 + cursor.close(); + } + // 如果查询结果为空或发生异常,返回0 + return 0; + } + + // 根据笔记ID查询摘要 + public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询指定笔记ID的摘要信息 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String [] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String [] { String.valueOf(noteId)}, + null); + + if (cursor != null) { + String snippet = ""; + if (cursor.moveToFirst()) { + // 如果查询结果不为空,获取摘要信息并返回 + snippet = cursor.getString(0); + } + // 关闭游标 + cursor.close(); + return snippet; + } + // 如果查询结果为空,抛出IllegalArgumentException异常 + 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; + } + +} diff --git a/Desktop/XIAOMINotes/tool/GTaskStringUtils.java b/Desktop/XIAOMINotes/tool/GTaskStringUtils.java new file mode 100644 index 0000000..c0d3a33 --- /dev/null +++ b/Desktop/XIAOMINotes/tool/GTaskStringUtils.java @@ -0,0 +1,160 @@ +/* + * 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; + +public class GTaskStringUtils { + + // GTASK JSON 中的操作 ID + public final static String GTASK_JSON_ACTION_ID = "action_id"; + + // GTASK JSON 中的操作列表 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + + // GTASK JSON 中的操作类型 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + + // 创建操作类型 + 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"; + + // GTASK JSON 中的创建者 ID + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + + // GTASK JSON 中的子实体 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + + // GTASK JSON 中的客户端版本 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + + // GTASK JSON 中的完成状态 + public final static String GTASK_JSON_COMPLETED = "completed"; + + // GTASK JSON 中的当前列表 ID + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + + // GTASK JSON 中的默认列表 ID + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + + // GTASK JSON 中的已删除标志 + public final static String GTASK_JSON_DELETED = "deleted"; + + // GTASK JSON 中的目标列表 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + + // GTASK JSON 中的目标父实体 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + + // GTASK JSON 中的目标父实体类型 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + + // GTASK JSON 中的实体变更 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + + // GTASK JSON 中的实体类型 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + // GTASK JSON 中的已删除获取标志 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + // GTASK JSON 中的 ID + public final static String GTASK_JSON_ID = "id"; + + // GTASK JSON 中的索引 + public final static String GTASK_JSON_INDEX = "index"; + + // GTASK JSON 中的最后修改时间 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + + // GTASK JSON 中的最新同步点 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + + // GTASK JSON 中的列表 ID + public final static String GTASK_JSON_LIST_ID = "list_id"; + + // GTASK JSON 中的列表 + public final static String GTASK_JSON_LISTS = "lists"; + + // GTASK JSON 中的名称 + public final static String GTASK_JSON_NAME = "name"; + + // GTASK JSON 中的新 ID + public final static String GTASK_JSON_NEW_ID = "new_id"; + + // GTASK JSON 中的笔记 + public final static String GTASK_JSON_NOTES = "notes"; + + // GTASK JSON 中的父 ID + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + + // GTASK JSON 中的上一个兄弟 ID + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + + // GTASK JSON 中的结果 + public final static String GTASK_JSON_RESULTS = "results"; + + // GTASK JSON 中的源列表 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + + // GTASK JSON 中的任务 + public final static String GTASK_JSON_TASKS = "tasks"; + + // GTASK JSON 中的类型 + public final static String GTASK_JSON_TYPE = "type"; + + // GTASK JSON 中的组类型 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + + // GTASK JSON 中的任务类型 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + + // GTASK JSON 中的用户 + public final static String GTASK_JSON_USER = "user"; + + // MIUI 文件夹前缀 + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + + // 默认文件夹 + public final static String FOLDER_DEFAULT = "Default"; + + // 通话记录文件夹 + public final static String FOLDER_CALL_NOTE = "Call_Note"; + + // 元数据文件夹 + public final static String FOLDER_META = "METADATA"; + + // 元数据头部 GTask ID + public final static String META_HEAD_GTASK_ID = "meta_gid"; + + // 元数据头部笔记 + public final static String META_HEAD_NOTE = "meta_note"; + + // 元数据头部数据 + public final static String META_HEAD_DATA = "meta_data"; + + // 元数据笔记名称 + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; + + +} diff --git a/Desktop/XIAOMINotes/tool/ResourceParser.java b/Desktop/XIAOMINotes/tool/ResourceParser.java new file mode 100644 index 0000000..21c8c9f --- /dev/null +++ b/Desktop/XIAOMINotes/tool/ResourceParser.java @@ -0,0 +1,214 @@ +/* + * 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; + +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]; + } + } + + // 获取默认背景颜色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 { + // 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小部件背景资源 + 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小部件背景资源 + 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: 修复存储资源ID在共享首选项中的bug。 + * ID可能大于资源的长度,在这种情况下,返回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; + } + } + +} diff --git a/Desktop/tool/BackupUtils.java b/Desktop/tool/BackupUtils.java new file mode 100644 index 0000000..cf2a0b4 --- /dev/null +++ b/Desktop/tool/BackupUtils.java @@ -0,0 +1,348 @@ +/* + * 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; + + public static synchronized BackupUtils getInstance(Context context) { + if (sInstance == null) { + sInstance = new BackupUtils(context); + } + return sInstance; + } + + /** + * 以下状态表示备份或还原的状态 + */ + // 当前,SD卡未挂载 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; + // 数据格式不正确,可能已被其他程序更改 + public static final int STATE_DATA_DESTROIED = 2; + // 出现运行时异常导致备份或还原失败 + public static final int STATE_SYSTEM_ERROR = 3; + // 备份或还原成功 + + public static final int STATE_SUCCESS = 4; + + private TextExport mTextExport; // 声明一个私有的TextExport对象mTextExport。 + + // 构造方法,接受一个Context参数,用于初始化mTextExport对象。 + private BackupUtils(Context context) { + mTextExport = new TextExport(context); + } + + // 静态方法,检查外部存储是否可用。 + private static boolean externalStorageAvailable() { + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + } + + // 公共方法,调用mTextExport对象的exportToText方法,返回int值。 + 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]; + } + + /** + * 将由文件夹ID标识的文件夹导出为文本 + */ + private void exportFolderToText(String folderId, PrintStream ps) { + // 查询属于此文件夹的笔记 + Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { + folderId + }, null); + + if (notesCursor != null) { + if (notesCursor.moveToFirst()) { + do { + // 打印笔记的最后修改日期 + ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); + // 查询属于此笔记的数据 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); + exportNoteToText(noteId, ps); + } while (notesCursor.moveToNext()); + } + notesCursor.close(); + } + } + + /** + * 将由ID标识的笔记导出到打印流中 + */ + private void exportNoteToText(String noteId, PrintStream ps) { + Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, + DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { + noteId + }, null); + + if (dataCursor != null) { + if (dataCursor.moveToFirst()) { + do { + String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + if (DataConstants.CALL_NOTE.equals(mimeType)) { + // 打印电话号码 + String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); + long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); + String location = dataCursor.getString(DATA_COLUMN_CONTENT); + + if (!TextUtils.isEmpty(phoneNumber)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + phoneNumber)); + } + // 打印通话日期 + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat + .format(mContext.getString(R.string.format_datetime_mdhm), + callDate))); + // 打印通话附件位置 + if (!TextUtils.isEmpty(location)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + location)); + } + } else if (DataConstants.NOTE.equals(mimeType)) { + String content = dataCursor.getString(DATA_COLUMN_CONTENT); + if (!TextUtils.isEmpty(content)) { + ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + content)); + } + } + } while (dataCursor.moveToNext()); + } + dataCursor.close(); + } + // 在笔记之间打印一行分隔符 + try { + ps.write(new byte[] { + Character.LINE_SEPARATOR, Character.LETTER_NUMBER + }); + } catch (IOException e) { + Log.e(TAG, e.toString()); + } + } + + /** + * 将笔记导出为文本,以便用户阅读 + */ + public int exportToText() { + if (!externalStorageAvailable()) { + Log.d(TAG, "存储设备未挂载"); + 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)))); + // 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; + } + + /** + * *获取一个指向文件{@generateExportedTextFile}的打印流 + */ + private PrintStream getExportToTextPrintStream() { + File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + R.string.file_name_txt_format); + if (file == null) { + Log.e(TAG, "create file to exported failed"); + return null; + } + mFileName = file.getName(); + mFileDirectory = mContext.getString(R.string.file_path); + PrintStream ps = null; + try { + FileOutputStream fos = new FileOutputStream(file); + ps = new PrintStream(fos); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (NullPointerException e) { + e.printStackTrace(); + return null; + } + return ps; + } + } + + /** + *生成用于存储导入数据的文本文件 + */ + private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + StringBuilder sb = new StringBuilder(); + sb.append(Environment.getExternalStorageDirectory()); + sb.append(context.getString(filePathResId)); + File filedir = new File(sb.toString()); + sb.append(context.getString( + fileNameFormatResId, + DateFormat.format(context.getString(R.string.format_date_ymd), + System.currentTimeMillis()))); + File file = new File(sb.toString()); + + try { + if (!filedir.exists()) { + filedir.mkdir(); + } + if (!file.exists()) { + file.createNewFile(); + } + return file; + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } +} + + diff --git a/Desktop/tool/DataUtils.java b/Desktop/tool/DataUtils.java new file mode 100644 index 0000000..01148c4 --- /dev/null +++ b/Desktop/tool/DataUtils.java @@ -0,0 +1,386 @@ +/* + * 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"; + + // 批量删除笔记的方法,接受ContentResolver对象和一组笔记的ID集合 + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查ID集合是否为null,如果为null,记录日志并返回true + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + // 检查ID集合是否为空,如果为空,记录日志并返回true + if(ids.size() == 0) { + Log.d(TAG, "no id is in the hashset"); + return true; + } + + // 创建一个ContentProviderOperation列表,用于存储删除操作 + ArrayList operationList = new ArrayList(); + + // 遍历ID集合,为每个ID创建一个删除操作 + for (long id : ids) { + // 如果ID是根文件夹ID,记录错误日志并跳过删除操作 + if(id == Notes.ID_ROOT_FOLDER) { + Log.e(TAG, "Don't delete system folder root"); + continue; + } + // 创建删除操作的Builder对象 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 将构建好的删除操作添加到操作列表中 + operationList.add(builder.build()); + } + + try { + // 执行批量操作,应用删除操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查删除结果,如果结果为空、长度为0或第一个结果为null,记录日志并返回false + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 删除成功,返回true + return true; + } catch (RemoteException e) { + // 捕获RemoteException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获OperationApplicationException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 如果发生异常,返回false + return false; + } + + + // 将笔记移动到指定文件夹 + public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { + // 创建一个ContentValues对象,用于存储更新的值 + ContentValues values = new ContentValues(); + // 将目标文件夹ID设置为新的父文件夹ID + values.put(NoteColumns.PARENT_ID, desFolderId); + // 将原始父文件夹ID设置为源文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); + // 将LOCAL_MODIFIED标记设置为1,表示已修改 + 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) { + // 检查ID集合是否为空,如果为空,记录日志并返回true + if (ids == null) { + Log.d(TAG, "the ids is null"); + return true; + } + + // 创建一个操作列表,用于存储更新操作 + ArrayList operationList = new ArrayList(); + // 遍历ID集合,为每个ID创建一个更新操作 + for (long id : ids) { + // 创建更新操作的Builder对象 + ContentProviderOperation.Builder builder = ContentProviderOperation + .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 设置更新的值,将目标文件夹ID设置为新的父文件夹ID,将LOCAL_MODIFIED标记设置为1 + builder.withValue(NoteColumns.PARENT_ID, folderId); + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + // 将更新操作添加到操作列表中 + operationList.add(builder.build()); + } + + try { + // 应用批量更新操作 + ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查更新结果,如果结果为空、长度为0或第一个结果为null,记录日志并返回false + if (results == null || results.length == 0 || results[0] == null) { + Log.d(TAG, "delete notes failed, ids:" + ids.toString()); + return false; + } + // 更新成功,返回true + return true; + } catch (RemoteException e) { + // 捕获RemoteException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } catch (OperationApplicationException e) { + // 捕获OperationApplicationException,记录错误日志 + Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); + } + // 更新失败,返回false + 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) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + 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()) { + // 如果查询结果不为空,创建一个HashSet对象,用于存储小部件信息 + set = new HashSet(); + do { + try { + // 遍历查询结果,为每个笔记创建一个小部件属性对象,并将其添加到HashSet中 + AppWidgetAttribute widget = new AppWidgetAttribute(); + widget.widgetId = c.getInt(0); + widget.widgetType = c.getInt(1); + set.add(widget); + } catch (IndexOutOfBoundsException e) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, e.toString()); + } + } while (c.moveToNext()); + } + // 关闭游标 + c.close(); + } + // 返回文件夹中所有笔记的小部件信息集合 + return set; + } + + // 根据笔记ID获取通话记录号码 + public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询指定笔记ID的通话记录号码 + 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) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, "Get call number fails " + e.toString()); + } finally { + // 关闭游标 + cursor.close(); + } + } + // 如果查询结果为空或发生异常,返回空字符串 + return ""; + } + + + // 根据通话日期和电话号码查询通话记录的笔记ID + public static long getCallNoteIdByCallDateAndPhoneNumber(ContentResolver resolver, long callDate, String phoneNumber) { + // 查询通话日期和电话号码匹配的通话记录的笔记ID + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, + new String [] { CallNote.NOTE_ID }, + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" + + CallNote.PHONE_NUMBER + ",?)", + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, + null); + + if (cursor != null) { + if (cursor.moveToFirst()) { + try { + // 如果查询结果不为空,获取通话记录的笔记ID并返回 + return cursor.getLong(0); + } catch (IndexOutOfBoundsException e) { + // 捕获IndexOutOfBoundsException异常,记录错误日志 + Log.e(TAG, "Get call note id fails " + e.toString()); + } + } + // 关闭游标 + cursor.close(); + } + // 如果查询结果为空或发生异常,返回0 + return 0; + } + + // 根据笔记ID查询摘要 + public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询指定笔记ID的摘要信息 + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, + new String [] { NoteColumns.SNIPPET }, + NoteColumns.ID + "=?", + new String [] { String.valueOf(noteId)}, + null); + + if (cursor != null) { + String snippet = ""; + if (cursor.moveToFirst()) { + // 如果查询结果不为空,获取摘要信息并返回 + snippet = cursor.getString(0); + } + // 关闭游标 + cursor.close(); + return snippet; + } + // 如果查询结果为空,抛出IllegalArgumentException异常 + 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; + } + +} diff --git a/Desktop/tool/GTaskStringUtils.java b/Desktop/tool/GTaskStringUtils.java new file mode 100644 index 0000000..c0d3a33 --- /dev/null +++ b/Desktop/tool/GTaskStringUtils.java @@ -0,0 +1,160 @@ +/* + * 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; + +public class GTaskStringUtils { + + // GTASK JSON 中的操作 ID + public final static String GTASK_JSON_ACTION_ID = "action_id"; + + // GTASK JSON 中的操作列表 + public final static String GTASK_JSON_ACTION_LIST = "action_list"; + + // GTASK JSON 中的操作类型 + public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + + // 创建操作类型 + 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"; + + // GTASK JSON 中的创建者 ID + public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + + // GTASK JSON 中的子实体 + public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + + // GTASK JSON 中的客户端版本 + public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + + // GTASK JSON 中的完成状态 + public final static String GTASK_JSON_COMPLETED = "completed"; + + // GTASK JSON 中的当前列表 ID + public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + + // GTASK JSON 中的默认列表 ID + public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + + // GTASK JSON 中的已删除标志 + public final static String GTASK_JSON_DELETED = "deleted"; + + // GTASK JSON 中的目标列表 + public final static String GTASK_JSON_DEST_LIST = "dest_list"; + + // GTASK JSON 中的目标父实体 + public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + + // GTASK JSON 中的目标父实体类型 + public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + + // GTASK JSON 中的实体变更 + public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + + // GTASK JSON 中的实体类型 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + + // GTASK JSON 中的已删除获取标志 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + + // GTASK JSON 中的 ID + public final static String GTASK_JSON_ID = "id"; + + // GTASK JSON 中的索引 + public final static String GTASK_JSON_INDEX = "index"; + + // GTASK JSON 中的最后修改时间 + public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + + // GTASK JSON 中的最新同步点 + public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + + // GTASK JSON 中的列表 ID + public final static String GTASK_JSON_LIST_ID = "list_id"; + + // GTASK JSON 中的列表 + public final static String GTASK_JSON_LISTS = "lists"; + + // GTASK JSON 中的名称 + public final static String GTASK_JSON_NAME = "name"; + + // GTASK JSON 中的新 ID + public final static String GTASK_JSON_NEW_ID = "new_id"; + + // GTASK JSON 中的笔记 + public final static String GTASK_JSON_NOTES = "notes"; + + // GTASK JSON 中的父 ID + public final static String GTASK_JSON_PARENT_ID = "parent_id"; + + // GTASK JSON 中的上一个兄弟 ID + public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + + // GTASK JSON 中的结果 + public final static String GTASK_JSON_RESULTS = "results"; + + // GTASK JSON 中的源列表 + public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + + // GTASK JSON 中的任务 + public final static String GTASK_JSON_TASKS = "tasks"; + + // GTASK JSON 中的类型 + public final static String GTASK_JSON_TYPE = "type"; + + // GTASK JSON 中的组类型 + public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + + // GTASK JSON 中的任务类型 + public final static String GTASK_JSON_TYPE_TASK = "TASK"; + + // GTASK JSON 中的用户 + public final static String GTASK_JSON_USER = "user"; + + // MIUI 文件夹前缀 + public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + + // 默认文件夹 + public final static String FOLDER_DEFAULT = "Default"; + + // 通话记录文件夹 + public final static String FOLDER_CALL_NOTE = "Call_Note"; + + // 元数据文件夹 + public final static String FOLDER_META = "METADATA"; + + // 元数据头部 GTask ID + public final static String META_HEAD_GTASK_ID = "meta_gid"; + + // 元数据头部笔记 + public final static String META_HEAD_NOTE = "meta_note"; + + // 元数据头部数据 + public final static String META_HEAD_DATA = "meta_data"; + + // 元数据笔记名称 + public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; + + +} diff --git a/Desktop/tool/ResourceParser.java b/Desktop/tool/ResourceParser.java new file mode 100644 index 0000000..21c8c9f --- /dev/null +++ b/Desktop/tool/ResourceParser.java @@ -0,0 +1,214 @@ +/* + * 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; + +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]; + } + } + + // 获取默认背景颜色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 { + // 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小部件背景资源 + 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小部件背景资源 + 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: 修复存储资源ID在共享首选项中的bug。 + * ID可能大于资源的长度,在这种情况下,返回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; + } + } + +}