parent
68895e7abc
commit
579ff9ecd8
@ -0,0 +1,251 @@
|
|||||||
|
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 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";
|
||||||
|
|
||||||
|
// 单例实例
|
||||||
|
private static BackupUtils sInstance;
|
||||||
|
|
||||||
|
// 获取实例的静态方法,确保在整个应用程序中只有一个实例存在
|
||||||
|
public static synchronized BackupUtils getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new BackupUtils(context);
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据导出类
|
||||||
|
private TextExport mTextExport;
|
||||||
|
|
||||||
|
// 私有构造函数
|
||||||
|
private BackupUtils(Context context) {
|
||||||
|
mTextExport = new TextExport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查外部存储是否可用
|
||||||
|
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 String[] DATA_PROJECTION = {
|
||||||
|
DataColumns.CONTENT,
|
||||||
|
DataColumns.MIME_TYPE,
|
||||||
|
DataColumns.DATA1,
|
||||||
|
DataColumns.DATA2,
|
||||||
|
DataColumns.DATA3,
|
||||||
|
DataColumns.DATA4,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 笔记和数据表的列索引
|
||||||
|
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 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 = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文本格式字符串
|
||||||
|
private String getFormat(int id) {
|
||||||
|
return TEXT_FORMAT[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出文件夹到文本
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出笔记到文本
|
||||||
|
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)) {
|
||||||
|
// 导出电话笔记内容
|
||||||
|
// ...
|
||||||
|
} else if (DataConstants.NOTE.equals(mimeType)) {
|
||||||
|
// 导出普通笔记内容
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
} while (dataCursor.moveToNext());
|
||||||
|
}
|
||||||
|
dataCursor.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出到文本文件的主方法
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出文件夹和笔记到文本文件
|
||||||
|
// ...
|
||||||
|
|
||||||
|
return STATE_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取导出到文本文件的打印流
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出状态常量
|
||||||
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
||||||
|
public static final int STATE_SUCCESS = 4;
|
||||||
|
public static final int STATE_SYSTEM_ERROR = 3;
|
||||||
|
|
||||||
|
// 构造导出工具
|
||||||
|
public BackupUtils(Context context) {
|
||||||
|
mTextExport = new TextExport(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 外部存储是否可用
|
||||||
|
private static boolean externalStorageAvailable
|
||||||
|
// 外部存储是否可用
|
||||||
|
private static boolean externalStorageAvailable() {
|
||||||
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成文件存储在SD卡上
|
||||||
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append(Environment.getExternalStorageDirectory()); // 获取SD卡路径
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue