|
|
@ -36,56 +36,59 @@ import java.io.IOException;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
好的,我将为`BackupUtils.java`文件中的每个语句提供更细致的注释:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
public class BackupUtils {
|
|
|
|
public class BackupUtils {
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
private static final String TAG = "BackupUtils"; // 定义日志标签,用于识别日志输出的来源
|
|
|
|
// Singleton stuff
|
|
|
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
private static BackupUtils sInstance; // 声明一个静态实例变量,用于实现单例模式
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提供一个公共的静态方法,用于获取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); // 如果实例不存在,则新建一个实例
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
return sInstance; // 返回单例实例
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// 定义一系列状态码,用于表示备份或恢复的状态
|
|
|
|
* Following states are signs to represents backup or restore
|
|
|
|
public static final int STATE_SD_CARD_UNMOUONTED = 0; // SD卡未挂载
|
|
|
|
* status
|
|
|
|
public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; // 备份文件不存在
|
|
|
|
*/
|
|
|
|
public static final int STATE_DATA_DESTROIED = 2; // 数据格式不正确,可能被其他程序更改
|
|
|
|
// Currently, the sdcard is not mounted
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3; // 系统错误,导致备份或恢复失败
|
|
|
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
|
|
|
public static final int STATE_SUCCESS = 4; // 备份或恢复成功
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
private TextExport mTextExport; // TextExport对象,用于将数据导出到文本
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BackupUtils的私有构造函数,传入Context对象
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
mTextExport = new TextExport(context); // 初始化TextExport对象
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查外部存储(SD卡)是否可用
|
|
|
|
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(); // 调用TextExport对象的exportToText方法
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取导出文本文件的文件名
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
return mTextExport.mFileName;
|
|
|
|
return mTextExport.mFileName; // 返回TextExport对象中保存的文件名
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取导出文本文件的目录路径
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
return mTextExport.mFileDirectory; // 返回TextExport对象中保存的文件目录
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 内部类TextExport,封装了将笔记数据导出到文本文件的逻辑
|
|
|
|
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,
|
|
|
@ -93,12 +96,12 @@ public class BackupUtils {
|
|
|
|
NoteColumns.TYPE
|
|
|
|
NoteColumns.TYPE
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义查询笔记数据时需要的字段
|
|
|
|
private static final String[] DATA_PROJECTION = {
|
|
|
|
private static final String[] DATA_PROJECTION = {
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
@ -108,93 +111,88 @@ public class BackupUtils {
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
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; // 导出文本文件的目录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TextExport类的构造函数,初始化上下文和格式化字符串数组
|
|
|
|
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 = ""; // 初始化文件目录为空字符串
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据索引获取格式化字符串
|
|
|
|
private String getFormat(int id) {
|
|
|
|
private String getFormat(int id) {
|
|
|
|
return TEXT_FORMAT[id];
|
|
|
|
return TEXT_FORMAT[id]; // 返回格式化字符串数组中指定索引的字符串
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// 导出指定文件夹ID的笔记到文本
|
|
|
|
* 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
|
|
|
|
// 查询属于该文件夹的笔记
|
|
|
|
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 {
|
|
|
|
// 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
|
|
|
|
// 查询属于该笔记的数据
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notesCursor.close();
|
|
|
|
notesCursor.close(); // 关闭游标
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// 导出指定ID的笔记到文本
|
|
|
|
* Export note identified by id to a print stream
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
// 如果MIME类型为通话笔记,则打印电话号码、通话日期和位置
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Print call date
|
|
|
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
|
|
|
|
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)));
|
|
|
|
callDate)));
|
|
|
|
// Print call attachment location
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(location)) {
|
|
|
|
if (!TextUtils.isEmpty(location)) {
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
location));
|
|
|
|
location));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
|
|
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
|
|
|
|
|
|
|
// 如果MIME类型为普通笔记,则打印笔记内容
|
|
|
|
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
|
|
|
|
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
|
|
|
|
if (!TextUtils.isEmpty(content)) {
|
|
|
|
if (!TextUtils.isEmpty(content)) {
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
@ -203,9 +201,9 @@ public class BackupUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataCursor.close();
|
|
|
|
dataCursor.close(); // 关闭游标
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// print a line separator between note
|
|
|
|
// 打印笔记之间的分隔符
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ps.write(new byte[] {
|
|
|
|
ps.write(new byte[] {
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
@ -215,130 +213,37 @@ public class BackupUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
// 执行导出操作,将笔记数据导出为用户可读的文本格式
|
|
|
|
* 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"); // 如果SD卡未挂载,则记录日志并返回状态码
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintStream ps = getExportToTextPrintStream(); // 获取指向导出文本文件的PrintStream对象
|
|
|
|
PrintStream ps = getExportToTextPrintStream();
|
|
|
|
|
|
|
|
if (ps == null) {
|
|
|
|
if (ps == null) {
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
Log.e(TAG, "get print stream error"); // 如果获取PrintStream失败,则记录日志并返回状态码
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 {
|
|
|
|
// 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
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
|
|
|
|
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.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
|
|
|
|
|
|
|
|
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
|
|
|
} while (noteCursor.moveToNext());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
noteCursor.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ps.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return STATE_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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) {
|
|
|
|
|
|
|
|
Log.e(TAG, "create file to exported failed");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mFileName = file.getName();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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());
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
|
|
|
|
filedir.mkdir();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|