|
|
|
@ -16,77 +16,80 @@
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
|
|
|
|
|
|
import javax.naming.Context;
|
|
|
|
|
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.os.Environment;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.format.DateFormat;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BackupUtils {
|
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
|
// Singleton stuff
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
|
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
|
if (sInstance == null) {
|
|
|
|
|
// synchronized"同步锁",作用是保证在同一时刻, 被修饰的代码块或方法只会有一个线程执行,以保证并发安全。这里是把这个方法锁住了
|
|
|
|
|
// 运行到这个方法时,要检查一下有没有其它线程正在用这个方法(或者该类的其他同步方法)
|
|
|
|
|
// (1)有:要等正在使用synchronized方法的线程运行完后再运行此线程(2)没有:锁定调用者,可以直接运行。
|
|
|
|
|
|
|
|
|
|
if (sInstance == null) {// 判断如果当前备份为null,就新声明一个
|
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
|
}
|
|
|
|
|
return sInstance;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
|
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
|
private BackupUtils(Context context) {// 初始化函数
|
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean externalStorageAvailable() {
|
|
|
|
|
private static boolean externalStorageAvailable() {// 检测外部存储功能是否可用
|
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int exportToText() {
|
|
|
|
|
public int exportToText() {// 导出文本
|
|
|
|
|
return mTextExport.exportToText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
|
public String getExportedTextFileName() {// 获取导出的文本文件名称
|
|
|
|
|
return mTextExport.mFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
|
public String getExportedTextFileDir() {// 获取导出的文本文件的文件地址
|
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class TextExport {
|
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
|
private static class TextExport {// 导出工具类
|
|
|
|
|
private static final String[] NOTE_PROJECTION = { // 把NotesNolumn的一些拿作出为NOTE_PROJECTION
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.MODIFIED_DATE,
|
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
@ -99,7 +102,7 @@ public class BackupUtils {
|
|
|
|
|
|
|
|
|
|
private static final int NOTE_COLUMN_SNIPPET = 2;
|
|
|
|
|
|
|
|
|
|
private static final String[] DATA_PROJECTION = {
|
|
|
|
|
private static final String[] DATA_PROJECTION = { //// 把NotesNolumn的一些拿作出为DATA_PROJECTION
|
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
|
DataColumns.DATA1,
|
|
|
|
@ -116,23 +119,23 @@ public class BackupUtils {
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
mFileName = "";
|
|
|
|
|
mFileDirectory = "";
|
|
|
|
|
mFileName = "";// 设置文件名称
|
|
|
|
|
mFileDirectory = "";// 设置导出文件地址
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getFormat(int id) {
|
|
|
|
|
private String getFormat(int id) {// 获取文本的组成部分
|
|
|
|
|
return TEXT_FORMAT[id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -140,25 +143,25 @@ public class BackupUtils {
|
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
|
*/
|
|
|
|
|
private void exportFolderToText(String folderId, PrintStream ps) {
|
|
|
|
|
// Query notes belong to this folder
|
|
|
|
|
// 通过查询PARENT_ID等于folderId的note来选出制定ID文件夹下的Note,默认输出为ID升序
|
|
|
|
|
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
|
|
|
|
|
folderId
|
|
|
|
|
folderId
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
if (notesCursor != null) {
|
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
|
if (notesCursor.moveToFirst()) {// 判断cursor是否移到了第一条上
|
|
|
|
|
do {
|
|
|
|
|
// Print note's last modified date
|
|
|
|
|
// 打印note最后一次更新的日期,ps里面保存有这份note的日期
|
|
|
|
|
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
|
|
|
|
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
|
exportNoteToText(noteId, ps);// 将文件导出到text
|
|
|
|
|
} while (notesCursor.moveToNext());// 使用moveToNext逐条读取
|
|
|
|
|
}
|
|
|
|
|
notesCursor.close();
|
|
|
|
|
notesCursor.close();// 关闭notesCursor且释放内存
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -166,13 +169,13 @@ public class BackupUtils {
|
|
|
|
|
* 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,
|
|
|
|
|
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, // 查询DataColumns的NOTE_ID等于传入的string型的noteId的note,默认为升序
|
|
|
|
|
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
|
|
|
|
|
noteId
|
|
|
|
|
noteId
|
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
if (dataCursor != null) {
|
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
|
if (dataCursor != null) {// 利用光标来扫描内容,区别为callnote和note两种,靠ps.printline输出
|
|
|
|
|
if (dataCursor.moveToFirst()) {// 从第一条开始遍历
|
|
|
|
|
do {
|
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
|
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
|
|
|
@ -181,31 +184,31 @@ public class BackupUtils {
|
|
|
|
|
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),
|
|
|
|
|
if (!TextUtils.isEmpty(phoneNumber)) { // 判断是否为空字符,不为空则打印phoneNumber
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 将phoneNumber格式化为和FORMAT_NOTE_CONTENT一样的格式后再打印
|
|
|
|
|
phoneNumber));
|
|
|
|
|
}
|
|
|
|
|
// Print call date
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat// 格式化为和FORMAT_NOTE_CONTENT一样的格式后再打印
|
|
|
|
|
.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),
|
|
|
|
|
if (!TextUtils.isEmpty(location)) {// 判断location是否为空字符
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 不为空则转换格式后打印location
|
|
|
|
|
location));
|
|
|
|
|
}
|
|
|
|
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
|
|
|
|
} else if (DataConstants.NOTE.equals(mimeType)) {// 判断是否mimeType和NOTE一致
|
|
|
|
|
String content = dataCursor.getString(DATA_COLUMN_CONTENT);
|
|
|
|
|
if (!TextUtils.isEmpty(content)) {
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
|
if (!TextUtils.isEmpty(content)) {// 判断content是否为空字符
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), // 不为空则转换格式和FORMAT_NOTE_CONTENT一致后打印content
|
|
|
|
|
content));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
dataCursor.close();
|
|
|
|
|
dataCursor.close();// 关闭dataCursor且释放内存
|
|
|
|
|
}
|
|
|
|
|
// print a line separator between note
|
|
|
|
|
// note之间打印line seperator
|
|
|
|
|
try {
|
|
|
|
|
ps.write(new byte[] {
|
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
@ -218,61 +221,63 @@ 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;
|
|
|
|
|
public int exportToText() {// 通过调用上面的定义过的exportFolder和exportNote方法实现导出note为text
|
|
|
|
|
if (!externalStorageAvailable()) {// 判断是否外部存储是被允许的
|
|
|
|
|
Log.d(TAG, "Media was not mounted");// 不被允许就log提醒
|
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;// 返回sd卡为被装入手机,也就是返回0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrintStream ps = getExportToTextPrintStream();
|
|
|
|
|
if (ps == null) {
|
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
|
if (ps == null) {// 判断ps是否为空
|
|
|
|
|
Log.e(TAG, "get print stream error");// 为空则log提醒并返回STATE_SYSTEM_ERROR代表的数值,即返回3
|
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
// First export folder and its notes
|
|
|
|
|
Cursor folderCursor = mContext.getContentResolver().query(
|
|
|
|
|
Cursor folderCursor = mContext.getContentResolver().query(// 查询赋给folderCursor进行初始化
|
|
|
|
|
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);
|
|
|
|
|
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER,
|
|
|
|
|
null, null);
|
|
|
|
|
|
|
|
|
|
if (folderCursor != null) {
|
|
|
|
|
if (folderCursor != null) {// 赋值成功则从第一个开始遍历
|
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// Print folder's name
|
|
|
|
|
// 针对两种情况对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);
|
|
|
|
|
} else {
|
|
|
|
|
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET);
|
|
|
|
|
}
|
|
|
|
|
if (!TextUtils.isEmpty(folderName)) {
|
|
|
|
|
if (!TextUtils.isEmpty(folderName)) {// 不为空则打印folderName
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName));
|
|
|
|
|
}
|
|
|
|
|
String folderId = folderCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportFolderToText(folderId, ps);
|
|
|
|
|
exportFolderToText(folderId, ps);// 以id为标识导出ps
|
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
|
// 将根目录里的便签导出(由于不属于任何文件夹,因此无法通过文件夹导出来实现这一部分便签的导出)
|
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(// 查询PARENT_ID等于0且NoteColumns.TYPE等于Notes.TYPE_NOTE的结果赋给noteCursor
|
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
|
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
|
|
|
|
|
+ "=0", null, null);
|
|
|
|
|
+ "=0",
|
|
|
|
|
null, null);// 默认升序
|
|
|
|
|
|
|
|
|
|
if (noteCursor != null) {
|
|
|
|
|
if (noteCursor.moveToFirst()) {
|
|
|
|
|
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);
|
|
|
|
|
exportNoteToText(noteId, ps);// 调用exportNoteToText进行导出
|
|
|
|
|
} while (noteCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
noteCursor.close();
|
|
|
|
@ -288,20 +293,20 @@ public class BackupUtils {
|
|
|
|
|
private PrintStream getExportToTextPrintStream() {
|
|
|
|
|
File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
|
|
|
|
|
R.string.file_name_txt_format);
|
|
|
|
|
if (file == null) {
|
|
|
|
|
if (file == null) {// file为空则产生file失败,log提醒后返回空
|
|
|
|
|
Log.e(TAG, "create file to exported failed");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
mFileName = file.getName();
|
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);
|
|
|
|
|
mFileName = file.getName();// 调用则返回一个字符串值,该值是给定File对象的名称
|
|
|
|
|
mFileDirectory = mContext.getString(R.string.file_path);// 得到文件地址,返回string
|
|
|
|
|
PrintStream ps = null;
|
|
|
|
|
try {
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
ps = new PrintStream(fos);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
ps = new PrintStream(fos);// 给ps赋值
|
|
|
|
|
} catch (FileNotFoundException e) {// 赋值失败,打开文件失败则将此可抛出对象及其回溯信息打印到标准错误流且返回空
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
|
} catch (NullPointerException e) {// 空指针异常则将此可抛出对象及其回溯信息打印到标准错误流且返回空
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -312,7 +317,7 @@ public class BackupUtils {
|
|
|
|
|
/**
|
|
|
|
|
* Generate the text file to store imported data
|
|
|
|
|
*/
|
|
|
|
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
|
|
|
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {// 产生一个文件并导出到SD卡上
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append(Environment.getExternalStorageDirectory());
|
|
|
|
|
sb.append(context.getString(filePathResId));
|
|
|
|
@ -340,5 +345,3 @@ public class BackupUtils {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|