dingshuoran
dingshuoran 8 months ago
parent c7e628c611
commit 2a48905e2d

@ -37,68 +37,78 @@ import java.io.PrintStream;
public class BackupUtils {
// 用于日志记录的标记,方便在日志中识别该类的输出
private static final String TAG = "BackupUtils";
// Singleton stuff
// 单例模式使用的静态变量,用于存储该类的唯一实例
private static BackupUtils sInstance;
// 单例模式的获取实例方法,确保在整个应用程序中只有一个 BackupUtils 实例
public static synchronized BackupUtils getInstance(Context context) {
// 如果 sInstance 为空,创建一个新的 BackupUtils 实例并存储在 sInstance 中
if (sInstance == null) {
sInstance = new BackupUtils(context);
}
// 返回 BackupUtils 的唯一实例
return sInstance;
}
/**
* Following states are signs to represents backup or restore
* status
*
*/
// Currently, the sdcard is not mounted
public static final int STATE_SD_CARD_UNMOUONTED = 0;
// The backup file not exist
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
// The data is not well formated, may be changed by other programs
public static final int STATE_DATA_DESTROIED = 2;
// Some run-time exception which causes restore or backup fails
public static final int STATE_SYSTEM_ERROR = 3;
// Backup or restore success
public static final int STATE_SUCCESS = 4;
// 表示 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;
// 用于文本导出的 TextExport 对象
private TextExport mTextExport;
// 构造函数,接收 Context 作为参数,并创建一个 TextExport 对象
private BackupUtils(Context context) {
mTextExport = new TextExport(context);
}
// 检查外部存储是否可用,通过检查存储状态是否为已挂载
private static boolean externalStorageAvailable() {
// 比较存储状态是否等于已挂载状态
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
// 调用 TextExport 对象的 exportToText 方法,进行文本导出操作
public int exportToText() {
return mTextExport.exportToText();
}
// 获取导出的文本文件名,调用 TextExport 对象的相应属性
public String getExportedTextFileName() {
return mTextExport.mFileName;
}
// 获取导出的文本文件所在的目录,调用 TextExport 对象的相应属性
public String getExportedTextFileDir() {
return mTextExport.mFileDirectory;
}
// 内部类 TextExport负责具体的文本导出操作
private static class TextExport {
// 用于查询笔记的投影,指定了从数据库中获取笔记信息时需要的列
private static final String[] NOTE_PROJECTION = {
NoteColumns.ID,
NoteColumns.MODIFIED_DATE,
NoteColumns.SNIPPET,
NoteColumns.TYPE
};
// 笔记投影中各列的索引,用于从 Cursor 中准确提取相应信息
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,
@ -107,105 +117,116 @@ public class BackupUtils {
DataColumns.DATA3,
DataColumns.DATA4,
};
// 数据投影中各列的索引,用于从 Cursor 中准确提取相应信息
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 final String[] TEXT_FORMAT;
// 表示文件夹名称的格式索引
private static final int FORMAT_FOLDER_NAME = 0;
// 表示笔记日期的格式索引
private static final int FORMAT_NOTE_DATE = 1;
// 表示笔记内容的格式索引
private static final int FORMAT_NOTE_CONTENT = 2;
// 上下文对象,用于资源访问和数据库操作等
private Context mContext;
// 导出的文件名
private String mFileName;
// 导出的文件目录
private String mFileDirectory;
// 构造函数,初始化 TextExport 对象
public TextExport(Context context) {
// 从资源中获取存储的文本格式数组
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
mFileName = "";
mFileDirectory = "";
}
// 根据传入的索引获取相应的文本格式
private String getFormat(int id) {
return TEXT_FORMAT[id];
}
/**
* Export the folder identified by folder id to text
*/
// 将指定文件夹的笔记导出为文本,接收文件夹 ID 和打印流作为参数
private void exportFolderToText(String folderId, PrintStream ps) {
// Query notes belong to this folder
// 使用 ContentResolver 查询属于该文件夹的笔记
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
folderId
}, null);
if (notesCursor != null) {
if (notesCursor!= null) {
if (notesCursor.moveToFirst()) {
do {
// Print note's last modified date
// 格式化并打印笔记的最后修改日期
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
// Query data belong to this note
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)));
// 获取该笔记的 ID
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
// 调用 exportNoteToText 方法将该笔记导出为文本
exportNoteToText(noteId, ps);
} while (notesCursor.moveToNext());
}
// 关闭 Cursor释放资源
notesCursor.close();
}
}
/**
* Export note identified by id to a print stream
*/
// 将指定笔记导出为文本,接收笔记 ID 和打印流作为参数
private void exportNoteToText(String noteId, PrintStream ps) {
// 使用 ContentResolver 查询属于该笔记的数据
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId
}, null);
if (dataCursor != null) {
if (dataCursor!= null) {
if (dataCursor.moveToFirst()) {
do {
// 获取该数据的 MIME 类型
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
if (DataConstants.CALL_NOTE.equals(mimeType)) {
// Print phone number
// 获取电话号码
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
// 获取通话日期
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
// 获取位置信息
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
if (!TextUtils.isEmpty(phoneNumber)) {
// 打印电话号码
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber));
}
// Print call date
// 格式化并打印通话日期
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
.format(mContext.getString(R.string.format_datetime_mdhm),
.format(mContext.getString(R.string.format_datetime_mdhm),
callDate)));
// Print call attachment location
if (!TextUtils.isEmpty(location)) {
// 打印通话附件的位置信息
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
location));
}
} else if (DataConstants.NOTE.equals(mimeType)) {
// 获取笔记的内容
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
if (!TextUtils.isEmpty(content)) {
// 打印笔记内容
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
content));
}
}
} while (dataCursor.moveToNext());
}
// 关闭 Cursor释放资源
dataCursor.close();
}
// print a line separator between note
// 在笔记之间打印行分隔符
try {
ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
@ -215,21 +236,21 @@ public class BackupUtils {
}
}
/**
* Note will be exported as text which is user readable
*/
// 导出文本的主要方法,将笔记和文件夹信息导出为文本文件
public int exportToText() {
// 如果外部存储不可用,记录日志并返回相应状态码
if (!externalStorageAvailable()) {
Log.d(TAG, "Media was not mounted");
return STATE_SD_CARD_UNMOUONTED;
}
// 获取导出的打印流
PrintStream ps = getExportToTextPrintStream();
if (ps == null) {
Log.e(TAG, "get print stream error");
return STATE_SYSTEM_ERROR;
}
// First export folder and its notes
// 使用 ContentResolver 查询要导出的文件夹信息
Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
@ -237,55 +258,63 @@ public class BackupUtils {
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
if (folderCursor != null) {
if (folderCursor!= null) {
if (folderCursor.moveToFirst()) {
do {
// Print folder's name
// 获取文件夹名称
String folderName = "";
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
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));
}
// 获取文件夹的 ID
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
// 调用 exportFolderToText 方法导出该文件夹的笔记
exportFolderToText(folderId, ps);
} while (folderCursor.moveToNext());
}
// 关闭 Cursor释放资源
folderCursor.close();
}
// Export notes in root's folder
// 使用 ContentResolver 查询根文件夹中的笔记信息
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!= 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
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)));
// 获取笔记的 ID
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
// 调用 exportNoteToText 方法导出该笔记
exportNoteToText(noteId, ps);
} while (noteCursor.moveToNext());
}
// 关闭 Cursor释放资源
noteCursor.close();
}
// 关闭打印流
ps.close();
// 导出成功,返回成功状态码
return STATE_SUCCESS;
}
/**
* Get a print stream pointed to the file {@generateExportedTextFile}
*/
// 获取导出到文本的打印流
private PrintStream getExportToTextPrintStream() {
// 生成存储在 SD 卡上的文件
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
R.string.file_name_txt_format);
if (file == null) {
@ -296,7 +325,9 @@ public class BackupUtils {
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();
@ -309,14 +340,15 @@ public class BackupUtils {
}
}
/**
* Generate the text file to store imported data
*/
// 在 SD 卡上生成存储导入数据的文本文件
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
StringBuilder sb = new StringBuilder();
// 获取外部存储目录并添加文件路径
sb.append(Environment.getExternalStorageDirectory());
sb.append(context.getString(filePathResId));
// 创建文件目录对象
File filedir = new File(sb.toString());
// 构建文件名,包含当前日期信息
sb.append(context.getString(
fileNameFormatResId,
DateFormat.format(context.getString(R.string.format_date_ymd),
@ -324,9 +356,11 @@ public class BackupUtils {
File file = new File(sb.toString());
try {
// 如果目录不存在,创建目录
if (!filedir.exists()) {
filedir.mkdir();
}
// 如果文件不存在,创建文件
if (!file.exists()) {
file.createNewFile();
}
@ -341,4 +375,3 @@ public class BackupUtils {
}
}

@ -36,99 +36,134 @@ import java.util.HashSet;
public class DataUtils {
// 日志标记,用于在日志中标识该类输出的信息
public static final String TAG = "DataUtils";
// 批量删除笔记的方法
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
// 如果 ids 集合为 null打印日志并返回 true
if (ids == null) {
Log.d(TAG, "the ids is null");
return true;
}
// 如果 ids 集合大小为 0打印日志并返回 true
if (ids.size() == 0) {
Log.d(TAG, "no id is in the hashset");
return true;
}
// 存储操作列表的 ArrayList用于存储要执行的删除操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 遍历 ids 集合
for (long id : ids) {
if(id == Notes.ID_ROOT_FOLDER) {
// 不允许删除系统根文件夹
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));
.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;
}
return true;
} catch (RemoteException e) {
// 打印异常信息
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
// 打印异常信息
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
}
return false;
}
// 将笔记移动到指定文件夹的方法
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
// 创建 ContentValues 对象存储要更新的值
ContentValues values = new ContentValues();
// 更新目标文件夹 ID
values.put(NoteColumns.PARENT_ID, desFolderId);
// 存储源文件夹 ID
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
// 标记为本地修改
values.put(NoteColumns.LOCAL_MODIFIED, 1);
// 执行更新操作
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
// 批量将笔记移动到指定文件夹的方法
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
long folderId) {
// 如果 ids 集合为 null打印日志并返回 true
if (ids == null) {
Log.d(TAG, "the ids is null");
return true;
}
// 存储操作列表的 ArrayList用于存储要执行的更新操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 遍历 ids 集合
for (long id : ids) {
// 创建一个更新操作的构建器
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
// 更新目标文件夹 ID
builder.withValue(NoteColumns.PARENT_ID, folderId);
// 标记为本地修改
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
// 将构建好的操作添加到操作列表中
operationList.add(builder.build());
}
try {
// 应用批量操作,并存储结果
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
// 如果结果为空或结果数组长度为 0 或第一个结果为 null打印日志并返回 false
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()));
// 打印异常信息
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage());
}
return false;
}
/**
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
* {@link Notes#TYPE_SYSTEM}
*/
public static int getUserFolderCount(ContentResolver resolver) {
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI,
// 查询用户自定义文件夹的数量
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)},
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) },
null);
int count = 0;
if(cursor != null) {
if(cursor.moveToFirst()) {
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 以释放资源
cursor.close();
}
}
@ -136,68 +171,86 @@ public class DataUtils {
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)},
new String[] { String.valueOf(type) },
null);
boolean exist = false;
if (cursor != null) {
if (cursor!= null) {
// 若结果集不为空,则表示存在
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭 Cursor 以释放资源
cursor.close();
}
return exist;
}
// 检查笔记是否存在于笔记数据库中
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!= null) {
// 若结果集不为空,则表示存在
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭 Cursor 以释放资源
cursor.close();
}
return exist;
}
// 检查数据是否存在于数据数据库中
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!= null) {
// 若结果集不为空,则表示存在
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭 Cursor 以释放资源
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) {
if (cursor!= null) {
// 若结果集不为空,则表示存在
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭 Cursor 以释放资源
cursor.close();
}
return exist;
}
// 获取文件夹中的笔记小部件信息
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
// 查询指定文件夹中的小部件信息
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI,
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
NoteColumns.PARENT_ID + "=?",
@ -205,91 +258,112 @@ public class DataUtils {
null);
HashSet<AppWidgetAttribute> set = null;
if (c != null) {
if (c!= null) {
if (c.moveToFirst()) {
set = new HashSet<AppWidgetAttribute>();
do {
try {
// 创建并存储小部件属性对象
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) {
// 打印异常信息
Log.e(TAG, e.toString());
}
} while (c.moveToNext());
}
// 关闭 Cursor 以释放资源
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 },
new String[] { CallNote.PHONE_NUMBER },
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
new String[] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
null);
if (cursor != null && cursor.moveToFirst()) {
if (cursor!= null && cursor.moveToFirst()) {
try {
// 获取通话号码
return cursor.getString(0);
} catch (IndexOutOfBoundsException e) {
// 打印异常信息
Log.e(TAG, "Get call number fails " + e.toString());
} finally {
// 关闭 Cursor 以释放资源
cursor.close();
}
}
return "";
}
// 通过电话号码和通话日期获取笔记 ID
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
// 查询具有指定电话号码和通话日期的笔记 ID
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.NOTE_ID },
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 },
new String[] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
null);
if (cursor != null) {
if (cursor!= null) {
if (cursor.moveToFirst()) {
try {
// 获取笔记 ID
return cursor.getLong(0);
} catch (IndexOutOfBoundsException e) {
// 打印异常信息
Log.e(TAG, "Get call note id fails " + e.toString());
}
}
// 关闭 Cursor 以释放资源
cursor.close();
}
return 0;
}
// 通过笔记 ID 获取片段
public static String getSnippetById(ContentResolver resolver, long noteId) {
// 查询指定笔记 ID 的片段
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
new String [] { NoteColumns.SNIPPET },
new String[] { NoteColumns.SNIPPET },
NoteColumns.ID + "=?",
new String [] { String.valueOf(noteId)},
new String[] { String.valueOf(noteId) },
null);
if (cursor != null) {
if (cursor!= null) {
String snippet = "";
if (cursor.moveToFirst()) {
// 获取片段
snippet = cursor.getString(0);
}
// 关闭 Cursor 以释放资源
cursor.close();
return snippet;
}
// 若未找到笔记,抛出异常
throw new IllegalArgumentException("Note is not found with id: " + noteId);
}
// 格式化片段
public static String getFormattedSnippet(String snippet) {
if (snippet != null) {
if (snippet!= null) {
// 去除首尾空格
snippet = snippet.trim();
int index = snippet.indexOf('\n');
if (index != -1) {
if (index!= -1) {
// 截取第一行
snippet = snippet.substring(0, index);
}
}
return snippet;
}
}
}

