|
|
@ -35,9 +35,11 @@ import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
@ -47,18 +49,19 @@ public class DataUtils {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); //提供一个任务列表
|
|
|
|
for (long id : ids) {
|
|
|
|
for (long id : ids) {
|
|
|
|
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");
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} //如果发现是根文件夹,则不删除
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
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());
|
|
|
|
operationList.add(builder.build()); //
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
|
|
|
|
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);//主机名(或叫Authority)用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
|
|
|
|
|
|
|
|
//数据库事务,数据库事务是由一组数据库操作序列组成,事务作为一个整体被执行
|
|
|
|
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;
|
|
|
@ -77,7 +80,7 @@ public class DataUtils {
|
|
|
|
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);
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
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,
|
|
|
|
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
|
|
|
@ -90,14 +93,14 @@ public class DataUtils {
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
|
|
|
|
for (long id : ids) {
|
|
|
|
for (long id : ids) {
|
|
|
|
ContentProviderOperation.Builder builder = ContentProviderOperation
|
|
|
|
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.PARENT_ID, folderId);
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1);
|
|
|
|
operationList.add(builder.build());
|
|
|
|
operationList.add(builder.build());
|
|
|
|
}
|
|
|
|
}//将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
@ -119,7 +122,7 @@ public class DataUtils {
|
|
|
|
new String[] { "COUNT(*)" },
|
|
|
|
new String[] { "COUNT(*)" },
|
|
|
|
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
|
|
|
|
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);
|
|
|
|
null); //筛选条件:源文件不为trash folder
|
|
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
|
if(cursor != null) {
|
|
|
|
if(cursor != null) {
|
|
|
@ -137,15 +140,15 @@ public class DataUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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), //通过withAppendedId方法,为该Uri加上ID
|
|
|
|
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); //查询条件:type符合,且不属于垃圾文件夹
|
|
|
|
|
|
|
|
|
|
|
|
boolean exist = false;
|
|
|
|
boolean exist = false;
|
|
|
|
if (cursor != null) {
|
|
|
|
if (cursor != null) {
|
|
|
|
if (cursor.getCount() > 0) {
|
|
|
|
if (cursor.getCount() > 0) {//用getcount函数判断cursor是否为空
|
|
|
|
exist = true;
|
|
|
|
exist = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor.close();
|
|
|
|
cursor.close();
|
|
|
@ -187,6 +190,7 @@ public class DataUtils {
|
|
|
|
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER +
|
|
|
|
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER +
|
|
|
|
" AND " + NoteColumns.SNIPPET + "=?",
|
|
|
|
" AND " + NoteColumns.SNIPPET + "=?",
|
|
|
|
new String[] { name }, null);
|
|
|
|
new String[] { name }, null);
|
|
|
|
|
|
|
|
//通过名字查询文件是否存在
|
|
|
|
boolean exist = false;
|
|
|
|
boolean exist = false;
|
|
|
|
if(cursor != null) {
|
|
|
|
if(cursor != null) {
|
|
|
|
if(cursor.getCount() > 0) {
|
|
|
|
if(cursor.getCount() > 0) {
|
|
|
@ -202,7 +206,7 @@ public class DataUtils {
|
|
|
|
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
|
|
|
|
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
|
|
|
|
NoteColumns.PARENT_ID + "=?",
|
|
|
|
NoteColumns.PARENT_ID + "=?",
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
new String[] { String.valueOf(folderId) },
|
|
|
|
null);
|
|
|
|
null); //查询条件:父ID是传入的folderId;
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
HashSet<AppWidgetAttribute> set = null;
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
@ -211,13 +215,13 @@ public class DataUtils {
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
|
widget.widgetId = c.getInt(0);
|
|
|
|
widget.widgetId = c.getInt(0); //0对应的NoteColumns.WIDGET_ID
|
|
|
|
widget.widgetType = c.getInt(1);
|
|
|
|
widget.widgetType = c.getInt(1); //1对应的NoteColumns.WIDGET_TYPE
|
|
|
|
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()); //查询下一条
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -250,11 +254,12 @@ public class DataUtils {
|
|
|
|
+ CallNote.PHONE_NUMBER + ",?)",
|
|
|
|
+ CallNote.PHONE_NUMBER + ",?)",
|
|
|
|
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
|
|
|
|
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber },
|
|
|
|
null);
|
|
|
|
null);
|
|
|
|
|
|
|
|
//通过数据库操作,查询条件是(callDate和phoneNumber匹配传入参数的值)
|
|
|
|
|
|
|
|
|
|
|
|
if (cursor != null) {
|
|
|
|
if (cursor != null) {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return cursor.getLong(0);
|
|
|
|
return cursor.getLong(0); //0对应的CallNote.NOTE_ID
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
Log.e(TAG, "Get call note id fails " + e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -269,7 +274,7 @@ public class DataUtils {
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
new String [] { NoteColumns.SNIPPET },
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
NoteColumns.ID + "=?",
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
new String [] { String.valueOf(noteId)},
|
|
|
|
null);
|
|
|
|
null);//查询条件:noteId
|
|
|
|
|
|
|
|
|
|
|
|
if (cursor != null) {
|
|
|
|
if (cursor != null) {
|
|
|
|
String snippet = "";
|
|
|
|
String snippet = "";
|
|
|
@ -281,8 +286,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');
|
|
|
@ -292,4 +296,5 @@ public class DataUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return snippet;
|
|
|
|
return snippet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|