You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18myx/tool--DataUtils.txt

80 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package net.micode.notes.tool;
// 导入语句
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashSet;
/**
* 笔记数据操作的工具类。
*/
public class DataUtils {
// 日志标签
public static final String TAG = "DataUtils";
/**
* 批量直接删除多个笔记。
*
* @param resolver ContentResolver 实例
* @param ids 要删除的笔记 ID 的 HashSet
* @return 删除是否成功的布尔值
*/
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
// 检查传入的 ID 集合是否为空
if (ids == null) {
Log.d(TAG, "ids 为空");
return true;
}
// 检查传入的 ID 集合是否为空集合
if (ids.size() == 0) {
Log.d(TAG, "hashset 中没有 ID");
return true;
}
// 准备要执行的操作列表
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 遍历传入的 ID 集合,构建删除操作
for (long id : ids) {
// 如果是根文件夹,则跳过删除操作
if (id == Notes.ID_ROOT_FOLDER) {
Log.e(TAG, "不要删除系统文件夹的根目录");
continue;
}
// 构建要执行的删除操作并添加到操作列表中
ContentProviderOperation.Builder builder = ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
operationList.add(builder.build());
}
try {
// 应用批处理操作
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
// 检查批处理结果
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "删除笔记失败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()));
}
// 删除失败,返回 false
return false;
}
// 其他方法使用相似的注释风格...
}