|
|
|
@ -37,106 +37,160 @@ import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者操作
|
|
|
|
|
* @param ids 要删除的笔记ID集合,如果为null或空集合则直接返回成功
|
|
|
|
|
* @return boolean 删除结果:true表示全部删除成功(或无需删除),false表示删除失败
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
// 处理空参数情况
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
// 构建批量删除操作列表
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
// 跳过系统根文件夹的删除
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);
|
|
|
|
|
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
|
|
|
|
|
// 执行批量删除操作
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
builder.withValue(NoteColumns.PARENT_ID, folderId);
|
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 将指定笔记从源文件夹移动到目标文件夹
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者操作
|
|
|
|
|
* @param id 要移动的笔记ID
|
|
|
|
|
* @param srcFolderId 源文件夹ID,笔记当前所在的文件夹
|
|
|
|
|
* @param desFolderId 目标文件夹ID,笔记将要移动到的文件夹
|
|
|
|
|
*/
|
|
|
|
|
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
|
|
|
|
|
// 创建ContentValues对象并设置笔记的新位置和修改状态
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);
|
|
|
|
|
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
// 更新笔记在数据库中的位置信息
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量将笔记移动到指定文件夹
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者操作
|
|
|
|
|
* @param ids 要移动的笔记ID集合,如果为null则视为成功(不执行任何操作)
|
|
|
|
|
* @param folderId 目标文件夹ID
|
|
|
|
|
* @return true表示操作成功,false表示操作失败
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
// 处理空ID集合的特殊情况
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建批量更新操作列表
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
builder.withValue(NoteColumns.PARENT_ID, folderId);
|
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行批量操作并处理结果
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
|
|
|
|
|
*/
|
|
|
|
|
// 定义一个静态方法,用于获取用户文件夹数量
|
|
|
|
|
public static int getUserFolderCount(ContentResolver resolver) {
|
|
|
|
|
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String[] { "COUNT(*)" },
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
|
|
|
|
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
|
|
|
|
|
null);
|
|
|
|
|
// 查询数据库,获取符合条件的记录数量
|
|
|
|
|
Cursor cursor = resolver.query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI, // 要查询的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()) {
|
|
|
|
|
int count = 0; // 初始化计数器为0
|
|
|
|
|
if(cursor != null) { // 检查Cursor对象是否有效
|
|
|
|
|
if(cursor.moveToFirst()) { // 将游标移动到第一条记录
|
|
|
|
|
try {
|
|
|
|
|
count = cursor.getInt(0);
|
|
|
|
|
count = cursor.getInt(0); // 获取第一列的值(即COUNT(*)的结果)
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
// 如果发生数组越界异常,记录错误日志
|
|
|
|
|
Log.e(TAG, "get folder count failed:" + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 无论是否发生异常,都要关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
return count; // 返回文件夹数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查指定类型的笔记是否存在于笔记数据库中(不包括回收站中的笔记)
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者查询
|
|
|
|
|
* @param noteId 要查询的笔记ID
|
|
|
|
|
* @param type 要匹配的笔记类型
|
|
|
|
|
* @return boolean 如果存在符合条件的笔记则返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
|
|
|
|
|
// 查询条件:匹配指定笔记ID、指定类型,且父ID不等于回收站文件夹ID
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null,
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
|
|
|
|
@ -145,6 +199,7 @@ public class DataUtils {
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
// 如果查询结果包含记录,则标记为存在
|
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
@ -153,12 +208,21 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查指定ID的笔记是否存在于笔记数据库中
|
|
|
|
|
*
|
|
|
|
|
* @param resolver 内容解析器,用于查询内容提供者
|
|
|
|
|
* @param noteId 要检查的笔记ID
|
|
|
|
|
* @return boolean 如果笔记存在返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
|
|
|
|
|
// 构建URI并查询指定ID的笔记
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
// 如果查询结果包含记录,则标记为存在
|
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
@ -167,12 +231,21 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查指定ID的数据是否存在于数据数据库中
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者查询
|
|
|
|
|
* @param dataId 要查询的数据记录的ID
|
|
|
|
|
* @return boolean 返回true表示数据存在,false表示不存在或查询失败
|
|
|
|
|
*/
|
|
|
|
|
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
|
|
|
|
|
// 构建URI并查询指定ID的数据记录
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
// 如果查询结果包含记录,则标记为存在
|
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
@ -181,13 +254,24 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查指定名称的文件夹是否已存在(非回收站中的文件夹)
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于查询内容提供者数据
|
|
|
|
|
* @param name 要检查的文件夹名称
|
|
|
|
|
* @return boolean - 如果存在同名文件夹返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
@ -197,42 +281,63 @@ public class DataUtils {
|
|
|
|
|
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 + "=?",
|
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
|
null);
|
|
|
|
|
/**
|
|
|
|
|
* 获取指定文件夹中所有笔记小部件的属性集合
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于查询内容提供者数据
|
|
|
|
|
* @param folderId 要查询的文件夹ID
|
|
|
|
|
* @return HashSet<AppWidgetAttribute> 包含所有笔记小部件属性的集合,如果没有则返回null
|
|
|
|
|
*/
|
|
|
|
|
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 + "=?",
|
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
HashSet<AppWidgetAttribute> set = 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());
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 如果查询结果不为空,则遍历Cursor构建小部件属性集合
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
set = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
// 从Cursor中读取小部件数据并创建属性对象
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取对应的电话号码
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于查询内容提供者数据
|
|
|
|
|
* @param noteId 要查询的笔记ID
|
|
|
|
|
* @return 返回查询到的电话号码字符串,如果查询失败或不存在则返回空字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
|
|
|
|
|
// 查询条件:指定noteId和mime类型为CallNote类型的数据
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
new String [] { CallNote.PHONE_NUMBER },
|
|
|
|
|
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
|
|
|
|
|
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 处理查询结果
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
// 返回第一行第一列的电话号码
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());
|
|
|
|
@ -243,7 +348,16 @@ public class DataUtils {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据电话号码和通话日期获取对应的便签ID
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于访问内容提供者
|
|
|
|
|
* @param phoneNumber 电话号码,用于查询匹配的便签记录
|
|
|
|
|
* @param callDate 通话日期时间戳,用于查询指定日期的便签记录
|
|
|
|
|
* @return 匹配的便签ID,如果未找到则返回0
|
|
|
|
|
*/
|
|
|
|
|
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
|
|
|
|
|
// 查询条件:通话日期、MIME类型和电话号码(使用PHONE_NUMBERS_EQUAL函数比较)
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
new String [] { CallNote.NOTE_ID },
|
|
|
|
|
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
|
|
|
|
@ -251,9 +365,11 @@ public class DataUtils {
|
|
|
|
|
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 处理查询结果
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
// 返回第一条记录的NOTE_ID
|
|
|
|
|
return cursor.getLong(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
@ -264,7 +380,16 @@ public class DataUtils {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取对应的内容片段(摘要)
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于执行内容提供者查询
|
|
|
|
|
* @param noteId 要查询的笔记的唯一ID
|
|
|
|
|
* @return 返回查询到的笔记内容片段(摘要),如果未找到则返回空字符串
|
|
|
|
|
* @throws IllegalArgumentException 如果未找到对应ID的笔记时抛出
|
|
|
|
|
*/
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
|
// 查询指定ID的笔记片段
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
@ -273,6 +398,7 @@ public class DataUtils {
|
|
|
|
|
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
String snippet = "";
|
|
|
|
|
// 如果查询到结果,获取第一条记录的片段内容
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
snippet = cursor.getString(0);
|
|
|
|
|
}
|
|
|
|
@ -282,9 +408,22 @@ public class DataUtils {
|
|
|
|
|
throw new IllegalArgumentException("Note is not found with id: " + noteId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取格式化后的代码片段
|
|
|
|
|
*
|
|
|
|
|
* 该方法用于处理原始代码片段,主要完成以下操作:
|
|
|
|
|
* 1. 去除首尾空白字符
|
|
|
|
|
* 2. 截取第一个换行符之前的内容(如果存在换行符)
|
|
|
|
|
*
|
|
|
|
|
* @param snippet 原始代码片段,可能为null
|
|
|
|
|
* @return 格式化后的代码片段,可能为null(当输入为null时)或单行字符串
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|