|
|
|
@ -2,294 +2,379 @@
|
|
|
|
|
* 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
|
|
|
|
|
* 您可以在遵守 License 的前提下使用本文件。
|
|
|
|
|
* 您可以从以下地址获取 License 副本:
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* 除非适用法律要求或书面同意,本软件根据 License 分发时“按现状”提供,
|
|
|
|
|
* 不附带任何明示或暗示的保证或条件。请参阅 License 了解具体的权限和限制。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
|
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 android.content.ContentProviderOperation; // 导入内容提供者操作类
|
|
|
|
|
import android.content.ContentProviderResult; // 导入内容提供者结果类
|
|
|
|
|
import android.content.ContentResolver; // 导入内容解析器类
|
|
|
|
|
import android.content.ContentUris; // 导入内容URI工具类
|
|
|
|
|
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;
|
|
|
|
|
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; // 导入ArrayList类
|
|
|
|
|
import java.util.HashSet; // 导入HashSet类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据工具类,提供与笔记数据操作相关的实用方法
|
|
|
|
|
* 包括批量删除、移动笔记、查询文件夹和笔记信息等功能
|
|
|
|
|
*/
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
public static final String TAG = "DataUtils"; // 日志标签
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @param ids 要删除的笔记ID集合
|
|
|
|
|
* @return 删除成功返回true,失败返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
if (ids == null) { // 检查ID集合是否为空
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 空集合视为操作成功
|
|
|
|
|
}
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
if (ids.size() == 0) { // 检查ID集合是否为空
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 空集合视为操作成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<>(); // 创建操作列表
|
|
|
|
|
for (long id : ids) { // 遍历ID集合
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) { // 检查是否为根文件夹
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
continue; // 跳过根文件夹,不删除
|
|
|
|
|
}
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
.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) {
|
|
|
|
|
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;
|
|
|
|
|
return false; // 操作失败
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
return true; // 操作成功
|
|
|
|
|
} catch (RemoteException e) { // 处理远程异常
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
} catch (OperationApplicationException e) { // 处理操作应用异常
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return 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();
|
|
|
|
|
values.put(NoteColumns.PARENT_ID, desFolderId);
|
|
|
|
|
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
|
|
|
|
|
ContentValues values = new 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 移动成功返回true,失败返回false
|
|
|
|
|
*/
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) { // 检查ID集合是否为空
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 空集合视为操作成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<>(); // 创建操作列表
|
|
|
|
|
for (long id : ids) { // 遍历ID集合
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
builder.withValue(NoteColumns.PARENT_ID, folderId);
|
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
.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) {
|
|
|
|
|
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;
|
|
|
|
|
return false; // 操作失败
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
return true; // 操作成功
|
|
|
|
|
} catch (RemoteException e) { // 处理远程异常
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
} catch (OperationApplicationException e) {
|
|
|
|
|
} catch (OperationApplicationException e) { // 处理操作应用异常
|
|
|
|
|
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 发生异常,操作失败
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
|
|
|
|
|
* 获取用户文件夹数量(不包括系统文件夹)
|
|
|
|
|
* @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);
|
|
|
|
|
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()) {
|
|
|
|
|
int count = 0; // 初始化数量为0
|
|
|
|
|
if(cursor != null) { // 检查游标是否有效
|
|
|
|
|
if(cursor.moveToFirst()) { // 移动到第一条记录
|
|
|
|
|
try {
|
|
|
|
|
count = cursor.getInt(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
count = cursor.getInt(0); // 获取数量值
|
|
|
|
|
} catch (IndexOutOfBoundsException e) { // 处理索引越界异常
|
|
|
|
|
Log.e(TAG, "get folder count failed:" + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
return count; // 返回文件夹数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查笔记是否在数据库中可见(存在且不在回收站)
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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,
|
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
|
|
|
|
|
new String [] {String.valueOf(type)},
|
|
|
|
|
null);
|
|
|
|
|
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;
|
|
|
|
|
boolean exist = false; // 初始化存在标志为false
|
|
|
|
|
if (cursor != null) { // 检查游标是否有效
|
|
|
|
|
if (cursor.getCount() > 0) { // 检查记录数量
|
|
|
|
|
exist = true; // 存在记录,设置标志为true
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
return exist; // 返回存在标志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查笔记是否存在于数据库中
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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);
|
|
|
|
|
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;
|
|
|
|
|
boolean exist = false; // 初始化存在标志为false
|
|
|
|
|
if (cursor != null) { // 检查游标是否有效
|
|
|
|
|
if (cursor.getCount() > 0) { // 检查记录数量
|
|
|
|
|
exist = true; // 存在记录,设置标志为true
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
return exist; // 返回存在标志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查数据项是否存在于数据库中
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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);
|
|
|
|
|
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;
|
|
|
|
|
boolean exist = false; // 初始化存在标志为false
|
|
|
|
|
if (cursor != null) { // 检查游标是否有效
|
|
|
|
|
if (cursor.getCount() > 0) { // 检查记录数量
|
|
|
|
|
exist = true; // 存在记录,设置标志为true
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
return exist; // 返回存在标志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查可见文件夹名称是否已存在
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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 +
|
|
|
|
|
" 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 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; // 初始化存在标志为false
|
|
|
|
|
if(cursor != null) { // 检查游标是否有效
|
|
|
|
|
if(cursor.getCount() > 0) { // 检查记录数量
|
|
|
|
|
exist = true; // 存在记录,设置标志为true
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
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,
|
|
|
|
|
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
|
|
|
|
|
NoteColumns.PARENT_ID + "=?",
|
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
|
null);
|
|
|
|
|
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记表
|
|
|
|
|
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, // 查询小部件ID和类型
|
|
|
|
|
NoteColumns.PARENT_ID + "=?", // 查询条件:父文件夹ID匹配
|
|
|
|
|
new String[] { String.valueOf(folderId) }, // 查询参数
|
|
|
|
|
null); // 排序方式
|
|
|
|
|
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
|
set = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null; // 初始化集合为null
|
|
|
|
|
if (c != null) { // 检查游标是否有效
|
|
|
|
|
if (c.moveToFirst()) { // 移动到第一条记录
|
|
|
|
|
set = new HashSet<>(); // 创建集合
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
|
|
widget.widgetId = c.getInt(0);
|
|
|
|
|
widget.widgetType = c.getInt(1);
|
|
|
|
|
set.add(widget);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
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());
|
|
|
|
|
} while (c.moveToNext()); // 移动到下一条记录
|
|
|
|
|
}
|
|
|
|
|
c.close();
|
|
|
|
|
c.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return set;
|
|
|
|
|
return set; // 返回集合
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取通话号码
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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 },
|
|
|
|
|
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
|
|
|
|
|
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
|
|
|
|
|
null);
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表
|
|
|
|
|
new String [] { CallNote.PHONE_NUMBER }, // 查询电话号码列
|
|
|
|
|
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 查询条件:笔记ID匹配且类型为通话记录
|
|
|
|
|
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, // 查询参数
|
|
|
|
|
null); // 排序方式
|
|
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) { // 检查游标是否有效并移动到第一条记录
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
return cursor.getString(0); // 返回电话号码
|
|
|
|
|
} catch (IndexOutOfBoundsException e) { // 处理索引越界异常
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
return ""; // 未找到返回空字符串
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据电话号码和通话日期获取笔记ID
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @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 },
|
|
|
|
|
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);
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表
|
|
|
|
|
new String [] { CallNote.NOTE_ID }, // 查询笔记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()) {
|
|
|
|
|
if (cursor != null) { // 检查游标是否有效
|
|
|
|
|
if (cursor.moveToFirst()) { // 移动到第一条记录
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getLong(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
return cursor.getLong(0); // 返回笔记ID
|
|
|
|
|
} catch (IndexOutOfBoundsException e) { // 处理索引越界异常
|
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
return 0; // 未找到返回0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取摘要
|
|
|
|
|
* @param resolver 内容解析器
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @return 摘要内容
|
|
|
|
|
* @throws IllegalArgumentException 笔记不存在时抛出异常
|
|
|
|
|
*/
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
|
null);
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_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);
|
|
|
|
|
if (cursor != null) { // 检查游标是否有效
|
|
|
|
|
String snippet = ""; // 初始化摘要为空字符串
|
|
|
|
|
if (cursor.moveToFirst()) { // 移动到第一条记录
|
|
|
|
|
snippet = cursor.getString(0); // 获取摘要内容
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
return snippet;
|
|
|
|
|
cursor.close(); // 关闭游标
|
|
|
|
|
return snippet; // 返回摘要
|
|
|
|
|
}
|
|
|
|
|
throw new IllegalArgumentException("Note is not found with id: " + noteId);
|
|
|
|
|
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);
|
|
|
|
|
if (snippet != null) { // 检查摘要是否为空
|
|
|
|
|
snippet = snippet.trim(); // 去除前后空格
|
|
|
|
|
int index = snippet.indexOf('\n'); // 查找换行符位置
|
|
|
|
|
if (index != -1) { // 如果存在换行符
|
|
|
|
|
snippet = snippet.substring(0, index); // 截取第一行
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return snippet;
|
|
|
|
|
return snippet; // 返回格式化后的摘要
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|