@ -18,96 +18,96 @@ package net.micode.notes.tool;
public class GTaskStringUtils {
// 表示 GTask JSON 中的 action_id 字段
public final static String GTASK_JSON_ACTION_ID = "action_id";
// 表示 GTask JSON 中的 action_list 字段
public final static String GTASK_JSON_ACTION_LIST = "action_list";
// 表示 GTask JSON 中的 action_type 字段
public final static String GTASK_JSON_ACTION_TYPE = "action_type";
// 表示 GTask JSON 中的 create 操作类型
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create";
// 表示 GTask JSON 中的 get_all 操作类型
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all";
// 表示 GTask JSON 中的 move 操作类型
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move";
// 表示 GTask JSON 中的 update 操作类型
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update";
// 表示 GTask JSON 中的 creator_id 字段
public final static String GTASK_JSON_CREATOR_ID = "creator_id";
// 表示 GTask JSON 中的 child_entity 字段
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity";
// 表示 GTask JSON 中的 client_version 字段
public final static String GTASK_JSON_CLIENT_VERSION = "client_version";
// 表示 GTask JSON 中的 completed 字段
public final static String GTASK_JSON_COMPLETED = "completed";
// 表示 GTask JSON 中的 current_list_id 字段
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";
// 表示 GTask JSON 中的 default_list_id 字段
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";
// 表示 GTask JSON 中的 deleted 字段
public final static String GTASK_JSON_DELETED = "deleted";
// 表示 GTask JSON 中的 dest_list 字段
public final static String GTASK_JSON_DEST_LIST = "dest_list";
// 表示 GTask JSON 中的 dest_parent 字段
public final static String GTASK_JSON_DEST_PARENT = "dest_parent";
// 表示 GTask JSON 中的 dest_parent_type 字段
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";
// 表示 GTask JSON 中的 entity_delta 字段
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta";
// 表示 GTask JSON 中的 entity_type 字段
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type";
// 表示 GTask JSON 中的 get_deleted 字段
public final static String GTASK_JSON_GET_DELETED = "get_deleted";
// 表示 GTask JSON 中的 id 字段
public final static String GTASK_JSON_ID = "id";
// 表示 GTask JSON 中的 index 字段
public final static String GTASK_JSON_INDEX = "index";
// 表示 GTask JSON 中的 last_modified 字段
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified";
// 表示 GTask JSON 中的 latest_sync_point 字段
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";
// 表示 GTask JSON 中的 list_id 字段
public final static String GTASK_JSON_LIST_ID = "list_id";
// 表示 GTask JSON 中的 lists 字段
public final static String GTASK_JSON_LISTS = "lists";
// 表示 GTask JSON 中的 name 字段
public final static String GTASK_JSON_NAME = "name";
// 表示 GTask JSON 中的 new_id 字段
public final static String GTASK_JSON_NEW_ID = "new_id";
// 表示 GTask JSON 中的 notes 字段
public final static String GTASK_JSON_NOTES = "notes";
// 表示 GTask JSON 中的 parent_id 字段
public final static String GTASK_JSON_PARENT_ID = "parent_id";
// 表示 GTask JSON 中的 prior_sibling_id 字段
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";
// 表示 GTask JSON 中的 results 字段
public final static String GTASK_JSON_RESULTS = "results";
// 表示 GTask JSON 中的 source_list 字段
public final static String GTASK_JSON_SOURCE_LIST = "source_list";
// 表示 GTask JSON 中的 tasks 字段
public final static String GTASK_JSON_TASKS = "tasks";
// 表示 GTask JSON 中的 type 字段
public final static String GTASK_JSON_TYPE = "type";
// 表示 GTask JSON 中的 GROUP 类型
public final static String GTASK_JSON_TYPE_GROUP = "GROUP";
// 表示 GTask JSON 中的 TASK 类型
public final static String GTASK_JSON_TYPE_TASK = "TASK";
// 表示 GTask JSON 中的 user 字段
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";
}

