Compare commits

...

2 Commits

Author SHA1 Message Date
ps9up4ig6 172c7ff27f 张心驰的tools注释
2 years ago
zxc 2d6409fd3b 4.20
2 years ago

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/gitProject1.iml" filepath="$PROJECT_DIR$/.idea/gitProject1.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -40,7 +40,7 @@ public class BackupUtils {
private static final String TAG = "BackupUtils";
// Singleton stuff
private static BackupUtils sInstance;
//如果当前备份不存在,则新声明一个
public static synchronized BackupUtils getInstance(Context context) {
if (sInstance == null) {
sInstance = new BackupUtils(context);
@ -51,16 +51,17 @@ 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;
@ -72,34 +73,34 @@ public class BackupUtils {
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 class TextExport {//内部类文本输出定义笔记ID、修改日期等笔记、内容、日期、电话号码的数据和格式等常量
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 = {
private static final String[] DATA_PROJECTION = {//字符串储存便签的基本信息
DataColumns.CONTENT,
DataColumns.MIME_TYPE,
DataColumns.DATA1,
@ -107,7 +108,7 @@ public class BackupUtils {
DataColumns.DATA3,
DataColumns.DATA4,
};
//标识设定数据内容标识为0媒体类型标识为1访问日期标识为2电话号码标识为4
private static final int DATA_COLUMN_CONTENT = 0;
private static final int DATA_COLUMN_MIME_TYPE = 1;
@ -115,17 +116,17 @@ public class BackupUtils {
private static final int DATA_COLUMN_CALL_DATE = 2;
private static final int DATA_COLUMN_PHONE_NUMBER = 4;
//文档格式标识名称标识为0;日期标识为1;内容标识为2
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) {
public TextExport(Context context) {//从context获取便签的信息并给其中一些成员赋予初值
TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note);
mContext = context;
mFileName = "";
@ -134,12 +135,13 @@ public class BackupUtils {
private String getFormat(int id) {
return TEXT_FORMAT[id];
}
}//通过ID返回文件的格式信息
/**
* Export the folder identified by folder id to text
*/
private void exportFolderToText(String folderId, PrintStream ps) {
private void exportFolderToText(String folderId, PrintStream ps) {//
// 通过文件ID找到该文件下的便签并且打印出该便签最后修改的日期并且将该便签的内容输出并显示出来。
// Query notes belong to this folder
Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] {
@ -166,6 +168,7 @@ public class BackupUtils {
* Export note identified by id to a print stream
*/
private void exportNoteToText(String noteId, PrintStream ps) {
//将便签的内容以文本的形式显示在屏幕上。比如电话号码、打电话的日期等。
Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI,
DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] {
noteId
@ -206,7 +209,7 @@ public class BackupUtils {
dataCursor.close();
}
// print a line separator between note
try {
try {//在note下面输出一条线
ps.write(new byte[] {
Character.LINE_SEPARATOR, Character.LETTER_NUMBER
});
@ -218,26 +221,26 @@ public class BackupUtils {
/**
* Note will be exported as text which is user readable
*/
public int exportToText() {
if (!externalStorageAvailable()) {
public int exportToText() {//将note里面的内容输出到用户的屏幕
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");
Log.e(TAG, "get print stream 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,
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 != null) {//导出文件夹,把里面的便签导出来
if (folderCursor.moveToFirst()) {
do {
// Print folder's name
@ -258,13 +261,13 @@ public class BackupUtils {
}
// Export notes in root's folder
Cursor noteCursor = mContext.getContentResolver().query(
Cursor noteCursor = mContext.getContentResolver().query(//输出根目录下的便签
Notes.CONTENT_NOTE_URI,
NOTE_PROJECTION,
NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID
+ "=0", null, null);
if (noteCursor != null) {
if (noteCursor != null) {//输出修改的时间
if (noteCursor.moveToFirst()) {
do {
ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format(
@ -285,17 +288,17 @@ public class BackupUtils {
/**
* Get a print stream pointed to the file {@generateExportedTextFile}
*/
private PrintStream getExportToTextPrintStream() {
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);
}//初始化储存在SD卡中的文件如果为空创造失败
mFileName = file.getName();//得到文件名
mFileDirectory = mContext.getString(R.string.file_path);//得到文件路径
PrintStream ps = null;
try {
try {//将ps输出流输出到特定的文件目的就是导出到文件而不是直接输出
FileOutputStream fos = new FileOutputStream(file);
ps = new PrintStream(fos);
} catch (FileNotFoundException e) {
@ -313,6 +316,7 @@ public class BackupUtils {
* Generate the text file to store imported data
*/
private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) {
//生成存储文件安装在SD卡上
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory());
sb.append(context.getString(filePathResId));
@ -323,7 +327,7 @@ public class BackupUtils {
System.currentTimeMillis())));
File file = new File(sb.toString());
try {
try {//如果这些文件不存在,则新建
if (!filedir.exists()) {
filedir.mkdir();
}

@ -35,10 +35,10 @@ import java.util.ArrayList;
import java.util.HashSet;
public class DataUtils {
public class DataUtils {//定义便签的处理工具类
public static final String TAG = "DataUtils";
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {
if (ids == null) {
public static boolean batchDeleteNotes(ContentResolver resolver, HashSet<Long> ids) {//一个处理批量删除便签的方法
if (ids == null) {//判断笔记是否为空
Log.d(TAG, "the ids is null");
return true;
}
@ -48,23 +48,23 @@ public class DataUtils {
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) {
for (long id : ids) {//遍历便签id
if(id == Notes.ID_ROOT_FOLDER) {
Log.e(TAG, "Don't delete system folder root");
Log.e(TAG, "Don't delete system folder root");//.如果是根目录的文件夹输出Don't delete system folder root
continue;
}
ContentProviderOperation.Builder builder = ContentProviderOperation
ContentProviderOperation.Builder builder = ContentProviderOperation//批量的删除对应的ID
.newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
operationList.add(builder.build());
}
try {
try {//返回被删除的数据如果返回为空则删除失败返回false打印异常信息删除成功返回true
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
return false;
}
return true;
} catch (RemoteException e) {
} catch (RemoteException e) {//错误处理
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
} catch (OperationApplicationException e) {
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
@ -73,6 +73,7 @@ public class DataUtils {
}
public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) {
//将某便签移动到另一个目录下
ContentValues values = new ContentValues();
values.put(NoteColumns.PARENT_ID, desFolderId);
values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId);
@ -80,7 +81,7 @@ public class DataUtils {
resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null);
}
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,
public static boolean batchMoveToFolder(ContentResolver resolver, HashSet<Long> ids,//批量移动
long folderId) {
if (ids == null) {
Log.d(TAG, "the ids is null");
@ -88,7 +89,7 @@ public class DataUtils {
}
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
for (long id : ids) {
for (long id : ids) {//遍历便签ID
ContentProviderOperation.Builder builder = ContentProviderOperation
.newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id));
builder.withValue(NoteColumns.PARENT_ID, folderId);
@ -96,7 +97,7 @@ public class DataUtils {
operationList.add(builder.build());
}
try {
try {//错误检测
ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList);
if (results == null || results.length == 0 || results[0] == null) {
Log.d(TAG, "delete notes failed, ids:" + ids.toString());
@ -114,7 +115,7 @@ public class DataUtils {
/**
* Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}}
*/
public static int getUserFolderCount(ContentResolver resolver) {
public static int getUserFolderCount(ContentResolver resolver) {//得到文件夹数目
Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI,
new String[] { "COUNT(*)" },
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?",
@ -136,14 +137,14 @@ public class DataUtils {
return count;
}
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),
null,
NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER,
new String [] {String.valueOf(type)},
null);
boolean exist = false;
boolean exist = false;//判断note是否在数据库中并返回相应的布尔值
if (cursor != null) {
if (cursor.getCount() > 0) {
exist = true;
@ -153,7 +154,7 @@ public class DataUtils {
return exist;
}
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {
public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) {//通过便签ID搜索判断该note是否在数据库中存在
Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId),
null, null, null, null);
@ -167,7 +168,7 @@ public class DataUtils {
return exist;
}
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),
null, null, null, null);
@ -181,7 +182,7 @@ public class DataUtils {
return exist;
}
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,
NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER +
" AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER +
@ -197,7 +198,7 @@ public class DataUtils {
return exist;
}
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,
new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE },
NoteColumns.PARENT_ID + "=?",
@ -209,12 +210,12 @@ public class DataUtils {
if (c.moveToFirst()) {
set = new HashSet<AppWidgetAttribute>();
do {
try {
try {//把每一个条目对应的窗口id和type记录下来放到set里面。
AppWidgetAttribute widget = new AppWidgetAttribute();
widget.widgetId = c.getInt(0);
widget.widgetType = c.getInt(1);
set.add(widget);
} catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {//当下标超过边界,那么返回错误
Log.e(TAG, e.toString());
}
} while (c.moveToNext());
@ -224,14 +225,14 @@ public class DataUtils {
return set;
}
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {
public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) {//通过笔记ID获取号码
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.PHONE_NUMBER },
CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?",
new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE },
null);
if (cursor != null && cursor.moveToFirst()) {
if (cursor != null && cursor.moveToFirst()) {//获取电话号码,并处理异常
try {
return cursor.getString(0);
} catch (IndexOutOfBoundsException e) {
@ -243,7 +244,7 @@ public class DataUtils {
return "";
}
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {
public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) {//通过号码和日期获取ID
Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI,
new String [] { CallNote.NOTE_ID },
CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL("
@ -264,7 +265,7 @@ public class DataUtils {
return 0;
}
public static String getSnippetById(ContentResolver resolver, long noteId) {
public static String getSnippetById(ContentResolver resolver, long noteId) {//通过Note的Id获取Note的片段
Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI,
new String [] { NoteColumns.SNIPPET },
NoteColumns.ID + "=?",
@ -282,7 +283,7 @@ public class DataUtils {
throw new IllegalArgumentException("Note is not found with id: " + noteId);
}
public static String getFormattedSnippet(String snippet) {
public static String getFormattedSnippet(String snippet) {//格式化,对字符串进行格式处理,将字符串两头的空格去掉,同时将换行符去掉
if (snippet != null) {
snippet = snippet.trim();
int index = snippet.indexOf('\n');

@ -17,7 +17,8 @@
package net.micode.notes.tool;
public class GTaskStringUtils {
//定义了很多的静态字符串目的就是为了提供jsonObject中相应字符串的"key"
//把这些静态的定义单独写到了一个类里面,是非常好的编程规范
public final static String GTASK_JSON_ACTION_ID = "action_id";
public final static String GTASK_JSON_ACTION_LIST = "action_list";

@ -23,7 +23,7 @@ import net.micode.notes.R;
import net.micode.notes.ui.NotesPreferenceActivity;
public class ResourceParser {
//ResourceParser类资源分析实际上就是获取资源并且在程序中使用比如图片资源、布局资源、字体大小等
public static final int YELLOW = 0;
public static final int BLUE = 1;
public static final int WHITE = 2;
@ -39,7 +39,7 @@ public class ResourceParser {
public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM;
public static class NoteBgResources {
public static class NoteBgResources {//背景资源的获取
private final static int [] BG_EDIT_RESOURCES = new int [] {
R.drawable.edit_yellow,
R.drawable.edit_blue,
@ -59,13 +59,13 @@ public class ResourceParser {
public static int getNoteBgResource(int id) {
return BG_EDIT_RESOURCES[id];
}
//获取Note的背景颜色
public static int getNoteTitleBgResource(int id) {
return BG_EDIT_TITLE_RESOURCES[id];
}
}
public static int getDefaultBgId(Context context) {
//ID可以查找到编辑框的颜色的地址
public static int getDefaultBgId(Context context) {//直接获取默认的背景颜色
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) {
return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length);
@ -74,7 +74,7 @@ public class ResourceParser {
}
}
public static class NoteItemBgResources {
public static class NoteItemBgResources {//便签背景资源类
private final static int [] BG_FIRST_RESOURCES = new int [] {
R.drawable.list_yellow_up,
R.drawable.list_blue_up,
@ -128,8 +128,8 @@ public class ResourceParser {
}
}
public static class WidgetBgResources {
private final static int [] BG_2X_RESOURCES = new int [] {
public static class WidgetBgResources {//获取图片资源类
private final static int [] BG_2X_RESOURCES = new int [] {//通过id获得2x的小窗口背景资源
R.drawable.widget_2x_yellow,
R.drawable.widget_2x_blue,
R.drawable.widget_2x_white,
@ -140,7 +140,7 @@ public class ResourceParser {
public static int getWidget2xBgResource(int id) {
return BG_2X_RESOURCES[id];
}
//通过ID获取较小的图片
private final static int [] BG_4X_RESOURCES = new int [] {
R.drawable.widget_4x_yellow,
R.drawable.widget_4x_blue,
@ -153,7 +153,7 @@ public class ResourceParser {
return BG_4X_RESOURCES[id];
}
}
//根据ID加载BG_4X_RESOURCES数组里的颜色资源序号。
public static class TextAppearanceResources {
private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] {
R.style.TextAppearanceNormal,
@ -161,7 +161,7 @@ public class ResourceParser {
R.style.TextAppearanceLarge,
R.style.TextAppearanceSuper
};
//文本外观资源,包括默认字体,以及获取资源大小
public static int getTexAppearanceResource(int id) {
/**
* HACKME: Fix bug of store the resource id in shared preference.
@ -173,9 +173,10 @@ public class ResourceParser {
}
return TEXTAPPEARANCE_RESOURCES[id];
}
//防止输入的id大于资源总量若如此则自动返回默认的设置结果
public static int getResourcesSize() {
return TEXTAPPEARANCE_RESOURCES.length;
}
}
}
//直接返回为资源的长度
Loading…
Cancel
Save