|
|
@ -16,33 +16,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.tool;
|
|
|
|
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 {
|
|
|
|
public class BackupUtils {
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
private static final String TAG = "BackupUtils";
|
|
|
|
// Singleton stuff
|
|
|
|
// Singleton stuff
|
|
|
|
private static BackupUtils sInstance;
|
|
|
|
private static BackupUtils sInstance; //类里面为什么可以定义自身类的对象?
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
public static synchronized BackupUtils getInstance(Context context) {
|
|
|
|
|
|
|
|
//ynchronized 关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A)
|
|
|
|
|
|
|
|
//运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行。
|
|
|
|
|
|
|
|
//它包括两种用法:synchronized 方法和 synchronized 块。
|
|
|
|
if (sInstance == null) {
|
|
|
|
if (sInstance == null) {
|
|
|
|
|
|
|
|
//如果当前备份不存在,则新声明一个
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
sInstance = new BackupUtils(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
return sInstance;
|
|
|
@ -52,24 +36,24 @@ public class BackupUtils {
|
|
|
|
* Following states are signs to represents backup or restore
|
|
|
|
* Following states are signs to represents backup or restore
|
|
|
|
* status
|
|
|
|
* status
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
// Currently, the sdcard is not mounted
|
|
|
|
// Currently, the sdcard is not mounted SD卡没有被装入手机
|
|
|
|
public static final int STATE_SD_CARD_UNMOUONTED = 0;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
public static final int STATE_SYSTEM_ERROR = 3;
|
|
|
|
// Backup or restore success
|
|
|
|
// Backup or restore success 成功存储
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
public static final int STATE_SUCCESS = 4;
|
|
|
|
|
|
|
|
|
|
|
|
private TextExport mTextExport;
|
|
|
|
private TextExport mTextExport;
|
|
|
|
|
|
|
|
|
|
|
|
private BackupUtils(Context context) {
|
|
|
|
private BackupUtils(Context context) { //初始化函数
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
mTextExport = new TextExport(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean externalStorageAvailable() {
|
|
|
|
private static boolean externalStorageAvailable() { //外部存储功能是否可用
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -128,11 +112,11 @@ public class BackupUtils {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -140,7 +124,7 @@ public class BackupUtils {
|
|
|
|
* Export the folder identified by folder id to text
|
|
|
|
* 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
|
|
|
|
// Query notes belong to this folder 通过查询parent id是文件夹id的note来选出制定ID文件夹下的Note
|
|
|
|
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
|
|
|
@ -149,13 +133,13 @@ public class BackupUtils {
|
|
|
|
if (notesCursor != null) {
|
|
|
|
if (notesCursor != null) {
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
if (notesCursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
// Print note's last modified date
|
|
|
|
// Print note's last modified date ps里面保存有这份note的日期
|
|
|
|
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
|
|
|
|
// 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); //将文件导出到text
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
} while (notesCursor.moveToNext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notesCursor.close();
|
|
|
|
notesCursor.close();
|
|
|
@ -171,7 +155,7 @@ public class BackupUtils {
|
|
|
|
noteId
|
|
|
|
noteId
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (dataCursor != null) {
|
|
|
|
if (dataCursor != null) { //利用光标来扫描内容,区别为callnote和note两种,靠ps.printline输出
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
if (dataCursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
|
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE);
|
|
|
@ -181,7 +165,7 @@ public class BackupUtils {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -218,7 +202,7 @@ public class BackupUtils {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Note will be exported as text which is user readable
|
|
|
|
* Note will be exported as text which is user readable
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public int exportToText() {
|
|
|
|
public int exportToText() { //总函数,调用上面的exportFolder和exportNote
|
|
|
|
if (!externalStorageAvailable()) {
|
|
|
|
if (!externalStorageAvailable()) {
|
|
|
|
Log.d(TAG, "Media was not mounted");
|
|
|
|
Log.d(TAG, "Media was not mounted");
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
|
return STATE_SD_CARD_UNMOUONTED;
|
|
|
@ -229,7 +213,7 @@ public class BackupUtils {
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
Log.e(TAG, "get print stream error");
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
return STATE_SYSTEM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// First export folder and its notes
|
|
|
|
// 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,
|
|
|
@ -257,7 +241,7 @@ public class BackupUtils {
|
|
|
|
folderCursor.close();
|
|
|
|
folderCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Export notes in root's folder
|
|
|
|
// Export notes in root's folder 将根目录里的便签导出(由于不属于任何文件夹,因此无法通过文件夹导出来实现这一部分便签的导出)
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
Cursor noteCursor = mContext.getContentResolver().query(
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
Notes.CONTENT_NOTE_URI,
|
|
|
|
NOTE_PROJECTION,
|
|
|
|
NOTE_PROJECTION,
|
|
|
@ -297,7 +281,7 @@ public class BackupUtils {
|
|
|
|
PrintStream ps = null;
|
|
|
|
PrintStream ps = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
ps = new PrintStream(fos);
|
|
|
|
ps = new PrintStream(fos); //将ps输出流输出到特定的文件,目的就是导出到文件,而不是直接输出
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
@ -314,16 +298,16 @@ public class BackupUtils {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
|
|
|
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append(Environment.getExternalStorageDirectory());
|
|
|
|
sb.append(Environment.getExternalStorageDirectory()); //外部(SD卡)的存储路径
|
|
|
|
sb.append(context.getString(filePathResId));
|
|
|
|
sb.append(context.getString(filePathResId)); //文件的存储路径
|
|
|
|
File filedir = new File(sb.toString());
|
|
|
|
File filedir = new File(sb.toString()); //filedir应该就是用来存储路径信息
|
|
|
|
sb.append(context.getString(
|
|
|
|
sb.append(context.getString(
|
|
|
|
fileNameFormatResId,
|
|
|
|
fileNameFormatResId,
|
|
|
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
|
|
|
DateFormat.format(context.getString(R.string.format_date_ymd),
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
System.currentTimeMillis())));
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
File file = new File(sb.toString());
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try { //如果这些文件不存在,则新建
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
if (!filedir.exists()) {
|
|
|
|
filedir.mkdir();
|
|
|
|
filedir.mkdir();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -336,9 +320,10 @@ public class BackupUtils {
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try catch 异常处理
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|