Update DataUtils.java

pull/5/head
pgf78rp5y 2 years ago
parent 1d5bd22965
commit 13de4eccc8

@ -16,6 +16,9 @@
package net.micode.notes.tool;
import java.util.ArrayList;
import java.util.HashSet;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
@ -25,19 +28,16 @@ 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";
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {// 直接删除多个笔记
if (ids == null) {
Log.d(TAG, "the ids is null");
return true;
@ -47,36 +47,37 @@ 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) {
if (id == Notes.ID_ROOT_FOLDER) {// 如果发现是根文件夹则不执行删除操作并且log提示
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);
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;
Log.d(TAG, "delete notes failed, ids:" + ids.toString());//log提醒ids为当下字符串的notes删除失败
return false;//返回失败
}
return true;
} catch (RemoteException e) {
return true;//返回成功
} catch (RemoteException e) {//出现远程异常
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()));
}
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);
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {// 对需要移动的便签进行数据更新然后用update实现
ContentValues values = new ContentValues();//new一个只能存储基本类型的数据的contentValues
values.put(NoteColumns.PARENT_ID, desFolderId);//使用put插入数据
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);//将移动前后的FolderId都插入
values.put(NoteColumns.LOCAL_MODIFIED, 1);//标记此folder为已修改
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
@ -90,22 +91,22 @@ 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 {
try { // applyBatch一次性处理一个操作列表
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false;
}
return true;
} catch (RemoteException e) {
} catch (RemoteException e) {//RemoteException 由于系统断电,内存不足等原因导致dataNode丢失超过设置的丢失百分比,系统自动进入安全模式
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()));
}
return false;
@ -119,14 +120,14 @@ public class DataUtils {
new String[] { "COUNT(*)" },
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) },
null);
null); // applyBatch一次性处理一个操作列表
int count = 0;
if (cursor != null) {
if (cursor.moveToFirst()) {
try {
count = cursor.getInt(0);
} catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {//索引边界异常
Log.e(TAG, "get folder count failed:" + e.toString());
} finally {
cursor.close();
@ -137,11 +138,12 @@ public class DataUtils {
}
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,
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
new String[] { String.valueOf(type) },
null);
null);// 查询条件type符合且不属于垃圾文件夹
boolean exist = false;
if (cursor != null) {
@ -159,7 +161,7 @@ public class DataUtils {
boolean exist = false;
if (cursor != null) {
if (cursor.getCount() > 0) {
if (cursor.getCount() > 0) {// 用getcount函数判断cursor是否为空
exist = true;
}
cursor.close();
@ -188,6 +190,7 @@ public class DataUtils {
" AND " + NoteColumns.SNIPPET + "=?",
new String[] { name }, null);
boolean exist = false;
// 通过名字查询文件是否存在
if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
@ -202,7 +205,7 @@ public class DataUtils {
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,7 +214,7 @@ public class DataUtils {
do {
try {
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetId = c.getInt(0);// 0对应的NoteColumns.WIDGET_I1对应的NoteColumns.WIDGET_TYPE
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) {
@ -249,12 +252,12 @@ 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);// 通过数据库操作查询条件是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());
}
@ -282,9 +285,10 @@ public class DataUtils {
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) {
snippet = snippet.trim();
int index = snippet.indexOf('\n');
if (index != -1) {
snippet = snippet.substring(0, index);

Loading…
Cancel
Save