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.
XIAOMINotes/Desktop/tool/DataUtils.java

387 lines
16 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.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
public class DataUtils {
// 定义一个用于日志的标签常量
public static final String TAG = "DataUtils";
// 批量删除笔记的方法接受ContentResolver对象和一组笔记的ID集合
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
// 检查ID集合是否为null如果为null记录日志并返回true
if (ids == null) {
Log.d(TAG, "the ids is null");
return true;
}
// 检查ID集合是否为空如果为空记录日志并返回true
if(ids.size() == 0) {
Log.d(TAG, "no id is in the hashset");
return true;
}
// 创建一个ContentProviderOperation列表用于存储删除操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 遍历ID集合为每个ID创建一个删除操作
for (long id : ids) {
// 如果ID是根文件夹ID记录错误日志并跳过删除操作
if(id == Notes.ID_ROOT_FOLDER) {
Log.e(TAG, "Don't delete system folder root");
continue;
}
// 创建删除操作的Builder对象
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记录日志并返回false
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) {
// 捕获RemoteException记录错误日志
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
// 捕获OperationApplicationException记录错误日志
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
}
// 如果发生异常返回false
return false;
}
// 将笔记移动到指定文件夹
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
// 创建一个ContentValues对象用于存储更新的值
ContentValues values = new ContentValues();
// 将目标文件夹ID设置为新的父文件夹ID
values.put(NoteColumns.PARENT_ID, desFolderId);
// 将原始父文件夹ID设置为源文件夹ID
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
// 将LOCAL_MODIFIED标记设置为1表示已修改
values.put(NoteColumns.LOCAL_MODIFIED, 1);
// 更新数据库中的笔记记录
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
// 批量将笔记移动到指定文件夹
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, long folderId) {
// 检查ID集合是否为空如果为空记录日志并返回true
if (ids == null) {
Log.d(TAG, "the ids is null");
return true;
}
// 创建一个操作列表,用于存储更新操作
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
// 遍历ID集合为每个ID创建一个更新操作
for (long id : ids) {
// 创建更新操作的Builder对象
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
// 设置更新的值将目标文件夹ID设置为新的父文件夹ID将LOCAL_MODIFIED标记设置为1
builder.withValue(NoteColumns.PARENT_ID, folderId);
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
// 将更新操作添加到操作列表中
operationList.add(builder.build());
}
try {
// 应用批量更新操作
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
// 检查更新结果如果结果为空、长度为0或第一个结果为null记录日志并返回false
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) {
// 捕获RemoteException记录错误日志
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
// 捕获OperationApplicationException记录错误日志
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
}
// 更新失败返回false
return false;
}
/**
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
*/
// 获取用户文件夹的数量
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) {
// 捕获IndexOutOfBoundsException异常记录错误日志
Log.e(TAG, "get folder count failed:" + e.toString());
} finally {
// 关闭游标
cursor.close();
}
}
}
// 返回用户文件夹的数量
return count;
}
// 检查笔记是否在数据库中可见
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
// 查询笔记数据库,检查笔记是否在数据库中可见
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null,
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
new String [] {String.valueOf(type)},
null);
boolean exist = false;
if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭游标
cursor.close();
}
// 返回笔记是否在数据库中可见
return exist;
}
// 检查笔记是否存在于数据库中
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
// 查询笔记数据库,检查笔记是否存在于数据库中
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, null, null, null);
boolean exist = false;
if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭游标
cursor.close();
}
// 返回笔记是否存在于数据库中
return exist;
}
// 检查数据是否存在于数据库中
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
// 查询数据数据库,检查数据是否存在于数据库中
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
null, null, null, null);
boolean exist = false;
if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
}
// 关闭游标
cursor.close();
}
// 返回数据是否存在于数据库中
return exist;
}
// 检查可见文件夹名称是否存在于数据库中
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
// 查询笔记数据库,检查可见文件夹名称是否存在于数据库中
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_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;
if(cursor != null) {
if(cursor.getCount() > 0) {
exist = true;
}
// 关闭游标
cursor.close();
}
// 返回可见文件夹名称是否存在于数据库中
return exist;
}
// 获取文件夹中所有笔记的小部件信息集合
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 },
NoteColumns.PARENT_ID + "=?",
new String[] { String.valueOf(folderId) },
null);
HashSet<AppWidgetAttribute> set = null;
if (c != null) {
if (c.moveToFirst()) {
// 如果查询结果不为空创建一个HashSet对象用于存储小部件信息
set = new HashSet<AppWidgetAttribute>();
do {
try {
// 遍历查询结果为每个笔记创建一个小部件属性对象并将其添加到HashSet中
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) {
// 捕获IndexOutOfBoundsException异常记录错误日志
Log.e(TAG, e.toString());
}
} while (c.moveToNext());
}
// 关闭游标
c.close();
}
// 返回文件夹中所有笔记的小部件信息集合
return set;
}
// 根据笔记ID获取通话记录号码
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
// 查询指定笔记ID的通话记录号码
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.PHONE_NUMBER },
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
null);
if (cursor != null && cursor.moveToFirst()) {
try {
// 如果查询结果不为空,获取通话记录号码并返回
return cursor.getString(0);
} catch (IndexOutOfBoundsException e) {
// 捕获IndexOutOfBoundsException异常记录错误日志
Log.e(TAG, "Get call number fails " + e.toString());
} finally {
// 关闭游标
cursor.close();
}
}
// 如果查询结果为空或发生异常,返回空字符串
return "";
}
// 根据通话日期和电话号码查询通话记录的笔记ID
public static long getCallNoteIdByCallDateAndPhoneNumber(ContentResolver resolver, long callDate, String phoneNumber) {
// 查询通话日期和电话号码匹配的通话记录的笔记ID
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.NOTE_ID },
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
+ CallNote.PHONE_NUMBER + ",?)",
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
try {
// 如果查询结果不为空获取通话记录的笔记ID并返回
return cursor.getLong(0);
} catch (IndexOutOfBoundsException e) {
// 捕获IndexOutOfBoundsException异常记录错误日志
Log.e(TAG, "Get call note id fails " + e.toString());
}
}
// 关闭游标
cursor.close();
}
// 如果查询结果为空或发生异常返回0
return 0;
}
// 根据笔记ID查询摘要
public static String getSnippetById(ContentResolver resolver, long noteId) {
// 查询指定笔记ID的摘要信息
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
new String [] { NoteColumns.SNIPPET },
NoteColumns.ID + "=?",
new String [] { String.valueOf(noteId)},
null);
if (cursor != null) {
String snippet = "";
if (cursor.moveToFirst()) {
// 如果查询结果不为空,获取摘要信息并返回
snippet = cursor.getString(0);
}
// 关闭游标
cursor.close();
return snippet;
}
// 如果查询结果为空抛出IllegalArgumentException异常
throw new IllegalArgumentException("Note is not found with id: " + noteId);
}
// 格式化摘要信息
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;
}
}