tool代码注释

pull/1/head
Wang-YYu-Lu 2 months ago
parent 44afe2c8c1
commit 06ce2ef0cc

@ -2,234 +2,277 @@
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * License 使
* You may obtain a copy of the License at * License
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * License
* distributed under the License is distributed on an "AS IS" BASIS, * License
* 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; package net.micode.notes.tool; // 指定代码所属的包名
import android.content.Context; import android.content.Context; // 导入Context类提供应用环境信息
import android.database.Cursor; import android.database.Cursor; // 导入Cursor类用于查询数据库结果集
import android.os.Environment; import android.os.Environment; // 导入Environment类获取外部存储信息
import android.text.TextUtils; import android.text.TextUtils; // 导入TextUtils类提供文本处理工具
import android.text.format.DateFormat; import android.text.format.DateFormat; // 导入DateFormat类用于日期格式化
import android.util.Log; import android.util.Log; // 导入Log类用于日志记录
import net.micode.notes.R; import net.micode.notes.R; // 导入R资源类访问应用资源
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes; // 导入Notes类访问笔记数据结构
import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataColumns; // 导入笔记数据列定义
import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.DataConstants; // 导入笔记数据常量
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns; // 导入笔记列定义
import java.io.File; import java.io.File; // 导入File类用于文件操作
import java.io.FileNotFoundException; import java.io.FileNotFoundException; // 导入文件未找到异常类
import java.io.FileOutputStream; import java.io.FileOutputStream; // 导入文件输出流类
import java.io.IOException; import java.io.IOException; // 导入IO异常类
import java.io.PrintStream; import java.io.PrintStream; // 导入打印流类,用于文本输出
/**
*
* SD
*/
public class BackupUtils { public class BackupUtils {
private static final String TAG = "BackupUtils"; private static final String TAG = "BackupUtils"; // 日志标签
// Singleton stuff // 单例模式实现
private static BackupUtils sInstance; private static BackupUtils sInstance;
/**
* BackupUtils
* @param context
* @return BackupUtils
*/
public static synchronized BackupUtils getInstance(Context context) { public static synchronized BackupUtils getInstance(Context context) {
if (sInstance == null) { if (sInstance == null) { // 检查实例是否已创建
sInstance = new BackupUtils(context); sInstance = new BackupUtils(context); // 创建新实例
} }
return sInstance; return sInstance; // 返回实例
} }
/** /**
* Following states are signs to represents backup or restore *
* status
*/ */
// 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 // 备份文件不存在
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 // 数据格式不正确,可能被其他程序修改
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 // 运行时异常导致备份或恢复失败
public static final int STATE_SYSTEM_ERROR = 3; public static final int STATE_SYSTEM_ERROR = 3;
// Backup or restore success // 备份或恢复成功
public static final int STATE_SUCCESS = 4; public static final int STATE_SUCCESS = 4;
private TextExport mTextExport; private TextExport mTextExport; // 文本导出器实例
/**
*
* @param context
*/
private BackupUtils(Context context) { private BackupUtils(Context context) {
mTextExport = new TextExport(context); mTextExport = new TextExport(context); // 初始化文本导出器
} }
/**
*
* @return truefalse
*/
private static boolean externalStorageAvailable() { private static boolean externalStorageAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); // 检查SD卡挂载状态
} }
/**
*
* @return
*/
public int exportToText() { public int exportToText() {
return mTextExport.exportToText(); return mTextExport.exportToText(); // 调用文本导出器的导出方法
} }
/**
*
* @return
*/
public String getExportedTextFileName() { public String getExportedTextFileName() {
return mTextExport.mFileName; return mTextExport.mFileName; // 返回导出的文件名
} }
/**
*
* @return
*/
public String getExportedTextFileDir() { public String getExportedTextFileDir() {
return mTextExport.mFileDirectory; return mTextExport.mFileDirectory; // 返回导出的文件目录
} }
/**
*
*/
private static class TextExport { private static class TextExport {
// 笔记查询投影,指定要查询的列
private static final String[] NOTE_PROJECTION = { private static final String[] NOTE_PROJECTION = {
NoteColumns.ID, NoteColumns.ID, // 笔记ID
NoteColumns.MODIFIED_DATE, NoteColumns.MODIFIED_DATE, // 修改日期
NoteColumns.SNIPPET, NoteColumns.SNIPPET, // 摘要
NoteColumns.TYPE NoteColumns.TYPE // 类型
}; };
private static final int NOTE_COLUMN_ID = 0; // 笔记列索引常量
private static final int NOTE_COLUMN_ID = 0; // ID列索引
private static final int NOTE_COLUMN_MODIFIED_DATE = 1; 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 = { private static final String[] DATA_PROJECTION = {
DataColumns.CONTENT, DataColumns.CONTENT, // 内容
DataColumns.MIME_TYPE, DataColumns.MIME_TYPE, // MIME类型
DataColumns.DATA1, DataColumns.DATA1, // 数据1通话日期
DataColumns.DATA2, DataColumns.DATA2, // 数据2
DataColumns.DATA3, DataColumns.DATA3, // 数据3
DataColumns.DATA4, DataColumns.DATA4, // 数据4电话号码
}; };
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; // MIME类型列索引
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 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_FOLDER_NAME = 0; // 文件夹名称格式索引
private static final int FORMAT_NOTE_CONTENT = 2; private static final int FORMAT_NOTE_DATE = 1; // 笔记日期格式索引
private static final int FORMAT_NOTE_CONTENT = 2; // 笔记内容格式索引
private Context mContext; private Context mContext; // 应用上下文
private String mFileName; private String mFileName; // 导出的文件名
private String mFileDirectory; private String mFileDirectory; // 导出的文件目录
/**
*
* @param context
*/
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 = ""; // 初始化文件目录
} }
/**
*
* @param id
* @return
*/
private String getFormat(int id) { private String getFormat(int id) {
return TEXT_FORMAT[id]; return TEXT_FORMAT[id]; // 返回指定索引的格式字符串
} }
/** /**
* Export the folder identified by folder id to text *
* @param folderId ID
* @param ps
*/ */
private void exportFolderToText(String folderId, PrintStream ps) { private void exportFolderToText(String folderId, PrintStream ps) {
// Query notes belong to this folder // 查询属于该文件夹的所有笔记
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
}, null); }, null);
if (notesCursor != null) { if (notesCursor != null) { // 检查游标是否有效
if (notesCursor.moveToFirst()) { if (notesCursor.moveToFirst()) { // 移动到第一条记录
do { do {
// Print note's last modified date // 打印笔记的最后修改日期
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 // 查询属于该笔记的数据
String noteId = notesCursor.getString(NOTE_COLUMN_ID); String noteId = notesCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps); exportNoteToText(noteId, ps); // 导出笔记内容
} while (notesCursor.moveToNext()); } while (notesCursor.moveToNext()); // 移动到下一条记录
} }
notesCursor.close(); notesCursor.close(); // 关闭游标
} }
} }
/** /**
* Export note identified by id to a print stream * ID
* @param noteId ID
* @param ps
*/ */
private void exportNoteToText(String noteId, PrintStream ps) { private void exportNoteToText(String noteId, PrintStream ps) {
// 查询属于该笔记的所有数据
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId noteId
}, null); }, null);
if (dataCursor != null) { if (dataCursor != null) { // 检查游标是否有效
if (dataCursor.moveToFirst()) { if (dataCursor.moveToFirst()) { // 移动到第一条记录
do { do {
String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); // 获取MIME类型
if (DataConstants.CALL_NOTE.equals(mimeType)) { if (DataConstants.CALL_NOTE.equals(mimeType)) { // 处理通话记录类型
// Print phone number // 打印电话号码
String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER);
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)); // 打印电话号码
} }
// Print call date // 打印通话日期
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat
.format(mContext.getString(R.string.format_datetime_mdhm), .format(mContext.getString(R.string.format_datetime_mdhm),
callDate))); callDate)));
// Print call attachment location // 打印通话附件位置
if (!TextUtils.isEmpty(location)) { if (!TextUtils.isEmpty(location)) { // 检查位置信息是否为空
ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT),
location)); location)); // 打印位置信息
} }
} else if (DataConstants.NOTE.equals(mimeType)) { } else if (DataConstants.NOTE.equals(mimeType)) { // 处理普通笔记类型
String content = dataCursor.getString(DATA_COLUMN_CONTENT); String content = dataCursor.getString(DATA_COLUMN_CONTENT); // 获取笔记内容
if (!TextUtils.isEmpty(content)) { if (!TextUtils.isEmpty(content)) { // 检查内容是否为空
ps.println(String.format(getFormat(FORMAT_NOTE_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 // 在笔记之间打印行分隔符
try { try {
ps.write(new byte[] { ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER Character.LINE_SEPARATOR, Character.LETTER_NUMBER
}); });
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString()); // 记录异常信息
} }
} }
/** /**
* Note will be exported as text which is user readable *
* @return
*/ */
public int exportToText() { public int exportToText() {
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; // 返回SD卡未挂载状态
} }
PrintStream ps = getExportToTextPrintStream(); PrintStream ps = getExportToTextPrintStream(); // 获取打印流
if (ps == null) { if (ps == null) { // 检查打印流是否获取成功
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 // 首先导出文件夹及其包含的笔记
Cursor folderCursor = mContext.getContentResolver().query( Cursor folderCursor = mContext.getContentResolver().query(
Notes.CONTENT_NOTE_URI, Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NOTE_PROJECTION,
@ -237,108 +280,112 @@ public class BackupUtils {
+ NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR "
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null);
if (folderCursor != null) { if (folderCursor != null) { // 检查游标是否有效
if (folderCursor.moveToFirst()) { if (folderCursor.moveToFirst()) { // 移动到第一条记录
do { do {
// Print folder's name // 打印文件夹名称
String folderName = ""; String folderName = "";
if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) {
folderName = mContext.getString(R.string.call_record_folder_name); folderName = mContext.getString(R.string.call_record_folder_name); // 获取通话记录文件夹名称
} else { } else {
folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); // 获取文件夹摘要作为名称
} }
if (!TextUtils.isEmpty(folderName)) { if (!TextUtils.isEmpty(folderName)) { // 检查文件夹名称是否为空
ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); // 打印文件夹名称
} }
String folderId = folderCursor.getString(NOTE_COLUMN_ID); String folderId = folderCursor.getString(NOTE_COLUMN_ID); // 获取文件夹ID
exportFolderToText(folderId, ps); exportFolderToText(folderId, ps); // 导出文件夹中的笔记
} while (folderCursor.moveToNext()); } while (folderCursor.moveToNext()); // 移动到下一条记录
} }
folderCursor.close(); folderCursor.close(); // 关闭游标
} }
// 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,
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
+ "=0", null, null); + "=0", null, null);
if (noteCursor != null) { if (noteCursor != null) { // 检查游标是否有效
if (noteCursor.moveToFirst()) { if (noteCursor.moveToFirst()) { // 移动到第一条记录
do { do {
// 打印笔记的最后修改日期
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),
noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))));
// Query data belong to this note // 查询属于该笔记的数据
String noteId = noteCursor.getString(NOTE_COLUMN_ID); String noteId = noteCursor.getString(NOTE_COLUMN_ID);
exportNoteToText(noteId, ps); exportNoteToText(noteId, ps); // 导出笔记内容
} while (noteCursor.moveToNext()); } while (noteCursor.moveToNext()); // 移动到下一条记录
} }
noteCursor.close(); noteCursor.close(); // 关闭游标
} }
ps.close(); ps.close(); // 关闭打印流
return STATE_SUCCESS; return STATE_SUCCESS; // 返回成功状态
} }
/** /**
* Get a print stream pointed to the file {@generateExportedTextFile} *
* @return null
*/ */
private PrintStream getExportToTextPrintStream() { private PrintStream getExportToTextPrintStream() {
File file = generateFileMountedOnSDcard(mContext, R.string.file_path, File file = generateFileMountedOnSDcard(mContext, R.string.file_path,
R.string.file_name_txt_format); R.string.file_name_txt_format); // 生成SD卡上的文件
if (file == null) { if (file == null) { // 检查文件是否生成成功
Log.e(TAG, "create file to exported failed"); Log.e(TAG, "create file to exported failed"); // 记录错误日志
return null; return null; // 返回null表示失败
} }
mFileName = file.getName(); mFileName = file.getName(); // 保存文件名
mFileDirectory = mContext.getString(R.string.file_path); mFileDirectory = mContext.getString(R.string.file_path); // 保存文件目录
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); // 创建打印流
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) { // 处理文件未找到异常
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈
return null; return null; // 返回null表示失败
} catch (NullPointerException e) { } catch (NullPointerException e) { // 处理空指针异常
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈
return null; return null; // 返回null表示失败
} }
return ps; return ps; // 返回打印流
} }
} }
/** /**
* Generate the text file to store imported data * SD
* @param context
* @param filePathResId ID
* @param fileNameFormatResId ID
* @return null
*/ */
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()); // 添加外部存储目录
sb.append(context.getString(filePathResId)); sb.append(context.getString(filePathResId)); // 添加文件路径
File filedir = new File(sb.toString()); File filedir = new File(sb.toString()); // 创建目录对象
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(); // 创建目录
} }
if (!file.exists()) { if (!file.exists()) { // 检查文件是否存在
file.createNewFile(); file.createNewFile(); // 创建文件
} }
return file; return file; // 返回文件对象
} catch (SecurityException e) { } catch (SecurityException e) { // 处理安全异常
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈
} catch (IOException e) { } catch (IOException e) { // 处理IO异常
e.printStackTrace(); e.printStackTrace(); // 打印异常堆栈
} }
return null; return null; // 返回null表示失败
} }
} }

