|
|
|
@ -36,42 +36,71 @@ import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
// 用于日志记录的标签
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记
|
|
|
|
|
*
|
|
|
|
|
* @param resolver 用于与内容提供器交互的 ContentResolver
|
|
|
|
|
* @param ids 要删除的笔记 ID 的 HashSet
|
|
|
|
|
* @return 如果删除成功返回 true,否则返回 false
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
// 检查 ids 是否为 null
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
// 如果为 null,视为删除操作成功(可能是因为没有要删除的内容)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// 检查 ids 集合是否为空
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
// 如果为空,视为删除操作成功(没有实际要删除的笔记)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
// 遍历要删除的笔记 ID 集合
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
// 不允许删除系统根文件夹
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 创建一个删除操作的构建器,指定要删除的笔记 URI
|
|
|
|
|
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
|
|
|
|
|
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) {
|
|
|
|
|
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()));
|
|
|
|
|
}
|
|
|
|
|
// 如果发生异常,返回 false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将笔记移动到指定文件夹
|
|
|
|
|
*
|
|
|
|
|
* @param resolver 用于与内容提供器交互的 ContentResolver
|
|
|
|
|
* @param id 要移动的笔记 ID
|
|
|
|
|
* @param srcFolderId 笔记的源文件夹 ID
|
|
|
|
|
* @param desFolderId 笔记的目标文件夹 ID
|
|
|
|
|
*/
|
|
|
|
|
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);
|
|
|
|
|