From 86b3b1879f7600faf8acb69e9fca9f45c379a8cd Mon Sep 17 00:00:00 2001 From: feng <2775873389@qq.com> Date: Wed, 10 May 2023 19:10:11 +0800 Subject: [PATCH] =?UTF-8?q?tool/BackupUtils.java=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/tool/BackupUtils.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/src/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java index 39f6ec4..f8bacc8 100644 --- a/src/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java +++ b/src/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -39,10 +39,14 @@ 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) { + //ynchronized 关键字,代表这个方法加锁,相当于不管哪一个线程(例如线程A) + //运行到这个方法时,都要检查有没有其它线程B(或者C、 D等)正在用这个方法(或者该类的其他同步方法),有的话要等正在使用synchronized方法的线程B(或者C 、D)运行完这个方法后再运行此线程A,没有的话,锁定调用者,然后直接运行 + //它包括两种用法:synchronized 方法和 synchronized 块 if (sInstance == null) { + //如果当前备份不存在,则新声明一个 sInstance = new BackupUtils(context); } return sInstance; @@ -52,24 +56,24 @@ public class BackupUtils { * Following states are signs to represents backup or restore * status */ - // Currently, the sdcard is not mounted + // Currently, the sdcard is not mounted SD卡没有被装入手机 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); - } + } //初始化函数 - private static boolean externalStorageAvailable() { + private static boolean externalStorageAvailable() { //外部存储功能是否可用 return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } @@ -128,19 +132,19 @@ public class BackupUtils { public TextExport(Context context) { TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); mContext = context; - mFileName = ""; + mFileName = ""; //为什么为空? mFileDirectory = ""; } private String getFormat(int id) { return TEXT_FORMAT[id]; - } + } //获取文本的组成部分 /** * 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 通过查询parent id是文件夹id的note来选出制定ID文件夹下的Note Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId @@ -149,13 +153,13 @@ public class BackupUtils { if (notesCursor != null) { if (notesCursor.moveToFirst()) { 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( 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); + exportNoteToText(noteId, ps); //将文件导出到text } while (notesCursor.moveToNext()); } notesCursor.close(); @@ -172,7 +176,7 @@ public class BackupUtils { }, null); if (dataCursor != null) { - if (dataCursor.moveToFirst()) { + if (dataCursor.moveToFirst()) { //利用光标来扫描内容,区别为callnote和note两种,靠ps.printline输出 do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { @@ -181,7 +185,7 @@ public class BackupUtils { long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); String location = dataCursor.getString(DATA_COLUMN_CONTENT); - if (!TextUtils.isEmpty(phoneNumber)) { + if (!TextUtils.isEmpty(phoneNumber)) { //判断是否为空字符 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } @@ -218,7 +222,8 @@ public class BackupUtils { /** * Note will be exported as text which is user readable */ - public int exportToText() { + public int exportToText() { //总函数,调用上面的exportFolder和exportNote + if (!externalStorageAvailable()) { Log.d(TAG, "Media was not mounted"); return STATE_SD_CARD_UNMOUONTED; @@ -229,7 +234,7 @@ public class BackupUtils { 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, @@ -297,7 +302,7 @@ public class BackupUtils { PrintStream ps = null; try { FileOutputStream fos = new FileOutputStream(file); - ps = new PrintStream(fos); + ps = new PrintStream(fos); //将ps输出流输出到特定的文件,目的就是导出到文件,而不是直接输出 } catch (FileNotFoundException e) { e.printStackTrace(); return null; @@ -314,16 +319,16 @@ public class BackupUtils { */ private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { StringBuilder sb = new StringBuilder(); - sb.append(Environment.getExternalStorageDirectory()); - sb.append(context.getString(filePathResId)); - File filedir = new File(sb.toString()); + sb.append(Environment.getExternalStorageDirectory()); //外部(SD卡)的存储路径 + sb.append(context.getString(filePathResId)); //文件的存储路径 + File filedir = new File(sb.toString()); //filedir应该就是用来存储路径信息 sb.append(context.getString( fileNameFormatResId, DateFormat.format(context.getString(R.string.format_date_ymd), System.currentTimeMillis()))); File file = new File(sb.toString()); - try { + try { //如果这些文件不存在,则新建 if (!filedir.exists()) { filedir.mkdir(); } @@ -336,7 +341,7 @@ public class BackupUtils { } catch (IOException e) { e.printStackTrace(); } - + // try catch 异常处理 return null; } }