Delete 'src/DataUtils.java'

于昊正^2
p2cafumxo 7 months ago
parent 8d263ab3dd
commit e2b56bb44c

@ -1,406 +0,0 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* Licensed under the Apache License, Version 2.0
* DataUtils
*/
package net.micode.notes.tool; // 声明该类所属的包路径
// 导入需要的类,用于操作内容提供器、数据库、日志和其他功能
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.RemoteException;
import android.util.Log;
import net.micode.notes.data.Notes; // 导入笔记数据定义
import net.micode.notes.data.Notes.CallNote; // 导入与通话笔记相关的定义
import net.micode.notes.data.Notes.NoteColumns; // 导入与笔记列相关的定义
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 导入笔记小部件属性定义
import java.util.ArrayList; // 用于创建动态数组
import java.util.HashSet; // 用于存储无重复的集合
// 定义 DataUtils 工具类
public class DataUtils {
public static final String TAG = "DataUtils"; // 定义日志标记常量
/**
*
*
* @param resolver
* @param ids ID
* @return nulltruefalse
*/
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
if (ids == null) { // 如果传入的ID集合为null
Log.d(TAG, "the ids is null"); // 输出调试日志
return true; // 返回true表示没有需要删除的内容
}
if (ids.size() == 0) { // 如果ID集合为空
Log.d(TAG, "no id is in the hashset"); // 输出调试日志
return true; // 返回true
}
// 创建一个操作列表,用于批量删除操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) { // 遍历需要删除的ID集合
if (id == Notes.ID_ROOT_FOLDER) { // 如果ID是系统根文件夹
Log.e(TAG, "Don't delete system folder root"); // 输出错误日志
continue; // 跳过此ID
}
// 创建删除操作
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, "delete notes failed, ids:" + ids.toString()); // 输出调试日志
return false; // 删除失败返回false
}
return true; // 删除成功返回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; // 出现异常时返回false
}
/**
*
*
* @param resolver
* @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(); // 创建一个ContentValues对象
values.put(NoteColumns.PARENT_ID, desFolderId); // 设置目标文件夹ID
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 设置原始文件夹ID
values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改
// 更新指定笔记的文件夹信息
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
/**
*
*
* @param resolver
* @param ids ID
* @param folderId ID
* @return nulltruefalse
*/
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, long folderId) {
if (ids == null) { // 如果ID集合为null
Log.d(TAG, "the ids is null"); // 输出调试日志
return true; // 返回true
}
// 构建更新操作的列表
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) { // 遍历ID集合
// 创建更新操作
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置目标文件夹ID
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, "move 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; // 出现异常时返回false
}
/**
*
*
* @param resolver
* @return
*/
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); // 排序:无
int count = 0; // 初始化文件夹计数
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.close(); // 关闭游标
}
}
}
return count; // 返回文件夹数量
}
}
/**
*
*
* @param resolver
* @param noteId ID
* @param type
* @return truefalse
*/
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
// 查询数据库,检查指定类型的笔记是否存在且不在垃圾箱中
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询的URI
null, // 查询的列null表示查询所有列
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // 条件
new String[]{String.valueOf(type)}, // 条件值
null); // 排序:无
boolean exist = false; // 初始化结果为false
if (cursor != null) { // 如果查询结果不为空
if (cursor.getCount() > 0) { // 如果查询结果有记录
exist = true; // 设置结果为true
}
cursor.close(); // 关闭游标
}
return exist; // 返回结果
}
/**
* ID
*
* @param resolver
* @param noteId ID
* @return truefalse
*/
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
// 查询数据库,检查笔记是否存在
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询的URI
null, // 查询所有列
null, // 无条件
null, // 无条件值
null); // 无排序
boolean exist = false; // 初始化结果为false
if (cursor != null) { // 如果查询结果不为空
if (cursor.getCount() > 0) { // 如果查询结果有记录
exist = true; // 设置结果为true
}
cursor.close(); // 关闭游标
}
return exist; // 返回结果
}
/**
* ID
*
* @param resolver
* @param dataId ID
* @return truefalse
*/
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
// 查询数据库,检查数据是否存在
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), // 查询的URI
null, // 查询所有列
null, // 无条件
null, // 无条件值
null); // 无排序
boolean exist = false; // 初始化结果为false
if (cursor != null) { // 如果查询结果不为空
if (cursor.getCount() > 0) { // 如果查询结果有记录
exist = true; // 设置结果为true
}
cursor.close(); // 关闭游标
}
return exist; // 返回结果
}
/**
*
*
* @param resolver
* @param name
* @return truefalse
*/
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
// 查询数据库,检查文件夹名称是否存在且不在垃圾箱中
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询的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; // 初始化结果为false
if (cursor != null) { // 如果查询结果不为空
if (cursor.getCount() > 0) { // 如果查询结果有记录
exist = true; // 设置结果为true
}
cursor.close(); // 关闭游标
}
return exist; // 返回结果
}
/**
*
*
* @param resolver
* @param folderId ID
* @return
*/
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
// 查询数据库,获取文件夹中的小部件信息
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, // 查询的URI
new String[]{NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE}, // 查询的列
NoteColumns.PARENT_ID + "=?", // 条件父ID为指定文件夹
new String[]{String.valueOf(folderId)}, // 条件值
null); // 无排序
HashSet<AppWidgetAttribute> set = null; // 初始化小部件集合为null
if (c != null) { // 如果查询结果不为空
if (c.moveToFirst()) { // 移动到第一条记录
set = new HashSet<AppWidgetAttribute>(); // 初始化小部件集合
do { // 遍历查询结果
try {
AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建小部件属性对象
widget.widgetId = c.getInt(0); // 获取小部件ID
widget.widgetType = c.getInt(1); // 获取小部件类型
set.add(widget); // 将小部件添加到集合
} catch (IndexOutOfBoundsException e) { // 捕获索引越界异常
Log.e(TAG, e.toString()); // 输出错误日志
}
} while (c.moveToNext()); // 移动到下一条记录
}
c.close(); // 关闭游标
}
return set; // 返回小部件集合
}
/**
* ID
*
* @param resolver
* @param noteId ID
* @return
*/
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
// 查询数据库获取与笔记ID关联的通话号码
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询的URI
new String[]{CallNote.PHONE_NUMBER}, // 查询的列:电话号码
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 条件匹配笔记ID和MIME类型
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()); // 输出错误日志
} finally {
cursor.close(); // 关闭游标
}
}
return ""; // 未找到记录时返回空字符串
}
/**
* ID
*
* @param resolver
* @param phoneNumber
* @param callDate
* @return ID0
*/
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
// 查询数据库根据电话号码和通话日期获取笔记ID
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询的URI
new String[]{CallNote.NOTE_ID}, // 查询的列笔记ID
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
+ CallNote.PHONE_NUMBER + ",?)", // 条件匹配通话日期、MIME类型和电话号码
new String[]{String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber}, // 条件值
null); // 无排序
if (cursor != null) { // 如果查询结果不为空
if (cursor.moveToFirst()) { // 如果有记录
try {
return cursor.getLong(0); // 返回笔记ID
} catch (IndexOutOfBoundsException e) { // 捕获索引越界异常
Log.e(TAG, "Get call note id fails " + e.toString()); // 输出错误日志
}
}
cursor.close(); // 关闭游标
}
return 0; // 未找到记录时返回0
}
/**
* ID
*
* @param resolver
* @param noteId ID
* @return IllegalArgumentException
*/
public static String getSnippetById(ContentResolver resolver, long noteId) {
// 查询数据库获取指定ID笔记的摘要
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询的URI
new String[]{NoteColumns.SNIPPET}, // 查询的列:摘要
NoteColumns.ID + "=?", // 条件匹配笔记ID
new String[]{String.valueOf(noteId)}, // 条件值
null); // 无排序
if (cursor != null) { // 如果查询结果不为空
String snippet = ""; // 初始化摘要为空字符串
if (cursor.moveToFirst()) { // 如果有记录
snippet = cursor.getString(0); // 获取摘要字符串
}
cursor.close(); // 关闭游标
return snippet; // 返回摘要
}
// 如果未找到记录,抛出异常
throw new IllegalArgumentException("Note is not found with id: " + noteId);
}
/**
*
*
*
* @param snippet
* @return
*/
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); // 截取至第一个换行符之前的内容
}
}
return snippet; // 返回格式化后的摘要字符串
}
}
Loading…
Cancel
Save