@ -2,294 +2,379 @@
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * License 使
* You may obtain a copy of the License at * License
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * License
* distributed under the License is distributed on an "AS IS" BASIS, * License
* 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; package net.micode.notes.tool; // 指定代码所属的包名
import android.content.ContentProviderOperation; import android.content.ContentProviderOperation; // 导入内容提供者操作类
import android.content.ContentProviderResult; import android.content.ContentProviderResult; // 导入内容提供者结果类
import android.content.ContentResolver; import android.content.ContentResolver; // 导入内容解析器类
import android.content.ContentUris; import android.content.ContentUris; // 导入内容URI工具类
import android.content.ContentValues; import android.content.ContentValues; // 导入内容值类
import android.content.OperationApplicationException; import android.content.OperationApplicationException; // 导入操作应用异常类
import android.database.Cursor; import android.database.Cursor; // 导入数据库游标类
import android.os.RemoteException; import android.os.RemoteException; // 导入远程异常类
import android.util.Log; import android.util.Log; // 导入日志记录类
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes; // 导入笔记数据类
import net.micode.notes.data.Notes.CallNote; import net.micode.notes.data.Notes.CallNote; // 导入通话记录数据类
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.NoteColumns; // 导入笔记列定义类
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 导入应用小部件属性类
import java.util.ArrayList;
import java.util.HashSet;
import java.util.ArrayList; // 导入ArrayList类
import java.util.HashSet; // 导入HashSet类
/**
*
*
*/
public class DataUtils { public class DataUtils {
public static final String TAG = "DataUtils"; public static final String TAG = "DataUtils"; // 日志标签
/**
*
* @param resolver
* @param ids ID
* @return truefalse
*/
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) { public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
if (ids == null) { if (ids == null) { // 检查ID集合是否为空
Log.d(TAG, "the ids is null"); Log.d(TAG, "the ids is null");
return true; return true; // 空集合视为操作成功
} }
if (ids.size() == 0) { if (ids.size() == 0) { // 检查ID集合是否为空
Log.d(TAG, "no id is in the hashset"); Log.d(TAG, "no id is in the hashset");
return true; return true; // 空集合视为操作成功
} }
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> operationList = new ArrayList<>(); // 创建操作列表
for (long id : ids) { for (long id : ids) { // 遍历ID集合
if(id == Notes.ID_ROOT_FOLDER) { if(id == Notes.ID_ROOT_FOLDER) { // 检查是否为根文件夹
Log.e(TAG, "Don't delete system folder root"); Log.e(TAG, "Don't delete system folder root");
continue; continue; // 跳过根文件夹,不删除
} }
ContentProviderOperation.Builder builder = ContentProviderOperation ContentProviderOperation.Builder builder = ContentProviderOperation
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); // 创建删除操作
operationList.add(builder.build()); operationList.add(builder.build()); // 将操作添加到列表
} }
try { try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); // 执行批量操作
if (results == null || results.length == 0 || results[0] == null) { if (results == null || results.length == 0 || results[0] == null) { // 检查操作结果
Log.d(TAG, "delete notes failed, ids:" + ids.toString()); Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false; return false; // 操作失败
} }
return true; return true; // 操作成功
} catch (RemoteException e) { } catch (RemoteException e) { // 处理远程异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) { } catch (OperationApplicationException e) { // 处理操作应用异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} }
return false; return false; // 发生异常,操作失败
} }
/**
*
* @param resolver
* @param id ID
* @param srcFolderId ID
* @param desFolderId ID
*/
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues(); // 创建内容值对象
values.put(NoteColumns.PARENT_ID, desFolderId); values.put(NoteColumns.PARENT_ID, desFolderId); // 设置目标文件夹ID
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 设置源文件夹ID
values.put(NoteColumns.LOCAL_MODIFIED, 1); values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); // 更新笔记
} }
/**
*
* @param resolver
* @param ids ID
* @param folderId ID
* @return truefalse
*/
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids, public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
long folderId) { long folderId) {
if (ids == null) { if (ids == null) { // 检查ID集合是否为空
Log.d(TAG, "the ids is null"); Log.d(TAG, "the ids is null");
return true; return true; // 空集合视为操作成功
} }
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); ArrayList<ContentProviderOperation> operationList = new ArrayList<>(); // 创建操作列表
for (long id : ids) { for (long id : ids) { // 遍历ID集合
ContentProviderOperation.Builder builder = ContentProviderOperation ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); // 创建更新操作
builder.withValue(NoteColumns.PARENT_ID, folderId); builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置目标文件夹ID
builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改
operationList.add(builder.build()); operationList.add(builder.build()); // 将操作添加到列表
} }
try { try {
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); // 执行批量操作
if (results == null || results.length == 0 || results[0] == null) { if (results == null || results.length == 0 || results[0] == null) { // 检查操作结果
Log.d(TAG, "delete notes failed, ids:" + ids.toString()); Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false; return false; // 操作失败
} }
return true; return true; // 操作成功
} catch (RemoteException e) { } catch (RemoteException e) { // 处理远程异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) { } catch (OperationApplicationException e) { // 处理操作应用异常
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} }
return false; return false; // 发生异常,操作失败
} }
/** /**
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} *
* @param resolver
* @return
*/ */
public static int getUserFolderCount(ContentResolver resolver) { public static int getUserFolderCount(ContentResolver resolver) {
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记表
new String[] { "COUNT(*)" }, new String[] { "COUNT(*)" }, // 查询数量
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 查询条件:类型为文件夹且不在回收站
new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, // 查询参数
null); null); // 排序方式
int count = 0; int count = 0; // 初始化数量为0
if(cursor != null) { if(cursor != null) { // 检查游标是否有效
if(cursor.moveToFirst()) { if(cursor.moveToFirst()) { // 移动到第一条记录
try { try {
count = cursor.getInt(0); count = cursor.getInt(0); // 获取数量值
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) { // 处理索引越界异常
Log.e(TAG, "get folder count failed:" + e.toString()); Log.e(TAG, "get folder count failed:" + e.toString());
} finally { } finally {
cursor.close(); cursor.close(); // 关闭游标
} }
} }
} }
return count; return count; // 返回文件夹数量
} }
/**
*
* @param resolver
* @param noteId ID
* @param type
* @return truefalse
*/
public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询指定笔记
null, null, // 查询所有列
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // 查询条件:类型匹配且不在回收站
new String [] {String.valueOf(type)}, new String [] {String.valueOf(type)}, // 查询参数
null); null); // 排序方式
boolean exist = false; boolean exist = false; // 初始化存在标志为false
if (cursor != null) { if (cursor != null) { // 检查游标是否有效
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) { // 检查记录数量
exist = true; exist = true; // 存在记录设置标志为true
} }
cursor.close(); cursor.close(); // 关闭游标
} }
return exist; return exist; // 返回存在标志
} }
/**
*
* @param resolver
* @param noteId ID
* @return truefalse
*/
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询指定笔记
null, null, null, null); null, // 查询所有列
null, // 无查询条件
null, // 无查询参数
null); // 排序方式
boolean exist = false; boolean exist = false; // 初始化存在标志为false
if (cursor != null) { if (cursor != null) { // 检查游标是否有效
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) { // 检查记录数量
exist = true; exist = true; // 存在记录设置标志为true
} }
cursor.close(); cursor.close(); // 关闭游标
} }
return exist; return exist; // 返回存在标志
} }
/**
*
* @param resolver
* @param dataId ID
* @return truefalse
*/
public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { public static boolean existInDataDatabase(ContentResolver resolver, long dataId) {
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), // 查询指定数据项
null, null, null, null); null, // 查询所有列
null, // 无查询条件
null, // 无查询参数
null); // 排序方式
boolean exist = false; boolean exist = false; // 初始化存在标志为false
if (cursor != null) { if (cursor != null) { // 检查游标是否有效
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) { // 检查记录数量
exist = true; exist = true; // 存在记录设置标志为true
} }
cursor.close(); cursor.close(); // 关闭游标
} }
return exist; return exist; // 返回存在标志
} }
/**
*
* @param resolver
* @param name
* @return truefalse
*/
public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { public static boolean checkVisibleFolderName(ContentResolver resolver, String name) {
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, // 查询笔记表
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + // 查询条件:类型为文件夹
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + // 且不在回收站
" AND " + NoteColumns.SNIPPET + "=?", " AND " + NoteColumns.SNIPPET + "=?", // 且名称匹配
new String[] { name }, null); new String[] { name }, null); // 查询参数
boolean exist = false; boolean exist = false; // 初始化存在标志为false
if(cursor != null) { if(cursor != null) { // 检查游标是否有效
if(cursor.getCount() > 0) { if(cursor.getCount() > 0) { // 检查记录数量
exist = true; exist = true; // 存在记录设置标志为true
} }
cursor.close(); cursor.close(); // 关闭游标
} }
return exist; return exist; // 返回存在标志
} }
/**
*
* @param resolver
* @param folderId ID
* @return
*/
public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) { public static HashSet<AppWidgetAttribute> getFolderNoteWidget(ContentResolver resolver, long folderId) {
Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记表
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, // 查询小部件ID和类型
NoteColumns.PARENT_ID + "=?", NoteColumns.PARENT_ID + "=?", // 查询条件父文件夹ID匹配
new String[] { String.valueOf(folderId) }, new String[] { String.valueOf(folderId) }, // 查询参数
null); null); // 排序方式
HashSet<AppWidgetAttribute> set = null; HashSet<AppWidgetAttribute> set = null; // 初始化集合为null
if (c != null) { if (c != null) { // 检查游标是否有效
if (c.moveToFirst()) { if (c.moveToFirst()) { // 移动到第一条记录
set = new HashSet<AppWidgetAttribute>(); set = new HashSet<>(); // 创建集合
do { do {
try { try {
AppWidgetAttribute widget = new AppWidgetAttribute(); AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建小部件属性对象
widget.widgetId = c.getInt(0); widget.widgetId = c.getInt(0); // 设置小部件ID
widget.widgetType = c.getInt(1); widget.widgetType = c.getInt(1); // 设置小部件类型
set.add(widget); set.add(widget); // 将小部件属性添加到集合
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) { // 处理索引越界异常
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
} while (c.moveToNext()); } while (c.moveToNext()); // 移动到下一条记录
} }
c.close(); c.close(); // 关闭游标
} }
return set; return set; // 返回集合
} }
/**
* ID
* @param resolver
* @param noteId ID
* @return
*/
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表
new String [] { CallNote.PHONE_NUMBER }, new String [] { CallNote.PHONE_NUMBER }, // 查询电话号码列
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 查询条件笔记ID匹配且类型为通话记录
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, // 查询参数
null); null); // 排序方式
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) { // 检查游标是否有效并移动到第一条记录
try { try {
return cursor.getString(0); return cursor.getString(0); // 返回电话号码
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) { // 处理索引越界异常
Log.e(TAG, "Get call number fails " + e.toString()); Log.e(TAG, "Get call number fails " + e.toString());
} finally { } finally {
cursor.close(); cursor.close(); // 关闭游标
} }
} }
return ""; return ""; // 未找到返回空字符串
} }
/**
* ID
* @param resolver
* @param phoneNumber
* @param callDate
* @return ID0
*/
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表
new String [] { CallNote.NOTE_ID }, new String [] { CallNote.NOTE_ID }, // 查询笔记ID列
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" // 查询条件:日期匹配、类型为通话记录且电话号码相等
+ CallNote.PHONE_NUMBER + ",?)", + CallNote.PHONE_NUMBER + ",?)",
new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, // 查询参数
null); null); // 排序方式
if (cursor != null) { if (cursor != null) { // 检查游标是否有效
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) { // 移动到第一条记录
try { try {
return cursor.getLong(0); return cursor.getLong(0); // 返回笔记ID
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) { // 处理索引越界异常
Log.e(TAG, "Get call note id fails " + e.toString()); Log.e(TAG, "Get call note id fails " + e.toString());
} }
} }
cursor.close(); cursor.close(); // 关闭游标
} }
return 0; return 0; // 未找到返回0
} }
/**
* ID
* @param resolver
* @param noteId ID
* @return
* @throws IllegalArgumentException
*/
public static String getSnippetById(ContentResolver resolver, long noteId) { public static String getSnippetById(ContentResolver resolver, long noteId) {
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记表
new String [] { NoteColumns.SNIPPET }, new String [] { NoteColumns.SNIPPET }, // 查询摘要列
NoteColumns.ID + "=?", NoteColumns.ID + "=?", // 查询条件笔记ID匹配
new String [] { String.valueOf(noteId)}, new String [] { String.valueOf(noteId)}, // 查询参数
null); null); // 排序方式
if (cursor != null) { if (cursor != null) { // 检查游标是否有效
String snippet = ""; String snippet = ""; // 初始化摘要为空字符串
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) { // 移动到第一条记录
snippet = cursor.getString(0); snippet = cursor.getString(0); // 获取摘要内容
} }
cursor.close(); cursor.close(); // 关闭游标
return snippet; return snippet; // 返回摘要
} }
throw new IllegalArgumentException("Note is not found with id: " + noteId); throw new IllegalArgumentException("Note is not found with id: " + noteId); // 未找到笔记,抛出异常
} }
/**
*
* @param snippet
* @return
*/
public static String getFormattedSnippet(String snippet) { public static String getFormattedSnippet(String snippet) {
if (snippet != null) { if (snippet != null) { // 检查摘要是否为空
snippet = snippet.trim(); snippet = snippet.trim(); // 去除前后空格
int index = snippet.indexOf('\n'); int index = snippet.indexOf('\n'); // 查找换行符位置
if (index != -1) { if (index != -1) { // 如果存在换行符
snippet = snippet.substring(0, index); snippet = snippet.substring(0, index); // 截取第一行
} }
} }
return snippet; return snippet; // 返回格式化后的摘要
} }
} }

@ -2,112 +2,118 @@
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * License 使
* You may obtain a copy of the License at * License
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * License
* distributed under the License is distributed on an "AS IS" BASIS, * License
* 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; package net.micode.notes.tool; // 指定代码所属的包名
/**
* Google Tasks
* Google Tasks使JSON
*/
public class GTaskStringUtils { public class GTaskStringUtils {
// --------------------------- JSON 字段常量 --------------------------- //
// 操作ID字段名
public final static String GTASK_JSON_ACTION_ID = "action_id"; public final static String GTASK_JSON_ACTION_ID = "action_id";
// 操作列表字段名
public final static String GTASK_JSON_ACTION_LIST = "action_list"; public final static String GTASK_JSON_ACTION_LIST = "action_list";
// 操作类型字段名
public final static String GTASK_JSON_ACTION_TYPE = "action_type"; public final static String GTASK_JSON_ACTION_TYPE = "action_type";
// 创建操作类型值
public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create";
// 获取全部数据操作类型值
public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all";
// 移动操作类型值
public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move";
// 更新操作类型值
public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update";
// 创建者ID字段名
public final static String GTASK_JSON_CREATOR_ID = "creator_id"; public final static String GTASK_JSON_CREATOR_ID = "creator_id";
// 子实体字段名
public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; public final static String GTASK_JSON_CHILD_ENTITY = "child_entity";
// 客户端版本字段名
public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; public final static String GTASK_JSON_CLIENT_VERSION = "client_version";
// 完成状态字段名
public final static String GTASK_JSON_COMPLETED = "completed"; public final static String GTASK_JSON_COMPLETED = "completed";
// 当前列表ID字段名
public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";
// 默认列表ID字段名
public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";
// 已删除状态字段名
public final static String GTASK_JSON_DELETED = "deleted"; public final static String GTASK_JSON_DELETED = "deleted";
// 目标列表字段名(移动操作)
public final static String GTASK_JSON_DEST_LIST = "dest_list"; public final static String GTASK_JSON_DEST_LIST = "dest_list";
// 目标父级字段名(移动操作)
public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; public final static String GTASK_JSON_DEST_PARENT = "dest_parent";
// 目标父级类型字段名
public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";
// 实体变更字段名
public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta";
// 实体类型字段名
public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; public final static String GTASK_JSON_ENTITY_TYPE = "entity_type";
// 是否获取已删除数据字段名
public final static String GTASK_JSON_GET_DELETED = "get_deleted"; public final static String GTASK_JSON_GET_DELETED = "get_deleted";
// ID字段名
public final static String GTASK_JSON_ID = "id"; public final static String GTASK_JSON_ID = "id";
// 索引字段名
public final static String GTASK_JSON_INDEX = "index"; public final static String GTASK_JSON_INDEX = "index";
// 最后修改时间字段名
public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; public final static String GTASK_JSON_LAST_MODIFIED = "last_modified";
// 最新同步点字段名
public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";
// 列表ID字段名
public final static String GTASK_JSON_LIST_ID = "list_id"; public final static String GTASK_JSON_LIST_ID = "list_id";
// 列表集合字段名
public final static String GTASK_JSON_LISTS = "lists"; public final static String GTASK_JSON_LISTS = "lists";
// 名称字段名
public final static String GTASK_JSON_NAME = "name"; public final static String GTASK_JSON_NAME = "name";
// 新ID字段名创建操作返回
public final static String GTASK_JSON_NEW_ID = "new_id"; public final static String GTASK_JSON_NEW_ID = "new_id";
// 备注字段名
public final static String GTASK_JSON_NOTES = "notes"; public final static String GTASK_JSON_NOTES = "notes";
// 父级ID字段名
public final static String GTASK_JSON_PARENT_ID = "parent_id"; public final static String GTASK_JSON_PARENT_ID = "parent_id";
// 前一个兄弟ID字段名排序用
public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";
// 结果集合字段名
public final static String GTASK_JSON_RESULTS = "results"; public final static String GTASK_JSON_RESULTS = "results";
// 源列表字段名(移动操作)
public final static String GTASK_JSON_SOURCE_LIST = "source_list"; public final static String GTASK_JSON_SOURCE_LIST = "source_list";
// 任务集合字段名
public final static String GTASK_JSON_TASKS = "tasks"; public final static String GTASK_JSON_TASKS = "tasks";
// 类型字段名
public final static String GTASK_JSON_TYPE = "type"; public final static String GTASK_JSON_TYPE = "type";
// 分组类型值
public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; public final static String GTASK_JSON_TYPE_GROUP = "GROUP";
// 任务类型值
public final static String GTASK_JSON_TYPE_TASK = "TASK"; public final static String GTASK_JSON_TYPE_TASK = "TASK";
// 用户字段名
public final static String GTASK_JSON_USER = "user"; public final static String GTASK_JSON_USER = "user";
// --------------------------- 文件夹相关常量 --------------------------- //
// MIUI笔记文件夹前缀用于标识本地创建的文件夹
public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]";
// 默认文件夹名称
public final static String FOLDER_DEFAULT = "Default"; public final static String FOLDER_DEFAULT = "Default";
// 通话记录文件夹名称
public final static String FOLDER_CALL_NOTE = "Call_Note"; public final static String FOLDER_CALL_NOTE = "Call_Note";
// 元数据文件夹名称(存储同步元数据)
public final static String FOLDER_META = "METADATA"; public final static String FOLDER_META = "METADATA";
// --------------------------- 元数据相关常量 --------------------------- //
// 元数据头部Google Tasks ID标识
public final static String META_HEAD_GTASK_ID = "meta_gid"; public final static String META_HEAD_GTASK_ID = "meta_gid";
// 元数据头部:笔记内容标识
public final static String META_HEAD_NOTE = "meta_note"; public final static String META_HEAD_NOTE = "meta_note";
// 元数据头部:数据项标识
public final static String META_HEAD_DATA = "meta_data"; public final static String META_HEAD_DATA = "meta_data";
// 元数据笔记名称(系统自动生成,禁止用户修改/删除)
public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE";
}
}

