diff --git a/app/src/main/java/net/micode/notes/tool/DataUtils.java b/app/src/main/java/net/micode/notes/tool/DataUtils.java index 2a14982..cf8b78f 100644 --- a/app/src/main/java/net/micode/notes/tool/DataUtils.java +++ b/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -39,28 +39,29 @@ public class DataUtils { public static final String TAG = "DataUtils"; public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { if (ids == null) { - Log.d(TAG, "the ids is null"); + Log.d(TAG, "the ids is null");//IDS是空 return true; } if (ids.size() == 0) { - Log.d(TAG, "no id is in the hashset"); + Log.d(TAG, "no id is in the hashset");//hash集里没有ID return true; } ArrayList operationList = new ArrayList(); for (long id : ids) { 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; } ContentProviderOperation.Builder builder = ContentProviderOperation - .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));//实现删除功能 operationList.add(builder.build()); } try { ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + //唯一标识这个ContentProvider,起标记作用 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());//提示删除失败并返回IDS return false; } return true; @@ -78,28 +79,30 @@ public class DataUtils { 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); + //对需要移动的便签进行数据更新,然后用update实现 } public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { - Log.d(TAG, "the ids is null"); + Log.d(TAG, "the ids is null");//IDS为空 return true; } 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 { 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()); + Log.d(TAG, "delete notes failed, ids:" + ids.toString());//删除失败并返回IDS return false; } return true; @@ -119,7 +122,7 @@ 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);//筛选条件:源文件不为trash folder int count = 0; if(cursor != null) { @@ -127,28 +130,29 @@ public class DataUtils { try { count = cursor.getInt(0); } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "get folder count failed:" + e.toString()); + Log.e(TAG, "get folder count failed:" + e.toString());//获取文件夹计数失败 } finally { - cursor.close(); + cursor.close();//释放 } } } - return count; + return count;//返回计数 } public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), - null, + //为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) { if (cursor.getCount() > 0) { exist = true; - } - cursor.close(); + }//判断cursor是否为空 + cursor.close();//释放 } return exist; } @@ -162,7 +166,7 @@ public class DataUtils { if (cursor.getCount() > 0) { exist = true; } - cursor.close(); + cursor.close();//释放 } return exist; } @@ -171,14 +175,14 @@ public class DataUtils { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); - boolean exist = false; + boolean exist = false;//设置条件值 if (cursor != null) { if (cursor.getCount() > 0) { - exist = true; + exist = true;//count存在 } - cursor.close(); + cursor.close();//释放 } - return exist; + return exist;//count是否存在 } public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { @@ -186,15 +190,15 @@ public class DataUtils { NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.SNIPPET + "=?", - new String[] { name }, null); - boolean exist = false; + new String[] { name }, null);//通过名字查询文件是否存在 + boolean exist = false;//设置条件值 if(cursor != null) { if(cursor.getCount() > 0) { exist = true; } - cursor.close(); + cursor.close();//关闭 } - return exist; + return exist;//是否存在 } public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { @@ -202,7 +206,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,15 +215,15 @@ public class DataUtils { do { try { AppWidgetAttribute widget = new AppWidgetAttribute(); - widget.widgetId = c.getInt(0); - widget.widgetType = c.getInt(1); - set.add(widget); + 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()); } - } while (c.moveToNext()); + } while (c.moveToNext());//查询下一条直到全部 } - c.close(); + c.close();//释放 } return set; } @@ -235,9 +239,9 @@ public class DataUtils { 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();//释放 } } return ""; @@ -249,17 +253,17 @@ 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()); + Log.e(TAG, "Get call note id fails " + e.toString());//获取呼叫便签ID失败 } } - cursor.close(); + cursor.close();//释放 } return 0; } @@ -269,17 +273,17 @@ public class DataUtils { new String [] { NoteColumns.SNIPPET }, NoteColumns.ID + "=?", new String [] { String.valueOf(noteId)}, - null); + null);//查询条件:noteId if (cursor != null) { String snippet = ""; if (cursor.moveToFirst()) { snippet = cursor.getString(0); - } - cursor.close(); + }//如果移到第一位 + cursor.close();//释放 return snippet; } - throw new IllegalArgumentException("Note is not found with id: " + noteId); + throw new IllegalArgumentException("Note is not found with id: " + noteId);//找不到便签的ID } public static String getFormattedSnippet(String snippet) { @@ -291,5 +295,5 @@ public class DataUtils { } } return snippet; - } + }//对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉 }