pull/8/head
hyf 7 months ago committed by 13864042598
parent 557e9f6072
commit 1249eaffd8

@ -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);

Loading…
Cancel
Save