Compare commits

...

2 Commits

Author SHA1 Message Date
lyy d16d33bf60 精读标注
1 year ago
lyy 9801f62dc6 精读标注
1 year ago

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

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

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

@ -1,54 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="5076d7d4-f4ad-435d-8771-5f5d4f68143b" name="更改" comment="">
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/Contact.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/Contact.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/Notes.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/Notes.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/NotesProvider.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/data/NotesProvider.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/model/Note.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/model/Note.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/model/WorkingNote.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/model/WorkingNote.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/DataUtils.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/DataUtils.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_2x.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_2x.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_4x.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/app/src/main/java/net/micode/notes/widget/NoteWidgetProvider_4x.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/build.gradle.kts" beforeDir="false" afterPath="$PROJECT_DIR$/src/build.gradle.kts" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2eJTxTem2bHgdzy7XgqryqQtErP" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="默认任务">
<changelist id="5076d7d4-f4ad-435d-8771-5f5d4f68143b" name="更改" comment="" />
<created>1711621930849</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1711621930849</updated>
</task>
<servers />
</component>
</project>

@ -15,15 +15,15 @@
*/
package net.micode.notes.model;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;
import android.content.ContentProviderOperation;//批量的更新、插入、删除数据。
import android.content.ContentProviderResult;//操作的结果
import android.content.ContentUris;//用于添加和获取Uri后面的ID
import android.content.ContentValues;//一种用来存储基本数据类型数据的存储机制
import android.content.Context;//需要用该类来弄清楚调用者的实例
import android.content.OperationApplicationException;//操作应用程序容错
import android.net.Uri;//表示待操作的数据
import android.os.RemoteException;//远程容错
import android.util.Log;//输出日志,比如说出错、警告等
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.CallNote;
@ -36,21 +36,24 @@ import java.util.ArrayList;
public class Note {//单个便签项
private ContentValues mNoteDiffValues;
private NoteData mNoteData;
private static final String TAG = "Note";
//ContentValues 是Android SQLite库提供的一个类用于存储键值对。在SQLite中通常使用ContentValues来构建要插入到数据库中的行或用于更新现有行的数据。它基本上是一个HashMap其中键是字符串代表列名值是对应要插入或更新的数据。
private NoteData mNoteData;//见下方自定义的NoteData类用于记录便签内容
private static final String TAG = "Note";//定义一个初始化后不可改变的静态变量作为数据库的标记
/**
* Create a new note id for adding a new note to databases
*/
public static synchronized long getNewNoteId(Context context, long folderId) {
// Create a new note in the database
ContentValues values = new ContentValues();
long createdTime = System.currentTimeMillis();
values.put(NoteColumns.CREATED_DATE, createdTime);
values.put(NoteColumns.MODIFIED_DATE, createdTime);
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
values.put(NoteColumns.LOCAL_MODIFIED, 1);
values.put(NoteColumns.PARENT_ID, folderId);
long createdTime = System.currentTimeMillis();//获取时间
values.put(NoteColumns.CREATED_DATE, createdTime);//设置创建日期
values.put(NoteColumns.MODIFIED_DATE, createdTime);//设置修改日期
values.put(NoteColumns.TYPE, Notes.TYPE_NOTE);//类型
values.put(NoteColumns.LOCAL_MODIFIED, 1);//通常用于存储与该记录相关的最后修改时间戳。这个时间戳通常是用来跟踪记录的版本或最后一次修改的时间,这样开发者就可以知道哪些记录是最新的,或者在数据同步时决定哪些记录需要更新。
values.put(NoteColumns.PARENT_ID, folderId);//设置ID
Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values);
//Uri统一资源标识符在Android中用于标识内容提供者中的数据
//将内容提供者对象中的数据插入到由uri指定的位置。
long noteId = 0;
try {
@ -58,39 +61,49 @@ public class Note {//单个便签项
} catch (NumberFormatException e) {
Log.e(TAG, "Get note id error :" + e.toString());
noteId = 0;
}
}//验证noteID是否正确
/**
* try-catch
* uri.getPathSegments()uriURI(/)
* get(1)0
* Long.valueOf(...)Long
* LongnoteId
* catch (NumberFormatException e) tryNumberFormatExceptioncatch
* Log.e(TAG, "Get note id error :" + e.toString())使AndroidTAGe.toString()
*noteId0URIID
*/
if (noteId == -1) {
throw new IllegalStateException("Wrong note id:" + noteId);
}
}//如果验证失败抛出异常
return noteId;
}
public Note() {
mNoteDiffValues = new ContentValues();
mNoteData = new NoteData();
}
}//构造函数
public void setNoteValue(String key, String value) {
mNoteDiffValues.put(key, value);
mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1);
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
}//设置数据库表格的标签属性数据
public void setTextData(String key, String value) {
mNoteData.setTextData(key, value);
}
}//设置数据库表格的标签文本内容的数据
public void setTextDataId(long id) {
mNoteData.setTextDataId(id);
}
}//设置文本数据的ID
public long getTextDataId() {
return mNoteData.mTextDataId;
}
}//设置电话号码数据的ID
public void setCallDataId(long id) {
mNoteData.setCallDataId(id);
}
}//得到电话号码数据的ID
public void setCallData(String key, String value) {
mNoteData.setCallData(key, value);
@ -98,46 +111,46 @@ public class Note {//单个便签项
public boolean isLocalModified() {
return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified();
}
}//判断是否是本地修改
public boolean syncNote(Context context, long noteId) {
public boolean syncNote(Context context, long noteId) {//同步
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
if (!isLocalModified()) {
return true;
}
}//没修改不需要同步
/**
* In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the
* note data info
* {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update thenote data info
* LOCAL_MODIFIEDMODIFIED_DATE使
*/
if (context.getContentResolver().update(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null,
null) == 0) {
Log.e(TAG, "Update note error, should not happen");
// Do not return, fall through
}
mNoteDiffValues.clear();
}//如果笔记更新失败记录日志
mNoteDiffValues.clear();//清除数据库标签属性中的所有内容
if (mNoteData.isLocalModified()
&& (mNoteData.pushIntoContentResolver(context, noteId) == null)) {
return false;
}
}//如果笔记内容在本地被修改把笔记内容推送到内容解析器如果失败则返回false
return true;
}
private class NoteData {
private class NoteData {//定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据
private long mTextDataId;
private ContentValues mTextDataValues;
private ContentValues mTextDataValues;//文本数据
private long mCallDataId;
private ContentValues mCallDataValues;
private ContentValues mCallDataValues;////电话号码数据
private static final String TAG = "NoteData";
@ -146,11 +159,11 @@ public class Note {//单个便签项
mCallDataValues = new ContentValues();
mTextDataId = 0;
mCallDataId = 0;
}
}//构造函数
boolean isLocalModified() {
return mTextDataValues.size() > 0 || mCallDataValues.size() > 0;
}
}//判断是否是本地修改
void setTextDataId(long id) {
if(id <= 0) {
@ -178,23 +191,26 @@ public class Note {//单个便签项
mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis());
}
Uri pushIntoContentResolver(Context context, long noteId) {
Uri pushIntoContentResolver(Context context, long noteId) {//将新的数据通过Uri的操作存储到数据库
/**
* Check for safety
*/
if (noteId <= 0) {
throw new IllegalArgumentException("Wrong note id:" + noteId);
}
}//判断数据是否合法
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
//定义了一个名为operationList的ArrayList这个ArrayList可以存储ContentProviderOperation对象初始化这个ArrayList。
//ContentProviderOperation是Android中用于表示内容提供者操作如插入、更新、删除等的类
ContentProviderOperation.Builder builder = null;
//Builder是Android框架中ContentProviderOperation类的一个内部类。Builder模式是一种设计模式它允许一个对象的构建与它的表示分离使得同样的构建过程可以创建不同的表示。
if(mTextDataValues.size() > 0) {
if(mTextDataValues.size() > 0) {//把文本数据存入DataColumns
mTextDataValues.put(DataColumns.NOTE_ID, noteId);
if (mTextDataId == 0) {
if (mTextDataId == 0) {//如果是首次插入数据
mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE);
Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI,
mTextDataValues);
mTextDataValues);//向指定的URL插入数据
try {
setTextDataId(Long.valueOf(uri.getPathSegments().get(1)));
} catch (NumberFormatException e) {
@ -202,16 +218,16 @@ public class Note {//单个便签项
mTextDataValues.clear();
return null;
}
} else {
} else {//如果数据已经存在,更新数据
builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mTextDataId));
builder.withValues(mTextDataValues);
operationList.add(builder.build());
operationList.add(builder.build());//加入操作列表
}
mTextDataValues.clear();
mTextDataValues.clear();//清空
}
if(mCallDataValues.size() > 0) {
if(mCallDataValues.size() > 0) {//把电话号码数据存入DataColumns
mCallDataValues.put(DataColumns.NOTE_ID, noteId);
if (mCallDataId == 0) {
mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE);
@ -236,13 +252,13 @@ public class Note {//单个便签项
if (operationList.size() > 0) {
try {
ContentProviderResult[] results = context.getContentResolver().applyBatch(
Notes.AUTHORITY, operationList);
Notes.AUTHORITY, operationList);//执行操作列表中的多个操作
return (results == null || results.length == 0 || results[0] == null) ? null
: ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId);
} catch (RemoteException e) {
} catch (RemoteException e) {//捕获RemoteException这是当尝试与远程服务如Content Provider通信或远程方法调用RPC时抛出表示远程对象调用失败。
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
} catch (OperationApplicationException e) {
} catch (OperationApplicationException e) {//OperationApplicationException通常表示应用层操作失败。
Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage()));
return null;
}

