From 7a6e77b5afbbbb1a392598fa1aed950c8d725ca2 Mon Sep 17 00:00:00 2001 From: brook <2417392526@qq.com> Date: Thu, 4 Jan 2024 09:19:44 +0800 Subject: [PATCH] 1 --- src/net/micode/notes/gtask/data/SqlNote.java | 86 ++++++++++++++----- src/net/micode/notes/gtask/data/Task.java | 10 +-- src/net/micode/notes/gtask/data/TaskList.java | 24 ++++-- .../exception/ActionFailureException.java | 10 +++ .../exception/NetworkFailureException.java | 11 ++- .../notes/gtask/remote/GTaskASyncTask.java | 32 ++++--- .../notes/gtask/remote/GTaskClient.java | 9 +- .../notes/gtask/remote/GTaskManager.java | 21 +++-- .../notes/gtask/remote/GTaskSyncService.java | 7 +- 9 files changed, 155 insertions(+), 55 deletions(-) diff --git a/src/net/micode/notes/gtask/data/SqlNote.java b/src/net/micode/notes/gtask/data/SqlNote.java index 79a4095..56db713 100644 --- a/src/net/micode/notes/gtask/data/SqlNote.java +++ b/src/net/micode/notes/gtask/data/SqlNote.java @@ -13,7 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +/* + * Description:用于支持小米便签最底层的数据库相关操作,和sqldata的关系上是父集关系,即note是data的子父集。 + * 和SqlData相比,SqlNote算是真正意义上的数据了。 + */ package net.micode.notes.gtask.data; import android.appwidget.AppWidgetManager; @@ -37,12 +40,21 @@ import org.json.JSONObject; import java.util.ArrayList; +/* + * 功能描述: + * 实现过程: + + */ public class SqlNote { private static final String TAG = SqlNote.class.getSimpleName(); + /* + * 功能描述:得到类的简写名称存入字符串TAG中 + * 实现过程:调用getSimpleName ()函数 + */ private static final int INVALID_ID = -99999; - + // 集合了interface NoteColumns中所有SF常量(17个) public static final String[] PROJECTION_NOTE = new String[] { NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, @@ -51,7 +63,7 @@ public class SqlNote { NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, NoteColumns.VERSION }; - + //以下设置17个列的编号 public static final int ID_COLUMN = 0; public static final int ALERTED_DATE_COLUMN = 1; @@ -85,7 +97,7 @@ public class SqlNote { public static final int GTASK_ID_COLUMN = 15; public static final int VERSION_COLUMN = 16; - + //以下定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new private Context mContext; private ContentResolver mContentResolver; @@ -121,7 +133,12 @@ public class SqlNote { private ContentValues mDiffNoteValues; private ArrayList mDataList; + /* + * 功能描述:构造函数 + * 参数注解: mIsCreate用于标示构造方式 + */ + //构造函数只有context,对所有的变量进行初始化 public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -129,9 +146,9 @@ public class SqlNote { mId = INVALID_ID; mAlertDate = 0; mBgColorId = ResourceParser.getDefaultBgId(context); - mCreatedDate = System.currentTimeMillis(); + mCreatedDate = System.currentTimeMillis();//调用系统函数获得创建时间 mHasAttachment = 0; - mModifiedDate = System.currentTimeMillis(); + mModifiedDate = System.currentTimeMillis();//最后一次修改时间初始化为创建时间 mParentId = 0; mSnippet = ""; mType = Notes.TYPE_NOTE; @@ -142,6 +159,12 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); mDataList = new ArrayList(); } + /* + * 功能描述:构造函数 + * 参数注解: mIsCreate用于标示构造方式 + + */ + //构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化 public SqlNote(Context context, Cursor c) { mContext = context; @@ -153,7 +176,10 @@ public class SqlNote { loadDataContent(); mDiffNoteValues = new ContentValues(); } - + /* + * 功能描述:构造函数 + * 参数注解: mIsCreate用于标示构造方式 + */ public SqlNote(Context context, long id) { mContext = context; mContentResolver = context.getContentResolver(); @@ -173,9 +199,10 @@ public class SqlNote { new String[] { String.valueOf(id) }, null); - if (c != null) { + if (c != null) {//通过id获得对应的ContentResolver中的cursor c.moveToNext(); - loadFromCursor(c); + loadFromCursor(c);//然后加载数据进行初始化,这样函数 + //SqlNote(Context context, long id)与SqlNote(Context context, long id)的实现方式基本相同 } else { Log.w(TAG, "loadFromCursor: cursor = null"); } @@ -184,8 +211,11 @@ public class SqlNote { c.close(); } } - +/* + * 功能描述:通过游标从光标处加载数据 +*/ private void loadFromCursor(Cursor c) { + //直接从一条记录中的获得以下变量的初始值 mId = c.getLong(ID_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); @@ -199,7 +229,9 @@ public class SqlNote { mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); mVersion = c.getLong(VERSION_COLUMN); } - +/* + * 功能描述:通过content机制获取共享数据并加载到数据库当前游标处 + * 参数注解:*/ private void loadDataContent() { Cursor c = null; mDataList.clear(); @@ -225,7 +257,8 @@ public class SqlNote { c.close(); } } - +/* + * 功能描述:设置通过content机制用于共享的数据信息*/ public boolean setContent(JSONObject js) { try { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); @@ -359,6 +392,8 @@ public class SqlNote { return true; } + /* + * 功能描述:获取content机制提供的数据并加载到note中*/ public JSONObject getContent() { try { JSONObject js = new JSONObject(); @@ -406,40 +441,49 @@ public class SqlNote { } return null; } - +/* + * 功能描述:给当前id设置父id*/ public void setParentId(long id) { mParentId = id; mDiffNoteValues.put(NoteColumns.PARENT_ID, id); } - +/* + * 功能描述:给当前id设置Gtaskid*/ public void setGtaskId(String gid) { mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); } - +/* + * 功能描述:给当前id设置同步id*/ public void setSyncId(long syncId) { mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); } - +/* + * 功能描述:初始化本地修改,即撤销所有当前修改*/ public void resetLocalModified() { mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); } - + /* + * 功能描述:获得当前id*/ public long getId() { return mId; } - + /* + * 功能描述:获得当前id的父id*/ public long getParentId() { return mParentId; } - +/* + * 功能描述:获取小片段即用于显示的部分便签内容*/ public String getSnippet() { return mSnippet; } - + /* + * 功能描述:判断是否为便签类型*/ public boolean isNoteType() { return mType == Notes.TYPE_NOTE; } - + /* + * 功能描述:commit函数用于把当前造作所做的修改保存到数据库*/ public void commit(boolean validateVersion) { if (mIsCreate) { if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { diff --git a/src/net/micode/notes/gtask/data/Task.java b/src/net/micode/notes/gtask/data/Task.java index 6a19454..2e5d477 100644 --- a/src/net/micode/notes/gtask/data/Task.java +++ b/src/net/micode/notes/gtask/data/Task.java @@ -35,22 +35,22 @@ import org.json.JSONObject; public class Task extends Node { private static final String TAG = Task.class.getSimpleName(); - private boolean mCompleted; + private boolean mCompleted;//是否完成 private String mNotes; - private JSONObject mMetaInfo; + private JSONObject mMetaInfo;//将在实例中存储数据的类型 private Task mPriorSibling; - private TaskList mParent; + private TaskList mParent;//所在的任务列表的指针 public Task() { super(); mCompleted = false; mNotes = null; - mPriorSibling = null; - mParent = null; + mPriorSibling = null;//TaskList中当前Task前面的Task的指针 + mParent = null;//当前Task所在的TaskList mMetaInfo = null; } diff --git a/src/net/micode/notes/gtask/data/TaskList.java b/src/net/micode/notes/gtask/data/TaskList.java index 4ea21c5..462dbc5 100644 --- a/src/net/micode/notes/gtask/data/TaskList.java +++ b/src/net/micode/notes/gtask/data/TaskList.java @@ -31,11 +31,11 @@ import java.util.ArrayList; public class TaskList extends Node { - private static final String TAG = TaskList.class.getSimpleName(); + private static final String TAG = TaskList.class.getSimpleName();//tag标记 - private int mIndex; + private int mIndex;//当前TaskList的指针 - private ArrayList mChildren; + private ArrayList mChildren;//类中主要的保存数据的单元,用来实现一个以Task为元素的ArrayList public TaskList() { super(); @@ -43,6 +43,7 @@ public class TaskList extends Node { mIndex = 1; } + //生成并返回一个包含了一定数据的JSONObject实体 public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); @@ -74,6 +75,8 @@ public class TaskList extends Node { return js; } + + //生成并返回一个包含了一定数据的JSONObject实体 public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); @@ -216,6 +219,8 @@ public class TaskList extends Node { return SYNC_ACTION_ERROR; } + + //功能:获得TaskList的大小,即mChildren的大小 public int getChildTaskCount() { return mChildren.size(); } @@ -233,7 +238,7 @@ public class TaskList extends Node { } return ret; } - + //功能:在当前任务表的指定位置添加新的任务 public boolean addChildTask(Task task, int index) { if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); @@ -260,6 +265,7 @@ public class TaskList extends Node { return true; } + // 功能:删除TaskList中的一个Task public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); @@ -281,6 +287,7 @@ public class TaskList extends Node { return ret; } + //功能:将当前TaskList中含有的某个Task移到index位置 public boolean moveChildTask(Task task, int index) { if (index < 0 || index >= mChildren.size()) { @@ -298,7 +305,10 @@ public class TaskList extends Node { return true; return (removeChildTask(task) && addChildTask(task, index)); } + //利用已实现好的功能完成当下功能; + + // 功能:按gid寻找Task public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -308,11 +318,13 @@ public class TaskList extends Node { } return null; } - + //功能:返回指定Task的index public int getChildTaskIndex(Task task) { return mChildren.indexOf(task); } + + //功能:返回指定index的Task public Task getChildTaskByIndex(int index) { if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "getTaskByIndex: invalid index"); @@ -321,6 +333,8 @@ public class TaskList extends Node { return mChildren.get(index); } + +//功能:返回指定index的Task public Task getChilTaskByGid(String gid) { for (Task task : mChildren) { if (task.getGid().equals(gid)) diff --git a/src/net/micode/notes/gtask/exception/ActionFailureException.java b/src/net/micode/notes/gtask/exception/ActionFailureException.java index 15504be..b2c0c73 100644 --- a/src/net/micode/notes/gtask/exception/ActionFailureException.java +++ b/src/net/micode/notes/gtask/exception/ActionFailureException.java @@ -14,14 +14,24 @@ * limitations under the License. */ + +// 支持小米便签运行过程中的运行异常处理 package net.micode.notes.gtask.exception; public class ActionFailureException extends RuntimeException { private static final long serialVersionUID = 4425249765923293627L; + /* + * serialVersionUID相当于java类的身份证。主要用于版本控制。 + * serialVersionUID作用是序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。 + */ public ActionFailureException() { super(); } + /* + * 在JAVA类中使用super来引用父类的成分,用this来引用当前对象. + * 如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象 + */ public ActionFailureException(String paramString) { super(paramString); diff --git a/src/net/micode/notes/gtask/exception/NetworkFailureException.java b/src/net/micode/notes/gtask/exception/NetworkFailureException.java index b08cfb1..fe9d511 100644 --- a/src/net/micode/notes/gtask/exception/NetworkFailureException.java +++ b/src/net/micode/notes/gtask/exception/NetworkFailureException.java @@ -13,11 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +/* + * Description:支持小米便签运行过程中的网络异常处理。 + */ package net.micode.notes.gtask.exception; public class NetworkFailureException extends Exception { private static final long serialVersionUID = 2107610287180234136L; +// serialVersionUID作用是序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。 + + + /* + * 在JAVA类中使用super来引用父类的成分,用this来引用当前对象. + * 如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象。 + */ public NetworkFailureException() { super(); diff --git a/src/net/micode/notes/gtask/remote/GTaskASyncTask.java b/src/net/micode/notes/gtask/remote/GTaskASyncTask.java index b3b61e7..ca74cc6 100644 --- a/src/net/micode/notes/gtask/remote/GTaskASyncTask.java +++ b/src/net/micode/notes/gtask/remote/GTaskASyncTask.java @@ -28,6 +28,13 @@ import net.micode.notes.R; import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesPreferenceActivity; +/*异步操作类,实现GTask的异步操作过程 + * 主要方法: + * private void showNotification(int tickerId, String content) 向用户提示当前同步的状态,是一个用于交互的方法 + * protected Integer doInBackground(Void... unused) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间 + * protected void onProgressUpdate(String... progress) 可以使用进度条增加用户体验度。 此方法在主线程执行,用于显示任务执行的进度。 + * protected void onPostExecute(Integer result) 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI + */ public class GTaskASyncTask extends AsyncTask { @@ -57,7 +64,7 @@ public class GTaskASyncTask extends AsyncTask { mTaskManager.cancelSync(); } - public void publishProgess(String message) { + public void publishProgess(String message) {// 发布进度单位,系统将会调用onProgressUpdate()方法更新这些值 publishProgress(new String[] { message }); @@ -66,30 +73,29 @@ public class GTaskASyncTask extends AsyncTask { private void showNotification(int tickerId, String content) { Notification notification = new Notification(R.drawable.notification, mContext .getString(tickerId), System.currentTimeMillis()); - notification.defaults = Notification.DEFAULT_LIGHTS; + notification.defaults = Notification.DEFAULT_LIGHTS;// 调用系统自带灯光 notification.flags = Notification.FLAG_AUTO_CANCEL; PendingIntent pendingIntent; if (tickerId != R.string.ticker_success) { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesPreferenceActivity.class), 0); - + NotesPreferenceActivity.class), 0);//如果同步不成功,那么从系统取得一个用于启动一个NotesPreferenceActivity的PendingIntent对象 } else { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, - NotesListActivity.class), 0); + NotesListActivity.class), 0);//如果同步成功,那么从系统取得一个用于启动一个NotesListActivity的PendingIntent对象 } - notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, - pendingIntent); - mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); + // notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, + // pendingIntent); + mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);//通过NotificationManager对象的notify()方法来执行一个notification的消息 } - @Override + // @Override protected Integer doInBackground(Void... unused) { publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity .getSyncAccountName(mContext))); - return mTaskManager.sync(mContext, this); + return mTaskManager.sync(mContext, this); //进行后台同步具体操作 } - @Override + // @Override protected void onProgressUpdate(String... progress) { showNotification(R.string.ticker_syncing, progress[0]); if (mContext instanceof GTaskSyncService) { @@ -97,8 +103,8 @@ public class GTaskASyncTask extends AsyncTask { } } - @Override - protected void onPostExecute(Integer result) { + //@Override + protected void onPostExecute(Integer result) {//用于在执行完后台任务后更新UI,显示结果 if (result == GTaskManager.STATE_SUCCESS) { showNotification(R.string.ticker_success, mContext.getString( R.string.success_sync_account, mTaskManager.getSyncAccount())); diff --git a/src/net/micode/notes/gtask/remote/GTaskClient.java b/src/net/micode/notes/gtask/remote/GTaskClient.java index c67dfdf..18dbaf2 100644 --- a/src/net/micode/notes/gtask/remote/GTaskClient.java +++ b/src/net/micode/notes/gtask/remote/GTaskClient.java @@ -60,7 +60,10 @@ import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; - +/* + * 主要功能:实现GTASK的登录操作,进行GTASK任务的创建,创建任务列表,从网络上获取任务和任务列表的内容 + * 主要使用类或技术:accountManager JSONObject HttpParams authToken Gid + */ public class GTaskClient { private static final String TAG = GTaskClient.class.getSimpleName(); @@ -102,6 +105,10 @@ public class GTaskClient { mUpdateArray = null; } + /*用来获取的实例化对象 + * 使用 getInstance() + * 返回mInstance这个实例化对象 + */ public static synchronized GTaskClient getInstance() { if (mInstance == null) { mInstance = new GTaskClient(); diff --git a/src/net/micode/notes/gtask/remote/GTaskManager.java b/src/net/micode/notes/gtask/remote/GTaskManager.java index d2b4082..c20e177 100644 --- a/src/net/micode/notes/gtask/remote/GTaskManager.java +++ b/src/net/micode/notes/gtask/remote/GTaskManager.java @@ -87,9 +87,9 @@ public class GTaskManager { private HashMap mNidToGid; - private GTaskManager() { - mSyncing = false; - mCancelled = false; + private GTaskManager() { //对象初始化函数 + mSyncing = false; //正在同步,flase代表未执行 + mCancelled = false; //全局标识,flase代表可以执行 mGTaskListHashMap = new HashMap(); mGTaskHashMap = new HashMap(); mMetaHashMap = new HashMap(); @@ -113,7 +113,7 @@ public class GTaskManager { public int sync(Context context, GTaskASyncTask asyncTask) { if (mSyncing) { - Log.d(TAG, "Sync is in progress"); + Log.d(TAG, "Sync is in progress");//创建日志文件(调试信息),debug return STATE_SYNC_IN_PROGRESS; } mContext = context; @@ -128,7 +128,7 @@ public class GTaskManager { mNidToGid.clear(); try { - GTaskClient client = GTaskClient.getInstance(); + GTaskClient client = GTaskClient.getInstance();//getInstance即为创建一个实例,client--客户机 client.resetUpdateArray(); // login google task @@ -140,7 +140,7 @@ public class GTaskManager { // get the task list from google asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); - initGTaskList(); + initGTaskList(); //获取Google上的JSONtasklist转为本地TaskList // do content sync work asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); @@ -168,12 +168,17 @@ public class GTaskManager { return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS; } + +/* + *功能:初始化GtaskList,获取Google上的JSONtasklist转为本地TaskList。 + *获得的数据存储在mMetaList,mGTaskListHashMap,mGTaskHashMap + */ private void initGTaskList() throws NetworkFailureException { if (mCancelled) return; GTaskClient client = GTaskClient.getInstance(); try { - JSONArray jsTaskLists = client.getTaskLists(); + JSONArray jsTaskLists = client.getTaskLists(); //getInstance即为创建一个实例,client应指远端客户机 // init meta list first mMetaList = null; @@ -247,6 +252,8 @@ public class GTaskManager { } } + + //功能:本地内容同步操作 private void syncContent() throws NetworkFailureException { int syncType; Cursor c = null; diff --git a/src/net/micode/notes/gtask/remote/GTaskSyncService.java b/src/net/micode/notes/gtask/remote/GTaskSyncService.java index cca36f7..275606b 100644 --- a/src/net/micode/notes/gtask/remote/GTaskSyncService.java +++ b/src/net/micode/notes/gtask/remote/GTaskSyncService.java @@ -42,6 +42,8 @@ public class GTaskSyncService extends Service { private static String mSyncProgress = ""; + + ////开始一个同步的工作 private void startSync() { if (mSyncTask == null) { mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { @@ -65,13 +67,14 @@ public class GTaskSyncService extends Service { @Override public void onCreate() { mSyncTask = null; - } + }///初始化一个service @Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle bundle = intent.getExtras(); if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) { switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) { + //两种情况,开始同步或者取消同步 case ACTION_START_SYNC: startSync(); break; @@ -81,7 +84,7 @@ public class GTaskSyncService extends Service { default: break; } - return START_STICKY; + return START_STICKY;//等待新的intent来是这个service继续运行 } return super.onStartCommand(intent, flags, startId); }