From 13de4eccc83213648de5224099b4fc190f1c42bd Mon Sep 17 00:00:00 2001 From: pgf78rp5y <1336406704@qq.com> Date: Wed, 6 Dec 2023 09:22:43 +0800 Subject: [PATCH] Update DataUtils.java --- src/src/net/micode/notes/tool/DataUtils.java | 108 ++++++++++--------- 1 file changed, 56 insertions(+), 52 deletions(-) diff --git a/src/src/net/micode/notes/tool/DataUtils.java b/src/src/net/micode/notes/tool/DataUtils.java index 2a14982..faf01cf 100644 --- a/src/src/net/micode/notes/tool/DataUtils.java +++ b/src/src/net/micode/notes/tool/DataUtils.java @@ -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 ids) { + + public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) {// 直接删除多个笔记 + if (ids == null) { Log.d(TAG, "the ids is null"); return true; @@ -47,36 +47,37 @@ public class DataUtils { return true; } - ArrayList operationList = new ArrayList(); + ArrayList operationList = new ArrayList();// 提供一个任务列表 + 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 operationList = new ArrayList(); 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; @@ -115,18 +116,18 @@ 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, + 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); + new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) }, + null); // applyBatch一次性处理一个操作列表 int count = 0; - if(cursor != null) { - if(cursor.moveToFirst()) { + 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); + new String[] { String.valueOf(type) }, + 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(); @@ -184,12 +186,13 @@ public class DataUtils { 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 + "=?", + " 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) { + // 通过名字查询文件是否存在 + if (cursor != null) { + if (cursor.getCount() > 0) { exist = true; } cursor.close(); @@ -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 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_I,1对应的NoteColumns.WIDGET_TYPE widget.widgetType = c.getInt(1); set.add(widget); } catch (IndexOutOfBoundsException e) { @@ -226,9 +229,9 @@ public class DataUtils { public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { 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 + "=?", - new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, + new String[] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, null); if (cursor != null && cursor.moveToFirst()) { @@ -245,16 +248,16 @@ public class DataUtils { 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 }, - null); + + CallNote.PHONE_NUMBER + ",?)", + new String[] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, + 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()); } @@ -266,9 +269,9 @@ public class DataUtils { 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) { @@ -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);