|
|
|
@ -16,15 +16,15 @@
|
|
|
|
|
|
|
|
|
|
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;//导入 Android 内容提供者操作(ContentProviderOperation)类,用于操作内容提供者中的数据。
|
|
|
|
|
import android.content.ContentProviderResult;//导入 Android 内容提供者结果(ContentProviderResult)类,用于存储内容提供者操作的结果。
|
|
|
|
|
import android.content.ContentResolver;//导入 Android 内容解析器(ContentResolver)类,用于访问应用程序中的各种内容提供者。
|
|
|
|
|
import android.content.ContentUris;//导入 Android 内容 URI(ContentUris)类,用于处理内容提供者的 URI。
|
|
|
|
|
import android.content.ContentValues;//导入 Android 内容值(ContentValues)类,用于封装要插入或更新到内容提供者中的数据。
|
|
|
|
|
import android.content.OperationApplicationException;//导入 Android 操作应用程序异常(OperationApplicationException)类,用于处理内容提供者操作时可能出现的异常情况。
|
|
|
|
|
import android.database.Cursor;//导入 Android 数据库查询结果集(Cursor)类,用于表示数据库查询结果集。
|
|
|
|
|
import android.os.RemoteException;//导入 Android 远程操作异常(RemoteException)类,用于处理远程操作时可能出现的异常情况。
|
|
|
|
|
import android.util.Log;//导入 Android 日志(Log)类,用于记录调试信息。
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.CallNote;
|
|
|
|
@ -37,6 +37,8 @@ import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
public class DataUtils {
|
|
|
|
|
public static final String TAG = "DataUtils";
|
|
|
|
|
|
|
|
|
|
//删除多个note
|
|
|
|
|
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
|
Log.d(TAG, "the ids is null");
|
|
|
|
@ -47,19 +49,20 @@ public class DataUtils {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
} //如果发现是根文件夹,则不删除
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); //用newDelete实现删除功能
|
|
|
|
|
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);//主机名(或叫Authority)用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
|
|
|
|
|
//数据库事务,数据库事务是由一组数据库操作序列组成,事务作为一个整体被执行
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {//若为空则没必要删
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -72,14 +75,16 @@ public class DataUtils {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//将便签移动至文件夹内
|
|
|
|
|
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);
|
|
|
|
|
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);//对需要移动的便签进行数据更新,然后用update实现
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//批量移动
|
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
|
|
long folderId) {
|
|
|
|
|
if (ids == null) {
|
|
|
|
@ -90,14 +95,14 @@ public class DataUtils {
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
|
for (long id : ids) {
|
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
|
|
|
|
|
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));//通过withAppendedId方法,为该Uri加上ID
|
|
|
|
|
builder.withValue(NoteColumns.PARENT_ID, folderId);
|
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
|
operationList.add(builder.build());
|
|
|
|
|
}
|
|
|
|
|
}//将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); //applyBatch一次性处理一个操作列表
|
|
|
|
|
if (results == null || results.length == 0 || results[0] == null) {
|
|
|
|
|
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
|
|
|
|
|
return false;
|
|
|
|
@ -114,12 +119,14 @@ public class DataUtils {
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
null);//筛选条件:源文件不为trash folder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
if(cursor != null) {
|
|
|
|
@ -136,23 +143,24 @@ public class DataUtils {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查看是否有指定ID的note可见
|
|
|
|
|
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);
|
|
|
|
|
null);//查询条件:type符合,且不属于垃圾文件夹
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
|
if (cursor.getCount() > 0) {//用getcount函数判断cursor是否为空
|
|
|
|
|
exist = true;
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///判定是否有指定note,即不需条件条件:type符合,且不属于垃圾文件夹
|
|
|
|
|
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
@ -166,7 +174,7 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判定是否有指定的data
|
|
|
|
|
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
|
|
|
|
|
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId),
|
|
|
|
|
null, null, null, null);
|
|
|
|
@ -180,13 +188,14 @@ public class DataUtils {
|
|
|
|
|
}
|
|
|
|
|
return exist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判定是否有指定的可见folder
|
|
|
|
|
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) {
|
|
|
|
@ -197,12 +206,13 @@ 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 },
|
|
|
|
|
NoteColumns.PARENT_ID + "=?",
|
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
|
null);
|
|
|
|
|
null);//查询条件:父ID是传入的folderId;
|
|
|
|
|
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
|
if (c != null) {
|
|
|
|
@ -211,8 +221,8 @@ public class DataUtils {
|
|
|
|
|
do {
|
|
|
|
|
try {
|
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
|
|
widget.widgetId = c.getInt(0);
|
|
|
|
|
widget.widgetType = c.getInt(1);
|
|
|
|
|
widget.widgetId = c.getInt(0);//0对应的NoteColumns.WIDGET_ID
|
|
|
|
|
widget.widgetType = c.getInt(1);//1对应的NoteColumns.WIDGET_TYPE
|
|
|
|
|
set.add(widget);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
@ -224,20 +234,21 @@ public class DataUtils {
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//根据给定的便签ID(noteId)获取该便签中的电话号码(CallNote.PHONE_NUMBER)
|
|
|
|
|
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);
|
|
|
|
|
null);//在查询条件中限定了便签ID(NoteId)和便签类型(CallNote.MIME_TYPE),使用了占位符(?)和参数数组,避免了SQL注入攻击
|
|
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {//如果查询结果非空且有记录,则尝试获取第一条记录的电话号码,并将其返回。
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());
|
|
|
|
|
Log.e(TAG, "Get call number fails " + e.toString());//如果获取电话号码失败,则在日志中打印错误信息,并返回空字符串
|
|
|
|
|
} finally {
|
|
|
|
|
cursor.close();
|
|
|
|
|
cursor.close();//关闭游标(Cursor)对象
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
@ -249,19 +260,19 @@ public class DataUtils {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
null);//在查询条件中限定了通话日期(CallNote.CALL_DATE)、便签类型(CallNote.MIME_TYPE)以及电话号码(CallNote.PHONE_NUMBER)。//使用了占位符(?)和参数数组来避免SQL注入攻击,并使用PHONE_NUMBERS_EQUAL函数来比较电话号码。
|
|
|
|
|
//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值)
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
|
try {
|
|
|
|
|
return cursor.getLong(0);
|
|
|
|
|
return cursor.getLong(0);//0对应的CallNote.NOTE_ID
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.close();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
return 0;//如果查询结果为空,则返回0作为默认值。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getSnippetById(ContentResolver resolver, long noteId) {
|
|
|
|
@ -269,8 +280,8 @@ public class DataUtils {
|
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
null);//查询条件:noteId
|
|
|
|
|
//如果查询结果非空,则尝试获取第一条记录的便签片段,并将其赋值给变量snippet。
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
String snippet = "";
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
@ -279,17 +290,17 @@ public class DataUtils {
|
|
|
|
|
cursor.close();
|
|
|
|
|
return snippet;
|
|
|
|
|
}
|
|
|
|
|
throw new IllegalArgumentException("Note is not found with id: " + noteId);
|
|
|
|
|
throw new IllegalArgumentException("Note is not found with id: " + noteId);//如果查询结果为空,则抛出IllegalArgumentException异常,提示便签ID不存在。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getFormattedSnippet(String snippet) {
|
|
|
|
|
if (snippet != null) {
|
|
|
|
|
snippet = snippet.trim();
|
|
|
|
|
int index = snippet.indexOf('\n');
|
|
|
|
|
public static String getFormattedSnippet(String snippet) { //对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉
|
|
|
|
|
if (snippet != null) {//首先判断传入的便签片段(snippet)是否为空。如果不为空,则执行下一步;否则直接返回空字符串
|
|
|
|
|
snippet = snippet.trim();//对便签片段进行trim操作,去除字符串前后的空格。
|
|
|
|
|
int index = snippet.indexOf('\n');//查找便签片段中第一个换行符(\n)的索引位置,如果找到了,则截取从开头到该索引位置的子字符串;否则保留原始字符串。
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
snippet = snippet.substring(0, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return snippet;
|
|
|
|
|
return snippet;//返回格式化后的便签片段。
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|