@ -124,11 +124,14 @@ public class WorkingNote {//当前活动便签项
loadNote();
}
// 加载Note
private void loadNote() {
// 通过数据库调用query函数找到第一个条目
Cursor cursor = mContext.getContentResolver().query(
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null,
null, null);
// 若存在,储存相应信息
if (cursor != null) {
if (cursor.moveToFirst()) {
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
@ -139,21 +142,26 @@ public class WorkingNote {//当前活动便签项
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN);
}
cursor.close();
// 若不存在,报错
} else {
Log.e(TAG, "No note with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
}
// 加载NoteData
loadNoteData();
}
// 加载NoteData
private void loadNoteData() {
// 通过数据库调用query函数找到第一个条目
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION,
DataColumns.NOTE_ID + "=?", new String[] {
String.valueOf(mNoteId)
}, null);
// 查到信息不为空
if (cursor != null) {
if (cursor.moveToFirst()) {
if (cursor.moveToFirst()) {// 查看第一项是否存在
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
if (DataConstants.NOTE.equals(type)) {
@ -165,15 +173,18 @@ public class WorkingNote {//当前活动便签项
} else {
Log.d(TAG, "Wrong note type with type:" + type);
}
} while (cursor.moveToNext());
} while (cursor.moveToNext());//查阅所有项,直到为空
}
cursor.close();
// 若不存在,报错
} else {
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
}
}
// 创建空的Note
// 传参context文件夹id小部件ID背景颜色
public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,
int widgetType, int defaultBgColorId) {
WorkingNote note = new WorkingNote(context, folderId);
@ -183,13 +194,15 @@ public class WorkingNote {//当前活动便签项
return note;
}
//构造函数是私有的。用load函数调用构造函数
public static WorkingNote load(Context context, long id) {
return new WorkingNote(context, id, 0);
}
// 保存Note
public synchronized boolean saveNote() {
if (isWorthSaving()) {
if (!existInDatabase()) {
if (isWorthSaving()) {//是否值得保存
if (!existInDatabase()) { // 是否存在数据库中
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) {
Log.e(TAG, "Create new note fail with id:" + mNoteId);
return false;
@ -200,6 +213,7 @@ public class WorkingNote {//当前活动便签项
/**
* Update widget content if there exist any widget of this note
*
*/
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
@ -212,11 +226,14 @@ public class WorkingNote {//当前活动便签项
}
}
// 是否在数据库中存在
public boolean existInDatabase() {
return mNoteId > 0;
}
// 是否值得保存
private boolean isWorthSaving() {
// 被删除,或(不在数据库中 内容为空),或本地已保存过
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|| (existInDatabase() && !mNote.isLocalModified())) {
return false;
@ -225,6 +242,7 @@ public class WorkingNote {//当前活动便签项
}
}
// 便签更新监视
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
mNoteSettingStatusListener = l;
}
@ -239,6 +257,7 @@ public class WorkingNote {//当前活动便签项
}
}
// 删除标记
public void markDeleted(boolean mark) {
mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
@ -247,6 +266,7 @@ public class WorkingNote {//当前活动便签项
}
}
//背景颜色
public void setBgColorId(int id) {
if (id != mBgColorId) {
mBgColorId = id;
@ -267,6 +287,7 @@ public class WorkingNote {//当前活动便签项
}
}
//小部件类型
public void setWidgetType(int type) {
if (type != mWidgetType) {
mWidgetType = type;
@ -274,6 +295,7 @@ public class WorkingNote {//当前活动便签项
}
}
//小部件ID
public void setWidgetId(int id) {
if (id != mWidgetId) {
mWidgetId = id;
@ -342,6 +364,8 @@ public class WorkingNote {//当前活动便签项
return mWidgetType;
}
// 创建接口 NoteSettingChangedListener,便签更新监视
// 为NoteEditActivity提供接口
public interface NoteSettingChangedListener {
/**
* Called when the background color of current note has just changed

Loading…
Cancel
Save