@ -35,10 +35,10 @@ import java.util.ArrayList;
import java.util.HashSet ;
import java.util.HashSet ;
public class DataUtils {
public class DataUtils { //定义便签的处理工具类
public static final String TAG = "DataUtils" ;
public static final String TAG = "DataUtils" ;
public static boolean batchDeleteNotes ( ContentResolver resolver , HashSet < Long > ids ) {
public static boolean batchDeleteNotes ( ContentResolver resolver , HashSet < Long > ids ) { //一个处理批量删除便签的方法
if ( ids = = null ) {
if ( ids = = null ) { //判断笔记是否为空
Log . d ( TAG , "the ids is null" ) ;
Log . d ( TAG , "the ids is null" ) ;
return true ;
return true ;
}
}
@ -48,23 +48,23 @@ public class DataUtils {
}
}
ArrayList < ContentProviderOperation > operationList = new ArrayList < ContentProviderOperation > ( ) ;
ArrayList < ContentProviderOperation > operationList = new ArrayList < ContentProviderOperation > ( ) ;
for ( long id : ids ) {
for ( long id : ids ) { //遍历便签id
if ( id = = Notes . ID_ROOT_FOLDER ) {
if ( id = = Notes . ID_ROOT_FOLDER ) {
Log . e ( TAG , "Don't delete system folder root" ) ;
Log . e ( TAG , "Don't delete system folder root" ) ; //.如果是根目录的文件夹, 输出Don't delete system folder root
continue ;
continue ;
}
}
ContentProviderOperation . Builder builder = ContentProviderOperation
ContentProviderOperation . Builder builder = ContentProviderOperation //批量的删除对应的ID
. newDelete ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) ) ;
. newDelete ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) ) ;
operationList . add ( builder . build ( ) ) ;
operationList . add ( builder . build ( ) ) ;
}
}
try {
try { //返回被删除的数据, 如果返回为空则删除失败返回false, 打印异常信息, 删除成功返回true
ContentProviderResult [ ] results = resolver . applyBatch ( Notes . AUTHORITY , operationList ) ;
ContentProviderResult [ ] results = resolver . applyBatch ( Notes . AUTHORITY , operationList ) ;
if ( results = = null | | results . length = = 0 | | results [ 0 ] = = null ) {
if ( results = = null | | results . length = = 0 | | results [ 0 ] = = null ) {
Log . d ( TAG , "delete notes failed, ids:" + ids . toString ( ) ) ;
Log . d ( TAG , "delete notes failed, ids:" + ids . toString ( ) ) ;
return false ;
return false ;
}
}
return true ;
return true ;
} catch ( RemoteException e ) {
} catch ( RemoteException e ) { //错误处理
Log . e ( TAG , String . format ( "%s: %s" , e . toString ( ) , e . getMessage ( ) ) ) ;
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 ( ) ) ) ;
Log . e ( TAG , String . format ( "%s: %s" , e . toString ( ) , e . getMessage ( ) ) ) ;
@ -73,6 +73,7 @@ public class DataUtils {
}
}
public static void moveNoteToFoler ( ContentResolver resolver , long id , long srcFolderId , long desFolderId ) {
public static void moveNoteToFoler ( ContentResolver resolver , long id , long srcFolderId , long desFolderId ) {
//将某便签移动到另一个目录下
ContentValues values = new ContentValues ( ) ;
ContentValues values = new ContentValues ( ) ;
values . put ( NoteColumns . PARENT_ID , desFolderId ) ;
values . put ( NoteColumns . PARENT_ID , desFolderId ) ;
values . put ( NoteColumns . ORIGIN_PARENT_ID , srcFolderId ) ;
values . put ( NoteColumns . ORIGIN_PARENT_ID , srcFolderId ) ;
@ -80,7 +81,7 @@ public class DataUtils {
resolver . update ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) , values , null , null ) ;
resolver . update ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) , values , null , null ) ;
}
}
public static boolean batchMoveToFolder ( ContentResolver resolver , HashSet < Long > ids ,
public static boolean batchMoveToFolder ( ContentResolver resolver , HashSet < Long > ids , //批量移动
long folderId ) {
long folderId ) {
if ( ids = = null ) {
if ( ids = = null ) {
Log . d ( TAG , "the ids is null" ) ;
Log . d ( TAG , "the ids is null" ) ;
@ -88,7 +89,7 @@ public class DataUtils {
}
}
ArrayList < ContentProviderOperation > operationList = new ArrayList < ContentProviderOperation > ( ) ;
ArrayList < ContentProviderOperation > operationList = new ArrayList < ContentProviderOperation > ( ) ;
for ( long id : ids ) {
for ( long id : ids ) { //遍历便签ID
ContentProviderOperation . Builder builder = ContentProviderOperation
ContentProviderOperation . Builder builder = ContentProviderOperation
. newUpdate ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) ) ;
. newUpdate ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , id ) ) ;
builder . withValue ( NoteColumns . PARENT_ID , folderId ) ;
builder . withValue ( NoteColumns . PARENT_ID , folderId ) ;
@ -96,7 +97,7 @@ public class DataUtils {
operationList . add ( builder . build ( ) ) ;
operationList . add ( builder . build ( ) ) ;
}
}
try {
try { //错误检测
ContentProviderResult [ ] results = resolver . applyBatch ( Notes . AUTHORITY , operationList ) ;
ContentProviderResult [ ] results = resolver . applyBatch ( Notes . AUTHORITY , operationList ) ;
if ( results = = null | | results . length = = 0 | | results [ 0 ] = = null ) {
if ( results = = null | | results . length = = 0 | | results [ 0 ] = = null ) {
Log . d ( TAG , "delete notes failed, ids:" + ids . toString ( ) ) ;
Log . d ( TAG , "delete notes failed, ids:" + ids . toString ( ) ) ;
@ -114,7 +115,7 @@ public class DataUtils {
/ * *
/ * *
* Get the all folder count except system folders { @link Notes # TYPE_SYSTEM } }
* Get the all folder count except system folders { @link Notes # TYPE_SYSTEM } }
* /
* /
public static int getUserFolderCount ( ContentResolver resolver ) {
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(*)" } ,
new String [ ] { "COUNT(*)" } ,
NoteColumns . TYPE + "=? AND " + NoteColumns . PARENT_ID + "<>?" ,
NoteColumns . TYPE + "=? AND " + NoteColumns . PARENT_ID + "<>?" ,
@ -136,14 +137,14 @@ public class DataUtils {
return count ;
return count ;
}
}
public static boolean visibleInNoteDatabase ( ContentResolver resolver , long noteId , int type ) {
public static boolean visibleInNoteDatabase ( ContentResolver resolver , long noteId , int type ) { //在数据库中是否可见
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , noteId ) ,
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , noteId ) ,
null ,
null ,
NoteColumns . TYPE + "=? AND " + NoteColumns . PARENT_ID + "<>" + Notes . ID_TRASH_FOLER ,
NoteColumns . TYPE + "=? AND " + NoteColumns . PARENT_ID + "<>" + Notes . ID_TRASH_FOLER ,
new String [ ] { String . valueOf ( type ) } ,
new String [ ] { String . valueOf ( type ) } ,
null ) ;
null ) ;
boolean exist = false ;
boolean exist = false ; //判断note是否在数据库中, 并返回相应的布尔值
if ( cursor ! = null ) {
if ( cursor ! = null ) {
if ( cursor . getCount ( ) > 0 ) {
if ( cursor . getCount ( ) > 0 ) {
exist = true ;
exist = true ;
@ -153,7 +154,7 @@ public class DataUtils {
return exist ;
return exist ;
}
}
public static boolean existInNoteDatabase ( ContentResolver resolver , long noteId ) {
public static boolean existInNoteDatabase ( ContentResolver resolver , long noteId ) { //通过便签ID搜索, 判断该note是否在数据库中存在
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , noteId ) ,
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_NOTE_URI , noteId ) ,
null , null , null , null ) ;
null , null , null , null ) ;
@ -167,7 +168,7 @@ public class DataUtils {
return exist ;
return exist ;
}
}
public static boolean existInDataDatabase ( ContentResolver resolver , long dataId ) {
public static boolean existInDataDatabase ( ContentResolver resolver , long dataId ) { //通过名字查询是否在数据库中存在
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_DATA_URI , dataId ) ,
Cursor cursor = resolver . query ( ContentUris . withAppendedId ( Notes . CONTENT_DATA_URI , dataId ) ,
null , null , null , null ) ;
null , null , null , null ) ;
@ -181,7 +182,7 @@ public class DataUtils {
return exist ;
return exist ;
}
}
public static boolean checkVisibleFolderName ( ContentResolver resolver , String name ) {
public static boolean checkVisibleFolderName ( ContentResolver resolver , String name ) { //检查文件名以获取文件是否存在
Cursor cursor = resolver . query ( Notes . CONTENT_NOTE_URI , null ,
Cursor cursor = resolver . query ( Notes . CONTENT_NOTE_URI , null ,
NoteColumns . TYPE + "=" + Notes . TYPE_FOLDER +
NoteColumns . TYPE + "=" + Notes . TYPE_FOLDER +
" AND " + NoteColumns . PARENT_ID + "<>" + Notes . ID_TRASH_FOLER +
" AND " + NoteColumns . PARENT_ID + "<>" + Notes . ID_TRASH_FOLER +
@ -197,7 +198,7 @@ public class DataUtils {
return exist ;
return exist ;
}
}
public static HashSet < AppWidgetAttribute > getFolderNoteWidget ( ContentResolver resolver , long folderId ) {
public static HashSet < AppWidgetAttribute > getFolderNoteWidget ( ContentResolver resolver , long folderId ) { //获得文件夹窗口小部件
Cursor c = resolver . query ( Notes . CONTENT_NOTE_URI ,
Cursor c = resolver . query ( Notes . CONTENT_NOTE_URI ,
new String [ ] { NoteColumns . WIDGET_ID , NoteColumns . WIDGET_TYPE } ,
new String [ ] { NoteColumns . WIDGET_ID , NoteColumns . WIDGET_TYPE } ,
NoteColumns . PARENT_ID + "=?" ,
NoteColumns . PARENT_ID + "=?" ,
@ -209,12 +210,12 @@ public class DataUtils {
if ( c . moveToFirst ( ) ) {
if ( c . moveToFirst ( ) ) {
set = new HashSet < AppWidgetAttribute > ( ) ;
set = new HashSet < AppWidgetAttribute > ( ) ;
do {
do {
try {
try { //把每一个条目对应的窗口id和type记录下来, 放到set里面。
AppWidgetAttribute widget = new AppWidgetAttribute ( ) ;
AppWidgetAttribute widget = new AppWidgetAttribute ( ) ;
widget . widgetId = c . getInt ( 0 ) ;
widget . widgetId = c . getInt ( 0 ) ;
widget . widgetType = c . getInt ( 1 ) ;
widget . widgetType = c . getInt ( 1 ) ;
set . add ( widget ) ;
set . add ( widget ) ;
} catch ( IndexOutOfBoundsException e ) {
} catch ( IndexOutOfBoundsException e ) { //当下标超过边界,那么返回错误
Log . e ( TAG , e . toString ( ) ) ;
Log . e ( TAG , e . toString ( ) ) ;
}
}
} while ( c . moveToNext ( ) ) ;
} while ( c . moveToNext ( ) ) ;
@ -224,14 +225,14 @@ public class DataUtils {
return set ;
return set ;
}
}
public static String getCallNumberByNoteId ( ContentResolver resolver , long noteId ) {
public static String getCallNumberByNoteId ( ContentResolver resolver , long noteId ) { //通过笔记ID获取号码
Cursor cursor = resolver . query ( Notes . CONTENT_DATA_URI ,
Cursor cursor = resolver . query ( Notes . CONTENT_DATA_URI ,
new String [ ] { CallNote . PHONE_NUMBER } ,
new String [ ] { CallNote . PHONE_NUMBER } ,
CallNote . NOTE_ID + "=? AND " + CallNote . MIME_TYPE + "=?" ,
CallNote . NOTE_ID + "=? AND " + CallNote . MIME_TYPE + "=?" ,
new String [ ] { String . valueOf ( noteId ) , CallNote . CONTENT_ITEM_TYPE } ,
new String [ ] { String . valueOf ( noteId ) , CallNote . CONTENT_ITEM_TYPE } ,
null ) ;
null ) ;
if ( cursor ! = null & & cursor . moveToFirst ( ) ) {
if ( cursor ! = null & & cursor . moveToFirst ( ) ) { //获取电话号码,并处理异常
try {
try {
return cursor . getString ( 0 ) ;
return cursor . getString ( 0 ) ;
} catch ( IndexOutOfBoundsException e ) {
} catch ( IndexOutOfBoundsException e ) {
@ -243,7 +244,7 @@ public class DataUtils {
return "" ;
return "" ;
}
}
public static long getNoteIdByPhoneNumberAndCallDate ( ContentResolver resolver , String phoneNumber , long callDate ) {
public static long getNoteIdByPhoneNumberAndCallDate ( ContentResolver resolver , String phoneNumber , long callDate ) { //通过号码和日期获取ID
Cursor cursor = resolver . query ( Notes . CONTENT_DATA_URI ,
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 . CALL_DATE + "=? AND " + CallNote . MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
@ -264,7 +265,7 @@ public class DataUtils {
return 0 ;
return 0 ;
}
}
public static String getSnippetById ( ContentResolver resolver , long noteId ) {
public static String getSnippetById ( ContentResolver resolver , long noteId ) { //通过Note的Id获取Note的片段
Cursor cursor = resolver . query ( Notes . CONTENT_NOTE_URI ,
Cursor cursor = resolver . query ( Notes . CONTENT_NOTE_URI ,
new String [ ] { NoteColumns . SNIPPET } ,
new String [ ] { NoteColumns . SNIPPET } ,
NoteColumns . ID + "=?" ,
NoteColumns . ID + "=?" ,
@ -282,7 +283,7 @@ public class DataUtils {
throw new IllegalArgumentException ( "Note is not found with id: " + noteId ) ;
throw new IllegalArgumentException ( "Note is not found with id: " + noteId ) ;
}
}
public static String getFormattedSnippet ( String snippet ) {
public static String getFormattedSnippet ( String snippet ) { //格式化,对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉
if ( snippet ! = null ) {
if ( snippet ! = null ) {
snippet = snippet . trim ( ) ;
snippet = snippet . trim ( ) ;
int index = snippet . indexOf ( '\n' ) ;
int index = snippet . indexOf ( '\n' ) ;