|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
|
|
|
|
|
|
import android.content.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";
|
|
|
|
|
// 单例模式,确保只有一个BackupUtils实例
|
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
|
|
|
|
|
|
// 获取BackupUtils的单例实例
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
|
if (sInstance == null) {
|
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
|
}
|
|
|
|
|
return sInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 备份或恢复状态标识符
|
|
|
|
|
*/
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化TextExport类
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查外部存储(SD卡)是否可用
|
|
|
|
|
private static boolean externalStorageAvailable() {
|
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 导出笔记为文本格式
|
|
|
|
|
public int exportToText() {
|
|
|
|
|
return mTextExport.exportToText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出的文本文件名
|
|
|
|
|
public String getExportedTextFileName() {
|
|
|
|
|
return mTextExport.mFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出的文本文件所在的目录
|
|
|
|
|
public String getExportedTextFileDir() {
|
|
|
|
|
return mTextExport.mFileDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 内部类,用于处理笔记的文本导出
|
|
|
|
|
private static class TextExport {
|
|
|
|
|
private static final String[] NOTE_PROJECTION = {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
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 String[] DATA_PROJECTION = {
|
|
|
|
|
DataColumns.CONTENT,
|
|
|
|
|
DataColumns.MIME_TYPE,
|
|
|
|
|
DataColumns.DATA1,
|
|
|
|
|
DataColumns.DATA2,
|
|
|
|
|
DataColumns.DATA3,
|
|
|
|
|
DataColumns.DATA4,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static final int DATA_COLUMN_CONTENT = 0;
|
|
|
|
|
private static final int DATA_COLUMN_MIME_TYPE = 1;
|
|
|
|
|
private static final int DATA_COLUMN_CALL_DATE = 2;
|
|
|
|
|
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 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 = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据格式ID获取相应的格式化字符串
|
|
|
|
|
private String getFormat(int id) {
|
|
|
|
|
return TEXT_FORMAT[id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将指定文件夹内的笔记导出为文本格式
|
|
|
|
|
* @param folderId 文件夹的ID
|
|
|
|
|
* @param ps 输出流,用于写入导出的数据
|
|
|
|
|
*/
|
|
|
|
|
private void exportFolderToText(String folderId, PrintStream ps) {
|
|
|
|
|
// 查询属于该文件夹的笔记
|
|
|
|
|
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 {
|
|
|
|
|
// 打印笔记的最后修改日期
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
|
|
|
|
|
mContext.getString(R.string.format_datetime_mdhm),
|
|
|
|
|
notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
|
|
|
|
|
// 查询与该笔记相关的数据
|
|
|
|
|
String noteId = notesCursor.getString(NOTE_COLUMN_ID);
|
|
|
|
|
exportNoteToText(noteId, ps);
|
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
notesCursor.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将指定笔记的内容导出为文本格式
|
|
|
|
|
* @param noteId 笔记的ID
|
|
|
|
|
* @param ps 输出流,用于写入导出的数据
|
|
|
|
|
*/
|
|
|
|
|
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 {
|
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
|
|
if (DataConstants.CALL_NOTE.equals(mimeType)) {
|
|
|
|
|
// 导出电话记录相关信息
|
|
|
|
|
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
|
|
|
|
|
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),
|
|
|
|
|
phoneNumber));
|
|
|
|
|
}
|
|
|
|
|
// 打印电话日期
|
|
|
|
|
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
|
|
|
|
|
.format(mContext.getString(R.string.format_datetime_mdhm),
|
|
|
|
|
callDate)));
|
|
|
|
|
// 打印电话记录的地点
|
|
|
|
|
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),
|
|
|
|
|
content));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (dataCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
dataCursor.close();
|
|
|
|
|
}
|
|
|
|
|
// 在每个笔记之间打印一个分隔符
|
|
|
|
|
try {
|
|
|
|
|
ps.write(new byte[] {
|
|
|
|
|
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
|
|
|
|
|
});
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将笔记导出为用户可读的文本格式
|
|
|
|
|
*/
|
|
|
|
|
public int exportToText() {
|
|
|
|
|
if (!externalStorageAvailable()) {
|
|
|
|
|
Log.d(TAG, "SD卡未挂载");
|
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PrintStream ps = getExportToTextPrintStream();
|
|
|
|
|
if (ps == null) {
|
|
|
|
|
Log.e(TAG, "获取输出流失败");
|
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 首先导出文件夹及其笔记
|
|
|
|
|
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.moveToFirst()) {
|
|
|
|
|
do {
|
|
|
|
|
// 打印文件夹的名称
|
|
|
|
|
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));
|
|
|
|
|
exportFolderToText(folderCursor.getString(NOTE_COLUMN_ID), ps);
|
|
|
|
|
}
|
|
|
|
|
} while (folderCursor.moveToNext());
|
|
|
|
|
}
|
|
|
|
|
folderCursor.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ps.close();
|
|
|
|
|
return STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取导出文本的输出流
|
|
|
|
|
private PrintStream getExportToTextPrintStream() {
|
|
|
|
|
// 获取文件路径
|
|
|
|
|
String path = Environment.getExternalStorageDirectory() + "/notesBackup";
|
|
|
|
|
File dir = new File(path);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
if (!dir.mkdirs()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置输出文件路径
|
|
|
|
|
mFileName = "NotesBackup_" + System.currentTimeMillis() + ".txt";
|
|
|
|
|
mFileDirectory = path;
|
|
|
|
|
File file = new File(path, mFileName);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
return new PrintStream(fos);
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|