From 2186942fb45f132cd1c3945787c9f01fd6364572 Mon Sep 17 00:00:00 2001 From: square group sky <2992305964@qq.com> Date: Fri, 11 Oct 2019 21:16:47 +0800 Subject: [PATCH] 1 --- .idea/workspace.xml | 484 +++++++++--------- .../net/micode/notes/tool/BackupUtils.java | 34 +- .../java/net/micode/notes/tool/DataUtils.java | 13 +- 3 files changed, 290 insertions(+), 241 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index fad963f..cc5d225 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,9 +11,8 @@ - - - + + + + + + + + + + + - + - - + + - + - - + + - + - - - - - - - - - - - - - - - - - - - - + + @@ -83,29 +73,17 @@ - - + + - + - - - - - - - - - - - - - + @@ -113,17 +91,26 @@ - - + + - + - - + + + + + + + + + + + @@ -135,6 +122,7 @@ JSONObject exportToText equal + notesCursor.moveToFirst() @@ -155,15 +143,16 @@ - + @@ -423,15 +374,22 @@ - + - + @@ -442,8 +400,8 @@ - - + + @@ -459,6 +417,28 @@ + + + + + jar://G:/Android/Sdk/platforms/android-28/android.jar!/android/database/AbstractCursor.class + 39 + + + + + jar://G:/Android/Sdk/platforms/android-28/android.jar!/android/database/AbstractCursor.class + 41 + + + + + + @@ -520,13 +500,6 @@ - - - - - - - @@ -534,13 +507,6 @@ - - - - - - - @@ -555,31 +521,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -598,13 +539,6 @@ - - - - - - - @@ -633,13 +567,6 @@ - - - - - - - @@ -647,61 +574,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -717,45 +596,100 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -769,17 +703,109 @@ - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/app/src/main/java/net/micode/notes/tool/BackupUtils.java index 7c507a3..a25a488 100644 --- a/app/src/main/java/net/micode/notes/tool/BackupUtils.java +++ b/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -42,7 +42,7 @@ public class BackupUtils { private static final String TAG = "BackupUtils"; // Singleton stuff private static BackupUtils sInstance; - + //获得实例 public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { sInstance = new BackupUtils(context); @@ -79,7 +79,7 @@ public class BackupUtils { 其中.equals表示的是二者的内容是否相等*/ return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } - + //导出文本 public int exportToText() { return mTextExport.exportToText(); } @@ -99,13 +99,13 @@ public class BackupUtils { NoteColumns.SNIPPET, NoteColumns.TYPE }; - + //便签列id private static final int NOTE_COLUMN_ID = 0; - + //便签修改的日期 private static final int NOTE_COLUMN_MODIFIED_DATE = 1; - + //便签列片段 private static final int NOTE_COLUMN_SNIPPET = 2; - + //数据项 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -114,15 +114,15 @@ public class BackupUtils { DataColumns.DATA3, DataColumns.DATA4, }; - + //数据列中内容 private static final int DATA_COLUMN_CONTENT = 0; - + //数据列中mime类型 private static final int DATA_COLUMN_MIME_TYPE = 1; - + //数据中拨打日期 private static final int DATA_COLUMN_CALL_DATE = 2; - + //数据列中的电话号码 private static final int DATA_COLUMN_PHONE_NUMBER = 4; - + //文本格式 private final String [] TEXT_FORMAT; private static final int FORMAT_FOLDER_NAME = 0; private static final int FORMAT_NOTE_DATE = 1; @@ -154,6 +154,11 @@ public class BackupUtils { }, null); //下面有很多地方调用了android.database.Cursor类有一些固定用法 if (notesCursor != null) { + /* + 查询出来的cursor的初始位置是指向第一条记录的前一个位置的 + cursor.moveToFirst()指向查询结果的第一个位置。 + 一般通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空。 + */ if (notesCursor.moveToFirst()) { do { // Print note's last modified date @@ -181,6 +186,7 @@ public class BackupUtils { if (dataCursor != null) { if (dataCursor.moveToFirst()) { do { + //获取mimeType String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { // Print phone number @@ -210,6 +216,7 @@ public class BackupUtils { } } while (dataCursor.moveToNext()); } + //及时关闭cursor dataCursor.close(); } // print a line separator between note @@ -233,6 +240,7 @@ public class BackupUtils { } //定义输出流ps PrintStream ps = getExportToTextPrintStream(); + //检查null if (ps == null) { Log.e(TAG, "get print stream error"); return STATE_SYSTEM_ERROR; @@ -250,18 +258,22 @@ public class BackupUtils { do { // Print folder's name String folderName = ""; + //若id为最近记录里调用的文件中 if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); } else { folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); } + //若文本工具为空 if (!TextUtils.isEmpty(folderName)) { ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); } + //获得文件id String folderId = folderCursor.getString(NOTE_COLUMN_ID); exportFolderToText(folderId, ps); } while (folderCursor.moveToNext()); } + //及时关闭folderCursor folderCursor.close(); } 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 074cbf0..f723fbe 100644 --- a/app/src/main/java/net/micode/notes/tool/DataUtils.java +++ b/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -41,10 +41,12 @@ public class DataUtils { public static final String TAG = "DataUtils"; //批处理删除便签 public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + //id不存在 if (ids == null) { Log.d(TAG, "the ids is null"); return true; } + //id在哈希表里 if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); return true; @@ -70,6 +72,7 @@ public class DataUtils { return false; } return true; + //捕捉远程异常 } catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { @@ -121,6 +124,12 @@ public class DataUtils { * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} */ public static int getUserFolderCount(ContentResolver resolver) { + /* + ContentResolver直译为内容解析器, + Android中程序间数据的共享是通过Provider/Resolver进行的。 + 提供数据(内容)的就叫Provider,Resovler提供接口对这个内容进行解读。 + 在这里,系统提供了联系人的Provider,那么我们就需要构建一个Resolver来读取联系人的内容。 + */ Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, new String[] { "COUNT(*)" }, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", @@ -239,6 +248,7 @@ public class DataUtils { null); if (cursor != null && cursor.moveToFirst()) { + //获得第一行数据 try { return cursor.getString(0); } catch (IndexOutOfBoundsException e) { @@ -249,7 +259,7 @@ public class DataUtils { } return ""; } - //通过电话号码和日期获得便签ID + //通过电话号码和拨打日期获得便签ID public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, @@ -262,6 +272,7 @@ public class DataUtils { if (cursor.moveToFirst()) { try { return cursor.getLong(0); + //记录获取失败 } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Get call note id fails " + e.toString()); }