|
|
|
@ -34,11 +34,20 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据工具类,提供笔记数据的常用操作方法
|
|
|
|
|
* 包括批量删除、移动、查询等功能
|
|
|
|
|
* 使用ContentProvider操作笔记数据库
|
|
|
|
|
*/
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param ids 要删除的笔记ID集合
|
|
|
|
|
* @return 删除成功返回true,失败返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
@ -49,8 +58,10 @@ public class DataUtils {
|
|
|
|
|
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;
|
|
|
|
@ -59,7 +70,9 @@ public class DataUtils {
|
|
|
|
|
.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());
|
|
|
|
@ -74,6 +87,13 @@ public class DataUtils {
|
|
|
|
|
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);
|
|
|
|
@ -82,6 +102,13 @@ public class DataUtils {
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量将笔记移动到指定文件夹
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param ids 要移动的笔记ID集合
|
|
|
|
|
* @param folderId 目标文件夹ID
|
|
|
|
|
* @return 移动成功返回true,失败返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
@ -89,6 +116,7 @@ public class DataUtils {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建批量操作列表
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
@ -99,6 +127,7 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
@ -114,7 +143,9 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
|
|
|
|
|
* 获取用户文件夹数量(不包括系统文件夹)
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @return 用户文件夹数量
|
|
|
|
|
*/
|
|
|
|
|
public static int getUserFolderCount(ContentResolver resolver) {
|
|
|
|
|
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
@ -138,6 +169,13 @@ public class DataUtils {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查笔记是否在数据库中可见(未被删除)
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @param type 笔记类型
|
|
|
|
|
* @return 可见返回true,不可见返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null,
|
|
|
|
@ -155,6 +193,12 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查笔记是否存在于数据库中
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @return 存在返回true,不存在返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
@ -169,6 +213,12 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查数据项是否存在于数据库中
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param dataId 数据项ID
|
|
|
|
|
* @return 存在返回true,不存在返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
@ -183,6 +233,12 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查是否存在同名的可见文件夹
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param name 文件夹名称
|
|
|
|
|
* @return 存在返回true,不存在返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null,
|
|
|
|
|
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
|
|
|
|
@ -199,6 +255,12 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取文件夹下的所有笔记小部件
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param folderId 文件夹ID
|
|
|
|
|
* @return 小部件属性集合
|
|
|
|
|
*/
|
|
|
|
|
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 },
|
|
|
|
@ -226,6 +288,12 @@ public class DataUtils {
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取通话号码
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @return 通话号码,不存在则返回空字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
new String [] { CallNote.PHONE_NUMBER },
|
|
|
|
@ -245,6 +313,13 @@ 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) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
new String [] { CallNote.NOTE_ID },
|
|
|
|
@ -266,6 +341,12 @@ public class DataUtils {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取摘要
|
|
|
|
|
* @param resolver ContentResolver对象
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @return 笔记摘要
|
|
|
|
|
*/
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
@ -284,6 +365,11 @@ public class DataUtils {
|
|
|
|
|
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();
|
|
|
|
|