|
|
|
@ -39,51 +39,50 @@ import java.io.PrintStream;
|
|
|
|
|
public class BackupUtils {
|
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
|
// Singleton stuff
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
|
private static BackupUtils sInstance;//在类内定义自身对象,可能会导致无限循环
|
|
|
|
|
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
|
if (sInstance == null) {
|
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
|
}
|
|
|
|
|
}//实例不存在时生成一个
|
|
|
|
|
return sInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//只允许一个线程调用它,不允许同时被多个进程使用
|
|
|
|
|
/**
|
|
|
|
|
* Following states are signs to represents backup or restore
|
|
|
|
|
* status
|
|
|
|
|
*/
|
|
|
|
|
// Currently, the sdcard is not mounted
|
|
|
|
|
// Currently, the sdcard is not mounted 卡没有装配
|
|
|
|
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
|
|
|
|
// The backup file not exist
|
|
|
|
|
// 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
|
|
|
|
|
// 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
|
|
|
|
|
// Some run-time exception which causes restore or backup fails 超时导致保存或备份失败
|
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3;
|
|
|
|
|
// Backup or restore success
|
|
|
|
|
// Backup or restore success 保存或备份成功
|
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
|
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
|
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
|
}
|
|
|
|
|
}//为mTextExport分配空间
|
|
|
|
|
|
|
|
|
|
private static boolean externalStorageAvailable() {
|
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
|
}
|
|
|
|
|
}//判断外部存储可用性
|
|
|
|
|
|
|
|
|
|
public int exportToText() {
|
|
|
|
|
return mTextExport.exportToText();
|
|
|
|
|
}
|
|
|
|
|
}//展示于文本
|
|
|
|
|
|
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
|
return mTextExport.mFileName;
|
|
|
|
|
}
|
|
|
|
|
}//get展示于文本的文件名
|
|
|
|
|
|
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
|
}
|
|
|
|
|
}//get展示于文本的文件目录
|
|
|
|
|
|
|
|
|
|
private static class TextExport {
|
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
@ -91,13 +90,13 @@ public class BackupUtils {
|
|
|
|
|
NoteColumns.MODIFIED_DATE,
|
|
|
|
|
NoteColumns.SNIPPET,
|
|
|
|
|
NoteColumns.TYPE
|
|
|
|
|
};
|
|
|
|
|
};//定义常量字符串
|
|
|
|
|
|
|
|
|
|
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 int NOTE_COLUMN_SNIPPET = 2;//定义常量参数
|
|
|
|
|
|
|
|
|
|
private static final String[] DATA_PROJECTION = {
|
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
@ -106,41 +105,41 @@ public class BackupUtils {
|
|
|
|
|
DataColumns.DATA2,
|
|
|
|
|
DataColumns.DATA3,
|
|
|
|
|
DataColumns.DATA4,
|
|
|
|
|
};
|
|
|
|
|
};//定义常量字符串
|
|
|
|
|
|
|
|
|
|
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 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;
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
}//get文本的形式
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
|
*/
|
|
|
|
|
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,
|
|
|
|
|
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
|
|
|
|
|
folderId
|
|
|
|
@ -149,16 +148,16 @@ public class BackupUtils {
|
|
|
|
|
if (notesCursor != null) {
|
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// Print note's last modified date
|
|
|
|
|
// 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
|
|
|
|
|
// Query data belong to this note 查询属于这个文本的数据
|
|
|
|
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
|
} while (notesCursor.moveToNext());//一直移到下一个直到最后停止循环
|
|
|
|
|
}
|
|
|
|
|
notesCursor.close();
|
|
|
|
|
notesCursor.close();//释放
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -176,7 +175,7 @@ public class BackupUtils {
|
|
|
|
|
do {
|
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
|
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
|
|
|
|
// Print phone number
|
|
|
|
|
// 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);
|
|
|
|
@ -184,35 +183,36 @@ public class BackupUtils {
|
|
|
|
|
if (!TextUtils.isEmpty(phoneNumber)) {
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
|
phoneNumber));
|
|
|
|
|
}
|
|
|
|
|
// Print call date
|
|
|
|
|
}//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
|
|
|
|
|
// Print call attachment location 给出呼叫的位置
|
|
|
|
|
if (!TextUtils.isEmpty(location)) {
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
|
|
|
|
|
location));
|
|
|
|
|
}
|
|
|
|
|
}//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));
|
|
|
|
|
}//content文本有效性
|
|
|
|
|
}
|
|
|
|
|
} while (dataCursor.moveToNext());//下一个直到最后停止循环
|
|
|
|
|
}
|
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
|
dataCursor.close();//释放
|
|
|
|
|
}
|
|
|
|
|
dataCursor.close();
|
|
|
|
|
}
|
|
|
|
|
// print a line separator between note
|
|
|
|
|
// print a line separator between note 在便签里给出一个行分隔符
|
|
|
|
|
try {
|
|
|
|
|
ps.write(new byte[] {
|
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
|
});
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
}//检查 撰写行分离与字母号码
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
}
|
|
|
|
|
}//抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -222,14 +222,14 @@ public class BackupUtils {
|
|
|
|
|
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
|
|
|
|
|
// First export folder and its notes //第一个展示的文件和它的便签
|
|
|
|
|
Cursor folderCursor = mContext.getContentResolver().query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
@ -240,7 +240,7 @@ public class BackupUtils {
|
|
|
|
|
if (folderCursor != null) {
|
|
|
|
|
if (folderCursor.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// Print folder's name
|
|
|
|
|
// 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);
|
|
|
|
@ -257,7 +257,7 @@ public class BackupUtils {
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
|
// Export notes in root's folder 展示在根文件的便签
|
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
@ -270,17 +270,17 @@ public class BackupUtils {
|
|
|
|
|
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
|
|
|
|
|
// Query data belong to this note 查询属于这个便签的数据
|
|
|
|
|
String noteId = noteCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (noteCursor.moveToNext());
|
|
|
|
|
} while (noteCursor.moveToNext());//直到最后一个停止循环
|
|
|
|
|
}
|
|
|
|
|
noteCursor.close();
|
|
|
|
|
noteCursor.close();//释放
|
|
|
|
|
}
|
|
|
|
|
ps.close();
|
|
|
|
|
ps.close();//释放
|
|
|
|
|
|
|
|
|
|
return STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}//展示于文本
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a print stream pointed to the file {@generateExportedTextFile}
|
|
|
|
@ -298,7 +298,8 @@ public class BackupUtils {
|
|
|
|
|
try {
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
ps = new PrintStream(fos);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
}//输出到指定文件
|
|
|
|
|
catch (FileNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
@ -307,15 +308,15 @@ public class BackupUtils {
|
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
sb.append(Environment.getExternalStorageDirectory());//外部(SD卡)的存储路径
|
|
|
|
|
sb.append(context.getString(filePathResId));//文件的存储路径
|
|
|
|
|
File filedir = new File(sb.toString());
|
|
|
|
|
sb.append(context.getString(
|
|
|
|
|
fileNameFormatResId,
|
|
|
|
@ -331,7 +332,8 @@ public class BackupUtils {
|
|
|
|
|
file.createNewFile();
|
|
|
|
|
}
|
|
|
|
|
return file;
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
}//不存在文件则新建
|
|
|
|
|
catch (SecurityException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|