@ -24,22 +24,28 @@ 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;
// 默认的背景颜色,使用了上面定义的颜色常量 YELLOW
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;
// 默认的字体大小,使用了上面定义的字体大小常量 TEXT_MEDIUM
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
// 内部类 NoteBgResources用于管理笔记背景资源
public static class NoteBgResources {
// 存储不同颜色的笔记编辑界面背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow,
R.drawable.edit_blue,
@ -48,6 +54,7 @@ public class ResourceParser {
R.drawable.edit_red
};
// 存储不同颜色的笔记编辑界面标题背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow,
R.drawable.edit_title_blue,
@ -56,16 +63,20 @@ public class ResourceParser {
R.drawable.edit_title_red
};
// 根据传入的颜色 ID 获取笔记的背景资源
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
// 根据传入的颜色 ID 获取笔记标题的背景资源
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
}
}
// 根据上下文获取默认的背景颜色 ID
public static int getDefaultBgId(Context context) {
// 检查偏好设置是否开启了背景颜色设置,如果开启,则随机选择一个背景颜色,否则使用默认颜色
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
@ -74,7 +85,9 @@ public class ResourceParser {
}
}
// 内部类 NoteItemBgResources用于管理笔记项的背景资源
public static class NoteItemBgResources {
// 存储不同颜色的笔记项第一个位置的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up,
R.drawable.list_blue_up,
@ -83,6 +96,7 @@ public class ResourceParser {
R.drawable.list_red_up
};
// 存储不同颜色的笔记项正常位置的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle,
R.drawable.list_blue_middle,
@ -91,6 +105,7 @@ public class ResourceParser {
R.drawable.list_red_middle
};
// 存储不同颜色的笔记项最后一个位置的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down,
R.drawable.list_blue_down,
@ -99,6 +114,7 @@ public class ResourceParser {
R.drawable.list_red_down,
};
// 存储不同颜色的单个笔记项的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_SINGLE_RESOURCES = new int [] {
R.drawable.list_yellow_single,
R.drawable.list_blue_single,
@ -107,28 +123,35 @@ public class ResourceParser {
R.drawable.list_red_single
};
// 根据传入的颜色 ID 获取笔记项第一个位置的背景资源
public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id];
}
// 根据传入的颜色 ID 获取笔记项最后一个位置的背景资源
public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id];
}
// 根据传入的颜色 ID 获取单个笔记项的背景资源
public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id];
}
// 根据传入的颜色 ID 获取笔记项正常位置的背景资源
public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id];
}
// 获取文件夹的背景资源
public static int getFolderBgRes() {
return R.drawable.list_folder;
}
}
// 内部类 WidgetBgResources用于管理小部件的背景资源
public static class WidgetBgResources {
// 存储不同颜色的 2x 小部件的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow,
R.drawable.widget_2x_blue,
@ -137,10 +160,12 @@ public class ResourceParser {
R.drawable.widget_2x_red,
};
// 根据传入的颜色 ID 获取 2x 小部件的背景资源
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
// 存储不同颜色的 4x 小部件的背景资源的数组,资源由 R.drawable 提供
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow,
R.drawable.widget_4x_blue,
@ -149,12 +174,15 @@ public class ResourceParser {
R.drawable.widget_4x_red
};
// 根据传入的颜色 ID 获取 4x 小部件的背景资源
public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id];
}
}
// 内部类 TextAppearanceResources用于管理文本外观资源
public static class TextAppearanceResources {
// 存储不同字体大小的文本外观资源的数组,资源由 R.style 提供
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal,
R.style.TextAppearanceMedium,
@ -162,11 +190,12 @@ public class ResourceParser {
R.style.TextAppearanceSuper
};
// 根据传入的字体大小 ID 获取文本外观资源
public static int getTexAppearanceResource(int id) {
/**
* HACKME: Fix bug of store the resource id in shared preference.
* The id may larger than the length of resources, in this case,
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
* HACKME: ID bug
* ID
* {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
*/
if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE;
@ -174,8 +203,9 @@ public class ResourceParser {
return TEXTAPPEARANCE_RESOURCES[id];
}
// 获取文本外观资源的数量
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}
}
Loading…
Cancel
Save