初始化工程

main
sweet 2 years ago
parent f52383be3d
commit 2e7963f92f

@ -37,10 +37,12 @@ import java.io.PrintStream;
public class BackupUtils { public class BackupUtils {
//定义了一个常量 TAG用于日志输出时标识日志来源。
private static final String TAG = "BackupUtils"; private static final String TAG = "BackupUtils";
// Singleton stuff // Singleton stuff
//用于保存 BackupUtils 类的单例对象
private static BackupUtils sInstance; private static BackupUtils sInstance;
//实现了 BackupUtils 类的单例模式,通过 getInstance 方法获取 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);
@ -52,6 +54,7 @@ public class BackupUtils {
* Following states are signs to represents backup or restore * Following states are signs to represents backup or restore
* status * status
*/ */
//这些是一些整数常量,用于表示不同的备份和恢复状态。每个常量都有一个对应的状态
// Currently, the sdcard is not mounted // Currently, the sdcard is not mounted
public static final int STATE_SD_CARD_UNMOUONTED = 0; public static final int STATE_SD_CARD_UNMOUONTED = 0;
// The backup file not exist // The backup file not exist
@ -62,29 +65,29 @@ 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;
//用于执行文本导出操作
private TextExport mTextExport; private TextExport mTextExport;
//BackupUtils 类的构造函数,接受一个 Context 对象作为参数
private BackupUtils(Context context) { private BackupUtils(Context context) {
mTextExport = new TextExport(context); mTextExport = new TextExport(context);
} }
//externalStorageAvailable 是一个私有静态方法,用于检查外部存储是否可用,如相等则表示可用。
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;
} }
//NOTE_PROJECTION 是一个包含 Note 数据表的列投影的字符串数组。它指定了需要查询的列
private static class TextExport { private static class TextExport {
private static final String[] NOTE_PROJECTION = { private static final String[] NOTE_PROJECTION = {
NoteColumns.ID, NoteColumns.ID,
@ -92,13 +95,13 @@ public class BackupUtils {
NoteColumns.SNIPPET, NoteColumns.SNIPPET,
NoteColumns.TYPE 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_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;
//DATA_PROJECTION 是一个包含 Data 数据表的列投影的字符串数组。它指定了需要查询的列
private static final String[] DATA_PROJECTION = { private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT, DataColumns.CONTENT,
DataColumns.MIME_TYPE, DataColumns.MIME_TYPE,
@ -107,7 +110,7 @@ public class BackupUtils {
DataColumns.DATA3, DataColumns.DATA3,
DataColumns.DATA4, DataColumns.DATA4,
}; };
//以下整数常量,用于表示 DATA_PROJECTION 数组中对应列的索引。
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;
@ -115,23 +118,23 @@ public class BackupUtils {
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;
//TEXT_FORMAT 是一个字符串数组,用于指定导出文本的格式。
private final String [] TEXT_FORMAT; private final String [] TEXT_FORMAT;
private static final int FORMAT_FOLDER_NAME = 0; private static final int FORMAT_FOLDER_NAME = 0;
private static final int FORMAT_NOTE_DATE = 1; private static final int FORMAT_NOTE_DATE = 1;
private static final int FORMAT_NOTE_CONTENT = 2; private static final int FORMAT_NOTE_CONTENT = 2;
//mContext 是一个 Context 对象用于获取应用程序的上下文。mFileName 和 mFileDirectory 是用于保存导出文本文件的文件名和文件目录的字符串变量。
private Context mContext; private Context mContext;
private String mFileName; private String mFileName;
private String mFileDirectory; private String mFileDirectory;
//TextExport 类的构造函数,接受一个 Context 对象作为参数,用于初始化成员变量
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 = "";
} }
//私有方法 getFormat 接受一个整数参数,并根据该参数获取对应的格式字符串
private String getFormat(int id) { private String getFormat(int id) {
return TEXT_FORMAT[id]; return TEXT_FORMAT[id];
} }
@ -139,13 +142,15 @@ public class BackupUtils {
/** /**
* Export the folder identified by folder id to text * Export the folder identified by folder id to text
*/ */
//定义了 TextExport 类中的一个私有方法 exportFolderToText用于将特定文件夹中的笔记导出为文本。
private void exportFolderToText(String folderId, PrintStream ps) { 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, 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 {
@ -165,22 +170,26 @@ public class BackupUtils {
/** /**
* Export note identified by id to a print stream * Export note identified by id to a print stream
*/ */
//定义了 TextExport 类中的另一个私有方法 exportNoteToText用于将特定笔记导出为文本。
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);
//在存在查询结果的情况下,通过判断 dataCursor.moveToFirst() 来移动到结果集中的第一行。然后使用 do-while 循环遍历查询结果的每一行。
if (dataCursor != null) { if (dataCursor != null) {
if (dataCursor.moveToFirst()) { if (dataCursor.moveToFirst()) {
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 // Print phone number
//从数据记录中获取电话号码 phoneNumber
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
//从数据记录中获取通话日期 callDate
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
//从数据记录中获取附件位置信息 location
String location = dataCursor.getString(DATA_COLUMN_CONTENT); String location = dataCursor.getString(DATA_COLUMN_CONTENT);
//处理数据记录的 MIME 类型为 DataConstants.CALL_NOTE 时打印电话号码、通话日期和附件位置信息。
if (!TextUtils.isEmpty(phoneNumber)) { if (!TextUtils.isEmpty(phoneNumber)) {
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
phoneNumber)); phoneNumber));
@ -203,6 +212,7 @@ public class BackupUtils {
} }
} while (dataCursor.moveToNext()); } while (dataCursor.moveToNext());
} }
//关闭游卡
dataCursor.close(); dataCursor.close();
} }
// print a line separator between note // print a line separator between note
@ -218,6 +228,7 @@ public class BackupUtils {
/** /**
* Note will be exported as text which is user readable * 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, "Media was not mounted");
@ -230,13 +241,14 @@ public class BackupUtils {
return STATE_SYSTEM_ERROR; return STATE_SYSTEM_ERROR;
} }
// First export folder and its notes // 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 {
@ -256,14 +268,17 @@ public class BackupUtils {
} }
folderCursor.close(); folderCursor.close();
} }
//导出根文件夹中的便签
// Export notes in root's folder // Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query( Cursor noteCursor = mContext.getContentResolver().query(
//便签内容的 URI
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 {
@ -275,17 +290,19 @@ public class BackupUtils {
exportNoteToText(noteId, ps); exportNoteToText(noteId, ps);
} while (noteCursor.moveToNext()); } while (noteCursor.moveToNext());
} }
//关闭游标,释放资源。
noteCursor.close(); noteCursor.close();
} }
ps.close(); ps.close();
//导出操作成功完成
return STATE_SUCCESS; return STATE_SUCCESS;
} }
////用于获取一个指向导出文本文件的打印流PrintStream)
/** /**
* Get a print stream pointed to the file {@generateExportedTextFile} * 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) {
@ -308,7 +325,7 @@ public class BackupUtils {
return ps; return ps;
} }
} }
//用于获取一个指向导出文本文件的打印流PrintStream
/** /**
* Generate the text file to store imported data * Generate the text file to store imported data
*/ */

Loading…
Cancel
Save