|  |  | @ -18,6 +18,7 @@ package net.micode.notes.tool; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.content.Context; |  |  |  | import android.content.Context; | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.database.Cursor; |  |  |  | import android.database.Cursor; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import android.os.Build; | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.os.Environment; |  |  |  | import android.os.Environment; | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.text.TextUtils; |  |  |  | import android.text.TextUtils; | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.text.format.DateFormat; |  |  |  | import android.text.format.DateFormat; | 
			
		
	
	
		
		
			
				
					|  |  | @ -29,12 +30,14 @@ import net.micode.notes.data.Notes.DataColumns; | 
			
		
	
		
		
			
				
					
					|  |  |  | import net.micode.notes.data.Notes.DataConstants; |  |  |  | import net.micode.notes.data.Notes.DataConstants; | 
			
		
	
		
		
			
				
					
					|  |  |  | import net.micode.notes.data.Notes.NoteColumns; |  |  |  | import net.micode.notes.data.Notes.NoteColumns; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.io.BufferedWriter; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.File; |  |  |  | import java.io.File; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.FileNotFoundException; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.FileOutputStream; |  |  |  | import java.io.FileOutputStream; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.IOException; |  |  |  | import java.io.IOException; | 
			
		
	
		
		
			
				
					
					|  |  |  | import java.io.PrintStream; |  |  |  | import java.io.OutputStreamWriter; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | import java.nio.charset.StandardCharsets; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.util.ArrayList; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import java.util.List; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | public class BackupUtils { |  |  |  | public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |     private static final String TAG = "BackupUtils"; |  |  |  |     private static final String TAG = "BackupUtils"; | 
			
		
	
	
		
		
			
				
					|  |  | @ -62,6 +65,8 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_SYSTEM_ERROR                 = 3; |  |  |  |     public static final int STATE_SYSTEM_ERROR                 = 3; | 
			
		
	
		
		
			
				
					
					|  |  |  |     // Backup or restore success
 |  |  |  |     // Backup or restore success
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     public static final int STATE_SUCCESS                      = 4; |  |  |  |     public static final int STATE_SUCCESS                      = 4; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Permission denied
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     public static final int STATE_PERMISSION_DENIED            = 5; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     private TextExport mTextExport; |  |  |  |     private TextExport mTextExport; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -90,14 +95,15 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.ID, |  |  |  |                 NoteColumns.ID, | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.MODIFIED_DATE, |  |  |  |                 NoteColumns.MODIFIED_DATE, | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.SNIPPET, |  |  |  |                 NoteColumns.SNIPPET, | 
			
		
	
		
		
			
				
					
					|  |  |  |                 NoteColumns.TYPE |  |  |  |                 NoteColumns.TYPE, | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 NoteColumns.PARENT_ID | 
			
		
	
		
		
			
				
					
					|  |  |  |         }; |  |  |  |         }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         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 int NOTE_COLUMN_TYPE = 3; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private static final int NOTE_COLUMN_PARENT_ID = 4; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private static final String[] DATA_PROJECTION = { |  |  |  |         private static final String[] DATA_PROJECTION = { | 
			
		
	
		
		
			
				
					
					|  |  |  |                 DataColumns.CONTENT, |  |  |  |                 DataColumns.CONTENT, | 
			
		
	
	
		
		
			
				
					|  |  | @ -109,11 +115,8 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         }; |  |  |  |         }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         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 final String [] TEXT_FORMAT; | 
			
		
	
	
		
		
			
				
					|  |  | @ -124,12 +127,17 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         private Context mContext; |  |  |  |         private Context mContext; | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String mFileName; |  |  |  |         private String mFileName; | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String mFileDirectory; |  |  |  |         private String mFileDirectory; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private BufferedWriter mWriter; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private int mNotesCount; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private int mFoldersCount; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         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; | 
			
		
	
		
		
			
				
					
					|  |  |  |             mFileName = ""; |  |  |  |             mFileName = ""; | 
			
		
	
		
		
			
				
					
					|  |  |  |             mFileDirectory = ""; |  |  |  |             mFileDirectory = ""; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mNotesCount = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mFoldersCount = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         private String getFormat(int id) { |  |  |  |         private String getFormat(int id) { | 
			
		
	
	
		
		
			
				
					|  |  | @ -137,208 +145,268 @@ public class BackupUtils { | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Export the folder identified by folder id to text |  |  |  |          * Export notes to text file | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private void exportFolderToText(String folderId, PrintStream ps) { |  |  |  |         public int exportToText() { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             // Query notes belong to this folder
 |  |  |  |             if (!externalStorageAvailable()) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, |  |  |  |                 Log.d(TAG, "Media was not mounted"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { |  |  |  |                 return STATE_SD_CARD_UNMOUONTED; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         folderId |  |  |  |             } | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     }, null); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (notesCursor != null) { |  |  |  |             File file = generateFileMountedOnSDcard(mContext, R.string.file_path, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 if (notesCursor.moveToFirst()) { |  |  |  |                     R.string.file_name_txt_format); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |             if (file == null) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         // Print note's last modified date
 |  |  |  |                 Log.e(TAG, "create file to exported failed"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( |  |  |  |                 return STATE_SYSTEM_ERROR; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), |  |  |  |             } | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                 notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         // Query data belong to this note
 |  |  |  |             mFileName = file.getName(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         String noteId = notesCursor.getString(NOTE_COLUMN_ID); |  |  |  |             mFileDirectory = mContext.getString(R.string.file_path); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         exportNoteToText(noteId, ps); |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     } while (notesCursor.moveToNext()); |  |  |  |             // 使用try-with-resources确保资源释放
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |             try (BufferedWriter writer = new BufferedWriter( | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 notesCursor.close(); |  |  |  |                     new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) { | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter = writer; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 // 导出文件夹和笔记
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 exportFoldersAndNotes(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 // 写入导出统计信息
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 writeExportSummary(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 Log.d(TAG, "Export successful. Exported " + mFoldersCount + " folders and " + mNotesCount + " notes."); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return STATE_SUCCESS; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } catch (IOException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 Log.e(TAG, "Error during export: " + e.getMessage(), e); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return STATE_SYSTEM_ERROR; | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |          | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Export note identified by id to a print stream |  |  |  |          * 写入导出摘要信息 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private void exportNoteToText(String noteId, PrintStream ps) { |  |  |  |         private void writeExportSummary() throws IOException { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, |  |  |  |             mWriter.write("\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { |  |  |  |             mWriter.write("==============================\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         noteId |  |  |  |             mWriter.write(mContext.getString(R.string.export_summary) + "\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     }, null); |  |  |  |             mWriter.write(mContext.getString(R.string.export_date) + ": "  | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |                     + DateFormat.format(mContext.getString(R.string.format_datetime_ymdhm), System.currentTimeMillis()) + "\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (dataCursor != null) { |  |  |  |             mWriter.write(mContext.getString(R.string.folder_count) + ": " + mFoldersCount + "\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 if (dataCursor.moveToFirst()) { |  |  |  |             mWriter.write(mContext.getString(R.string.note_count) + ": " + mNotesCount + "\n"); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mWriter.write("==============================\n"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          * 导出所有文件夹和笔记 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportFoldersAndNotes() throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 首先导出文件夹和其中的笔记
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             exportFolders(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 导出根文件夹中的笔记
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             exportRootNotes(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          * 导出所有文件夹 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportFolders() throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 查询所有文件夹
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String selection = "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try (Cursor folderCursor = mContext.getContentResolver().query( | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     Notes.CONTENT_NOTE_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     NOTE_PROJECTION, | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     selection, null, null)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (folderCursor != null && folderCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); |  |  |  |                         mFoldersCount++; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         if (DataConstants.CALL_NOTE.equals(mimeType)) { |  |  |  |                         exportFolder(folderCursor); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print phone number
 |  |  |  |                     } while (folderCursor.moveToNext()); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             String location = dataCursor.getString(DATA_COLUMN_CONTENT); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             if (!TextUtils.isEmpty(phoneNumber)) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                         phoneNumber)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print call date
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                     .format(mContext.getString(R.string.format_datetime_mdhm), |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                             callDate))); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             // Print call attachment location
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             if (!TextUtils.isEmpty(location)) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                         location)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         } else if (DataConstants.NOTE.equals(mimeType)) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             String content = dataCursor.getString(DATA_COLUMN_CONTENT); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             if (!TextUtils.isEmpty(content)) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                                         content)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                     } while (dataCursor.moveToNext()); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |                 dataCursor.close(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             // print a line separator between note
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             try { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 ps.write(new byte[] { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         Character.LINE_SEPARATOR, Character.LETTER_NUMBER |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 }); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } catch (IOException e) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.e(TAG, e.toString()); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |          | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Note will be exported as text which is user readable |  |  |  |          * 导出单个文件夹及其包含的笔记 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         public int exportToText() { |  |  |  |         private void exportFolder(Cursor folderCursor) throws IOException { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (!externalStorageAvailable()) { |  |  |  |             String folderId = folderCursor.getString(NOTE_COLUMN_ID); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.d(TAG, "Media was not mounted"); |  |  |  |             String folderName; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 return STATE_SD_CARD_UNMOUONTED; |  |  |  |              | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             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); | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             PrintStream ps = getExportToTextPrintStream(); |  |  |  |             if (!TextUtils.isEmpty(folderName)) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (ps == null) { |  |  |  |                 mWriter.write(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.e(TAG, "get print stream error"); |  |  |  |                 mWriter.newLine(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 return STATE_SYSTEM_ERROR; |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             // First export folder and its notes
 |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor folderCursor = mContext.getContentResolver().query( |  |  |  |             // 导出文件夹中的笔记
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     Notes.CONTENT_NOTE_URI, |  |  |  |             exportNotesInFolder(folderId); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     NOTE_PROJECTION, |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " |  |  |  |             // 添加文件夹分隔线
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " |  |  |  |             mWriter.write("\n"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); |  |  |  |         } | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |          | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (folderCursor != null) { |  |  |  |         /** | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 if (folderCursor.moveToFirst()) { |  |  |  |          * 导出指定文件夹中的所有笔记 | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportNotesInFolder(String folderId) throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try (Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId }, null)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (notesCursor != null && notesCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Print folder's name
 |  |  |  |                         mNotesCount++; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         String folderName = ""; |  |  |  |                         exportNote(notesCursor); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                         if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { |  |  |  |                     } while (notesCursor.moveToNext()); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             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)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         String folderId = folderCursor.getString(NOTE_COLUMN_ID); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         exportFolderToText(folderId, ps); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                     } while (folderCursor.moveToNext()); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |                 folderCursor.close(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |         } | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             // Export notes in root's folder
 |  |  |  |          | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             Cursor noteCursor = mContext.getContentResolver().query( |  |  |  |         /** | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          * 导出根文件夹中的笔记 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportRootNotes() throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try (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",  | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                             + "=0", null, null); |  |  |  |                     null, null)) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |                  | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (noteCursor != null) { |  |  |  |                 if (noteCursor != null && noteCursor.moveToFirst()) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 if (noteCursor.moveToFirst()) { |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                     do { |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |                         ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( |  |  |  |                         mNotesCount++; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                 mContext.getString(R.string.format_datetime_mdhm), |  |  |  |                         exportNote(noteCursor); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                                 noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         // Query data belong to this note
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         String noteId = noteCursor.getString(NOTE_COLUMN_ID); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         exportNoteToText(noteId, ps); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                     } while (noteCursor.moveToNext()); |  |  |  |                     } while (noteCursor.moveToNext()); | 
			
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |                 noteCursor.close(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             ps.close(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             return STATE_SUCCESS; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |          | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         /** |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |          * Get a print stream pointed to the file {@generateExportedTextFile} |  |  |  |          * 导出单个笔记 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |          */ |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |         private PrintStream getExportToTextPrintStream() { |  |  |  |         private void exportNote(Cursor noteCursor) throws IOException { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             File file = generateFileMountedOnSDcard(mContext, R.string.file_path, |  |  |  |             // 打印笔记的最后修改日期
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                     R.string.file_name_txt_format); |  |  |  |             mWriter.write(String.format(getFormat(FORMAT_NOTE_DATE),  | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (file == null) { |  |  |  |                     DateFormat.format(mContext.getString(R.string.format_datetime_mdhm), | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 Log.e(TAG, "create file to exported failed"); |  |  |  |                             noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 return null; |  |  |  |             mWriter.newLine(); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 查询并导出笔记的数据
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String noteId = noteCursor.getString(NOTE_COLUMN_ID); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             exportNoteData(noteId); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 添加笔记分隔线
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mWriter.write("\n"); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          * 导出笔记的所有数据 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportNoteData(String noteId) throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             try (Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId }, null)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (dataCursor != null && dataCursor.moveToFirst()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     do { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                         String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                         if (DataConstants.CALL_NOTE.equals(mimeType)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                             exportCallNote(dataCursor); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                         } else if (DataConstants.NOTE.equals(mimeType)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                             exportTextNote(dataCursor); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     } while (dataCursor.moveToNext()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             mFileName = file.getName(); |  |  |  |         } | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             mFileDirectory = mContext.getString(R.string.file_path); |  |  |  |          | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             PrintStream ps = null; |  |  |  |         /** | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             try { |  |  |  |          * 导出通话记录笔记 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 FileOutputStream fos = new FileOutputStream(file); |  |  |  |          */ | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 ps = new PrintStream(fos); |  |  |  |         private void exportCallNote(Cursor dataCursor) throws IOException { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             } catch (FileNotFoundException e) { |  |  |  |             // 打印电话号码
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 e.printStackTrace(); |  |  |  |             String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 return null; |  |  |  |             long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             } catch (NullPointerException e) { |  |  |  |             String location = dataCursor.getString(DATA_COLUMN_CONTENT); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 e.printStackTrace(); |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 return null; |  |  |  |             if (!TextUtils.isEmpty(phoneNumber)) { | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.write(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.newLine(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 打印通话日期
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mWriter.write(String.format(getFormat(FORMAT_NOTE_CONTENT),  | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     DateFormat.format(mContext.getString(R.string.format_datetime_mdhm), callDate))); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             mWriter.newLine(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 打印通话附件位置
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (!TextUtils.isEmpty(location)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.write(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.newLine(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         /** | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          * 导出文本笔记 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          */ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private void exportTextNote(Cursor dataCursor) throws IOException { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             String content = dataCursor.getString(DATA_COLUMN_CONTENT); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (!TextUtils.isEmpty(content)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.write(String.format(getFormat(FORMAT_NOTE_CONTENT), content)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 mWriter.newLine(); | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             return ps; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     /** |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |      * Generate the text file to store imported data |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |      */ |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         StringBuilder sb = new StringBuilder(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(Environment.getExternalStorageDirectory()); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(context.getString(filePathResId)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         File filedir = new File(sb.toString()); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         sb.append(context.getString( |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 fileNameFormatResId, |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 DateFormat.format(context.getString(R.string.format_date_ymd), |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                         System.currentTimeMillis()))); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         File file = new File(sb.toString()); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         try { |  |  |  |         /** | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             if (!filedir.exists()) { |  |  |  |          * Generate the text file to store imported data | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 filedir.mkdir(); |  |  |  |          */ | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             StringBuilder sb = new StringBuilder(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 适配Android 10+的存储访问
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 sb.append(context.getExternalFilesDir(null)); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } else { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 sb.append(Environment.getExternalStorageDirectory()); | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (!file.exists()) { |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 file.createNewFile(); |  |  |  |             sb.append(context.getString(filePathResId)); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             File filedir = new File(sb.toString()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |              | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             // 确保目录存在
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (!filedir.exists() && !filedir.mkdirs()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 Log.e(TAG, "Failed to create directory: " + filedir.getAbsolutePath()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |             return file; |  |  |  |              | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         } catch (SecurityException e) { |  |  |  |             // 构建文件名
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             e.printStackTrace(); |  |  |  |             sb.append(context.getString( | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         } catch (IOException e) { |  |  |  |                     fileNameFormatResId, | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             e.printStackTrace(); |  |  |  |                     DateFormat.format(context.getString(R.string.format_date_ymd), | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |                             System.currentTimeMillis()))); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             File file = new File(sb.toString()); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         return null; |  |  |  |             try { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 if (!file.exists() && !file.createNewFile()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     Log.e(TAG, "Failed to create file: " + file.getAbsolutePath()); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return file; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } catch (SecurityException | IOException e) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 Log.e(TAG, "Error creating file: " + e.getMessage(), e); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 return null; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |