初始化工程

main
sweet 2 years ago
parent f52383be3d
commit 2e7963f92f

@ -37,10 +37,12 @@ import java.io.PrintStream;
public class BackupUtils {
//定义了一个常量 TAG用于日志输出时标识日志来源。
private static final String TAG = "BackupUtils";
// Singleton stuff
//用于保存 BackupUtils 类的单例对象
private static BackupUtils sInstance;
//实现了 BackupUtils 类的单例模式,通过 getInstance 方法获取 BackupUtils 类的单例对象,并确保在多线程环境下获取单例对象的安全性。
public static synchronized BackupUtils getInstance(Context context) {
if (sInstance == null) {
sInstance = new BackupUtils(context);
@ -52,6 +54,7 @@ public class BackupUtils {
* 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
@ -62,29 +65,29 @@ public class BackupUtils {
public static final int STATE_SYSTEM_ERROR = 3;
// Backup or restore success
public static final int STATE_SUCCESS = 4;
//用于执行文本导出操作
private TextExport mTextExport;
//BackupUtils 类的构造函数,接受一个 Context 对象作为参数
private BackupUtils(Context context) {
mTextExport = new TextExport(context);
}
//externalStorageAvailable 是一个私有静态方法,用于检查外部存储是否可用,如相等则表示可用。
private static boolean externalStorageAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
//执行导出为文本的操作
public int exportToText() {
return mTextExport.exportToText();
}
//获取导出的文本文件的文件名
public String getExportedTextFileName() {
return mTextExport.mFileName;
}
//获取导出的文本文件的目录
public String getExportedTextFileDir() {
return mTextExport.mFileDirectory;
}
//NOTE_PROJECTION 是一个包含 Note 数据表的列投影的字符串数组。它指定了需要查询的列
private static class TextExport {
private static final String[] NOTE_PROJECTION = {
NoteColumns.ID,
@ -92,13 +95,13 @@ public class BackupUtils {
NoteColumns.SNIPPET,
NoteColumns.TYPE
};
//NOTE_COLUMN_ID、NOTE_COLUMN_MODIFIED_DATE 和 NOTE_COLUMN_SNIPPET 是一些整数常量,用于表示 NOTE_PROJECTION 数组中对应列的索引。
private static final int NOTE_COLUMN_ID = 0;
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
private static final int NOTE_COLUMN_SNIPPET = 2;
//DATA_PROJECTION 是一个包含 Data 数据表的列投影的字符串数组。它指定了需要查询的列
private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
@ -107,7 +110,7 @@ public class BackupUtils {
DataColumns.DATA3,
DataColumns.DATA4,
};
//以下整数常量,用于表示 DATA_PROJECTION 数组中对应列的索引。
private static final int DATA_COLUMN_CONTENT = 0;
private static final int DATA_COLUMN_MIME_TYPE = 1;
@ -115,23 +118,23 @@ public class BackupUtils {
private static final int DATA_COLUMN_CALL_DATE = 2;
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
//TEXT_FORMAT 是一个字符串数组,用于指定导出文本的格式。
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;
//mContext 是一个 Context 对象用于获取应用程序的上下文。mFileName 和 mFileDirectory 是用于保存导出文本文件的文件名和文件目录的字符串变量。
private Context mContext;
private String mFileName;
private String mFileDirectory;
//TextExport 类的构造函数,接受一个 Context 对象作为参数,用于初始化成员变量
public TextExport(Context context) {
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
mFileName = "";
mFileDirectory = "";
}
//私有方法 getFormat 接受一个整数参数,并根据该参数获取对应的格式字符串
private String getFormat(int id) {
return TEXT_FORMAT[id];
}
@ -139,13 +142,15 @@ public class BackupUtils {
/**
* Export the folder identified by folder id to text
*/
//定义了 TextExport 类中的一个私有方法 exportFolderToText用于将特定文件夹中的笔记导出为文本。
private void exportFolderToText(String folderId, PrintStream ps) {
// Query notes belong to this folder
//查询与特定文件夹关联的便签记录
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
folderId
}, null);
//通过查询特定文件夹关联的便签记录,并使用循环遍历查询结果,逐个导出便签。
if (notesCursor != null) {
if (notesCursor.moveToFirst()) {
do {
@ -165,22 +170,26 @@ public class BackupUtils {
/**
* Export note identified by id to a print stream
*/
//定义了 TextExport 类中的另一个私有方法 exportNoteToText用于将特定笔记导出为文本。
private void exportNoteToText(String noteId, PrintStream ps) {
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId
}, null);
//在存在查询结果的情况下,通过判断 dataCursor.moveToFirst() 来移动到结果集中的第一行。然后使用 do-while 循环遍历查询结果的每一行。
if (dataCursor != null) {
if (dataCursor.moveToFirst()) {
do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
if (DataConstants.CALL_NOTE.equals(mimeType)) {
// Print phone number
//从数据记录中获取电话号码 phoneNumber
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
//从数据记录中获取通话日期 callDate
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
//从数据记录中获取附件位置信息 location
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
//处理数据记录的 MIME 类型为 DataConstants.CALL_NOTE 时打印电话号码、通话日期和附件位置信息。
if (!TextUtils.isEmpty(phoneNumber)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber));
@ -203,6 +212,7 @@ public class BackupUtils {
}
} while (dataCursor.moveToNext());
}
//关闭游卡
dataCursor.close();
}
// print a line separator between note
@ -218,6 +228,7 @@ public class BackupUtils {
/**
* Note will be exported as text which is user readable
*/
//在不同的笔记之间打印一个行分隔符。
public int exportToText() {
if (!externalStorageAvailable()) {
Log.d(TAG, "Media was not mounted");
@ -230,13 +241,14 @@ public class BackupUtils {
return STATE_SYSTEM_ERROR;
}
// First export folder and its notes
//使用内容解析器查询便签的文件夹信息
Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
"(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND "
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
//处理查询到的笔记文件夹信息,并将文件夹名和文件夹内容导出为文本格式。
if (folderCursor != null) {
if (folderCursor.moveToFirst()) {
do {
@ -256,14 +268,17 @@ public class BackupUtils {
}
folderCursor.close();
}
//导出根文件夹中的便签
// Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query(
//便签内容的 URI
Notes.CONTENT_NOTE_URI,
//查询结果的列投影,指定了查询返回的列。
NOTE_PROJECTION,
//查询的选择条件
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
+ "=0", null, null);
//查询根文件夹中的笔记,并将笔记的修改日期和内容导出为文本格式。
if (noteCursor != null) {
if (noteCursor.moveToFirst()) {
do {
@ -275,17 +290,19 @@ public class BackupUtils {
exportNoteToText(noteId, ps);
} while (noteCursor.moveToNext());
}
//关闭游标,释放资源。
noteCursor.close();
}
ps.close();
//导出操作成功完成
return STATE_SUCCESS;
}
////用于获取一个指向导出文本文件的打印流PrintStream)
/**
* Get a print stream pointed to the file {@generateExportedTextFile}
*/
private PrintStream getExportToTextPrintStream() {
//生成一个文件对象
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
R.string.file_name_txt_format);
if (file == null) {
@ -308,7 +325,7 @@ public class BackupUtils {
return ps;
}
}
//用于获取一个指向导出文本文件的打印流PrintStream
/**
* Generate the text file to store imported data
*/

Loading…
Cancel
Save