|
|
|
@ -1,42 +1,16 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量删除笔记。
|
|
|
|
|
*
|
|
|
|
|
* @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");
|
|
|
|
@ -72,6 +46,14 @@ 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);
|
|
|
|
@ -80,6 +62,14 @@ 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) {
|
|
|
|
@ -112,7 +102,10 @@ 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,
|
|
|
|
@ -136,6 +129,14 @@ 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,
|
|
|
|
@ -153,6 +154,13 @@ 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);
|
|
|
|
@ -167,6 +175,13 @@ 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);
|
|
|
|
@ -181,6 +196,13 @@ 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 +
|
|
|
|
@ -197,6 +219,13 @@ 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 },
|
|
|
|
@ -224,6 +253,13 @@ 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 },
|
|
|
|
@ -243,6 +279,14 @@ 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 },
|
|
|
|
@ -264,6 +308,14 @@ public class DataUtils {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据笔记ID获取笔记摘要。
|
|
|
|
|
*
|
|
|
|
|
* @param resolver ContentResolver对象,用于与内容提供者进行交互
|
|
|
|
|
* @param noteId 笔记ID
|
|
|
|
|
* @return 笔记摘要
|
|
|
|
|
* @throws IllegalArgumentException 如果未找到指定ID的笔记
|
|
|
|
|
*/
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
@ -282,6 +334,12 @@ 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();
|
|
|
|
@ -292,4 +350,4 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|