|
|
|
@ -37,68 +37,78 @@ import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BackupUtils {
|
|
|
|
|
// 用于日志记录的标记,方便在日志中识别该类的输出
|
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
|
// Singleton stuff
|
|
|
|
|
// 单例模式使用的静态变量,用于存储该类的唯一实例
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
|
|
|
|
|
|
// 单例模式的获取实例方法,确保在整个应用程序中只有一个 BackupUtils 实例
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
|
// 如果 sInstance 为空,创建一个新的 BackupUtils 实例并存储在 sInstance 中
|
|
|
|
|
if (sInstance == null) {
|
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
|
}
|
|
|
|
|
// 返回 BackupUtils 的唯一实例
|
|
|
|
|
return sInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
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;
|
|
|
|
|
// Some run-time exception which causes restore or backup fails
|
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3;
|
|
|
|
|
// Backup or restore success
|
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
|
|
|
|
|
|
// 表示 SD 卡未挂载的状态码
|
|
|
|
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
|
|
|
|
// 表示备份文件不存在的状态码
|
|
|
|
|
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
|
|
|
|
|
// 表示数据格式被破坏,可能被其他程序修改的数据状态码
|
|
|
|
|
public static final int STATE_DATA_DESTROIED = 2;
|
|
|
|
|
// 表示由于系统运行时异常导致备份或恢复失败的状态码
|
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3;
|
|
|
|
|
// 表示备份或恢复成功的状态码
|
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
|
|
|
|
|
|
// 用于文本导出的 TextExport 对象
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
|
|
|
|
|
|
// 构造函数,接收 Context 作为参数,并创建一个 TextExport 对象
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查外部存储是否可用,通过检查存储状态是否为已挂载
|
|
|
|
|
private static boolean externalStorageAvailable() {
|
|
|
|
|
// 比较存储状态是否等于已挂载状态
|
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用 TextExport 对象的 exportToText 方法,进行文本导出操作
|
|
|
|
|
public int exportToText() {
|
|
|
|
|
return mTextExport.exportToText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出的文本文件名,调用 TextExport 对象的相应属性
|
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
|
return mTextExport.mFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出的文本文件所在的目录,调用 TextExport 对象的相应属性
|
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 内部类 TextExport,负责具体的文本导出操作
|
|
|
|
|
private static class TextExport {
|
|
|
|
|
// 用于查询笔记的投影,指定了从数据库中获取笔记信息时需要的列
|
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.MODIFIED_DATE,
|
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
|
NoteColumns.TYPE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 笔记投影中各列的索引,用于从 Cursor 中准确提取相应信息
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 用于查询数据的投影,指定了从数据库中获取数据时需要的列
|
|
|
|
|
private static final String[] DATA_PROJECTION = {
|
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
@ -107,105 +117,116 @@ public class BackupUtils {
|
|
|
|
|
DataColumns.DATA3,
|
|
|
|
|
DataColumns.DATA4,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 数据投影中各列的索引,用于从 Cursor 中准确提取相应信息
|
|
|
|
|
private static final int DATA_COLUMN_CONTENT = 0;
|
|
|
|
|
|
|
|
|
|
private static final int DATA_COLUMN_MIME_TYPE = 1;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 存储文本格式化的字符串数组,包含不同的文本格式
|
|
|
|
|
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;
|
|
|
|
|
// 上下文对象,用于资源访问和数据库操作等
|
|
|
|
|
private Context mContext;
|
|
|
|
|
// 导出的文件名
|
|
|
|
|
private String mFileName;
|
|
|
|
|
// 导出的文件目录
|
|
|
|
|
private String mFileDirectory;
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化 TextExport 对象
|
|
|
|
|
public TextExport(Context context) {
|
|
|
|
|
// 从资源中获取存储的文本格式数组
|
|
|
|
|
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
|
|
|
|
|
mContext = context;
|
|
|
|
|
mFileName = "";
|
|
|
|
|
mFileDirectory = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据传入的索引获取相应的文本格式
|
|
|
|
|
private String getFormat(int id) {
|
|
|
|
|
return TEXT_FORMAT[id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
|
*/
|
|
|
|
|
// 将指定文件夹的笔记导出为文本,接收文件夹 ID 和打印流作为参数
|
|
|
|
|
private void exportFolderToText(String folderId, PrintStream ps) {
|
|
|
|
|
// Query notes belong to this folder
|
|
|
|
|
// 使用 ContentResolver 查询属于该文件夹的笔记
|
|
|
|
|
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
|
|
|
|
|
folderId
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
if (notesCursor != null) {
|
|
|
|
|
if (notesCursor!= null) {
|
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// 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
|
|
|
|
|
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)));
|
|
|
|
|
// 获取该笔记的 ID
|
|
|
|
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
// 调用 exportNoteToText 方法将该笔记导出为文本
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭 Cursor,释放资源
|
|
|
|
|
notesCursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export note identified by id to a print stream
|
|
|
|
|
*/
|
|
|
|
|
// 将指定笔记导出为文本,接收笔记 ID 和打印流作为参数
|
|
|
|
|
private void exportNoteToText(String noteId, PrintStream ps) {
|
|
|
|
|
// 使用 ContentResolver 查询属于该笔记的数据
|
|
|
|
|
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
|
|
|
|
|
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
|
|
|
|
|
noteId
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
if (dataCursor != null) {
|
|
|
|
|
if (dataCursor!= null) {
|
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// 获取该数据的 MIME 类型
|
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
|
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
.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());
|
|
|
|
|
}
|
|
|
|
|
// 关闭 Cursor,释放资源
|
|
|
|
|
dataCursor.close();
|
|
|
|
|
}
|
|
|
|
|
// print a line separator between note
|
|
|
|
|
// 在笔记之间打印行分隔符
|
|
|
|
|
try {
|
|
|
|
|
ps.write(new byte[] {
|
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
@ -215,21 +236,21 @@ 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");
|
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出的打印流
|
|
|
|
|
PrintStream ps = getExportToTextPrintStream();
|
|
|
|
|
if (ps == null) {
|
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
// First export folder and its notes
|
|
|
|
|
// 使用 ContentResolver 查询要导出的文件夹信息
|
|
|
|
|
Cursor folderCursor = mContext.getContentResolver().query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
@ -237,55 +258,63 @@ public class BackupUtils {
|
|
|
|
|
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
|
|
|
|
|
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
|
|
|
|
|
|
|
|
|
|
if (folderCursor != null) {
|
|
|
|
|
if (folderCursor!= null) {
|
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// Print folder's name
|
|
|
|
|
// 获取文件夹名称
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
|
|
|
|
|
}
|
|
|
|
|
if (!TextUtils.isEmpty(folderName)) {
|
|
|
|
|
// 打印文件夹名称
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
|
|
|
|
|
}
|
|
|
|
|
// 获取文件夹的 ID
|
|
|
|
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
// 调用 exportFolderToText 方法导出该文件夹的笔记
|
|
|
|
|
exportFolderToText(folderId, ps);
|
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭 Cursor,释放资源
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
|
// 使用 ContentResolver 查询根文件夹中的笔记信息
|
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
|
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
|
|
|
|
|
+ "=0", null, null);
|
|
|
|
|
|
|
|
|
|
if (noteCursor != null) {
|
|
|
|
|
if (noteCursor!= null) {
|
|
|
|
|
if (noteCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// 格式化并打印笔记的修改日期
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
|
|
mContext.getString(R.string.format_datetime_mdhm),
|
|
|
|
|
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
|
|
|
|
// Query data belong to this note
|
|
|
|
|
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)));
|
|
|
|
|
// 获取笔记的 ID
|
|
|
|
|
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
// 调用 exportNoteToText 方法导出该笔记
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (noteCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭 Cursor,释放资源
|
|
|
|
|
noteCursor.close();
|
|
|
|
|
}
|
|
|
|
|
// 关闭打印流
|
|
|
|
|
ps.close();
|
|
|
|
|
|
|
|
|
|
// 导出成功,返回成功状态码
|
|
|
|
|
return STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a print stream pointed to the file {@generateExportedTextFile}
|
|
|
|
|
*/
|
|
|
|
|
// 获取导出到文本的打印流
|
|
|
|
|
private PrintStream getExportToTextPrintStream() {
|
|
|
|
|
// 生成存储在 SD 卡上的文件
|
|
|
|
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
|
|
|
|
|
R.string.file_name_txt_format);
|
|
|
|
|
if (file == null) {
|
|
|
|
@ -296,7 +325,9 @@ public class BackupUtils {
|
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);
|
|
|
|
|
PrintStream ps = null;
|
|
|
|
|
try {
|
|
|
|
|
// 创建文件输出流
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
// 创建打印流,将数据输出到文件中
|
|
|
|
|
ps = new PrintStream(fos);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
@ -309,14 +340,15 @@ public class BackupUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate the text file to store imported data
|
|
|
|
|
*/
|
|
|
|
|
// 在 SD 卡上生成存储导入数据的文本文件
|
|
|
|
|
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),
|
|
|
|
@ -324,9 +356,11 @@ public class BackupUtils {
|
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 如果目录不存在,创建目录
|
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
|
filedir.mkdir();
|
|
|
|
|
}
|
|
|
|
|
// 如果文件不存在,创建文件
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
}
|
|
|
|
@ -341,4 +375,3 @@ public class BackupUtils {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|