|
|
|
@ -35,10 +35,10 @@ import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//笔记备份工具类
|
|
|
|
|
public class BackupUtils {
|
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
|
// Singleton stuff
|
|
|
|
|
// Singleton stuff 单例模式
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
|
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
@ -47,23 +47,34 @@ public class 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 class BackupState {
|
|
|
|
|
|
|
|
|
|
//当前SD未挂载
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SD_CARD_UNMOUNTED = 0;
|
|
|
|
|
|
|
|
|
|
//备份文件不存在
|
|
|
|
|
|
|
|
|
|
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_DATA_DESTROYED = 2;
|
|
|
|
|
|
|
|
|
|
//运行时异常导致恢复或备份失败
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3;
|
|
|
|
|
// Backup or restore success
|
|
|
|
|
|
|
|
|
|
// 备份或恢复成功
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 用于导出文件的目录
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
@ -84,7 +95,7 @@ public class BackupUtils {
|
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 笔记备份类
|
|
|
|
|
private static class TextExport {
|
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
@ -92,7 +103,7 @@ public class BackupUtils {
|
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
|
NoteColumns.TYPE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//定义Note数据表中各列对应的索引常量。
|
|
|
|
|
private static final int NOTE_COLUMN_ID = 0;
|
|
|
|
|
|
|
|
|
|
private static final int NOTE_COLUMN_MODIFIED_DATE = 1;
|
|
|
|
@ -124,28 +135,26 @@ public class BackupUtils {
|
|
|
|
|
private Context mContext;
|
|
|
|
|
private String mFileName;
|
|
|
|
|
private String mFileDirectory;
|
|
|
|
|
|
|
|
|
|
// 备份和恢复功能
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
// 导出笔记到文本
|
|
|
|
|
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 {
|
|
|
|
@ -161,22 +170,21 @@ public class BackupUtils {
|
|
|
|
|
notesCursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export note identified by id to a print stream
|
|
|
|
|
*/
|
|
|
|
|
// 导出笔记到文本
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 查询笔记数据
|
|
|
|
|
if (dataCursor != null) {
|
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
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);
|
|
|
|
@ -185,16 +193,17 @@ public class BackupUtils {
|
|
|
|
|
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),
|
|
|
|
|
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),
|
|
|
|
@ -203,9 +212,11 @@ public class BackupUtils {
|
|
|
|
|
}
|
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭数据游标
|
|
|
|
|
dataCursor.close();
|
|
|
|
|
}
|
|
|
|
|
// print a line separator between note
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ps.write(new byte[] {
|
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
@ -214,48 +225,53 @@ public class BackupUtils {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
// 查询文件夹
|
|
|
|
|
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 != null) {
|
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// Print folder's name
|
|
|
|
|
// 获取并处理当前文件夹的名称
|
|
|
|
|
String folderName = "";
|
|
|
|
|
// 判断当前文件夹是否为通话记录文件夹,据此设置不同的文件夹名称
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
// 获取并导出当前文件夹的详细信息到文本
|
|
|
|
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportFolderToText(folderId, ps);
|
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
// 关闭文件夹游标
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export trash folder
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
@ -263,7 +279,7 @@ public class BackupUtils {
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
|
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
|
|
|
|
|
+ "=0", null, null);
|
|
|
|
|
|
|
|
|
|
// 遍历笔记
|
|
|
|
|
if (noteCursor != null) {
|
|
|
|
|
if (noteCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
@ -285,6 +301,7 @@ public class BackupUtils {
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
@ -294,7 +311,9 @@ public class BackupUtils {
|
|
|
|
|
}
|
|
|
|
|
mFileName = file.getName();
|
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);
|
|
|
|
|
|
|
|
|
|
PrintStream ps = null;
|
|
|
|
|
// 创建文件输出流
|
|
|
|
|
try {
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
ps = new PrintStream(fos);
|
|
|
|
@ -312,6 +331,8 @@ public class BackupUtils {
|
|
|
|
|
/**
|
|
|
|
|
* Generate the text file to store imported data
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 生成文件
|
|
|
|
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append(Environment.getExternalStorageDirectory());
|
|
|
|
@ -322,21 +343,26 @@ public class BackupUtils {
|
|
|
|
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 创建文件
|
|
|
|
|
try {
|
|
|
|
|
// 检查目录是否存在,不存在则创建目录
|
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
|
filedir.mkdir();
|
|
|
|
|
}
|
|
|
|
|
// 检查文件是否存在,不存在则创建文件
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
}
|
|
|
|
|
return file;
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
// 处理安全异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 处理IO异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 遇到异常返回null
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|