|  |  | @ -1,17 +1,18 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 以下是带有中文注释的代码: | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | ```java | 
			
		
	
		
		
			
				
					
					|  |  |  | /* |  |  |  | /* | 
			
		
	
		
		
			
				
					
					|  |  |  |  * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) |  |  |  |  * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) | 
			
		
	
		
		
			
				
					
					|  |  |  |  * |  |  |  |  * | 
			
		
	
		
		
			
				
					
					|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); |  |  |  |  * 根据Apache License, Version 2.0(以下简称“许可证”)授权; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |  * you may not use this file except in compliance with the License. |  |  |  |  * 除非遵守许可证,否则不得使用此文件。 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |  * You may obtain a copy of the License at |  |  |  |  * 你可以在以下网址获得许可证的副本: | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  * |  |  |  |  * | 
			
		
	
		
		
			
				
					
					|  |  |  |  *        http://www.apache.org/licenses/LICENSE-2.0
 |  |  |  |  *        http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  * |  |  |  |  * | 
			
		
	
		
		
			
				
					
					|  |  |  |  * Unless required by applicable law or agreed to in writing, software |  |  |  |  * 除非适用法律要求或书面同意,否则按“原样”提供软件 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, |  |  |  |  * 在许可证下分发,不附带任何明示或暗示的保证或条件。 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |  |  |  |  * 请参阅许可证,了解有关权限和限制的具体语言。 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |  * See the License for the specific language governing permissions and |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |  * limitations under the License. |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  */ |  |  |  |  */ | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | package net.micode.notes.tool; |  |  |  | package net.micode.notes.tool; | 
			
		
	
	
		
		
			
				
					|  |  | @ -35,12 +36,13 @@ import java.io.FileOutputStream; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.IOException; |  |  |  | import java.io.IOException; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.PrintStream; |  |  |  | import java.io.PrintStream; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | // 备份工具类,用于将笔记数据备份到文本文件中。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | public class BackupUtils { |  |  |  | public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |     private static final String TAG = "BackupUtils"; |  |  |  |     private static final String TAG = "BackupUtils"; | 
			
		
	
		
		
			
				
					
					|  |  |  |     // Singleton stuff
 |  |  |  |     // 单例模式
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     private static BackupUtils sInstance; |  |  |  |     private static BackupUtils sInstance; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 获取BackupUtils的单例,如果不存在则创建。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public static synchronized BackupUtils getInstance(Context context) { |  |  |  |     public static synchronized BackupUtils getInstance(Context context) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         if (sInstance == null) { |  |  |  |         if (sInstance == null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             sInstance = new BackupUtils(context); |  |  |  |             sInstance = new BackupUtils(context); | 
			
		
	
	
		
		
			
				
					|  |  | @ -49,43 +51,49 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     /** |  |  |  |     /** | 
			
		
	
		
		
			
				
					
					|  |  |  |      * Following states are signs to represents backup or restore |  |  |  |      * 以下状态码用于表示备份或恢复的状态。 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |      * status |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |      */ |  |  |  |      */ | 
			
		
	
		
		
			
				
					
					|  |  |  |     // Currently, the sdcard is not mounted
 |  |  |  |     // SD卡未挂载
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_SD_CARD_UNMOUONTED           = 0; |  |  |  |     public static final int STATE_SD_CARD_UNMOUONTED = 0; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // The backup file not exist
 |  |  |  |     // 备份文件不存在
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_BACKUP_FILE_NOT_EXIST        = 1; |  |  |  |     public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // The data is not well formated, may be changed by other programs
 |  |  |  |     // 数据格式不正确,可能被其他程序更改
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_DATA_DESTROIED               = 2; |  |  |  |     public static final int STATE_DATA_DESTROIED = 2; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // Some run-time exception which causes restore or backup fails
 |  |  |  |     // 运行时异常导致备份或恢复失败
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_SYSTEM_ERROR                 = 3; |  |  |  |     public static final int STATE_SYSTEM_ERROR = 3; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // Backup or restore success
 |  |  |  |     // 备份或恢复成功
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_SUCCESS                      = 4; |  |  |  |     public static final int STATE_SUCCESS = 4; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     private TextExport mTextExport; |  |  |  |     private TextExport mTextExport; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 私有构造函数,用于创建BackupUtils实例。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     private BackupUtils(Context context) { |  |  |  |     private BackupUtils(Context context) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         mTextExport = new TextExport(context); |  |  |  |         mTextExport = new TextExport(context); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 检查外部存储是否可用。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     private static boolean externalStorageAvailable() { |  |  |  |     private static boolean externalStorageAvailable() { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); |  |  |  |         return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 导出笔记数据到文本文件。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public int exportToText() { |  |  |  |     public int exportToText() { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return mTextExport.exportToText(); |  |  |  |         return mTextExport.exportToText(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 获取导出的文本文件名。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public String getExportedTextFileName() { |  |  |  |     public String getExportedTextFileName() { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return mTextExport.mFileName; |  |  |  |         return mTextExport.mFileName; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 获取导出的文本文件目录。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public String getExportedTextFileDir() { |  |  |  |     public String getExportedTextFileDir() { | 
			
		
	
		
		
			
				
					
					|  |  |  |         return mTextExport.mFileDirectory; |  |  |  |         return mTextExport.mFileDirectory; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // 内部类,用于将笔记数据导出到文本。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     private static class TextExport { |  |  |  |     private static class TextExport { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 笔记数据投影数组。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final String[] NOTE_PROJECTION = { |  |  |  |         private static final String[] NOTE_PROJECTION = { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.ID, |  |  |  |                 NoteColumns.ID, | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.MODIFIED_DATE, |  |  |  |                 NoteColumns.MODIFIED_DATE, | 
			
		
	
	
		
		
			
				
					|  |  | @ -93,12 +101,12 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.TYPE |  |  |  |                 NoteColumns.TYPE | 
			
		
	
		
		
			
				
					
					|  |  |  |         }; |  |  |  |         }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 数据列索引。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int NOTE_COLUMN_ID = 0; |  |  |  |         private static final int NOTE_COLUMN_ID = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int NOTE_COLUMN_MODIFIED_DATE = 1; |  |  |  |         private static final int NOTE_COLUMN_MODIFIED_DATE = 1; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int NOTE_COLUMN_SNIPPET = 2; |  |  |  |         private static final int NOTE_COLUMN_SNIPPET = 2; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 数据数据投影数组。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final String[] DATA_PROJECTION = { |  |  |  |         private static final String[] DATA_PROJECTION = { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 DataColumns.CONTENT, |  |  |  |                 DataColumns.CONTENT, | 
			
		
	
		
		
			
				
					
					|  |  |  |                 DataColumns.MIME_TYPE, |  |  |  |                 DataColumns.MIME_TYPE, | 
			
		
	
	
		
		
			
				
					|  |  | @ -108,23 +116,25 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 DataColumns.DATA4, |  |  |  |                 DataColumns.DATA4, | 
			
		
	
		
		
			
				
					
					|  |  |  |         }; |  |  |  |         }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 数据列索引。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int DATA_COLUMN_CONTENT = 0; |  |  |  |         private static final int DATA_COLUMN_CONTENT = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int DATA_COLUMN_MIME_TYPE = 1; |  |  |  |         private static final int DATA_COLUMN_MIME_TYPE = 1; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int DATA_COLUMN_CALL_DATE = 2; |  |  |  |         private static final int DATA_COLUMN_CALL_DATE = 2; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int DATA_COLUMN_PHONE_NUMBER = 4; |  |  |  |         private static final int DATA_COLUMN_PHONE_NUMBER = 4; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private final String [] TEXT_FORMAT; |  |  |  |         // 导出文本的格式数组。
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int FORMAT_FOLDER_NAME          = 0; |  |  |  |         private final String[] TEXT_FORMAT; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int FORMAT_NOTE_DATE            = 1; |  |  |  |         private static final int FORMAT_FOLDER_NAME = 0; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         private static final int FORMAT_NOTE_CONTENT         = 2; |  |  |  |         private static final int FORMAT_NOTE_DATE = 1; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private static final int FORMAT_NOTE_CONTENT = 2; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 上下文对象,用于访问资源和内容解析器。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private Context mContext; |  |  |  |         private Context mContext; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 导出文件的名称和目录。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String mFileName; |  |  |  |         private String mFileName; | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String mFileDirectory; |  |  |  |         private String mFileDirectory; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 构造函数,初始化上下文和格式数组。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         public TextExport(Context context) { |  |  |  |         public TextExport(Context context) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); |  |  |  |             TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); | 
			
		
	
		
		
			
				
					
					|  |  |  |             mContext = context; |  |  |  |             mContext = context; | 
			
		
	
	
		
		
			
				
					|  |  | @ -132,28 +142,29 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |             mFileDirectory = ""; |  |  |  |             mFileDirectory = ""; | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // 根据ID获取对应的格式字符串。
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String getFormat(int id) { |  |  |  |         private String getFormat(int id) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             return TEXT_FORMAT[id]; |  |  |  |             return TEXT_FORMAT[id]; | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Export the folder identified by folder id to text |  |  |  |          * 将指定文件夹ID的笔记导出到文本。 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private void exportFolderToText(String folderId, PrintStream ps) { |  |  |  |         private void exportFolderToText(String folderId, PrintStream ps) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             // Query notes belong to this folder
 |  |  |  |             // 查询属于该文件夹的笔记。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, |  |  |  |             Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { |  |  |  |                     NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         folderId |  |  |  |                             folderId | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                     }, null); |  |  |  |                     }, null); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (notesCursor != null) { |  |  |  |             if (notesCursor != null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 if (notesCursor.moveToFirst()) { |  |  |  |                 if (notesCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Print note's last modified date
 |  |  |  |                         // 打印笔记最后修改日期。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( |  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), |  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); |  |  |  |                                 notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Query data belong to this note
 |  |  |  |                         // 查询属于该笔记的数据。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                         String noteId = notesCursor.getString(NOTE_COLUMN_ID); |  |  |  |                         String noteId = notesCursor.getString(NOTE_COLUMN_ID); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         exportNoteToText(noteId, ps); |  |  |  |                         exportNoteToText(noteId, ps); | 
			
		
	
		
		
			
				
					
					|  |  |  |                     } while (notesCursor.moveToNext()); |  |  |  |                     } while (notesCursor.moveToNext()); | 
			
		
	
	
		
		
			
				
					|  |  | @ -163,12 +174,12 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Export note identified by id to a print stream |  |  |  |          * 将指定ID的笔记导出到打印流。 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private void exportNoteToText(String noteId, PrintStream ps) { |  |  |  |         private void exportNoteToText(String noteId, PrintStream ps) { | 
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, |  |  |  |             Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { |  |  |  |                     DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         noteId |  |  |  |                             noteId | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                     }, null); |  |  |  |                     }, null); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (dataCursor != null) { |  |  |  |             if (dataCursor != null) { | 
			
		
	
	
		
		
			
				
					|  |  | @ -176,7 +187,7 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); |  |  |  |                         String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         if (DataConstants.CALL_NOTE.equals(mimeType)) { |  |  |  |                         if (DataConstants.CALL_NOTE.equals(mimeType)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print phone number
 |  |  |  |                             // 打印电话号码
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                             String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); |  |  |  |                             String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); | 
			
		
	
		
		
			
				
					
					|  |  |  |                             long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); |  |  |  |                             long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); | 
			
		
	
		
		
			
				
					
					|  |  |  |                             String location = dataCursor.getString(DATA_COLUMN_CONTENT); |  |  |  |                             String location = dataCursor.getString(DATA_COLUMN_CONTENT); | 
			
		
	
	
		
		
			
				
					|  |  | @ -185,11 +196,11 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), |  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), | 
			
		
	
		
		
			
				
					
					|  |  |  |                                         phoneNumber)); |  |  |  |                                         phoneNumber)); | 
			
		
	
		
		
			
				
					
					|  |  |  |                             } |  |  |  |                             } | 
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print call date
 |  |  |  |                             // 打印通话日期
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                             ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat |  |  |  |                             ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat | 
			
		
	
		
		
			
				
					
					|  |  |  |                                     .format(mContext.getString(R.string.format_datetime_mdhm), |  |  |  |                                     .format(mContext.getString(R.string.format_datetime_mdhm), | 
			
		
	
		
		
			
				
					
					|  |  |  |                                             callDate))); |  |  |  |                                             callDate))); | 
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print call attachment location
 |  |  |  |                             // 打印通话附件位置
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                             if (!TextUtils.isEmpty(location)) { |  |  |  |                             if (!TextUtils.isEmpty(location)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), |  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), | 
			
		
	
		
		
			
				
					
					|  |  |  |                                         location)); |  |  |  |                                         location)); | 
			
		
	
	
		
		
			
				
					|  |  | @ -205,7 +216,7 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |                 dataCursor.close(); |  |  |  |                 dataCursor.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             // print a line separator between note
 |  |  |  |             // 打印笔记之间的分隔线。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             try { |  |  |  |             try { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 ps.write(new byte[] { |  |  |  |                 ps.write(new byte[] { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         Character.LINE_SEPARATOR, Character.LETTER_NUMBER |  |  |  |                         Character.LINE_SEPARATOR, Character.LETTER_NUMBER | 
			
		
	
	
		
		
			
				
					|  |  | @ -216,33 +227,34 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Note will be exported as text which is user readable |  |  |  |          * 将笔记导出为用户可读的文本。 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         public int exportToText() { |  |  |  |         public int exportToText() { | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (!externalStorageAvailable()) { |  |  |  |             if (!externalStorageAvailable()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.d(TAG, "Media was not mounted"); |  |  |  |                 Log.d(TAG, "媒体未挂载"); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 return STATE_SD_CARD_UNMOUONTED; |  |  |  |                 return STATE_SD_CARD_UNMOUONTED; | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             PrintStream ps = getExportToTextPrintStream(); |  |  |  |             PrintStream ps = getExportToTextPrintStream(); | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (ps == null) { |  |  |  |             if (ps == null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.e(TAG, "get print stream error"); |  |  |  |                 Log.e(TAG, "获取打印流出错"); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 return STATE_SYSTEM_ERROR; |  |  |  |                 return STATE_SYSTEM_ERROR; | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             // First export folder and its notes
 |  |  |  |             // 首先导出文件夹及其笔记。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             Cursor folderCursor = mContext.getContentResolver().query( |  |  |  |             Cursor folderCursor = mContext.getContentResolver().query( | 
			
		
	
		
		
			
				
					
					|  |  |  |                     Notes.CONTENT_NOTE_URI, |  |  |  |                     Notes.CONTENT_NOTE_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     NOTE_PROJECTION, |  |  |  |                     NOTE_PROJECTION, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " |  |  |  |                     "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " | 
			
		
	
		
		
			
				
					
					|  |  |  |                             + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " |  |  |  |                             + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " | 
			
		
	
		
		
			
				
					
					|  |  |  |                             + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); |  |  |  |                             + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     null, null); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (folderCursor != null) { |  |  |  |             if (folderCursor != null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 if (folderCursor.moveToFirst()) { |  |  |  |                 if (folderCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Print folder's name
 |  |  |  |                         // 打印文件夹名称
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                         String folderName = ""; |  |  |  |                         String folderName = ""; | 
			
		
	
		
		
			
				
					
					|  |  |  |                         if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { |  |  |  |                         if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                             folderName = mContext.getString(R.string.call_record_folder_name); |  |  |  |                             folderName = mContext.getString(R.string.call_record_folder_name); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         } else { |  |  |  |                         } else { | 
			
		
	
		
		
			
				
					
					|  |  |  |                             folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); |  |  |  |                             folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); | 
			
		
	
	
		
		
			
				
					|  |  | @ -257,20 +269,21 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 folderCursor.close(); |  |  |  |                 folderCursor.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             // Export notes in root's folder
 |  |  |  |             // 导出根文件夹中的笔记。
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 导出根文件夹中的笔记
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor noteCursor = mContext.getContentResolver().query( |  |  |  |             Cursor noteCursor = mContext.getContentResolver().query( | 
			
		
	
		
		
			
				
					
					|  |  |  |                     Notes.CONTENT_NOTE_URI, |  |  |  |                     Notes.CONTENT_NOTE_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     NOTE_PROJECTION, |  |  |  |                     NOTE_PROJECTION, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID |  |  |  |                     NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", null, null); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             + "=0", null, null); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (noteCursor != null) { |  |  |  |             if (noteCursor != null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 if (noteCursor.moveToFirst()) { |  |  |  |                 if (noteCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                         // 打印笔记最后修改日期
 | 
			
		
	
		
		
			
				
					
					|  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( |  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), |  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); |  |  |  |                                 noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Query data belong to this note
 |  |  |  |                         // 查询属于这条笔记的数据
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                         String noteId = noteCursor.getString(NOTE_COLUMN_ID); |  |  |  |                         String noteId = noteCursor.getString(NOTE_COLUMN_ID); | 
			
		
	
		
		
			
				
					
					|  |  |  |                         exportNoteToText(noteId, ps); |  |  |  |                         exportNoteToText(noteId, ps); | 
			
		
	
		
		
			
				
					
					|  |  |  |                     } while (noteCursor.moveToNext()); |  |  |  |                     } while (noteCursor.moveToNext()); | 
			
		
	
	
		
		
			
				
					|  |  | @ -279,17 +292,19 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             ps.close(); |  |  |  |             ps.close(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 返回成功状态码
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             return STATE_SUCCESS; |  |  |  |             return STATE_SUCCESS; | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Get a print stream pointed to the file {@generateExportedTextFile} |  |  |  |          * 获取指向导出文本文件的打印流 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private PrintStream getExportToTextPrintStream() { |  |  |  |         private PrintStream getExportToTextPrintStream() { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 生成存储导出数据的文本文件
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             File file = generateFileMountedOnSDcard(mContext, R.string.file_path, |  |  |  |             File file = generateFileMountedOnSDcard(mContext, R.string.file_path, | 
			
		
	
		
		
			
				
					
					|  |  |  |                     R.string.file_name_txt_format); |  |  |  |                     R.string.file_name_txt_format); | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (file == null) { |  |  |  |             if (file == null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.e(TAG, "create file to exported failed"); |  |  |  |                 Log.e(TAG, "创建导出文件失败"); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 return null; |  |  |  |                 return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             mFileName = file.getName(); |  |  |  |             mFileName = file.getName(); | 
			
		
	
	
		
		
			
				
					|  |  | @ -309,36 +324,33 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     /** |  |  |  | /** | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |      * Generate the text file to store imported data |  |  |  |  * 在SD卡上生成用于存储导入数据的文本文件 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |      */ |  |  |  |  */ | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { |  |  |  | private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         StringBuilder sb = new StringBuilder(); |  |  |  |     StringBuilder sb = new StringBuilder(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(Environment.getExternalStorageDirectory()); |  |  |  |     sb.append(Environment.getExternalStorageDirectory()); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(context.getString(filePathResId)); |  |  |  |     sb.append(context.getString(filePathResId)); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         File filedir = new File(sb.toString()); |  |  |  |     File filedir = new File(sb.toString()); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(context.getString( |  |  |  |     sb.append(context.getString( | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 fileNameFormatResId, |  |  |  |             fileNameFormatResId, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 DateFormat.format(context.getString(R.string.format_date_ymd), |  |  |  |             DateFormat.format(context.getString(R.string.format_date_ymd), | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         System.currentTimeMillis()))); |  |  |  |                     System.currentTimeMillis()))); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         File file = new File(sb.toString()); |  |  |  |     File file = new File(sb.toString()); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         try { |  |  |  |     try { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (!filedir.exists()) { |  |  |  |         if (!filedir.exists()) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 filedir.mkdir(); |  |  |  |             filedir.mkdir(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (!file.exists()) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 file.createNewFile(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             return file; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } catch (SecurityException e) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             e.printStackTrace(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } catch (IOException e) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             e.printStackTrace(); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |         if (!file.exists()) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         return null; |  |  |  |             file.createNewFile(); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return file; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } catch (SecurityException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         e.printStackTrace(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } catch (IOException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         e.printStackTrace(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |