|
|
|
@ -1,16 +1,17 @@
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* 根据Apache License, Version 2.0(以下简称“许可证”)授权;
|
|
|
|
|
* 除非遵守许可证,否则不得使用此文件。
|
|
|
|
|
* 你可以在以下网址获得许可证的副本:
|
|
|
|
|
* 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;
|
|
|
|
@ -33,25 +34,25 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
// 数据工具类,提供对笔记数据的操作函数
|
|
|
|
|
// 数据工具类,提供批量删除笔记、移动笔记到文件夹等操作
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
|
|
|
|
|
// 批量删除笔记
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "id集合为空");
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (ids.size() == 0) {
|
|
|
|
|
Log.d(TAG, "id集合中没有元素");
|
|
|
|
|
Log.d(TAG, "no id is in the hashset");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "不要删除系统根文件夹");
|
|
|
|
|
if(id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Don't delete system folder root");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
@ -61,7 +62,7 @@ public class DataUtils {
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "删除笔记失败, ids:" + ids.toString());
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -86,7 +87,7 @@ public class DataUtils {
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "id集合为空");
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,7 +103,7 @@ public class DataUtils {
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "移动笔记失败, ids:" + ids.toString());
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -115,22 +116,22 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取除了系统文件夹外的所有文件夹数量
|
|
|
|
|
* 获取用户文件夹(非系统文件夹)的数量
|
|
|
|
|
*/
|
|
|
|
|
public static int getUserFolderCount(ContentResolver resolver) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
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) },
|
|
|
|
|
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)},
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
if(cursor != null) {
|
|
|
|
|
if(cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
count = cursor.getInt(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "获取文件夹数量失败:" + e.toString());
|
|
|
|
|
Log.e(TAG, "get folder count failed:" + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
@ -139,12 +140,12 @@ public class DataUtils {
|
|
|
|
|
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) },
|
|
|
|
|
new String [] {String.valueOf(type)},
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
@ -157,7 +158,7 @@ public class DataUtils {
|
|
|
|
|
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);
|
|
|
|
@ -172,7 +173,7 @@ public class DataUtils {
|
|
|
|
|
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);
|
|
|
|
@ -187,7 +188,7 @@ public class DataUtils {
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查文件夹名称是否可见
|
|
|
|
|
// 检查指定名称的用户文件夹是否存在
|
|
|
|
|
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null,
|
|
|
|
|
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
|
|
|
|
@ -195,8 +196,8 @@ public class DataUtils {
|
|
|
|
|
" AND " + NoteColumns.SNIPPET + "=?",
|
|
|
|
|
new String[] { name }, null);
|
|
|
|
|
boolean exist = false;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
|
if(cursor != null) {
|
|
|
|
|
if(cursor.getCount() > 0) {
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
@ -204,7 +205,7 @@ public class DataUtils {
|
|
|
|
|
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 },
|
|
|
|
@ -240,74 +241,68 @@ public class DataUtils {
|
|
|
|
|
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果游标不为空并且移动到第一行,则尝试获取通话号码
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getString(0); // 返回获取到的字符串
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "获取通话号码失败:" + e.toString()); // 记录异常信息
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close(); // 确保游标被关闭
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""; // 如果失败,返回空字符串
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据电话号码和通话日期获取笔记ID
|
|
|
|
|
// 根据通话号码和通话日期获取笔记ID
|
|
|
|
|
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
new String[] { CallNote.NOTE_ID },
|
|
|
|
|
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 },
|
|
|
|
|
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 如果游标不为空并且移动到第一行,则尝试获取笔记ID
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getLong(0); // 返回获取到的长整型数
|
|
|
|
|
return cursor.getLong(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "获取通话笔记ID失败:" + e.toString()); // 记录异常信息
|
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.close(); // 确保游标被关闭
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
return 0; // 如果失败,返回0
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据笔记ID获取摘要
|
|
|
|
|
// 根据笔记ID获取笔记摘要
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
new String[] { NoteColumns.SNIPPET },
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
|
new String[] { String.valueOf(noteId) },
|
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
// 如果游标不为空并且移动到第一行,则尝试获取摘要
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
String snippet = "";
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
snippet = cursor.getString(0); // 获取摘要
|
|
|
|
|
snippet = cursor.getString(0);
|
|
|
|
|
}
|
|
|
|
|
cursor.close(); // 确保游标被关闭
|
|
|
|
|
return snippet; // 返回摘要
|
|
|
|
|
cursor.close();
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
throw new IllegalArgumentException("未找到ID为:" + noteId + "的笔记"); // 如果失败,抛出异常
|
|
|
|
|
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'); // 查找换行符
|
|
|
|
|
snippet = snippet.trim();
|
|
|
|
|
int index = snippet.indexOf('\n');
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
snippet = snippet.substring(0, index); // 截取换行符之前的字符串
|
|
|
|
|
snippet = snippet.substring(0, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return snippet; // 返回格式化后的摘要
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
}
|