@ -2,180 +2,270 @@
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * License 使
* You may obtain a copy of the License at * License
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * License
* distributed under the License is distributed on an "AS IS" BASIS, * License
* 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; package net.micode.notes.tool; // 指定代码所属的包名
import android.content.Context; import android.content.Context; // 导入Context类提供应用环境信息
import android.preference.PreferenceManager; import android.preference.PreferenceManager; // 导入偏好设置管理类
import net.micode.notes.R; import net.micode.notes.R; // 导入R资源类访问应用资源
import net.micode.notes.ui.NotesPreferenceActivity; import net.micode.notes.ui.NotesPreferenceActivity; // 导入笔记偏好设置活动类
/**
*
* ID
*/
public class ResourceParser { public class ResourceParser {
public static final int YELLOW = 0; // 笔记背景颜色常量定义
public static final int BLUE = 1; public static final int YELLOW = 0; // 黄色背景
public static final int WHITE = 2; public static final int BLUE = 1; // 蓝色背景
public static final int GREEN = 3; public static final int WHITE = 2; // 白色背景
public static final int RED = 4; public static final int GREEN = 3; // 绿色背景
public static final int RED = 4; // 红色背景
// 默认背景颜色(黄色)
public static final int BG_DEFAULT_COLOR = YELLOW; public static final int BG_DEFAULT_COLOR = YELLOW;
public static final int TEXT_SMALL = 0; // 文本大小常量定义
public static final int TEXT_MEDIUM = 1; public static final int TEXT_SMALL = 0; // 小号文本
public static final int TEXT_LARGE = 2; public static final int TEXT_MEDIUM = 1; // 中号文本(默认)
public static final int TEXT_SUPER = 3; public static final int TEXT_LARGE = 2; // 大号文本
public static final int TEXT_SUPER = 3; // 超大号文本
// 默认文本大小(中号)
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
/**
*
* ID
*/
public static class NoteBgResources { public static class NoteBgResources {
// 笔记编辑界面背景资源数组
private final static int [] BG_EDIT_RESOURCES = new int [] { private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow, R.drawable.edit_yellow, // 黄色背景
R.drawable.edit_blue, R.drawable.edit_blue, // 蓝色背景
R.drawable.edit_white, R.drawable.edit_white, // 白色背景
R.drawable.edit_green, R.drawable.edit_green, // 绿色背景
R.drawable.edit_red R.drawable.edit_red // 红色背景
}; };
// 笔记编辑界面标题栏背景资源数组
private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] {
R.drawable.edit_title_yellow, R.drawable.edit_title_yellow, // 黄色标题栏
R.drawable.edit_title_blue, R.drawable.edit_title_blue, // 蓝色标题栏
R.drawable.edit_title_white, R.drawable.edit_title_white, // 白色标题栏
R.drawable.edit_title_green, R.drawable.edit_title_green, // 绿色标题栏
R.drawable.edit_title_red R.drawable.edit_title_red // 红色标题栏
}; };
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteBgResource(int id) { public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id]; return BG_EDIT_RESOURCES[id]; // 返回对应颜色的背景资源ID
} }
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteTitleBgResource(int id) { public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id]; return BG_EDIT_TITLE_RESOURCES[id]; // 返回对应颜色的标题栏背景资源ID
} }
} }
/**
* ID
*
* @param context
* @return ID
*/
public static int getDefaultBgId(Context context) { public static int getDefaultBgId(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
// 如果用户开启了随机背景选项则返回随机背景ID
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
} else { } else {
// 否则返回默认背景ID黄色
return BG_DEFAULT_COLOR; return BG_DEFAULT_COLOR;
} }
} }
/**
*
* ID
*/
public static class NoteItemBgResources { public static class NoteItemBgResources {
// 列表中第一项的背景资源数组
private final static int [] BG_FIRST_RESOURCES = new int [] { private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up, R.drawable.list_yellow_up, // 黄色首项背景
R.drawable.list_blue_up, R.drawable.list_blue_up, // 蓝色首项背景
R.drawable.list_white_up, R.drawable.list_white_up, // 白色首项背景
R.drawable.list_green_up, R.drawable.list_green_up, // 绿色首项背景
R.drawable.list_red_up R.drawable.list_red_up // 红色首项背景
}; };
// 列表中中间项的背景资源数组
private final static int [] BG_NORMAL_RESOURCES = new int [] { private final static int [] BG_NORMAL_RESOURCES = new int [] {
R.drawable.list_yellow_middle, R.drawable.list_yellow_middle, // 黄色中间项背景
R.drawable.list_blue_middle, R.drawable.list_blue_middle, // 蓝色中间项背景
R.drawable.list_white_middle, R.drawable.list_white_middle, // 白色中间项背景
R.drawable.list_green_middle, R.drawable.list_green_middle, // 绿色中间项背景
R.drawable.list_red_middle R.drawable.list_red_middle // 红色中间项背景
}; };
// 列表中最后一项的背景资源数组
private final static int [] BG_LAST_RESOURCES = new int [] { private final static int [] BG_LAST_RESOURCES = new int [] {
R.drawable.list_yellow_down, R.drawable.list_yellow_down, // 黄色末项背景
R.drawable.list_blue_down, R.drawable.list_blue_down, // 蓝色末项背景
R.drawable.list_white_down, R.drawable.list_white_down, // 白色末项背景
R.drawable.list_green_down, R.drawable.list_green_down, // 绿色末项背景
R.drawable.list_red_down, R.drawable.list_red_down, // 红色末项背景
}; };
// 列表中单独一项(唯一项)的背景资源数组
private final static int [] BG_SINGLE_RESOURCES = new int [] { private final static int [] BG_SINGLE_RESOURCES = new int [] {
R.drawable.list_yellow_single, R.drawable.list_yellow_single, // 黄色单项背景
R.drawable.list_blue_single, R.drawable.list_blue_single, // 蓝色单项背景
R.drawable.list_white_single, R.drawable.list_white_single, // 白色单项背景
R.drawable.list_green_single, R.drawable.list_green_single, // 绿色单项背景
R.drawable.list_red_single R.drawable.list_red_single // 红色单项背景
}; };
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteBgFirstRes(int id) { public static int getNoteBgFirstRes(int id) {
return BG_FIRST_RESOURCES[id]; return BG_FIRST_RESOURCES[id]; // 返回对应颜色的首项背景资源ID
} }
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteBgLastRes(int id) { public static int getNoteBgLastRes(int id) {
return BG_LAST_RESOURCES[id]; return BG_LAST_RESOURCES[id]; // 返回对应颜色的末项背景资源ID
} }
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteBgSingleRes(int id) { public static int getNoteBgSingleRes(int id) {
return BG_SINGLE_RESOURCES[id]; return BG_SINGLE_RESOURCES[id]; // 返回对应颜色的单项背景资源ID
} }
/**
* IDID
* @param id ID0-4
* @return ID
*/
public static int getNoteBgNormalRes(int id) { public static int getNoteBgNormalRes(int id) {
return BG_NORMAL_RESOURCES[id]; return BG_NORMAL_RESOURCES[id]; // 返回对应颜色的中间项背景资源ID
} }
/**
* ID
* @return ID
*/
public static int getFolderBgRes() { public static int getFolderBgRes() {
return R.drawable.list_folder; return R.drawable.list_folder; // 返回文件夹背景资源ID
} }
} }
/**
*
* 2x4xID
*/
public static class WidgetBgResources { public static class WidgetBgResources {
// 2x尺寸桌面小部件背景资源数组
private final static int [] BG_2X_RESOURCES = new int [] { private final static int [] BG_2X_RESOURCES = new int [] {
R.drawable.widget_2x_yellow, R.drawable.widget_2x_yellow, // 黄色2x小部件背景
R.drawable.widget_2x_blue, R.drawable.widget_2x_blue, // 蓝色2x小部件背景
R.drawable.widget_2x_white, R.drawable.widget_2x_white, // 白色2x小部件背景
R.drawable.widget_2x_green, R.drawable.widget_2x_green, // 绿色2x小部件背景
R.drawable.widget_2x_red, R.drawable.widget_2x_red, // 红色2x小部件背景
}; };
/**
* ID2xID
* @param id ID0-4
* @return 2xID
*/
public static int getWidget2xBgResource(int id) { public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id]; return BG_2X_RESOURCES[id]; // 返回对应颜色的2x小部件背景资源ID
} }
// 4x尺寸桌面小部件背景资源数组
private final static int [] BG_4X_RESOURCES = new int [] { private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow, R.drawable.widget_4x_yellow, // 黄色4x小部件背景
R.drawable.widget_4x_blue, R.drawable.widget_4x_blue, // 蓝色4x小部件背景
R.drawable.widget_4x_white, R.drawable.widget_4x_white, // 白色4x小部件背景
R.drawable.widget_4x_green, R.drawable.widget_4x_green, // 绿色4x小部件背景
R.drawable.widget_4x_red R.drawable.widget_4x_red // 红色4x小部件背景
}; };
/**
* ID4xID
* @param id ID0-4
* @return 4xID
*/
public static int getWidget4xBgResource(int id) { public static int getWidget4xBgResource(int id) {
return BG_4X_RESOURCES[id]; return BG_4X_RESOURCES[id]; // 返回对应颜色的4x小部件背景资源ID
} }
} }
/**
*
* ID
*/
public static class TextAppearanceResources { public static class TextAppearanceResources {
// 文本样式资源数组
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal, R.style.TextAppearanceNormal, // 小号文本样式
R.style.TextAppearanceMedium, R.style.TextAppearanceMedium, // 中号文本样式
R.style.TextAppearanceLarge, R.style.TextAppearanceLarge, // 大号文本样式
R.style.TextAppearanceSuper R.style.TextAppearanceSuper // 超大号文本样式
}; };
/**
* IDID
* @param id ID0-3
* @return ID
*/
public static int getTexAppearanceResource(int id) { public static int getTexAppearanceResource(int id) {
/** /**
* HACKME: Fix bug of store the resource id in shared preference. * HACKME: ID
* The id may larger than the length of resources, in this case, * ID
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
*/ */
if (id >= TEXTAPPEARANCE_RESOURCES.length) { if (id >= TEXTAPPEARANCE_RESOURCES.length) {
return BG_DEFAULT_FONT_SIZE; return BG_DEFAULT_FONT_SIZE; // ID越界时返回默认文本大小
} }
return TEXTAPPEARANCE_RESOURCES[id]; return TEXTAPPEARANCE_RESOURCES[id]; // 返回对应文本大小的样式资源ID
} }
/**
*
* @return
*/
public static int getResourcesSize() { public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length; return TEXTAPPEARANCE_RESOURCES.length; // 返回资源数组长度
} }
} }
} }
Loading…
Cancel
Save