邹兴云注释

pull/10/head
邹兴云 2 years ago
parent 225f60584e
commit d600a5fb23

@ -47,23 +47,28 @@ public class BackupUtils {
}
return sInstance;
}
/* `TAG` String tag
`sInstance`
`getInstance()` `Context` `BackupUtils`
使线
*/
/**
* Following states are signs to represents backup or restore
* Following states are signs to represents backup or restore
* status
*/
// Currently, the sdcard is not mounted
public static final int STATE_SD_CARD_UNMOUONTED = 0;
// The backup file not exist
// The backup file not exist(备份文件不存在)
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
// The data is not well formated, may be changed by other programs
// The data is not well formated, may be changed by other programs(数据格式不正确可能被其他程序更改)
public static final int STATE_DATA_DESTROIED = 2;
// Some run-time exception which causes restore or backup fails
// Some run-time exception which causes restore or backup fails(导致备份或还原失败的默写运行异常)
public static final int STATE_SYSTEM_ERROR = 3;
// Backup or restore success
// Backup or restore success(备份或还原成功)
public static final int STATE_SUCCESS = 4;
private TextExport mTextExport;
private TextExport mTextExport;//实例化对象
private BackupUtils(Context context) {
mTextExport = new TextExport(context);
@ -75,15 +80,15 @@ public class BackupUtils {
public int exportToText() {
return mTextExport.exportToText();
}
}//这是一个 public 方法,用于将数据导出为文本,并返回一个整数值。
public String getExportedTextFileName() {
return mTextExport.mFileName;
}
}//这是一个 public 方法,用于获取导出的文本文件名。
public String getExportedTextFileDir() {
return mTextExport.mFileDirectory;
}
}//这是一个 public 方法,用于获取导出的文本文件所在的目录。
private static class TextExport {
private static final String[] NOTE_PROJECTION = {
@ -91,7 +96,8 @@ public class BackupUtils {
NoteColumns.MODIFIED_DATE,
NoteColumns.SNIPPET,
NoteColumns.TYPE
};
};//这是一个私有的静态内部类,用于完成数据导出的操作。
private static final int NOTE_COLUMN_ID = 0;
@ -115,32 +121,37 @@ public class BackupUtils {
private static final int DATA_COLUMN_CALL_DATE = 2;
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
// 定义了多个常量,用于操作笔记和数据的列索引
private final String [] TEXT_FORMAT;
private static final int FORMAT_FOLDER_NAME = 0;
private static final int FORMAT_NOTE_DATE = 1;
private static final int FORMAT_NOTE_CONTENT = 2;
// 定义了一个字符串数组 TEXT_FORMAT其中存储了导出笔记时的文件格式
private Context mContext;
private String mFileName;
private String mFileDirectory;
// 定义了 Context mContext、String mFileName、String mFileDirectory 三个变量
// mContext 存储了当前上下文mFileName 存储了文件名mFileDirectory 存储了文件夹路径
public TextExport(Context context) {
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
mFileName = "";
mFileDirectory = "";
mFileDirectory = "";// 构造函数,初始化了 TEXT_FORMAT、mContext、mFileName 和 mFileDirectory
}
private String getFormat(int id) {
return TEXT_FORMAT[id];
}
}// getFormat 方法返回 TEXT_FORMAT 数组中指定 id 的字符串
/**
* Export the folder identified by folder id to text
* (exportFolderToText )
*/
private void exportFolderToText(String folderId, PrintStream ps) {
// Query notes belong to this folder
// Query notes belong to this folder// 查询属于该文件夹的笔记
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
folderId
@ -149,11 +160,11 @@ public class BackupUtils {
if (notesCursor != null) {
if (notesCursor.moveToFirst()) {
do {
// Print note's last modified date
// Print note's last modified date // 输出笔记的最后修改日期
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
mContext.getString(R.string.format_datetime_mdhm),
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
// Query data belong to this note
// Query data belong to this note // 查询属于该笔记的数据
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps);
} while (notesCursor.moveToNext());
@ -169,14 +180,14 @@ public class BackupUtils {
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId
}, null);
}, null);// exportNoteToText 方法用于将指定笔记的数据导出到文本中
if (dataCursor != null) {
if (dataCursor.moveToFirst()) {
do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
if (DataConstants.CALL_NOTE.equals(mimeType)) {
// Print phone number
// Print phone number // 输出电话号码
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
@ -185,11 +196,11 @@ public class BackupUtils {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber));
}
// Print call date
// 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
// Print call attachment location // 输出通话附件位置
if (!TextUtils.isEmpty(location)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
location));
@ -205,7 +216,7 @@ public class BackupUtils {
}
dataCursor.close();
}
// print a line separator between note
// print a line separator between note// 在导出每个笔记后输出一个分隔符
try {
ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
@ -222,14 +233,20 @@ public class BackupUtils {
if (!externalStorageAvailable()) {
Log.d(TAG, "Media was not mounted");
return STATE_SD_CARD_UNMOUONTED;
}
}/*PrintStream使JavabyteCharacter.LINE_SEPARATORCharacter.LETTER_NUMBER
Character.LINE_SEPARATORWindows"\r\n"Unix/Linux"\n"
Character.LETTER_NUMBER
bytePrintStream便IOExceptionLogcat*/
PrintStream ps = getExportToTextPrintStream();
if (ps == null) {
Log.e(TAG, "get print stream error");
return STATE_SYSTEM_ERROR;
}
// First export folder and its notes
}//这段代码中的 getExportToTextPrintStream() 是一个自定义方法,它返回一个 PrintStream 对象,该对象用于将笔记数据导出到文本文件中
// First export folder and its notes//“首先导出文件夹及其包含的笔记”
Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
@ -255,7 +272,11 @@ public class BackupUtils {
} while (folderCursor.moveToNext());
}
folderCursor.close();
}
}/*
ContentResolver query() Call Record Cursor folderCursor
folderCursor ID ps Call Record 使*/
// Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query(
@ -280,7 +301,13 @@ public class BackupUtils {
ps.close();
return STATE_SUCCESS;
}
}/*
ContentResolver query() Cursor noteCursor
noteCursor ps exportNoteToText() ps
ps STATE_SUCCESS noteCursor */
/**
* Get a print stream pointed to the file {@generateExportedTextFile}
@ -307,7 +334,15 @@ public class BackupUtils {
}
return ps;
}
}
}/* PrintStream
generateFileMountedOnSDcard() File file
file nullLogcat "create file to exported failed" null
file FileOutputStream fos PrintStream PrintStream ps
ps PrintStream FileNotFoundException NullPointerException null*/
/**
* Generate the text file to store imported data
@ -339,6 +374,14 @@ public class BackupUtils {
return null;
}
}
}/*
filePathResId StringBuilder sb
File filedir mkdir()
使 fileNameFormatResId sb File file
file filedir file SecurityException IOException null*/

Loading…
Cancel
Save