|
|
@ -37,13 +37,13 @@ import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BackupUtils {
|
|
|
|
public class BackupUtils {
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
private static final String TAG = "BackupUtils";// 定义了一个私有静态常量字符串TAG,用于在日志中标记该类
|
|
|
|
// Singleton stuff
|
|
|
|
// Singleton stuff
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
private static BackupUtils sInstance;// 声明了一个私有静态BackupUtils对象sInstance,用于实现单例模式
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {//获取BackupUtils类的实例的静态方法
|
|
|
|
if (sInstance == null) {
|
|
|
|
if (sInstance == null) {
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
sInstance = new BackupUtils(context);//在第一次调用时,会创建一个新的BackupUtils对象,并将其赋值给静态变量sInstance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -53,53 +53,53 @@ public class BackupUtils {
|
|
|
|
* 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;//表示sd卡未挂载的状态。
|
|
|
|
// The backup file not exist
|
|
|
|
// The backup file not exist
|
|
|
|
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
private BackupUtils(Context context) {//用于初始化BackupUtils对象。在构造函数内部,创建了一个TextExport对象mTextExport。
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class TextExport {
|
|
|
|
private static class TextExport {//处理文本导出的具体实现
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
private static final String[] NOTE_PROJECTION = {//查询备份数据的列名
|
|
|
|
NoteColumns.ID,
|
|
|
|
NoteColumns.ID,
|
|
|
|
NoteColumns.MODIFIED_DATE,
|
|
|
|
NoteColumns.MODIFIED_DATE,
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
NoteColumns.TYPE
|
|
|
|
NoteColumns.TYPE
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private static final int NOTE_COLUMN_ID = 0;
|
|
|
|
private static final int NOTE_COLUMN_ID = 0;//笔记的ID列索引
|
|
|
|
|
|
|
|
|
|
|
|
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 String[] DATA_PROJECTION = {
|
|
|
|
private static final String[] DATA_PROJECTION = {//查询的数据列
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
DataColumns.DATA1,
|
|
|
|
DataColumns.DATA1,
|
|
|
@ -116,14 +116,14 @@ public class BackupUtils {
|
|
|
|
|
|
|
|
|
|
|
|
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;//存储了用于格式化导出文本的模板字符串
|
|
|
|
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;//笔记内容的格式索引
|
|
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private Context mContext;//上下文对象
|
|
|
|
private String mFileName;
|
|
|
|
private String mFileName;//文件名
|
|
|
|
private String mFileDirectory;
|
|
|
|
private String mFileDirectory;//文件所在的目录
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
@ -139,7 +139,7 @@ public class BackupUtils {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
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[] {
|
|
|
@ -148,12 +148,13 @@ public class BackupUtils {
|
|
|
|
|
|
|
|
|
|
|
|
if (notesCursor != null) {
|
|
|
|
if (notesCursor != null) {
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
do {// 打印笔记的最后修改日期
|
|
|
|
// Print note's last modified date
|
|
|
|
// Print note's last modified date
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
|
mContext.getString(R.string.format_datetime_mdhm),
|
|
|
|
mContext.getString(R.string.format_datetime_mdhm),
|
|
|
|
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
|
|
|
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);
|
|
|
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
@ -171,17 +172,18 @@ public class BackupUtils {
|
|
|
|
noteId
|
|
|
|
noteId
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (dataCursor != null) {
|
|
|
|
if (dataCursor != null) {//是否有符合条件的数据行
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
|
|
|
|
// 遍历游标以检索每个数据行的MIME类型
|
|
|
|
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
|
|
|
|
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
|
|
|
|
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
|
|
|
|
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
|
|
|
|
long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE);
|
|
|
|
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
|
|
|
|
String location = dataCursor.getString(DATA_COLUMN_CONTENT);
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -218,43 +220,45 @@ 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() { //总函数,调用上面的exportFolder和exportNote
|
|
|
|
if (!externalStorageAvailable()) {
|
|
|
|
if (!externalStorageAvailable()) {// 检查外部存储是否可用(即 SD 卡是否已挂载)
|
|
|
|
Log.d(TAG, "Media was not mounted");
|
|
|
|
Log.d(TAG, "Media was not mounted");// 如果外部存储不可用,则在日志中记录错误消息
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;// 返回常量表示 SD 卡未挂载
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PrintStream ps = getExportToTextPrintStream();
|
|
|
|
PrintStream ps = getExportToTextPrintStream();// 获取一个打印流来写入导出的笔记
|
|
|
|
if (ps == null) {
|
|
|
|
if (ps == null) {// 如果无法获取打印流,则记录错误消息并返回常量表示系统错误
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
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,// 查询内容 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 {
|
|
|
|
// Print folder's name
|
|
|
|
// Print folder's name
|
|
|
|
|
|
|
|
// 打印文件夹的名称
|
|
|
|
String folderName = "";
|
|
|
|
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);
|
|
|
|
folderName = mContext.getString(R.string.call_record_folder_name);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
|
|
|
|
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(folderName)) {
|
|
|
|
if (!TextUtils.isEmpty(folderName)) {// 如果文件夹名称不为空
|
|
|
|
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
|
|
|
|
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));// 写入文件夹名称到打印流中
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
exportFolderToText(folderId, ps);
|
|
|
|
exportFolderToText(folderId, ps); // 导出当前文件夹的笔记到打印流中
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folderCursor.close();
|
|
|
|
folderCursor.close();// 关闭查询结果集游标
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
// Export notes in root's folder
|
|
|
@ -264,7 +268,7 @@ public class BackupUtils {
|
|
|
|
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 {
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
@ -285,15 +289,15 @@ public class BackupUtils {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get a print stream pointed to the file {@generateExportedTextFile}
|
|
|
|
* Get a print stream pointed to the file {@generateExportedTextFile}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private PrintStream getExportToTextPrintStream() {
|
|
|
|
private PrintStream getExportToTextPrintStream() {//私有方法,返回一个PrintStream对象,用于导出文本
|
|
|
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
|
|
|
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,//调用generateFileMountedOnSDcard方法生成一个文件对象,作为导出文件的路径。
|
|
|
|
R.string.file_name_txt_format);
|
|
|
|
R.string.file_name_txt_format);
|
|
|
|
if (file == null) {
|
|
|
|
if (file == null) {//如果文件对象为空,表示创建导出文件失败,打印错误消息并返回空
|
|
|
|
Log.e(TAG, "create file to exported failed");
|
|
|
|
Log.e(TAG, "create file to exported failed");
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mFileName = file.getName();
|
|
|
|
mFileName = file.getName();
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);//将文件名和文件路径保存到相应的变量中
|
|
|
|
PrintStream ps = null;
|
|
|
|
PrintStream ps = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
@ -321,10 +325,10 @@ public class BackupUtils {
|
|
|
|
fileNameFormatResId,
|
|
|
|
fileNameFormatResId,
|
|
|
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
|
|
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
File file = new File(sb.toString());//在指定的路径上创建一个新的文件
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
if (!filedir.exists()) {//如果文件目录不存在,则尝试创建目录
|
|
|
|
filedir.mkdir();
|
|
|
|
filedir.mkdir();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!file.exists()) {
|
|
|
|
if (!file.exists()) {
|
|
|
@ -334,7 +338,7 @@ public class BackupUtils {
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();//会打印错误信息并返回空
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|